1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 rm -f e${EMULATION_NAME}.c
4 (echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
5 cat >>e${EMULATION_NAME}.c <<EOF
6 /* This file is part of GLD, the Gnu Linker.
7 Copyright 1995, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 /* The original file generated returned different default scripts depending
25 on whether certain switches were set, but these switches pertain to the
26 Linux system and that particular version of coff. In the NT case, we
27 only determine if the subsystem is console or windows in order to select
28 the correct entry point by default. */
34 #include "libiberty.h"
45 #include "coff/internal.h"
47 /* FIXME: This is a BFD internal header file, and we should not be
49 #include "../bfd/libcoff.h"
54 #define TARGET_IS_${EMULATION_NAME}
56 /* Permit the emulation parameters to override the default section
57 alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes
58 it seem that include/coff/internal.h should not define
59 PE_DEF_SECTION_ALIGNMENT. */
60 #if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
61 #undef PE_DEF_SECTION_ALIGNMENT
62 #define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
65 #if defined(TARGET_IS_i386pe)
68 #if defined(TARGET_IS_shpe) || defined(TARGET_IS_mipspe) || defined(TARGET_IS_armpe)
72 #if defined(TARGET_IS_i386pe) || ! defined(DLL_SUPPORT)
73 #define PE_DEF_SUBSYSTEM 3
75 #undef NT_EXE_IMAGE_BASE
76 #undef PE_DEF_SECTION_ALIGNMENT
77 #undef PE_DEF_FILE_ALIGNMENT
78 #define NT_EXE_IMAGE_BASE 0x00010000
79 #ifdef TARGET_IS_armpe
80 #define PE_DEF_SECTION_ALIGNMENT 0x00001000
81 #define PE_DEF_SUBSYSTEM 9
83 #define PE_DEF_SECTION_ALIGNMENT 0x00000400
84 #define PE_DEF_SUBSYSTEM 2
86 #define PE_DEF_FILE_ALIGNMENT 0x00000200
89 #ifdef TARGET_IS_arm_epoc_pe
90 #define bfd_arm_pe_allocate_interworking_sections \
91 bfd_arm_epoc_pe_allocate_interworking_sections
92 #define bfd_arm_pe_get_bfd_for_interworking \
93 bfd_arm_epoc_pe_get_bfd_for_interworking
94 #define bfd_arm_pe_process_before_allocation \
95 bfd_arm_epoc_pe_process_before_allocation
98 static void gld_${EMULATION_NAME}_set_symbols PARAMS ((void));
99 static void gld_${EMULATION_NAME}_after_open PARAMS ((void));
100 static void gld_${EMULATION_NAME}_before_parse PARAMS ((void));
101 static void gld_${EMULATION_NAME}_after_parse PARAMS ((void));
102 static void gld_${EMULATION_NAME}_before_allocation PARAMS ((void));
103 static boolean gld_${EMULATION_NAME}_place_orphan
104 PARAMS ((lang_input_statement_type *, asection *));
105 static void gld${EMULATION_NAME}_place_section
106 PARAMS ((lang_statement_union_type *));
107 static char *gld_${EMULATION_NAME}_get_script PARAMS ((int *));
108 static int gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **));
109 static void gld_${EMULATION_NAME}_finish PARAMS ((void));
111 static struct internal_extra_pe_aouthdr pe;
113 static int support_old_code = 0;
114 static char * thumb_entry_symbol = NULL;
115 static lang_assignment_statement_type *image_base_statement = 0;
117 static char *pe_out_def_filename = 0;
118 static int pe_enable_stdcall_fixup = -1; /* 0=disable 1=enable */
119 static char *pe_implib_filename = 0;
121 extern const char *output_filename;
124 gld_${EMULATION_NAME}_before_parse()
126 output_filename = "${EXECUTABLE_NAME:-a.exe}";
127 ldfile_output_architecture = bfd_arch_${ARCH};
129 config.has_shared = 1;
131 #if (PE_DEF_SUBSYSTEM == 9) || (PE_DEF_SUBSYSTEM == 2)
132 #if defined TARGET_IS_mipspe || defined TARGET_IS_armpe
133 lang_add_entry ("WinMainCRTStartup", 1);
135 lang_add_entry ("_WinMainCRTStartup", 1);
141 /* PE format extra command line options. */
143 /* Used for setting flags in the PE header. */
144 #define OPTION_BASE_FILE (300 + 1)
145 #define OPTION_DLL (OPTION_BASE_FILE + 1)
146 #define OPTION_FILE_ALIGNMENT (OPTION_DLL + 1)
147 #define OPTION_IMAGE_BASE (OPTION_FILE_ALIGNMENT + 1)
148 #define OPTION_MAJOR_IMAGE_VERSION (OPTION_IMAGE_BASE + 1)
149 #define OPTION_MAJOR_OS_VERSION (OPTION_MAJOR_IMAGE_VERSION + 1)
150 #define OPTION_MAJOR_SUBSYSTEM_VERSION (OPTION_MAJOR_OS_VERSION + 1)
151 #define OPTION_MINOR_IMAGE_VERSION (OPTION_MAJOR_SUBSYSTEM_VERSION + 1)
152 #define OPTION_MINOR_OS_VERSION (OPTION_MINOR_IMAGE_VERSION + 1)
153 #define OPTION_MINOR_SUBSYSTEM_VERSION (OPTION_MINOR_OS_VERSION + 1)
154 #define OPTION_SECTION_ALIGNMENT (OPTION_MINOR_SUBSYSTEM_VERSION + 1)
155 #define OPTION_STACK (OPTION_SECTION_ALIGNMENT + 1)
156 #define OPTION_SUBSYSTEM (OPTION_STACK + 1)
157 #define OPTION_HEAP (OPTION_SUBSYSTEM + 1)
158 #define OPTION_SUPPORT_OLD_CODE (OPTION_HEAP + 1)
159 #define OPTION_OUT_DEF (OPTION_SUPPORT_OLD_CODE + 1)
160 #define OPTION_EXPORT_ALL (OPTION_OUT_DEF + 1)
161 #define OPTION_EXCLUDE_SYMBOLS (OPTION_EXPORT_ALL + 1)
162 #define OPTION_KILL_ATS (OPTION_EXCLUDE_SYMBOLS + 1)
163 #define OPTION_STDCALL_ALIASES (OPTION_KILL_ATS + 1)
164 #define OPTION_ENABLE_STDCALL_FIXUP (OPTION_STDCALL_ALIASES + 1)
165 #define OPTION_DISABLE_STDCALL_FIXUP (OPTION_ENABLE_STDCALL_FIXUP + 1)
166 #define OPTION_IMPLIB_FILENAME (OPTION_DISABLE_STDCALL_FIXUP + 1)
167 #define OPTION_THUMB_ENTRY (OPTION_IMPLIB_FILENAME + 1)
168 #define OPTION_WARN_DUPLICATE_EXPORTS (OPTION_THUMB_ENTRY + 1)
169 #define OPTION_IMP_COMPAT (OPTION_WARN_DUPLICATE_EXPORTS + 1)
171 static struct option longopts[] =
174 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
175 {"dll", no_argument, NULL, OPTION_DLL},
176 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
177 {"heap", required_argument, NULL, OPTION_HEAP},
178 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
179 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
180 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
181 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
182 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
183 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
184 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
185 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
186 {"stack", required_argument, NULL, OPTION_STACK},
187 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
188 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
189 {"thumb-entry", required_argument, NULL, OPTION_THUMB_ENTRY},
191 /* getopt allows abbreviations, so we do this to stop it from treating -o
192 as an abbreviation for this option */
193 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
194 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
195 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
196 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
197 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
198 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
199 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
200 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
201 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
202 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
203 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
205 {NULL, no_argument, NULL, 0}
209 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
210 parameters which may be input from the command line */
221 #define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol,0}
223 static definfo init[] =
225 /* imagebase must be first */
226 #define IMAGEBASEOFF 0
227 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
229 {&dll, sizeof(dll), 0, "__dll__", 0},
230 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
231 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
232 D(MajorOperatingSystemVersion,"__major_os_version__", 4),
233 D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
234 D(MajorImageVersion,"__major_image_version__", 1),
235 D(MinorImageVersion,"__minor_image_version__", 0),
236 #ifdef TARGET_IS_armpe
237 D(MajorSubsystemVersion,"__major_subsystem_version__", 2),
239 D(MajorSubsystemVersion,"__major_subsystem_version__", 4),
241 D(MinorSubsystemVersion,"__minor_subsystem_version__", 0),
242 D(Subsystem,"__subsystem__", ${SUBSYSTEM}),
243 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x2000000),
244 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
245 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
246 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
247 D(LoaderFlags,"__loader_flags__", 0x0),
248 { NULL, 0, 0, NULL, 0 }
252 gld_${EMULATION_NAME}_list_options (file)
255 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
256 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
257 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
258 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
259 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
260 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
261 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
262 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
263 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
264 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
265 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
266 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
267 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
268 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
269 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
270 fprintf (file, _(" --thumb-entry=<symbol> Set the entry point to be Thumb <symbol>\n"));
272 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
273 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
274 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
275 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
276 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
277 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
278 fprintf (file, _(" --out-implib <file> Generate import library\n"));
279 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
280 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
281 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n"));
282 fprintf (file, _(" create __imp_<SYMBOL> as well.\n"));
287 set_pe_name (name, val)
292 /* Find the name and set it. */
293 for (i = 0; init[i].ptr; i++)
295 if (strcmp (name, init[i].symbol) == 0)
320 { "native", 1, "NtProcessStartup" },
321 #if defined TARGET_IS_mipspe || defined TARGET_IS_armpe
322 { "windows", 2, "WinMainCRTStartup" },
324 { "windows", 2, "WinMainCRTStartup" },
326 { "console", 3, "mainCRTStartup" },
328 /* The Microsoft linker does not recognize this. */
331 { "posix", 7, "__PosixProcessStartup"},
332 { "wince", 9, "_WinMainCRTStartup" },
336 sver = strchr (optarg, ':');
338 len = strlen (optarg);
344 set_pe_name ("__major_subsystem_version__",
345 strtoul (sver + 1, &end, 0));
347 set_pe_name ("__minor_subsystem_version__",
348 strtoul (end + 1, &end, 0));
350 einfo (_("%P: warning: bad version number in -subsystem option\n"));
353 for (i = 0; v[i].name; i++)
355 if (strncmp (optarg, v[i].name, len) == 0
356 && v[i].name[len] == '\0')
358 const char *initial_symbol_char;
361 set_pe_name ("__subsystem__", v[i].value);
363 initial_symbol_char = ${INITIAL_SYMBOL_CHAR};
364 if (*initial_symbol_char == '\0')
370 /* lang_add_entry expects its argument to be permanently
371 allocated, so we don't free this string. */
372 alc_entry = xmalloc (strlen (initial_symbol_char)
373 + strlen (v[i].entry)
375 strcpy (alc_entry, initial_symbol_char);
376 strcat (alc_entry, v[i].entry);
380 lang_add_entry (entry, 1);
386 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
398 set_pe_name (name, strtoul (optarg, &end, 0));
401 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
407 set_pe_stack_heap (resname, comname)
411 set_pe_value (resname);
416 set_pe_value (comname);
419 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
425 gld_${EMULATION_NAME}_parse_args(argc, argv)
431 int prevoptind = optind;
432 int prevopterr = opterr;
434 static int lastoptind = -1;
436 if (lastoptind != optind)
442 optc = getopt_long_only (argc, argv, "-", longopts, &longind);
453 case OPTION_BASE_FILE:
454 link_info.base_file = (PTR) fopen (optarg, FOPEN_WB);
455 if (link_info.base_file == NULL)
457 /* xgettext:c-format */
458 fprintf (stderr, _("%s: Can't open base file %s\n"),
459 program_name, optarg);
466 set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
469 set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
471 case OPTION_SUBSYSTEM:
474 case OPTION_MAJOR_OS_VERSION:
475 set_pe_value ("__major_os_version__");
477 case OPTION_MINOR_OS_VERSION:
478 set_pe_value ("__minor_os_version__");
480 case OPTION_MAJOR_SUBSYSTEM_VERSION:
481 set_pe_value ("__major_subsystem_version__");
483 case OPTION_MINOR_SUBSYSTEM_VERSION:
484 set_pe_value ("__minor_subsystem_version__");
486 case OPTION_MAJOR_IMAGE_VERSION:
487 set_pe_value ("__major_image_version__");
489 case OPTION_MINOR_IMAGE_VERSION:
490 set_pe_value ("__minor_image_version__");
492 case OPTION_FILE_ALIGNMENT:
493 set_pe_value ("__file_alignment__");
495 case OPTION_SECTION_ALIGNMENT:
496 set_pe_value ("__section_alignment__");
499 set_pe_name ("__dll__", 1);
501 case OPTION_IMAGE_BASE:
502 set_pe_value ("__image_base__");
504 case OPTION_SUPPORT_OLD_CODE:
505 support_old_code = 1;
507 case OPTION_THUMB_ENTRY:
508 thumb_entry_symbol = optarg;
512 pe_out_def_filename = xstrdup (optarg);
514 case OPTION_EXPORT_ALL:
515 pe_dll_export_everything = 1;
517 case OPTION_EXCLUDE_SYMBOLS:
518 pe_dll_add_excludes (optarg);
520 case OPTION_KILL_ATS:
523 case OPTION_STDCALL_ALIASES:
524 pe_dll_stdcall_aliases = 1;
526 case OPTION_ENABLE_STDCALL_FIXUP:
527 pe_enable_stdcall_fixup = 1;
529 case OPTION_DISABLE_STDCALL_FIXUP:
530 pe_enable_stdcall_fixup = 0;
532 case OPTION_IMPLIB_FILENAME:
533 pe_implib_filename = xstrdup (optarg);
535 case OPTION_WARN_DUPLICATE_EXPORTS:
536 pe_dll_warn_dup_exports = 1;
538 case OPTION_IMP_COMPAT:
539 pe_dll_compat_implib = 1;
546 /* Assign values to the special symbols before the linker script is
550 gld_${EMULATION_NAME}_set_symbols ()
552 /* Run through and invent symbols for all the
553 names and insert the defaults. */
555 lang_statement_list_type *save;
557 if (!init[IMAGEBASEOFF].inited)
559 if (link_info.relocateable)
560 init[IMAGEBASEOFF].value = 0;
561 else if (init[DLLOFF].value || link_info.shared)
562 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
564 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
567 /* Don't do any symbol assignments if this is a relocateable link. */
568 if (link_info.relocateable)
571 /* Glue the assignments into the abs section */
574 stat_ptr = &(abs_output_section->children);
576 for (j = 0; init[j].ptr; j++)
578 long val = init[j].value;
579 lang_assignment_statement_type *rv;
580 rv = lang_add_assignment (exp_assop ('=' ,init[j].symbol, exp_intop (val)));
581 if (init[j].size == sizeof(short))
582 *(short *)init[j].ptr = val;
583 else if (init[j].size == sizeof(int))
584 *(int *)init[j].ptr = val;
585 else if (init[j].size == sizeof(long))
586 *(long *)init[j].ptr = val;
587 /* This might be a long long or other special type. */
588 else if (init[j].size == sizeof(bfd_vma))
589 *(bfd_vma *)init[j].ptr = val;
591 if (j == IMAGEBASEOFF)
592 image_base_statement = rv;
594 /* Restore the pointer. */
597 if (pe.FileAlignment >
600 einfo (_("%P: warning, file alignment > section alignment.\n"));
604 /* This is called after the linker script and the command line options
608 gld_${EMULATION_NAME}_after_parse ()
610 /* The Windows libraries are designed for the linker to treat the
611 entry point as an undefined symbol. Otherwise, the .obj that
612 defines mainCRTStartup is brought in because it is the first
613 encountered in libc.lib and it has other symbols in it which will
614 be pulled in by the link process. To avoid this, we act as
615 though the user specified -u with the entry point symbol.
617 This function is called after the linker script and command line
618 options have been read, so at this point we know the right entry
619 point. This function is called before the input files are
620 opened, so registering the symbol as undefined will make a
623 if (! link_info.relocateable && entry_symbol != NULL)
624 ldlang_add_undef (entry_symbol);
627 static struct bfd_link_hash_entry *pe_undef_found_sym;
630 pe_undef_cdecl_match (h, string)
631 struct bfd_link_hash_entry *h;
634 int sl = strlen (string);
635 if (h->type == bfd_link_hash_defined
636 && strncmp (h->root.string, string, sl) == 0
637 && h->root.string[sl] == '@')
639 pe_undef_found_sym = h;
648 static int gave_warning_message = 0;
649 struct bfd_link_hash_entry *undef, *sym;
651 for (undef = link_info.hash->undefs; undef; undef=undef->next)
652 if (undef->type == bfd_link_hash_undefined)
654 at = strchr (undef->root.string, '@');
657 /* The symbol is a stdcall symbol, so let's look for a cdecl
658 symbol with the same name and resolve to that */
659 char *cname = xstrdup (undef->root.string);
660 at = strchr (cname, '@');
662 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
663 if (sym && sym->type == bfd_link_hash_defined)
665 undef->type = bfd_link_hash_defined;
666 undef->u.def.value = sym->u.def.value;
667 undef->u.def.section = sym->u.def.section;
668 if (pe_enable_stdcall_fixup == -1)
670 einfo (_("Warning: resolving %s by linking to %s\n"),
671 undef->root.string, cname);
672 if (! gave_warning_message)
674 gave_warning_message = 1;
675 einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
676 einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
683 /* The symbol is a cdecl symbol, so we look for stdcall
684 symbols - which means scanning the whole symbol table */
685 pe_undef_found_sym = 0;
686 bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
687 (PTR) undef->root.string);
688 sym = pe_undef_found_sym;
691 undef->type = bfd_link_hash_defined;
692 undef->u.def.value = sym->u.def.value;
693 undef->u.def.section = sym->u.def.section;
694 if (pe_enable_stdcall_fixup == -1)
696 einfo (_("Warning: resolving %s by linking to %s\n"),
697 undef->root.string, sym->root.string);
698 if (! gave_warning_message)
700 gave_warning_message = 1;
701 einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
702 einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
711 gld_${EMULATION_NAME}_after_open ()
713 /* Pass the wacky PE command line options into the output bfd.
714 FIXME: This should be done via a function, rather than by
715 including an internal BFD header. */
717 if (!coff_data (output_bfd)->pe)
718 einfo (_("%F%P: PE operations on non PE file.\n"));
720 pe_data (output_bfd)->pe_opthdr = pe;
721 pe_data (output_bfd)->dll = init[DLLOFF].value;
724 if (pe_enable_stdcall_fixup) /* -1=warn or 1=disable */
725 pe_fixup_stdcalls ();
727 pe_process_import_defs(output_bfd, &link_info);
728 if (link_info.shared)
729 pe_dll_build_sections (output_bfd, &link_info);
731 #ifndef TARGET_IS_i386pe
732 #ifndef TARGET_IS_armpe
734 pe_exe_build_sections (output_bfd, &link_info);
739 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
740 if (strstr (bfd_get_target (output_bfd), "arm") == NULL)
742 /* The arm backend needs special fields in the output hash structure.
743 These will only be created if the output format is an arm format,
744 hence we do not support linking and changing output formats at the
745 same time. Use a link followed by objcopy to change output formats. */
746 einfo ("%F%X%P: error: cannot change output format whilst linking ARM binaries\n");
750 /* Find a BFD that can hold the interworking stubs. */
751 LANG_FOR_EACH_INPUT_STATEMENT (is)
753 if (bfd_arm_pe_get_bfd_for_interworking (is->the_bfd, & link_info))
762 lang_input_statement_type *is2;
764 /* Careful - this is a shell script. Watch those dollar signs! */
765 /* Microsoft import libraries have every member named the same,
766 and not in the right order for us to link them correctly. We
767 must detect these and rename the members so that they'll link
768 correctly. There are three types of objects: the head, the
769 thunks, and the sentinel(s). The head is easy; it's the one
770 with idata2. We assume that the sentinels won't have relocs,
771 and the thunks will. It's easier than checking the symbol
772 table for external references. */
773 LANG_FOR_EACH_INPUT_STATEMENT (is)
775 if (is->the_bfd->my_archive)
777 bfd *arch = is->the_bfd->my_archive;
778 if (cur_arch != arch)
783 is2 && is2->the_bfd->my_archive == arch;
784 is2 = (lang_input_statement_type *)is2->next)
786 if (strcmp (is->the_bfd->filename, is2->the_bfd->filename))
793 int idata2 = 0, reloc_count=0;
797 for (sec = is->the_bfd->sections; sec; sec = sec->next)
799 if (strcmp (sec->name, ".idata\$2") == 0)
801 reloc_count += sec->reloc_count;
804 if (idata2) /* .idata2 is the TOC */
806 else if (reloc_count > 0) /* thunks */
811 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
812 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
813 is->the_bfd->filename = new_name;
815 new_name = xmalloc (strlen(is->filename) + 3);
816 sprintf (new_name, "%s.%c", is->filename, seq);
817 is->filename = new_name;
825 gld_${EMULATION_NAME}_before_allocation()
827 #ifdef TARGET_IS_ppcpe
828 /* Here we rummage through the found bfds to collect toc information */
830 LANG_FOR_EACH_INPUT_STATEMENT (is)
832 if (!ppc_process_before_allocation (is->the_bfd, &link_info))
834 /* xgettext:c-format */
835 einfo (_("Errors encountered processing file %s\n"), is->filename);
840 /* We have seen it all. Allocate it, and carry on */
841 ppc_allocate_toc_section (&link_info);
842 #endif /* TARGET_IS_ppcpe */
844 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
845 /* FIXME: we should be able to set the size of the interworking stub
848 Here we rummage through the found bfds to collect glue
849 information. FIXME: should this be based on a command line
850 option? krk@cygnus.com */
852 LANG_FOR_EACH_INPUT_STATEMENT (is)
854 if (! bfd_arm_pe_process_before_allocation
855 (is->the_bfd, & link_info, support_old_code))
857 /* xgettext:c-format */
858 einfo (_("Errors encountered processing file %s for interworking"),
864 /* We have seen it all. Allocate it, and carry on */
865 bfd_arm_pe_allocate_interworking_sections (& link_info);
866 #endif /* TARGET_IS_armpe */
870 /* This is called when an input file isn't recognized as a BFD. We
871 check here for .DEF files and pull them in automatically. */
874 saw_option(char *option)
877 for (i=0; init[i].ptr; i++)
878 if (strcmp (init[i].symbol, option) == 0)
879 return init[i].inited;
884 gld_${EMULATION_NAME}_unrecognized_file(entry)
885 lang_input_statement_type *entry;
888 const char *ext = entry->filename + strlen (entry->filename) - 4;
890 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
892 if (pe_def_file == 0)
893 pe_def_file = def_file_empty ();
894 def_file_parse (entry->filename, pe_def_file);
897 int i, buflen=0, len;
899 for (i=0; i<pe_def_file->num_exports; i++)
901 len = strlen(pe_def_file->exports[i].internal_name);
905 buf = (char *) xmalloc (buflen);
906 for (i=0; i<pe_def_file->num_exports; i++)
908 struct bfd_link_hash_entry *h;
909 sprintf(buf, "_%s", pe_def_file->exports[i].internal_name);
911 h = bfd_link_hash_lookup (link_info.hash, buf, true, true, true);
912 if (h == (struct bfd_link_hash_entry *) NULL)
913 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
914 if (h->type == bfd_link_hash_new)
916 h->type = bfd_link_hash_undefined;
917 h->u.undef.abfd = NULL;
918 bfd_link_add_undef (link_info.hash, h);
923 /* def_file_print (stdout, pe_def_file); */
924 if (pe_def_file->is_dll == 1)
925 link_info.shared = 1;
927 if (pe_def_file->base_address != (bfd_vma)(-1))
930 pe_data (output_bfd)->pe_opthdr.ImageBase =
931 init[IMAGEBASEOFF].value = pe_def_file->base_address;
932 init[IMAGEBASEOFF].inited = 1;
933 if (image_base_statement)
934 image_base_statement->exp =
935 exp_assop ('=', "__image_base__", exp_intop (pe.ImageBase));
939 /* Not sure if these *should* be set */
940 if (pe_def_file->version_major != -1)
942 pe.MajorImageVersion = pe_def_file->version_major;
943 pe.MinorImageVersion = pe_def_file->version_minor;
946 if (pe_def_file->stack_reserve != -1
947 && ! saw_option ("__size_of_stack_reserve__"))
949 pe.SizeOfStackReserve = pe_def_file->stack_reserve;
950 if (pe_def_file->stack_commit != -1)
951 pe.SizeOfStackCommit = pe_def_file->stack_commit;
953 if (pe_def_file->heap_reserve != -1
954 && ! saw_option ("__size_of_heap_reserve__"))
956 pe.SizeOfHeapReserve = pe_def_file->heap_reserve;
957 if (pe_def_file->heap_commit != -1)
958 pe.SizeOfHeapCommit = pe_def_file->heap_commit;
969 gld_${EMULATION_NAME}_recognized_file(entry)
970 lang_input_statement_type *entry;
973 #ifdef TARGET_IS_i386pe
974 pe_dll_id_target ("pei-i386");
976 #ifdef TARGET_IS_shpe
977 pe_dll_id_target ("pei-shl");
979 #ifdef TARGET_IS_mipspe
980 pe_dll_id_target ("pei-mips");
982 #ifdef TARGET_IS_armpe
983 pe_dll_id_target ("pei-arm-little");
985 if (bfd_get_format (entry->the_bfd) == bfd_object)
987 const char *ext = entry->filename + strlen (entry->filename) - 4;
988 if (strcmp (ext, ".dll") == 0 || strcmp (ext, ".DLL") == 0)
989 return pe_implied_import_dll (entry->filename);
996 gld_${EMULATION_NAME}_finish ()
998 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
999 struct bfd_link_hash_entry * h;
1001 if (thumb_entry_symbol != NULL)
1003 h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol, false, false, true);
1005 if (h != (struct bfd_link_hash_entry *) NULL
1006 && (h->type == bfd_link_hash_defined
1007 || h->type == bfd_link_hash_defweak)
1008 && h->u.def.section->output_section != NULL)
1010 static char buffer[32];
1013 /* Special procesing is required for a Thumb entry symbol. The
1014 bottom bit of its address must be set. */
1015 val = (h->u.def.value
1016 + bfd_get_section_vma (output_bfd,
1017 h->u.def.section->output_section)
1018 + h->u.def.section->output_offset);
1022 /* Now convert this value into a string and store it in entry_symbol
1023 where the lang_finish() function will pick it up. */
1027 sprintf_vma (buffer + 2, val);
1029 if (entry_symbol != NULL && entry_from_cmdline)
1030 einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
1031 thumb_entry_symbol, entry_symbol);
1032 entry_symbol = buffer;
1035 einfo (_("%P: warning: connot find thumb start symbol %s\n"), thumb_entry_symbol);
1037 #endif /* defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe) */
1040 if (link_info.shared)
1042 pe_dll_fill_sections (output_bfd, &link_info);
1043 if (pe_implib_filename)
1044 pe_dll_generate_implib (pe_def_file, pe_implib_filename);
1046 #if defined(TARGET_IS_shpe) || defined(TARGET_IS_mipspe)
1049 pe_exe_fill_sections (output_bfd, &link_info);
1053 if (pe_out_def_filename)
1054 pe_dll_generate_def_file (pe_out_def_filename);
1059 /* Place an orphan section.
1061 We use this to put sections in a reasonable place in the file, and
1062 to ensure that they are aligned as required.
1064 We handle grouped sections here as well. A section named .foo$nn
1065 goes into the output section .foo. All grouped sections are sorted
1068 Grouped sections for the default sections are handled by the
1069 default linker script using wildcards, and are sorted by
1072 static asection *hold_section;
1073 static char *hold_section_name;
1074 static lang_output_section_statement_type *hold_use;
1075 static lang_output_section_statement_type *hold_text;
1076 static lang_output_section_statement_type *hold_rdata;
1077 static lang_output_section_statement_type *hold_data;
1078 static lang_output_section_statement_type *hold_bss;
1080 /* Place an orphan section. We use this to put random SHF_ALLOC
1081 sections in the right segment. */
1085 gld_${EMULATION_NAME}_place_orphan (file, s)
1086 lang_input_statement_type *file;
1089 const char *secname;
1092 if ((s->flags & SEC_ALLOC) == 0)
1095 secname = bfd_get_section_name (s->owner, s);
1097 /* Look through the script to see where to place this section. */
1101 hold_section_name = xstrdup (secname);
1102 if (!link_info.relocateable)
1104 dollar = strchr (hold_section_name, '$');
1110 lang_for_each_statement (gld${EMULATION_NAME}_place_section);
1112 if (hold_use == NULL)
1114 lang_output_section_statement_type *place;
1116 asection *snew, **pps;
1117 lang_statement_list_type *old;
1118 lang_statement_list_type add;
1119 etree_type *address;
1121 /* Try to put the new output section in a reasonable place based
1122 on the section name and section flags. */
1124 if ((s->flags & SEC_HAS_CONTENTS) == 0
1125 && hold_bss != NULL)
1127 else if ((s->flags & SEC_READONLY) == 0
1128 && hold_data != NULL)
1130 else if ((s->flags & SEC_CODE) == 0
1131 && (s->flags & SEC_READONLY) != 0
1132 && hold_rdata != NULL)
1134 else if ((s->flags & SEC_READONLY) != 0
1135 && hold_text != NULL)
1138 /* Choose a unique name for the section. This will be needed if
1139 the same section name appears in the input file with
1140 different loadable or allocateable characteristics. */
1141 outsecname = xstrdup (hold_section_name);
1142 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
1148 len = strlen (outsecname);
1149 newname = xmalloc (len + 5);
1150 strcpy (newname, outsecname);
1154 sprintf (newname + len, "%d", i);
1157 while (bfd_get_section_by_name (output_bfd, newname) != NULL);
1160 outsecname = newname;
1163 /* We don't want to free OUTSECNAME, as it may get attached to
1164 the output section statement. */
1166 /* Create the section in the output file, and put it in the
1167 right place. This shuffling is to make the output file look
1169 snew = bfd_make_section (output_bfd, outsecname);
1171 einfo ("%P%F: output format %s cannot represent section called %s\n",
1172 output_bfd->xvec->name, outsecname);
1173 if (place != NULL && place->bfd_section != NULL)
1175 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1178 snew->next = place->bfd_section->next;
1179 place->bfd_section->next = snew;
1182 /* Start building a list of statements for this section. */
1185 lang_list_init (stat_ptr);
1187 if (link_info.relocateable)
1191 /* All sections in an executable must be aligned to a page
1193 address = exp_unop (ALIGN_K,
1194 exp_nameop (NAME, "__section_alignment__"));
1197 lang_enter_output_section_statement (outsecname, address, 0,
1199 (etree_type *) NULL,
1200 (etree_type *) NULL,
1201 (etree_type *) NULL);
1203 hold_use = lang_output_section_statement_lookup (outsecname);
1205 lang_leave_output_section_statement
1206 ((bfd_vma) 0, "*default*",
1207 (struct lang_output_section_phdr_list *) NULL,
1210 /* Now stick the new statement list right after PLACE. */
1213 *add.tail = place->header.next;
1214 place->header.next = add.head;
1221 wild_doit (&hold_use->children, s, hold_use, file);
1224 lang_statement_union_type **pl;
1225 boolean found_dollar;
1226 lang_statement_list_type list;
1228 /* The section name has a '$'. Sort it with the other '$'
1231 found_dollar = false;
1232 for (pl = &hold_use->children.head; *pl != NULL; pl = &(*pl)->next)
1234 lang_input_section_type *ls;
1237 if ((*pl)->header.type != lang_input_section_enum)
1240 ls = &(*pl)->input_section;
1242 lname = bfd_get_section_name (ls->ifile->the_bfd, ls->section);
1243 if (strchr (lname, '$') == NULL)
1250 found_dollar = true;
1251 if (strcmp (secname, lname) < 0)
1256 lang_list_init (&list);
1257 wild_doit (&list, s, hold_use, file);
1258 if (list.head != NULL)
1260 ASSERT (list.head->next == NULL);
1261 list.head->next = *pl;
1266 free (hold_section_name);
1272 gld${EMULATION_NAME}_place_section (s)
1273 lang_statement_union_type *s;
1275 lang_output_section_statement_type *os;
1277 if (s->header.type != lang_output_section_statement_enum)
1280 os = &s->output_section_statement;
1282 if (strcmp (os->name, hold_section_name) == 0
1283 && os->bfd_section != NULL
1284 && ((hold_section->flags & (SEC_LOAD | SEC_ALLOC))
1285 == (os->bfd_section->flags & (SEC_LOAD | SEC_ALLOC))))
1288 if (strcmp (os->name, ".text") == 0)
1290 else if (strcmp (os->name, ".rdata") == 0)
1292 else if (strcmp (os->name, ".data") == 0)
1294 else if (strcmp (os->name, ".bss") == 0)
1299 gld_${EMULATION_NAME}_find_potential_libraries (name, entry)
1301 lang_input_statement_type * entry;
1303 return ldfile_open_file_search (name, entry, "", ".lib");
1307 gld_${EMULATION_NAME}_get_script(isfile)
1310 # Scripts compiled in.
1311 # sed commands to quote an ld script as a C string.
1312 sc="-f stringify.sed"
1314 cat >>e${EMULATION_NAME}.c <<EOF
1318 if (link_info.relocateable == true && config.build_constructors == true)
1321 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1322 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1323 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1324 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1325 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1326 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1327 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1328 echo ' ; else return' >> e${EMULATION_NAME}.c
1329 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1330 echo '; }' >> e${EMULATION_NAME}.c
1332 cat >>e${EMULATION_NAME}.c <<EOF
1335 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1337 gld_${EMULATION_NAME}_before_parse,
1340 gld_${EMULATION_NAME}_after_parse,
1341 gld_${EMULATION_NAME}_after_open,
1342 after_allocation_default,
1343 set_output_arch_default,
1344 ldemul_default_target,
1345 gld_${EMULATION_NAME}_before_allocation,
1346 gld_${EMULATION_NAME}_get_script,
1347 "${EMULATION_NAME}",
1349 gld_${EMULATION_NAME}_finish, /* finish */
1350 NULL, /* create output section statements */
1351 NULL, /* open dynamic archive */
1352 gld_${EMULATION_NAME}_place_orphan,
1353 gld_${EMULATION_NAME}_set_symbols,
1354 gld_${EMULATION_NAME}_parse_args,
1355 gld_${EMULATION_NAME}_unrecognized_file,
1356 gld_${EMULATION_NAME}_list_options,
1357 gld_${EMULATION_NAME}_recognized_file,
1358 gld_${EMULATION_NAME}_find_potential_libraries