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