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