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;
121 static int pe_enable_auto_image_base = 0;
124 extern const char *output_filename;
127 gld_${EMULATION_NAME}_before_parse()
129 output_filename = "${EXECUTABLE_NAME:-a.exe}";
130 ldfile_output_architecture = bfd_arch_${ARCH};
132 config.has_shared = 1;
134 #if (PE_DEF_SUBSYSTEM == 9) || (PE_DEF_SUBSYSTEM == 2)
135 #if defined TARGET_IS_mipspe || defined TARGET_IS_armpe
136 lang_add_entry ("WinMainCRTStartup", 1);
138 lang_add_entry ("_WinMainCRTStartup", 1);
144 /* PE format extra command line options. */
146 /* Used for setting flags in the PE header. */
147 #define OPTION_BASE_FILE (300 + 1)
148 #define OPTION_DLL (OPTION_BASE_FILE + 1)
149 #define OPTION_FILE_ALIGNMENT (OPTION_DLL + 1)
150 #define OPTION_IMAGE_BASE (OPTION_FILE_ALIGNMENT + 1)
151 #define OPTION_MAJOR_IMAGE_VERSION (OPTION_IMAGE_BASE + 1)
152 #define OPTION_MAJOR_OS_VERSION (OPTION_MAJOR_IMAGE_VERSION + 1)
153 #define OPTION_MAJOR_SUBSYSTEM_VERSION (OPTION_MAJOR_OS_VERSION + 1)
154 #define OPTION_MINOR_IMAGE_VERSION (OPTION_MAJOR_SUBSYSTEM_VERSION + 1)
155 #define OPTION_MINOR_OS_VERSION (OPTION_MINOR_IMAGE_VERSION + 1)
156 #define OPTION_MINOR_SUBSYSTEM_VERSION (OPTION_MINOR_OS_VERSION + 1)
157 #define OPTION_SECTION_ALIGNMENT (OPTION_MINOR_SUBSYSTEM_VERSION + 1)
158 #define OPTION_STACK (OPTION_SECTION_ALIGNMENT + 1)
159 #define OPTION_SUBSYSTEM (OPTION_STACK + 1)
160 #define OPTION_HEAP (OPTION_SUBSYSTEM + 1)
161 #define OPTION_SUPPORT_OLD_CODE (OPTION_HEAP + 1)
162 #define OPTION_OUT_DEF (OPTION_SUPPORT_OLD_CODE + 1)
163 #define OPTION_EXPORT_ALL (OPTION_OUT_DEF + 1)
164 #define OPTION_EXCLUDE_SYMBOLS (OPTION_EXPORT_ALL + 1)
165 #define OPTION_KILL_ATS (OPTION_EXCLUDE_SYMBOLS + 1)
166 #define OPTION_STDCALL_ALIASES (OPTION_KILL_ATS + 1)
167 #define OPTION_ENABLE_STDCALL_FIXUP (OPTION_STDCALL_ALIASES + 1)
168 #define OPTION_DISABLE_STDCALL_FIXUP (OPTION_ENABLE_STDCALL_FIXUP + 1)
169 #define OPTION_IMPLIB_FILENAME (OPTION_DISABLE_STDCALL_FIXUP + 1)
170 #define OPTION_THUMB_ENTRY (OPTION_IMPLIB_FILENAME + 1)
171 #define OPTION_WARN_DUPLICATE_EXPORTS (OPTION_THUMB_ENTRY + 1)
172 #define OPTION_IMP_COMPAT (OPTION_WARN_DUPLICATE_EXPORTS + 1)
173 #define OPTION_ENABLE_AUTO_IMAGE_BASE (OPTION_IMP_COMPAT + 1)
174 #define OPTION_DISABLE_AUTO_IMAGE_BASE (OPTION_ENABLE_AUTO_IMAGE_BASE + 1)
176 static struct option longopts[] =
179 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
180 {"dll", no_argument, NULL, OPTION_DLL},
181 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
182 {"heap", required_argument, NULL, OPTION_HEAP},
183 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
184 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
185 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
186 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
187 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
188 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
189 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
190 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
191 {"stack", required_argument, NULL, OPTION_STACK},
192 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
193 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
194 {"thumb-entry", required_argument, NULL, OPTION_THUMB_ENTRY},
196 /* getopt allows abbreviations, so we do this to stop it from treating -o
197 as an abbreviation for this option */
198 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
199 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
200 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
201 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
202 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
203 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
204 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
205 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
206 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
207 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
208 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
209 {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
210 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
212 {NULL, no_argument, NULL, 0}
216 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
217 parameters which may be input from the command line */
228 #define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol,0}
230 static definfo init[] =
232 /* imagebase must be first */
233 #define IMAGEBASEOFF 0
234 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
236 {&dll, sizeof(dll), 0, "__dll__", 0},
237 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
238 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
239 D(MajorOperatingSystemVersion,"__major_os_version__", 4),
240 D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
241 D(MajorImageVersion,"__major_image_version__", 1),
242 D(MinorImageVersion,"__minor_image_version__", 0),
243 #ifdef TARGET_IS_armpe
244 D(MajorSubsystemVersion,"__major_subsystem_version__", 2),
246 D(MajorSubsystemVersion,"__major_subsystem_version__", 4),
248 D(MinorSubsystemVersion,"__minor_subsystem_version__", 0),
249 D(Subsystem,"__subsystem__", ${SUBSYSTEM}),
250 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x2000000),
251 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
252 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
253 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
254 D(LoaderFlags,"__loader_flags__", 0x0),
255 { NULL, 0, 0, NULL, 0 }
259 gld_${EMULATION_NAME}_list_options (file)
262 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
263 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
264 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
265 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
266 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
267 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
268 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
269 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
270 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
271 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
272 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
273 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
274 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
275 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
276 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
277 fprintf (file, _(" --thumb-entry=<symbol> Set the entry point to be Thumb <symbol>\n"));
279 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
280 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
281 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
282 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
283 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
284 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
285 fprintf (file, _(" --out-implib <file> Generate import library\n"));
286 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
287 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
288 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n"));
289 fprintf (file, _(" create __imp_<SYMBOL> as well.\n"));
290 fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n"));
291 fprintf (file, _(" unless user specifies one\n"));
292 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
297 set_pe_name (name, val)
302 /* Find the name and set it. */
303 for (i = 0; init[i].ptr; i++)
305 if (strcmp (name, init[i].symbol) == 0)
330 { "native", 1, "NtProcessStartup" },
331 #if defined TARGET_IS_mipspe || defined TARGET_IS_armpe
332 { "windows", 2, "WinMainCRTStartup" },
334 { "windows", 2, "WinMainCRTStartup" },
336 { "console", 3, "mainCRTStartup" },
338 /* The Microsoft linker does not recognize this. */
341 { "posix", 7, "__PosixProcessStartup"},
342 { "wince", 9, "_WinMainCRTStartup" },
346 sver = strchr (optarg, ':');
348 len = strlen (optarg);
354 set_pe_name ("__major_subsystem_version__",
355 strtoul (sver + 1, &end, 0));
357 set_pe_name ("__minor_subsystem_version__",
358 strtoul (end + 1, &end, 0));
360 einfo (_("%P: warning: bad version number in -subsystem option\n"));
363 for (i = 0; v[i].name; i++)
365 if (strncmp (optarg, v[i].name, len) == 0
366 && v[i].name[len] == '\0')
368 const char *initial_symbol_char;
371 set_pe_name ("__subsystem__", v[i].value);
373 initial_symbol_char = ${INITIAL_SYMBOL_CHAR};
374 if (*initial_symbol_char == '\0')
380 /* lang_add_entry expects its argument to be permanently
381 allocated, so we don't free this string. */
382 alc_entry = xmalloc (strlen (initial_symbol_char)
383 + strlen (v[i].entry)
385 strcpy (alc_entry, initial_symbol_char);
386 strcat (alc_entry, v[i].entry);
390 lang_add_entry (entry, 1);
396 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
408 set_pe_name (name, strtoul (optarg, &end, 0));
411 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
417 set_pe_stack_heap (resname, comname)
421 set_pe_value (resname);
426 set_pe_value (comname);
429 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
435 gld_${EMULATION_NAME}_parse_args(argc, argv)
441 int prevoptind = optind;
442 int prevopterr = opterr;
444 static int lastoptind = -1;
446 if (lastoptind != optind)
452 optc = getopt_long_only (argc, argv, "-", longopts, &longind);
463 case OPTION_BASE_FILE:
464 link_info.base_file = (PTR) fopen (optarg, FOPEN_WB);
465 if (link_info.base_file == NULL)
467 /* xgettext:c-format */
468 fprintf (stderr, _("%s: Can't open base file %s\n"),
469 program_name, optarg);
476 set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
479 set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
481 case OPTION_SUBSYSTEM:
484 case OPTION_MAJOR_OS_VERSION:
485 set_pe_value ("__major_os_version__");
487 case OPTION_MINOR_OS_VERSION:
488 set_pe_value ("__minor_os_version__");
490 case OPTION_MAJOR_SUBSYSTEM_VERSION:
491 set_pe_value ("__major_subsystem_version__");
493 case OPTION_MINOR_SUBSYSTEM_VERSION:
494 set_pe_value ("__minor_subsystem_version__");
496 case OPTION_MAJOR_IMAGE_VERSION:
497 set_pe_value ("__major_image_version__");
499 case OPTION_MINOR_IMAGE_VERSION:
500 set_pe_value ("__minor_image_version__");
502 case OPTION_FILE_ALIGNMENT:
503 set_pe_value ("__file_alignment__");
505 case OPTION_SECTION_ALIGNMENT:
506 set_pe_value ("__section_alignment__");
509 set_pe_name ("__dll__", 1);
511 case OPTION_IMAGE_BASE:
512 set_pe_value ("__image_base__");
514 case OPTION_SUPPORT_OLD_CODE:
515 support_old_code = 1;
517 case OPTION_THUMB_ENTRY:
518 thumb_entry_symbol = optarg;
522 pe_out_def_filename = xstrdup (optarg);
524 case OPTION_EXPORT_ALL:
525 pe_dll_export_everything = 1;
527 case OPTION_EXCLUDE_SYMBOLS:
528 pe_dll_add_excludes (optarg);
530 case OPTION_KILL_ATS:
533 case OPTION_STDCALL_ALIASES:
534 pe_dll_stdcall_aliases = 1;
536 case OPTION_ENABLE_STDCALL_FIXUP:
537 pe_enable_stdcall_fixup = 1;
539 case OPTION_DISABLE_STDCALL_FIXUP:
540 pe_enable_stdcall_fixup = 0;
542 case OPTION_IMPLIB_FILENAME:
543 pe_implib_filename = xstrdup (optarg);
545 case OPTION_WARN_DUPLICATE_EXPORTS:
546 pe_dll_warn_dup_exports = 1;
548 case OPTION_IMP_COMPAT:
549 pe_dll_compat_implib = 1;
551 case OPTION_ENABLE_AUTO_IMAGE_BASE:
552 pe_enable_auto_image_base = 1;
554 case OPTION_DISABLE_AUTO_IMAGE_BASE:
555 pe_enable_auto_image_base = 0;
564 strhash (const char *str)
566 const unsigned char *s;
573 s = (const unsigned char *) str;
574 while ((c = *s++) != '\0')
576 hash += c + (c << 17);
580 hash += len + (len << 17);
586 /* Use the output file to create a image base for relocatable DLLs. */
588 compute_dll_image_base (const char *ofile)
590 unsigned long hash = strhash (ofile);
591 return 0x60000000 | ((hash << 16) & 0x0FFC0000);
594 /* Assign values to the special symbols before the linker script is
598 gld_${EMULATION_NAME}_set_symbols ()
600 /* Run through and invent symbols for all the
601 names and insert the defaults. */
603 lang_statement_list_type *save;
605 if (!init[IMAGEBASEOFF].inited)
607 if (link_info.relocateable)
608 init[IMAGEBASEOFF].value = 0;
609 else if (init[DLLOFF].value || link_info.shared)
610 init[IMAGEBASEOFF].value = (pe_enable_auto_image_base) ?
611 compute_dll_image_base (output_filename) : NT_DLL_IMAGE_BASE;
613 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
616 /* Don't do any symbol assignments if this is a relocateable link. */
617 if (link_info.relocateable)
620 /* Glue the assignments into the abs section */
623 stat_ptr = &(abs_output_section->children);
625 for (j = 0; init[j].ptr; j++)
627 long val = init[j].value;
628 lang_assignment_statement_type *rv;
629 rv = lang_add_assignment (exp_assop ('=' ,init[j].symbol, exp_intop (val)));
630 if (init[j].size == sizeof(short))
631 *(short *)init[j].ptr = val;
632 else if (init[j].size == sizeof(int))
633 *(int *)init[j].ptr = val;
634 else if (init[j].size == sizeof(long))
635 *(long *)init[j].ptr = val;
636 /* This might be a long long or other special type. */
637 else if (init[j].size == sizeof(bfd_vma))
638 *(bfd_vma *)init[j].ptr = val;
640 if (j == IMAGEBASEOFF)
641 image_base_statement = rv;
643 /* Restore the pointer. */
646 if (pe.FileAlignment >
649 einfo (_("%P: warning, file alignment > section alignment.\n"));
653 /* This is called after the linker script and the command line options
657 gld_${EMULATION_NAME}_after_parse ()
659 /* The Windows libraries are designed for the linker to treat the
660 entry point as an undefined symbol. Otherwise, the .obj that
661 defines mainCRTStartup is brought in because it is the first
662 encountered in libc.lib and it has other symbols in it which will
663 be pulled in by the link process. To avoid this, we act as
664 though the user specified -u with the entry point symbol.
666 This function is called after the linker script and command line
667 options have been read, so at this point we know the right entry
668 point. This function is called before the input files are
669 opened, so registering the symbol as undefined will make a
672 if (! link_info.relocateable && entry_symbol != NULL)
673 ldlang_add_undef (entry_symbol);
676 static struct bfd_link_hash_entry *pe_undef_found_sym;
679 pe_undef_cdecl_match (h, string)
680 struct bfd_link_hash_entry *h;
683 int sl = strlen (string);
684 if (h->type == bfd_link_hash_defined
685 && strncmp (h->root.string, string, sl) == 0
686 && h->root.string[sl] == '@')
688 pe_undef_found_sym = h;
698 static int gave_warning_message = 0;
699 struct bfd_link_hash_entry *undef, *sym;
701 for (undef = link_info.hash->undefs; undef; undef=undef->next)
702 if (undef->type == bfd_link_hash_undefined)
704 at = strchr (undef->root.string, '@');
707 /* The symbol is a stdcall symbol, so let's look for a cdecl
708 symbol with the same name and resolve to that */
709 char *cname = xstrdup (undef->root.string);
710 at = strchr (cname, '@');
712 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
713 if (sym && sym->type == bfd_link_hash_defined)
715 undef->type = bfd_link_hash_defined;
716 undef->u.def.value = sym->u.def.value;
717 undef->u.def.section = sym->u.def.section;
718 if (pe_enable_stdcall_fixup == -1)
720 einfo (_("Warning: resolving %s by linking to %s\n"),
721 undef->root.string, cname);
722 if (! gave_warning_message)
724 gave_warning_message = 1;
725 einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
726 einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
733 /* The symbol is a cdecl symbol, so we look for stdcall
734 symbols - which means scanning the whole symbol table */
735 pe_undef_found_sym = 0;
736 bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
737 (PTR) undef->root.string);
738 sym = pe_undef_found_sym;
741 undef->type = bfd_link_hash_defined;
742 undef->u.def.value = sym->u.def.value;
743 undef->u.def.section = sym->u.def.section;
744 if (pe_enable_stdcall_fixup == -1)
746 einfo (_("Warning: resolving %s by linking to %s\n"),
747 undef->root.string, sym->root.string);
748 if (! gave_warning_message)
750 gave_warning_message = 1;
751 einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
752 einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
759 #endif /* DLL_SUPPORT */
762 gld_${EMULATION_NAME}_after_open ()
764 /* Pass the wacky PE command line options into the output bfd.
765 FIXME: This should be done via a function, rather than by
766 including an internal BFD header. */
768 if (!coff_data (output_bfd)->pe)
769 einfo (_("%F%P: PE operations on non PE file.\n"));
771 pe_data (output_bfd)->pe_opthdr = pe;
772 pe_data (output_bfd)->dll = init[DLLOFF].value;
775 if (pe_enable_stdcall_fixup) /* -1=warn or 1=disable */
776 pe_fixup_stdcalls ();
778 pe_process_import_defs(output_bfd, &link_info);
779 if (link_info.shared)
780 pe_dll_build_sections (output_bfd, &link_info);
782 #ifndef TARGET_IS_i386pe
783 #ifndef TARGET_IS_armpe
785 pe_exe_build_sections (output_bfd, &link_info);
790 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
791 if (strstr (bfd_get_target (output_bfd), "arm") == NULL)
793 /* The arm backend needs special fields in the output hash structure.
794 These will only be created if the output format is an arm format,
795 hence we do not support linking and changing output formats at the
796 same time. Use a link followed by objcopy to change output formats. */
797 einfo ("%F%X%P: error: cannot change output format whilst linking ARM binaries\n");
801 /* Find a BFD that can hold the interworking stubs. */
802 LANG_FOR_EACH_INPUT_STATEMENT (is)
804 if (bfd_arm_pe_get_bfd_for_interworking (is->the_bfd, & link_info))
813 lang_input_statement_type *is2;
815 /* Careful - this is a shell script. Watch those dollar signs! */
816 /* Microsoft import libraries have every member named the same,
817 and not in the right order for us to link them correctly. We
818 must detect these and rename the members so that they'll link
819 correctly. There are three types of objects: the head, the
820 thunks, and the sentinel(s). The head is easy; it's the one
821 with idata2. We assume that the sentinels won't have relocs,
822 and the thunks will. It's easier than checking the symbol
823 table for external references. */
824 LANG_FOR_EACH_INPUT_STATEMENT (is)
826 if (is->the_bfd->my_archive)
828 bfd *arch = is->the_bfd->my_archive;
829 if (cur_arch != arch)
834 is2 && is2->the_bfd->my_archive == arch;
835 is2 = (lang_input_statement_type *)is2->next)
837 if (strcmp (is->the_bfd->filename, is2->the_bfd->filename))
844 int idata2 = 0, reloc_count=0;
848 for (sec = is->the_bfd->sections; sec; sec = sec->next)
850 if (strcmp (sec->name, ".idata\$2") == 0)
852 reloc_count += sec->reloc_count;
855 if (idata2) /* .idata2 is the TOC */
857 else if (reloc_count > 0) /* thunks */
862 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
863 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
864 is->the_bfd->filename = new_name;
866 new_name = xmalloc (strlen (is->filename) + 3);
867 sprintf (new_name, "%s.%c", is->filename, seq);
868 is->filename = new_name;
876 gld_${EMULATION_NAME}_before_allocation()
878 #ifdef TARGET_IS_ppcpe
879 /* Here we rummage through the found bfds to collect toc information */
881 LANG_FOR_EACH_INPUT_STATEMENT (is)
883 if (!ppc_process_before_allocation (is->the_bfd, &link_info))
885 /* xgettext:c-format */
886 einfo (_("Errors encountered processing file %s\n"), is->filename);
891 /* We have seen it all. Allocate it, and carry on */
892 ppc_allocate_toc_section (&link_info);
893 #endif /* TARGET_IS_ppcpe */
895 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
896 /* FIXME: we should be able to set the size of the interworking stub
899 Here we rummage through the found bfds to collect glue
900 information. FIXME: should this be based on a command line
901 option? krk@cygnus.com */
903 LANG_FOR_EACH_INPUT_STATEMENT (is)
905 if (! bfd_arm_pe_process_before_allocation
906 (is->the_bfd, & link_info, support_old_code))
908 /* xgettext:c-format */
909 einfo (_("Errors encountered processing file %s for interworking"),
915 /* We have seen it all. Allocate it, and carry on */
916 bfd_arm_pe_allocate_interworking_sections (& link_info);
917 #endif /* TARGET_IS_armpe */
921 /* This is called when an input file isn't recognized as a BFD. We
922 check here for .DEF files and pull them in automatically. */
925 saw_option(char *option)
928 for (i=0; init[i].ptr; i++)
929 if (strcmp (init[i].symbol, option) == 0)
930 return init[i].inited;
933 #endif /* DLL_SUPPORT */
936 gld_${EMULATION_NAME}_unrecognized_file(entry)
937 lang_input_statement_type *entry ATTRIBUTE_UNUSED;
940 const char *ext = entry->filename + strlen (entry->filename) - 4;
942 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
944 if (pe_def_file == 0)
945 pe_def_file = def_file_empty ();
946 def_file_parse (entry->filename, pe_def_file);
949 int i, buflen=0, len;
951 for (i=0; i<pe_def_file->num_exports; i++)
953 len = strlen(pe_def_file->exports[i].internal_name);
957 buf = (char *) xmalloc (buflen);
958 for (i=0; i<pe_def_file->num_exports; i++)
960 struct bfd_link_hash_entry *h;
961 sprintf(buf, "_%s", pe_def_file->exports[i].internal_name);
963 h = bfd_link_hash_lookup (link_info.hash, buf, true, true, true);
964 if (h == (struct bfd_link_hash_entry *) NULL)
965 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
966 if (h->type == bfd_link_hash_new)
968 h->type = bfd_link_hash_undefined;
969 h->u.undef.abfd = NULL;
970 bfd_link_add_undef (link_info.hash, h);
975 /* def_file_print (stdout, pe_def_file); */
976 if (pe_def_file->is_dll == 1)
977 link_info.shared = 1;
979 if (pe_def_file->base_address != (bfd_vma)(-1))
982 pe_data (output_bfd)->pe_opthdr.ImageBase =
983 init[IMAGEBASEOFF].value = pe_def_file->base_address;
984 init[IMAGEBASEOFF].inited = 1;
985 if (image_base_statement)
986 image_base_statement->exp =
987 exp_assop ('=', "__image_base__", exp_intop (pe.ImageBase));
991 /* Not sure if these *should* be set */
992 if (pe_def_file->version_major != -1)
994 pe.MajorImageVersion = pe_def_file->version_major;
995 pe.MinorImageVersion = pe_def_file->version_minor;
998 if (pe_def_file->stack_reserve != -1
999 && ! saw_option ("__size_of_stack_reserve__"))
1001 pe.SizeOfStackReserve = pe_def_file->stack_reserve;
1002 if (pe_def_file->stack_commit != -1)
1003 pe.SizeOfStackCommit = pe_def_file->stack_commit;
1005 if (pe_def_file->heap_reserve != -1
1006 && ! saw_option ("__size_of_heap_reserve__"))
1008 pe.SizeOfHeapReserve = pe_def_file->heap_reserve;
1009 if (pe_def_file->heap_commit != -1)
1010 pe.SizeOfHeapCommit = pe_def_file->heap_commit;
1021 gld_${EMULATION_NAME}_recognized_file(entry)
1022 lang_input_statement_type *entry ATTRIBUTE_UNUSED;
1025 #ifdef TARGET_IS_i386pe
1026 pe_dll_id_target ("pei-i386");
1028 #ifdef TARGET_IS_shpe
1029 pe_dll_id_target ("pei-shl");
1031 #ifdef TARGET_IS_mipspe
1032 pe_dll_id_target ("pei-mips");
1034 #ifdef TARGET_IS_armpe
1035 pe_dll_id_target ("pei-arm-little");
1037 if (bfd_get_format (entry->the_bfd) == bfd_object)
1039 const char *ext = entry->filename + strlen (entry->filename) - 4;
1040 if (strcmp (ext, ".dll") == 0 || strcmp (ext, ".DLL") == 0)
1041 return pe_implied_import_dll (entry->filename);
1048 gld_${EMULATION_NAME}_finish ()
1050 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
1051 struct bfd_link_hash_entry * h;
1053 if (thumb_entry_symbol != NULL)
1055 h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol, false, false, true);
1057 if (h != (struct bfd_link_hash_entry *) NULL
1058 && (h->type == bfd_link_hash_defined
1059 || h->type == bfd_link_hash_defweak)
1060 && h->u.def.section->output_section != NULL)
1062 static char buffer[32];
1065 /* Special procesing is required for a Thumb entry symbol. The
1066 bottom bit of its address must be set. */
1067 val = (h->u.def.value
1068 + bfd_get_section_vma (output_bfd,
1069 h->u.def.section->output_section)
1070 + h->u.def.section->output_offset);
1074 /* Now convert this value into a string and store it in entry_symbol
1075 where the lang_finish() function will pick it up. */
1079 sprintf_vma (buffer + 2, val);
1081 if (entry_symbol != NULL && entry_from_cmdline)
1082 einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
1083 thumb_entry_symbol, entry_symbol);
1084 entry_symbol = buffer;
1087 einfo (_("%P: warning: connot find thumb start symbol %s\n"), thumb_entry_symbol);
1089 #endif /* defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe) */
1092 if (link_info.shared)
1094 pe_dll_fill_sections (output_bfd, &link_info);
1095 if (pe_implib_filename)
1096 pe_dll_generate_implib (pe_def_file, pe_implib_filename);
1098 #if defined(TARGET_IS_shpe) || defined(TARGET_IS_mipspe)
1099 /* ARM doesn't need relocs. */
1102 pe_exe_fill_sections (output_bfd, &link_info);
1106 if (pe_out_def_filename)
1107 pe_dll_generate_def_file (pe_out_def_filename);
1108 #endif /* DLL_SUPPORT */
1112 /* Place an orphan section.
1114 We use this to put sections in a reasonable place in the file, and
1115 to ensure that they are aligned as required.
1117 We handle grouped sections here as well. A section named .foo$nn
1118 goes into the output section .foo. All grouped sections are sorted
1121 Grouped sections for the default sections are handled by the
1122 default linker script using wildcards, and are sorted by
1127 lang_output_section_statement_type *os;
1129 lang_statement_union_type **stmt;
1134 gld_${EMULATION_NAME}_place_orphan (file, s)
1135 lang_input_statement_type *file;
1138 const char *secname;
1139 char *hold_section_name;
1140 char *dollar = NULL;
1141 lang_output_section_statement_type *os;
1142 lang_statement_list_type add_child;
1144 secname = bfd_get_section_name (s->owner, s);
1146 /* Look through the script to see where to place this section. */
1148 hold_section_name = xstrdup (secname);
1149 if (!link_info.relocateable)
1151 dollar = strchr (hold_section_name, '$');
1156 os = lang_output_section_find (hold_section_name);
1158 lang_list_init (&add_child);
1161 && os->bfd_section != NULL
1162 && ((s->flags ^ os->bfd_section->flags) & (SEC_LOAD | SEC_ALLOC)) == 0)
1164 wild_doit (&add_child, s, os, file);
1168 struct orphan_save *place;
1169 static struct orphan_save hold_text;
1170 static struct orphan_save hold_rdata;
1171 static struct orphan_save hold_data;
1172 static struct orphan_save hold_bss;
1174 lang_statement_list_type *old;
1175 lang_statement_list_type add;
1176 etree_type *address;
1178 /* Try to put the new output section in a reasonable place based
1179 on the section name and section flags. */
1180 #define HAVE_SECTION(hold, name) \
1181 (hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
1184 if ((s->flags & SEC_ALLOC) == 0)
1186 else if ((s->flags & SEC_HAS_CONTENTS) == 0
1187 && HAVE_SECTION (hold_bss, ".bss"))
1189 else if ((s->flags & SEC_READONLY) == 0
1190 && HAVE_SECTION (hold_data, ".data"))
1192 else if ((s->flags & SEC_CODE) == 0
1193 && (s->flags & SEC_READONLY) != 0
1194 && HAVE_SECTION (hold_rdata, ".rdata"))
1195 place = &hold_rdata;
1196 else if ((s->flags & SEC_READONLY) != 0
1197 && HAVE_SECTION (hold_text, ".text"))
1202 /* Choose a unique name for the section. This will be needed if
1203 the same section name appears in the input file with
1204 different loadable or allocateable characteristics. */
1205 outsecname = xstrdup (hold_section_name);
1206 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
1212 len = strlen (outsecname);
1213 newname = xmalloc (len + 5);
1214 strcpy (newname, outsecname);
1218 sprintf (newname + len, "%d", i);
1221 while (bfd_get_section_by_name (output_bfd, newname) != NULL);
1224 outsecname = newname;
1227 /* Start building a list of statements for this section. */
1230 lang_list_init (stat_ptr);
1232 if (link_info.relocateable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1233 address = exp_intop ((bfd_vma) 0);
1236 /* All sections in an executable must be aligned to a page
1238 address = exp_unop (ALIGN_K,
1239 exp_nameop (NAME, "__section_alignment__"));
1242 os = lang_enter_output_section_statement (outsecname, address, 0,
1244 (etree_type *) NULL,
1245 (etree_type *) NULL,
1246 (etree_type *) NULL);
1248 wild_doit (&add_child, s, os, file);
1250 lang_leave_output_section_statement
1251 ((bfd_vma) 0, "*default*",
1252 (struct lang_output_section_phdr_list *) NULL, "*default*");
1258 asection *snew, **pps;
1260 snew = os->bfd_section;
1261 if (place->os->bfd_section != NULL || place->section != NULL)
1263 /* Shuffle the section to make the output file look neater. */
1264 if (place->section == NULL)
1267 /* Finding the end of the list is a little tricky. We
1268 make a wild stab at it by comparing section flags. */
1269 flagword first_flags = place->os->bfd_section->flags;
1270 for (pps = &place->os->bfd_section->next;
1271 *pps != NULL && (*pps)->flags == first_flags;
1272 pps = &(*pps)->next)
1274 place->section = pps;
1276 /* Put orphans after the first section on the list. */
1277 place->section = &place->os->bfd_section->next;
1281 /* Unlink the section. */
1282 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1286 /* Now tack it on to the "place->os" section list. */
1287 snew->next = *place->section;
1288 *place->section = snew;
1290 place->section = &snew->next; /* Save the end of this list. */
1292 if (place->stmt == NULL)
1294 /* Put the new statement list right at the head. */
1295 *add.tail = place->os->header.next;
1296 place->os->header.next = add.head;
1300 /* Put it after the last orphan statement we added. */
1301 *add.tail = *place->stmt;
1302 *place->stmt = add.head;
1304 place->stmt = add.tail; /* Save the end of this list. */
1309 lang_statement_union_type **pl = &os->children.head;
1313 boolean found_dollar;
1315 /* The section name has a '$'. Sort it with the other '$'
1318 found_dollar = false;
1319 for ( ; *pl != NULL; pl = &(*pl)->next)
1321 lang_input_section_type *ls;
1324 if ((*pl)->header.type != lang_input_section_enum)
1327 ls = &(*pl)->input_section;
1329 lname = bfd_get_section_name (ls->ifile->the_bfd, ls->section);
1330 if (strchr (lname, '$') == NULL)
1337 found_dollar = true;
1338 if (strcmp (secname, lname) < 0)
1344 if (add_child.head != NULL)
1346 add_child.head->next = *pl;
1347 *pl = add_child.head;
1351 free (hold_section_name);
1357 gld_${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
1359 search_dirs_type * search;
1360 lang_input_statement_type * entry;
1362 const char * filename;
1365 if (! entry->is_archive)
1368 filename = entry->filename;
1370 string = (char *) xmalloc (strlen (search->name)
1375 /* Try "foo.dll" first. */
1376 sprintf (string, "%s/%s.dll", search->name, filename);
1378 if (! ldfile_try_open_bfd (string, entry))
1380 /* Try "libfoo.dll" next. */
1381 sprintf (string, "%s/lib%s.dll", search->name, filename);
1383 if (! ldfile_try_open_bfd (string, entry))
1390 entry->filename = string;
1396 gld_${EMULATION_NAME}_find_potential_libraries (name, entry)
1398 lang_input_statement_type * entry;
1400 return ldfile_open_file_search (name, entry, "", ".lib");
1404 gld_${EMULATION_NAME}_get_script(isfile)
1407 # Scripts compiled in.
1408 # sed commands to quote an ld script as a C string.
1409 sc="-f stringify.sed"
1411 cat >>e${EMULATION_NAME}.c <<EOF
1415 if (link_info.relocateable == true && config.build_constructors == true)
1418 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1419 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1420 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1421 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1422 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1423 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1424 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1425 echo ' ; else return' >> e${EMULATION_NAME}.c
1426 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1427 echo '; }' >> e${EMULATION_NAME}.c
1429 cat >>e${EMULATION_NAME}.c <<EOF
1432 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1434 gld_${EMULATION_NAME}_before_parse,
1437 gld_${EMULATION_NAME}_after_parse,
1438 gld_${EMULATION_NAME}_after_open,
1439 after_allocation_default,
1440 set_output_arch_default,
1441 ldemul_default_target,
1442 gld_${EMULATION_NAME}_before_allocation,
1443 gld_${EMULATION_NAME}_get_script,
1444 "${EMULATION_NAME}",
1446 gld_${EMULATION_NAME}_finish, /* finish */
1447 NULL, /* create output section statements */
1448 gld_${EMULATION_NAME}_open_dynamic_archive,
1449 gld_${EMULATION_NAME}_place_orphan,
1450 gld_${EMULATION_NAME}_set_symbols,
1451 gld_${EMULATION_NAME}_parse_args,
1452 gld_${EMULATION_NAME}_unrecognized_file,
1453 gld_${EMULATION_NAME}_list_options,
1454 gld_${EMULATION_NAME}_recognized_file,
1455 gld_${EMULATION_NAME}_find_potential_libraries