* elf64-alpha.c (elf64_alpha_relocate_section): Soft fail
[binutils.git] / ld / pe-dll.c
blobe47a17fd3beebfb3edaf4d277c82fd9400eb429d
1 /* Routines to help build PEI-format DLLs (Win32 etc)
2 Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 Written by DJ Delorie <dj@cygnus.com>
5 This file is part of GLD, the Gnu Linker.
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libiberty.h"
27 #include <time.h>
28 #include <ctype.h>
30 #include "ld.h"
31 #include "ldexp.h"
32 #include "ldlang.h"
33 #include "ldwrite.h"
34 #include "ldmisc.h"
35 #include "ldgram.h"
36 #include "ldmain.h"
37 #include "ldfile.h"
38 #include "ldemul.h"
39 #include "coff/internal.h"
40 #include "../bfd/libcoff.h"
41 #include "deffile.h"
42 #include "pe-dll.h"
44 /************************************************************************
46 This file turns a regular Windows PE image into a DLL. Because of
47 the complexity of this operation, it has been broken down into a
48 number of separate modules which are all called by the main function
49 at the end of this file. This function is not re-entrant and is
50 normally only called once, so static variables are used to reduce
51 the number of parameters and return values required.
53 See also: ld/emultempl/pe.em
55 ************************************************************************/
57 /************************************************************************
59 Auto-import feature by Paul Sokolovsky
61 Quick facts:
63 1. With this feature on, DLL clients can import variables from DLL
64 without any concern from their side (for example, without any source
65 code modifications).
67 2. This is done completely in bounds of the PE specification (to be fair,
68 there's a place where it pokes nose out of, but in practise it works).
69 So, resulting module can be used with any other PE compiler/linker.
71 3. Auto-import is fully compatible with standard import method and they
72 can be mixed together.
74 4. Overheads: space: 8 bytes per imported symbol, plus 20 for each
75 reference to it; load time: negligible; virtual/physical memory: should be
76 less than effect of DLL relocation, and I sincerely hope it doesn't affect
77 DLL sharability (too much).
79 Idea
81 The obvious and only way to get rid of dllimport insanity is to make client
82 access variable directly in the DLL, bypassing extra dereference. I.e.,
83 whenever client contains someting like
85 mov dll_var,%eax,
87 address of dll_var in the command should be relocated to point into loaded
88 DLL. The aim is to make OS loader do so, and than make ld help with that.
89 Import section of PE made following way: there's a vector of structures
90 each describing imports from particular DLL. Each such structure points
91 to two other parellel vectors: one holding imported names, and one which
92 will hold address of corresponding imported name. So, the solution is
93 de-vectorize these structures, making import locations be sparse and
94 pointing directly into code. Before continuing, it is worth a note that,
95 while authors strives to make PE act ELF-like, there're some other people
96 make ELF act PE-like: elfvector, ;-) .
98 Implementation
100 For each reference of data symbol to be imported from DLL (to set of which
101 belong symbols with name <sym>, if __imp_<sym> is found in implib), the
102 import fixup entry is generated. That entry is of type
103 IMAGE_IMPORT_DESCRIPTOR and stored in .idata$3 subsection. Each
104 fixup entry contains pointer to symbol's address within .text section
105 (marked with __fuN_<sym> symbol, where N is integer), pointer to DLL name
106 (so, DLL name is referenced by multiple entries), and pointer to symbol
107 name thunk. Symbol name thunk is singleton vector (__nm_th_<symbol>)
108 pointing to IMAGE_IMPORT_BY_NAME structure (__nm_<symbol>) directly
109 containing imported name. Here comes that "om the edge" problem mentioned
110 above: PE specification rambles that name vector (OriginalFirstThunk)
111 should run in parallel with addresses vector (FirstThunk), i.e. that they
112 should have same number of elements and terminated with zero. We violate
113 this, since FirstThunk points directly into machine code. But in practise,
114 OS loader implemented the sane way: it goes thru OriginalFirstThunk and
115 puts addresses to FirstThunk, not something else. It once again should be
116 noted that dll and symbol name structures are reused across fixup entries
117 and should be there anyway to support standard import stuff, so sustained
118 overhead is 20 bytes per reference. Other question is whether having several
119 IMAGE_IMPORT_DESCRIPTORS for the same DLL is possible. Answer is yes, it is
120 done even by native compiler/linker (libth32's functions are in fact reside
121 in windows9x kernel32.dll, so if you use it, you have two
122 IMAGE_IMPORT_DESCRIPTORS for kernel32.dll). Yet other question is whether
123 referencing the same PE structures several times is valid. The answer is why
124 not, prohibitting that (detecting violation) would require more work on
125 behalf of loader than not doing it.
128 See also: ld/emultempl/pe.em
130 ************************************************************************/
132 static void
133 add_bfd_to_link (bfd *abfd, CONST char *name,
134 struct bfd_link_info *link_info);
136 /* for emultempl/pe.em */
138 def_file *pe_def_file = 0;
139 int pe_dll_export_everything = 0;
140 int pe_dll_do_default_excludes = 1;
141 int pe_dll_kill_ats = 0;
142 int pe_dll_stdcall_aliases = 0;
143 int pe_dll_warn_dup_exports = 0;
144 int pe_dll_compat_implib = 0;
145 int pe_dll_extra_pe_debug = 0;
147 /************************************************************************
149 static variables and types
151 ************************************************************************/
153 static bfd_vma image_base;
155 static bfd *filler_bfd;
156 static struct sec *edata_s, *reloc_s;
157 static unsigned char *edata_d, *reloc_d;
158 static size_t edata_sz, reloc_sz;
160 typedef struct {
161 char *target_name;
162 char *object_target;
163 unsigned int imagebase_reloc;
164 int pe_arch;
165 int bfd_arch;
166 int underscored;
167 } pe_details_type;
169 typedef struct {
170 char *name;
171 int len;
172 } autofilter_entry_type;
174 #define PE_ARCH_i386 1
175 #define PE_ARCH_sh 2
176 #define PE_ARCH_mips 3
177 #define PE_ARCH_arm 4
179 static pe_details_type pe_detail_list[] = {
181 "pei-i386",
182 "pe-i386",
183 7 /* R_IMAGEBASE */,
184 PE_ARCH_i386,
185 bfd_arch_i386,
189 "pei-shl",
190 "pe-shl",
191 16 /* R_SH_IMAGEBASE */,
192 PE_ARCH_sh,
193 bfd_arch_sh,
197 "pei-mips",
198 "pe-mips",
199 34 /* MIPS_R_RVA */,
200 PE_ARCH_mips,
201 bfd_arch_mips,
205 "pei-arm-little",
206 "pe-arm-little",
207 11 /* ARM_RVA32 */,
208 PE_ARCH_arm,
209 bfd_arch_arm,
212 { NULL, NULL, 0, 0, 0, 0 }
215 static pe_details_type *pe_details;
217 static autofilter_entry_type autofilter_symbollist[] = {
218 { "DllMain@12", 10 },
219 { "DllEntryPoint@0", 15 },
220 { "DllMainCRTStartup@12", 20 },
221 { "_cygwin_dll_entry@12", 20 },
222 { "_cygwin_crt0_common@8", 21 },
223 { "_cygwin_noncygwin_dll_entry@12", 30 },
224 { "impure_ptr", 10 },
225 { NULL, 0 }
227 /* Do not specify library suffix explicitly, to allow for dllized versions */
228 static autofilter_entry_type autofilter_liblist[] = {
229 { "libgcc.", 7 },
230 { "libstdc++.", 10 },
231 { "libmingw32.", 11 },
232 { NULL, 0 }
234 static autofilter_entry_type autofilter_objlist[] = {
235 { "crt0.o", 6 },
236 { "crt1.o", 6 },
237 { "crt2.o", 6 },
238 { NULL, 0 }
240 static autofilter_entry_type autofilter_symbolprefixlist[] = {
241 /* { "__imp_", 6 }, */
242 /* Do __imp_ explicitly to save time */
243 { "__rtti_", 7 },
244 { "__builtin_", 10 },
245 { "_head_", 6 }, /* don't export symbols specifying internal DLL layout */
246 { "_fmode", 6 },
247 { "_impure_ptr", 11 },
248 { "cygwin_attach_dll", 17 },
249 { "cygwin_premain0", 15 },
250 { "cygwin_premain1", 15 },
251 { "cygwin_premain2", 15 },
252 { "cygwin_premain3", 15 },
253 { "environ", 7 },
254 { NULL, 0 }
256 static autofilter_entry_type autofilter_symbolsuffixlist[] = {
257 { "_iname", 6 },
258 { NULL, 0 }
261 #define U(str) (pe_details->underscored ? "_" str : str)
263 static int reloc_sort PARAMS ((const void *, const void *));
264 static int pe_export_sort PARAMS ((const void *, const void *));
265 static int auto_export PARAMS ((bfd *, def_file *, const char *));
266 static void process_def_file PARAMS ((bfd *, struct bfd_link_info *));
267 static void build_filler_bfd PARAMS ((int));
268 static void generate_edata PARAMS ((bfd *, struct bfd_link_info *));
269 static void fill_exported_offsets PARAMS ((bfd *, struct bfd_link_info *));
270 static void fill_edata PARAMS ((bfd *, struct bfd_link_info *));
271 static void generate_reloc PARAMS ((bfd *, struct bfd_link_info *));
272 static void quoteput PARAMS ((char *, FILE *, int));
273 static asection *quick_section PARAMS ((bfd *, const char *, int, int));
274 static void quick_symbol
275 PARAMS ((bfd *, char *, char *, char *, asection *, int, int));
276 static void quick_reloc PARAMS ((bfd *, int, int, int));
277 static bfd *make_head PARAMS ((bfd *));
278 static bfd *make_tail PARAMS ((bfd *));
279 static bfd *make_one PARAMS ((def_file_export *, bfd *));
280 static bfd *make_singleton_name_thunk PARAMS ((char *, bfd *));
281 static char *make_import_fixup_mark PARAMS ((arelent *));
282 static bfd *make_import_fixup_entry PARAMS ((char *, char *, char *, bfd *));
283 static unsigned int pe_get16 PARAMS ((bfd *, int));
284 static unsigned int pe_get32 PARAMS ((bfd *, int));
285 static unsigned int pe_as32 PARAMS ((void *));
287 void
288 pe_dll_id_target (target)
289 const char *target;
291 int i;
292 for (i = 0; pe_detail_list[i].target_name; i++)
293 if (strcmp (pe_detail_list[i].target_name, target) == 0
294 || strcmp (pe_detail_list[i].object_target, target) == 0)
296 pe_details = pe_detail_list + i;
297 return;
299 einfo (_("%XUnsupported PEI architecture: %s\n"), target);
300 exit (1);
303 /************************************************************************
305 Helper functions for qsort. Relocs must be sorted so that we can write
306 them out by pages.
308 ************************************************************************/
310 typedef struct {
311 bfd_vma vma;
312 char type;
313 short extra;
314 } reloc_data_type;
316 static int
317 reloc_sort (va, vb)
318 const void *va, *vb;
320 bfd_vma a = ((reloc_data_type *) va)->vma;
321 bfd_vma b = ((reloc_data_type *) vb)->vma;
322 return (a > b) ? 1 : ((a < b) ? -1 : 0);
325 static int
326 pe_export_sort (va, vb)
327 const void *va, *vb;
329 def_file_export *a = (def_file_export *) va;
330 def_file_export *b = (def_file_export *) vb;
331 return strcmp (a->name, b->name);
334 /************************************************************************
336 Read and process the .DEF file
338 ************************************************************************/
340 /* These correspond to the entries in pe_def_file->exports[]. I use
341 exported_symbol_sections[i] to tag whether or not the symbol was
342 defined, since we can't export symbols we don't have. */
344 static bfd_vma *exported_symbol_offsets;
345 static struct sec **exported_symbol_sections;
347 static int export_table_size;
348 static int count_exported;
349 static int count_exported_byname;
350 static int count_with_ordinals;
351 static const char *dll_name;
352 static int min_ordinal, max_ordinal;
353 static int *exported_symbols;
355 typedef struct exclude_list_struct {
356 char *string;
357 struct exclude_list_struct *next;
358 } exclude_list_struct;
360 static struct exclude_list_struct *excludes = 0;
362 void
363 pe_dll_add_excludes (new_excludes)
364 const char *new_excludes;
366 char *local_copy;
367 char *exclude_string;
369 local_copy = xstrdup (new_excludes);
371 exclude_string = strtok (local_copy, ",:");
372 for (; exclude_string; exclude_string = strtok (NULL, ",:"))
374 struct exclude_list_struct *new_exclude;
376 new_exclude = ((struct exclude_list_struct *)
377 xmalloc (sizeof (struct exclude_list_struct)));
378 new_exclude->string = (char *) xmalloc (strlen (exclude_string) + 1);
379 strcpy (new_exclude->string, exclude_string);
380 new_exclude->next = excludes;
381 excludes = new_exclude;
384 free (local_copy);
388 abfd is a bfd containing n (or NULL)
389 It can be used for contextual checks.
391 static int
392 auto_export (abfd, d, n)
393 bfd *abfd;
394 def_file *d;
395 const char *n;
397 int i;
398 struct exclude_list_struct *ex;
399 autofilter_entry_type *afptr;
401 /* we should not re-export imported stuff */
402 if (strncmp (n, "_imp__", 6) == 0)
403 return 0;
405 for (i = 0; i < d->num_exports; i++)
406 if (strcmp (d->exports[i].name, n) == 0)
407 return 0;
408 if (pe_dll_do_default_excludes)
410 if (pe_dll_extra_pe_debug)
412 printf ("considering exporting: %s, abfd=%p, abfd->my_arc=%p\n",
413 n, abfd, abfd->my_archive);
416 /* First of all, make context checks:
417 Don't export anything from libgcc */
418 if (abfd && abfd->my_archive)
420 afptr = autofilter_liblist;
421 while (afptr->name)
423 if (strstr (abfd->my_archive->filename, afptr->name))
424 return 0;
425 afptr++;
429 /* Next, exclude symbols from certain startup objects */
431 char *p;
432 afptr = autofilter_objlist;
433 while (afptr->name)
435 if (abfd &&
436 (p = strstr (abfd->filename, afptr->name)) &&
437 (*(p + afptr->len - 1) == 0))
438 return 0;
439 afptr++;
443 /* Don't try to blindly exclude all symbols
444 that begin with '__'; this was tried and
445 it is too restrictive */
447 /* Then, exclude specific symbols */
448 afptr = autofilter_symbollist;
449 while (afptr->name)
451 if (strcmp (n, afptr->name) == 0)
452 return 0;
453 afptr++;
456 /* Next, exclude symbols starting with ... */
457 afptr = autofilter_symbolprefixlist;
458 while (afptr->name)
460 if (strncmp (n, afptr->name, afptr->len) == 0)
461 return 0;
462 afptr++;
465 /* Finally, exclude symbols ending with ... */
467 int len = strlen(n);
468 afptr = autofilter_symbolsuffixlist;
469 while (afptr->name)
471 if ((len >= afptr->len) &&
472 /* add 1 to insure match with trailing '\0' */
473 strncmp (n + len - afptr->len, afptr->name,
474 afptr->len + 1) == 0)
475 return 0;
476 afptr++;
480 for (ex = excludes; ex; ex = ex->next)
481 if (strcmp (n, ex->string) == 0)
482 return 0;
483 return 1;
486 static void
487 process_def_file (abfd, info)
488 bfd *abfd ATTRIBUTE_UNUSED;
489 struct bfd_link_info *info;
491 int i, j;
492 struct bfd_link_hash_entry *blhe;
493 bfd *b;
494 struct sec *s;
495 def_file_export *e = 0;
497 if (!pe_def_file)
498 pe_def_file = def_file_empty ();
500 /* First, run around to all the objects looking for the .drectve
501 sections, and push those into the def file too. */
503 for (b = info->input_bfds; b; b = b->link_next)
505 s = bfd_get_section_by_name (b, ".drectve");
506 if (s)
508 int size = bfd_get_section_size_before_reloc (s);
509 char *buf = xmalloc (size);
510 bfd_get_section_contents (b, s, buf, 0, size);
511 def_file_add_directive (pe_def_file, buf, size);
512 free (buf);
516 /* Now, maybe export everything else the default way. */
518 if (pe_dll_export_everything || pe_def_file->num_exports == 0)
520 for (b = info->input_bfds; b; b = b->link_next)
522 asymbol **symbols;
523 int nsyms, symsize;
525 symsize = bfd_get_symtab_upper_bound (b);
526 symbols = (asymbol **) xmalloc (symsize);
527 nsyms = bfd_canonicalize_symtab (b, symbols);
529 for (j = 0; j < nsyms; j++)
531 /* We should export symbols which are either global or not
532 anything at all. (.bss data is the latter)
533 We should not export undefined symbols
535 if (symbols[j]->section != &bfd_und_section
536 && ((symbols[j]->flags & BSF_GLOBAL)
537 || (symbols[j]->flags == BFD_FORT_COMM_DEFAULT_VALUE)))
539 const char *sn = symbols[j]->name;
541 /* we should not re-export imported stuff */
543 char *name = (char *) xmalloc (strlen (sn) + 2 + 6);
544 sprintf (name, "%s%s", U("_imp_"), sn);
545 blhe = bfd_link_hash_lookup (info->hash, name,
546 false, false, false);
547 free (name);
549 if (blhe && blhe->type == bfd_link_hash_defined)
550 continue;
553 if (*sn == '_')
554 sn++;
555 if (auto_export (b, pe_def_file, sn))
557 def_file_export *p;
558 p=def_file_add_export (pe_def_file, sn, 0, -1);
559 /* Fill data flag properly, from dlltool.c */
560 p->flag_data = !(symbols[j]->flags & BSF_FUNCTION);
567 #undef NE
568 #define NE pe_def_file->num_exports
570 /* Canonicalize the export list. */
572 if (pe_dll_kill_ats)
574 for (i = 0; i < NE; i++)
576 if (strchr (pe_def_file->exports[i].name, '@'))
578 /* This will preserve internal_name, which may have been
579 pointing to the same memory as name, or might not
580 have. */
581 char *tmp = xstrdup (pe_def_file->exports[i].name);
582 *(strchr (tmp, '@')) = 0;
583 pe_def_file->exports[i].name = tmp;
588 if (pe_dll_stdcall_aliases)
590 for (i = 0; i < NE; i++)
592 if (strchr (pe_def_file->exports[i].name, '@'))
594 char *tmp = xstrdup (pe_def_file->exports[i].name);
595 *(strchr (tmp, '@')) = 0;
596 if (auto_export (NULL, pe_def_file, tmp))
597 def_file_add_export (pe_def_file, tmp,
598 pe_def_file->exports[i].internal_name,
599 -1);
600 else
601 free (tmp);
606 /* Convenience, but watch out for it changing. */
607 e = pe_def_file->exports;
609 exported_symbol_offsets = (bfd_vma *) xmalloc (NE * sizeof (bfd_vma));
610 exported_symbol_sections = (struct sec **) xmalloc (NE * sizeof (struct sec *));
612 memset (exported_symbol_sections, 0, NE * sizeof (struct sec *));
613 max_ordinal = 0;
614 min_ordinal = 65536;
615 count_exported = 0;
616 count_exported_byname = 0;
617 count_with_ordinals = 0;
619 qsort (pe_def_file->exports, NE, sizeof (pe_def_file->exports[0]), pe_export_sort);
620 for (i = 0, j = 0; i < NE; i++)
622 if (i > 0 && strcmp (e[i].name, e[i - 1].name) == 0)
624 /* This is a duplicate. */
625 if (e[j - 1].ordinal != -1
626 && e[i].ordinal != -1
627 && e[j - 1].ordinal != e[i].ordinal)
629 if (pe_dll_warn_dup_exports)
630 /* xgettext:c-format */
631 einfo (_("%XError, duplicate EXPORT with ordinals: %s (%d vs %d)\n"),
632 e[j - 1].name, e[j - 1].ordinal, e[i].ordinal);
634 else
636 if (pe_dll_warn_dup_exports)
637 /* xgettext:c-format */
638 einfo (_("Warning, duplicate EXPORT: %s\n"),
639 e[j - 1].name);
641 if (e[i].ordinal != -1)
642 e[j - 1].ordinal = e[i].ordinal;
643 e[j - 1].flag_private |= e[i].flag_private;
644 e[j - 1].flag_constant |= e[i].flag_constant;
645 e[j - 1].flag_noname |= e[i].flag_noname;
646 e[j - 1].flag_data |= e[i].flag_data;
648 else
650 if (i != j)
651 e[j] = e[i];
652 j++;
655 pe_def_file->num_exports = j; /* == NE */
657 for (i = 0; i < NE; i++)
659 char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
660 if (pe_details->underscored)
662 *name = '_';
663 strcpy (name + 1, pe_def_file->exports[i].internal_name);
665 else
666 strcpy (name, pe_def_file->exports[i].internal_name);
668 blhe = bfd_link_hash_lookup (info->hash,
669 name,
670 false, false, true);
672 if (blhe
673 && (blhe->type == bfd_link_hash_defined
674 || (blhe->type == bfd_link_hash_common)))
676 count_exported++;
677 if (!pe_def_file->exports[i].flag_noname)
678 count_exported_byname++;
680 /* Only fill in the sections. The actual offsets are computed
681 in fill_exported_offsets() after common symbols are laid
682 out. */
683 if (blhe->type == bfd_link_hash_defined)
684 exported_symbol_sections[i] = blhe->u.def.section;
685 else
686 exported_symbol_sections[i] = blhe->u.c.p->section;
688 if (pe_def_file->exports[i].ordinal != -1)
690 if (max_ordinal < pe_def_file->exports[i].ordinal)
691 max_ordinal = pe_def_file->exports[i].ordinal;
692 if (min_ordinal > pe_def_file->exports[i].ordinal)
693 min_ordinal = pe_def_file->exports[i].ordinal;
694 count_with_ordinals++;
697 else if (blhe && blhe->type == bfd_link_hash_undefined)
699 /* xgettext:c-format */
700 einfo (_("%XCannot export %s: symbol not defined\n"),
701 pe_def_file->exports[i].internal_name);
703 else if (blhe)
705 /* xgettext:c-format */
706 einfo (_("%XCannot export %s: symbol wrong type (%d vs %d)\n"),
707 pe_def_file->exports[i].internal_name,
708 blhe->type, bfd_link_hash_defined);
710 else
712 /* xgettext:c-format */
713 einfo (_("%XCannot export %s: symbol not found\n"),
714 pe_def_file->exports[i].internal_name);
716 free (name);
720 /************************************************************************
722 Build the bfd that will contain .edata and .reloc sections
724 ************************************************************************/
726 static void
727 build_filler_bfd (include_edata)
728 int include_edata;
730 lang_input_statement_type *filler_file;
731 filler_file = lang_add_input_file ("dll stuff",
732 lang_input_file_is_fake_enum,
733 NULL);
734 filler_file->the_bfd = filler_bfd = bfd_create ("dll stuff", output_bfd);
735 if (filler_bfd == NULL
736 || !bfd_set_arch_mach (filler_bfd,
737 bfd_get_arch (output_bfd),
738 bfd_get_mach (output_bfd)))
740 einfo ("%X%P: can not create BFD %E\n");
741 return;
744 if (include_edata)
746 edata_s = bfd_make_section_old_way (filler_bfd, ".edata");
747 if (edata_s == NULL
748 || !bfd_set_section_flags (filler_bfd, edata_s,
749 (SEC_HAS_CONTENTS
750 | SEC_ALLOC
751 | SEC_LOAD
752 | SEC_KEEP
753 | SEC_IN_MEMORY)))
755 einfo ("%X%P: can not create .edata section: %E\n");
756 return;
758 bfd_set_section_size (filler_bfd, edata_s, edata_sz);
761 reloc_s = bfd_make_section_old_way (filler_bfd, ".reloc");
762 if (reloc_s == NULL
763 || !bfd_set_section_flags (filler_bfd, reloc_s,
764 (SEC_HAS_CONTENTS
765 | SEC_ALLOC
766 | SEC_LOAD
767 | SEC_KEEP
768 | SEC_IN_MEMORY)))
770 einfo ("%X%P: can not create .reloc section: %E\n");
771 return;
773 bfd_set_section_size (filler_bfd, reloc_s, 0);
775 ldlang_add_file (filler_file);
778 /************************************************************************
780 Gather all the exported symbols and build the .edata section
782 ************************************************************************/
784 static void
785 generate_edata (abfd, info)
786 bfd *abfd;
787 struct bfd_link_info *info ATTRIBUTE_UNUSED;
789 int i, next_ordinal;
790 int name_table_size = 0;
791 const char *dlnp;
793 /* First, we need to know how many exported symbols there are,
794 and what the range of ordinals is. */
796 if (pe_def_file->name)
798 dll_name = pe_def_file->name;
800 else
802 dll_name = abfd->filename;
803 for (dlnp = dll_name; *dlnp; dlnp++)
805 if (*dlnp == '\\' || *dlnp == '/' || *dlnp == ':')
806 dll_name = dlnp + 1;
810 if (count_with_ordinals && max_ordinal > count_exported)
812 if (min_ordinal > max_ordinal - count_exported + 1)
813 min_ordinal = max_ordinal - count_exported + 1;
815 else
817 min_ordinal = 1;
818 max_ordinal = count_exported;
820 export_table_size = max_ordinal - min_ordinal + 1;
822 exported_symbols = (int *) xmalloc (export_table_size * sizeof (int));
823 for (i = 0; i < export_table_size; i++)
824 exported_symbols[i] = -1;
826 /* Now we need to assign ordinals to those that don't have them. */
827 for (i = 0; i < NE; i++)
829 if (exported_symbol_sections[i])
831 if (pe_def_file->exports[i].ordinal != -1)
833 int ei = pe_def_file->exports[i].ordinal - min_ordinal;
834 int pi = exported_symbols[ei];
835 if (pi != -1)
837 /* xgettext:c-format */
838 einfo (_("%XError, ordinal used twice: %d (%s vs %s)\n"),
839 pe_def_file->exports[i].ordinal,
840 pe_def_file->exports[i].name,
841 pe_def_file->exports[pi].name);
843 exported_symbols[ei] = i;
845 name_table_size += strlen (pe_def_file->exports[i].name) + 1;
849 next_ordinal = min_ordinal;
850 for (i = 0; i < NE; i++)
851 if (exported_symbol_sections[i])
852 if (pe_def_file->exports[i].ordinal == -1)
854 while (exported_symbols[next_ordinal - min_ordinal] != -1)
855 next_ordinal++;
856 exported_symbols[next_ordinal - min_ordinal] = i;
857 pe_def_file->exports[i].ordinal = next_ordinal;
860 /* OK, now we can allocate some memory. */
862 edata_sz = (40 /* directory */
863 + 4 * export_table_size /* addresses */
864 + 4 * count_exported_byname /* name ptrs */
865 + 2 * count_exported_byname /* ordinals */
866 + name_table_size + strlen (dll_name) + 1);
869 /* Fill the exported symbol offsets. The preliminary work has already
870 been done in process_def_file(). */
872 static void
873 fill_exported_offsets (abfd, info)
874 bfd *abfd ATTRIBUTE_UNUSED;
875 struct bfd_link_info *info;
877 int i;
878 struct bfd_link_hash_entry *blhe;
880 for (i = 0; i < pe_def_file->num_exports; i++)
882 char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
883 if (pe_details->underscored)
885 *name = '_';
886 strcpy (name + 1, pe_def_file->exports[i].internal_name);
888 else
889 strcpy (name, pe_def_file->exports[i].internal_name);
891 blhe = bfd_link_hash_lookup (info->hash,
892 name,
893 false, false, true);
895 if (blhe && (blhe->type == bfd_link_hash_defined))
897 exported_symbol_offsets[i] = blhe->u.def.value;
899 free (name);
903 static void
904 fill_edata (abfd, info)
905 bfd *abfd;
906 struct bfd_link_info *info ATTRIBUTE_UNUSED;
908 int i, hint;
909 unsigned char *edirectory;
910 unsigned long *eaddresses;
911 unsigned long *enameptrs;
912 unsigned short *eordinals;
913 unsigned char *enamestr;
914 time_t now;
916 time (&now);
918 edata_d = (unsigned char *) xmalloc (edata_sz);
920 /* Note use of array pointer math here. */
921 edirectory = edata_d;
922 eaddresses = (unsigned long *) (edata_d + 40);
923 enameptrs = eaddresses + export_table_size;
924 eordinals = (unsigned short *) (enameptrs + count_exported_byname);
925 enamestr = (char *) (eordinals + count_exported_byname);
927 #define ERVA(ptr) (((unsigned char *)(ptr) - edata_d) + edata_s->output_section->vma - image_base)
929 memset (edata_d, 0, edata_sz);
930 bfd_put_32 (abfd, now, edata_d + 4);
931 if (pe_def_file->version_major != -1)
933 bfd_put_16 (abfd, pe_def_file->version_major, edata_d + 8);
934 bfd_put_16 (abfd, pe_def_file->version_minor, edata_d + 10);
936 bfd_put_32 (abfd, ERVA (enamestr), edata_d + 12);
937 strcpy (enamestr, dll_name);
938 enamestr += strlen (enamestr) + 1;
939 bfd_put_32 (abfd, min_ordinal, edata_d + 16);
940 bfd_put_32 (abfd, export_table_size, edata_d + 20);
941 bfd_put_32 (abfd, count_exported_byname, edata_d + 24);
942 bfd_put_32 (abfd, ERVA (eaddresses), edata_d + 28);
943 bfd_put_32 (abfd, ERVA (enameptrs), edata_d + 32);
944 bfd_put_32 (abfd, ERVA (eordinals), edata_d + 36);
946 fill_exported_offsets (abfd, info);
948 /* Ok, now for the filling in part. */
949 hint = 0;
950 for (i = 0; i < export_table_size; i++)
952 int s = exported_symbols[i];
953 if (s != -1)
955 struct sec *ssec = exported_symbol_sections[s];
956 unsigned long srva = (exported_symbol_offsets[s]
957 + ssec->output_section->vma
958 + ssec->output_offset);
959 int ord = pe_def_file->exports[s].ordinal;
961 bfd_put_32 (abfd, srva - image_base,
962 (void *) (eaddresses + ord - min_ordinal));
963 if (!pe_def_file->exports[s].flag_noname)
965 char *ename = pe_def_file->exports[s].name;
966 bfd_put_32 (abfd, ERVA (enamestr), (void *) enameptrs);
967 enameptrs++;
968 strcpy (enamestr, ename);
969 enamestr += strlen (enamestr) + 1;
970 bfd_put_16 (abfd, ord - min_ordinal, (void *) eordinals);
971 eordinals++;
972 pe_def_file->exports[s].hint = hint++;
979 static struct sec *current_sec;
981 void
982 pe_walk_relocs_of_symbol (info, name, cb)
983 struct bfd_link_info *info;
984 CONST char *name;
985 int (*cb) (arelent *);
987 bfd *b;
988 struct sec *s;
990 for (b = info->input_bfds; b; b = b->link_next)
992 arelent **relocs;
993 int relsize, nrelocs, i;
995 for (s = b->sections; s; s = s->next)
997 asymbol **symbols;
998 int nsyms, symsize;
999 int flags = bfd_get_section_flags (b, s);
1001 /* Skip discarded linkonce sections */
1002 if (flags & SEC_LINK_ONCE
1003 && s->output_section == bfd_abs_section_ptr)
1004 continue;
1006 current_sec=s;
1008 symsize = bfd_get_symtab_upper_bound (b);
1009 symbols = (asymbol **) xmalloc (symsize);
1010 nsyms = bfd_canonicalize_symtab (b, symbols);
1012 relsize = bfd_get_reloc_upper_bound (b, s);
1013 relocs = (arelent **) xmalloc ((size_t) relsize);
1014 nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols);
1016 for (i = 0; i < nrelocs; i++)
1018 struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr;
1019 if (!strcmp(name,sym->name)) cb(relocs[i]);
1021 free (relocs);
1022 /* Warning: the allocated symbols are remembered in BFD and reused
1023 later, so don't free them! */
1024 /* free (symbols); */
1029 /************************************************************************
1031 Gather all the relocations and build the .reloc section
1033 ************************************************************************/
1035 static void
1036 generate_reloc (abfd, info)
1037 bfd *abfd;
1038 struct bfd_link_info *info;
1041 /* For .reloc stuff. */
1042 reloc_data_type *reloc_data;
1043 int total_relocs = 0;
1044 int i;
1045 unsigned long sec_page = (unsigned long) (-1);
1046 unsigned long page_ptr, page_count;
1047 int bi;
1048 bfd *b;
1049 struct sec *s;
1051 total_relocs = 0;
1052 for (b = info->input_bfds; b; b = b->link_next)
1053 for (s = b->sections; s; s = s->next)
1054 total_relocs += s->reloc_count;
1056 reloc_data =
1057 (reloc_data_type *) xmalloc (total_relocs * sizeof (reloc_data_type));
1059 total_relocs = 0;
1060 bi = 0;
1061 for (bi = 0, b = info->input_bfds; b; bi++, b = b->link_next)
1063 arelent **relocs;
1064 int relsize, nrelocs, i;
1066 for (s = b->sections; s; s = s->next)
1068 unsigned long sec_vma = s->output_section->vma + s->output_offset;
1069 asymbol **symbols;
1070 int nsyms, symsize;
1072 /* If it's not loaded, we don't need to relocate it this way. */
1073 if (!(s->output_section->flags & SEC_LOAD))
1074 continue;
1076 /* I don't know why there would be a reloc for these, but I've
1077 seen it happen - DJ */
1078 if (s->output_section == &bfd_abs_section)
1079 continue;
1081 if (s->output_section->vma == 0)
1083 /* Huh? Shouldn't happen, but punt if it does. */
1084 einfo ("DJ: zero vma section reloc detected: `%s' #%d f=%d\n",
1085 s->output_section->name, s->output_section->index,
1086 s->output_section->flags);
1087 continue;
1090 symsize = bfd_get_symtab_upper_bound (b);
1091 symbols = (asymbol **) xmalloc (symsize);
1092 nsyms = bfd_canonicalize_symtab (b, symbols);
1094 relsize = bfd_get_reloc_upper_bound (b, s);
1095 relocs = (arelent **) xmalloc ((size_t) relsize);
1096 nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols);
1098 for (i = 0; i < nrelocs; i++)
1100 if (pe_dll_extra_pe_debug)
1102 struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr;
1103 printf("rel: %s\n",sym->name);
1105 if (!relocs[i]->howto->pc_relative
1106 && relocs[i]->howto->type != pe_details->imagebase_reloc)
1108 bfd_vma sym_vma;
1109 struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr;
1110 sym_vma = (relocs[i]->addend
1111 + sym->value
1112 + sym->section->vma
1113 + sym->section->output_offset
1114 + sym->section->output_section->vma);
1115 reloc_data[total_relocs].vma = sec_vma + relocs[i]->address;
1117 #define BITS_AND_SHIFT(bits, shift) (bits * 1000 | shift)
1119 switch BITS_AND_SHIFT (relocs[i]->howto->bitsize,
1120 relocs[i]->howto->rightshift)
1122 case BITS_AND_SHIFT (32, 0):
1123 reloc_data[total_relocs].type = 3;
1124 total_relocs++;
1125 break;
1126 case BITS_AND_SHIFT (16, 0):
1127 reloc_data[total_relocs].type = 2;
1128 total_relocs++;
1129 break;
1130 case BITS_AND_SHIFT (16, 16):
1131 reloc_data[total_relocs].type = 4;
1132 /* FIXME: we can't know the symbol's right value
1133 yet, but we probably can safely assume that
1134 CE will relocate us in 64k blocks, so leaving
1135 it zero is safe. */
1136 reloc_data[total_relocs].extra = 0;
1137 total_relocs++;
1138 break;
1139 case BITS_AND_SHIFT (26, 2):
1140 reloc_data[total_relocs].type = 5;
1141 total_relocs++;
1142 break;
1143 default:
1144 /* xgettext:c-format */
1145 einfo (_("%XError: %d-bit reloc in dll\n"),
1146 relocs[i]->howto->bitsize);
1147 break;
1151 free (relocs);
1152 /* Warning: the allocated symbols are remembered in BFD and
1153 reused later, so don't free them! */
1154 #if 0
1155 free (symbol);
1156 #endif
1160 /* At this point, we have total_relocs relocation addresses in
1161 reloc_addresses, which are all suitable for the .reloc section.
1162 We must now create the new sections. */
1164 qsort (reloc_data, total_relocs, sizeof (*reloc_data), reloc_sort);
1166 for (i = 0; i < total_relocs; i++)
1168 unsigned long this_page = (reloc_data[i].vma >> 12);
1170 if (this_page != sec_page)
1172 reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align */
1173 reloc_sz += 8;
1174 sec_page = this_page;
1177 reloc_sz += 2;
1179 if (reloc_data[i].type == 4)
1180 reloc_sz += 2;
1182 reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align */
1184 reloc_d = (unsigned char *) xmalloc (reloc_sz);
1186 sec_page = (unsigned long) (-1);
1187 reloc_sz = 0;
1188 page_ptr = (unsigned long) (-1);
1189 page_count = 0;
1190 for (i = 0; i < total_relocs; i++)
1192 unsigned long rva = reloc_data[i].vma - image_base;
1193 unsigned long this_page = (rva & ~0xfff);
1194 if (this_page != sec_page)
1196 while (reloc_sz & 3)
1197 reloc_d[reloc_sz++] = 0;
1198 if (page_ptr != (unsigned long) (-1))
1199 bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
1200 bfd_put_32 (abfd, this_page, reloc_d + reloc_sz);
1201 page_ptr = reloc_sz;
1202 reloc_sz += 8;
1203 sec_page = this_page;
1204 page_count = 0;
1206 bfd_put_16 (abfd, (rva & 0xfff) + (reloc_data[i].type << 12),
1207 reloc_d + reloc_sz);
1208 reloc_sz += 2;
1209 if (reloc_data[i].type == 4)
1211 bfd_put_16 (abfd, reloc_data[i].extra, reloc_d + reloc_sz);
1212 reloc_sz += 2;
1214 page_count++;
1216 while (reloc_sz & 3)
1217 reloc_d[reloc_sz++] = 0;
1218 if (page_ptr != (unsigned long) (-1))
1219 bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
1220 while (reloc_sz < reloc_s->_raw_size)
1221 reloc_d[reloc_sz++] = 0;
1224 /************************************************************************
1226 Given the exiting def_file structure, print out a .DEF file that
1227 corresponds to it.
1229 ************************************************************************/
1231 static void
1232 quoteput (s, f, needs_quotes)
1233 char *s;
1234 FILE *f;
1235 int needs_quotes;
1237 char *cp;
1238 for (cp = s; *cp; cp++)
1239 if (*cp == '\''
1240 || *cp == '"'
1241 || *cp == '\\'
1242 || isspace ((unsigned char) *cp)
1243 || *cp == ','
1244 || *cp == ';')
1245 needs_quotes = 1;
1246 if (needs_quotes)
1248 putc ('"', f);
1249 while (*s)
1251 if (*s == '"' || *s == '\\')
1252 putc ('\\', f);
1253 putc (*s, f);
1254 s++;
1256 putc ('"', f);
1258 else
1259 fputs (s, f);
1262 void
1263 pe_dll_generate_def_file (pe_out_def_filename)
1264 const char *pe_out_def_filename;
1266 int i;
1267 FILE *out = fopen (pe_out_def_filename, "w");
1268 if (out == NULL)
1270 /* xgettext:c-format */
1271 einfo (_("%s: Can't open output def file %s\n"),
1272 program_name, pe_out_def_filename);
1275 if (pe_def_file)
1277 if (pe_def_file->name)
1279 if (pe_def_file->is_dll)
1280 fprintf (out, "LIBRARY ");
1281 else
1282 fprintf (out, "NAME ");
1283 quoteput (pe_def_file->name, out, 1);
1284 if (pe_data (output_bfd)->pe_opthdr.ImageBase)
1285 fprintf (out, " BASE=0x%lx",
1286 (unsigned long) pe_data (output_bfd)->pe_opthdr.ImageBase);
1287 fprintf (out, "\n");
1290 if (pe_def_file->description)
1292 fprintf (out, "DESCRIPTION ");
1293 quoteput (pe_def_file->description, out, 1);
1294 fprintf (out, "\n");
1297 if (pe_def_file->version_minor != -1)
1298 fprintf (out, "VERSION %d.%d\n", pe_def_file->version_major,
1299 pe_def_file->version_minor);
1300 else if (pe_def_file->version_major != -1)
1301 fprintf (out, "VERSION %d\n", pe_def_file->version_major);
1303 if (pe_def_file->stack_reserve != -1 || pe_def_file->heap_reserve != -1)
1304 fprintf (out, "\n");
1306 if (pe_def_file->stack_commit != -1)
1307 fprintf (out, "STACKSIZE 0x%x,0x%x\n",
1308 pe_def_file->stack_reserve, pe_def_file->stack_commit);
1309 else if (pe_def_file->stack_reserve != -1)
1310 fprintf (out, "STACKSIZE 0x%x\n", pe_def_file->stack_reserve);
1311 if (pe_def_file->heap_commit != -1)
1312 fprintf (out, "HEAPSIZE 0x%x,0x%x\n",
1313 pe_def_file->heap_reserve, pe_def_file->heap_commit);
1314 else if (pe_def_file->heap_reserve != -1)
1315 fprintf (out, "HEAPSIZE 0x%x\n", pe_def_file->heap_reserve);
1317 if (pe_def_file->num_section_defs > 0)
1319 fprintf (out, "\nSECTIONS\n\n");
1320 for (i = 0; i < pe_def_file->num_section_defs; i++)
1322 fprintf (out, " ");
1323 quoteput (pe_def_file->section_defs[i].name, out, 0);
1324 if (pe_def_file->section_defs[i].class)
1326 fprintf (out, " CLASS ");
1327 quoteput (pe_def_file->section_defs[i].class, out, 0);
1329 if (pe_def_file->section_defs[i].flag_read)
1330 fprintf (out, " READ");
1331 if (pe_def_file->section_defs[i].flag_write)
1332 fprintf (out, " WRITE");
1333 if (pe_def_file->section_defs[i].flag_execute)
1334 fprintf (out, " EXECUTE");
1335 if (pe_def_file->section_defs[i].flag_shared)
1336 fprintf (out, " SHARED");
1337 fprintf (out, "\n");
1341 if (pe_def_file->num_exports > 0)
1343 fprintf (out, "EXPORTS\n");
1344 for (i = 0; i < pe_def_file->num_exports; i++)
1346 def_file_export *e = pe_def_file->exports + i;
1347 fprintf (out, " ");
1348 quoteput (e->name, out, 0);
1349 if (e->internal_name && strcmp (e->internal_name, e->name))
1351 fprintf (out, " = ");
1352 quoteput (e->internal_name, out, 0);
1354 if (e->ordinal != -1)
1355 fprintf (out, " @%d", e->ordinal);
1356 if (e->flag_private)
1357 fprintf (out, " PRIVATE");
1358 if (e->flag_constant)
1359 fprintf (out, " CONSTANT");
1360 if (e->flag_noname)
1361 fprintf (out, " NONAME");
1362 if (e->flag_data)
1363 fprintf (out, " DATA");
1365 fprintf (out, "\n");
1369 if (pe_def_file->num_imports > 0)
1371 fprintf (out, "\nIMPORTS\n\n");
1372 for (i = 0; i < pe_def_file->num_imports; i++)
1374 def_file_import *im = pe_def_file->imports + i;
1375 fprintf (out, " ");
1376 if (im->internal_name
1377 && (!im->name || strcmp (im->internal_name, im->name)))
1379 quoteput (im->internal_name, out, 0);
1380 fprintf (out, " = ");
1382 quoteput (im->module->name, out, 0);
1383 fprintf (out, ".");
1384 if (im->name)
1385 quoteput (im->name, out, 0);
1386 else
1387 fprintf (out, "%d", im->ordinal);
1388 fprintf (out, "\n");
1392 else
1393 fprintf (out, _("; no contents available\n"));
1395 if (fclose (out) == EOF)
1397 /* xgettext:c-format */
1398 einfo (_("%P: Error closing file `%s'\n"), pe_out_def_filename);
1402 /************************************************************************
1404 Generate the import library
1406 ************************************************************************/
1408 static asymbol **symtab;
1409 static int symptr;
1410 static int tmp_seq;
1411 static const char *dll_filename;
1412 static char *dll_symname;
1414 #define UNDSEC (asection *) &bfd_und_section
1416 static asection *
1417 quick_section (abfd, name, flags, align)
1418 bfd *abfd;
1419 const char *name;
1420 int flags;
1421 int align;
1423 asection *sec;
1424 asymbol *sym;
1426 sec = bfd_make_section_old_way (abfd, name);
1427 bfd_set_section_flags (abfd, sec, flags | SEC_ALLOC | SEC_LOAD | SEC_KEEP);
1428 bfd_set_section_alignment (abfd, sec, align);
1429 /* Remember to undo this before trying to link internally! */
1430 sec->output_section = sec;
1432 sym = bfd_make_empty_symbol (abfd);
1433 symtab[symptr++] = sym;
1434 sym->name = sec->name;
1435 sym->section = sec;
1436 sym->flags = BSF_LOCAL;
1437 sym->value = 0;
1439 return sec;
1442 static void
1443 quick_symbol (abfd, n1, n2, n3, sec, flags, addr)
1444 bfd *abfd;
1445 char *n1;
1446 char *n2;
1447 char *n3;
1448 asection *sec;
1449 int flags;
1450 int addr;
1452 asymbol *sym;
1453 char *name = (char *) xmalloc (strlen (n1) + strlen (n2) + strlen (n3) + 1);
1454 strcpy (name, n1);
1455 strcat (name, n2);
1456 strcat (name, n3);
1457 sym = bfd_make_empty_symbol (abfd);
1458 sym->name = name;
1459 sym->section = sec;
1460 sym->flags = flags;
1461 sym->value = addr;
1462 symtab[symptr++] = sym;
1465 static arelent *reltab = 0;
1466 static int relcount = 0, relsize = 0;
1468 static void
1469 quick_reloc (abfd, address, which_howto, symidx)
1470 bfd *abfd;
1471 int address;
1472 int which_howto;
1473 int symidx;
1475 if (relcount >= (relsize - 1))
1477 relsize += 10;
1478 if (reltab)
1479 reltab = (arelent *) xrealloc (reltab, relsize * sizeof (arelent));
1480 else
1481 reltab = (arelent *) xmalloc (relsize * sizeof (arelent));
1483 reltab[relcount].address = address;
1484 reltab[relcount].addend = 0;
1485 reltab[relcount].howto = bfd_reloc_type_lookup (abfd, which_howto);
1486 reltab[relcount].sym_ptr_ptr = symtab + symidx;
1487 relcount++;
1490 static void
1491 save_relocs (asection *sec)
1493 int i;
1494 sec->relocation = reltab;
1495 sec->reloc_count = relcount;
1496 sec->orelocation = (arelent **) xmalloc ((relcount + 1) * sizeof (arelent *));
1497 for (i = 0; i < relcount; i++)
1498 sec->orelocation[i] = sec->relocation + i;
1499 sec->orelocation[relcount] = 0;
1500 sec->flags |= SEC_RELOC;
1501 reltab = 0;
1502 relcount = relsize = 0;
1506 * .section .idata$2
1507 * .global __head_my_dll
1508 * __head_my_dll:
1509 * .rva hname
1510 * .long 0
1511 * .long 0
1512 * .rva __my_dll_iname
1513 * .rva fthunk
1515 * .section .idata$5
1516 * .long 0
1517 * fthunk:
1519 * .section .idata$4
1520 * .long 0
1521 * hname:
1524 static bfd *
1525 make_head (parent)
1526 bfd *parent;
1528 asection *id2, *id5, *id4;
1529 unsigned char *d2, *d5, *d4;
1530 char *oname;
1531 bfd *abfd;
1533 oname = (char *) xmalloc (20);
1534 sprintf (oname, "d%06d.o", tmp_seq);
1535 tmp_seq++;
1537 abfd = bfd_create (oname, parent);
1538 bfd_find_target (pe_details->object_target, abfd);
1539 bfd_make_writable (abfd);
1541 bfd_set_format (abfd, bfd_object);
1542 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1544 symptr = 0;
1545 symtab = (asymbol **) xmalloc (6 * sizeof (asymbol *));
1546 id2 = quick_section (abfd, ".idata$2", SEC_HAS_CONTENTS, 2);
1547 id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1548 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1549 quick_symbol (abfd, U ("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0);
1550 quick_symbol (abfd, U (""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
1552 /* OK, pay attention here. I got confused myself looking back at
1553 it. We create a four-byte section to mark the beginning of the
1554 list, and we include an offset of 4 in the section, so that the
1555 pointer to the list points to the *end* of this section, which is
1556 the start of the list of sections from other objects. */
1558 bfd_set_section_size (abfd, id2, 20);
1559 d2 = (unsigned char *) xmalloc (20);
1560 id2->contents = d2;
1561 memset (d2, 0, 20);
1562 d2[0] = d2[16] = 4; /* reloc addend */
1563 quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
1564 quick_reloc (abfd, 12, BFD_RELOC_RVA, 4);
1565 quick_reloc (abfd, 16, BFD_RELOC_RVA, 1);
1566 save_relocs (id2);
1568 bfd_set_section_size (abfd, id5, 4);
1569 d5 = (unsigned char *) xmalloc (4);
1570 id5->contents = d5;
1571 memset (d5, 0, 4);
1573 bfd_set_section_size (abfd, id4, 4);
1574 d4 = (unsigned char *) xmalloc (4);
1575 id4->contents = d4;
1576 memset (d4, 0, 4);
1578 bfd_set_symtab (abfd, symtab, symptr);
1580 bfd_set_section_contents (abfd, id2, d2, 0, 20);
1581 bfd_set_section_contents (abfd, id5, d5, 0, 4);
1582 bfd_set_section_contents (abfd, id4, d4, 0, 4);
1584 bfd_make_readable (abfd);
1585 return abfd;
1589 * .section .idata$4
1590 * .long 0
1591 * .section .idata$5
1592 * .long 0
1593 * .section idata$7
1594 * .global __my_dll_iname
1595 *__my_dll_iname:
1596 * .asciz "my.dll"
1599 static bfd *
1600 make_tail (parent)
1601 bfd *parent;
1603 asection *id4, *id5, *id7;
1604 unsigned char *d4, *d5, *d7;
1605 int len;
1606 char *oname;
1607 bfd *abfd;
1609 oname = (char *) xmalloc (20);
1610 sprintf (oname, "d%06d.o", tmp_seq);
1611 tmp_seq++;
1613 abfd = bfd_create (oname, parent);
1614 bfd_find_target (pe_details->object_target, abfd);
1615 bfd_make_writable (abfd);
1617 bfd_set_format (abfd, bfd_object);
1618 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1620 symptr = 0;
1621 symtab = (asymbol **) xmalloc (5 * sizeof (asymbol *));
1622 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1623 id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1624 id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
1625 quick_symbol (abfd, U (""), dll_symname, "_iname", id7, BSF_GLOBAL, 0);
1627 bfd_set_section_size (abfd, id4, 4);
1628 d4 = (unsigned char *) xmalloc (4);
1629 id4->contents = d4;
1630 memset (d4, 0, 4);
1632 bfd_set_section_size (abfd, id5, 4);
1633 d5 = (unsigned char *) xmalloc (4);
1634 id5->contents = d5;
1635 memset (d5, 0, 4);
1637 len = strlen (dll_filename) + 1;
1638 if (len & 1)
1639 len++;
1640 bfd_set_section_size (abfd, id7, len);
1641 d7 = (unsigned char *) xmalloc (len);
1642 id7->contents = d7;
1643 strcpy (d7, dll_filename);
1645 bfd_set_symtab (abfd, symtab, symptr);
1647 bfd_set_section_contents (abfd, id4, d4, 0, 4);
1648 bfd_set_section_contents (abfd, id5, d5, 0, 4);
1649 bfd_set_section_contents (abfd, id7, d7, 0, len);
1651 bfd_make_readable (abfd);
1652 return abfd;
1656 * .text
1657 * .global _function
1658 * .global ___imp_function
1659 * .global __imp__function
1660 *_function:
1661 * jmp *__imp__function:
1663 * .section idata$7
1664 * .long __head_my_dll
1666 * .section .idata$5
1667 *___imp_function:
1668 *__imp__function:
1669 *iat?
1670 * .section .idata$4
1671 *iat?
1672 * .section .idata$6
1673 *ID<ordinal>:
1674 * .short <hint>
1675 * .asciz "function" xlate? (add underscore, kill at)
1678 static unsigned char jmp_ix86_bytes[] = {
1679 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90
1683 *_function:
1684 * mov.l ip+8,r0
1685 * mov.l @r0,r0
1686 * jmp @r0
1687 * nop
1688 * .dw __imp_function
1691 static unsigned char jmp_sh_bytes[] = {
1692 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00
1696 *_function:
1697 * lui $t0,<high:__imp_function>
1698 * lw $t0,<low:__imp_function>
1699 * jr $t0
1700 * nop
1703 static unsigned char jmp_mips_bytes[] = {
1704 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d,
1705 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00
1708 static bfd *
1709 make_one (exp, parent)
1710 def_file_export *exp;
1711 bfd *parent;
1713 asection *tx, *id7, *id5, *id4, *id6;
1714 unsigned char *td = NULL, *d7, *d5, *d4, *d6 = NULL;
1715 int len;
1716 char *oname;
1717 bfd *abfd;
1718 unsigned char *jmp_bytes = NULL;
1719 int jmp_byte_count = 0;
1721 switch (pe_details->pe_arch)
1723 case PE_ARCH_i386:
1724 jmp_bytes = jmp_ix86_bytes;
1725 jmp_byte_count = sizeof (jmp_ix86_bytes);
1726 break;
1727 case PE_ARCH_sh:
1728 jmp_bytes = jmp_sh_bytes;
1729 jmp_byte_count = sizeof (jmp_sh_bytes);
1730 break;
1731 case PE_ARCH_mips:
1732 jmp_bytes = jmp_mips_bytes;
1733 jmp_byte_count = sizeof (jmp_mips_bytes);
1734 break;
1737 oname = (char *) xmalloc (20);
1738 sprintf (oname, "d%06d.o", tmp_seq);
1739 tmp_seq++;
1741 abfd = bfd_create (oname, parent);
1742 bfd_find_target (pe_details->object_target, abfd);
1743 bfd_make_writable (abfd);
1745 bfd_set_format (abfd, bfd_object);
1746 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1748 symptr = 0;
1749 symtab = (asymbol **) xmalloc (11 * sizeof (asymbol *));
1750 tx = quick_section (abfd, ".text", SEC_CODE|SEC_HAS_CONTENTS, 2);
1751 id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
1752 id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1753 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1754 id6 = quick_section (abfd, ".idata$6", SEC_HAS_CONTENTS, 2);
1755 if (! exp->flag_data)
1756 quick_symbol (abfd, U (""), exp->internal_name, "", tx, BSF_GLOBAL, 0);
1757 quick_symbol (abfd, U ("_head_"), dll_symname, "", UNDSEC, BSF_GLOBAL, 0);
1758 quick_symbol (abfd, U ("_imp__"), exp->internal_name, "", id5, BSF_GLOBAL, 0);
1759 /* symbol to reference ord/name of imported symbol, used to implement
1760 auto-import */
1761 quick_symbol (abfd, U("_nm__"), exp->internal_name, "", id6, BSF_GLOBAL, 0);
1762 if (pe_dll_compat_implib)
1763 quick_symbol (abfd, U ("__imp_"), exp->internal_name, "",
1764 id5, BSF_GLOBAL, 0);
1766 if (! exp->flag_data)
1768 bfd_set_section_size (abfd, tx, jmp_byte_count);
1769 td = (unsigned char *) xmalloc (jmp_byte_count);
1770 tx->contents = td;
1771 memcpy (td, jmp_bytes, jmp_byte_count);
1772 switch (pe_details->pe_arch)
1774 case PE_ARCH_i386:
1775 quick_reloc (abfd, 2, BFD_RELOC_32, 2);
1776 break;
1777 case PE_ARCH_sh:
1778 quick_reloc (abfd, 8, BFD_RELOC_32, 2);
1779 break;
1780 case PE_ARCH_mips:
1781 quick_reloc (abfd, 0, BFD_RELOC_HI16_S, 2);
1782 quick_reloc (abfd, 0, BFD_RELOC_LO16, 0); /* MIPS_R_PAIR */
1783 quick_reloc (abfd, 4, BFD_RELOC_LO16, 2);
1784 break;
1786 save_relocs (tx);
1789 bfd_set_section_size (abfd, id7, 4);
1790 d7 = (unsigned char *) xmalloc (4);
1791 id7->contents = d7;
1792 memset (d7, 0, 4);
1793 quick_reloc (abfd, 0, BFD_RELOC_RVA, 6);
1794 save_relocs (id7);
1796 bfd_set_section_size (abfd, id5, 4);
1797 d5 = (unsigned char *) xmalloc (4);
1798 id5->contents = d5;
1799 memset (d5, 0, 4);
1800 if (exp->flag_noname)
1802 d5[0] = exp->ordinal;
1803 d5[1] = exp->ordinal >> 8;
1804 d5[3] = 0x80;
1806 else
1808 quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
1809 save_relocs (id5);
1812 bfd_set_section_size (abfd, id4, 4);
1813 d4 = (unsigned char *) xmalloc (4);
1814 id4->contents = d4;
1815 memset (d4, 0, 4);
1816 if (exp->flag_noname)
1818 d4[0] = exp->ordinal;
1819 d4[1] = exp->ordinal >> 8;
1820 d4[3] = 0x80;
1822 else
1824 quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
1825 save_relocs (id4);
1828 if (exp->flag_noname)
1830 len = 0;
1831 bfd_set_section_size (abfd, id6, 0);
1833 else
1835 len = strlen (exp->name) + 3;
1836 if (len & 1)
1837 len++;
1838 bfd_set_section_size (abfd, id6, len);
1839 d6 = (unsigned char *) xmalloc (len);
1840 id6->contents = d6;
1841 memset (d6, 0, len);
1842 d6[0] = exp->hint & 0xff;
1843 d6[1] = exp->hint >> 8;
1844 strcpy (d6 + 2, exp->name);
1847 bfd_set_symtab (abfd, symtab, symptr);
1849 bfd_set_section_contents (abfd, tx, td, 0, jmp_byte_count);
1850 bfd_set_section_contents (abfd, id7, d7, 0, 4);
1851 bfd_set_section_contents (abfd, id5, d5, 0, 4);
1852 bfd_set_section_contents (abfd, id4, d4, 0, 4);
1853 if (!exp->flag_noname)
1854 bfd_set_section_contents (abfd, id6, d6, 0, len);
1856 bfd_make_readable (abfd);
1857 return abfd;
1860 static bfd *
1861 make_singleton_name_thunk (import, parent)
1862 char *import;
1863 bfd *parent;
1865 /* name thunks go to idata$4 */
1867 asection *id4;
1868 unsigned char *d4;
1869 char *oname;
1870 bfd *abfd;
1872 oname = (char *) xmalloc (20);
1873 sprintf (oname, "nmth%06d.o", tmp_seq);
1874 tmp_seq++;
1876 abfd = bfd_create (oname, parent);
1877 bfd_find_target (pe_details->object_target, abfd);
1878 bfd_make_writable (abfd);
1880 bfd_set_format (abfd, bfd_object);
1881 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1883 symptr = 0;
1884 symtab = (asymbol **) xmalloc (3 * sizeof (asymbol *));
1885 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1886 quick_symbol (abfd, U ("_nm_thnk_"), import, "", id4, BSF_GLOBAL, 0);
1887 quick_symbol (abfd, U ("_nm_"), import, "", UNDSEC, BSF_GLOBAL, 0);
1889 bfd_set_section_size (abfd, id4, 8);
1890 d4 = (unsigned char *) xmalloc (4);
1891 id4->contents = d4;
1892 memset (d4, 0, 8);
1893 quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
1894 save_relocs (id4);
1896 bfd_set_symtab (abfd, symtab, symptr);
1898 bfd_set_section_contents (abfd, id4, d4, 0, 8);
1900 bfd_make_readable (abfd);
1901 return abfd;
1904 static char *
1905 make_import_fixup_mark (rel)
1906 arelent *rel;
1908 /* we convert reloc to symbol, for later reference */
1909 static int counter;
1910 static char *fixup_name = NULL;
1911 static int buffer_len = 0;
1913 struct symbol_cache_entry *sym = *rel->sym_ptr_ptr;
1915 bfd *abfd = bfd_asymbol_bfd (sym);
1916 struct coff_link_hash_entry *myh = NULL;
1918 if (!fixup_name)
1920 fixup_name = (char *) xmalloc (384);
1921 buffer_len = 384;
1924 if (strlen (sym->name) + 25 > buffer_len)
1925 /* assume 25 chars for "__fu" + counter + "_". If counter is
1926 bigger than 20 digits long, we've got worse problems than
1927 overflowing this buffer... */
1929 free (fixup_name);
1930 /* new buffer size is length of symbol, plus 25, but then
1931 rounded up to the nearest multiple of 128 */
1932 buffer_len = ((strlen (sym->name) + 25) + 127) & ~127;
1933 fixup_name = (char *) xmalloc (buffer_len);
1936 sprintf (fixup_name, "__fu%d_%s", counter++, sym->name);
1938 bfd_coff_link_add_one_symbol (&link_info, abfd, fixup_name, BSF_GLOBAL,
1939 current_sec, /* sym->section, */
1940 rel->address, NULL, true, false,
1941 (struct bfd_link_hash_entry **) &myh);
1944 printf("type:%d\n",myh->type);
1945 printf("%s\n",myh->root.u.def.section->name);
1947 return fixup_name;
1952 * .section .idata$3
1953 * .rva __nm_thnk_SYM (singleton thunk with name of func)
1954 * .long 0
1955 * .long 0
1956 * .rva __my_dll_iname (name of dll)
1957 * .rva __fuNN_SYM (pointer to reference (address) in text)
1961 static bfd *
1962 make_import_fixup_entry (name, fixup_name, dll_symname,parent)
1963 char *name;
1964 char *fixup_name;
1965 char *dll_symname;
1966 bfd *parent;
1968 asection *id3;
1969 unsigned char *d3;
1970 char *oname;
1971 bfd *abfd;
1973 oname = (char *) xmalloc (20);
1974 sprintf (oname, "fu%06d.o", tmp_seq);
1975 tmp_seq++;
1977 abfd = bfd_create (oname, parent);
1978 bfd_find_target (pe_details->object_target, abfd);
1979 bfd_make_writable (abfd);
1981 bfd_set_format (abfd, bfd_object);
1982 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1984 symptr = 0;
1985 symtab = (asymbol **) xmalloc (6 * sizeof (asymbol *));
1986 id3 = quick_section (abfd, ".idata$3", SEC_HAS_CONTENTS, 2);
1988 quick_symbol (abfd, U("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0);
1990 quick_symbol (abfd, U ("_nm_thnk_"), name, "", UNDSEC, BSF_GLOBAL, 0);
1991 quick_symbol (abfd, U (""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
1992 quick_symbol (abfd, "", fixup_name, "", UNDSEC, BSF_GLOBAL, 0);
1994 bfd_set_section_size (abfd, id3, 20);
1995 d3 = (unsigned char *) xmalloc (20);
1996 id3->contents = d3;
1997 memset (d3, 0, 20);
1999 quick_reloc (abfd, 0, BFD_RELOC_RVA, 1);
2000 quick_reloc (abfd, 12, BFD_RELOC_RVA, 2);
2001 quick_reloc (abfd, 16, BFD_RELOC_RVA, 3);
2002 save_relocs (id3);
2004 bfd_set_symtab (abfd, symtab, symptr);
2006 bfd_set_section_contents (abfd, id3, d3, 0, 20);
2008 bfd_make_readable (abfd);
2009 return abfd;
2012 void
2013 pe_create_import_fixup (rel)
2014 arelent *rel;
2016 char buf[300];
2017 struct symbol_cache_entry *sym = *rel->sym_ptr_ptr;
2018 struct bfd_link_hash_entry *name_thunk_sym;
2019 CONST char *name = sym->name;
2020 char *fixup_name = make_import_fixup_mark (rel);
2022 sprintf (buf, U ("_nm_thnk_%s"), name);
2024 name_thunk_sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
2026 if (!name_thunk_sym || name_thunk_sym->type != bfd_link_hash_defined)
2028 bfd *b = make_singleton_name_thunk (name, output_bfd);
2029 add_bfd_to_link (b, b->filename, &link_info);
2031 /* If we ever use autoimport, we have to cast text section writable */
2032 config.text_read_only = false;
2036 extern char *pe_data_import_dll;
2037 bfd *b = make_import_fixup_entry (name, fixup_name, pe_data_import_dll,
2038 output_bfd);
2039 add_bfd_to_link (b, b->filename, &link_info);
2044 void
2045 pe_dll_generate_implib (def, impfilename)
2046 def_file *def;
2047 const char *impfilename;
2049 int i;
2050 bfd *ar_head;
2051 bfd *ar_tail;
2052 bfd *outarch;
2053 bfd *head = 0;
2055 dll_filename = (def->name) ? def->name : dll_name;
2056 dll_symname = xstrdup (dll_filename);
2057 for (i = 0; dll_symname[i]; i++)
2058 if (!isalnum ((unsigned char) dll_symname[i]))
2059 dll_symname[i] = '_';
2061 unlink (impfilename);
2063 outarch = bfd_openw (impfilename, 0);
2065 if (!outarch)
2067 /* xgettext:c-format */
2068 einfo (_("%XCan't open .lib file: %s\n"), impfilename);
2069 return;
2072 /* xgettext:c-format */
2073 einfo (_("Creating library file: %s\n"), impfilename);
2075 bfd_set_format (outarch, bfd_archive);
2076 outarch->has_armap = 1;
2078 /* Work out a reasonable size of things to put onto one line. */
2080 ar_head = make_head (outarch);
2082 for (i = 0; i < def->num_exports; i++)
2084 /* The import library doesn't know about the internal name. */
2085 char *internal = def->exports[i].internal_name;
2086 bfd *n;
2087 def->exports[i].internal_name = def->exports[i].name;
2088 n = make_one (def->exports + i, outarch);
2089 n->next = head;
2090 head = n;
2091 def->exports[i].internal_name = internal;
2094 ar_tail = make_tail (outarch);
2096 if (ar_head == NULL || ar_tail == NULL)
2097 return;
2099 /* Now stick them all into the archive. */
2101 ar_head->next = head;
2102 ar_tail->next = ar_head;
2103 head = ar_tail;
2105 if (! bfd_set_archive_head (outarch, head))
2106 einfo ("%Xbfd_set_archive_head: %s\n", bfd_errmsg (bfd_get_error ()));
2108 if (! bfd_close (outarch))
2109 einfo ("%Xbfd_close %s: %s\n", impfilename, bfd_errmsg (bfd_get_error ()));
2111 while (head != NULL)
2113 bfd *n = head->next;
2114 bfd_close (head);
2115 head = n;
2119 static void
2120 add_bfd_to_link (abfd, name, link_info)
2121 bfd *abfd;
2122 CONST char *name;
2123 struct bfd_link_info *link_info;
2125 lang_input_statement_type *fake_file;
2126 fake_file = lang_add_input_file (name,
2127 lang_input_file_is_fake_enum,
2128 NULL);
2129 fake_file->the_bfd = abfd;
2130 ldlang_add_file (fake_file);
2131 if (!bfd_link_add_symbols (abfd, link_info))
2132 einfo ("%Xaddsym %s: %s\n", name, bfd_errmsg (bfd_get_error ()));
2135 void
2136 pe_process_import_defs (output_bfd, link_info)
2137 bfd *output_bfd;
2138 struct bfd_link_info *link_info;
2140 def_file_module *module;
2141 pe_dll_id_target (bfd_get_target (output_bfd));
2143 if (!pe_def_file)
2144 return;
2146 for (module = pe_def_file->modules; module; module = module->next)
2148 int i, do_this_dll;
2150 dll_filename = module->name;
2151 dll_symname = xstrdup (module->name);
2152 for (i = 0; dll_symname[i]; i++)
2153 if (!isalnum (dll_symname[i]))
2154 dll_symname[i] = '_';
2156 do_this_dll = 0;
2158 for (i = 0; i < pe_def_file->num_imports; i++)
2159 if (pe_def_file->imports[i].module == module)
2161 def_file_export exp;
2162 struct bfd_link_hash_entry *blhe;
2164 /* See if we need this import. */
2165 char *name = (char *) xmalloc (strlen (pe_def_file->imports[i].internal_name) + 2 + 6);
2166 sprintf (name, "%s%s", U (""), pe_def_file->imports[i].internal_name);
2167 blhe = bfd_link_hash_lookup (link_info->hash, name,
2168 false, false, false);
2169 if (!blhe || (blhe && blhe->type != bfd_link_hash_undefined))
2171 sprintf (name, "%s%s", U ("_imp__"),
2172 pe_def_file->imports[i].internal_name);
2173 blhe = bfd_link_hash_lookup (link_info->hash, name,
2174 false, false, false);
2176 free (name);
2177 if (blhe && blhe->type == bfd_link_hash_undefined)
2179 bfd *one;
2180 /* We do. */
2181 if (!do_this_dll)
2183 bfd *ar_head = make_head (output_bfd);
2184 add_bfd_to_link (ar_head, ar_head->filename, link_info);
2185 do_this_dll = 1;
2187 exp.internal_name = pe_def_file->imports[i].internal_name;
2188 exp.name = pe_def_file->imports[i].name;
2189 exp.ordinal = pe_def_file->imports[i].ordinal;
2190 exp.hint = exp.ordinal >= 0 ? exp.ordinal : 0;
2191 exp.flag_private = 0;
2192 exp.flag_constant = 0;
2193 exp.flag_data = 0;
2194 exp.flag_noname = exp.name ? 0 : 1;
2195 one = make_one (&exp, output_bfd);
2196 add_bfd_to_link (one, one->filename, link_info);
2199 if (do_this_dll)
2201 bfd *ar_tail = make_tail (output_bfd);
2202 add_bfd_to_link (ar_tail, ar_tail->filename, link_info);
2205 free (dll_symname);
2209 /************************************************************************
2211 We were handed a *.DLL file. Parse it and turn it into a set of
2212 IMPORTS directives in the def file. Return true if the file was
2213 handled, false if not.
2215 ************************************************************************/
2217 static unsigned int
2218 pe_get16 (abfd, where)
2219 bfd *abfd;
2220 int where;
2222 unsigned char b[2];
2223 bfd_seek (abfd, where, SEEK_SET);
2224 bfd_read (b, 1, 2, abfd);
2225 return b[0] + (b[1] << 8);
2228 static unsigned int
2229 pe_get32 (abfd, where)
2230 bfd *abfd;
2231 int where;
2233 unsigned char b[4];
2234 bfd_seek (abfd, where, SEEK_SET);
2235 bfd_read (b, 1, 4, abfd);
2236 return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
2239 #if 0 /* This is not currently used. */
2241 static unsigned int
2242 pe_as16 (ptr)
2243 void *ptr;
2245 unsigned char *b = ptr;
2246 return b[0] + (b[1] << 8);
2249 #endif
2251 static unsigned int
2252 pe_as32 (ptr)
2253 void *ptr;
2255 unsigned char *b = ptr;
2256 return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
2259 boolean
2260 pe_implied_import_dll (filename)
2261 const char *filename;
2263 bfd *dll;
2264 unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
2265 unsigned long export_rva, export_size, nsections, secptr, expptr;
2266 unsigned char *expdata, *erva;
2267 unsigned long name_rvas, ordinals, nexp, ordbase;
2268 const char *dll_name;
2270 /* No, I can't use bfd here. kernel32.dll puts its export table in
2271 the middle of the .rdata section. */
2273 dll = bfd_openr (filename, pe_details->target_name);
2274 if (!dll)
2276 einfo ("%Xopen %s: %s\n", filename, bfd_errmsg (bfd_get_error ()));
2277 return false;
2279 /* PEI dlls seem to be bfd_objects. */
2280 if (!bfd_check_format (dll, bfd_object))
2282 einfo ("%X%s: this doesn't appear to be a DLL\n", filename);
2283 return false;
2286 dll_name = filename;
2287 for (i = 0; filename[i]; i++)
2288 if (filename[i] == '/' || filename[i] == '\\' || filename[i] == ':')
2289 dll_name = filename + i + 1;
2291 pe_header_offset = pe_get32 (dll, 0x3c);
2292 opthdr_ofs = pe_header_offset + 4 + 20;
2293 num_entries = pe_get32 (dll, opthdr_ofs + 92);
2294 if (num_entries < 1) /* no exports */
2295 return false;
2296 export_rva = pe_get32 (dll, opthdr_ofs + 96);
2297 export_size = pe_get32 (dll, opthdr_ofs + 100);
2298 nsections = pe_get16 (dll, pe_header_offset + 4 + 2);
2299 secptr = (pe_header_offset + 4 + 20 +
2300 pe_get16 (dll, pe_header_offset + 4 + 16));
2301 expptr = 0;
2302 for (i = 0; i < nsections; i++)
2304 char sname[8];
2305 unsigned long secptr1 = secptr + 40 * i;
2306 unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
2307 unsigned long vsize = pe_get32 (dll, secptr1 + 16);
2308 unsigned long fptr = pe_get32 (dll, secptr1 + 20);
2309 bfd_seek (dll, secptr1, SEEK_SET);
2310 bfd_read (sname, 1, 8, dll);
2311 if (vaddr <= export_rva && vaddr + vsize > export_rva)
2313 expptr = fptr + (export_rva - vaddr);
2314 if (export_rva + export_size > vaddr + vsize)
2315 export_size = vsize - (export_rva - vaddr);
2316 break;
2320 expdata = (unsigned char *) xmalloc (export_size);
2321 bfd_seek (dll, expptr, SEEK_SET);
2322 bfd_read (expdata, 1, export_size, dll);
2323 erva = expdata - export_rva;
2325 if (pe_def_file == 0)
2326 pe_def_file = def_file_empty ();
2328 nexp = pe_as32 (expdata + 24);
2329 name_rvas = pe_as32 (expdata + 32);
2330 ordinals = pe_as32 (expdata + 36);
2331 ordbase = pe_as32 (expdata + 16);
2332 for (i = 0; i < nexp; i++)
2334 unsigned long name_rva = pe_as32 (erva + name_rvas + i * 4);
2335 def_file_import *imp;
2336 imp = def_file_add_import (pe_def_file, erva + name_rva, dll_name,
2337 i, 0);
2340 return true;
2343 /************************************************************************
2345 These are the main functions, called from the emulation. The first
2346 is called after the bfds are read, so we can guess at how much space
2347 we need. The second is called after everything is placed, so we
2348 can put the right values in place.
2350 ************************************************************************/
2352 void
2353 pe_dll_build_sections (abfd, info)
2354 bfd *abfd;
2355 struct bfd_link_info *info;
2357 pe_dll_id_target (bfd_get_target (abfd));
2358 process_def_file (abfd, info);
2360 generate_edata (abfd, info);
2361 build_filler_bfd (1);
2364 void
2365 pe_exe_build_sections (abfd, info)
2366 bfd *abfd;
2367 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2369 pe_dll_id_target (bfd_get_target (abfd));
2370 build_filler_bfd (0);
2373 void
2374 pe_dll_fill_sections (abfd, info)
2375 bfd *abfd;
2376 struct bfd_link_info *info;
2378 pe_dll_id_target (bfd_get_target (abfd));
2379 image_base = pe_data (abfd)->pe_opthdr.ImageBase;
2381 generate_reloc (abfd, info);
2382 if (reloc_sz > 0)
2384 bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
2386 /* Resize the sections. */
2387 lang_size_sections (stat_ptr->head, abs_output_section,
2388 &stat_ptr->head, 0, (bfd_vma) 0, NULL);
2390 /* Redo special stuff. */
2391 ldemul_after_allocation ();
2393 /* Do the assignments again. */
2394 lang_do_assignments (stat_ptr->head,
2395 abs_output_section,
2396 (fill_type) 0, (bfd_vma) 0);
2399 fill_edata (abfd, info);
2401 pe_data (abfd)->dll = 1;
2403 edata_s->contents = edata_d;
2404 reloc_s->contents = reloc_d;
2407 void
2408 pe_exe_fill_sections (abfd, info)
2409 bfd *abfd;
2410 struct bfd_link_info *info;
2412 pe_dll_id_target (bfd_get_target (abfd));
2413 image_base = pe_data (abfd)->pe_opthdr.ImageBase;
2415 generate_reloc (abfd, info);
2416 if (reloc_sz > 0)
2418 bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
2420 /* Resize the sections. */
2421 lang_size_sections (stat_ptr->head, abs_output_section,
2422 &stat_ptr->head, 0, (bfd_vma) 0, NULL);
2424 /* Redo special stuff. */
2425 ldemul_after_allocation ();
2427 /* Do the assignments again. */
2428 lang_do_assignments (stat_ptr->head,
2429 abs_output_section,
2430 (fill_type) 0, (bfd_vma) 0);
2432 reloc_s->contents = reloc_d;