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