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 char *gld_${EMULATION_NAME}_get_script PARAMS ((int *));
106 static int gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **));
107 static void gld_${EMULATION_NAME}_finish PARAMS ((void));
108 static boolean gld_${EMULATION_NAME}_open_dynamic_archive
109 PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
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 int pe_enable_stdcall_fixup = -1; /* 0=disable 1=enable */
119 static char *pe_out_def_filename = NULL;
120 static char *pe_implib_filename = NULL;
123 extern const char *output_filename;
126 gld_${EMULATION_NAME}_before_parse()
128 output_filename = "${EXECUTABLE_NAME:-a.exe}";
129 ldfile_output_architecture = bfd_arch_${ARCH};
131 config.has_shared = 1;
133 #if (PE_DEF_SUBSYSTEM == 9) || (PE_DEF_SUBSYSTEM == 2)
134 #if defined TARGET_IS_mipspe || defined TARGET_IS_armpe
135 lang_add_entry ("WinMainCRTStartup", 1);
137 lang_add_entry ("_WinMainCRTStartup", 1);
143 /* PE format extra command line options. */
145 /* Used for setting flags in the PE header. */
146 #define OPTION_BASE_FILE (300 + 1)
147 #define OPTION_DLL (OPTION_BASE_FILE + 1)
148 #define OPTION_FILE_ALIGNMENT (OPTION_DLL + 1)
149 #define OPTION_IMAGE_BASE (OPTION_FILE_ALIGNMENT + 1)
150 #define OPTION_MAJOR_IMAGE_VERSION (OPTION_IMAGE_BASE + 1)
151 #define OPTION_MAJOR_OS_VERSION (OPTION_MAJOR_IMAGE_VERSION + 1)
152 #define OPTION_MAJOR_SUBSYSTEM_VERSION (OPTION_MAJOR_OS_VERSION + 1)
153 #define OPTION_MINOR_IMAGE_VERSION (OPTION_MAJOR_SUBSYSTEM_VERSION + 1)
154 #define OPTION_MINOR_OS_VERSION (OPTION_MINOR_IMAGE_VERSION + 1)
155 #define OPTION_MINOR_SUBSYSTEM_VERSION (OPTION_MINOR_OS_VERSION + 1)
156 #define OPTION_SECTION_ALIGNMENT (OPTION_MINOR_SUBSYSTEM_VERSION + 1)
157 #define OPTION_STACK (OPTION_SECTION_ALIGNMENT + 1)
158 #define OPTION_SUBSYSTEM (OPTION_STACK + 1)
159 #define OPTION_HEAP (OPTION_SUBSYSTEM + 1)
160 #define OPTION_SUPPORT_OLD_CODE (OPTION_HEAP + 1)
161 #define OPTION_OUT_DEF (OPTION_SUPPORT_OLD_CODE + 1)
162 #define OPTION_EXPORT_ALL (OPTION_OUT_DEF + 1)
163 #define OPTION_EXCLUDE_SYMBOLS (OPTION_EXPORT_ALL + 1)
164 #define OPTION_KILL_ATS (OPTION_EXCLUDE_SYMBOLS + 1)
165 #define OPTION_STDCALL_ALIASES (OPTION_KILL_ATS + 1)
166 #define OPTION_ENABLE_STDCALL_FIXUP (OPTION_STDCALL_ALIASES + 1)
167 #define OPTION_DISABLE_STDCALL_FIXUP (OPTION_ENABLE_STDCALL_FIXUP + 1)
168 #define OPTION_IMPLIB_FILENAME (OPTION_DISABLE_STDCALL_FIXUP + 1)
169 #define OPTION_THUMB_ENTRY (OPTION_IMPLIB_FILENAME + 1)
170 #define OPTION_WARN_DUPLICATE_EXPORTS (OPTION_THUMB_ENTRY + 1)
171 #define OPTION_IMP_COMPAT (OPTION_WARN_DUPLICATE_EXPORTS + 1)
173 static struct option longopts[] =
176 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
177 {"dll", no_argument, NULL, OPTION_DLL},
178 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
179 {"heap", required_argument, NULL, OPTION_HEAP},
180 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
181 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
182 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
183 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
184 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
185 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
186 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
187 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
188 {"stack", required_argument, NULL, OPTION_STACK},
189 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
190 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
191 {"thumb-entry", required_argument, NULL, OPTION_THUMB_ENTRY},
193 /* getopt allows abbreviations, so we do this to stop it from treating -o
194 as an abbreviation for this option */
195 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
196 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
197 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
198 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
199 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
200 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
201 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
202 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
203 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
204 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
205 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
207 {NULL, no_argument, NULL, 0}
211 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
212 parameters which may be input from the command line */
223 #define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol,0}
225 static definfo init[] =
227 /* imagebase must be first */
228 #define IMAGEBASEOFF 0
229 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
231 {&dll, sizeof(dll), 0, "__dll__", 0},
232 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
233 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
234 D(MajorOperatingSystemVersion,"__major_os_version__", 4),
235 D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
236 D(MajorImageVersion,"__major_image_version__", 1),
237 D(MinorImageVersion,"__minor_image_version__", 0),
238 #ifdef TARGET_IS_armpe
239 D(MajorSubsystemVersion,"__major_subsystem_version__", 2),
241 D(MajorSubsystemVersion,"__major_subsystem_version__", 4),
243 D(MinorSubsystemVersion,"__minor_subsystem_version__", 0),
244 D(Subsystem,"__subsystem__", ${SUBSYSTEM}),
245 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x2000000),
246 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
247 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
248 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
249 D(LoaderFlags,"__loader_flags__", 0x0),
250 { NULL, 0, 0, NULL, 0 }
254 gld_${EMULATION_NAME}_list_options (file)
257 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
258 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
259 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
260 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
261 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
262 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
263 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
264 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
265 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
266 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
267 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
268 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
269 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
270 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
271 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
272 fprintf (file, _(" --thumb-entry=<symbol> Set the entry point to be Thumb <symbol>\n"));
274 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
275 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
276 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
277 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
278 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
279 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
280 fprintf (file, _(" --out-implib <file> Generate import library\n"));
281 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
282 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
283 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n"));
284 fprintf (file, _(" create __imp_<SYMBOL> as well.\n"));
289 set_pe_name (name, val)
294 /* Find the name and set it. */
295 for (i = 0; init[i].ptr; i++)
297 if (strcmp (name, init[i].symbol) == 0)
322 { "native", 1, "NtProcessStartup" },
323 #if defined TARGET_IS_mipspe || defined TARGET_IS_armpe
324 { "windows", 2, "WinMainCRTStartup" },
326 { "windows", 2, "WinMainCRTStartup" },
328 { "console", 3, "mainCRTStartup" },
330 /* The Microsoft linker does not recognize this. */
333 { "posix", 7, "__PosixProcessStartup"},
334 { "wince", 9, "_WinMainCRTStartup" },
338 sver = strchr (optarg, ':');
340 len = strlen (optarg);
346 set_pe_name ("__major_subsystem_version__",
347 strtoul (sver + 1, &end, 0));
349 set_pe_name ("__minor_subsystem_version__",
350 strtoul (end + 1, &end, 0));
352 einfo (_("%P: warning: bad version number in -subsystem option\n"));
355 for (i = 0; v[i].name; i++)
357 if (strncmp (optarg, v[i].name, len) == 0
358 && v[i].name[len] == '\0')
360 const char *initial_symbol_char;
363 set_pe_name ("__subsystem__", v[i].value);
365 initial_symbol_char = ${INITIAL_SYMBOL_CHAR};
366 if (*initial_symbol_char == '\0')
372 /* lang_add_entry expects its argument to be permanently
373 allocated, so we don't free this string. */
374 alc_entry = xmalloc (strlen (initial_symbol_char)
375 + strlen (v[i].entry)
377 strcpy (alc_entry, initial_symbol_char);
378 strcat (alc_entry, v[i].entry);
382 lang_add_entry (entry, 1);
388 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
400 set_pe_name (name, strtoul (optarg, &end, 0));
403 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
409 set_pe_stack_heap (resname, comname)
413 set_pe_value (resname);
418 set_pe_value (comname);
421 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
427 gld_${EMULATION_NAME}_parse_args(argc, argv)
433 int prevoptind = optind;
434 int prevopterr = opterr;
436 static int lastoptind = -1;
438 if (lastoptind != optind)
444 optc = getopt_long_only (argc, argv, "-", longopts, &longind);
455 case OPTION_BASE_FILE:
456 link_info.base_file = (PTR) fopen (optarg, FOPEN_WB);
457 if (link_info.base_file == NULL)
459 /* xgettext:c-format */
460 fprintf (stderr, _("%s: Can't open base file %s\n"),
461 program_name, optarg);
468 set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
471 set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
473 case OPTION_SUBSYSTEM:
476 case OPTION_MAJOR_OS_VERSION:
477 set_pe_value ("__major_os_version__");
479 case OPTION_MINOR_OS_VERSION:
480 set_pe_value ("__minor_os_version__");
482 case OPTION_MAJOR_SUBSYSTEM_VERSION:
483 set_pe_value ("__major_subsystem_version__");
485 case OPTION_MINOR_SUBSYSTEM_VERSION:
486 set_pe_value ("__minor_subsystem_version__");
488 case OPTION_MAJOR_IMAGE_VERSION:
489 set_pe_value ("__major_image_version__");
491 case OPTION_MINOR_IMAGE_VERSION:
492 set_pe_value ("__minor_image_version__");
494 case OPTION_FILE_ALIGNMENT:
495 set_pe_value ("__file_alignment__");
497 case OPTION_SECTION_ALIGNMENT:
498 set_pe_value ("__section_alignment__");
501 set_pe_name ("__dll__", 1);
503 case OPTION_IMAGE_BASE:
504 set_pe_value ("__image_base__");
506 case OPTION_SUPPORT_OLD_CODE:
507 support_old_code = 1;
509 case OPTION_THUMB_ENTRY:
510 thumb_entry_symbol = optarg;
514 pe_out_def_filename = xstrdup (optarg);
516 case OPTION_EXPORT_ALL:
517 pe_dll_export_everything = 1;
519 case OPTION_EXCLUDE_SYMBOLS:
520 pe_dll_add_excludes (optarg);
522 case OPTION_KILL_ATS:
525 case OPTION_STDCALL_ALIASES:
526 pe_dll_stdcall_aliases = 1;
528 case OPTION_ENABLE_STDCALL_FIXUP:
529 pe_enable_stdcall_fixup = 1;
531 case OPTION_DISABLE_STDCALL_FIXUP:
532 pe_enable_stdcall_fixup = 0;
534 case OPTION_IMPLIB_FILENAME:
535 pe_implib_filename = xstrdup (optarg);
537 case OPTION_WARN_DUPLICATE_EXPORTS:
538 pe_dll_warn_dup_exports = 1;
540 case OPTION_IMP_COMPAT:
541 pe_dll_compat_implib = 1;
548 /* Assign values to the special symbols before the linker script is
552 gld_${EMULATION_NAME}_set_symbols ()
554 /* Run through and invent symbols for all the
555 names and insert the defaults. */
557 lang_statement_list_type *save;
559 if (!init[IMAGEBASEOFF].inited)
561 if (link_info.relocateable)
562 init[IMAGEBASEOFF].value = 0;
563 else if (init[DLLOFF].value || link_info.shared)
564 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
566 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
569 /* Don't do any symbol assignments if this is a relocateable link. */
570 if (link_info.relocateable)
573 /* Glue the assignments into the abs section */
576 stat_ptr = &(abs_output_section->children);
578 for (j = 0; init[j].ptr; j++)
580 long val = init[j].value;
581 lang_assignment_statement_type *rv;
582 rv = lang_add_assignment (exp_assop ('=' ,init[j].symbol, exp_intop (val)));
583 if (init[j].size == sizeof(short))
584 *(short *)init[j].ptr = val;
585 else if (init[j].size == sizeof(int))
586 *(int *)init[j].ptr = val;
587 else if (init[j].size == sizeof(long))
588 *(long *)init[j].ptr = val;
589 /* This might be a long long or other special type. */
590 else if (init[j].size == sizeof(bfd_vma))
591 *(bfd_vma *)init[j].ptr = val;
593 if (j == IMAGEBASEOFF)
594 image_base_statement = rv;
596 /* Restore the pointer. */
599 if (pe.FileAlignment >
602 einfo (_("%P: warning, file alignment > section alignment.\n"));
606 /* This is called after the linker script and the command line options
610 gld_${EMULATION_NAME}_after_parse ()
612 /* The Windows libraries are designed for the linker to treat the
613 entry point as an undefined symbol. Otherwise, the .obj that
614 defines mainCRTStartup is brought in because it is the first
615 encountered in libc.lib and it has other symbols in it which will
616 be pulled in by the link process. To avoid this, we act as
617 though the user specified -u with the entry point symbol.
619 This function is called after the linker script and command line
620 options have been read, so at this point we know the right entry
621 point. This function is called before the input files are
622 opened, so registering the symbol as undefined will make a
625 if (! link_info.relocateable && entry_symbol != NULL)
626 ldlang_add_undef (entry_symbol);
629 static struct bfd_link_hash_entry *pe_undef_found_sym;
632 pe_undef_cdecl_match (h, string)
633 struct bfd_link_hash_entry *h;
636 int sl = strlen (string);
637 if (h->type == bfd_link_hash_defined
638 && strncmp (h->root.string, string, sl) == 0
639 && h->root.string[sl] == '@')
641 pe_undef_found_sym = h;
651 static int gave_warning_message = 0;
652 struct bfd_link_hash_entry *undef, *sym;
654 for (undef = link_info.hash->undefs; undef; undef=undef->next)
655 if (undef->type == bfd_link_hash_undefined)
657 at = strchr (undef->root.string, '@');
660 /* The symbol is a stdcall symbol, so let's look for a cdecl
661 symbol with the same name and resolve to that */
662 char *cname = xstrdup (undef->root.string);
663 at = strchr (cname, '@');
665 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
666 if (sym && sym->type == bfd_link_hash_defined)
668 undef->type = bfd_link_hash_defined;
669 undef->u.def.value = sym->u.def.value;
670 undef->u.def.section = sym->u.def.section;
671 if (pe_enable_stdcall_fixup == -1)
673 einfo (_("Warning: resolving %s by linking to %s\n"),
674 undef->root.string, cname);
675 if (! gave_warning_message)
677 gave_warning_message = 1;
678 einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
679 einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
686 /* The symbol is a cdecl symbol, so we look for stdcall
687 symbols - which means scanning the whole symbol table */
688 pe_undef_found_sym = 0;
689 bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
690 (PTR) undef->root.string);
691 sym = pe_undef_found_sym;
694 undef->type = bfd_link_hash_defined;
695 undef->u.def.value = sym->u.def.value;
696 undef->u.def.section = sym->u.def.section;
697 if (pe_enable_stdcall_fixup == -1)
699 einfo (_("Warning: resolving %s by linking to %s\n"),
700 undef->root.string, sym->root.string);
701 if (! gave_warning_message)
703 gave_warning_message = 1;
704 einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
705 einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
712 #endif /* DLL_SUPPORT */
715 gld_${EMULATION_NAME}_after_open ()
717 /* Pass the wacky PE command line options into the output bfd.
718 FIXME: This should be done via a function, rather than by
719 including an internal BFD header. */
721 if (!coff_data (output_bfd)->pe)
722 einfo (_("%F%P: PE operations on non PE file.\n"));
724 pe_data (output_bfd)->pe_opthdr = pe;
725 pe_data (output_bfd)->dll = init[DLLOFF].value;
728 if (pe_enable_stdcall_fixup) /* -1=warn or 1=disable */
729 pe_fixup_stdcalls ();
731 pe_process_import_defs(output_bfd, &link_info);
732 if (link_info.shared)
733 pe_dll_build_sections (output_bfd, &link_info);
735 #ifndef TARGET_IS_i386pe
736 #ifndef TARGET_IS_armpe
738 pe_exe_build_sections (output_bfd, &link_info);
743 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
744 if (strstr (bfd_get_target (output_bfd), "arm") == NULL)
746 /* The arm backend needs special fields in the output hash structure.
747 These will only be created if the output format is an arm format,
748 hence we do not support linking and changing output formats at the
749 same time. Use a link followed by objcopy to change output formats. */
750 einfo ("%F%X%P: error: cannot change output format whilst linking ARM binaries\n");
754 /* Find a BFD that can hold the interworking stubs. */
755 LANG_FOR_EACH_INPUT_STATEMENT (is)
757 if (bfd_arm_pe_get_bfd_for_interworking (is->the_bfd, & link_info))
766 lang_input_statement_type *is2;
768 /* Careful - this is a shell script. Watch those dollar signs! */
769 /* Microsoft import libraries have every member named the same,
770 and not in the right order for us to link them correctly. We
771 must detect these and rename the members so that they'll link
772 correctly. There are three types of objects: the head, the
773 thunks, and the sentinel(s). The head is easy; it's the one
774 with idata2. We assume that the sentinels won't have relocs,
775 and the thunks will. It's easier than checking the symbol
776 table for external references. */
777 LANG_FOR_EACH_INPUT_STATEMENT (is)
779 if (is->the_bfd->my_archive)
781 bfd *arch = is->the_bfd->my_archive;
782 if (cur_arch != arch)
787 is2 && is2->the_bfd->my_archive == arch;
788 is2 = (lang_input_statement_type *)is2->next)
790 if (strcmp (is->the_bfd->filename, is2->the_bfd->filename))
797 int idata2 = 0, reloc_count=0;
801 for (sec = is->the_bfd->sections; sec; sec = sec->next)
803 if (strcmp (sec->name, ".idata\$2") == 0)
805 reloc_count += sec->reloc_count;
808 if (idata2) /* .idata2 is the TOC */
810 else if (reloc_count > 0) /* thunks */
815 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
816 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
817 is->the_bfd->filename = new_name;
819 new_name = xmalloc (strlen (is->filename) + 3);
820 sprintf (new_name, "%s.%c", is->filename, seq);
821 is->filename = new_name;
829 gld_${EMULATION_NAME}_before_allocation()
831 #ifdef TARGET_IS_ppcpe
832 /* Here we rummage through the found bfds to collect toc information */
834 LANG_FOR_EACH_INPUT_STATEMENT (is)
836 if (!ppc_process_before_allocation (is->the_bfd, &link_info))
838 /* xgettext:c-format */
839 einfo (_("Errors encountered processing file %s\n"), is->filename);
844 /* We have seen it all. Allocate it, and carry on */
845 ppc_allocate_toc_section (&link_info);
846 #endif /* TARGET_IS_ppcpe */
848 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
849 /* FIXME: we should be able to set the size of the interworking stub
852 Here we rummage through the found bfds to collect glue
853 information. FIXME: should this be based on a command line
854 option? krk@cygnus.com */
856 LANG_FOR_EACH_INPUT_STATEMENT (is)
858 if (! bfd_arm_pe_process_before_allocation
859 (is->the_bfd, & link_info, support_old_code))
861 /* xgettext:c-format */
862 einfo (_("Errors encountered processing file %s for interworking"),
868 /* We have seen it all. Allocate it, and carry on */
869 bfd_arm_pe_allocate_interworking_sections (& link_info);
870 #endif /* TARGET_IS_armpe */
874 /* This is called when an input file isn't recognized as a BFD. We
875 check here for .DEF files and pull them in automatically. */
878 saw_option(char *option)
881 for (i=0; init[i].ptr; i++)
882 if (strcmp (init[i].symbol, option) == 0)
883 return init[i].inited;
886 #endif /* DLL_SUPPORT */
889 gld_${EMULATION_NAME}_unrecognized_file(entry)
890 lang_input_statement_type *entry ATTRIBUTE_UNUSED;
893 const char *ext = entry->filename + strlen (entry->filename) - 4;
895 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
897 if (pe_def_file == 0)
898 pe_def_file = def_file_empty ();
899 def_file_parse (entry->filename, pe_def_file);
902 int i, buflen=0, len;
904 for (i=0; i<pe_def_file->num_exports; i++)
906 len = strlen(pe_def_file->exports[i].internal_name);
910 buf = (char *) xmalloc (buflen);
911 for (i=0; i<pe_def_file->num_exports; i++)
913 struct bfd_link_hash_entry *h;
914 sprintf(buf, "_%s", pe_def_file->exports[i].internal_name);
916 h = bfd_link_hash_lookup (link_info.hash, buf, true, true, true);
917 if (h == (struct bfd_link_hash_entry *) NULL)
918 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
919 if (h->type == bfd_link_hash_new)
921 h->type = bfd_link_hash_undefined;
922 h->u.undef.abfd = NULL;
923 bfd_link_add_undef (link_info.hash, h);
928 /* def_file_print (stdout, pe_def_file); */
929 if (pe_def_file->is_dll == 1)
930 link_info.shared = 1;
932 if (pe_def_file->base_address != (bfd_vma)(-1))
935 pe_data (output_bfd)->pe_opthdr.ImageBase =
936 init[IMAGEBASEOFF].value = pe_def_file->base_address;
937 init[IMAGEBASEOFF].inited = 1;
938 if (image_base_statement)
939 image_base_statement->exp =
940 exp_assop ('=', "__image_base__", exp_intop (pe.ImageBase));
944 /* Not sure if these *should* be set */
945 if (pe_def_file->version_major != -1)
947 pe.MajorImageVersion = pe_def_file->version_major;
948 pe.MinorImageVersion = pe_def_file->version_minor;
951 if (pe_def_file->stack_reserve != -1
952 && ! saw_option ("__size_of_stack_reserve__"))
954 pe.SizeOfStackReserve = pe_def_file->stack_reserve;
955 if (pe_def_file->stack_commit != -1)
956 pe.SizeOfStackCommit = pe_def_file->stack_commit;
958 if (pe_def_file->heap_reserve != -1
959 && ! saw_option ("__size_of_heap_reserve__"))
961 pe.SizeOfHeapReserve = pe_def_file->heap_reserve;
962 if (pe_def_file->heap_commit != -1)
963 pe.SizeOfHeapCommit = pe_def_file->heap_commit;
974 gld_${EMULATION_NAME}_recognized_file(entry)
975 lang_input_statement_type *entry ATTRIBUTE_UNUSED;
978 #ifdef TARGET_IS_i386pe
979 pe_dll_id_target ("pei-i386");
981 #ifdef TARGET_IS_shpe
982 pe_dll_id_target ("pei-shl");
984 #ifdef TARGET_IS_mipspe
985 pe_dll_id_target ("pei-mips");
987 #ifdef TARGET_IS_armpe
988 pe_dll_id_target ("pei-arm-little");
990 if (bfd_get_format (entry->the_bfd) == bfd_object)
992 const char *ext = entry->filename + strlen (entry->filename) - 4;
993 if (strcmp (ext, ".dll") == 0 || strcmp (ext, ".DLL") == 0)
994 return pe_implied_import_dll (entry->filename);
1001 gld_${EMULATION_NAME}_finish ()
1003 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
1004 struct bfd_link_hash_entry * h;
1006 if (thumb_entry_symbol != NULL)
1008 h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol, false, false, true);
1010 if (h != (struct bfd_link_hash_entry *) NULL
1011 && (h->type == bfd_link_hash_defined
1012 || h->type == bfd_link_hash_defweak)
1013 && h->u.def.section->output_section != NULL)
1015 static char buffer[32];
1018 /* Special procesing is required for a Thumb entry symbol. The
1019 bottom bit of its address must be set. */
1020 val = (h->u.def.value
1021 + bfd_get_section_vma (output_bfd,
1022 h->u.def.section->output_section)
1023 + h->u.def.section->output_offset);
1027 /* Now convert this value into a string and store it in entry_symbol
1028 where the lang_finish() function will pick it up. */
1032 sprintf_vma (buffer + 2, val);
1034 if (entry_symbol != NULL && entry_from_cmdline)
1035 einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
1036 thumb_entry_symbol, entry_symbol);
1037 entry_symbol = buffer;
1040 einfo (_("%P: warning: connot find thumb start symbol %s\n"), thumb_entry_symbol);
1042 #endif /* defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe) */
1045 if (link_info.shared)
1047 pe_dll_fill_sections (output_bfd, &link_info);
1048 if (pe_implib_filename)
1049 pe_dll_generate_implib (pe_def_file, pe_implib_filename);
1051 #if defined(TARGET_IS_shpe) || defined(TARGET_IS_mipspe)
1052 /* ARM doesn't need relocs. */
1055 pe_exe_fill_sections (output_bfd, &link_info);
1059 if (pe_out_def_filename)
1060 pe_dll_generate_def_file (pe_out_def_filename);
1061 #endif /* DLL_SUPPORT */
1065 /* Place an orphan section.
1067 We use this to put sections in a reasonable place in the file, and
1068 to ensure that they are aligned as required.
1070 We handle grouped sections here as well. A section named .foo$nn
1071 goes into the output section .foo. All grouped sections are sorted
1074 Grouped sections for the default sections are handled by the
1075 default linker script using wildcards, and are sorted by
1080 lang_output_section_statement_type *os;
1082 lang_statement_union_type **stmt;
1087 gld_${EMULATION_NAME}_place_orphan (file, s)
1088 lang_input_statement_type *file;
1091 const char *secname;
1092 char *hold_section_name;
1093 char *dollar = NULL;
1094 lang_output_section_statement_type *os;
1095 lang_statement_list_type add_child;
1097 secname = bfd_get_section_name (s->owner, s);
1099 /* 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, '$');
1109 os = lang_output_section_find (hold_section_name);
1111 lang_list_init (&add_child);
1114 && os->bfd_section != NULL
1115 && ((s->flags ^ os->bfd_section->flags) & (SEC_LOAD | SEC_ALLOC)) == 0)
1117 wild_doit (&add_child, s, os, file);
1121 struct orphan_save *place;
1122 static struct orphan_save hold_text;
1123 static struct orphan_save hold_rdata;
1124 static struct orphan_save hold_data;
1125 static struct orphan_save hold_bss;
1127 lang_statement_list_type *old;
1128 lang_statement_list_type add;
1129 etree_type *address;
1131 /* Try to put the new output section in a reasonable place based
1132 on the section name and section flags. */
1133 #define HAVE_SECTION(hold, name) \
1134 (hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
1137 if ((s->flags & SEC_ALLOC) == 0)
1139 else if ((s->flags & SEC_HAS_CONTENTS) == 0
1140 && HAVE_SECTION (hold_bss, ".bss"))
1142 else if ((s->flags & SEC_READONLY) == 0
1143 && HAVE_SECTION (hold_data, ".data"))
1145 else if ((s->flags & SEC_CODE) == 0
1146 && (s->flags & SEC_READONLY) != 0
1147 && HAVE_SECTION (hold_rdata, ".rdata"))
1148 place = &hold_rdata;
1149 else if ((s->flags & SEC_READONLY) != 0
1150 && HAVE_SECTION (hold_text, ".text"))
1155 /* Choose a unique name for the section. This will be needed if
1156 the same section name appears in the input file with
1157 different loadable or allocateable characteristics. */
1158 outsecname = xstrdup (hold_section_name);
1159 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
1165 len = strlen (outsecname);
1166 newname = xmalloc (len + 5);
1167 strcpy (newname, outsecname);
1171 sprintf (newname + len, "%d", i);
1174 while (bfd_get_section_by_name (output_bfd, newname) != NULL);
1177 outsecname = newname;
1180 /* Start building a list of statements for this section. */
1183 lang_list_init (stat_ptr);
1185 if (link_info.relocateable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1186 address = exp_intop ((bfd_vma) 0);
1189 /* All sections in an executable must be aligned to a page
1191 address = exp_unop (ALIGN_K,
1192 exp_nameop (NAME, "__section_alignment__"));
1195 os = lang_enter_output_section_statement (outsecname, address, 0,
1197 (etree_type *) NULL,
1198 (etree_type *) NULL,
1199 (etree_type *) NULL);
1201 wild_doit (&add_child, s, os, file);
1203 lang_leave_output_section_statement
1204 ((bfd_vma) 0, "*default*",
1205 (struct lang_output_section_phdr_list *) NULL, "*default*");
1211 asection *snew, **pps;
1213 snew = os->bfd_section;
1214 if (place->os->bfd_section != NULL || place->section != NULL)
1216 /* Shuffle the section to make the output file look neater. */
1217 if (place->section == NULL)
1220 /* Finding the end of the list is a little tricky. We
1221 make a wild stab at it by comparing section flags. */
1222 flagword first_flags = place->os->bfd_section->flags;
1223 for (pps = &place->os->bfd_section->next;
1224 *pps != NULL && (*pps)->flags == first_flags;
1225 pps = &(*pps)->next)
1227 place->section = pps;
1229 /* Put orphans after the first section on the list. */
1230 place->section = &place->os->bfd_section->next;
1234 /* Unlink the section. */
1235 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1239 /* Now tack it on to the "place->os" section list. */
1240 snew->next = *place->section;
1241 *place->section = snew;
1243 place->section = &snew->next; /* Save the end of this list. */
1245 if (place->stmt == NULL)
1247 /* Put the new statement list right at the head. */
1248 *add.tail = place->os->header.next;
1249 place->os->header.next = add.head;
1253 /* Put it after the last orphan statement we added. */
1254 *add.tail = *place->stmt;
1255 *place->stmt = add.head;
1257 place->stmt = add.tail; /* Save the end of this list. */
1262 lang_statement_union_type **pl = &os->children.head;
1266 boolean found_dollar;
1268 /* The section name has a '$'. Sort it with the other '$'
1271 found_dollar = false;
1272 for ( ; *pl != NULL; pl = &(*pl)->next)
1274 lang_input_section_type *ls;
1277 if ((*pl)->header.type != lang_input_section_enum)
1280 ls = &(*pl)->input_section;
1282 lname = bfd_get_section_name (ls->ifile->the_bfd, ls->section);
1283 if (strchr (lname, '$') == NULL)
1290 found_dollar = true;
1291 if (strcmp (secname, lname) < 0)
1297 if (add_child.head != NULL)
1299 add_child.head->next = *pl;
1300 *pl = add_child.head;
1304 free (hold_section_name);
1310 gld_${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
1312 search_dirs_type * search;
1313 lang_input_statement_type * entry;
1315 const char * filename;
1318 if (! entry->is_archive)
1321 filename = entry->filename;
1323 string = (char *) xmalloc (strlen (search->name)
1328 /* Try "foo.dll" first. */
1329 sprintf (string, "%s/%s.dll", search->name, filename);
1331 if (! ldfile_try_open_bfd (string, entry))
1333 /* Try "libfoo.dll" next. */
1334 sprintf (string, "%s/lib%s.dll", search->name, filename);
1336 if (! ldfile_try_open_bfd (string, entry))
1343 entry->filename = string;
1349 gld_${EMULATION_NAME}_find_potential_libraries (name, entry)
1351 lang_input_statement_type * entry;
1353 return ldfile_open_file_search (name, entry, "", ".lib");
1357 gld_${EMULATION_NAME}_get_script(isfile)
1360 # Scripts compiled in.
1361 # sed commands to quote an ld script as a C string.
1362 sc="-f stringify.sed"
1364 cat >>e${EMULATION_NAME}.c <<EOF
1368 if (link_info.relocateable == true && config.build_constructors == true)
1371 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1372 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1373 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1374 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1375 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1376 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1377 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1378 echo ' ; else return' >> e${EMULATION_NAME}.c
1379 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1380 echo '; }' >> e${EMULATION_NAME}.c
1382 cat >>e${EMULATION_NAME}.c <<EOF
1385 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1387 gld_${EMULATION_NAME}_before_parse,
1390 gld_${EMULATION_NAME}_after_parse,
1391 gld_${EMULATION_NAME}_after_open,
1392 after_allocation_default,
1393 set_output_arch_default,
1394 ldemul_default_target,
1395 gld_${EMULATION_NAME}_before_allocation,
1396 gld_${EMULATION_NAME}_get_script,
1397 "${EMULATION_NAME}",
1399 gld_${EMULATION_NAME}_finish, /* finish */
1400 NULL, /* create output section statements */
1401 gld_${EMULATION_NAME}_open_dynamic_archive,
1402 gld_${EMULATION_NAME}_place_orphan,
1403 gld_${EMULATION_NAME}_set_symbols,
1404 gld_${EMULATION_NAME}_parse_args,
1405 gld_${EMULATION_NAME}_unrecognized_file,
1406 gld_${EMULATION_NAME}_list_options,
1407 gld_${EMULATION_NAME}_recognized_file,
1408 gld_${EMULATION_NAME}_find_potential_libraries