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));
109 static struct internal_extra_pe_aouthdr pe;
111 static int support_old_code = 0;
112 static char * thumb_entry_symbol = NULL;
113 static lang_assignment_statement_type *image_base_statement = 0;
115 static int pe_enable_stdcall_fixup = -1; /* 0=disable 1=enable */
117 static char *pe_out_def_filename = 0;
118 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;
649 static int gave_warning_message = 0;
650 struct bfd_link_hash_entry *undef, *sym;
652 for (undef = link_info.hash->undefs; undef; undef=undef->next)
653 if (undef->type == bfd_link_hash_undefined)
655 at = strchr (undef->root.string, '@');
658 /* The symbol is a stdcall symbol, so let's look for a cdecl
659 symbol with the same name and resolve to that */
660 char *cname = xstrdup (undef->root.string);
661 at = strchr (cname, '@');
663 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
664 if (sym && sym->type == bfd_link_hash_defined)
666 undef->type = bfd_link_hash_defined;
667 undef->u.def.value = sym->u.def.value;
668 undef->u.def.section = sym->u.def.section;
669 if (pe_enable_stdcall_fixup == -1)
671 einfo (_("Warning: resolving %s by linking to %s\n"),
672 undef->root.string, cname);
673 if (! gave_warning_message)
675 gave_warning_message = 1;
676 einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
677 einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
684 /* The symbol is a cdecl symbol, so we look for stdcall
685 symbols - which means scanning the whole symbol table */
686 pe_undef_found_sym = 0;
687 bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
688 (PTR) undef->root.string);
689 sym = pe_undef_found_sym;
692 undef->type = bfd_link_hash_defined;
693 undef->u.def.value = sym->u.def.value;
694 undef->u.def.section = sym->u.def.section;
695 if (pe_enable_stdcall_fixup == -1)
697 einfo (_("Warning: resolving %s by linking to %s\n"),
698 undef->root.string, sym->root.string);
699 if (! gave_warning_message)
701 gave_warning_message = 1;
702 einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
703 einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
710 #endif /* DLL_SUPPORT */
713 gld_${EMULATION_NAME}_after_open ()
715 /* Pass the wacky PE command line options into the output bfd.
716 FIXME: This should be done via a function, rather than by
717 including an internal BFD header. */
719 if (!coff_data (output_bfd)->pe)
720 einfo (_("%F%P: PE operations on non PE file.\n"));
722 pe_data (output_bfd)->pe_opthdr = pe;
723 pe_data (output_bfd)->dll = init[DLLOFF].value;
726 if (pe_enable_stdcall_fixup) /* -1=warn or 1=disable */
727 pe_fixup_stdcalls ();
729 pe_process_import_defs(output_bfd, &link_info);
730 if (link_info.shared)
731 pe_dll_build_sections (output_bfd, &link_info);
733 #ifndef TARGET_IS_i386pe
734 #ifndef TARGET_IS_armpe
736 pe_exe_build_sections (output_bfd, &link_info);
741 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
742 if (strstr (bfd_get_target (output_bfd), "arm") == NULL)
744 /* The arm backend needs special fields in the output hash structure.
745 These will only be created if the output format is an arm format,
746 hence we do not support linking and changing output formats at the
747 same time. Use a link followed by objcopy to change output formats. */
748 einfo ("%F%X%P: error: cannot change output format whilst linking ARM binaries\n");
752 /* Find a BFD that can hold the interworking stubs. */
753 LANG_FOR_EACH_INPUT_STATEMENT (is)
755 if (bfd_arm_pe_get_bfd_for_interworking (is->the_bfd, & link_info))
764 lang_input_statement_type *is2;
766 /* Careful - this is a shell script. Watch those dollar signs! */
767 /* Microsoft import libraries have every member named the same,
768 and not in the right order for us to link them correctly. We
769 must detect these and rename the members so that they'll link
770 correctly. There are three types of objects: the head, the
771 thunks, and the sentinel(s). The head is easy; it's the one
772 with idata2. We assume that the sentinels won't have relocs,
773 and the thunks will. It's easier than checking the symbol
774 table for external references. */
775 LANG_FOR_EACH_INPUT_STATEMENT (is)
777 if (is->the_bfd->my_archive)
779 bfd *arch = is->the_bfd->my_archive;
780 if (cur_arch != arch)
785 is2 && is2->the_bfd->my_archive == arch;
786 is2 = (lang_input_statement_type *)is2->next)
788 if (strcmp (is->the_bfd->filename, is2->the_bfd->filename))
795 int idata2 = 0, reloc_count=0;
799 for (sec = is->the_bfd->sections; sec; sec = sec->next)
801 if (strcmp (sec->name, ".idata\$2") == 0)
803 reloc_count += sec->reloc_count;
806 if (idata2) /* .idata2 is the TOC */
808 else if (reloc_count > 0) /* thunks */
813 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
814 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
815 is->the_bfd->filename = new_name;
817 new_name = xmalloc (strlen(is->filename) + 3);
818 sprintf (new_name, "%s.%c", is->filename, seq);
819 is->filename = new_name;
827 gld_${EMULATION_NAME}_before_allocation()
829 #ifdef TARGET_IS_ppcpe
830 /* Here we rummage through the found bfds to collect toc information */
832 LANG_FOR_EACH_INPUT_STATEMENT (is)
834 if (!ppc_process_before_allocation (is->the_bfd, &link_info))
836 /* xgettext:c-format */
837 einfo (_("Errors encountered processing file %s\n"), is->filename);
842 /* We have seen it all. Allocate it, and carry on */
843 ppc_allocate_toc_section (&link_info);
844 #endif /* TARGET_IS_ppcpe */
846 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
847 /* FIXME: we should be able to set the size of the interworking stub
850 Here we rummage through the found bfds to collect glue
851 information. FIXME: should this be based on a command line
852 option? krk@cygnus.com */
854 LANG_FOR_EACH_INPUT_STATEMENT (is)
856 if (! bfd_arm_pe_process_before_allocation
857 (is->the_bfd, & link_info, support_old_code))
859 /* xgettext:c-format */
860 einfo (_("Errors encountered processing file %s for interworking"),
866 /* We have seen it all. Allocate it, and carry on */
867 bfd_arm_pe_allocate_interworking_sections (& link_info);
868 #endif /* TARGET_IS_armpe */
872 /* This is called when an input file isn't recognized as a BFD. We
873 check here for .DEF files and pull them in automatically. */
876 saw_option(char *option)
879 for (i=0; init[i].ptr; i++)
880 if (strcmp (init[i].symbol, option) == 0)
881 return init[i].inited;
887 gld_${EMULATION_NAME}_unrecognized_file(entry)
888 lang_input_statement_type *entry ATTRIBUTE_UNUSED;
891 const char *ext = entry->filename + strlen (entry->filename) - 4;
893 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
895 if (pe_def_file == 0)
896 pe_def_file = def_file_empty ();
897 def_file_parse (entry->filename, pe_def_file);
900 int i, buflen=0, len;
902 for (i=0; i<pe_def_file->num_exports; i++)
904 len = strlen(pe_def_file->exports[i].internal_name);
908 buf = (char *) xmalloc (buflen);
909 for (i=0; i<pe_def_file->num_exports; i++)
911 struct bfd_link_hash_entry *h;
912 sprintf(buf, "_%s", pe_def_file->exports[i].internal_name);
914 h = bfd_link_hash_lookup (link_info.hash, buf, true, true, true);
915 if (h == (struct bfd_link_hash_entry *) NULL)
916 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
917 if (h->type == bfd_link_hash_new)
919 h->type = bfd_link_hash_undefined;
920 h->u.undef.abfd = NULL;
921 bfd_link_add_undef (link_info.hash, h);
926 /* def_file_print (stdout, pe_def_file); */
927 if (pe_def_file->is_dll == 1)
928 link_info.shared = 1;
930 if (pe_def_file->base_address != (bfd_vma)(-1))
933 pe_data (output_bfd)->pe_opthdr.ImageBase =
934 init[IMAGEBASEOFF].value = pe_def_file->base_address;
935 init[IMAGEBASEOFF].inited = 1;
936 if (image_base_statement)
937 image_base_statement->exp =
938 exp_assop ('=', "__image_base__", exp_intop (pe.ImageBase));
942 /* Not sure if these *should* be set */
943 if (pe_def_file->version_major != -1)
945 pe.MajorImageVersion = pe_def_file->version_major;
946 pe.MinorImageVersion = pe_def_file->version_minor;
949 if (pe_def_file->stack_reserve != -1
950 && ! saw_option ("__size_of_stack_reserve__"))
952 pe.SizeOfStackReserve = pe_def_file->stack_reserve;
953 if (pe_def_file->stack_commit != -1)
954 pe.SizeOfStackCommit = pe_def_file->stack_commit;
956 if (pe_def_file->heap_reserve != -1
957 && ! saw_option ("__size_of_heap_reserve__"))
959 pe.SizeOfHeapReserve = pe_def_file->heap_reserve;
960 if (pe_def_file->heap_commit != -1)
961 pe.SizeOfHeapCommit = pe_def_file->heap_commit;
972 gld_${EMULATION_NAME}_recognized_file(entry)
973 lang_input_statement_type *entry ATTRIBUTE_UNUSED;
976 #ifdef TARGET_IS_i386pe
977 pe_dll_id_target ("pei-i386");
979 #ifdef TARGET_IS_shpe
980 pe_dll_id_target ("pei-shl");
982 #ifdef TARGET_IS_mipspe
983 pe_dll_id_target ("pei-mips");
985 #ifdef TARGET_IS_armpe
986 pe_dll_id_target ("pei-arm-little");
988 if (bfd_get_format (entry->the_bfd) == bfd_object)
990 const char *ext = entry->filename + strlen (entry->filename) - 4;
991 if (strcmp (ext, ".dll") == 0 || strcmp (ext, ".DLL") == 0)
992 return pe_implied_import_dll (entry->filename);
999 gld_${EMULATION_NAME}_finish ()
1001 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
1002 struct bfd_link_hash_entry * h;
1004 if (thumb_entry_symbol != NULL)
1006 h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol, false, false, true);
1008 if (h != (struct bfd_link_hash_entry *) NULL
1009 && (h->type == bfd_link_hash_defined
1010 || h->type == bfd_link_hash_defweak)
1011 && h->u.def.section->output_section != NULL)
1013 static char buffer[32];
1016 /* Special procesing is required for a Thumb entry symbol. The
1017 bottom bit of its address must be set. */
1018 val = (h->u.def.value
1019 + bfd_get_section_vma (output_bfd,
1020 h->u.def.section->output_section)
1021 + h->u.def.section->output_offset);
1025 /* Now convert this value into a string and store it in entry_symbol
1026 where the lang_finish() function will pick it up. */
1030 sprintf_vma (buffer + 2, val);
1032 if (entry_symbol != NULL && entry_from_cmdline)
1033 einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
1034 thumb_entry_symbol, entry_symbol);
1035 entry_symbol = buffer;
1038 einfo (_("%P: warning: connot find thumb start symbol %s\n"), thumb_entry_symbol);
1040 #endif /* defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe) */
1043 if (link_info.shared)
1045 pe_dll_fill_sections (output_bfd, &link_info);
1046 if (pe_implib_filename)
1047 pe_dll_generate_implib (pe_def_file, pe_implib_filename);
1049 #if defined(TARGET_IS_shpe) || defined(TARGET_IS_mipspe)
1052 pe_exe_fill_sections (output_bfd, &link_info);
1056 if (pe_out_def_filename)
1057 pe_dll_generate_def_file (pe_out_def_filename);
1062 /* Place an orphan section.
1064 We use this to put sections in a reasonable place in the file, and
1065 to ensure that they are aligned as required.
1067 We handle grouped sections here as well. A section named .foo$nn
1068 goes into the output section .foo. All grouped sections are sorted
1071 Grouped sections for the default sections are handled by the
1072 default linker script using wildcards, and are sorted by
1077 lang_output_section_statement_type *os;
1079 lang_statement_union_type **stmt;
1084 gld_${EMULATION_NAME}_place_orphan (file, s)
1085 lang_input_statement_type *file;
1088 const char *secname;
1089 char *hold_section_name;
1090 char *dollar = NULL;
1091 lang_output_section_statement_type *os;
1092 lang_statement_list_type add_child;
1094 secname = bfd_get_section_name (s->owner, s);
1096 /* Look through the script to see where to place this section. */
1098 hold_section_name = xstrdup (secname);
1099 if (!link_info.relocateable)
1101 dollar = strchr (hold_section_name, '$');
1106 os = lang_output_section_find (hold_section_name);
1108 lang_list_init (&add_child);
1111 && os->bfd_section != NULL
1112 && ((s->flags ^ os->bfd_section->flags) & (SEC_LOAD | SEC_ALLOC)) == 0)
1114 wild_doit (&add_child, s, os, file);
1118 struct orphan_save *place;
1119 static struct orphan_save hold_text;
1120 static struct orphan_save hold_rdata;
1121 static struct orphan_save hold_data;
1122 static struct orphan_save hold_bss;
1124 lang_statement_list_type *old;
1125 lang_statement_list_type add;
1126 etree_type *address;
1128 /* Try to put the new output section in a reasonable place based
1129 on the section name and section flags. */
1130 #define HAVE_SECTION(hold, name) \
1131 (hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
1134 if ((s->flags & SEC_ALLOC) == 0)
1136 else if ((s->flags & SEC_HAS_CONTENTS) == 0
1137 && HAVE_SECTION (hold_bss, ".bss"))
1139 else if ((s->flags & SEC_READONLY) == 0
1140 && HAVE_SECTION (hold_data, ".data"))
1142 else if ((s->flags & SEC_CODE) == 0
1143 && (s->flags & SEC_READONLY) != 0
1144 && HAVE_SECTION (hold_rdata, ".rdata"))
1145 place = &hold_rdata;
1146 else if ((s->flags & SEC_READONLY) != 0
1147 && HAVE_SECTION (hold_text, ".text"))
1152 /* Choose a unique name for the section. This will be needed if
1153 the same section name appears in the input file with
1154 different loadable or allocateable characteristics. */
1155 outsecname = xstrdup (hold_section_name);
1156 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
1162 len = strlen (outsecname);
1163 newname = xmalloc (len + 5);
1164 strcpy (newname, outsecname);
1168 sprintf (newname + len, "%d", i);
1171 while (bfd_get_section_by_name (output_bfd, newname) != NULL);
1174 outsecname = newname;
1177 /* Start building a list of statements for this section. */
1180 lang_list_init (stat_ptr);
1182 if (link_info.relocateable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1183 address = exp_intop ((bfd_vma) 0);
1186 /* All sections in an executable must be aligned to a page
1188 address = exp_unop (ALIGN_K,
1189 exp_nameop (NAME, "__section_alignment__"));
1192 os = lang_enter_output_section_statement (outsecname, address, 0,
1194 (etree_type *) NULL,
1195 (etree_type *) NULL,
1196 (etree_type *) NULL);
1198 wild_doit (&add_child, s, os, file);
1200 lang_leave_output_section_statement
1201 ((bfd_vma) 0, "*default*",
1202 (struct lang_output_section_phdr_list *) NULL, "*default*");
1208 asection *snew, **pps;
1210 snew = os->bfd_section;
1211 if (place->os->bfd_section != NULL || place->section != NULL)
1213 /* Shuffle the section to make the output file look neater. */
1214 if (place->section == NULL)
1217 /* Finding the end of the list is a little tricky. We
1218 make a wild stab at it by comparing section flags. */
1219 flagword first_flags = place->os->bfd_section->flags;
1220 for (pps = &place->os->bfd_section->next;
1221 *pps != NULL && (*pps)->flags == first_flags;
1222 pps = &(*pps)->next)
1224 place->section = pps;
1226 /* Put orphans after the first section on the list. */
1227 place->section = &place->os->bfd_section->next;
1231 /* Unlink the section. */
1232 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1236 /* Now tack it on to the "place->os" section list. */
1237 snew->next = *place->section;
1238 *place->section = snew;
1240 place->section = &snew->next; /* Save the end of this list. */
1242 if (place->stmt == NULL)
1244 /* Put the new statement list right at the head. */
1245 *add.tail = place->os->header.next;
1246 place->os->header.next = add.head;
1250 /* Put it after the last orphan statement we added. */
1251 *add.tail = *place->stmt;
1252 *place->stmt = add.head;
1254 place->stmt = add.tail; /* Save the end of this list. */
1259 lang_statement_union_type **pl = &os->children.head;
1263 boolean found_dollar;
1265 /* The section name has a '$'. Sort it with the other '$'
1268 found_dollar = false;
1269 for ( ; *pl != NULL; pl = &(*pl)->next)
1271 lang_input_section_type *ls;
1274 if ((*pl)->header.type != lang_input_section_enum)
1277 ls = &(*pl)->input_section;
1279 lname = bfd_get_section_name (ls->ifile->the_bfd, ls->section);
1280 if (strchr (lname, '$') == NULL)
1287 found_dollar = true;
1288 if (strcmp (secname, lname) < 0)
1294 if (add_child.head != NULL)
1296 add_child.head->next = *pl;
1297 *pl = add_child.head;
1301 free (hold_section_name);
1307 gld_${EMULATION_NAME}_find_potential_libraries (name, entry)
1309 lang_input_statement_type * entry;
1311 return ldfile_open_file_search (name, entry, "", ".lib");
1315 gld_${EMULATION_NAME}_get_script(isfile)
1318 # Scripts compiled in.
1319 # sed commands to quote an ld script as a C string.
1320 sc="-f stringify.sed"
1322 cat >>e${EMULATION_NAME}.c <<EOF
1326 if (link_info.relocateable == true && config.build_constructors == true)
1329 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1330 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1331 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1332 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1333 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1334 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1335 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1336 echo ' ; else return' >> e${EMULATION_NAME}.c
1337 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1338 echo '; }' >> e${EMULATION_NAME}.c
1340 cat >>e${EMULATION_NAME}.c <<EOF
1343 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1345 gld_${EMULATION_NAME}_before_parse,
1348 gld_${EMULATION_NAME}_after_parse,
1349 gld_${EMULATION_NAME}_after_open,
1350 after_allocation_default,
1351 set_output_arch_default,
1352 ldemul_default_target,
1353 gld_${EMULATION_NAME}_before_allocation,
1354 gld_${EMULATION_NAME}_get_script,
1355 "${EMULATION_NAME}",
1357 gld_${EMULATION_NAME}_finish, /* finish */
1358 NULL, /* create output section statements */
1359 NULL, /* open dynamic archive */
1360 gld_${EMULATION_NAME}_place_orphan,
1361 gld_${EMULATION_NAME}_set_symbols,
1362 gld_${EMULATION_NAME}_parse_args,
1363 gld_${EMULATION_NAME}_unrecognized_file,
1364 gld_${EMULATION_NAME}_list_options,
1365 gld_${EMULATION_NAME}_recognized_file,
1366 gld_${EMULATION_NAME}_find_potential_libraries