4ca567f32147e43bcbbb4214227c2d993b7500b2
[deliverable/binutils-gdb.git] / ld / pe-dll.c
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>
4
5 This file is part of GLD, the Gnu Linker.
6
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.
11
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.
16
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. */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libiberty.h"
26 #include "safe-ctype.h"
27
28 #include <time.h>
29
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"
43
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.
50
51 See also: ld/emultempl/pe.em. */
52
53 /* Auto-import feature by Paul Sokolovsky
54
55 Quick facts:
56
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).
60
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.
64
65 3. Auto-import is fully compatible with standard import method and they
66 can be mixed together.
67
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).
72
73 Idea
74
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
78
79 mov dll_var,%eax,
80
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, ;-) .
91
92 Implementation
93
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.
120
121 See also: ld/emultempl/pe.em. */
122
123 static void
124 add_bfd_to_link PARAMS ((bfd *, const char *, struct bfd_link_info *));
125
126 /* For emultempl/pe.em. */
127
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;
136
137 /* Static variables and types. */
138
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;
144
145 typedef struct
146 {
147 char *target_name;
148 char *object_target;
149 unsigned int imagebase_reloc;
150 int pe_arch;
151 int bfd_arch;
152 int underscored;
153 }
154 pe_details_type;
155
156 typedef struct
157 {
158 char *name;
159 int len;
160 }
161 autofilter_entry_type;
162
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
168
169 static pe_details_type pe_detail_list[] =
170 {
171 {
172 "pei-i386",
173 "pe-i386",
174 7 /* R_IMAGEBASE */,
175 PE_ARCH_i386,
176 bfd_arch_i386,
177 1
178 },
179 {
180 "pei-shl",
181 "pe-shl",
182 16 /* R_SH_IMAGEBASE */,
183 PE_ARCH_sh,
184 bfd_arch_sh,
185 1
186 },
187 {
188 "pei-mips",
189 "pe-mips",
190 34 /* MIPS_R_RVA */,
191 PE_ARCH_mips,
192 bfd_arch_mips,
193 0
194 },
195 {
196 "pei-arm-little",
197 "pe-arm-little",
198 11 /* ARM_RVA32 */,
199 PE_ARCH_arm,
200 bfd_arch_arm,
201 0
202 },
203 {
204 "epoc-pei-arm-little",
205 "epoc-pe-arm-little",
206 11 /* ARM_RVA32 */,
207 PE_ARCH_arm_epoc,
208 bfd_arch_arm,
209 0
210 },
211 { NULL, NULL, 0, 0, 0, 0 }
212 };
213
214 static pe_details_type *pe_details;
215
216 static autofilter_entry_type autofilter_symbollist[] =
217 {
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 }
226 };
227
228 /* Do not specify library suffix explicitly, to allow for dllized versions. */
229 static autofilter_entry_type autofilter_liblist[] =
230 {
231 { "libgcc.", 7 },
232 { "libstdc++.", 10 },
233 { "libmingw32.", 11 },
234 { NULL, 0 }
235 };
236
237 static autofilter_entry_type autofilter_objlist[] =
238 {
239 { "crt0.o", 6 },
240 { "crt1.o", 6 },
241 { "crt2.o", 6 },
242 { "dllcrt1.o", 9 },
243 { "dllcrt2.o", 9 },
244 { "gcrt1.o", 7 },
245 { "gcrt2.o", 7 },
246 { NULL, 0 }
247 };
248
249 static autofilter_entry_type autofilter_symbolprefixlist[] =
250 {
251 /* { "__imp_", 6 }, */
252 /* Do __imp_ explicitly to save time. */
253 { "__rtti_", 7 },
254 { "__builtin_", 10 },
255 /* Don't export symbols specifying internal DLL layout. */
256 { "_head_", 6 },
257 { "_fmode", 6 },
258 { "_impure_ptr", 11 },
259 { "cygwin_attach_dll", 17 },
260 { "cygwin_premain0", 15 },
261 { "cygwin_premain1", 15 },
262 { "cygwin_premain2", 15 },
263 { "cygwin_premain3", 15 },
264 { "environ", 7 },
265 { NULL, 0 }
266 };
267
268 static autofilter_entry_type autofilter_symbolsuffixlist[] =
269 {
270 { "_iname", 6 },
271 { NULL, 0 }
272 };
273
274 #define U(str) (pe_details->underscored ? "_" str : str)
275
276 static int reloc_sort PARAMS ((const void *, const void *));
277 static int pe_export_sort PARAMS ((const void *, const void *));
278 static int auto_export PARAMS ((bfd *, def_file *, const char *));
279 static void process_def_file PARAMS ((bfd *, struct bfd_link_info *));
280 static void build_filler_bfd PARAMS ((int));
281 static void generate_edata PARAMS ((bfd *, struct bfd_link_info *));
282 static void fill_exported_offsets PARAMS ((bfd *, struct bfd_link_info *));
283 static void fill_edata PARAMS ((bfd *, struct bfd_link_info *));
284 static void generate_reloc PARAMS ((bfd *, struct bfd_link_info *));
285 static void quoteput PARAMS ((char *, FILE *, int));
286 static asection *quick_section PARAMS ((bfd *, const char *, int, int));
287 static void quick_symbol
288 PARAMS ((bfd *, const char *, const char *, const char *,
289 asection *, int, int));
290 static void quick_reloc PARAMS ((bfd *, int, int, int));
291 static bfd *make_head PARAMS ((bfd *));
292 static bfd *make_tail PARAMS ((bfd *));
293 static bfd *make_one PARAMS ((def_file_export *, bfd *));
294 static bfd *make_singleton_name_thunk PARAMS ((const char *, bfd *));
295 static char *make_import_fixup_mark PARAMS ((arelent *));
296 static bfd *make_import_fixup_entry
297 PARAMS ((const char *, const char *, const char *, bfd *));
298 static unsigned int pe_get16 PARAMS ((bfd *, int));
299 static unsigned int pe_get32 PARAMS ((bfd *, int));
300 static unsigned int pe_as32 PARAMS ((void *));
301
302 void
303 pe_dll_id_target (target)
304 const char *target;
305 {
306 int i;
307
308 for (i = 0; pe_detail_list[i].target_name; i++)
309 if (strcmp (pe_detail_list[i].target_name, target) == 0
310 || strcmp (pe_detail_list[i].object_target, target) == 0)
311 {
312 pe_details = pe_detail_list + i;
313 return;
314 }
315 einfo (_("%XUnsupported PEI architecture: %s\n"), target);
316 exit (1);
317 }
318
319 /* Helper functions for qsort. Relocs must be sorted so that we can write
320 them out by pages. */
321
322 typedef struct
323 {
324 bfd_vma vma;
325 char type;
326 short extra;
327 }
328 reloc_data_type;
329
330 static int
331 reloc_sort (va, vb)
332 const void *va, *vb;
333 {
334 bfd_vma a = ((reloc_data_type *) va)->vma;
335 bfd_vma b = ((reloc_data_type *) vb)->vma;
336
337 return (a > b) ? 1 : ((a < b) ? -1 : 0);
338 }
339
340 static int
341 pe_export_sort (va, vb)
342 const void *va, *vb;
343 {
344 def_file_export *a = (def_file_export *) va;
345 def_file_export *b = (def_file_export *) vb;
346
347 return strcmp (a->name, b->name);
348 }
349
350 /* Read and process the .DEF file. */
351
352 /* These correspond to the entries in pe_def_file->exports[]. I use
353 exported_symbol_sections[i] to tag whether or not the symbol was
354 defined, since we can't export symbols we don't have. */
355
356 static bfd_vma *exported_symbol_offsets;
357 static struct sec **exported_symbol_sections;
358 static int export_table_size;
359 static int count_exported;
360 static int count_exported_byname;
361 static int count_with_ordinals;
362 static const char *dll_name;
363 static int min_ordinal, max_ordinal;
364 static int *exported_symbols;
365
366 typedef struct exclude_list_struct
367 {
368 char *string;
369 struct exclude_list_struct *next;
370 }
371 exclude_list_struct;
372
373 static struct exclude_list_struct *excludes = 0;
374
375 void
376 pe_dll_add_excludes (new_excludes)
377 const char *new_excludes;
378 {
379 char *local_copy;
380 char *exclude_string;
381
382 local_copy = xstrdup (new_excludes);
383
384 exclude_string = strtok (local_copy, ",:");
385 for (; exclude_string; exclude_string = strtok (NULL, ",:"))
386 {
387 struct exclude_list_struct *new_exclude;
388
389 new_exclude = ((struct exclude_list_struct *)
390 xmalloc (sizeof (struct exclude_list_struct)));
391 new_exclude->string = (char *) xmalloc (strlen (exclude_string) + 1);
392 strcpy (new_exclude->string, exclude_string);
393 new_exclude->next = excludes;
394 excludes = new_exclude;
395 }
396
397 free (local_copy);
398 }
399
400 /* abfd is a bfd containing n (or NULL)
401 It can be used for contextual checks. */
402
403 static int
404 auto_export (abfd, d, n)
405 bfd *abfd;
406 def_file *d;
407 const char *n;
408 {
409 int i;
410 struct exclude_list_struct *ex;
411 autofilter_entry_type *afptr;
412
413 /* We should not re-export imported stuff. */
414 if (strncmp (n, "_imp__", 6) == 0)
415 return 0;
416
417 for (i = 0; i < d->num_exports; i++)
418 if (strcmp (d->exports[i].name, n) == 0)
419 return 0;
420
421 if (pe_dll_do_default_excludes)
422 {
423 const char * p;
424 int len;
425
426 if (pe_dll_extra_pe_debug)
427 printf ("considering exporting: %s, abfd=%p, abfd->my_arc=%p\n",
428 n, abfd, abfd->my_archive);
429
430 /* First of all, make context checks:
431 Don't export anything from libgcc. */
432 if (abfd && abfd->my_archive)
433 {
434 afptr = autofilter_liblist;
435
436 while (afptr->name)
437 {
438 if (strstr (abfd->my_archive->filename, afptr->name))
439 return 0;
440 afptr++;
441 }
442 }
443
444 /* Next, exclude symbols from certain startup objects. */
445
446 if (abfd && (p = lbasename (abfd->filename)) )
447 {
448 afptr = autofilter_objlist;
449 while (afptr->name)
450 {
451 if ( strcmp (p, afptr->name) == 0 )
452 return 0;
453 afptr ++;
454 }
455 }
456
457 /* Don't try to blindly exclude all symbols
458 that begin with '__'; this was tried and
459 it is too restrictive. */
460
461 /* Then, exclude specific symbols. */
462 afptr = autofilter_symbollist;
463 while (afptr->name)
464 {
465 if (strcmp (n, afptr->name) == 0)
466 return 0;
467
468 afptr ++;
469 }
470
471 /* Next, exclude symbols starting with ... */
472 afptr = autofilter_symbolprefixlist;
473 while (afptr->name)
474 {
475 if (strncmp (n, afptr->name, afptr->len) == 0)
476 return 0;
477
478 afptr ++;
479 }
480
481 /* Finally, exclude symbols ending with ... */
482 len = strlen (n);
483 afptr = autofilter_symbolsuffixlist;
484 while (afptr->name)
485 {
486 if ((len >= afptr->len) &&
487 /* Add 1 to insure match with trailing '\0'. */
488 strncmp (n + len - afptr->len, afptr->name,
489 afptr->len + 1) == 0)
490 return 0;
491
492 afptr ++;
493 }
494 }
495
496 for (ex = excludes; ex; ex = ex->next)
497 if (strcmp (n, ex->string) == 0)
498 return 0;
499
500 return 1;
501 }
502
503 static void
504 process_def_file (abfd, info)
505 bfd *abfd ATTRIBUTE_UNUSED;
506 struct bfd_link_info *info;
507 {
508 int i, j;
509 struct bfd_link_hash_entry *blhe;
510 bfd *b;
511 struct sec *s;
512 def_file_export *e = 0;
513
514 if (!pe_def_file)
515 pe_def_file = def_file_empty ();
516
517 /* First, run around to all the objects looking for the .drectve
518 sections, and push those into the def file too. */
519 for (b = info->input_bfds; b; b = b->link_next)
520 {
521 s = bfd_get_section_by_name (b, ".drectve");
522 if (s)
523 {
524 int size = bfd_get_section_size_before_reloc (s);
525 char *buf = xmalloc (size);
526
527 bfd_get_section_contents (b, s, buf, 0, size);
528 def_file_add_directive (pe_def_file, buf, size);
529 free (buf);
530 }
531 }
532
533 /* Now, maybe export everything else the default way. */
534 if (pe_dll_export_everything || pe_def_file->num_exports == 0)
535 {
536 for (b = info->input_bfds; b; b = b->link_next)
537 {
538 asymbol **symbols;
539 int nsyms, symsize;
540
541 symsize = bfd_get_symtab_upper_bound (b);
542 symbols = (asymbol **) xmalloc (symsize);
543 nsyms = bfd_canonicalize_symtab (b, symbols);
544
545 for (j = 0; j < nsyms; j++)
546 {
547 /* We should export symbols which are either global or not
548 anything at all. (.bss data is the latter)
549 We should not export undefined symbols. */
550 if (symbols[j]->section != &bfd_und_section
551 && ((symbols[j]->flags & BSF_GLOBAL)
552 || (symbols[j]->flags == BFD_FORT_COMM_DEFAULT_VALUE)))
553 {
554 const char *sn = symbols[j]->name;
555
556 /* We should not re-export imported stuff. */
557 {
558 char *name = (char *) xmalloc (strlen (sn) + 2 + 6);
559 sprintf (name, "%s%s", U("_imp_"), sn);
560
561 blhe = bfd_link_hash_lookup (info->hash, name,
562 false, false, false);
563 free (name);
564
565 if (blhe && blhe->type == bfd_link_hash_defined)
566 continue;
567 }
568
569 if (*sn == '_')
570 sn++;
571
572 if (auto_export (b, pe_def_file, sn))
573 {
574 def_file_export *p;
575 p=def_file_add_export (pe_def_file, sn, 0, -1);
576 /* Fill data flag properly, from dlltool.c. */
577 p->flag_data = !(symbols[j]->flags & BSF_FUNCTION);
578 }
579 }
580 }
581 }
582 }
583
584 #undef NE
585 #define NE pe_def_file->num_exports
586
587 /* Canonicalize the export list. */
588 if (pe_dll_kill_ats)
589 {
590 for (i = 0; i < NE; i++)
591 {
592 if (strchr (pe_def_file->exports[i].name, '@'))
593 {
594 /* This will preserve internal_name, which may have been
595 pointing to the same memory as name, or might not
596 have. */
597 char *tmp = xstrdup (pe_def_file->exports[i].name);
598
599 *(strchr (tmp, '@')) = 0;
600 pe_def_file->exports[i].name = tmp;
601 }
602 }
603 }
604
605 if (pe_dll_stdcall_aliases)
606 {
607 for (i = 0; i < NE; i++)
608 {
609 if (strchr (pe_def_file->exports[i].name, '@'))
610 {
611 char *tmp = xstrdup (pe_def_file->exports[i].name);
612
613 *(strchr (tmp, '@')) = 0;
614 if (auto_export (NULL, pe_def_file, tmp))
615 def_file_add_export (pe_def_file, tmp,
616 pe_def_file->exports[i].internal_name,
617 -1);
618 else
619 free (tmp);
620 }
621 }
622 }
623
624 /* Convenience, but watch out for it changing. */
625 e = pe_def_file->exports;
626
627 exported_symbol_offsets = (bfd_vma *) xmalloc (NE * sizeof (bfd_vma));
628 exported_symbol_sections = (struct sec **) xmalloc (NE * sizeof (struct sec *));
629
630 memset (exported_symbol_sections, 0, NE * sizeof (struct sec *));
631 max_ordinal = 0;
632 min_ordinal = 65536;
633 count_exported = 0;
634 count_exported_byname = 0;
635 count_with_ordinals = 0;
636
637 qsort (pe_def_file->exports, NE, sizeof (pe_def_file->exports[0]), pe_export_sort);
638 for (i = 0, j = 0; i < NE; i++)
639 {
640 if (i > 0 && strcmp (e[i].name, e[i - 1].name) == 0)
641 {
642 /* This is a duplicate. */
643 if (e[j - 1].ordinal != -1
644 && e[i].ordinal != -1
645 && e[j - 1].ordinal != e[i].ordinal)
646 {
647 if (pe_dll_warn_dup_exports)
648 /* xgettext:c-format */
649 einfo (_("%XError, duplicate EXPORT with ordinals: %s (%d vs %d)\n"),
650 e[j - 1].name, e[j - 1].ordinal, e[i].ordinal);
651 }
652 else
653 {
654 if (pe_dll_warn_dup_exports)
655 /* xgettext:c-format */
656 einfo (_("Warning, duplicate EXPORT: %s\n"),
657 e[j - 1].name);
658 }
659
660 if (e[i].ordinal != -1)
661 e[j - 1].ordinal = e[i].ordinal;
662 e[j - 1].flag_private |= e[i].flag_private;
663 e[j - 1].flag_constant |= e[i].flag_constant;
664 e[j - 1].flag_noname |= e[i].flag_noname;
665 e[j - 1].flag_data |= e[i].flag_data;
666 }
667 else
668 {
669 if (i != j)
670 e[j] = e[i];
671 j++;
672 }
673 }
674 pe_def_file->num_exports = j; /* == NE */
675
676 for (i = 0; i < NE; i++)
677 {
678 char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
679
680 if (pe_details->underscored)
681 {
682 *name = '_';
683 strcpy (name + 1, pe_def_file->exports[i].internal_name);
684 }
685 else
686 strcpy (name, pe_def_file->exports[i].internal_name);
687
688 blhe = bfd_link_hash_lookup (info->hash,
689 name,
690 false, false, true);
691
692 if (blhe
693 && (blhe->type == bfd_link_hash_defined
694 || (blhe->type == bfd_link_hash_common)))
695 {
696 count_exported++;
697 if (!pe_def_file->exports[i].flag_noname)
698 count_exported_byname++;
699
700 /* Only fill in the sections. The actual offsets are computed
701 in fill_exported_offsets() after common symbols are laid
702 out. */
703 if (blhe->type == bfd_link_hash_defined)
704 exported_symbol_sections[i] = blhe->u.def.section;
705 else
706 exported_symbol_sections[i] = blhe->u.c.p->section;
707
708 if (pe_def_file->exports[i].ordinal != -1)
709 {
710 if (max_ordinal < pe_def_file->exports[i].ordinal)
711 max_ordinal = pe_def_file->exports[i].ordinal;
712 if (min_ordinal > pe_def_file->exports[i].ordinal)
713 min_ordinal = pe_def_file->exports[i].ordinal;
714 count_with_ordinals++;
715 }
716 }
717 else if (blhe && blhe->type == bfd_link_hash_undefined)
718 {
719 /* xgettext:c-format */
720 einfo (_("%XCannot export %s: symbol not defined\n"),
721 pe_def_file->exports[i].internal_name);
722 }
723 else if (blhe)
724 {
725 /* xgettext:c-format */
726 einfo (_("%XCannot export %s: symbol wrong type (%d vs %d)\n"),
727 pe_def_file->exports[i].internal_name,
728 blhe->type, bfd_link_hash_defined);
729 }
730 else
731 {
732 /* xgettext:c-format */
733 einfo (_("%XCannot export %s: symbol not found\n"),
734 pe_def_file->exports[i].internal_name);
735 }
736 free (name);
737 }
738 }
739
740 /* Build the bfd that will contain .edata and .reloc sections. */
741
742 static void
743 build_filler_bfd (include_edata)
744 int include_edata;
745 {
746 lang_input_statement_type *filler_file;
747 filler_file = lang_add_input_file ("dll stuff",
748 lang_input_file_is_fake_enum,
749 NULL);
750 filler_file->the_bfd = filler_bfd = bfd_create ("dll stuff", output_bfd);
751 if (filler_bfd == NULL
752 || !bfd_set_arch_mach (filler_bfd,
753 bfd_get_arch (output_bfd),
754 bfd_get_mach (output_bfd)))
755 {
756 einfo ("%X%P: can not create BFD %E\n");
757 return;
758 }
759
760 if (include_edata)
761 {
762 edata_s = bfd_make_section_old_way (filler_bfd, ".edata");
763 if (edata_s == NULL
764 || !bfd_set_section_flags (filler_bfd, edata_s,
765 (SEC_HAS_CONTENTS
766 | SEC_ALLOC
767 | SEC_LOAD
768 | SEC_KEEP
769 | SEC_IN_MEMORY)))
770 {
771 einfo ("%X%P: can not create .edata section: %E\n");
772 return;
773 }
774 bfd_set_section_size (filler_bfd, edata_s, edata_sz);
775 }
776
777 reloc_s = bfd_make_section_old_way (filler_bfd, ".reloc");
778 if (reloc_s == NULL
779 || !bfd_set_section_flags (filler_bfd, reloc_s,
780 (SEC_HAS_CONTENTS
781 | SEC_ALLOC
782 | SEC_LOAD
783 | SEC_KEEP
784 | SEC_IN_MEMORY)))
785 {
786 einfo ("%X%P: can not create .reloc section: %E\n");
787 return;
788 }
789
790 bfd_set_section_size (filler_bfd, reloc_s, 0);
791
792 ldlang_add_file (filler_file);
793 }
794
795 /* Gather all the exported symbols and build the .edata section. */
796
797 static void
798 generate_edata (abfd, info)
799 bfd *abfd;
800 struct bfd_link_info *info ATTRIBUTE_UNUSED;
801 {
802 int i, next_ordinal;
803 int name_table_size = 0;
804 const char *dlnp;
805
806 /* First, we need to know how many exported symbols there are,
807 and what the range of ordinals is. */
808 if (pe_def_file->name)
809 dll_name = pe_def_file->name;
810 else
811 {
812 dll_name = abfd->filename;
813
814 for (dlnp = dll_name; *dlnp; dlnp++)
815 if (*dlnp == '\\' || *dlnp == '/' || *dlnp == ':')
816 dll_name = dlnp + 1;
817 }
818
819 if (count_with_ordinals && max_ordinal > count_exported)
820 {
821 if (min_ordinal > max_ordinal - count_exported + 1)
822 min_ordinal = max_ordinal - count_exported + 1;
823 }
824 else
825 {
826 min_ordinal = 1;
827 max_ordinal = count_exported;
828 }
829
830 export_table_size = max_ordinal - min_ordinal + 1;
831 exported_symbols = (int *) xmalloc (export_table_size * sizeof (int));
832 for (i = 0; i < export_table_size; i++)
833 exported_symbols[i] = -1;
834
835 /* Now we need to assign ordinals to those that don't have them. */
836 for (i = 0; i < NE; i++)
837 {
838 if (exported_symbol_sections[i])
839 {
840 if (pe_def_file->exports[i].ordinal != -1)
841 {
842 int ei = pe_def_file->exports[i].ordinal - min_ordinal;
843 int pi = exported_symbols[ei];
844
845 if (pi != -1)
846 {
847 /* xgettext:c-format */
848 einfo (_("%XError, ordinal used twice: %d (%s vs %s)\n"),
849 pe_def_file->exports[i].ordinal,
850 pe_def_file->exports[i].name,
851 pe_def_file->exports[pi].name);
852 }
853 exported_symbols[ei] = i;
854 }
855 name_table_size += strlen (pe_def_file->exports[i].name) + 1;
856 }
857 }
858
859 next_ordinal = min_ordinal;
860 for (i = 0; i < NE; i++)
861 if (exported_symbol_sections[i])
862 if (pe_def_file->exports[i].ordinal == -1)
863 {
864 while (exported_symbols[next_ordinal - min_ordinal] != -1)
865 next_ordinal ++;
866
867 exported_symbols[next_ordinal - min_ordinal] = i;
868 pe_def_file->exports[i].ordinal = next_ordinal;
869 }
870
871 /* OK, now we can allocate some memory. */
872 edata_sz = (40 /* directory */
873 + 4 * export_table_size /* addresses */
874 + 4 * count_exported_byname /* name ptrs */
875 + 2 * count_exported_byname /* ordinals */
876 + name_table_size + strlen (dll_name) + 1);
877 }
878
879 /* Fill the exported symbol offsets. The preliminary work has already
880 been done in process_def_file(). */
881
882 static void
883 fill_exported_offsets (abfd, info)
884 bfd *abfd ATTRIBUTE_UNUSED;
885 struct bfd_link_info *info;
886 {
887 int i;
888 struct bfd_link_hash_entry *blhe;
889
890 for (i = 0; i < pe_def_file->num_exports; i++)
891 {
892 char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
893
894 if (pe_details->underscored)
895 {
896 *name = '_';
897 strcpy (name + 1, pe_def_file->exports[i].internal_name);
898 }
899 else
900 strcpy (name, pe_def_file->exports[i].internal_name);
901
902 blhe = bfd_link_hash_lookup (info->hash,
903 name,
904 false, false, true);
905
906 if (blhe && (blhe->type == bfd_link_hash_defined))
907 exported_symbol_offsets[i] = blhe->u.def.value;
908
909 free (name);
910 }
911 }
912
913 static void
914 fill_edata (abfd, info)
915 bfd *abfd;
916 struct bfd_link_info *info ATTRIBUTE_UNUSED;
917 {
918 int i, hint;
919 unsigned char *edirectory;
920 unsigned long *eaddresses;
921 unsigned long *enameptrs;
922 unsigned short *eordinals;
923 unsigned char *enamestr;
924 time_t now;
925
926 time (&now);
927
928 edata_d = (unsigned char *) xmalloc (edata_sz);
929
930 /* Note use of array pointer math here. */
931 edirectory = edata_d;
932 eaddresses = (unsigned long *) (edata_d + 40);
933 enameptrs = eaddresses + export_table_size;
934 eordinals = (unsigned short *) (enameptrs + count_exported_byname);
935 enamestr = (char *) (eordinals + count_exported_byname);
936
937 #define ERVA(ptr) (((unsigned char *)(ptr) - edata_d) + edata_s->output_section->vma - image_base)
938
939 memset (edata_d, 0, edata_sz);
940 bfd_put_32 (abfd, now, edata_d + 4);
941 if (pe_def_file->version_major != -1)
942 {
943 bfd_put_16 (abfd, pe_def_file->version_major, edata_d + 8);
944 bfd_put_16 (abfd, pe_def_file->version_minor, edata_d + 10);
945 }
946
947 bfd_put_32 (abfd, ERVA (enamestr), edata_d + 12);
948 strcpy (enamestr, dll_name);
949 enamestr += strlen (enamestr) + 1;
950 bfd_put_32 (abfd, min_ordinal, edata_d + 16);
951 bfd_put_32 (abfd, export_table_size, edata_d + 20);
952 bfd_put_32 (abfd, count_exported_byname, edata_d + 24);
953 bfd_put_32 (abfd, ERVA (eaddresses), edata_d + 28);
954 bfd_put_32 (abfd, ERVA (enameptrs), edata_d + 32);
955 bfd_put_32 (abfd, ERVA (eordinals), edata_d + 36);
956
957 fill_exported_offsets (abfd, info);
958
959 /* Ok, now for the filling in part. */
960 hint = 0;
961 for (i = 0; i < export_table_size; i++)
962 {
963 int s = exported_symbols[i];
964
965 if (s != -1)
966 {
967 struct sec *ssec = exported_symbol_sections[s];
968 unsigned long srva = (exported_symbol_offsets[s]
969 + ssec->output_section->vma
970 + ssec->output_offset);
971 int ord = pe_def_file->exports[s].ordinal;
972
973 bfd_put_32 (abfd, srva - image_base,
974 (void *) (eaddresses + ord - min_ordinal));
975
976 if (!pe_def_file->exports[s].flag_noname)
977 {
978 char *ename = pe_def_file->exports[s].name;
979 bfd_put_32 (abfd, ERVA (enamestr), (void *) enameptrs);
980 enameptrs++;
981 strcpy (enamestr, ename);
982 enamestr += strlen (enamestr) + 1;
983 bfd_put_16 (abfd, ord - min_ordinal, (void *) eordinals);
984 eordinals++;
985 pe_def_file->exports[s].hint = hint++;
986 }
987 }
988 }
989 }
990
991
992 static struct sec *current_sec;
993
994 void
995 pe_walk_relocs_of_symbol (info, name, cb)
996 struct bfd_link_info *info;
997 const char *name;
998 int (*cb) (arelent *, asection *);
999 {
1000 bfd *b;
1001 asection *s;
1002
1003 for (b = info->input_bfds; b; b = b->link_next)
1004 {
1005 asymbol **symbols;
1006 int nsyms, symsize;
1007
1008 symsize = bfd_get_symtab_upper_bound (b);
1009 symbols = (asymbol **) xmalloc (symsize);
1010 nsyms = bfd_canonicalize_symtab (b, symbols);
1011
1012 for (s = b->sections; s; s = s->next)
1013 {
1014 arelent **relocs;
1015 int relsize, nrelocs, i;
1016 int flags = bfd_get_section_flags (b, s);
1017
1018 /* Skip discarded linkonce sections. */
1019 if (flags & SEC_LINK_ONCE
1020 && s->output_section == bfd_abs_section_ptr)
1021 continue;
1022
1023 current_sec = s;
1024
1025 relsize = bfd_get_reloc_upper_bound (b, s);
1026 relocs = (arelent **) xmalloc ((size_t) relsize);
1027 nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols);
1028
1029 for (i = 0; i < nrelocs; i++)
1030 {
1031 struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr;
1032
1033 if (!strcmp (name, sym->name))
1034 cb (relocs[i], s);
1035 }
1036
1037 free (relocs);
1038
1039 /* Warning: the allocated symbols are remembered in BFD and reused
1040 later, so don't free them! */
1041 /* free (symbols); */
1042 }
1043 }
1044 }
1045
1046 /* Gather all the relocations and build the .reloc section. */
1047
1048 static void
1049 generate_reloc (abfd, info)
1050 bfd *abfd;
1051 struct bfd_link_info *info;
1052 {
1053
1054 /* For .reloc stuff. */
1055 reloc_data_type *reloc_data;
1056 int total_relocs = 0;
1057 int i;
1058 unsigned long sec_page = (unsigned long) (-1);
1059 unsigned long page_ptr, page_count;
1060 int bi;
1061 bfd *b;
1062 struct sec *s;
1063
1064 total_relocs = 0;
1065 for (b = info->input_bfds; b; b = b->link_next)
1066 for (s = b->sections; s; s = s->next)
1067 total_relocs += s->reloc_count;
1068
1069 reloc_data =
1070 (reloc_data_type *) xmalloc (total_relocs * sizeof (reloc_data_type));
1071
1072 total_relocs = 0;
1073 bi = 0;
1074 for (bi = 0, b = info->input_bfds; b; bi++, b = b->link_next)
1075 {
1076 arelent **relocs;
1077 int relsize, nrelocs, i;
1078
1079 for (s = b->sections; s; s = s->next)
1080 {
1081 unsigned long sec_vma = s->output_section->vma + s->output_offset;
1082 asymbol **symbols;
1083 int nsyms, symsize;
1084
1085 /* If it's not loaded, we don't need to relocate it this way. */
1086 if (!(s->output_section->flags & SEC_LOAD))
1087 continue;
1088
1089 /* I don't know why there would be a reloc for these, but I've
1090 seen it happen - DJ */
1091 if (s->output_section == &bfd_abs_section)
1092 continue;
1093
1094 if (s->output_section->vma == 0)
1095 {
1096 /* Huh? Shouldn't happen, but punt if it does. */
1097 einfo ("DJ: zero vma section reloc detected: `%s' #%d f=%d\n",
1098 s->output_section->name, s->output_section->index,
1099 s->output_section->flags);
1100 continue;
1101 }
1102
1103 symsize = bfd_get_symtab_upper_bound (b);
1104 symbols = (asymbol **) xmalloc (symsize);
1105 nsyms = bfd_canonicalize_symtab (b, symbols);
1106
1107 relsize = bfd_get_reloc_upper_bound (b, s);
1108 relocs = (arelent **) xmalloc ((size_t) relsize);
1109 nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols);
1110
1111 for (i = 0; i < nrelocs; i++)
1112 {
1113 if (pe_dll_extra_pe_debug)
1114 {
1115 struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr;
1116 printf("rel: %s\n",sym->name);
1117 }
1118 if (!relocs[i]->howto->pc_relative
1119 && relocs[i]->howto->type != pe_details->imagebase_reloc)
1120 {
1121 bfd_vma sym_vma;
1122 struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr;
1123
1124 sym_vma = (relocs[i]->addend
1125 + sym->value
1126 + sym->section->vma
1127 + sym->section->output_offset
1128 + sym->section->output_section->vma);
1129 reloc_data[total_relocs].vma = sec_vma + relocs[i]->address;
1130
1131 #define BITS_AND_SHIFT(bits, shift) (bits * 1000 | shift)
1132
1133 switch BITS_AND_SHIFT (relocs[i]->howto->bitsize,
1134 relocs[i]->howto->rightshift)
1135 {
1136 case BITS_AND_SHIFT (32, 0):
1137 reloc_data[total_relocs].type = 3;
1138 total_relocs++;
1139 break;
1140 case BITS_AND_SHIFT (16, 0):
1141 reloc_data[total_relocs].type = 2;
1142 total_relocs++;
1143 break;
1144 case BITS_AND_SHIFT (16, 16):
1145 reloc_data[total_relocs].type = 4;
1146 /* FIXME: we can't know the symbol's right value
1147 yet, but we probably can safely assume that
1148 CE will relocate us in 64k blocks, so leaving
1149 it zero is safe. */
1150 reloc_data[total_relocs].extra = 0;
1151 total_relocs++;
1152 break;
1153 case BITS_AND_SHIFT (26, 2):
1154 reloc_data[total_relocs].type = 5;
1155 total_relocs++;
1156 break;
1157 default:
1158 /* xgettext:c-format */
1159 einfo (_("%XError: %d-bit reloc in dll\n"),
1160 relocs[i]->howto->bitsize);
1161 break;
1162 }
1163 }
1164 }
1165 free (relocs);
1166 /* Warning: the allocated symbols are remembered in BFD and
1167 reused later, so don't free them! */
1168 #if 0
1169 free (symbol);
1170 #endif
1171 }
1172 }
1173
1174 /* At this point, we have total_relocs relocation addresses in
1175 reloc_addresses, which are all suitable for the .reloc section.
1176 We must now create the new sections. */
1177 qsort (reloc_data, total_relocs, sizeof (*reloc_data), reloc_sort);
1178
1179 for (i = 0; i < total_relocs; i++)
1180 {
1181 unsigned long this_page = (reloc_data[i].vma >> 12);
1182
1183 if (this_page != sec_page)
1184 {
1185 reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align. */
1186 reloc_sz += 8;
1187 sec_page = this_page;
1188 }
1189
1190 reloc_sz += 2;
1191
1192 if (reloc_data[i].type == 4)
1193 reloc_sz += 2;
1194 }
1195
1196 reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align. */
1197 reloc_d = (unsigned char *) xmalloc (reloc_sz);
1198 sec_page = (unsigned long) (-1);
1199 reloc_sz = 0;
1200 page_ptr = (unsigned long) (-1);
1201 page_count = 0;
1202
1203 for (i = 0; i < total_relocs; i++)
1204 {
1205 unsigned long rva = reloc_data[i].vma - image_base;
1206 unsigned long this_page = (rva & ~0xfff);
1207
1208 if (this_page != sec_page)
1209 {
1210 while (reloc_sz & 3)
1211 reloc_d[reloc_sz++] = 0;
1212
1213 if (page_ptr != (unsigned long) (-1))
1214 bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
1215
1216 bfd_put_32 (abfd, this_page, reloc_d + reloc_sz);
1217 page_ptr = reloc_sz;
1218 reloc_sz += 8;
1219 sec_page = this_page;
1220 page_count = 0;
1221 }
1222
1223 bfd_put_16 (abfd, (rva & 0xfff) + (reloc_data[i].type << 12),
1224 reloc_d + reloc_sz);
1225 reloc_sz += 2;
1226
1227 if (reloc_data[i].type == 4)
1228 {
1229 bfd_put_16 (abfd, reloc_data[i].extra, reloc_d + reloc_sz);
1230 reloc_sz += 2;
1231 }
1232
1233 page_count++;
1234 }
1235
1236 while (reloc_sz & 3)
1237 reloc_d[reloc_sz++] = 0;
1238
1239 if (page_ptr != (unsigned long) (-1))
1240 bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
1241
1242 while (reloc_sz < reloc_s->_raw_size)
1243 reloc_d[reloc_sz++] = 0;
1244 }
1245
1246 /* Given the exiting def_file structure, print out a .DEF file that
1247 corresponds to it. */
1248
1249 static void
1250 quoteput (s, f, needs_quotes)
1251 char *s;
1252 FILE *f;
1253 int needs_quotes;
1254 {
1255 char *cp;
1256
1257 for (cp = s; *cp; cp++)
1258 if (*cp == '\''
1259 || *cp == '"'
1260 || *cp == '\\'
1261 || ISSPACE (*cp)
1262 || *cp == ','
1263 || *cp == ';')
1264 needs_quotes = 1;
1265
1266 if (needs_quotes)
1267 {
1268 putc ('"', f);
1269
1270 while (*s)
1271 {
1272 if (*s == '"' || *s == '\\')
1273 putc ('\\', f);
1274
1275 putc (*s, f);
1276 s++;
1277 }
1278
1279 putc ('"', f);
1280 }
1281 else
1282 fputs (s, f);
1283 }
1284
1285 void
1286 pe_dll_generate_def_file (pe_out_def_filename)
1287 const char *pe_out_def_filename;
1288 {
1289 int i;
1290 FILE *out = fopen (pe_out_def_filename, "w");
1291
1292 if (out == NULL)
1293 /* xgettext:c-format */
1294 einfo (_("%s: Can't open output def file %s\n"),
1295 program_name, pe_out_def_filename);
1296
1297 if (pe_def_file)
1298 {
1299 if (pe_def_file->name)
1300 {
1301 if (pe_def_file->is_dll)
1302 fprintf (out, "LIBRARY ");
1303 else
1304 fprintf (out, "NAME ");
1305
1306 quoteput (pe_def_file->name, out, 1);
1307
1308 if (pe_data (output_bfd)->pe_opthdr.ImageBase)
1309 fprintf (out, " BASE=0x%lx",
1310 (unsigned long) pe_data (output_bfd)->pe_opthdr.ImageBase);
1311 fprintf (out, "\n");
1312 }
1313
1314 if (pe_def_file->description)
1315 {
1316 fprintf (out, "DESCRIPTION ");
1317 quoteput (pe_def_file->description, out, 1);
1318 fprintf (out, "\n");
1319 }
1320
1321 if (pe_def_file->version_minor != -1)
1322 fprintf (out, "VERSION %d.%d\n", pe_def_file->version_major,
1323 pe_def_file->version_minor);
1324 else if (pe_def_file->version_major != -1)
1325 fprintf (out, "VERSION %d\n", pe_def_file->version_major);
1326
1327 if (pe_def_file->stack_reserve != -1 || pe_def_file->heap_reserve != -1)
1328 fprintf (out, "\n");
1329
1330 if (pe_def_file->stack_commit != -1)
1331 fprintf (out, "STACKSIZE 0x%x,0x%x\n",
1332 pe_def_file->stack_reserve, pe_def_file->stack_commit);
1333 else if (pe_def_file->stack_reserve != -1)
1334 fprintf (out, "STACKSIZE 0x%x\n", pe_def_file->stack_reserve);
1335
1336 if (pe_def_file->heap_commit != -1)
1337 fprintf (out, "HEAPSIZE 0x%x,0x%x\n",
1338 pe_def_file->heap_reserve, pe_def_file->heap_commit);
1339 else if (pe_def_file->heap_reserve != -1)
1340 fprintf (out, "HEAPSIZE 0x%x\n", pe_def_file->heap_reserve);
1341
1342 if (pe_def_file->num_section_defs > 0)
1343 {
1344 fprintf (out, "\nSECTIONS\n\n");
1345
1346 for (i = 0; i < pe_def_file->num_section_defs; i++)
1347 {
1348 fprintf (out, " ");
1349 quoteput (pe_def_file->section_defs[i].name, out, 0);
1350
1351 if (pe_def_file->section_defs[i].class)
1352 {
1353 fprintf (out, " CLASS ");
1354 quoteput (pe_def_file->section_defs[i].class, out, 0);
1355 }
1356
1357 if (pe_def_file->section_defs[i].flag_read)
1358 fprintf (out, " READ");
1359
1360 if (pe_def_file->section_defs[i].flag_write)
1361 fprintf (out, " WRITE");
1362
1363 if (pe_def_file->section_defs[i].flag_execute)
1364 fprintf (out, " EXECUTE");
1365
1366 if (pe_def_file->section_defs[i].flag_shared)
1367 fprintf (out, " SHARED");
1368
1369 fprintf (out, "\n");
1370 }
1371 }
1372
1373 if (pe_def_file->num_exports > 0)
1374 {
1375 fprintf (out, "EXPORTS\n");
1376
1377 for (i = 0; i < pe_def_file->num_exports; i++)
1378 {
1379 def_file_export *e = pe_def_file->exports + i;
1380 fprintf (out, " ");
1381 quoteput (e->name, out, 0);
1382
1383 if (e->internal_name && strcmp (e->internal_name, e->name))
1384 {
1385 fprintf (out, " = ");
1386 quoteput (e->internal_name, out, 0);
1387 }
1388
1389 if (e->ordinal != -1)
1390 fprintf (out, " @%d", e->ordinal);
1391
1392 if (e->flag_private)
1393 fprintf (out, " PRIVATE");
1394
1395 if (e->flag_constant)
1396 fprintf (out, " CONSTANT");
1397
1398 if (e->flag_noname)
1399 fprintf (out, " NONAME");
1400
1401 if (e->flag_data)
1402 fprintf (out, " DATA");
1403
1404 fprintf (out, "\n");
1405 }
1406 }
1407
1408 if (pe_def_file->num_imports > 0)
1409 {
1410 fprintf (out, "\nIMPORTS\n\n");
1411
1412 for (i = 0; i < pe_def_file->num_imports; i++)
1413 {
1414 def_file_import *im = pe_def_file->imports + i;
1415 fprintf (out, " ");
1416
1417 if (im->internal_name
1418 && (!im->name || strcmp (im->internal_name, im->name)))
1419 {
1420 quoteput (im->internal_name, out, 0);
1421 fprintf (out, " = ");
1422 }
1423
1424 quoteput (im->module->name, out, 0);
1425 fprintf (out, ".");
1426
1427 if (im->name)
1428 quoteput (im->name, out, 0);
1429 else
1430 fprintf (out, "%d", im->ordinal);
1431
1432 fprintf (out, "\n");
1433 }
1434 }
1435 }
1436 else
1437 fprintf (out, _("; no contents available\n"));
1438
1439 if (fclose (out) == EOF)
1440 /* xgettext:c-format */
1441 einfo (_("%P: Error closing file `%s'\n"), pe_out_def_filename);
1442 }
1443
1444 /* Generate the import library. */
1445
1446 static asymbol **symtab;
1447 static int symptr;
1448 static int tmp_seq;
1449 static const char *dll_filename;
1450 static char *dll_symname;
1451
1452 #define UNDSEC (asection *) &bfd_und_section
1453
1454 static asection *
1455 quick_section (abfd, name, flags, align)
1456 bfd *abfd;
1457 const char *name;
1458 int flags;
1459 int align;
1460 {
1461 asection *sec;
1462 asymbol *sym;
1463
1464 sec = bfd_make_section_old_way (abfd, name);
1465 bfd_set_section_flags (abfd, sec, flags | SEC_ALLOC | SEC_LOAD | SEC_KEEP);
1466 bfd_set_section_alignment (abfd, sec, align);
1467 /* Remember to undo this before trying to link internally! */
1468 sec->output_section = sec;
1469
1470 sym = bfd_make_empty_symbol (abfd);
1471 symtab[symptr++] = sym;
1472 sym->name = sec->name;
1473 sym->section = sec;
1474 sym->flags = BSF_LOCAL;
1475 sym->value = 0;
1476
1477 return sec;
1478 }
1479
1480 static void
1481 quick_symbol (abfd, n1, n2, n3, sec, flags, addr)
1482 bfd *abfd;
1483 const char *n1;
1484 const char *n2;
1485 const char *n3;
1486 asection *sec;
1487 int flags;
1488 int addr;
1489 {
1490 asymbol *sym;
1491 char *name = (char *) xmalloc (strlen (n1) + strlen (n2) + strlen (n3) + 1);
1492
1493 strcpy (name, n1);
1494 strcat (name, n2);
1495 strcat (name, n3);
1496 sym = bfd_make_empty_symbol (abfd);
1497 sym->name = name;
1498 sym->section = sec;
1499 sym->flags = flags;
1500 sym->value = addr;
1501 symtab[symptr++] = sym;
1502 }
1503
1504 static arelent *reltab = 0;
1505 static int relcount = 0, relsize = 0;
1506
1507 static void
1508 quick_reloc (abfd, address, which_howto, symidx)
1509 bfd *abfd;
1510 int address;
1511 int which_howto;
1512 int symidx;
1513 {
1514 if (relcount >= (relsize - 1))
1515 {
1516 relsize += 10;
1517 if (reltab)
1518 reltab = (arelent *) xrealloc (reltab, relsize * sizeof (arelent));
1519 else
1520 reltab = (arelent *) xmalloc (relsize * sizeof (arelent));
1521 }
1522 reltab[relcount].address = address;
1523 reltab[relcount].addend = 0;
1524 reltab[relcount].howto = bfd_reloc_type_lookup (abfd, which_howto);
1525 reltab[relcount].sym_ptr_ptr = symtab + symidx;
1526 relcount++;
1527 }
1528
1529 static void
1530 save_relocs (asection *sec)
1531 {
1532 int i;
1533
1534 sec->relocation = reltab;
1535 sec->reloc_count = relcount;
1536 sec->orelocation = (arelent **) xmalloc ((relcount + 1) * sizeof (arelent *));
1537 for (i = 0; i < relcount; i++)
1538 sec->orelocation[i] = sec->relocation + i;
1539 sec->orelocation[relcount] = 0;
1540 sec->flags |= SEC_RELOC;
1541 reltab = 0;
1542 relcount = relsize = 0;
1543 }
1544
1545 /* .section .idata$2
1546 .global __head_my_dll
1547 __head_my_dll:
1548 .rva hname
1549 .long 0
1550 .long 0
1551 .rva __my_dll_iname
1552 .rva fthunk
1553
1554 .section .idata$5
1555 .long 0
1556 fthunk:
1557
1558 .section .idata$4
1559 .long 0
1560 hname: */
1561
1562 static bfd *
1563 make_head (parent)
1564 bfd *parent;
1565 {
1566 asection *id2, *id5, *id4;
1567 unsigned char *d2, *d5, *d4;
1568 char *oname;
1569 bfd *abfd;
1570
1571 oname = (char *) xmalloc (20);
1572 sprintf (oname, "d%06d.o", tmp_seq);
1573 tmp_seq++;
1574
1575 abfd = bfd_create (oname, parent);
1576 bfd_find_target (pe_details->object_target, abfd);
1577 bfd_make_writable (abfd);
1578
1579 bfd_set_format (abfd, bfd_object);
1580 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1581
1582 symptr = 0;
1583 symtab = (asymbol **) xmalloc (6 * sizeof (asymbol *));
1584 id2 = quick_section (abfd, ".idata$2", SEC_HAS_CONTENTS, 2);
1585 id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1586 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1587 quick_symbol (abfd, U ("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0);
1588 quick_symbol (abfd, U (""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
1589
1590 /* OK, pay attention here. I got confused myself looking back at
1591 it. We create a four-byte section to mark the beginning of the
1592 list, and we include an offset of 4 in the section, so that the
1593 pointer to the list points to the *end* of this section, which is
1594 the start of the list of sections from other objects. */
1595
1596 bfd_set_section_size (abfd, id2, 20);
1597 d2 = (unsigned char *) xmalloc (20);
1598 id2->contents = d2;
1599 memset (d2, 0, 20);
1600 d2[0] = d2[16] = 4; /* Reloc addend. */
1601 quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
1602 quick_reloc (abfd, 12, BFD_RELOC_RVA, 4);
1603 quick_reloc (abfd, 16, BFD_RELOC_RVA, 1);
1604 save_relocs (id2);
1605
1606 bfd_set_section_size (abfd, id5, 4);
1607 d5 = (unsigned char *) xmalloc (4);
1608 id5->contents = d5;
1609 memset (d5, 0, 4);
1610
1611 bfd_set_section_size (abfd, id4, 4);
1612 d4 = (unsigned char *) xmalloc (4);
1613 id4->contents = d4;
1614 memset (d4, 0, 4);
1615
1616 bfd_set_symtab (abfd, symtab, symptr);
1617
1618 bfd_set_section_contents (abfd, id2, d2, 0, 20);
1619 bfd_set_section_contents (abfd, id5, d5, 0, 4);
1620 bfd_set_section_contents (abfd, id4, d4, 0, 4);
1621
1622 bfd_make_readable (abfd);
1623 return abfd;
1624 }
1625
1626 /* .section .idata$4
1627 .long 0
1628 .section .idata$5
1629 .long 0
1630 .section idata$7
1631 .global __my_dll_iname
1632 __my_dll_iname:
1633 .asciz "my.dll" */
1634
1635 static bfd *
1636 make_tail (parent)
1637 bfd *parent;
1638 {
1639 asection *id4, *id5, *id7;
1640 unsigned char *d4, *d5, *d7;
1641 int len;
1642 char *oname;
1643 bfd *abfd;
1644
1645 oname = (char *) xmalloc (20);
1646 sprintf (oname, "d%06d.o", tmp_seq);
1647 tmp_seq++;
1648
1649 abfd = bfd_create (oname, parent);
1650 bfd_find_target (pe_details->object_target, abfd);
1651 bfd_make_writable (abfd);
1652
1653 bfd_set_format (abfd, bfd_object);
1654 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1655
1656 symptr = 0;
1657 symtab = (asymbol **) xmalloc (5 * sizeof (asymbol *));
1658 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1659 id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1660 id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
1661 quick_symbol (abfd, U (""), dll_symname, "_iname", id7, BSF_GLOBAL, 0);
1662
1663 bfd_set_section_size (abfd, id4, 4);
1664 d4 = (unsigned char *) xmalloc (4);
1665 id4->contents = d4;
1666 memset (d4, 0, 4);
1667
1668 bfd_set_section_size (abfd, id5, 4);
1669 d5 = (unsigned char *) xmalloc (4);
1670 id5->contents = d5;
1671 memset (d5, 0, 4);
1672
1673 len = strlen (dll_filename) + 1;
1674 if (len & 1)
1675 len++;
1676 bfd_set_section_size (abfd, id7, len);
1677 d7 = (unsigned char *) xmalloc (len);
1678 id7->contents = d7;
1679 strcpy (d7, dll_filename);
1680
1681 bfd_set_symtab (abfd, symtab, symptr);
1682
1683 bfd_set_section_contents (abfd, id4, d4, 0, 4);
1684 bfd_set_section_contents (abfd, id5, d5, 0, 4);
1685 bfd_set_section_contents (abfd, id7, d7, 0, len);
1686
1687 bfd_make_readable (abfd);
1688 return abfd;
1689 }
1690
1691 /* .text
1692 .global _function
1693 .global ___imp_function
1694 .global __imp__function
1695 _function:
1696 jmp *__imp__function:
1697
1698 .section idata$7
1699 .long __head_my_dll
1700
1701 .section .idata$5
1702 ___imp_function:
1703 __imp__function:
1704 iat?
1705 .section .idata$4
1706 iat?
1707 .section .idata$6
1708 ID<ordinal>:
1709 .short <hint>
1710 .asciz "function" xlate? (add underscore, kill at) */
1711
1712 static unsigned char jmp_ix86_bytes[] =
1713 {
1714 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90
1715 };
1716
1717 /* _function:
1718 mov.l ip+8,r0
1719 mov.l @r0,r0
1720 jmp @r0
1721 nop
1722 .dw __imp_function */
1723
1724 static unsigned char jmp_sh_bytes[] =
1725 {
1726 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00
1727 };
1728
1729 /* _function:
1730 lui $t0,<high:__imp_function>
1731 lw $t0,<low:__imp_function>
1732 jr $t0
1733 nop */
1734
1735 static unsigned char jmp_mips_bytes[] =
1736 {
1737 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d,
1738 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00
1739 };
1740
1741 static bfd *
1742 make_one (exp, parent)
1743 def_file_export *exp;
1744 bfd *parent;
1745 {
1746 asection *tx, *id7, *id5, *id4, *id6;
1747 unsigned char *td = NULL, *d7, *d5, *d4, *d6 = NULL;
1748 int len;
1749 char *oname;
1750 bfd *abfd;
1751 unsigned char *jmp_bytes = NULL;
1752 int jmp_byte_count = 0;
1753
1754 switch (pe_details->pe_arch)
1755 {
1756 case PE_ARCH_i386:
1757 jmp_bytes = jmp_ix86_bytes;
1758 jmp_byte_count = sizeof (jmp_ix86_bytes);
1759 break;
1760 case PE_ARCH_sh:
1761 jmp_bytes = jmp_sh_bytes;
1762 jmp_byte_count = sizeof (jmp_sh_bytes);
1763 break;
1764 case PE_ARCH_mips:
1765 jmp_bytes = jmp_mips_bytes;
1766 jmp_byte_count = sizeof (jmp_mips_bytes);
1767 break;
1768 default:
1769 abort ();
1770 }
1771
1772 oname = (char *) xmalloc (20);
1773 sprintf (oname, "d%06d.o", tmp_seq);
1774 tmp_seq++;
1775
1776 abfd = bfd_create (oname, parent);
1777 bfd_find_target (pe_details->object_target, abfd);
1778 bfd_make_writable (abfd);
1779
1780 bfd_set_format (abfd, bfd_object);
1781 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1782
1783 symptr = 0;
1784 symtab = (asymbol **) xmalloc (11 * sizeof (asymbol *));
1785 tx = quick_section (abfd, ".text", SEC_CODE|SEC_HAS_CONTENTS, 2);
1786 id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
1787 id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1788 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1789 id6 = quick_section (abfd, ".idata$6", SEC_HAS_CONTENTS, 2);
1790 if (! exp->flag_data)
1791 quick_symbol (abfd, U (""), exp->internal_name, "", tx, BSF_GLOBAL, 0);
1792 quick_symbol (abfd, U ("_head_"), dll_symname, "", UNDSEC, BSF_GLOBAL, 0);
1793 quick_symbol (abfd, U ("_imp__"), exp->internal_name, "", id5, BSF_GLOBAL, 0);
1794 /* Symbol to reference ord/name of imported
1795 symbol, used to implement auto-import. */
1796 quick_symbol (abfd, U("_nm__"), exp->internal_name, "", id6, BSF_GLOBAL, 0);
1797 if (pe_dll_compat_implib)
1798 quick_symbol (abfd, U ("__imp_"), exp->internal_name, "",
1799 id5, BSF_GLOBAL, 0);
1800
1801 if (! exp->flag_data)
1802 {
1803 bfd_set_section_size (abfd, tx, jmp_byte_count);
1804 td = (unsigned char *) xmalloc (jmp_byte_count);
1805 tx->contents = td;
1806 memcpy (td, jmp_bytes, jmp_byte_count);
1807
1808 switch (pe_details->pe_arch)
1809 {
1810 case PE_ARCH_i386:
1811 quick_reloc (abfd, 2, BFD_RELOC_32, 2);
1812 break;
1813 case PE_ARCH_sh:
1814 quick_reloc (abfd, 8, BFD_RELOC_32, 2);
1815 break;
1816 case PE_ARCH_mips:
1817 quick_reloc (abfd, 0, BFD_RELOC_HI16_S, 2);
1818 quick_reloc (abfd, 0, BFD_RELOC_LO16, 0); /* MIPS_R_PAIR */
1819 quick_reloc (abfd, 4, BFD_RELOC_LO16, 2);
1820 break;
1821 default:
1822 abort ();
1823 }
1824 save_relocs (tx);
1825 }
1826
1827 bfd_set_section_size (abfd, id7, 4);
1828 d7 = (unsigned char *) xmalloc (4);
1829 id7->contents = d7;
1830 memset (d7, 0, 4);
1831 quick_reloc (abfd, 0, BFD_RELOC_RVA, 6);
1832 save_relocs (id7);
1833
1834 bfd_set_section_size (abfd, id5, 4);
1835 d5 = (unsigned char *) xmalloc (4);
1836 id5->contents = d5;
1837 memset (d5, 0, 4);
1838
1839 if (exp->flag_noname)
1840 {
1841 d5[0] = exp->ordinal;
1842 d5[1] = exp->ordinal >> 8;
1843 d5[3] = 0x80;
1844 }
1845 else
1846 {
1847 quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
1848 save_relocs (id5);
1849 }
1850
1851 bfd_set_section_size (abfd, id4, 4);
1852 d4 = (unsigned char *) xmalloc (4);
1853 id4->contents = d4;
1854 memset (d4, 0, 4);
1855
1856 if (exp->flag_noname)
1857 {
1858 d4[0] = exp->ordinal;
1859 d4[1] = exp->ordinal >> 8;
1860 d4[3] = 0x80;
1861 }
1862 else
1863 {
1864 quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
1865 save_relocs (id4);
1866 }
1867
1868 if (exp->flag_noname)
1869 {
1870 len = 0;
1871 bfd_set_section_size (abfd, id6, 0);
1872 }
1873 else
1874 {
1875 len = strlen (exp->name) + 3;
1876 if (len & 1)
1877 len++;
1878 bfd_set_section_size (abfd, id6, len);
1879 d6 = (unsigned char *) xmalloc (len);
1880 id6->contents = d6;
1881 memset (d6, 0, len);
1882 d6[0] = exp->hint & 0xff;
1883 d6[1] = exp->hint >> 8;
1884 strcpy (d6 + 2, exp->name);
1885 }
1886
1887 bfd_set_symtab (abfd, symtab, symptr);
1888
1889 bfd_set_section_contents (abfd, tx, td, 0, jmp_byte_count);
1890 bfd_set_section_contents (abfd, id7, d7, 0, 4);
1891 bfd_set_section_contents (abfd, id5, d5, 0, 4);
1892 bfd_set_section_contents (abfd, id4, d4, 0, 4);
1893 if (!exp->flag_noname)
1894 bfd_set_section_contents (abfd, id6, d6, 0, len);
1895
1896 bfd_make_readable (abfd);
1897 return abfd;
1898 }
1899
1900 static bfd *
1901 make_singleton_name_thunk (import, parent)
1902 const char *import;
1903 bfd *parent;
1904 {
1905 /* Name thunks go to idata$4. */
1906 asection *id4;
1907 unsigned char *d4;
1908 char *oname;
1909 bfd *abfd;
1910
1911 oname = (char *) xmalloc (20);
1912 sprintf (oname, "nmth%06d.o", tmp_seq);
1913 tmp_seq++;
1914
1915 abfd = bfd_create (oname, parent);
1916 bfd_find_target (pe_details->object_target, abfd);
1917 bfd_make_writable (abfd);
1918
1919 bfd_set_format (abfd, bfd_object);
1920 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1921
1922 symptr = 0;
1923 symtab = (asymbol **) xmalloc (3 * sizeof (asymbol *));
1924 id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1925 quick_symbol (abfd, U ("_nm_thnk_"), import, "", id4, BSF_GLOBAL, 0);
1926 quick_symbol (abfd, U ("_nm_"), import, "", UNDSEC, BSF_GLOBAL, 0);
1927
1928 bfd_set_section_size (abfd, id4, 8);
1929 d4 = (unsigned char *) xmalloc (4);
1930 id4->contents = d4;
1931 memset (d4, 0, 8);
1932 quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
1933 save_relocs (id4);
1934
1935 bfd_set_symtab (abfd, symtab, symptr);
1936
1937 bfd_set_section_contents (abfd, id4, d4, 0, 8);
1938
1939 bfd_make_readable (abfd);
1940 return abfd;
1941 }
1942
1943 static char *
1944 make_import_fixup_mark (rel)
1945 arelent *rel;
1946 {
1947 /* We convert reloc to symbol, for later reference. */
1948 static int counter;
1949 static char *fixup_name = NULL;
1950 static size_t buffer_len = 0;
1951
1952 struct symbol_cache_entry *sym = *rel->sym_ptr_ptr;
1953
1954 bfd *abfd = bfd_asymbol_bfd (sym);
1955 struct coff_link_hash_entry *myh = NULL;
1956
1957 if (!fixup_name)
1958 {
1959 fixup_name = (char *) xmalloc (384);
1960 buffer_len = 384;
1961 }
1962
1963 if (strlen (sym->name) + 25 > buffer_len)
1964 /* Assume 25 chars for "__fu" + counter + "_". If counter is
1965 bigger than 20 digits long, we've got worse problems than
1966 overflowing this buffer... */
1967 {
1968 free (fixup_name);
1969 /* New buffer size is length of symbol, plus 25, but then
1970 rounded up to the nearest multiple of 128. */
1971 buffer_len = ((strlen (sym->name) + 25) + 127) & ~127;
1972 fixup_name = (char *) xmalloc (buffer_len);
1973 }
1974
1975 sprintf (fixup_name, "__fu%d_%s", counter++, sym->name);
1976
1977 bfd_coff_link_add_one_symbol (&link_info, abfd, fixup_name, BSF_GLOBAL,
1978 current_sec, /* sym->section, */
1979 rel->address, NULL, true, false,
1980 (struct bfd_link_hash_entry **) &myh);
1981
1982 #if 0
1983 printf ("type:%d\n", myh->type);
1984 printf ("%s\n", myh->root.u.def.section->name);
1985 #endif
1986 return fixup_name;
1987 }
1988
1989 /* .section .idata$3
1990 .rva __nm_thnk_SYM (singleton thunk with name of func)
1991 .long 0
1992 .long 0
1993 .rva __my_dll_iname (name of dll)
1994 .rva __fuNN_SYM (pointer to reference (address) in text) */
1995
1996 static bfd *
1997 make_import_fixup_entry (name, fixup_name, dll_symname,parent)
1998 const char *name;
1999 const char *fixup_name;
2000 const char *dll_symname;
2001 bfd *parent;
2002 {
2003 asection *id3;
2004 unsigned char *d3;
2005 char *oname;
2006 bfd *abfd;
2007
2008 oname = (char *) xmalloc (20);
2009 sprintf (oname, "fu%06d.o", tmp_seq);
2010 tmp_seq++;
2011
2012 abfd = bfd_create (oname, parent);
2013 bfd_find_target (pe_details->object_target, abfd);
2014 bfd_make_writable (abfd);
2015
2016 bfd_set_format (abfd, bfd_object);
2017 bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
2018
2019 symptr = 0;
2020 symtab = (asymbol **) xmalloc (6 * sizeof (asymbol *));
2021 id3 = quick_section (abfd, ".idata$3", SEC_HAS_CONTENTS, 2);
2022
2023 #if 0
2024 quick_symbol (abfd, U ("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0);
2025 #endif
2026 quick_symbol (abfd, U ("_nm_thnk_"), name, "", UNDSEC, BSF_GLOBAL, 0);
2027 quick_symbol (abfd, U (""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
2028 quick_symbol (abfd, "", fixup_name, "", UNDSEC, BSF_GLOBAL, 0);
2029
2030 bfd_set_section_size (abfd, id3, 20);
2031 d3 = (unsigned char *) xmalloc (20);
2032 id3->contents = d3;
2033 memset (d3, 0, 20);
2034
2035 quick_reloc (abfd, 0, BFD_RELOC_RVA, 1);
2036 quick_reloc (abfd, 12, BFD_RELOC_RVA, 2);
2037 quick_reloc (abfd, 16, BFD_RELOC_RVA, 3);
2038 save_relocs (id3);
2039
2040 bfd_set_symtab (abfd, symtab, symptr);
2041
2042 bfd_set_section_contents (abfd, id3, d3, 0, 20);
2043
2044 bfd_make_readable (abfd);
2045 return abfd;
2046 }
2047
2048 void
2049 pe_create_import_fixup (rel)
2050 arelent *rel;
2051 {
2052 char buf[300];
2053 struct symbol_cache_entry *sym = *rel->sym_ptr_ptr;
2054 struct bfd_link_hash_entry *name_thunk_sym;
2055 const char *name = sym->name;
2056 char *fixup_name = make_import_fixup_mark (rel);
2057
2058 sprintf (buf, U ("_nm_thnk_%s"), name);
2059
2060 name_thunk_sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
2061
2062 if (!name_thunk_sym || name_thunk_sym->type != bfd_link_hash_defined)
2063 {
2064 bfd *b = make_singleton_name_thunk (name, output_bfd);
2065 add_bfd_to_link (b, b->filename, &link_info);
2066
2067 /* If we ever use autoimport, we have to cast text section writable. */
2068 config.text_read_only = false;
2069 }
2070
2071 {
2072 extern char * pe_data_import_dll;
2073 char * dll_symname = pe_data_import_dll ? pe_data_import_dll : "unknown";
2074
2075 bfd *b = make_import_fixup_entry (name, fixup_name, dll_symname,
2076 output_bfd);
2077 add_bfd_to_link (b, b->filename, &link_info);
2078 }
2079 }
2080
2081
2082 void
2083 pe_dll_generate_implib (def, impfilename)
2084 def_file *def;
2085 const char *impfilename;
2086 {
2087 int i;
2088 bfd *ar_head;
2089 bfd *ar_tail;
2090 bfd *outarch;
2091 bfd *head = 0;
2092
2093 dll_filename = (def->name) ? def->name : dll_name;
2094 dll_symname = xstrdup (dll_filename);
2095 for (i = 0; dll_symname[i]; i++)
2096 if (!ISALNUM (dll_symname[i]))
2097 dll_symname[i] = '_';
2098
2099 unlink (impfilename);
2100
2101 outarch = bfd_openw (impfilename, 0);
2102
2103 if (!outarch)
2104 {
2105 /* xgettext:c-format */
2106 einfo (_("%XCan't open .lib file: %s\n"), impfilename);
2107 return;
2108 }
2109
2110 /* xgettext:c-format */
2111 einfo (_("Creating library file: %s\n"), impfilename);
2112
2113 bfd_set_format (outarch, bfd_archive);
2114 outarch->has_armap = 1;
2115
2116 /* Work out a reasonable size of things to put onto one line. */
2117 ar_head = make_head (outarch);
2118
2119 for (i = 0; i < def->num_exports; i++)
2120 {
2121 /* The import library doesn't know about the internal name. */
2122 char *internal = def->exports[i].internal_name;
2123 bfd *n;
2124
2125 def->exports[i].internal_name = def->exports[i].name;
2126 n = make_one (def->exports + i, outarch);
2127 n->next = head;
2128 head = n;
2129 def->exports[i].internal_name = internal;
2130 }
2131
2132 ar_tail = make_tail (outarch);
2133
2134 if (ar_head == NULL || ar_tail == NULL)
2135 return;
2136
2137 /* Now stick them all into the archive. */
2138 ar_head->next = head;
2139 ar_tail->next = ar_head;
2140 head = ar_tail;
2141
2142 if (! bfd_set_archive_head (outarch, head))
2143 einfo ("%Xbfd_set_archive_head: %s\n", bfd_errmsg (bfd_get_error ()));
2144
2145 if (! bfd_close (outarch))
2146 einfo ("%Xbfd_close %s: %s\n", impfilename, bfd_errmsg (bfd_get_error ()));
2147
2148 while (head != NULL)
2149 {
2150 bfd *n = head->next;
2151 bfd_close (head);
2152 head = n;
2153 }
2154 }
2155
2156 static void
2157 add_bfd_to_link (abfd, name, link_info)
2158 bfd *abfd;
2159 const char *name;
2160 struct bfd_link_info *link_info;
2161 {
2162 lang_input_statement_type *fake_file;
2163
2164 fake_file = lang_add_input_file (name,
2165 lang_input_file_is_fake_enum,
2166 NULL);
2167 fake_file->the_bfd = abfd;
2168 ldlang_add_file (fake_file);
2169
2170 if (!bfd_link_add_symbols (abfd, link_info))
2171 einfo ("%Xaddsym %s: %s\n", name, bfd_errmsg (bfd_get_error ()));
2172 }
2173
2174 void
2175 pe_process_import_defs (output_bfd, link_info)
2176 bfd *output_bfd;
2177 struct bfd_link_info *link_info;
2178 {
2179 def_file_module *module;
2180
2181 pe_dll_id_target (bfd_get_target (output_bfd));
2182
2183 if (!pe_def_file)
2184 return;
2185
2186 for (module = pe_def_file->modules; module; module = module->next)
2187 {
2188 int i, do_this_dll;
2189
2190 dll_filename = module->name;
2191 dll_symname = xstrdup (module->name);
2192 for (i = 0; dll_symname[i]; i++)
2193 if (!ISALNUM (dll_symname[i]))
2194 dll_symname[i] = '_';
2195
2196 do_this_dll = 0;
2197
2198 for (i = 0; i < pe_def_file->num_imports; i++)
2199 if (pe_def_file->imports[i].module == module)
2200 {
2201 def_file_export exp;
2202 struct bfd_link_hash_entry *blhe;
2203
2204 /* See if we need this import. */
2205 char *name = (char *) xmalloc (strlen (pe_def_file->imports[i].internal_name) + 2 + 6);
2206 sprintf (name, "%s%s", U (""), pe_def_file->imports[i].internal_name);
2207 blhe = bfd_link_hash_lookup (link_info->hash, name,
2208 false, false, false);
2209 if (!blhe || (blhe && blhe->type != bfd_link_hash_undefined))
2210 {
2211 sprintf (name, "%s%s", U ("_imp__"),
2212 pe_def_file->imports[i].internal_name);
2213 blhe = bfd_link_hash_lookup (link_info->hash, name,
2214 false, false, false);
2215 }
2216 free (name);
2217 if (blhe && blhe->type == bfd_link_hash_undefined)
2218 {
2219 bfd *one;
2220 /* We do. */
2221 if (!do_this_dll)
2222 {
2223 bfd *ar_head = make_head (output_bfd);
2224 add_bfd_to_link (ar_head, ar_head->filename, link_info);
2225 do_this_dll = 1;
2226 }
2227 exp.internal_name = pe_def_file->imports[i].internal_name;
2228 exp.name = pe_def_file->imports[i].name;
2229 exp.ordinal = pe_def_file->imports[i].ordinal;
2230 exp.hint = exp.ordinal >= 0 ? exp.ordinal : 0;
2231 exp.flag_private = 0;
2232 exp.flag_constant = 0;
2233 exp.flag_data = 0;
2234 exp.flag_noname = exp.name ? 0 : 1;
2235 one = make_one (&exp, output_bfd);
2236 add_bfd_to_link (one, one->filename, link_info);
2237 }
2238 }
2239 if (do_this_dll)
2240 {
2241 bfd *ar_tail = make_tail (output_bfd);
2242 add_bfd_to_link (ar_tail, ar_tail->filename, link_info);
2243 }
2244
2245 free (dll_symname);
2246 }
2247 }
2248
2249 /* We were handed a *.DLL file. Parse it and turn it into a set of
2250 IMPORTS directives in the def file. Return true if the file was
2251 handled, false if not. */
2252
2253 static unsigned int
2254 pe_get16 (abfd, where)
2255 bfd *abfd;
2256 int where;
2257 {
2258 unsigned char b[2];
2259
2260 bfd_seek (abfd, (file_ptr) where, SEEK_SET);
2261 bfd_bread (b, (bfd_size_type) 2, abfd);
2262 return b[0] + (b[1] << 8);
2263 }
2264
2265 static unsigned int
2266 pe_get32 (abfd, where)
2267 bfd *abfd;
2268 int where;
2269 {
2270 unsigned char b[4];
2271
2272 bfd_seek (abfd, (file_ptr) where, SEEK_SET);
2273 bfd_bread (b, (bfd_size_type) 4, abfd);
2274 return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
2275 }
2276
2277 #if 0 /* This is not currently used. */
2278
2279 static unsigned int
2280 pe_as16 (ptr)
2281 void *ptr;
2282 {
2283 unsigned char *b = ptr;
2284
2285 return b[0] + (b[1] << 8);
2286 }
2287
2288 #endif
2289
2290 static unsigned int
2291 pe_as32 (ptr)
2292 void *ptr;
2293 {
2294 unsigned char *b = ptr;
2295
2296 return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
2297 }
2298
2299 boolean
2300 pe_implied_import_dll (filename)
2301 const char *filename;
2302 {
2303 bfd *dll;
2304 unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
2305 unsigned long export_rva, export_size, nsections, secptr, expptr;
2306 unsigned char *expdata, *erva;
2307 unsigned long name_rvas, ordinals, nexp, ordbase;
2308 const char *dll_name;
2309
2310 /* No, I can't use bfd here. kernel32.dll puts its export table in
2311 the middle of the .rdata section. */
2312 dll = bfd_openr (filename, pe_details->target_name);
2313 if (!dll)
2314 {
2315 einfo ("%Xopen %s: %s\n", filename, bfd_errmsg (bfd_get_error ()));
2316 return false;
2317 }
2318
2319 /* PEI dlls seem to be bfd_objects. */
2320 if (!bfd_check_format (dll, bfd_object))
2321 {
2322 einfo ("%X%s: this doesn't appear to be a DLL\n", filename);
2323 return false;
2324 }
2325
2326 dll_name = filename;
2327 for (i = 0; filename[i]; i++)
2328 if (filename[i] == '/' || filename[i] == '\\' || filename[i] == ':')
2329 dll_name = filename + i + 1;
2330
2331 pe_header_offset = pe_get32 (dll, 0x3c);
2332 opthdr_ofs = pe_header_offset + 4 + 20;
2333 num_entries = pe_get32 (dll, opthdr_ofs + 92);
2334
2335 if (num_entries < 1) /* No exports. */
2336 return false;
2337
2338 export_rva = pe_get32 (dll, opthdr_ofs + 96);
2339 export_size = pe_get32 (dll, opthdr_ofs + 100);
2340 nsections = pe_get16 (dll, pe_header_offset + 4 + 2);
2341 secptr = (pe_header_offset + 4 + 20 +
2342 pe_get16 (dll, pe_header_offset + 4 + 16));
2343 expptr = 0;
2344
2345 for (i = 0; i < nsections; i++)
2346 {
2347 char sname[8];
2348 unsigned long secptr1 = secptr + 40 * i;
2349 unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
2350 unsigned long vsize = pe_get32 (dll, secptr1 + 16);
2351 unsigned long fptr = pe_get32 (dll, secptr1 + 20);
2352
2353 bfd_seek (dll, (file_ptr) secptr1, SEEK_SET);
2354 bfd_bread (sname, (bfd_size_type) 8, dll);
2355
2356 if (vaddr <= export_rva && vaddr + vsize > export_rva)
2357 {
2358 expptr = fptr + (export_rva - vaddr);
2359 if (export_rva + export_size > vaddr + vsize)
2360 export_size = vsize - (export_rva - vaddr);
2361 break;
2362 }
2363 }
2364
2365 expdata = (unsigned char *) xmalloc (export_size);
2366 bfd_seek (dll, (file_ptr) expptr, SEEK_SET);
2367 bfd_bread (expdata, (bfd_size_type) export_size, dll);
2368 erva = expdata - export_rva;
2369
2370 if (pe_def_file == 0)
2371 pe_def_file = def_file_empty ();
2372
2373 nexp = pe_as32 (expdata + 24);
2374 name_rvas = pe_as32 (expdata + 32);
2375 ordinals = pe_as32 (expdata + 36);
2376 ordbase = pe_as32 (expdata + 16);
2377
2378 for (i = 0; i < nexp; i++)
2379 {
2380 unsigned long name_rva = pe_as32 (erva + name_rvas + i * 4);
2381 def_file_import *imp;
2382
2383 imp = def_file_add_import (pe_def_file, erva + name_rva, dll_name,
2384 i, 0);
2385 }
2386
2387 return true;
2388 }
2389
2390 /* These are the main functions, called from the emulation. The first
2391 is called after the bfds are read, so we can guess at how much space
2392 we need. The second is called after everything is placed, so we
2393 can put the right values in place. */
2394
2395 void
2396 pe_dll_build_sections (abfd, info)
2397 bfd *abfd;
2398 struct bfd_link_info *info;
2399 {
2400 pe_dll_id_target (bfd_get_target (abfd));
2401 process_def_file (abfd, info);
2402
2403 generate_edata (abfd, info);
2404 build_filler_bfd (1);
2405 }
2406
2407 void
2408 pe_exe_build_sections (abfd, info)
2409 bfd *abfd;
2410 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2411 {
2412 pe_dll_id_target (bfd_get_target (abfd));
2413 build_filler_bfd (0);
2414 }
2415
2416 void
2417 pe_dll_fill_sections (abfd, info)
2418 bfd *abfd;
2419 struct bfd_link_info *info;
2420 {
2421 pe_dll_id_target (bfd_get_target (abfd));
2422 image_base = pe_data (abfd)->pe_opthdr.ImageBase;
2423
2424 generate_reloc (abfd, info);
2425 if (reloc_sz > 0)
2426 {
2427 bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
2428
2429 /* Resize the sections. */
2430 lang_size_sections (stat_ptr->head, abs_output_section,
2431 &stat_ptr->head, 0, (bfd_vma) 0, NULL);
2432
2433 /* Redo special stuff. */
2434 ldemul_after_allocation ();
2435
2436 /* Do the assignments again. */
2437 lang_do_assignments (stat_ptr->head,
2438 abs_output_section,
2439 (fill_type) 0, (bfd_vma) 0);
2440 }
2441
2442 fill_edata (abfd, info);
2443
2444 pe_data (abfd)->dll = 1;
2445
2446 edata_s->contents = edata_d;
2447 reloc_s->contents = reloc_d;
2448 }
2449
2450 void
2451 pe_exe_fill_sections (abfd, info)
2452 bfd *abfd;
2453 struct bfd_link_info *info;
2454 {
2455 pe_dll_id_target (bfd_get_target (abfd));
2456 image_base = pe_data (abfd)->pe_opthdr.ImageBase;
2457
2458 generate_reloc (abfd, info);
2459 if (reloc_sz > 0)
2460 {
2461 bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
2462
2463 /* Resize the sections. */
2464 lang_size_sections (stat_ptr->head, abs_output_section,
2465 &stat_ptr->head, 0, (bfd_vma) 0, NULL);
2466
2467 /* Redo special stuff. */
2468 ldemul_after_allocation ();
2469
2470 /* Do the assignments again. */
2471 lang_do_assignments (stat_ptr->head,
2472 abs_output_section,
2473 (fill_type) 0, (bfd_vma) 0);
2474 }
2475 reloc_s->contents = reloc_d;
2476 }
This page took 0.098509 seconds and 4 git commands to generate.