7b213b1e8ad413e143a61683d6d033874372bd72
[binutils-gdb.git] / ld / emultempl / pe.em
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 (echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
4 cat >>e${EMULATION_NAME}.c <<EOF
5 /* This file is part of GLD, the Gnu Linker.
6 Copyright 1995, 96, 97, 1998 Free Software Foundation, Inc.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 /* For WINDOWS_NT */
23 /* The original file generated returned different default scripts depending
24 on whether certain switches were set, but these switches pertain to the
25 Linux system and that particular version of coff. In the NT case, we
26 only determine if the subsystem is console or windows in order to select
27 the correct entry point by default. */
28
29 #include "bfd.h"
30 #include "sysdep.h"
31 #include "bfdlink.h"
32 #include "getopt.h"
33 #include "libiberty.h"
34 #include "ld.h"
35 #include "ldmain.h"
36 #include "ldgram.h"
37 #include "ldexp.h"
38 #include "ldlang.h"
39 #include "ldemul.h"
40 #include "ldlex.h"
41 #include "ldmisc.h"
42 #include "ldctor.h"
43 #include "ldfile.h"
44 #include "coff/internal.h"
45 #include "../bfd/libcoff.h"
46 #include "../bfd/libbfd.h"
47 #include "deffile.h"
48
49 #define TARGET_IS_${EMULATION_NAME}
50
51 #if defined(TARGET_IS_i386pe)
52 #define DLL_SUPPORT
53 #endif
54
55 #define PE_DEF_SUBSYSTEM 3
56
57 #ifdef TARGET_IS_arm_epoc_pe
58 #define bfd_arm_pe_allocate_interworking_sections \
59 bfd_arm_epoc_pe_allocate_interworking_sections
60 #define bfd_arm_pe_get_bfd_for_interworking \
61 bfd_arm_epoc_pe_get_bfd_for_interworking
62 #define bfd_arm_pe_process_before_allocation \
63 bfd_arm_epoc_pe_process_before_allocation
64 #endif
65
66 static void gld_${EMULATION_NAME}_set_symbols PARAMS ((void));
67 static void gld_${EMULATION_NAME}_after_open PARAMS ((void));
68 static void gld_${EMULATION_NAME}_before_parse PARAMS ((void));
69 static void gld_${EMULATION_NAME}_after_parse PARAMS ((void));
70 static void gld_${EMULATION_NAME}_before_allocation PARAMS ((void));
71 static boolean gld_${EMULATION_NAME}_place_orphan
72 PARAMS ((lang_input_statement_type *, asection *));
73 static void gld${EMULATION_NAME}_place_section
74 PARAMS ((lang_statement_union_type *));
75 static char *gld_${EMULATION_NAME}_get_script PARAMS ((int *));
76 static int gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **));
77
78 static struct internal_extra_pe_aouthdr pe;
79 static int dll;
80 static int support_old_code = 0;
81 extern def_file *pe_def_file;
82 static lang_assignment_statement_type *image_base_statement = 0;
83
84 static char *pe_out_def_filename = 0;
85 extern int pe_dll_export_everything;
86 extern int pe_dll_kill_ats;
87 extern int pe_dll_stdcall_aliases;
88 static int pe_enable_stdcall_fixup = -1; /* 0=disable 1=enable */
89 static char *pe_implib_filename = 0;
90
91 extern const char *output_filename;
92
93 static void
94 gld_${EMULATION_NAME}_before_parse()
95 {
96 output_filename = "a.exe";
97 ldfile_output_architecture = bfd_arch_${ARCH};
98 #ifdef DLL_SUPPORT
99 config.has_shared = 1;
100 #endif
101 }
102 \f
103 /* PE format extra command line options. */
104
105 /* Used for setting flags in the PE header. */
106 #define OPTION_BASE_FILE (300 + 1)
107 #define OPTION_DLL (OPTION_BASE_FILE + 1)
108 #define OPTION_FILE_ALIGNMENT (OPTION_DLL + 1)
109 #define OPTION_IMAGE_BASE (OPTION_FILE_ALIGNMENT + 1)
110 #define OPTION_MAJOR_IMAGE_VERSION (OPTION_IMAGE_BASE + 1)
111 #define OPTION_MAJOR_OS_VERSION (OPTION_MAJOR_IMAGE_VERSION + 1)
112 #define OPTION_MAJOR_SUBSYSTEM_VERSION (OPTION_MAJOR_OS_VERSION + 1)
113 #define OPTION_MINOR_IMAGE_VERSION (OPTION_MAJOR_SUBSYSTEM_VERSION + 1)
114 #define OPTION_MINOR_OS_VERSION (OPTION_MINOR_IMAGE_VERSION + 1)
115 #define OPTION_MINOR_SUBSYSTEM_VERSION (OPTION_MINOR_OS_VERSION + 1)
116 #define OPTION_SECTION_ALIGNMENT (OPTION_MINOR_SUBSYSTEM_VERSION + 1)
117 #define OPTION_STACK (OPTION_SECTION_ALIGNMENT + 1)
118 #define OPTION_SUBSYSTEM (OPTION_STACK + 1)
119 #define OPTION_HEAP (OPTION_SUBSYSTEM + 1)
120 #define OPTION_SUPPORT_OLD_CODE (OPTION_HEAP + 1)
121 #define OPTION_OUT_DEF (OPTION_SUPPORT_OLD_CODE + 1)
122 #define OPTION_EXPORT_ALL (OPTION_OUT_DEF + 1)
123 #define OPTION_EXCLUDE_SYMBOLS (OPTION_EXPORT_ALL + 1)
124 #define OPTION_KILL_ATS (OPTION_EXCLUDE_SYMBOLS + 1)
125 #define OPTION_STDCALL_ALIASES (OPTION_KILL_ATS + 1)
126 #define OPTION_ENABLE_STDCALL_FIXUP (OPTION_STDCALL_ALIASES + 1)
127 #define OPTION_DISABLE_STDCALL_FIXUP (OPTION_ENABLE_STDCALL_FIXUP + 1)
128 #define OPTION_IMPLIB_FILENAME (OPTION_DISABLE_STDCALL_FIXUP + 1)
129
130 static struct option longopts[] =
131 {
132 /* PE options */
133 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
134 {"dll", no_argument, NULL, OPTION_DLL},
135 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
136 {"heap", required_argument, NULL, OPTION_HEAP},
137 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
138 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
139 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
140 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
141 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
142 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
143 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
144 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
145 {"stack", required_argument, NULL, OPTION_STACK},
146 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
147 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
148 #ifdef DLL_SUPPORT
149 /* getopt allows abbreviations, so we do this to stop it from treating -o
150 as an abbreviation for this option */
151 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
152 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
153 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
154 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
155 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
156 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
157 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
158 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
159 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
160 #endif
161 {NULL, no_argument, NULL, 0}
162 };
163
164
165 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
166 parameters which may be input from the command line */
167
168 typedef struct
169 {
170 void *ptr;
171 int size;
172 int value;
173 char *symbol;
174 int inited;
175 } definfo;
176
177 #define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol,0}
178
179 static definfo init[] =
180 {
181 /* imagebase must be first */
182 #define IMAGEBASEOFF 0
183 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
184 #define DLLOFF 1
185 {&dll, sizeof(dll), 0, "__dll__"},
186 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
187 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
188 D(MajorOperatingSystemVersion,"__major_os_version__", 4),
189 D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
190 D(MajorImageVersion,"__major_image_version__", 1),
191 D(MinorImageVersion,"__minor_image_version__", 0),
192 D(MajorSubsystemVersion,"__major_subsystem_version__", 4),
193 D(MinorSubsystemVersion,"__minor_subsystem_version__", 0),
194 D(Subsystem,"__subsystem__", PE_DEF_SUBSYSTEM),
195 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x2000000),
196 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
197 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
198 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
199 D(LoaderFlags,"__loader_flags__", 0x0),
200 { NULL, 0, 0, NULL, 0 }
201 };
202
203 static void
204 gld_${EMULATION_NAME}_list_options (file)
205 FILE * file;
206 {
207 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
208 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
209 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
210 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
211 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
212 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
213 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
214 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
215 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
216 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
217 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
218 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
219 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
220 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
221 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
222 #ifdef DLL_SUPPORT
223 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
224 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
225 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
226 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
227 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
228 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
229 fprintf (file, _(" --out-implib <file> Generate import library\n"));
230 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
231 #endif
232 }
233
234 static void
235 set_pe_name (name, val)
236 char *name;
237 long val;
238 {
239 int i;
240 /* Find the name and set it. */
241 for (i = 0; init[i].ptr; i++)
242 {
243 if (strcmp (name, init[i].symbol) == 0)
244 {
245 init[i].value = val;
246 init[i].inited = 1;
247 return;
248 }
249 }
250 abort();
251 }
252
253
254 static void
255 set_pe_subsystem ()
256 {
257 const char *sver;
258 int len;
259 int i;
260 static const struct
261 {
262 const char *name;
263 const int value;
264 const char *entry;
265 }
266 v[] =
267 {
268 { "native", 1, "_NtProcessStartup" },
269 { "windows", 2, "_WinMainCRTStartup" },
270 { "console", 3, "_mainCRTStartup" },
271 #if 0
272 /* The Microsoft linker does not recognize this. */
273 { "os2", 5, "" },
274 #endif
275 { "posix", 7, "___PosixProcessStartup"},
276 { 0, 0, 0 }
277 };
278
279 sver = strchr (optarg, ':');
280 if (sver == NULL)
281 len = strlen (optarg);
282 else
283 {
284 char *end;
285
286 len = sver - optarg;
287 set_pe_name ("__major_subsystem_version__",
288 strtoul (sver + 1, &end, 0));
289 if (*end == '.')
290 set_pe_name ("__minor_subsystem_version__",
291 strtoul (end + 1, &end, 0));
292 if (*end != '\0')
293 einfo (_("%P: warning: bad version number in -subsystem option\n"));
294 }
295
296 for (i = 0; v[i].name; i++)
297 {
298 if (strncmp (optarg, v[i].name, len) == 0
299 && v[i].name[len] == '\0')
300 {
301 set_pe_name ("__subsystem__", v[i].value);
302
303 lang_add_entry (v[i].entry, 1);
304
305 return;
306 }
307 }
308
309 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
310 }
311
312
313
314 static void
315 set_pe_value (name)
316 char *name;
317
318 {
319 char *end;
320
321 set_pe_name (name, strtoul (optarg, &end, 0));
322
323 if (end == optarg)
324 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
325
326 optarg = end;
327 }
328
329 static void
330 set_pe_stack_heap (resname, comname)
331 char *resname;
332 char *comname;
333 {
334 set_pe_value (resname);
335
336 if (*optarg == ',')
337 {
338 optarg++;
339 set_pe_value (comname);
340 }
341 else if (*optarg)
342 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
343 }
344
345
346
347 static int
348 gld_${EMULATION_NAME}_parse_args(argc, argv)
349 int argc;
350 char **argv;
351 {
352 int longind;
353 int optc;
354 int prevoptind = optind;
355 int prevopterr = opterr;
356 int wanterror;
357 static int lastoptind = -1;
358
359 if (lastoptind != optind)
360 opterr = 0;
361 wanterror = opterr;
362
363 lastoptind = optind;
364
365 optc = getopt_long_only (argc, argv, "-", longopts, &longind);
366 opterr = prevopterr;
367
368 switch (optc)
369 {
370 default:
371 if (wanterror)
372 xexit (1);
373 optind = prevoptind;
374 return 0;
375
376 case OPTION_BASE_FILE:
377 link_info.base_file = (PTR) fopen (optarg, FOPEN_WB);
378 if (link_info.base_file == NULL)
379 {
380 /* xgettext:c-format */
381 fprintf (stderr, _("%s: Can't open base file %s\n"),
382 program_name, optarg);
383 xexit (1);
384 }
385 break;
386
387 /* PE options */
388 case OPTION_HEAP:
389 set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
390 break;
391 case OPTION_STACK:
392 set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
393 break;
394 case OPTION_SUBSYSTEM:
395 set_pe_subsystem ();
396 break;
397 case OPTION_MAJOR_OS_VERSION:
398 set_pe_value ("__major_os_version__");
399 break;
400 case OPTION_MINOR_OS_VERSION:
401 set_pe_value ("__minor_os_version__");
402 break;
403 case OPTION_MAJOR_SUBSYSTEM_VERSION:
404 set_pe_value ("__major_subsystem_version__");
405 break;
406 case OPTION_MINOR_SUBSYSTEM_VERSION:
407 set_pe_value ("__minor_subsystem_version__");
408 break;
409 case OPTION_MAJOR_IMAGE_VERSION:
410 set_pe_value ("__major_image_version__");
411 break;
412 case OPTION_MINOR_IMAGE_VERSION:
413 set_pe_value ("__minor_image_version__");
414 break;
415 case OPTION_FILE_ALIGNMENT:
416 set_pe_value ("__file_alignment__");
417 break;
418 case OPTION_SECTION_ALIGNMENT:
419 set_pe_value ("__section_alignment__");
420 break;
421 case OPTION_DLL:
422 set_pe_name ("__dll__", 1);
423 break;
424 case OPTION_IMAGE_BASE:
425 set_pe_value ("__image_base__");
426 break;
427 case OPTION_SUPPORT_OLD_CODE:
428 support_old_code = 1;
429 break;
430 #ifdef DLL_SUPPORT
431 case OPTION_OUT_DEF:
432 pe_out_def_filename = xstrdup (optarg);
433 break;
434 case OPTION_EXPORT_ALL:
435 pe_dll_export_everything = 1;
436 break;
437 case OPTION_EXCLUDE_SYMBOLS:
438 pe_dll_add_excludes (optarg);
439 break;
440 case OPTION_KILL_ATS:
441 pe_dll_kill_ats = 1;
442 break;
443 case OPTION_STDCALL_ALIASES:
444 pe_dll_stdcall_aliases = 1;
445 break;
446 case OPTION_ENABLE_STDCALL_FIXUP:
447 pe_enable_stdcall_fixup = 1;
448 break;
449 case OPTION_DISABLE_STDCALL_FIXUP:
450 pe_enable_stdcall_fixup = 0;
451 break;
452 case OPTION_IMPLIB_FILENAME:
453 pe_implib_filename = xstrdup (optarg);
454 break;
455 #endif
456 }
457 return 1;
458 }
459 \f
460 /* Assign values to the special symbols before the linker script is
461 read. */
462
463 static void
464 gld_${EMULATION_NAME}_set_symbols ()
465 {
466 /* Run through and invent symbols for all the
467 names and insert the defaults. */
468 int j;
469 lang_statement_list_type *save;
470
471 if (!init[IMAGEBASEOFF].inited)
472 {
473 if (link_info.relocateable)
474 init[IMAGEBASEOFF].value = 0;
475 else if (init[DLLOFF].value || link_info.shared)
476 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
477 else
478 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
479 }
480
481 /* Don't do any symbol assignments if this is a relocateable link. */
482 if (link_info.relocateable)
483 return;
484
485 /* Glue the assignments into the abs section */
486 save = stat_ptr;
487
488 stat_ptr = &(abs_output_section->children);
489
490 for (j = 0; init[j].ptr; j++)
491 {
492 long val = init[j].value;
493 lang_assignment_statement_type *rv;
494 rv = lang_add_assignment (exp_assop ('=' ,init[j].symbol, exp_intop (val)));
495 if (init[j].size == sizeof(short))
496 *(short *)init[j].ptr = val;
497 else if (init[j].size == sizeof(int))
498 *(int *)init[j].ptr = val;
499 else if (init[j].size == sizeof(long))
500 *(long *)init[j].ptr = val;
501 /* This might be a long long or other special type. */
502 else if (init[j].size == sizeof(bfd_vma))
503 *(bfd_vma *)init[j].ptr = val;
504 else abort();
505 if (j == IMAGEBASEOFF)
506 image_base_statement = rv;
507 }
508 /* Restore the pointer. */
509 stat_ptr = save;
510
511 if (pe.FileAlignment >
512 pe.SectionAlignment)
513 {
514 einfo (_("%P: warning, file alignment > section alignment.\n"));
515 }
516 }
517
518 /* This is called after the linker script and the command line options
519 have been read. */
520
521 static void
522 gld_${EMULATION_NAME}_after_parse ()
523 {
524 /* The Windows libraries are designed for the linker to treat the
525 entry point as an undefined symbol. Otherwise, the .obj that
526 defines mainCRTStartup is brought in because it is the first
527 encountered in libc.lib and it has other symbols in it which will
528 be pulled in by the link process. To avoid this, we act as
529 though the user specified -u with the entry point symbol.
530
531 This function is called after the linker script and command line
532 options have been read, so at this point we know the right entry
533 point. This function is called before the input files are
534 opened, so registering the symbol as undefined will make a
535 difference. */
536
537 if (entry_symbol)
538 ldlang_add_undef (entry_symbol);
539 }
540
541 static struct bfd_link_hash_entry *pe_undef_found_sym;
542
543 static boolean
544 pe_undef_cdecl_match (h, string)
545 struct bfd_link_hash_entry *h;
546 PTR string;
547 {
548 int sl = strlen (string);
549 if (h->type == bfd_link_hash_defined
550 && strncmp (h->root.string, string, sl) == 0
551 && h->root.string[sl] == '@')
552 {
553 pe_undef_found_sym = h;
554 return false;
555 }
556 return true;
557 }
558
559 static void
560 pe_fixup_stdcalls ()
561 {
562 static int gave_warning_message = 0;
563 struct bfd_link_hash_entry *undef, *sym;
564 char *at;
565 for (undef = link_info.hash->undefs; undef; undef=undef->next)
566 if (undef->type == bfd_link_hash_undefined)
567 {
568 at = strchr (undef->root.string, '@');
569 if (at)
570 {
571 /* The symbol is a stdcall symbol, so let's look for a cdecl
572 symbol with the same name and resolve to that */
573 char *cname = xstrdup (undef->root.string);
574 at = strchr (cname, '@');
575 *at = 0;
576 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
577 if (sym && sym->type == bfd_link_hash_defined)
578 {
579 undef->type = bfd_link_hash_defined;
580 undef->u.def.value = sym->u.def.value;
581 undef->u.def.section = sym->u.def.section;
582 if (pe_enable_stdcall_fixup == -1)
583 {
584 einfo (_("Warning: resolving %s by linking to %s\n"),
585 undef->root.string, cname);
586 if (! gave_warning_message)
587 {
588 gave_warning_message = 1;
589 einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
590 einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
591 }
592 }
593 }
594 }
595 else
596 {
597 /* The symbol is a cdecl symbol, so we look for stdcall
598 symbols - which means scanning the whole symbol table */
599 pe_undef_found_sym = 0;
600 bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
601 (PTR) undef->root.string);
602 sym = pe_undef_found_sym;
603 if (sym)
604 {
605 undef->type = bfd_link_hash_defined;
606 undef->u.def.value = sym->u.def.value;
607 undef->u.def.section = sym->u.def.section;
608 if (pe_enable_stdcall_fixup == -1)
609 {
610 einfo (_("Warning: resolving %s by linking to %s\n"),
611 undef->root.string, sym->root.string);
612 if (! gave_warning_message)
613 {
614 gave_warning_message = 1;
615 einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
616 einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
617 }
618 }
619 }
620 }
621 }
622 }
623
624 static void
625 gld_${EMULATION_NAME}_after_open ()
626 {
627 /* Pass the wacky PE command line options into the output bfd.
628 FIXME: This should be done via a function, rather than by
629 including an internal BFD header. */
630
631 if (!coff_data (output_bfd)->pe)
632 einfo (_("%F%P: PE operations on non PE file.\n"));
633
634 pe_data (output_bfd)->pe_opthdr = pe;
635 pe_data (output_bfd)->dll = init[DLLOFF].value;
636
637 #ifdef DLL_SUPPORT
638 if (pe_enable_stdcall_fixup) /* -1=warn or 1=disable */
639 pe_fixup_stdcalls ();
640
641 pe_process_import_defs(output_bfd, &link_info);
642 if (link_info.shared)
643 pe_dll_build_sections (output_bfd, &link_info);
644 #endif
645
646 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
647 {
648 /* Find a BFD that can hold the interworking stubs. */
649 LANG_FOR_EACH_INPUT_STATEMENT (is)
650 {
651 if (bfd_arm_pe_get_bfd_for_interworking (is->the_bfd, & link_info))
652 break;
653 }
654 }
655 #endif
656
657 {
658 static int sequence = 0;
659 int is_ms_arch;
660 bfd *cur_arch = 0, *elt;
661 lang_input_statement_type *is2;
662 /* Careful - this is a shell script. Watch those dollar signs! */
663 /* Microsoft import libraries have every member named the same,
664 and not in the right order for us to link them correctly. We
665 must detect these and rename the members so that they'll link
666 correctly. There are three types of objects: the head, the
667 thunks, and the sentinel(s). The head is easy; it's the one
668 with idata2. We assume that the sentinels won't have relocs,
669 and the thunks will. It's easier than checking the symbol
670 table for external references. */
671 LANG_FOR_EACH_INPUT_STATEMENT (is)
672 {
673 if (is->the_bfd->my_archive)
674 {
675 bfd *arch = is->the_bfd->my_archive;
676 if (cur_arch != arch)
677 {
678 cur_arch = arch;
679 is_ms_arch = 1;
680 for (is2 = is;
681 is2 && is2->the_bfd->my_archive == arch;
682 is2 = (lang_input_statement_type *)is2->next)
683 {
684 if (strcmp (is->the_bfd->filename, is2->the_bfd->filename))
685 is_ms_arch = 0;
686 }
687 }
688
689 if (is_ms_arch)
690 {
691 int idata2 = 0, i, reloc_count=0;
692 asection *sec;
693 char *new_name, seq;
694 for (sec = is->the_bfd->sections; sec; sec = sec->next)
695 {
696 if (strcmp (sec->name, ".idata\$2") == 0)
697 idata2 = 1;
698 reloc_count += sec->reloc_count;
699 }
700
701 if (idata2) /* .idata2 is the TOC */
702 seq = 'a';
703 else if (reloc_count > 0) /* thunks */
704 seq = 'b';
705 else /* sentinel */
706 seq = 'c';
707
708 new_name = bfd_alloc (is->the_bfd,
709 strlen (is->the_bfd->filename)+2);
710 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
711 is->the_bfd->filename = new_name;
712
713 new_name = bfd_alloc(is->the_bfd, strlen(is->filename)+2);
714 sprintf (new_name, "%s.%c", is->filename, seq);
715 is->filename = new_name;
716 }
717 }
718 }
719 }
720 }
721 \f
722 static void
723 gld_${EMULATION_NAME}_before_allocation()
724 {
725 #ifdef TARGET_IS_ppcpe
726 /* Here we rummage through the found bfds to collect toc information */
727 {
728 LANG_FOR_EACH_INPUT_STATEMENT (is)
729 {
730 if (!ppc_process_before_allocation (is->the_bfd, &link_info))
731 {
732 /* xgettext:c-format */
733 einfo (_("Errors encountered processing file %s\n"), is->filename);
734 }
735 }
736 }
737
738 /* We have seen it all. Allocate it, and carry on */
739 ppc_allocate_toc_section (&link_info);
740 #endif /* TARGET_IS_ppcpe */
741
742 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
743 /* FIXME: we should be able to set the size of the interworking stub
744 section.
745
746 Here we rummage through the found bfds to collect glue
747 information. FIXME: should this be based on a command line
748 option? krk@cygnus.com */
749 {
750 LANG_FOR_EACH_INPUT_STATEMENT (is)
751 {
752 if (! bfd_arm_pe_process_before_allocation
753 (is->the_bfd, & link_info, support_old_code))
754 {
755 /* xgettext:c-format */
756 einfo (_("Errors encountered processing file %s for interworking"),
757 is->filename);
758 }
759 }
760 }
761
762 /* We have seen it all. Allocate it, and carry on */
763 bfd_arm_pe_allocate_interworking_sections (& link_info);
764 #endif /* TARGET_IS_armpe */
765 }
766 \f
767
768 /* This is called when an input file isn't recognized as a BFD. We
769 check here for .DEF files and pull them in automatically. */
770
771 static int
772 saw_option(char *option)
773 {
774 int i;
775 for (i=0; init[i].ptr; i++)
776 if (strcmp (init[i].symbol, option) == 0)
777 return init[i].inited;
778 return 0;
779 }
780
781 static boolean
782 gld_${EMULATION_NAME}_unrecognized_file(entry)
783 lang_input_statement_type *entry;
784 {
785 #ifdef DLL_SUPPORT
786 const char *ext = entry->filename + strlen (entry->filename) - 4;
787
788 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
789 {
790 if (pe_def_file == 0)
791 pe_def_file = def_file_empty ();
792 def_file_parse (entry->filename, pe_def_file);
793 if (pe_def_file)
794 {
795 int i, buflen=0, len;
796 char *buf;
797 for (i=0; i<pe_def_file->num_exports; i++)
798 {
799 len = strlen(pe_def_file->exports[i].internal_name);
800 if (buflen < len+2)
801 buflen = len+2;
802 }
803 buf = (char *) xmalloc (buflen);
804 for (i=0; i<pe_def_file->num_exports; i++)
805 {
806 struct bfd_link_hash_entry *h;
807 sprintf(buf, "_%s", pe_def_file->exports[i].internal_name);
808
809 h = bfd_link_hash_lookup (link_info.hash, buf, true, true, true);
810 if (h == (struct bfd_link_hash_entry *) NULL)
811 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
812 if (h->type == bfd_link_hash_new)
813 {
814 h->type = bfd_link_hash_undefined;
815 h->u.undef.abfd = NULL;
816 bfd_link_add_undef (link_info.hash, h);
817 }
818 }
819 free (buf);
820
821 /* def_file_print (stdout, pe_def_file); */
822 if (pe_def_file->is_dll == 1)
823 link_info.shared = 1;
824
825 if (pe_def_file->base_address != (bfd_vma)(-1))
826 {
827 pe.ImageBase =
828 pe_data (output_bfd)->pe_opthdr.ImageBase =
829 init[IMAGEBASEOFF].value = pe_def_file->base_address;
830 init[IMAGEBASEOFF].inited = 1;
831 if (image_base_statement)
832 image_base_statement->exp =
833 exp_assop ('=', "__image_base__", exp_intop (pe.ImageBase));
834 }
835
836 #if 0
837 /* Not sure if these *should* be set */
838 if (pe_def_file->version_major != -1)
839 {
840 pe.MajorImageVersion = pe_def_file->version_major;
841 pe.MinorImageVersion = pe_def_file->version_minor;
842 }
843 #endif
844 if (pe_def_file->stack_reserve != -1
845 && ! saw_option ("__size_of_stack_reserve__"))
846 {
847 pe.SizeOfStackReserve = pe_def_file->stack_reserve;
848 if (pe_def_file->stack_commit != -1)
849 pe.SizeOfStackCommit = pe_def_file->stack_commit;
850 }
851 if (pe_def_file->heap_reserve != -1
852 && ! saw_option ("__size_of_heap_reserve__"))
853 {
854 pe.SizeOfHeapReserve = pe_def_file->heap_reserve;
855 if (pe_def_file->heap_commit != -1)
856 pe.SizeOfHeapCommit = pe_def_file->heap_commit;
857 }
858 return true;
859 }
860 }
861 #endif
862 return false;
863
864 }
865
866 static boolean
867 gld_${EMULATION_NAME}_recognized_file(entry)
868 lang_input_statement_type *entry;
869 {
870 #ifdef DLL_SUPPORT
871 #ifdef TARGET_IS_i386pe
872 pe_dll_id_target ("pei-i386");
873 #endif
874 if (bfd_get_format (entry->the_bfd) == bfd_object)
875 {
876 const char *ext = entry->filename + strlen (entry->filename) - 4;
877 if (strcmp (ext, ".dll") == 0 || strcmp (ext, ".DLL") == 0)
878 return pe_implied_import_dll (entry->filename);
879 }
880 #endif
881 return false;
882 }
883
884 static void
885 gld_${EMULATION_NAME}_finish ()
886 {
887 #ifdef DLL_SUPPORT
888 if (link_info.shared)
889 {
890 pe_dll_fill_sections (output_bfd, &link_info);
891 if (pe_implib_filename)
892 pe_dll_generate_implib (pe_def_file, pe_implib_filename);
893 }
894 if (pe_out_def_filename)
895 pe_dll_generate_def_file (pe_out_def_filename);
896 #endif
897 }
898
899 \f
900 /* Place an orphan section.
901
902 We use this to put sections in a reasonable place in the file, and
903 to ensure that they are aligned as required.
904
905 We handle grouped sections here as well. A section named .foo$nn
906 goes into the output section .foo. All grouped sections are sorted
907 by name.
908
909 Grouped sections for the default sections are handled by the
910 default linker script using wildcards, and are sorted by
911 sort_sections. */
912
913 static asection *hold_section;
914 static char *hold_section_name;
915 static lang_output_section_statement_type *hold_use;
916 static lang_output_section_statement_type *hold_text;
917 static lang_output_section_statement_type *hold_rdata;
918 static lang_output_section_statement_type *hold_data;
919 static lang_output_section_statement_type *hold_bss;
920
921 /* Place an orphan section. We use this to put random SHF_ALLOC
922 sections in the right segment. */
923
924 /*ARGSUSED*/
925 static boolean
926 gld_${EMULATION_NAME}_place_orphan (file, s)
927 lang_input_statement_type *file;
928 asection *s;
929 {
930 const char *secname;
931 char *dollar;
932
933 if ((s->flags & SEC_ALLOC) == 0)
934 return false;
935
936 secname = bfd_get_section_name (s->owner, s);
937
938 /* Look through the script to see where to place this section. */
939
940 hold_section = s;
941
942 hold_section_name = xstrdup (secname);
943 dollar = strchr (hold_section_name, '$');
944 if (dollar != NULL)
945 *dollar = '\0';
946
947 hold_use = NULL;
948 lang_for_each_statement (gld${EMULATION_NAME}_place_section);
949
950 if (hold_use == NULL)
951 {
952 lang_output_section_statement_type *place;
953 char *outsecname;
954 asection *snew, **pps;
955 lang_statement_list_type *old;
956 lang_statement_list_type add;
957 etree_type *address;
958
959 /* Try to put the new output section in a reasonable place based
960 on the section name and section flags. */
961 place = NULL;
962 if ((s->flags & SEC_HAS_CONTENTS) == 0
963 && hold_bss != NULL)
964 place = hold_bss;
965 else if ((s->flags & SEC_READONLY) == 0
966 && hold_data != NULL)
967 place = hold_data;
968 else if ((s->flags & SEC_CODE) == 0
969 && (s->flags & SEC_READONLY) != 0
970 && hold_rdata != NULL)
971 place = hold_rdata;
972 else if ((s->flags & SEC_READONLY) != 0
973 && hold_text != NULL)
974 place = hold_text;
975
976 /* Choose a unique name for the section. This will be needed if
977 the same section name appears in the input file with
978 different loadable or allocateable characteristics. */
979 outsecname = xstrdup (hold_section_name);
980 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
981 {
982 unsigned int len;
983 char *newname;
984 unsigned int i;
985
986 len = strlen (outsecname);
987 newname = xmalloc (len + 5);
988 strcpy (newname, outsecname);
989 i = 0;
990 do
991 {
992 sprintf (newname + len, "%d", i);
993 ++i;
994 }
995 while (bfd_get_section_by_name (output_bfd, newname) != NULL);
996
997 free (outsecname);
998 outsecname = newname;
999 }
1000
1001 /* We don't want to free OUTSECNAME, as it may get attached to
1002 the output section statement. */
1003
1004 /* Create the section in the output file, and put it in the
1005 right place. This shuffling is to make the output file look
1006 neater. */
1007 snew = bfd_make_section (output_bfd, outsecname);
1008 if (snew == NULL)
1009 einfo ("%P%F: output format %s cannot represent section called %s\n",
1010 output_bfd->xvec->name, outsecname);
1011 if (place != NULL && place->bfd_section != NULL)
1012 {
1013 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1014 ;
1015 *pps = snew->next;
1016 snew->next = place->bfd_section->next;
1017 place->bfd_section->next = snew;
1018 }
1019
1020 /* Start building a list of statements for this section. */
1021 old = stat_ptr;
1022 stat_ptr = &add;
1023 lang_list_init (stat_ptr);
1024
1025 if (link_info.relocateable)
1026 address = NULL;
1027 else
1028 {
1029 /* All sections in an executable must be aligned to a page
1030 boundary. */
1031 address = exp_unop (ALIGN_K,
1032 exp_nameop (NAME, "__section_alignment__"));
1033 }
1034
1035 lang_enter_output_section_statement (outsecname, address, 0,
1036 (bfd_vma) 0,
1037 (etree_type *) NULL,
1038 (etree_type *) NULL,
1039 (etree_type *) NULL);
1040
1041 hold_use = lang_output_section_statement_lookup (outsecname);
1042
1043 lang_leave_output_section_statement
1044 ((bfd_vma) 0, "*default*",
1045 (struct lang_output_section_phdr_list *) NULL);
1046
1047 /* Now stick the new statement list right after PLACE. */
1048 if (place != NULL)
1049 {
1050 *add.tail = place->header.next;
1051 place->header.next = add.head;
1052 }
1053
1054 stat_ptr = old;
1055 }
1056
1057 if (dollar == NULL)
1058 wild_doit (&hold_use->children, s, hold_use, file);
1059 else
1060 {
1061 lang_statement_union_type **pl;
1062 boolean found_dollar;
1063 lang_statement_list_type list;
1064
1065 /* The section name has a '$'. Sort it with the other '$'
1066 sections. */
1067
1068 found_dollar = false;
1069 for (pl = &hold_use->children.head; *pl != NULL; pl = &(*pl)->next)
1070 {
1071 lang_input_section_type *ls;
1072 const char *lname;
1073
1074 if ((*pl)->header.type != lang_input_section_enum)
1075 continue;
1076
1077 ls = &(*pl)->input_section;
1078
1079 lname = bfd_get_section_name (ls->ifile->the_bfd, ls->section);
1080 if (strchr (lname, '$') == NULL)
1081 {
1082 if (found_dollar)
1083 break;
1084 }
1085 else
1086 {
1087 found_dollar = true;
1088 if (strcmp (secname, lname) < 0)
1089 break;
1090 }
1091 }
1092
1093 lang_list_init (&list);
1094 wild_doit (&list, s, hold_use, file);
1095 if (list.head != NULL)
1096 {
1097 ASSERT (list.head->next == NULL);
1098 list.head->next = *pl;
1099 *pl = list.head;
1100 }
1101 }
1102
1103 free (hold_section_name);
1104
1105 return true;
1106 }
1107
1108 static void
1109 gld${EMULATION_NAME}_place_section (s)
1110 lang_statement_union_type *s;
1111 {
1112 lang_output_section_statement_type *os;
1113
1114 if (s->header.type != lang_output_section_statement_enum)
1115 return;
1116
1117 os = &s->output_section_statement;
1118
1119 if (strcmp (os->name, hold_section_name) == 0
1120 && os->bfd_section != NULL
1121 && ((hold_section->flags & (SEC_LOAD | SEC_ALLOC))
1122 == (os->bfd_section->flags & (SEC_LOAD | SEC_ALLOC))))
1123 hold_use = os;
1124
1125 if (strcmp (os->name, ".text") == 0)
1126 hold_text = os;
1127 else if (strcmp (os->name, ".rdata") == 0)
1128 hold_rdata = os;
1129 else if (strcmp (os->name, ".data") == 0)
1130 hold_data = os;
1131 else if (strcmp (os->name, ".bss") == 0)
1132 hold_bss = os;
1133 }
1134 \f
1135 static char *
1136 gld_${EMULATION_NAME}_get_script(isfile)
1137 int *isfile;
1138 EOF
1139 # Scripts compiled in.
1140 # sed commands to quote an ld script as a C string.
1141 sc="-f ${srcdir}/emultempl/stringify.sed"
1142
1143 cat >>e${EMULATION_NAME}.c <<EOF
1144 {
1145 *isfile = 0;
1146
1147 if (link_info.relocateable == true && config.build_constructors == true)
1148 return
1149 EOF
1150 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1151 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1152 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1153 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1154 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1155 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1156 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1157 echo ' ; else return' >> e${EMULATION_NAME}.c
1158 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1159 echo '; }' >> e${EMULATION_NAME}.c
1160
1161 cat >>e${EMULATION_NAME}.c <<EOF
1162
1163
1164 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1165 {
1166 gld_${EMULATION_NAME}_before_parse,
1167 syslib_default,
1168 hll_default,
1169 gld_${EMULATION_NAME}_after_parse,
1170 gld_${EMULATION_NAME}_after_open,
1171 after_allocation_default,
1172 set_output_arch_default,
1173 ldemul_default_target,
1174 gld_${EMULATION_NAME}_before_allocation,
1175 gld_${EMULATION_NAME}_get_script,
1176 "${EMULATION_NAME}",
1177 "${OUTPUT_FORMAT}",
1178 gld_${EMULATION_NAME}_finish, /* finish */
1179 NULL, /* create output section statements */
1180 NULL, /* open dynamic archive */
1181 gld_${EMULATION_NAME}_place_orphan,
1182 gld_${EMULATION_NAME}_set_symbols,
1183 gld_${EMULATION_NAME}_parse_args,
1184 gld_${EMULATION_NAME}_unrecognized_file,
1185 gld_${EMULATION_NAME}_list_options,
1186 gld_${EMULATION_NAME}_recognized_file
1187 };
1188 EOF