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