* ldmain.c (main): Rename BufferSize to ld_bufsz because HPUX
[binutils.git] / ld / pe-dll.c
blob2f326481170d5b07fd865a115a9a63cf9081179c
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 /* This file turns a regular Windows PE image into a DLL. Because of
45 the complexity of this operation, it has been broken down into a
46 number of separate modules which are all called by the main function
47 at the end of this file. This function is not re-entrant and is
48 normally only called once, so static variables are used to reduce
49 the number of parameters and return values required.
51 See also: ld/emultempl/pe.em. */
53 /* Auto-import feature by Paul Sokolovsky
55 Quick facts:
57 1. With this feature on, DLL clients can import variables from DLL
58 without any concern from their side (for example, without any source
59 code modifications).
61 2. This is done completely in bounds of the PE specification (to be fair,
62 there's a place where it pokes nose out of, but in practise it works).
63 So, resulting module can be used with any other PE compiler/linker.
65 3. Auto-import is fully compatible with standard import method and they
66 can be mixed together.
68 4. Overheads: space: 8 bytes per imported symbol, plus 20 for each
69 reference to it; load time: negligible; virtual/physical memory: should be
70 less than effect of DLL relocation, and I sincerely hope it doesn't affect
71 DLL sharability (too much).
73 Idea
75 The obvious and only way to get rid of dllimport insanity is to make client
76 access variable directly in the DLL, bypassing extra dereference. I.e.,
77 whenever client contains someting like
79 mov dll_var,%eax,
81 address of dll_var in the command should be relocated to point into loaded
82 DLL. The aim is to make OS loader do so, and than make ld help with that.
83 Import section of PE made following way: there's a vector of structures
84 each describing imports from particular DLL. Each such structure points
85 to two other parellel vectors: one holding imported names, and one which
86 will hold address of corresponding imported name. So, the solution is
87 de-vectorize these structures, making import locations be sparse and
88 pointing directly into code. Before continuing, it is worth a note that,
89 while authors strives to make PE act ELF-like, there're some other people
90 make ELF act PE-like: elfvector, ;-) .
92 Implementation
94 For each reference of data symbol to be imported from DLL (to set of which
95 belong symbols with name <sym>, if __imp_<sym> is found in implib), the
96 import fixup entry is generated. That entry is of type
97 IMAGE_IMPORT_DESCRIPTOR and stored in .idata$3 subsection. Each
98 fixup entry contains pointer to symbol's address within .text section
99 (marked with __fuN_<sym> symbol, where N is integer), pointer to DLL name
100 (so, DLL name is referenced by multiple entries), and pointer to symbol
101 name thunk. Symbol name thunk is singleton vector (__nm_th_<symbol>)
102 pointing to IMAGE_IMPORT_BY_NAME structure (__nm_<symbol>) directly
103 containing imported name. Here comes that "om the edge" problem mentioned
104 above: PE specification rambles that name vector (OriginalFirstThunk)
105 should run in parallel with addresses vector (FirstThunk), i.e. that they
106 should have same number of elements and terminated with zero. We violate
107 this, since FirstThunk points directly into machine code. But in practise,
108 OS loader implemented the sane way: it goes thru OriginalFirstThunk and
109 puts addresses to FirstThunk, not something else. It once again should be
110 noted that dll and symbol name structures are reused across fixup entries
111 and should be there anyway to support standard import stuff, so sustained
112 overhead is 20 bytes per reference. Other question is whether having several
113 IMAGE_IMPORT_DESCRIPTORS for the same DLL is possible. Answer is yes, it is
114 done even by native compiler/linker (libth32's functions are in fact reside
115 in windows9x kernel32.dll, so if you use it, you have two
116 IMAGE_IMPORT_DESCRIPTORS for kernel32.dll). Yet other question is whether
117 referencing the same PE structures several times is valid. The answer is why
118 not, prohibitting that (detecting violation) would require more work on
119 behalf of loader than not doing it.
121 See also: ld/emultempl/pe.em. */
123 static void
124 add_bfd_to_link PARAMS ((bfd *, const char *, struct bfd_link_info *));
126 /* For emultempl/pe.em. */
128 def_file * pe_def_file = 0;
129 int pe_dll_export_everything = 0;
130 int pe_dll_do_default_excludes = 1;
131 int pe_dll_kill_ats = 0;
132 int pe_dll_stdcall_aliases = 0;
133 int pe_dll_warn_dup_exports = 0;
134 int pe_dll_compat_implib = 0;
135 int pe_dll_extra_pe_debug = 0;
137 /* Static variables and types. */
139 static bfd_vma image_base;
140 static bfd *filler_bfd;
141 static struct sec *edata_s, *reloc_s;
142 static unsigned char *edata_d, *reloc_d;
143 static size_t edata_sz, reloc_sz;
145 typedef struct
147 char *target_name;
148 char *object_target;
149 unsigned int imagebase_reloc;
150 int pe_arch;
151 int bfd_arch;
152 int underscored;
154 pe_details_type;
156 typedef struct
158 char *name;
159 int len;
161 autofilter_entry_type;
163 #define PE_ARCH_i386 1
164 #define PE_ARCH_sh 2
165 #define PE_ARCH_mips 3
166 #define PE_ARCH_arm 4
167 #define PE_ARCH_arm_epoc 5
169 static pe_details_type pe_detail_list[] =
172 "pei-i386",
173 "pe-i386",
174 7 /* R_IMAGEBASE */,
175 PE_ARCH_i386,
176 bfd_arch_i386,
180 "pei-shl",
181 "pe-shl",
182 16 /* R_SH_IMAGEBASE */,
183 PE_ARCH_sh,
184 bfd_arch_sh,
188 "pei-mips",
189 "pe-mips",
190 34 /* MIPS_R_RVA */,
191 PE_ARCH_mips,
192 bfd_arch_mips,
196 "pei-arm-little",
197 "pe-arm-little",
198 11 /* ARM_RVA32 */,
199 PE_ARCH_arm,
200 bfd_arch_arm,
204 "epoc-pei-arm-little",
205 "epoc-pe-arm-little",
206 11 /* ARM_RVA32 */,
207 PE_ARCH_arm_epoc,
208 bfd_arch_arm,
211 { NULL, NULL, 0, 0, 0, 0 }
214 static pe_details_type *pe_details;
216 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 }
228 /* Do not specify library suffix explicitly, to allow for dllized versions. */
229 static autofilter_entry_type autofilter_liblist[] =
231 { "libgcc.", 7 },
232 { "libstdc++.", 10 },
233 { "libmingw32.", 11 },
234 { NULL, 0 }
237 static autofilter_entry_type autofilter_objlist[] =
239 { "crt0.o", 6 },
240 { "crt1.o", 6 },
241 { "crt2.o", 6 },
242 { NULL, 0 }
245 static autofilter_entry_type autofilter_symbolprefixlist[] =
247 /* { "__imp_", 6 }, */
248 /* Do __imp_ explicitly to save time. */
249 { "__rtti_", 7 },
250 { "__builtin_", 10 },
251 /* Don't export symbols specifying internal DLL layout. */
252 { "_head_", 6 },
253 { "_fmode", 6 },
254 { "_impure_ptr", 11 },
255 { "cygwin_attach_dll", 17 },
256 { "cygwin_premain0", 15 },
257 { "cygwin_premain1", 15 },
258 { "cygwin_premain2", 15 },
259 { "cygwin_premain3", 15 },
260 { "environ", 7 },
261 { NULL, 0 }
264 static autofilter_entry_type autofilter_symbolsuffixlist[] =
266 { "_iname", 6 },
267 { NULL, 0 }
270 #define U(str) (pe_details->underscored ? "_" str : str)
272 static int reloc_sort PARAMS ((const void *, const void *));
273 static int pe_export_sort PARAMS ((const void *, const void *));
274 static int auto_export PARAMS ((bfd *, def_file *, const char *));
275 static void process_def_file PARAMS ((bfd *, struct bfd_link_info *));
276 static void build_filler_bfd PARAMS ((int));
277 static void generate_edata PARAMS ((bfd *, struct bfd_link_info *));
278 static void fill_exported_offsets PARAMS ((bfd *, struct bfd_link_info *));
279 static void fill_edata PARAMS ((bfd *, struct bfd_link_info *));
280 static void generate_reloc PARAMS ((bfd *, struct bfd_link_info *));
281 static void quoteput PARAMS ((char *, FILE *, int));
282 static asection *quick_section PARAMS ((bfd *, const char *, int, int));
283 static void quick_symbol
284 PARAMS ((bfd *, char *, char *, char *, asection *, int, int));
285 static void quick_reloc PARAMS ((bfd *, int, int, int));
286 static bfd *make_head PARAMS ((bfd *));
287 static bfd *make_tail PARAMS ((bfd *));
288 static bfd *make_one PARAMS ((def_file_export *, bfd *));
289 static bfd *make_singleton_name_thunk PARAMS ((char *, bfd *));
290 static char *make_import_fixup_mark PARAMS ((arelent *));
291 static bfd *make_import_fixup_entry PARAMS ((char *, char *, char *, bfd *));
292 static unsigned int pe_get16 PARAMS ((bfd *, int));
293 static unsigned int pe_get32 PARAMS ((bfd *, int));
294 static unsigned int pe_as32 PARAMS ((void *));
296 void
297 pe_dll_id_target (target)
298 const char *target;
300 int i;
302 for (i = 0; pe_detail_list[i].target_name; i++)
303 if (strcmp (pe_detail_list[i].target_name, target) == 0
304 || strcmp (pe_detail_list[i].object_target, target) == 0)
306 pe_details = pe_detail_list + i;
307 return;
309 einfo (_("%XUnsupported PEI architecture: %s\n"), target);
310 exit (1);
313 /* Helper functions for qsort. Relocs must be sorted so that we can write
314 them out by pages. */
316 typedef struct
318 bfd_vma vma;
319 char type;
320 short extra;
322 reloc_data_type;
324 static int
325 reloc_sort (va, vb)
326 const void *va, *vb;
328 bfd_vma a = ((reloc_data_type *) va)->vma;
329 bfd_vma b = ((reloc_data_type *) vb)->vma;
331 return (a > b) ? 1 : ((a < b) ? -1 : 0);
334 static int
335 pe_export_sort (va, vb)
336 const void *va, *vb;
338 def_file_export *a = (def_file_export *) va;
339 def_file_export *b = (def_file_export *) vb;
341 return strcmp (a->name, b->name);
344 /* Read and process the .DEF file. */
346 /* These correspond to the entries in pe_def_file->exports[]. I use
347 exported_symbol_sections[i] to tag whether or not the symbol was
348 defined, since we can't export symbols we don't have. */
350 static bfd_vma *exported_symbol_offsets;
351 static struct sec **exported_symbol_sections;
352 static int export_table_size;
353 static int count_exported;
354 static int count_exported_byname;
355 static int count_with_ordinals;
356 static const char *dll_name;
357 static int min_ordinal, max_ordinal;
358 static int *exported_symbols;
360 typedef struct exclude_list_struct
362 char *string;
363 struct exclude_list_struct *next;
365 exclude_list_struct;
367 static struct exclude_list_struct *excludes = 0;
369 void
370 pe_dll_add_excludes (new_excludes)
371 const char *new_excludes;
373 char *local_copy;
374 char *exclude_string;
376 local_copy = xstrdup (new_excludes);
378 exclude_string = strtok (local_copy, ",:");
379 for (; exclude_string; exclude_string = strtok (NULL, ",:"))
381 struct exclude_list_struct *new_exclude;
383 new_exclude = ((struct exclude_list_struct *)
384 xmalloc (sizeof (struct exclude_list_struct)));
385 new_exclude->string = (char *) xmalloc (strlen (exclude_string) + 1);
386 strcpy (new_exclude->string, exclude_string);
387 new_exclude->next = excludes;
388 excludes = new_exclude;
391 free (local_copy);
394 /* abfd is a bfd containing n (or NULL)
395 It can be used for contextual checks. */
397 static int
398 auto_export (abfd, d, n)
399 bfd *abfd;
400 def_file *d;
401 const char *n;
403 int i;
404 struct exclude_list_struct *ex;
405 autofilter_entry_type *afptr;
407 /* We should not re-export imported stuff. */
408 if (strncmp (n, "_imp__", 6) == 0)
409 return 0;
411 for (i = 0; i < d->num_exports; i++)
412 if (strcmp (d->exports[i].name, n) == 0)
413 return 0;
415 if (pe_dll_do_default_excludes)
417 char * p;
418 int len;
420 if (pe_dll_extra_pe_debug)
421 printf ("considering exporting: %s, abfd=%p, abfd->my_arc=%p\n",
422 n, abfd, abfd->my_archive);
424 /* First of all, make context checks:
425 Don't export anything from libgcc. */
426 if (abfd && abfd->my_archive)
428 afptr = autofilter_liblist;
430 while (afptr->name)
432 if (strstr (abfd->my_archive->filename, afptr->name))
433 return 0;
434 afptr++;
438 /* Next, exclude symbols from certain startup objects. */
439 afptr = autofilter_objlist;
441 while (afptr->name)
443 if (abfd &&
444 (p = strstr (abfd->filename, afptr->name)) &&
445 (*(p + afptr->len - 1) == 0))
446 return 0;
448 afptr ++;
451 /* Don't try to blindly exclude all symbols
452 that begin with '__'; this was tried and
453 it is too restrictive. */
455 /* Then, exclude specific symbols. */
456 afptr = autofilter_symbollist;
457 while (afptr->name)
459 if (strcmp (n, afptr->name) == 0)
460 return 0;
462 afptr ++;
465 /* Next, exclude symbols starting with ... */
466 afptr = autofilter_symbolprefixlist;
467 while (afptr->name)
469 if (strncmp (n, afptr->name, afptr->len) == 0)
470 return 0;
472 afptr ++;
475 /* Finally, exclude symbols ending with ... */
476 len = strlen (n);
477 afptr = autofilter_symbolsuffixlist;
478 while (afptr->name)
480 if ((len >= afptr->len) &&
481 /* Add 1 to insure match with trailing '\0'. */
482 strncmp (n + len - afptr->len, afptr->name,
483 afptr->len + 1) == 0)
484 return 0;
486 afptr ++;
490 for (ex = excludes; ex; ex = ex->next)
491 if (strcmp (n, ex->string) == 0)
492 return 0;
494 return 1;
497 static void
498 process_def_file (abfd, info)
499 bfd *abfd ATTRIBUTE_UNUSED;
500 struct bfd_link_info *info;
502 int i, j;
503 struct bfd_link_hash_entry *blhe;
504 bfd *b;
505 struct sec *s;
506 def_file_export *e = 0;
508 if (!pe_def_file)
509 pe_def_file = def_file_empty ();
511 /* First, run around to all the objects looking for the .drectve
512 sections, and push those into the def file too. */
513 for (b = info->input_bfds; b; b = b->link_next)
515 s = bfd_get_section_by_name (b, ".drectve");
516 if (s)
518 int size = bfd_get_section_size_before_reloc (s);
519 char *buf = xmalloc (size);
521 bfd_get_section_contents (b, s, buf, 0, size);
522 def_file_add_directive (pe_def_file, buf, size);
523 free (buf);
527 /* Now, maybe export everything else the default way. */
528 if (pe_dll_export_everything || pe_def_file->num_exports == 0)
530 for (b = info->input_bfds; b; b = b->link_next)
532 asymbol **symbols;
533 int nsyms, symsize;
535 symsize = bfd_get_symtab_upper_bound (b);
536 symbols = (asymbol **) xmalloc (symsize);
537 nsyms = bfd_canonicalize_symtab (b, symbols);
539 for (j = 0; j < nsyms; j++)
541 /* We should export symbols which are either global or not
542 anything at all. (.bss data is the latter)
543 We should not export undefined symbols. */
544 if (symbols[j]->section != &bfd_und_section
545 && ((symbols[j]->flags & BSF_GLOBAL)
546 || (symbols[j]->flags == BFD_FORT_COMM_DEFAULT_VALUE)))
548 const char *sn = symbols[j]->name;
550 /* We should not re-export imported stuff. */
552 char *name = (char *) xmalloc (strlen (sn) + 2 + 6);
553 sprintf (name, "%s%s", U("_imp_"), sn);
555 blhe = bfd_link_hash_lookup (info->hash, name,
556 false, false, false);
557 free (name);
559 if (blhe && blhe->type == bfd_link_hash_defined)
560 continue;
563 if (*sn == '_')
564 sn++;
566 if (auto_export (b, pe_def_file, sn))
568 def_file_export *p;
569 p=def_file_add_export (pe_def_file, sn, 0, -1);
570 /* Fill data flag properly, from dlltool.c. */
571 p->flag_data = !(symbols[j]->flags & BSF_FUNCTION);
578 #undef NE
579 #define NE pe_def_file->num_exports
581 /* Canonicalize the export list. */
582 if (pe_dll_kill_ats)
584 for (i = 0; i < NE; i++)
586 if (strchr (pe_def_file->exports[i].name, '@'))
588 /* This will preserve internal_name, which may have been
589 pointing to the same memory as name, or might not
590 have. */
591 char *tmp = xstrdup (pe_def_file->exports[i].name);
593 *(strchr (tmp, '@')) = 0;
594 pe_def_file->exports[i].name = tmp;
599 if (pe_dll_stdcall_aliases)
601 for (i = 0; i < NE; i++)
603 if (strchr (pe_def_file->exports[i].name, '@'))
605 char *tmp = xstrdup (pe_def_file->exports[i].name);
607 *(strchr (tmp, '@')) = 0;
608 if (auto_export (NULL, pe_def_file, tmp))
609 def_file_add_export (pe_def_file, tmp,
610 pe_def_file->exports[i].internal_name,
611 -1);
612 else
613 free (tmp);
618 /* Convenience, but watch out for it changing. */
619 e = pe_def_file->exports;
621 exported_symbol_offsets = (bfd_vma *) xmalloc (NE * sizeof (bfd_vma));
622 exported_symbol_sections = (struct sec **) xmalloc (NE * sizeof (struct sec *));
624 memset (exported_symbol_sections, 0, NE * sizeof (struct sec *));
625 max_ordinal = 0;
626 min_ordinal = 65536;
627 count_exported = 0;
628 count_exported_byname = 0;
629 count_with_ordinals = 0;
631 qsort (pe_def_file->exports, NE, sizeof (pe_def_file->exports[0]), pe_export_sort);
632 for (i = 0, j = 0; i < NE; i++)
634 if (i > 0 && strcmp (e[i].name, e[i - 1].name) == 0)
636 /* This is a duplicate. */
637 if (e[j - 1].ordinal != -1
638 && e[i].ordinal != -1
639 && e[j - 1].ordinal != e[i].ordinal)
641 if (pe_dll_warn_dup_exports)
642 /* xgettext:c-format */
643 einfo (_("%XError, duplicate EXPORT with ordinals: %s (%d vs %d)\n"),
644 e[j - 1].name, e[j - 1].ordinal, e[i].ordinal);
646 else
648 if (pe_dll_warn_dup_exports)
649 /* xgettext:c-format */
650 einfo (_("Warning, duplicate EXPORT: %s\n"),
651 e[j - 1].name);
654 if (e[i].ordinal != -1)
655 e[j - 1].ordinal = e[i].ordinal;
656 e[j - 1].flag_private |= e[i].flag_private;
657 e[j - 1].flag_constant |= e[i].flag_constant;
658 e[j - 1].flag_noname |= e[i].flag_noname;
659 e[j - 1].flag_data |= e[i].flag_data;
661 else
663 if (i != j)
664 e[j] = e[i];
665 j++;
668 pe_def_file->num_exports = j; /* == NE */
670 for (i = 0; i < NE; i++)
672 char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
674 if (pe_details->underscored)
676 *name = '_';
677 strcpy (name + 1, pe_def_file->exports[i].internal_name);
679 else
680 strcpy (name, pe_def_file->exports[i].internal_name);
682 blhe = bfd_link_hash_lookup (info->hash,
683 name,
684 false, false, true);
686 if (blhe
687 && (blhe->type == bfd_link_hash_defined
688 || (blhe->type == bfd_link_hash_common)))
690 count_exported++;
691 if (!pe_def_file->exports[i].flag_noname)
692 count_exported_byname++;
694 /* Only fill in the sections. The actual offsets are computed
695 in fill_exported_offsets() after common symbols are laid
696 out. */
697 if (blhe->type == bfd_link_hash_defined)
698 exported_symbol_sections[i] = blhe->u.def.section;
699 else
700 exported_symbol_sections[i] = blhe->u.c.p->section;
702 if (pe_def_file->exports[i].ordinal != -1)
704 if (max_ordinal < pe_def_file->exports[i].ordinal)
705 max_ordinal = pe_def_file->exports[i].ordinal;
706 if (min_ordinal > pe_def_file->exports[i].ordinal)
707 min_ordinal = pe_def_file->exports[i].ordinal;
708 count_with_ordinals++;
711 else if (blhe && blhe->type == bfd_link_hash_undefined)
713 /* xgettext:c-format */
714 einfo (_("%XCannot export %s: symbol not defined\n"),
715 pe_def_file->exports[i].internal_name);
717 else if (blhe)
719 /* xgettext:c-format */
720 einfo (_("%XCannot export %s: symbol wrong type (%d vs %d)\n"),
721 pe_def_file->exports[i].internal_name,
722 blhe->type, bfd_link_hash_defined);
724 else
726 /* xgettext:c-format */
727 einfo (_("%XCannot export %s: symbol not found\n"),
728 pe_def_file->exports[i].internal_name);
730 free (name);
734 /* Build the bfd that will contain .edata and .reloc sections. */
736 static void
737 build_filler_bfd (include_edata)
738 int include_edata;
740 lang_input_statement_type *filler_file;
741 filler_file = lang_add_input_file ("dll stuff",
742 lang_input_file_is_fake_enum,
743 NULL);
744 filler_file->the_bfd = filler_bfd = bfd_create ("dll stuff", output_bfd);
745 if (filler_bfd == NULL
746 || !bfd_set_arch_mach (filler_bfd,
747 bfd_get_arch (output_bfd),
748 bfd_get_mach (output_bfd)))
750 einfo ("%X%P: can not create BFD %E\n");
751 return;
754 if (include_edata)
756 edata_s = bfd_make_section_old_way (filler_bfd, ".edata");
757 if (edata_s == NULL
758 || !bfd_set_section_flags (filler_bfd, edata_s,
759 (SEC_HAS_CONTENTS
760 | SEC_ALLOC
761 | SEC_LOAD
762 | SEC_KEEP
763 | SEC_IN_MEMORY)))
765 einfo ("%X%P: can not create .edata section: %E\n");
766 return;
768 bfd_set_section_size (filler_bfd, edata_s, edata_sz);
771 reloc_s = bfd_make_section_old_way (filler_bfd, ".reloc");
772 if (reloc_s == NULL
773 || !bfd_set_section_flags (filler_bfd, reloc_s,
774 (SEC_HAS_CONTENTS
775 | SEC_ALLOC
776 | SEC_LOAD
777 | SEC_KEEP
778 | SEC_IN_MEMORY)))
780 einfo ("%X%P: can not create .reloc section: %E\n");
781 return;
784 bfd_set_section_size (filler_bfd, reloc_s, 0);
786 ldlang_add_file (filler_file);
789 /* Gather all the exported symbols and build the .edata section. */
791 static void
792 generate_edata (abfd, info)
793 bfd *abfd;
794 struct bfd_link_info *info ATTRIBUTE_UNUSED;
796 int i, next_ordinal;
797 int name_table_size = 0;
798 const char *dlnp;
800 /* First, we need to know how many exported symbols there are,
801 and what the range of ordinals is. */
802 if (pe_def_file->name)
803 dll_name = pe_def_file->name;
804 else
806 dll_name = abfd->filename;
808 for (dlnp = dll_name; *dlnp; dlnp++)
809 if (*dlnp == '\\' || *dlnp == '/' || *dlnp == ':')
810 dll_name = dlnp + 1;
813 if (count_with_ordinals && max_ordinal > count_exported)
815 if (min_ordinal > max_ordinal - count_exported + 1)
816 min_ordinal = max_ordinal - count_exported + 1;
818 else
820 min_ordinal = 1;
821 max_ordinal = count_exported;
824 export_table_size = max_ordinal - min_ordinal + 1;
825 exported_symbols = (int *) xmalloc (export_table_size * sizeof (int));
826 for (i = 0; i < export_table_size; i++)
827 exported_symbols[i] = -1;
829 /* Now we need to assign ordinals to those that don't have them. */
830 for (i = 0; i < NE; i++)
832 if (exported_symbol_sections[i])
834 if (pe_def_file->exports[i].ordinal != -1)
836 int ei = pe_def_file->exports[i].ordinal - min_ordinal;
837 int pi = exported_symbols[ei];
839 if (pi != -1)
841 /* xgettext:c-format */
842 einfo (_("%XError, ordinal used twice: %d (%s vs %s)\n"),
843 pe_def_file->exports[i].ordinal,
844 pe_def_file->exports[i].name,
845 pe_def_file->exports[pi].name);
847 exported_symbols[ei] = i;
849 name_table_size += strlen (pe_def_file->exports[i].name) + 1;
853 next_ordinal = min_ordinal;
854 for (i = 0; i < NE; i++)
855 if (exported_symbol_sections[i])
856 if (pe_def_file->exports[i].ordinal == -1)
858 while (exported_symbols[next_ordinal - min_ordinal] != -1)
859 next_ordinal ++;
861 exported_symbols[next_ordinal - min_ordinal] = i;
862 pe_def_file->exports[i].ordinal = next_ordinal;
865 /* OK, now we can allocate some memory. */
866 edata_sz = (40 /* directory */
867 + 4 * export_table_size /* addresses */
868 + 4 * count_exported_byname /* name ptrs */
869 + 2 * count_exported_byname /* ordinals */
870 + name_table_size + strlen (dll_name) + 1);
873 /* Fill the exported symbol offsets. The preliminary work has already
874 been done in process_def_file(). */
876 static void
877 fill_exported_offsets (abfd, info)
878 bfd *abfd ATTRIBUTE_UNUSED;
879 struct bfd_link_info *info;
881 int i;
882 struct bfd_link_hash_entry *blhe;
884 for (i = 0; i < pe_def_file->num_exports; i++)
886 char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
888 if (pe_details->underscored)
890 *name = '_';
891 strcpy (name + 1, pe_def_file->exports[i].internal_name);
893 else
894 strcpy (name, pe_def_file->exports[i].internal_name);
896 blhe = bfd_link_hash_lookup (info->hash,
897 name,
898 false, false, true);
900 if (blhe && (blhe->type == bfd_link_hash_defined))
901 exported_symbol_offsets[i] = blhe->u.def.value;
903 free (name);
907 static void
908 fill_edata (abfd, info)
909 bfd *abfd;
910 struct bfd_link_info *info ATTRIBUTE_UNUSED;
912 int i, hint;
913 unsigned char *edirectory;
914 unsigned long *eaddresses;
915 unsigned long *enameptrs;
916 unsigned short *eordinals;
917 unsigned char *enamestr;
918 time_t now;
920 time (&now);
922 edata_d = (unsigned char *) xmalloc (edata_sz);
924 /* Note use of array pointer math here. */
925 edirectory = edata_d;
926 eaddresses = (unsigned long *) (edata_d + 40);
927 enameptrs = eaddresses + export_table_size;
928 eordinals = (unsigned short *) (enameptrs + count_exported_byname);
929 enamestr = (char *) (eordinals + count_exported_byname);
931 #define ERVA(ptr) (((unsigned char *)(ptr) - edata_d) + edata_s->output_section->vma - image_base)
933 memset (edata_d, 0, edata_sz);
934 bfd_put_32 (abfd, now, edata_d + 4);
935 if (pe_def_file->version_major != -1)
937 bfd_put_16 (abfd, pe_def_file->version_major, edata_d + 8);
938 bfd_put_16 (abfd, pe_def_file->version_minor, edata_d + 10);
941 bfd_put_32 (abfd, ERVA (enamestr), edata_d + 12);
942 strcpy (enamestr, dll_name);
943 enamestr += strlen (enamestr) + 1;
944 bfd_put_32 (abfd, min_ordinal, edata_d + 16);
945 bfd_put_32 (abfd, export_table_size, edata_d + 20);
946 bfd_put_32 (abfd, count_exported_byname, edata_d + 24);
947 bfd_put_32 (abfd, ERVA (eaddresses), edata_d + 28);
948 bfd_put_32 (abfd, ERVA (enameptrs), edata_d + 32);
949 bfd_put_32 (abfd, ERVA (eordinals), edata_d + 36);
951 fill_exported_offsets (abfd, info);
953 /* Ok, now for the filling in part. */
954 hint = 0;
955 for (i = 0; i < export_table_size; i++)
957 int s = exported_symbols[i];
959 if (s != -1)
961 struct sec *ssec = exported_symbol_sections[s];
962 unsigned long srva = (exported_symbol_offsets[s]
963 + ssec->output_section->vma
964 + ssec->output_offset);
965 int ord = pe_def_file->exports[s].ordinal;
967 bfd_put_32 (abfd, srva - image_base,
968 (void *) (eaddresses + ord - min_ordinal));
970 if (!pe_def_file->exports[s].flag_noname)
972 char *ename = pe_def_file->exports[s].name;
973 bfd_put_32 (abfd, ERVA (enamestr), (void *) enameptrs);
974 enameptrs++;
975 strcpy (enamestr, ename);
976 enamestr += strlen (enamestr) + 1;
977 bfd_put_16 (abfd, ord - min_ordinal, (void *) eordinals);
978 eordinals++;
979 pe_def_file->exports[s].hint = hint++;
986 static struct sec *current_sec;
988 void
989 pe_walk_relocs_of_symbol (info, name, cb)
990 struct bfd_link_info *info;
991 CONST char *name;
992 int (*cb) (arelent *, asection *);
994 bfd *b;
995 asection *s;
997 for (b = info->input_bfds; b; b = b->link_next)
999 asymbol **symbols;
1000 int nsyms, symsize;
1002 symsize = bfd_get_symtab_upper_bound (b);
1003 symbols = (asymbol **) xmalloc (symsize);
1004 nsyms = bfd_canonicalize_symtab (b, symbols);
1006 for (s = b->sections; s; s = s->next)
1008 arelent **relocs;
1009 int relsize, nrelocs, i;
1010 int flags = bfd_get_section_flags (b, s);
1012 /* Skip discarded linkonce sections. */
1013 if (flags & SEC_LINK_ONCE
1014 && s->output_section == bfd_abs_section_ptr)
1015 continue;
1017 current_sec = s;
1019 relsize = bfd_get_reloc_upper_bound (b, s);
1020 relocs = (arelent **) xmalloc ((size_t) relsize);
1021 nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols);
1023 for (i = 0; i < nrelocs; i++)
1025 struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr;
1027 if (!strcmp (name, sym->name))
1028 cb (relocs[i], s);
1031 free (relocs);
1033 /* Warning: the allocated symbols are remembered in BFD and reused
1034 later, so don't free them! */
1035 /* free (symbols); */
1040 /* Gather all the relocations and build the .reloc section. */
1042 static void
1043 generate_reloc (abfd, info)
1044 bfd *abfd;
1045 struct bfd_link_info *info;
1048 /* For .reloc stuff. */
1049 reloc_data_type *reloc_data;
1050 int total_relocs = 0;
1051 int i;
1052 unsigned long sec_page = (unsigned long) (-1);
1053 unsigned long page_ptr, page_count;
1054 int bi;
1055 bfd *b;
1056 struct sec *s;
1058 total_relocs = 0;
1059 for (b = info->input_bfds; b; b = b->link_next)
1060 for (s = b->sections; s; s = s->next)
1061 total_relocs += s->reloc_count;
1063 reloc_data =
1064 (reloc_data_type *) xmalloc (total_relocs * sizeof (reloc_data_type));
1066 total_relocs = 0;
1067 bi = 0;
1068 for (bi = 0, b = info->input_bfds; b; bi++, b = b->link_next)
1070 arelent **relocs;
1071 int relsize, nrelocs, i;
1073 for (s = b->sections; s; s = s->next)
1075 unsigned long sec_vma = s->output_section->vma + s->output_offset;
1076 asymbol **symbols;
1077 int nsyms, symsize;
1079 /* If it's not loaded, we don't need to relocate it this way. */
1080 if (!(s->output_section->flags & SEC_LOAD))
1081 continue;
1083 /* I don't know why there would be a reloc for these, but I've
1084 seen it happen - DJ */
1085 if (s->output_section == &bfd_abs_section)
1086 continue;
1088 if (s->output_section->vma == 0)
1090 /* Huh? Shouldn't happen, but punt if it does. */
1091 einfo ("DJ: zero vma section reloc detected: `%s' #%d f=%d\n",
1092 s->output_section->name, s->output_section->index,
1093 s->output_section->flags);
1094 continue;
1097 symsize = bfd_get_symtab_upper_bound (b);
1098 symbols = (asymbol **) xmalloc (symsize);
1099 nsyms = bfd_canonicalize_symtab (b, symbols);
1101 relsize = bfd_get_reloc_upper_bound (b, s);
1102 relocs = (arelent **) xmalloc ((size_t) relsize);
1103 nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols);
1105 for (i = 0; i < nrelocs; i++)
1107 if (pe_dll_extra_pe_debug)
1109 struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr;
1110 printf("rel: %s\n",sym->name);
1112 if (!relocs[i]->howto->pc_relative
1113 && relocs[i]->howto->type != pe_details->imagebase_reloc)
1115 bfd_vma sym_vma;
1116 struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr;
1118 sym_vma = (relocs[i]->addend
1119 + sym->value
1120 + sym->section->vma
1121 + sym->section->output_offset
1122 + sym->section->output_section->vma);
1123 reloc_data[total_relocs].vma = sec_vma + relocs[i]->address;
1125 #define BITS_AND_SHIFT(bits, shift) (bits * 1000 | shift)
1127 switch BITS_AND_SHIFT (relocs[i]->howto->bitsize,
1128 relocs[i]->howto->rightshift)
1130 case BITS_AND_SHIFT (32, 0):
1131 reloc_data[total_relocs].type = 3;
1132 total_relocs++;
1133 break;
1134 case BITS_AND_SHIFT (16, 0):
1135 reloc_data[total_relocs].type = 2;
1136 total_relocs++;
1137 break;
1138 case BITS_AND_SHIFT (16, 16):
1139 reloc_data[total_relocs].type = 4;
1140 /* FIXME: we can't know the symbol's right value
1141 yet, but we probably can safely assume that
1142 CE will relocate us in 64k blocks, so leaving
1143 it zero is safe. */
1144 reloc_data[total_relocs].extra = 0;
1145 total_relocs++;
1146 break;
1147 case BITS_AND_SHIFT (26, 2):
1148 reloc_data[total_relocs].type = 5;
1149 total_relocs++;
1150 break;
1151 default:
1152 /* xgettext:c-format */
1153 einfo (_("%XError: %d-bit reloc in dll\n"),
1154 relocs[i]->howto->bitsize);
1155 break;
1159 free (relocs);
1160 /* Warning: the allocated symbols are remembered in BFD and
1161 reused later, so don't free them! */
1162 #if 0
1163 free (symbol);
1164 #endif
1168 /* At this point, we have total_relocs relocation addresses in
1169 reloc_addresses, which are all suitable for the .reloc section.
1170 We must now create the new sections. */
1171 qsort (reloc_data, total_relocs, sizeof (*reloc_data), reloc_sort);
1173 for (i = 0; i < total_relocs; i++)
1175 unsigned long this_page = (reloc_data[i].vma >> 12);
1177 if (this_page != sec_page)
1179 reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align. */
1180 reloc_sz += 8;
1181 sec_page = this_page;
1184 reloc_sz += 2;
1186 if (reloc_data[i].type == 4)
1187 reloc_sz += 2;
1190 reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align. */
1191 reloc_d = (unsigned char *) xmalloc (reloc_sz);
1192 sec_page = (unsigned long) (-1);
1193 reloc_sz = 0;
1194 page_ptr = (unsigned long) (-1);
1195 page_count = 0;
1197 for (i = 0; i < total_relocs; i++)
1199 unsigned long rva = reloc_data[i].vma - image_base;
1200 unsigned long this_page = (rva & ~0xfff);
1202 if (this_page != sec_page)
1204 while (reloc_sz & 3)
1205 reloc_d[reloc_sz++] = 0;
1207 if (page_ptr != (unsigned long) (-1))
1208 bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
1210 bfd_put_32 (abfd, this_page, reloc_d + reloc_sz);
1211 page_ptr = reloc_sz;
1212 reloc_sz += 8;
1213 sec_page = this_page;
1214 page_count = 0;
1217 bfd_put_16 (abfd, (rva & 0xfff) + (reloc_data[i].type << 12),
1218 reloc_d + reloc_sz);
1219 reloc_sz += 2;
1221 if (reloc_data[i].type == 4)
1223 bfd_put_16 (abfd, reloc_data[i].extra, reloc_d + reloc_sz);
1224 reloc_sz += 2;
1227 page_count++;
1230 while (reloc_sz & 3)
1231 reloc_d[reloc_sz++] = 0;
1233 if (page_ptr != (unsigned long) (-1))
1234 bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
1236 while (reloc_sz < reloc_s->_raw_size)
1237 reloc_d[reloc_sz++] = 0;
1240 /* Given the exiting def_file structure, print out a .DEF file that
1241 corresponds to it. */
1243 static void
1244 quoteput (s, f, needs_quotes)
1245 char *s;
1246 FILE *f;
1247 int needs_quotes;
1249 char *cp;
1251 for (cp = s; *cp; cp++)
1252 if (*cp == '\''
1253 || *cp == '"'
1254 || *cp == '\\'
1255 || isspace ((unsigned char) *cp)
1256 || *cp == ','
1257 || *cp == ';')
1258 needs_quotes = 1;
1260 if (needs_quotes)
1262 putc ('"', f);
1264 while (*s)
1266 if (*s == '"' || *s == '\\')
1267 putc ('\\', f);
1269 putc (*s, f);
1270 s++;
1273 putc ('"', f);
1275 else
1276 fputs (s, f);
1279 void
1280 pe_dll_generate_def_file (pe_out_def_filename)
1281 const char *pe_out_def_filename;
1283 int i;
1284 FILE *out = fopen (pe_out_def_filename, "w");
1286 if (out == NULL)
1287 /* xgettext:c-format */
1288 einfo (_("%s: Can't open output def file %s\n"),
1289 program_name, pe_out_def_filename);
1291 if (pe_def_file)
1293 if (pe_def_file->name)
1295 if (pe_def_file->is_dll)
1296 fprintf (out, "LIBRARY ");
1297 else
1298 fprintf (out, "NAME ");
1300 quoteput (pe_def_file->name, out, 1);
1302 if (pe_data (output_bfd)->pe_opthdr.ImageBase)
1303 fprintf (out, " BASE=0x%lx",
1304 (unsigned long) pe_data (output_bfd)->pe_opthdr.ImageBase);
1305 fprintf (out, "\n");
1308 if (pe_def_file->description)
1310 fprintf (out, "DESCRIPTION ");
1311 quoteput (pe_def_file->description, out, 1);
1312 fprintf (out, "\n");
1315 if (pe_def_file->version_minor != -1)
1316 fprintf (out, "VERSION %d.%d\n", pe_def_file->version_major,
1317 pe_def_file->version_minor);
1318 else if (pe_def_file->version_major != -1)
1319 fprintf (out, "VERSION %d\n", pe_def_file->version_major);
1321 if (pe_def_file->stack_reserve != -1 || pe_def_file->heap_reserve != -1)
1322 fprintf (out, "\n");
1324 if (pe_def_file->stack_commit != -1)
1325 fprintf (out, "STACKSIZE 0x%x,0x%x\n",
1326 pe_def_file->stack_reserve, pe_def_file->stack_commit);
1327 else if (pe_def_file->stack_reserve != -1)
1328 fprintf (out, "STACKSIZE 0x%x\n", pe_def_file->stack_reserve);
1330 if (pe_def_file->heap_commit != -1)
1331 fprintf (out, "HEAPSIZE 0x%x,0x%x\n",
1332 pe_def_file->heap_reserve, pe_def_file->heap_commit);
1333 else if (pe_def_file->heap_reserve != -1)
1334 fprintf (out, "HEAPSIZE 0x%x\n", pe_def_file->heap_reserve);
1336 if (pe_def_file->num_section_defs > 0)
1338 fprintf (out, "\nSECTIONS\n\n");
1340 for (i = 0; i < pe_def_file->num_section_defs; i++)
1342 fprintf (out, " ");
1343 quoteput (pe_def_file->section_defs[i].name, out, 0);
1345 if (pe_def_file->section_defs[i].class)
1347 fprintf (out, " CLASS ");
1348 quoteput (pe_def_file->section_defs[i].class, out, 0);
1351 if (pe_def_file->section_defs[i].flag_read)
1352 fprintf (out, " READ");
1354 if (pe_def_file->section_defs[i].flag_write)
1355 fprintf (out, " WRITE");
1357 if (pe_def_file->section_defs[i].flag_execute)
1358 fprintf (out, " EXECUTE");
1360 if (pe_def_file->section_defs[i].flag_shared)
1361 fprintf (out, " SHARED");
1363 fprintf (out, "\n");
1367 if (pe_def_file->num_exports > 0)
1369 fprintf (out, "EXPORTS\n");
1371 for (i = 0; i < pe_def_file->num_exports; i++)
1373 def_file_export *e = pe_def_file->exports + i;
1374 fprintf (out, " ");
1375 quoteput (e->name, out, 0);
1377 if (e->internal_name && strcmp (e->internal_name, e->name))
1379 fprintf (out, " = ");
1380 quoteput (e->internal_name, out, 0);
1383 if (e->ordinal != -1)
1384 fprintf (out, " @%d", e->ordinal);
1386 if (e->flag_private)
1387 fprintf (out, " PRIVATE");
1389 if (e->flag_constant)
1390 fprintf (out, " CONSTANT");
1392 if (e->flag_noname)
1393 fprintf (out, " NONAME");
1395 if (e->flag_data)
1396 fprintf (out, " DATA");
1398 fprintf (out, "\n");
1402 if (pe_def_file->num_imports > 0)
1404 fprintf (out, "\nIMPORTS\n\n");
1406 for (i = 0; i < pe_def_file->num_imports; i++)
1408 def_file_import *im = pe_def_file->imports + i;
1409 fprintf (out, " ");
1411 if (im->internal_name
1412 && (!im->name || strcmp (im->internal_name, im->name)))
1414 quoteput (im->internal_name, out, 0);
1415 fprintf (out, " = ");
1418 quoteput (im->module->name, out, 0);
1419 fprintf (out, ".");
1421 if (im->name)
1422 quoteput (im->name, out, 0);
1423 else
1424 fprintf (out, "%d", im->ordinal);
1426 fprintf (out, "\n");
1430 else
1431 fprintf (out, _("; no contents available\n"));
1433 if (fclose (out) == EOF)
1434 /* xgettext:c-format */
1435 einfo (_("%P: Error closing file `%s'\n"), pe_out_def_filename);
1438 /* Generate the import library. */
1440 static asymbol **symtab;
1441 static int symptr;
1442 static int tmp_seq;
1443 static const char *dll_filename;
1444 static char *dll_symname;
1446 #define UNDSEC (asection *) &bfd_und_section
1448 static asection *
1449 quick_section (abfd, name, flags, align)
1450 bfd *abfd;
1451 const char *name;
1452 int flags;
1453 int align;
1455 asection *sec;
1456 asymbol *sym;
1458 sec = bfd_make_section_old_way (abfd, name);
1459 bfd_set_section_flags (abfd, sec, flags | SEC_ALLOC | SEC_LOAD | SEC_KEEP);
1460 bfd_set_section_alignment (abfd, sec, align);
1461 /* Remember to undo this before trying to link internally! */
1462 sec->output_section = sec;
1464 sym = bfd_make_empty_symbol (abfd);
1465 symtab[symptr++] = sym;
1466 sym->name = sec->name;
1467 sym->section = sec;
1468 sym->flags = BSF_LOCAL;
1469 sym->value = 0;
1471 return sec;
1474 static void
1475 quick_symbol (abfd, n1, n2, n3, sec, flags, addr)
1476 bfd *abfd;
1477 char *n1;
1478 char *n2;
1479 char *n3;
1480 asection *sec;
1481 int flags;
1482 int addr;
1484 asymbol *sym;
1485 char *name = (char *) xmalloc (strlen (n1) + strlen (n2) + strlen (n3) + 1);
1487 strcpy (name, n1);
1488 strcat (name, n2);
1489 strcat (name, n3);
1490 sym = bfd_make_empty_symbol (abfd);
1491 sym->name = name;
1492 sym->section = sec;
1493 sym->flags = flags;
1494 sym->value = addr;
1495 symtab[symptr++] = sym;
1498 static arelent *reltab = 0;
1499 static int relcount = 0, relsize = 0;
1501 static void
1502 quick_reloc (abfd, address, which_howto, symidx)
1503 bfd *abfd;
1504 int address;
1505 int which_howto;
1506 int symidx;
1508 if (relcount >= (relsize - 1))
1510 relsize += 10;
1511 if (reltab)
1512 reltab = (arelent *) xrealloc (reltab, relsize * sizeof (arelent));
1513 else
1514 reltab = (arelent *) xmalloc (relsize * sizeof (arelent));
1516 reltab[relcount].address = address;
1517 reltab[relcount].addend = 0;
1518 reltab[relcount].howto = bfd_reloc_type_lookup (abfd, which_howto);
1519 reltab[relcount].sym_ptr_ptr = symtab + symidx;
1520 relcount++;
1523 static void
1524 save_relocs (asection *sec)
1526 int i;
1528 sec->relocation = reltab;
1529 sec->reloc_count = relcount;
1530 sec->orelocation = (arelent **) xmalloc ((relcount + 1) * sizeof (arelent *));
1531 for (i = 0; i < relcount; i++)
1532 sec->orelocation[i] = sec->relocation + i;
1533 sec->orelocation[relcount] = 0;
1534 sec->flags |= SEC_RELOC;
1535 reltab = 0;
1536 relcount = relsize = 0;
1539 /* .section .idata$2
1540 .global __head_my_dll
1541 __head_my_dll:
1542 .rva hname
1543 .long 0
1544 .long 0
1545 .rva __my_dll_iname
1546 .rva fthunk
1548 .section .idata$5
1549 .long 0
1550 fthunk:
1552 .section .idata$4
1553 .long 0
1554 hname: */
1556 static bfd *
1557 make_head (parent)
1558 bfd *parent;
1560 asection *id2, *id5, *id4;
1561 unsigned char *d2, *d5, *d4;
1562 char *oname;
1563 bfd *abfd;
1565 oname = (char *) xmalloc (20);
1566 sprintf (oname, "d%06d.o", tmp_seq);
1567 tmp_seq++;
1569 abfd = bfd_create (oname, parent);
1570 bfd_find_target (pe_details->object_target, abfd);
1571 bfd_make_writable (abfd);
1573 bfd_set_format (abfd, bfd_object);
1574 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1576 symptr = 0;
1577 symtab = (asymbol **) xmalloc (6 * sizeof (asymbol *));
1578 id2 = quick_section (abfd, ".idata$2", SEC_HAS_CONTENTS, 2);
1579 id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1580 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1581 quick_symbol (abfd, U ("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0);
1582 quick_symbol (abfd, U (""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
1584 /* OK, pay attention here. I got confused myself looking back at
1585 it. We create a four-byte section to mark the beginning of the
1586 list, and we include an offset of 4 in the section, so that the
1587 pointer to the list points to the *end* of this section, which is
1588 the start of the list of sections from other objects. */
1590 bfd_set_section_size (abfd, id2, 20);
1591 d2 = (unsigned char *) xmalloc (20);
1592 id2->contents = d2;
1593 memset (d2, 0, 20);
1594 d2[0] = d2[16] = 4; /* Reloc addend. */
1595 quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
1596 quick_reloc (abfd, 12, BFD_RELOC_RVA, 4);
1597 quick_reloc (abfd, 16, BFD_RELOC_RVA, 1);
1598 save_relocs (id2);
1600 bfd_set_section_size (abfd, id5, 4);
1601 d5 = (unsigned char *) xmalloc (4);
1602 id5->contents = d5;
1603 memset (d5, 0, 4);
1605 bfd_set_section_size (abfd, id4, 4);
1606 d4 = (unsigned char *) xmalloc (4);
1607 id4->contents = d4;
1608 memset (d4, 0, 4);
1610 bfd_set_symtab (abfd, symtab, symptr);
1612 bfd_set_section_contents (abfd, id2, d2, 0, 20);
1613 bfd_set_section_contents (abfd, id5, d5, 0, 4);
1614 bfd_set_section_contents (abfd, id4, d4, 0, 4);
1616 bfd_make_readable (abfd);
1617 return abfd;
1620 /* .section .idata$4
1621 .long 0
1622 .section .idata$5
1623 .long 0
1624 .section idata$7
1625 .global __my_dll_iname
1626 __my_dll_iname:
1627 .asciz "my.dll" */
1629 static bfd *
1630 make_tail (parent)
1631 bfd *parent;
1633 asection *id4, *id5, *id7;
1634 unsigned char *d4, *d5, *d7;
1635 int len;
1636 char *oname;
1637 bfd *abfd;
1639 oname = (char *) xmalloc (20);
1640 sprintf (oname, "d%06d.o", tmp_seq);
1641 tmp_seq++;
1643 abfd = bfd_create (oname, parent);
1644 bfd_find_target (pe_details->object_target, abfd);
1645 bfd_make_writable (abfd);
1647 bfd_set_format (abfd, bfd_object);
1648 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1650 symptr = 0;
1651 symtab = (asymbol **) xmalloc (5 * sizeof (asymbol *));
1652 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1653 id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1654 id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
1655 quick_symbol (abfd, U (""), dll_symname, "_iname", id7, BSF_GLOBAL, 0);
1657 bfd_set_section_size (abfd, id4, 4);
1658 d4 = (unsigned char *) xmalloc (4);
1659 id4->contents = d4;
1660 memset (d4, 0, 4);
1662 bfd_set_section_size (abfd, id5, 4);
1663 d5 = (unsigned char *) xmalloc (4);
1664 id5->contents = d5;
1665 memset (d5, 0, 4);
1667 len = strlen (dll_filename) + 1;
1668 if (len & 1)
1669 len++;
1670 bfd_set_section_size (abfd, id7, len);
1671 d7 = (unsigned char *) xmalloc (len);
1672 id7->contents = d7;
1673 strcpy (d7, dll_filename);
1675 bfd_set_symtab (abfd, symtab, symptr);
1677 bfd_set_section_contents (abfd, id4, d4, 0, 4);
1678 bfd_set_section_contents (abfd, id5, d5, 0, 4);
1679 bfd_set_section_contents (abfd, id7, d7, 0, len);
1681 bfd_make_readable (abfd);
1682 return abfd;
1685 /* .text
1686 .global _function
1687 .global ___imp_function
1688 .global __imp__function
1689 _function:
1690 jmp *__imp__function:
1692 .section idata$7
1693 .long __head_my_dll
1695 .section .idata$5
1696 ___imp_function:
1697 __imp__function:
1698 iat?
1699 .section .idata$4
1700 iat?
1701 .section .idata$6
1702 ID<ordinal>:
1703 .short <hint>
1704 .asciz "function" xlate? (add underscore, kill at) */
1706 static unsigned char jmp_ix86_bytes[] =
1708 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90
1711 /* _function:
1712 mov.l ip+8,r0
1713 mov.l @r0,r0
1714 jmp @r0
1716 .dw __imp_function */
1718 static unsigned char jmp_sh_bytes[] =
1720 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00
1723 /* _function:
1724 lui $t0,<high:__imp_function>
1725 lw $t0,<low:__imp_function>
1726 jr $t0
1727 nop */
1729 static unsigned char jmp_mips_bytes[] =
1731 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d,
1732 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00
1735 static bfd *
1736 make_one (exp, parent)
1737 def_file_export *exp;
1738 bfd *parent;
1740 asection *tx, *id7, *id5, *id4, *id6;
1741 unsigned char *td = NULL, *d7, *d5, *d4, *d6 = NULL;
1742 int len;
1743 char *oname;
1744 bfd *abfd;
1745 unsigned char *jmp_bytes = NULL;
1746 int jmp_byte_count = 0;
1748 switch (pe_details->pe_arch)
1750 case PE_ARCH_i386:
1751 jmp_bytes = jmp_ix86_bytes;
1752 jmp_byte_count = sizeof (jmp_ix86_bytes);
1753 break;
1754 case PE_ARCH_sh:
1755 jmp_bytes = jmp_sh_bytes;
1756 jmp_byte_count = sizeof (jmp_sh_bytes);
1757 break;
1758 case PE_ARCH_mips:
1759 jmp_bytes = jmp_mips_bytes;
1760 jmp_byte_count = sizeof (jmp_mips_bytes);
1761 break;
1762 default:
1763 abort ();
1766 oname = (char *) xmalloc (20);
1767 sprintf (oname, "d%06d.o", tmp_seq);
1768 tmp_seq++;
1770 abfd = bfd_create (oname, parent);
1771 bfd_find_target (pe_details->object_target, abfd);
1772 bfd_make_writable (abfd);
1774 bfd_set_format (abfd, bfd_object);
1775 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1777 symptr = 0;
1778 symtab = (asymbol **) xmalloc (11 * sizeof (asymbol *));
1779 tx = quick_section (abfd, ".text", SEC_CODE|SEC_HAS_CONTENTS, 2);
1780 id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
1781 id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1782 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1783 id6 = quick_section (abfd, ".idata$6", SEC_HAS_CONTENTS, 2);
1784 if (! exp->flag_data)
1785 quick_symbol (abfd, U (""), exp->internal_name, "", tx, BSF_GLOBAL, 0);
1786 quick_symbol (abfd, U ("_head_"), dll_symname, "", UNDSEC, BSF_GLOBAL, 0);
1787 quick_symbol (abfd, U ("_imp__"), exp->internal_name, "", id5, BSF_GLOBAL, 0);
1788 /* Symbol to reference ord/name of imported
1789 symbol, used to implement auto-import. */
1790 quick_symbol (abfd, U("_nm__"), exp->internal_name, "", id6, BSF_GLOBAL, 0);
1791 if (pe_dll_compat_implib)
1792 quick_symbol (abfd, U ("__imp_"), exp->internal_name, "",
1793 id5, BSF_GLOBAL, 0);
1795 if (! exp->flag_data)
1797 bfd_set_section_size (abfd, tx, jmp_byte_count);
1798 td = (unsigned char *) xmalloc (jmp_byte_count);
1799 tx->contents = td;
1800 memcpy (td, jmp_bytes, jmp_byte_count);
1802 switch (pe_details->pe_arch)
1804 case PE_ARCH_i386:
1805 quick_reloc (abfd, 2, BFD_RELOC_32, 2);
1806 break;
1807 case PE_ARCH_sh:
1808 quick_reloc (abfd, 8, BFD_RELOC_32, 2);
1809 break;
1810 case PE_ARCH_mips:
1811 quick_reloc (abfd, 0, BFD_RELOC_HI16_S, 2);
1812 quick_reloc (abfd, 0, BFD_RELOC_LO16, 0); /* MIPS_R_PAIR */
1813 quick_reloc (abfd, 4, BFD_RELOC_LO16, 2);
1814 break;
1815 default:
1816 abort ();
1818 save_relocs (tx);
1821 bfd_set_section_size (abfd, id7, 4);
1822 d7 = (unsigned char *) xmalloc (4);
1823 id7->contents = d7;
1824 memset (d7, 0, 4);
1825 quick_reloc (abfd, 0, BFD_RELOC_RVA, 6);
1826 save_relocs (id7);
1828 bfd_set_section_size (abfd, id5, 4);
1829 d5 = (unsigned char *) xmalloc (4);
1830 id5->contents = d5;
1831 memset (d5, 0, 4);
1833 if (exp->flag_noname)
1835 d5[0] = exp->ordinal;
1836 d5[1] = exp->ordinal >> 8;
1837 d5[3] = 0x80;
1839 else
1841 quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
1842 save_relocs (id5);
1845 bfd_set_section_size (abfd, id4, 4);
1846 d4 = (unsigned char *) xmalloc (4);
1847 id4->contents = d4;
1848 memset (d4, 0, 4);
1850 if (exp->flag_noname)
1852 d4[0] = exp->ordinal;
1853 d4[1] = exp->ordinal >> 8;
1854 d4[3] = 0x80;
1856 else
1858 quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
1859 save_relocs (id4);
1862 if (exp->flag_noname)
1864 len = 0;
1865 bfd_set_section_size (abfd, id6, 0);
1867 else
1869 len = strlen (exp->name) + 3;
1870 if (len & 1)
1871 len++;
1872 bfd_set_section_size (abfd, id6, len);
1873 d6 = (unsigned char *) xmalloc (len);
1874 id6->contents = d6;
1875 memset (d6, 0, len);
1876 d6[0] = exp->hint & 0xff;
1877 d6[1] = exp->hint >> 8;
1878 strcpy (d6 + 2, exp->name);
1881 bfd_set_symtab (abfd, symtab, symptr);
1883 bfd_set_section_contents (abfd, tx, td, 0, jmp_byte_count);
1884 bfd_set_section_contents (abfd, id7, d7, 0, 4);
1885 bfd_set_section_contents (abfd, id5, d5, 0, 4);
1886 bfd_set_section_contents (abfd, id4, d4, 0, 4);
1887 if (!exp->flag_noname)
1888 bfd_set_section_contents (abfd, id6, d6, 0, len);
1890 bfd_make_readable (abfd);
1891 return abfd;
1894 static bfd *
1895 make_singleton_name_thunk (import, parent)
1896 char *import;
1897 bfd *parent;
1899 /* Name thunks go to idata$4. */
1900 asection *id4;
1901 unsigned char *d4;
1902 char *oname;
1903 bfd *abfd;
1905 oname = (char *) xmalloc (20);
1906 sprintf (oname, "nmth%06d.o", tmp_seq);
1907 tmp_seq++;
1909 abfd = bfd_create (oname, parent);
1910 bfd_find_target (pe_details->object_target, abfd);
1911 bfd_make_writable (abfd);
1913 bfd_set_format (abfd, bfd_object);
1914 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1916 symptr = 0;
1917 symtab = (asymbol **) xmalloc (3 * sizeof (asymbol *));
1918 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1919 quick_symbol (abfd, U ("_nm_thnk_"), import, "", id4, BSF_GLOBAL, 0);
1920 quick_symbol (abfd, U ("_nm_"), import, "", UNDSEC, BSF_GLOBAL, 0);
1922 bfd_set_section_size (abfd, id4, 8);
1923 d4 = (unsigned char *) xmalloc (4);
1924 id4->contents = d4;
1925 memset (d4, 0, 8);
1926 quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
1927 save_relocs (id4);
1929 bfd_set_symtab (abfd, symtab, symptr);
1931 bfd_set_section_contents (abfd, id4, d4, 0, 8);
1933 bfd_make_readable (abfd);
1934 return abfd;
1937 static char *
1938 make_import_fixup_mark (rel)
1939 arelent *rel;
1941 /* We convert reloc to symbol, for later reference. */
1942 static int counter;
1943 static char *fixup_name = NULL;
1944 static unsigned int buffer_len = 0;
1946 struct symbol_cache_entry *sym = *rel->sym_ptr_ptr;
1948 bfd *abfd = bfd_asymbol_bfd (sym);
1949 struct coff_link_hash_entry *myh = NULL;
1951 if (!fixup_name)
1953 fixup_name = (char *) xmalloc (384);
1954 buffer_len = 384;
1957 if (strlen (sym->name) + 25 > buffer_len)
1958 /* Assume 25 chars for "__fu" + counter + "_". If counter is
1959 bigger than 20 digits long, we've got worse problems than
1960 overflowing this buffer... */
1962 free (fixup_name);
1963 /* New buffer size is length of symbol, plus 25, but then
1964 rounded up to the nearest multiple of 128. */
1965 buffer_len = ((strlen (sym->name) + 25) + 127) & ~127;
1966 fixup_name = (char *) xmalloc (buffer_len);
1969 sprintf (fixup_name, "__fu%d_%s", counter++, sym->name);
1971 bfd_coff_link_add_one_symbol (&link_info, abfd, fixup_name, BSF_GLOBAL,
1972 current_sec, /* sym->section, */
1973 rel->address, NULL, true, false,
1974 (struct bfd_link_hash_entry **) &myh);
1976 #if 0
1977 printf ("type:%d\n", myh->type);
1978 printf ("%s\n", myh->root.u.def.section->name);
1979 #endif
1980 return fixup_name;
1983 /* .section .idata$3
1984 .rva __nm_thnk_SYM (singleton thunk with name of func)
1985 .long 0
1986 .long 0
1987 .rva __my_dll_iname (name of dll)
1988 .rva __fuNN_SYM (pointer to reference (address) in text) */
1990 static bfd *
1991 make_import_fixup_entry (name, fixup_name, dll_symname,parent)
1992 char *name;
1993 char *fixup_name;
1994 char *dll_symname;
1995 bfd *parent;
1997 asection *id3;
1998 unsigned char *d3;
1999 char *oname;
2000 bfd *abfd;
2002 oname = (char *) xmalloc (20);
2003 sprintf (oname, "fu%06d.o", tmp_seq);
2004 tmp_seq++;
2006 abfd = bfd_create (oname, parent);
2007 bfd_find_target (pe_details->object_target, abfd);
2008 bfd_make_writable (abfd);
2010 bfd_set_format (abfd, bfd_object);
2011 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
2013 symptr = 0;
2014 symtab = (asymbol **) xmalloc (6 * sizeof (asymbol *));
2015 id3 = quick_section (abfd, ".idata$3", SEC_HAS_CONTENTS, 2);
2017 #if 0
2018 quick_symbol (abfd, U ("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0);
2019 #endif
2020 quick_symbol (abfd, U ("_nm_thnk_"), name, "", UNDSEC, BSF_GLOBAL, 0);
2021 quick_symbol (abfd, U (""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
2022 quick_symbol (abfd, "", fixup_name, "", UNDSEC, BSF_GLOBAL, 0);
2024 bfd_set_section_size (abfd, id3, 20);
2025 d3 = (unsigned char *) xmalloc (20);
2026 id3->contents = d3;
2027 memset (d3, 0, 20);
2029 quick_reloc (abfd, 0, BFD_RELOC_RVA, 1);
2030 quick_reloc (abfd, 12, BFD_RELOC_RVA, 2);
2031 quick_reloc (abfd, 16, BFD_RELOC_RVA, 3);
2032 save_relocs (id3);
2034 bfd_set_symtab (abfd, symtab, symptr);
2036 bfd_set_section_contents (abfd, id3, d3, 0, 20);
2038 bfd_make_readable (abfd);
2039 return abfd;
2042 void
2043 pe_create_import_fixup (rel)
2044 arelent *rel;
2046 char buf[300];
2047 struct symbol_cache_entry *sym = *rel->sym_ptr_ptr;
2048 struct bfd_link_hash_entry *name_thunk_sym;
2049 CONST char *name = sym->name;
2050 char *fixup_name = make_import_fixup_mark (rel);
2052 sprintf (buf, U ("_nm_thnk_%s"), name);
2054 name_thunk_sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
2056 if (!name_thunk_sym || name_thunk_sym->type != bfd_link_hash_defined)
2058 bfd *b = make_singleton_name_thunk (name, output_bfd);
2059 add_bfd_to_link (b, b->filename, &link_info);
2061 /* If we ever use autoimport, we have to cast text section writable. */
2062 config.text_read_only = false;
2066 extern char * pe_data_import_dll; /* Defined in emultempl/pe.em. */
2068 bfd *b = make_import_fixup_entry (name, fixup_name, pe_data_import_dll,
2069 output_bfd);
2070 add_bfd_to_link (b, b->filename, &link_info);
2075 void
2076 pe_dll_generate_implib (def, impfilename)
2077 def_file *def;
2078 const char *impfilename;
2080 int i;
2081 bfd *ar_head;
2082 bfd *ar_tail;
2083 bfd *outarch;
2084 bfd *head = 0;
2086 dll_filename = (def->name) ? def->name : dll_name;
2087 dll_symname = xstrdup (dll_filename);
2088 for (i = 0; dll_symname[i]; i++)
2089 if (!isalnum ((unsigned char) dll_symname[i]))
2090 dll_symname[i] = '_';
2092 unlink (impfilename);
2094 outarch = bfd_openw (impfilename, 0);
2096 if (!outarch)
2098 /* xgettext:c-format */
2099 einfo (_("%XCan't open .lib file: %s\n"), impfilename);
2100 return;
2103 /* xgettext:c-format */
2104 einfo (_("Creating library file: %s\n"), impfilename);
2106 bfd_set_format (outarch, bfd_archive);
2107 outarch->has_armap = 1;
2109 /* Work out a reasonable size of things to put onto one line. */
2110 ar_head = make_head (outarch);
2112 for (i = 0; i < def->num_exports; i++)
2114 /* The import library doesn't know about the internal name. */
2115 char *internal = def->exports[i].internal_name;
2116 bfd *n;
2118 def->exports[i].internal_name = def->exports[i].name;
2119 n = make_one (def->exports + i, outarch);
2120 n->next = head;
2121 head = n;
2122 def->exports[i].internal_name = internal;
2125 ar_tail = make_tail (outarch);
2127 if (ar_head == NULL || ar_tail == NULL)
2128 return;
2130 /* Now stick them all into the archive. */
2131 ar_head->next = head;
2132 ar_tail->next = ar_head;
2133 head = ar_tail;
2135 if (! bfd_set_archive_head (outarch, head))
2136 einfo ("%Xbfd_set_archive_head: %s\n", bfd_errmsg (bfd_get_error ()));
2138 if (! bfd_close (outarch))
2139 einfo ("%Xbfd_close %s: %s\n", impfilename, bfd_errmsg (bfd_get_error ()));
2141 while (head != NULL)
2143 bfd *n = head->next;
2144 bfd_close (head);
2145 head = n;
2149 static void
2150 add_bfd_to_link (abfd, name, link_info)
2151 bfd *abfd;
2152 CONST char *name;
2153 struct bfd_link_info *link_info;
2155 lang_input_statement_type *fake_file;
2157 fake_file = lang_add_input_file (name,
2158 lang_input_file_is_fake_enum,
2159 NULL);
2160 fake_file->the_bfd = abfd;
2161 ldlang_add_file (fake_file);
2163 if (!bfd_link_add_symbols (abfd, link_info))
2164 einfo ("%Xaddsym %s: %s\n", name, bfd_errmsg (bfd_get_error ()));
2167 void
2168 pe_process_import_defs (output_bfd, link_info)
2169 bfd *output_bfd;
2170 struct bfd_link_info *link_info;
2172 def_file_module *module;
2174 pe_dll_id_target (bfd_get_target (output_bfd));
2176 if (!pe_def_file)
2177 return;
2179 for (module = pe_def_file->modules; module; module = module->next)
2181 int i, do_this_dll;
2183 dll_filename = module->name;
2184 dll_symname = xstrdup (module->name);
2185 for (i = 0; dll_symname[i]; i++)
2186 if (!isalnum (dll_symname[i]))
2187 dll_symname[i] = '_';
2189 do_this_dll = 0;
2191 for (i = 0; i < pe_def_file->num_imports; i++)
2192 if (pe_def_file->imports[i].module == module)
2194 def_file_export exp;
2195 struct bfd_link_hash_entry *blhe;
2197 /* See if we need this import. */
2198 char *name = (char *) xmalloc (strlen (pe_def_file->imports[i].internal_name) + 2 + 6);
2199 sprintf (name, "%s%s", U (""), pe_def_file->imports[i].internal_name);
2200 blhe = bfd_link_hash_lookup (link_info->hash, name,
2201 false, false, false);
2202 if (!blhe || (blhe && blhe->type != bfd_link_hash_undefined))
2204 sprintf (name, "%s%s", U ("_imp__"),
2205 pe_def_file->imports[i].internal_name);
2206 blhe = bfd_link_hash_lookup (link_info->hash, name,
2207 false, false, false);
2209 free (name);
2210 if (blhe && blhe->type == bfd_link_hash_undefined)
2212 bfd *one;
2213 /* We do. */
2214 if (!do_this_dll)
2216 bfd *ar_head = make_head (output_bfd);
2217 add_bfd_to_link (ar_head, ar_head->filename, link_info);
2218 do_this_dll = 1;
2220 exp.internal_name = pe_def_file->imports[i].internal_name;
2221 exp.name = pe_def_file->imports[i].name;
2222 exp.ordinal = pe_def_file->imports[i].ordinal;
2223 exp.hint = exp.ordinal >= 0 ? exp.ordinal : 0;
2224 exp.flag_private = 0;
2225 exp.flag_constant = 0;
2226 exp.flag_data = 0;
2227 exp.flag_noname = exp.name ? 0 : 1;
2228 one = make_one (&exp, output_bfd);
2229 add_bfd_to_link (one, one->filename, link_info);
2232 if (do_this_dll)
2234 bfd *ar_tail = make_tail (output_bfd);
2235 add_bfd_to_link (ar_tail, ar_tail->filename, link_info);
2238 free (dll_symname);
2242 /* We were handed a *.DLL file. Parse it and turn it into a set of
2243 IMPORTS directives in the def file. Return true if the file was
2244 handled, false if not. */
2246 static unsigned int
2247 pe_get16 (abfd, where)
2248 bfd *abfd;
2249 int where;
2251 unsigned char b[2];
2253 bfd_seek (abfd, where, SEEK_SET);
2254 bfd_read (b, 1, 2, abfd);
2255 return b[0] + (b[1] << 8);
2258 static unsigned int
2259 pe_get32 (abfd, where)
2260 bfd *abfd;
2261 int where;
2263 unsigned char b[4];
2265 bfd_seek (abfd, where, SEEK_SET);
2266 bfd_read (b, 1, 4, abfd);
2267 return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
2270 #if 0 /* This is not currently used. */
2272 static unsigned int
2273 pe_as16 (ptr)
2274 void *ptr;
2276 unsigned char *b = ptr;
2278 return b[0] + (b[1] << 8);
2281 #endif
2283 static unsigned int
2284 pe_as32 (ptr)
2285 void *ptr;
2287 unsigned char *b = ptr;
2289 return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
2292 boolean
2293 pe_implied_import_dll (filename)
2294 const char *filename;
2296 bfd *dll;
2297 unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
2298 unsigned long export_rva, export_size, nsections, secptr, expptr;
2299 unsigned char *expdata, *erva;
2300 unsigned long name_rvas, ordinals, nexp, ordbase;
2301 const char *dll_name;
2303 /* No, I can't use bfd here. kernel32.dll puts its export table in
2304 the middle of the .rdata section. */
2305 dll = bfd_openr (filename, pe_details->target_name);
2306 if (!dll)
2308 einfo ("%Xopen %s: %s\n", filename, bfd_errmsg (bfd_get_error ()));
2309 return false;
2312 /* PEI dlls seem to be bfd_objects. */
2313 if (!bfd_check_format (dll, bfd_object))
2315 einfo ("%X%s: this doesn't appear to be a DLL\n", filename);
2316 return false;
2319 dll_name = filename;
2320 for (i = 0; filename[i]; i++)
2321 if (filename[i] == '/' || filename[i] == '\\' || filename[i] == ':')
2322 dll_name = filename + i + 1;
2324 pe_header_offset = pe_get32 (dll, 0x3c);
2325 opthdr_ofs = pe_header_offset + 4 + 20;
2326 num_entries = pe_get32 (dll, opthdr_ofs + 92);
2328 if (num_entries < 1) /* No exports. */
2329 return false;
2331 export_rva = pe_get32 (dll, opthdr_ofs + 96);
2332 export_size = pe_get32 (dll, opthdr_ofs + 100);
2333 nsections = pe_get16 (dll, pe_header_offset + 4 + 2);
2334 secptr = (pe_header_offset + 4 + 20 +
2335 pe_get16 (dll, pe_header_offset + 4 + 16));
2336 expptr = 0;
2338 for (i = 0; i < nsections; i++)
2340 char sname[8];
2341 unsigned long secptr1 = secptr + 40 * i;
2342 unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
2343 unsigned long vsize = pe_get32 (dll, secptr1 + 16);
2344 unsigned long fptr = pe_get32 (dll, secptr1 + 20);
2346 bfd_seek (dll, secptr1, SEEK_SET);
2347 bfd_read (sname, 1, 8, dll);
2349 if (vaddr <= export_rva && vaddr + vsize > export_rva)
2351 expptr = fptr + (export_rva - vaddr);
2352 if (export_rva + export_size > vaddr + vsize)
2353 export_size = vsize - (export_rva - vaddr);
2354 break;
2358 expdata = (unsigned char *) xmalloc (export_size);
2359 bfd_seek (dll, expptr, SEEK_SET);
2360 bfd_read (expdata, 1, export_size, dll);
2361 erva = expdata - export_rva;
2363 if (pe_def_file == 0)
2364 pe_def_file = def_file_empty ();
2366 nexp = pe_as32 (expdata + 24);
2367 name_rvas = pe_as32 (expdata + 32);
2368 ordinals = pe_as32 (expdata + 36);
2369 ordbase = pe_as32 (expdata + 16);
2371 for (i = 0; i < nexp; i++)
2373 unsigned long name_rva = pe_as32 (erva + name_rvas + i * 4);
2374 def_file_import *imp;
2376 imp = def_file_add_import (pe_def_file, erva + name_rva, dll_name,
2377 i, 0);
2380 return true;
2383 /* These are the main functions, called from the emulation. The first
2384 is called after the bfds are read, so we can guess at how much space
2385 we need. The second is called after everything is placed, so we
2386 can put the right values in place. */
2388 void
2389 pe_dll_build_sections (abfd, info)
2390 bfd *abfd;
2391 struct bfd_link_info *info;
2393 pe_dll_id_target (bfd_get_target (abfd));
2394 process_def_file (abfd, info);
2396 generate_edata (abfd, info);
2397 build_filler_bfd (1);
2400 void
2401 pe_exe_build_sections (abfd, info)
2402 bfd *abfd;
2403 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2405 pe_dll_id_target (bfd_get_target (abfd));
2406 build_filler_bfd (0);
2409 void
2410 pe_dll_fill_sections (abfd, info)
2411 bfd *abfd;
2412 struct bfd_link_info *info;
2414 pe_dll_id_target (bfd_get_target (abfd));
2415 image_base = pe_data (abfd)->pe_opthdr.ImageBase;
2417 generate_reloc (abfd, info);
2418 if (reloc_sz > 0)
2420 bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
2422 /* Resize the sections. */
2423 lang_size_sections (stat_ptr->head, abs_output_section,
2424 &stat_ptr->head, 0, (bfd_vma) 0, NULL);
2426 /* Redo special stuff. */
2427 ldemul_after_allocation ();
2429 /* Do the assignments again. */
2430 lang_do_assignments (stat_ptr->head,
2431 abs_output_section,
2432 (fill_type) 0, (bfd_vma) 0);
2435 fill_edata (abfd, info);
2437 pe_data (abfd)->dll = 1;
2439 edata_s->contents = edata_d;
2440 reloc_s->contents = reloc_d;
2443 void
2444 pe_exe_fill_sections (abfd, info)
2445 bfd *abfd;
2446 struct bfd_link_info *info;
2448 pe_dll_id_target (bfd_get_target (abfd));
2449 image_base = pe_data (abfd)->pe_opthdr.ImageBase;
2451 generate_reloc (abfd, info);
2452 if (reloc_sz > 0)
2454 bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
2456 /* Resize the sections. */
2457 lang_size_sections (stat_ptr->head, abs_output_section,
2458 &stat_ptr->head, 0, (bfd_vma) 0, NULL);
2460 /* Redo special stuff. */
2461 ldemul_after_allocation ();
2463 /* Do the assignments again. */
2464 lang_do_assignments (stat_ptr->head,
2465 abs_output_section,
2466 (fill_type) 0, (bfd_vma) 0);
2468 reloc_s->contents = reloc_d;