1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 test -z "${ENTRY}" && ENTRY="_mainCRTStartup"
4 if [ -z "$MACHINE" ]; then
7 OUTPUT_ARCH=${ARCH}:${MACHINE}
9 rm -f e${EMULATION_NAME}.c
10 (echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
12 /* Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
13 Written by Kai Tietz, OneVision Software GmbH&CoKg.
15 This file is part of the GNU Binutils.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 3 of the License, or
20 (at your option) any later version.
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
30 MA 02110-1301, USA. */
33 /* For WINDOWS_XP64 and higher */
34 /* Based on pe.em, but modified for 64 bit support. */
36 #define TARGET_IS_${EMULATION_NAME}
38 #define COFF_IMAGE_WITH_PE
40 #define COFF_WITH_pex64
46 #include "libiberty.h"
57 #include "coff/internal.h"
59 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
60 header in generic PE code. */
61 #include "coff/x86_64.h"
64 /* FIXME: This is a BFD internal header file, and we should not be
66 #include "../bfd/libcoff.h"
69 #define AOUTSZ PEPAOUTSZ
70 #define PEAOUTHDR PEPAOUTHDR
74 #include "safe-ctype.h"
76 /* Permit the emulation parameters to override the default section
77 alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes
78 it seem that include/coff/internal.h should not define
79 PE_DEF_SECTION_ALIGNMENT. */
80 #if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
81 #undef PE_DEF_SECTION_ALIGNMENT
82 #define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
85 #ifdef TARGET_IS_i386pep
89 #if defined(TARGET_IS_i386pep) || ! defined(DLL_SUPPORT)
90 #define PE_DEF_SUBSYSTEM 3
92 #undef NT_EXE_IMAGE_BASE
93 #define NT_EXE_IMAGE_BASE 0x00010000
94 #undef PE_DEF_SECTION_ALIGNMENT
95 #define PE_DEF_SUBSYSTEM 2
96 #undef PE_DEF_FILE_ALIGNMENT
97 #define PE_DEF_FILE_ALIGNMENT 0x00000200
98 #define PE_DEF_SECTION_ALIGNMENT 0x00000400
102 static struct internal_extra_pe_aouthdr pep;
104 static flagword real_flags = IMAGE_FILE_LARGE_ADDRESS_AWARE;
105 static int support_old_code = 0;
106 static lang_assignment_statement_type *image_base_statement = 0;
109 static int pep_enable_stdcall_fixup = 1; /* 0=disable 1=enable (default). */
110 static char * pep_out_def_filename = NULL;
111 static char * pep_implib_filename = NULL;
112 static int pep_enable_auto_image_base = 0;
113 static char * pep_dll_search_prefix = NULL;
116 extern const char *output_filename;
119 gld_${EMULATION_NAME}_before_parse (void)
121 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
122 output_filename = "${EXECUTABLE_NAME:-a.exe}";
124 config.dynamic_link = TRUE;
125 config.has_shared = 1;
126 link_info.pei386_auto_import = -1;
127 link_info.pei386_runtime_pseudo_reloc = 2; /* Use by default version 2. */
129 #if (PE_DEF_SUBSYSTEM == 9) || (PE_DEF_SUBSYSTEM == 2)
130 lang_default_entry ("_WinMainCRTStartup");
132 lang_default_entry ("${ENTRY}");
137 /* PE format extra command line options. */
139 /* Used for setting flags in the PE header. */
142 OPTION_BASE_FILE = 300 + 1,
144 OPTION_FILE_ALIGNMENT,
146 OPTION_MAJOR_IMAGE_VERSION,
147 OPTION_MAJOR_OS_VERSION,
148 OPTION_MAJOR_SUBSYSTEM_VERSION,
149 OPTION_MINOR_IMAGE_VERSION,
150 OPTION_MINOR_OS_VERSION,
151 OPTION_MINOR_SUBSYSTEM_VERSION,
152 OPTION_SECTION_ALIGNMENT,
156 OPTION_SUPPORT_OLD_CODE,
159 OPTION_EXCLUDE_SYMBOLS,
161 OPTION_STDCALL_ALIASES,
162 OPTION_ENABLE_STDCALL_FIXUP,
163 OPTION_DISABLE_STDCALL_FIXUP,
164 OPTION_IMPLIB_FILENAME,
165 OPTION_WARN_DUPLICATE_EXPORTS,
167 OPTION_ENABLE_AUTO_IMAGE_BASE,
168 OPTION_DISABLE_AUTO_IMAGE_BASE,
169 OPTION_DLL_SEARCH_PREFIX,
170 OPTION_NO_DEFAULT_EXCLUDES,
171 OPTION_DLL_ENABLE_AUTO_IMPORT,
172 OPTION_DLL_DISABLE_AUTO_IMPORT,
173 OPTION_ENABLE_EXTRA_PE_DEBUG,
175 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC,
176 OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC,
177 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1,
178 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2,
179 OPTION_EXCLUDE_MODULES_FOR_IMPLIB,
180 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES,
181 OPTION_ENABLE_LONG_SECTION_NAMES,
182 OPTION_DISABLE_LONG_SECTION_NAMES
186 gld${EMULATION_NAME}_add_options
187 (int ns ATTRIBUTE_UNUSED,
188 char **shortopts ATTRIBUTE_UNUSED,
190 struct option **longopts,
191 int nrl ATTRIBUTE_UNUSED,
192 struct option **really_longopts ATTRIBUTE_UNUSED)
194 static const struct option xtra_long[] =
197 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
198 {"dll", no_argument, NULL, OPTION_DLL},
199 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
200 {"heap", required_argument, NULL, OPTION_HEAP},
201 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
202 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
203 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
204 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
205 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
206 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
207 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
208 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
209 {"stack", required_argument, NULL, OPTION_STACK},
210 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
211 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
212 {"use-nul-prefixed-import-tables", no_argument, NULL,
213 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
215 /* getopt allows abbreviations, so we do this to stop it
216 from treating -o as an abbreviation for this option. */
217 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
218 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
219 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
220 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
221 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
222 {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
223 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
224 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
225 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
226 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
227 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
228 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
229 /* getopt() allows abbreviations, so we do this to stop it from
230 treating -c as an abbreviation for these --compat-implib. */
231 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
232 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
233 {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
234 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
235 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
236 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
237 {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
238 {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
239 {"enable-extra-pep-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
240 {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
241 {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
242 {"enable-runtime-pseudo-reloc-v1", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1},
243 {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2},
245 {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
246 {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
247 {NULL, no_argument, NULL, 0}
250 *longopts = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
251 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
254 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
255 parameters which may be input from the command line. */
266 #define D(field,symbol,def) {&pep.field,sizeof(pep.field), def, symbol,0}
268 static definfo init[] =
270 /* imagebase must be first */
271 #define IMAGEBASEOFF 0
272 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
274 {&dll, sizeof(dll), 0, "__dll__", 0},
275 #define MSIMAGEBASEOFF 2
276 D(ImageBase,"___ImageBase", NT_EXE_IMAGE_BASE),
277 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
278 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
279 D(MajorOperatingSystemVersion,"__major_os_version__", 4),
280 D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
281 D(MajorImageVersion,"__major_image_version__", 0),
282 D(MinorImageVersion,"__minor_image_version__", 0),
283 D(MajorSubsystemVersion,"__major_subsystem_version__", 5),
284 D(MinorSubsystemVersion,"__minor_subsystem_version__", 2),
285 D(Subsystem,"__subsystem__", ${SUBSYSTEM}),
286 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000),
287 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
288 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
289 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
290 D(LoaderFlags,"__loader_flags__", 0x0),
291 { NULL, 0, 0, NULL, 0 }
296 gld_${EMULATION_NAME}_list_options (FILE *file)
298 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
299 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
300 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
301 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
302 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
303 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
304 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
305 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
306 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
307 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
308 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
309 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
310 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
311 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
312 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
314 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
315 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
316 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
317 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
318 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
319 fprintf (file, _(" --exclude-modules-for-implib mod,mod,...\n"));
320 fprintf (file, _(" Exclude objects, archive members from auto\n"));
321 fprintf (file, _(" export, place into import library instead.\n"));
322 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
323 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
324 fprintf (file, _(" --out-implib <file> Generate import library\n"));
325 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
326 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
327 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
328 create __imp_<SYMBOL> as well.\n"));
329 fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n\
330 unless user specifies one\n"));
331 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
332 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
333 an importlib, use <string><basename>.dll\n\
334 in preference to lib<basename>.dll \n"));
335 fprintf (file, _(" --enable-auto-import Do sophistcated linking of _sym to\n\
336 __imp_sym for DATA references\n"));
337 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
338 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
339 adding pseudo-relocations resolved at\n\
341 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
342 auto-imported DATA.\n"));
343 fprintf (file, _(" --enable-extra-pep-debug Enable verbose debug output when building\n\
344 or linking to DLLs (esp. auto-import)\n"));
345 fprintf (file, _(" --enable-long-section-names Use long COFF section names even in\n\
346 executable image files\n"));
347 fprintf (file, _(" --disable-long-section-names Never use long COFF section names, even\n\
348 in object files\n"));
354 set_pep_name (char *name, bfd_vma val)
358 /* Find the name and set it. */
359 for (i = 0; init[i].ptr; i++)
361 if (strcmp (name, init[i].symbol) == 0)
365 if (strcmp (name,"__image_base__") == 0)
366 set_pep_name ("___ImageBase", val);
375 set_pep_subsystem (void)
379 const char *initial_symbol_char;
384 unsigned long temp_subsystem;
393 { "native", 1, "NtProcessStartup" },
394 { "windows", 2, "WinMainCRTStartup" },
395 { "console", 3, "mainCRTStartup" },
396 { "posix", 7, "__PosixProcessStartup"},
397 { "wince", 9, "_WinMainCRTStartup" },
398 { "xbox", 14, "mainCRTStartup" },
401 /* Entry point name for arbitrary subsystem numbers. */
402 static const char default_entry[] = "mainCRTStartup";
404 /* Check for the presence of a version number. */
405 sver = strchr (optarg, ':');
407 len = strlen (optarg);
411 set_pep_name ("__major_subsystem_version__",
412 strtoul (sver + 1, &end, 0));
414 set_pep_name ("__minor_subsystem_version__",
415 strtoul (end + 1, &end, 0));
417 einfo (_("%P: warning: bad version number in -subsystem option\n"));
420 /* Check for numeric subsystem. */
421 temp_subsystem = strtoul (optarg, & end, 0);
422 if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
424 /* Search list for a numeric match to use its entry point. */
425 for (i = 0; v[i].name; i++)
426 if (v[i].value == (int) temp_subsystem)
429 /* If no match, use the default. */
430 if (v[i].name != NULL)
433 entry = default_entry;
435 /* Use this subsystem. */
436 subsystem = (int) temp_subsystem;
440 /* Search for subsystem by name. */
441 for (i = 0; v[i].name; i++)
442 if (strncmp (optarg, v[i].name, len) == 0
443 && v[i].name[len] == '\0')
446 if (v[i].name == NULL)
448 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
453 subsystem = v[i].value;
456 set_pep_name ("__subsystem__", subsystem);
458 initial_symbol_char = ${INITIAL_SYMBOL_CHAR};
459 if (*initial_symbol_char != '\0')
463 /* lang_default_entry expects its argument to be permanently
464 allocated, so we don't free this string. */
465 alc_entry = xmalloc (strlen (initial_symbol_char)
468 strcpy (alc_entry, initial_symbol_char);
469 strcat (alc_entry, entry);
473 lang_default_entry (entry);
480 set_pep_value (char *name)
484 set_pep_name (name, (bfd_vma) strtoull (optarg, &end, 0));
487 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
494 set_pep_stack_heap (char *resname, char *comname)
496 set_pep_value (resname);
501 set_pep_value (comname);
504 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
509 gld${EMULATION_NAME}_handle_option (int optc)
516 case OPTION_BASE_FILE:
517 link_info.base_file = fopen (optarg, FOPEN_WB);
518 if (link_info.base_file == NULL)
520 /* xgettext:c-format */
521 fprintf (stderr, _("%s: Can't open base file %s\n"),
522 program_name, optarg);
529 set_pep_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
532 set_pep_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
534 case OPTION_SUBSYSTEM:
535 set_pep_subsystem ();
537 case OPTION_MAJOR_OS_VERSION:
538 set_pep_value ("__major_os_version__");
540 case OPTION_MINOR_OS_VERSION:
541 set_pep_value ("__minor_os_version__");
543 case OPTION_MAJOR_SUBSYSTEM_VERSION:
544 set_pep_value ("__major_subsystem_version__");
546 case OPTION_MINOR_SUBSYSTEM_VERSION:
547 set_pep_value ("__minor_subsystem_version__");
549 case OPTION_MAJOR_IMAGE_VERSION:
550 set_pep_value ("__major_image_version__");
552 case OPTION_MINOR_IMAGE_VERSION:
553 set_pep_value ("__minor_image_version__");
555 case OPTION_FILE_ALIGNMENT:
556 set_pep_value ("__file_alignment__");
558 case OPTION_SECTION_ALIGNMENT:
559 set_pep_value ("__section_alignment__");
562 set_pep_name ("__dll__", 1);
564 case OPTION_IMAGE_BASE:
565 set_pep_value ("__image_base__");
567 case OPTION_SUPPORT_OLD_CODE:
568 support_old_code = 1;
570 case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
571 pep_use_nul_prefixed_import_tables = TRUE;
575 pep_out_def_filename = xstrdup (optarg);
577 case OPTION_EXPORT_ALL:
578 pep_dll_export_everything = 1;
580 case OPTION_EXCLUDE_SYMBOLS:
581 pep_dll_add_excludes (optarg, EXCLUDESYMS);
583 case OPTION_EXCLUDE_LIBS:
584 pep_dll_add_excludes (optarg, EXCLUDELIBS);
586 case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
587 pep_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
589 case OPTION_KILL_ATS:
590 pep_dll_kill_ats = 1;
592 case OPTION_STDCALL_ALIASES:
593 pep_dll_stdcall_aliases = 1;
595 case OPTION_ENABLE_STDCALL_FIXUP:
596 pep_enable_stdcall_fixup = 1;
598 case OPTION_DISABLE_STDCALL_FIXUP:
599 pep_enable_stdcall_fixup = 0;
601 case OPTION_IMPLIB_FILENAME:
602 pep_implib_filename = xstrdup (optarg);
604 case OPTION_WARN_DUPLICATE_EXPORTS:
605 pep_dll_warn_dup_exports = 1;
607 case OPTION_IMP_COMPAT:
608 pep_dll_compat_implib = 1;
610 case OPTION_ENABLE_AUTO_IMAGE_BASE:
611 pep_enable_auto_image_base = 1;
613 case OPTION_DISABLE_AUTO_IMAGE_BASE:
614 pep_enable_auto_image_base = 0;
616 case OPTION_DLL_SEARCH_PREFIX:
617 pep_dll_search_prefix = xstrdup (optarg);
619 case OPTION_NO_DEFAULT_EXCLUDES:
620 pep_dll_do_default_excludes = 0;
622 case OPTION_DLL_ENABLE_AUTO_IMPORT:
623 link_info.pei386_auto_import = 1;
625 case OPTION_DLL_DISABLE_AUTO_IMPORT:
626 link_info.pei386_auto_import = 0;
628 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
629 link_info.pei386_runtime_pseudo_reloc = 2;
631 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
632 link_info.pei386_runtime_pseudo_reloc = 0;
634 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1:
635 link_info.pei386_runtime_pseudo_reloc = 1;
637 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2:
638 link_info.pei386_runtime_pseudo_reloc = 2;
640 case OPTION_ENABLE_EXTRA_PE_DEBUG:
641 pep_dll_extra_pe_debug = 1;
644 case OPTION_ENABLE_LONG_SECTION_NAMES:
645 pep_use_coff_long_section_names = 1;
647 case OPTION_DISABLE_LONG_SECTION_NAMES:
648 pep_use_coff_long_section_names = 0;
657 strhash (const char *str)
659 const unsigned char *s;
666 s = (const unsigned char *) str;
667 while ((c = *s++) != '\0')
669 hash += c + (c << 17);
673 hash += len + (len << 17);
679 /* Use the output file to create a image base for relocatable DLLs. */
682 compute_dll_image_base (const char *ofile)
684 bfd_vma hash = (bfd_vma) strhash (ofile);
685 return 0x61300000 + ((hash << 16) & 0x0FFC0000);
689 /* Assign values to the special symbols before the linker script is
693 gld_${EMULATION_NAME}_set_symbols (void)
695 /* Run through and invent symbols for all the
696 names and insert the defaults. */
699 if (!init[IMAGEBASEOFF].inited)
701 if (link_info.relocatable)
702 init[IMAGEBASEOFF].value = 0;
703 else if (init[DLLOFF].value || (link_info.shared && !link_info.pie))
705 init[IMAGEBASEOFF].value = (pep_enable_auto_image_base) ?
706 compute_dll_image_base (output_filename) : NT_DLL_IMAGE_BASE;
708 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
711 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
712 init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
715 /* Don't do any symbol assignments if this is a relocatable link. */
716 if (link_info.relocatable)
719 /* Glue the assignments into the abs section. */
720 push_stat_ptr (&abs_output_section->children);
722 for (j = 0; init[j].ptr; j++)
724 bfd_vma val = init[j].value;
725 lang_assignment_statement_type *rv;
726 rv = lang_add_assignment (exp_assop ('=', init[j].symbol,
728 if (init[j].size == sizeof (short))
729 *(short *) init[j].ptr = (short) val;
730 else if (init[j].size == sizeof (int))
731 *(int *) init[j].ptr = (int) val;
732 else if (init[j].size == sizeof (long))
733 *(long *) init[j].ptr = (long) val;
734 /* This might be a long long or other special type. */
735 else if (init[j].size == sizeof (bfd_vma))
736 *(bfd_vma *) init[j].ptr = val;
738 if (j == IMAGEBASEOFF)
739 image_base_statement = rv;
741 /* Restore the pointer. */
744 if (pep.FileAlignment > pep.SectionAlignment)
746 einfo (_("%P: warning, file alignment > section alignment.\n"));
750 /* This is called after the linker script and the command line options
754 gld_${EMULATION_NAME}_after_parse (void)
756 /* The Windows libraries are designed for the linker to treat the
757 entry point as an undefined symbol. Otherwise, the .obj that
758 defines mainCRTStartup is brought in because it is the first
759 encountered in libc.lib and it has other symbols in it which will
760 be pulled in by the link process. To avoid this, we act as
761 though the user specified -u with the entry point symbol.
763 This function is called after the linker script and command line
764 options have been read, so at this point we know the right entry
765 point. This function is called before the input files are
766 opened, so registering the symbol as undefined will make a
769 if (! link_info.relocatable && entry_symbol.name != NULL)
770 ldlang_add_undef (entry_symbol.name);
773 /* pep-dll.c directly accesses pep_data_import_dll,
774 so it must be defined outside of #ifdef DLL_SUPPORT.
775 Note - this variable is deliberately not initialised.
776 This allows it to be treated as a common varaible, and only
777 exist in one incarnation in a multiple target enabled linker. */
778 char * pep_data_import_dll;
781 static struct bfd_link_hash_entry *pep_undef_found_sym;
784 pep_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
789 sl = strlen (string);
790 if (h->type == bfd_link_hash_defined
791 && strncmp (h->root.string, string, sl) == 0
792 && h->root.string[sl] == '@')
794 pep_undef_found_sym = h;
801 pep_fixup_stdcalls (void)
803 static int gave_warning_message = 0;
804 struct bfd_link_hash_entry *undef, *sym;
806 if (pep_dll_extra_pe_debug)
807 printf ("%s\n", __FUNCTION__);
809 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
810 if (undef->type == bfd_link_hash_undefined)
812 char* at = strchr (undef->root.string, '@');
813 int lead_at = (*undef->root.string == '@');
814 /* For now, don't try to fixup fastcall symbols. */
818 /* The symbol is a stdcall symbol, so let's look for a
819 cdecl symbol with the same name and resolve to that. */
820 char *cname = xstrdup (undef->root.string /* + lead_at */);
821 at = strchr (cname, '@');
823 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
825 if (sym && sym->type == bfd_link_hash_defined)
827 undef->type = bfd_link_hash_defined;
828 undef->u.def.value = sym->u.def.value;
829 undef->u.def.section = sym->u.def.section;
831 if (pep_enable_stdcall_fixup == -1)
833 einfo (_("Warning: resolving %s by linking to %s\n"),
834 undef->root.string, cname);
835 if (! gave_warning_message)
837 gave_warning_message = 1;
838 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
839 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
846 /* The symbol is a cdecl symbol, so we look for stdcall
847 symbols - which means scanning the whole symbol table. */
848 pep_undef_found_sym = 0;
849 bfd_link_hash_traverse (link_info.hash, pep_undef_cdecl_match,
850 (char *) undef->root.string);
851 sym = pep_undef_found_sym;
854 undef->type = bfd_link_hash_defined;
855 undef->u.def.value = sym->u.def.value;
856 undef->u.def.section = sym->u.def.section;
858 if (pep_enable_stdcall_fixup == -1)
860 einfo (_("Warning: resolving %s by linking to %s\n"),
861 undef->root.string, sym->root.string);
862 if (! gave_warning_message)
864 gave_warning_message = 1;
865 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
866 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
875 make_import_fixup (arelent *rel, asection *s)
877 struct bfd_symbol *sym = *rel->sym_ptr_ptr;
882 if (pep_dll_extra_pe_debug)
883 printf ("arelent: %s@%#lx: add=%li\n", sym->name,
884 (unsigned long) rel->address, (long) rel->addend);
886 memset (addend, 0, sizeof (addend));
887 switch ((rel->howto->bitsize))
890 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 1);
891 if (suc && rel->howto->pc_relative)
892 _addend = (bfd_vma) ((bfd_signed_vma) ((char) bfd_get_8 (s->owner, addend)));
894 _addend = ((bfd_vma) bfd_get_8 (s->owner, addend)) & 0xff;
897 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 2);
898 if (suc && rel->howto->pc_relative)
899 _addend = (bfd_vma) ((bfd_signed_vma) ((short) bfd_get_16 (s->owner, addend)));
901 _addend = ((bfd_vma) bfd_get_16 (s->owner, addend)) & 0xffff;
904 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 4);
905 if (suc && rel->howto->pc_relative)
906 _addend = (bfd_vma) ((bfd_signed_vma) ((int) bfd_get_32 (s->owner, addend)));
908 _addend = ((bfd_vma) bfd_get_32 (s->owner, addend)) & 0xffffffff;
911 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 8);
913 _addend = ((bfd_vma) bfd_get_64 (s->owner, addend));
917 einfo (_("%C: Cannot get section contents - auto-import exception\n"),
918 s->owner, s, rel->address);
920 if (pep_dll_extra_pe_debug)
922 printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx", (long) _addend, (long) rel->addend, (long) rel->address);
923 if (rel->howto->pc_relative) printf (" pcrel");
924 printf (" %d bit rel.\n",(int) rel->howto->bitsize);
926 pep_create_import_fixup (rel, s, _addend);
932 pep_find_data_imports (void)
934 struct bfd_link_hash_entry *undef, *sym;
936 if (link_info.pei386_auto_import == 0)
939 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
941 if (undef->type == bfd_link_hash_undefined)
943 /* C++ symbols are *long*. */
946 if (pep_dll_extra_pe_debug)
947 printf ("%s:%s\n", __FUNCTION__, undef->root.string);
949 sprintf (buf, "__imp_%s", undef->root.string);
951 sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
953 if (sym && sym->type == bfd_link_hash_defined)
955 bfd *b = sym->u.def.section->owner;
959 if (link_info.pei386_auto_import == -1)
961 static bfd_boolean warned = FALSE;
963 info_msg (_("Info: resolving %s by linking to %s (auto-import)\n"),
964 undef->root.string, buf);
966 /* PR linker/4844. */
970 einfo (_("%P: warning: auto-importing has been activated without --enable-auto-import specified on the command line.\n\
971 This should work unless it involves constant data structures referencing symbols from auto-imported DLLs.\n"));
975 if (!bfd_generic_link_read_symbols (b))
977 einfo (_("%B%F: could not read symbols: %E\n"), b);
981 symbols = bfd_get_outsymbols (b);
982 nsyms = bfd_get_symcount (b);
984 for (i = 0; i < nsyms; i++)
986 if (! CONST_STRNEQ (symbols[i]->name, "__head_"))
989 if (pep_dll_extra_pe_debug)
990 printf ("->%s\n", symbols[i]->name);
992 pep_data_import_dll = (char*) (symbols[i]->name +
993 sizeof ("__head_") - 1);
997 pep_walk_relocs_of_symbol (&link_info, undef->root.string,
1000 /* Let's differentiate it somehow from defined. */
1001 undef->type = bfd_link_hash_defweak;
1002 /* We replace original name with __imp_ prefixed, this
1003 1) may trash memory 2) leads to duplicate symbol generation.
1004 Still, IMHO it's better than having name poluted. */
1005 undef->root.string = sym->root.string;
1006 undef->u.def.value = sym->u.def.value;
1007 undef->u.def.section = sym->u.def.section;
1014 pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
1016 if (pep_dll_extra_pe_debug)
1017 printf ("+%s\n", h->string);
1021 #endif /* DLL_SUPPORT */
1024 debug_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
1026 int *found = (int *) obj;
1027 if (strncmp (".debug_", sect->name, sizeof (".debug_") - 1) == 0)
1032 gld_${EMULATION_NAME}_after_open (void)
1035 if (pep_dll_extra_pe_debug)
1038 struct bfd_link_hash_entry *sym;
1040 printf ("%s()\n", __FUNCTION__);
1042 for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1043 printf ("-%s\n", sym->root.string);
1044 bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
1046 for (a = link_info.input_bfds; a; a = a->link_next)
1047 printf ("*%s\n",a->filename);
1051 /* Pass the wacky PE command line options into the output bfd.
1052 FIXME: This should be done via a function, rather than by
1053 including an internal BFD header. */
1055 if (coff_data (link_info.output_bfd) == NULL
1056 || coff_data (link_info.output_bfd)->pe == 0)
1057 einfo (_("%F%P: cannot perform PE operations on non PE output file '%B'.\n"),
1058 link_info.output_bfd);
1060 pe_data (link_info.output_bfd)->pe_opthdr = pep;
1061 pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
1062 pe_data (link_info.output_bfd)->real_flags |= real_flags;
1064 /* At this point we must decide whether to use long section names
1065 in the output or not. If the user hasn't explicitly specified
1066 on the command line, we leave it to the default for the format
1067 (object files yes, image files no), except if there is debug
1068 information present; GDB relies on the long section names to
1069 find it, so enable it in that case. */
1070 if (pep_use_coff_long_section_names < 0 && link_info.strip == strip_none)
1072 /* Iterate over all sections of all input BFDs, checking
1073 for any that begin 'debug_' and are long names. */
1074 LANG_FOR_EACH_INPUT_STATEMENT (is)
1076 int found_debug = 0;
1077 bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug);
1080 pep_use_coff_long_section_names = 1;
1086 pep_output_file_set_long_section_names (link_info.output_bfd);
1089 if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
1090 pep_fixup_stdcalls ();
1092 pep_process_import_defs (link_info.output_bfd, &link_info);
1094 pep_find_data_imports ();
1096 #ifndef TARGET_IS_i386pep
1097 if (link_info.shared)
1099 if (!link_info.relocatable)
1101 pep_dll_build_sections (link_info.output_bfd, &link_info);
1103 #ifndef TARGET_IS_i386pep
1105 pep_exe_build_sections (link_info.output_bfd, &link_info);
1107 #endif /* DLL_SUPPORT */
1110 /* This next chunk of code tries to detect the case where you have
1111 two import libraries for the same DLL (specifically,
1112 symbolically linking libm.a and libc.a in cygwin to
1113 libcygwin.a). In those cases, it's possible for function
1114 thunks from the second implib to be used but without the
1115 head/tail objects, causing an improper import table. We detect
1116 those cases and rename the "other" import libraries to match
1117 the one the head/tail come from, so that the linker will sort
1118 things nicely and produce a valid import table. */
1120 LANG_FOR_EACH_INPUT_STATEMENT (is)
1122 if (is->the_bfd->my_archive)
1124 int idata2 = 0, reloc_count=0, is_imp = 0;
1127 /* See if this is an import library thunk. */
1128 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1130 if (strcmp (sec->name, ".idata\$2") == 0)
1132 if (CONST_STRNEQ (sec->name, ".idata\$"))
1134 reloc_count += sec->reloc_count;
1137 if (is_imp && !idata2 && reloc_count)
1139 /* It is, look for the reference to head and see if it's
1140 from our own library. */
1141 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1149 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1153 if (!bfd_generic_link_read_symbols (is->the_bfd))
1155 einfo (_("%B%F: could not read symbols: %E\n"),
1159 symbols = bfd_get_outsymbols (is->the_bfd);
1161 relocs = xmalloc ((size_t) relsize);
1162 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1167 einfo ("%X%P: unable to process relocs: %E\n");
1171 for (i = 0; i < nrelocs; i++)
1173 struct bfd_symbol *s;
1174 struct bfd_link_hash_entry * blhe;
1175 char *other_bfd_filename;
1178 s = (relocs[i]->sym_ptr_ptr)[0];
1180 if (s->flags & BSF_LOCAL)
1183 /* Thunk section with reloc to another bfd. */
1184 blhe = bfd_link_hash_lookup (link_info.hash,
1186 FALSE, FALSE, TRUE);
1189 || blhe->type != bfd_link_hash_defined)
1193 = blhe->u.def.section->owner->my_archive
1194 ? bfd_get_filename (blhe->u.def.section->owner->my_archive)
1195 : bfd_get_filename (blhe->u.def.section->owner);
1197 if (strcmp (bfd_get_filename (is->the_bfd->my_archive),
1198 other_bfd_filename) == 0)
1201 /* Rename this implib to match the other one. */
1202 n = xmalloc (strlen (other_bfd_filename) + 1);
1203 strcpy (n, other_bfd_filename);
1204 is->the_bfd->my_archive->filename = n;
1208 /* Note - we do not free the symbols,
1209 they are now cached in the BFD. */
1219 lang_input_statement_type *is2;
1220 lang_input_statement_type *is3;
1222 /* Careful - this is a shell script. Watch those dollar signs! */
1223 /* Microsoft import libraries have every member named the same,
1224 and not in the right order for us to link them correctly. We
1225 must detect these and rename the members so that they'll link
1226 correctly. There are three types of objects: the head, the
1227 thunks, and the sentinel(s). The head is easy; it's the one
1228 with idata2. We assume that the sentinels won't have relocs,
1229 and the thunks will. It's easier than checking the symbol
1230 table for external references. */
1231 LANG_FOR_EACH_INPUT_STATEMENT (is)
1233 if (is->the_bfd->my_archive)
1236 bfd *arch = is->the_bfd->my_archive;
1238 if (cur_arch != arch)
1244 is3 && is3->the_bfd->my_archive == arch;
1245 is3 = (lang_input_statement_type *) is3->next)
1247 /* A MS dynamic import library can also contain static
1248 members, so look for the first element with a .dll
1249 extension, and use that for the remainder of the
1251 pnt = strrchr (is3->the_bfd->filename, '.');
1252 if (pnt != NULL && strcmp (pnt, ".dll") == 0)
1260 /* OK, found one. Now look to see if the remaining
1261 (dynamic import) members use the same name. */
1263 is2 && is2->the_bfd->my_archive == arch;
1264 is2 = (lang_input_statement_type *) is2->next)
1266 /* Skip static members, ie anything with a .obj
1268 pnt = strrchr (is2->the_bfd->filename, '.');
1269 if (pnt != NULL && strcmp (pnt, ".obj") == 0)
1272 if (strcmp (is3->the_bfd->filename,
1273 is2->the_bfd->filename))
1282 /* This fragment might have come from an .obj file in a Microsoft
1283 import, and not an actual import record. If this is the case,
1284 then leave the filename alone. */
1285 pnt = strrchr (is->the_bfd->filename, '.');
1287 if (is_ms_arch && (strcmp (pnt, ".dll") == 0))
1289 int idata2 = 0, reloc_count=0;
1291 char *new_name, seq;
1293 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1295 if (strcmp (sec->name, ".idata\$2") == 0)
1297 reloc_count += sec->reloc_count;
1300 if (idata2) /* .idata2 is the TOC */
1302 else if (reloc_count > 0) /* thunks */
1307 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
1308 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
1309 is->the_bfd->filename = new_name;
1311 new_name = xmalloc (strlen (is->filename) + 3);
1312 sprintf (new_name, "%s.%c", is->filename, seq);
1313 is->filename = new_name;
1321 gld_${EMULATION_NAME}_before_allocation (void)
1323 before_allocation_default ();
1327 /* This is called when an input file isn't recognized as a BFD. We
1328 check here for .DEF files and pull them in automatically. */
1331 saw_option (char *option)
1335 for (i = 0; init[i].ptr; i++)
1336 if (strcmp (init[i].symbol, option) == 0)
1337 return init[i].inited;
1340 #endif /* DLL_SUPPORT */
1343 gld_${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1346 const char *ext = entry->filename + strlen (entry->filename) - 4;
1348 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
1350 pep_def_file = def_file_parse (entry->filename, pep_def_file);
1354 int i, buflen=0, len;
1357 for (i = 0; i < pep_def_file->num_exports; i++)
1359 len = strlen (pep_def_file->exports[i].internal_name);
1360 if (buflen < len + 2)
1364 buf = xmalloc (buflen);
1366 for (i = 0; i < pep_def_file->num_exports; i++)
1368 struct bfd_link_hash_entry *h;
1370 sprintf (buf, "_%s", pep_def_file->exports[i].internal_name);
1372 h = bfd_link_hash_lookup (link_info.hash, buf, TRUE, TRUE, TRUE);
1373 if (h == (struct bfd_link_hash_entry *) NULL)
1374 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1375 if (h->type == bfd_link_hash_new)
1377 h->type = bfd_link_hash_undefined;
1378 h->u.undef.abfd = NULL;
1379 bfd_link_add_undef (link_info.hash, h);
1384 /* def_file_print (stdout, pep_def_file); */
1385 if (pep_def_file->is_dll == 1)
1386 link_info.shared = 1;
1388 if (pep_def_file->base_address != (bfd_vma)(-1))
1391 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1392 = init[IMAGEBASEOFF].value
1393 = pep_def_file->base_address;
1394 init[IMAGEBASEOFF].inited = 1;
1395 if (image_base_statement)
1396 image_base_statement->exp = exp_assop ('=', "__image_base__",
1397 exp_intop (pep.ImageBase));
1400 if (pep_def_file->stack_reserve != -1
1401 && ! saw_option ("__size_of_stack_reserve__"))
1403 pep.SizeOfStackReserve = pep_def_file->stack_reserve;
1404 if (pep_def_file->stack_commit != -1)
1405 pep.SizeOfStackCommit = pep_def_file->stack_commit;
1407 if (pep_def_file->heap_reserve != -1
1408 && ! saw_option ("__size_of_heap_reserve__"))
1410 pep.SizeOfHeapReserve = pep_def_file->heap_reserve;
1411 if (pep_def_file->heap_commit != -1)
1412 pep.SizeOfHeapCommit = pep_def_file->heap_commit;
1422 gld_${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1425 #ifdef TARGET_IS_i386pep
1426 pep_dll_id_target ("pei-x86-64");
1428 if (pep_bfd_is_dll (entry->the_bfd))
1429 return pep_implied_import_dll (entry->filename);
1435 gld_${EMULATION_NAME}_finish (void)
1440 if (link_info.shared
1441 || (!link_info.relocatable && pep_def_file->num_exports != 0))
1443 pep_dll_fill_sections (link_info.output_bfd, &link_info);
1444 if (pep_implib_filename)
1445 pep_dll_generate_implib (pep_def_file, pep_implib_filename, &link_info);
1448 if (pep_out_def_filename)
1449 pep_dll_generate_def_file (pep_out_def_filename);
1450 #endif /* DLL_SUPPORT */
1452 /* I don't know where .idata gets set as code, but it shouldn't be. */
1454 asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
1458 asec->flags &= ~SEC_CODE;
1459 asec->flags |= SEC_DATA;
1465 /* Place an orphan section.
1467 We use this to put sections in a reasonable place in the file, and
1468 to ensure that they are aligned as required.
1470 We handle grouped sections here as well. A section named .foo\$nn
1471 goes into the output section .foo. All grouped sections are sorted
1474 Grouped sections for the default sections are handled by the
1475 default linker script using wildcards, and are sorted by
1478 static lang_output_section_statement_type *
1479 gld_${EMULATION_NAME}_place_orphan (asection *s,
1480 const char *secname,
1483 const char *orig_secname = secname;
1484 char *dollar = NULL;
1485 lang_output_section_statement_type *os;
1486 lang_statement_list_type add_child;
1488 /* Look through the script to see where to place this section. */
1489 if (!link_info.relocatable
1490 && (dollar = strchr (secname, '\$')) != NULL)
1492 size_t len = dollar - secname;
1493 char *newname = xmalloc (len + 1);
1494 memcpy (newname, secname, len);
1495 newname[len] = '\0';
1499 lang_list_init (&add_child);
1502 && (os = lang_output_section_find (secname)) != NULL
1503 && os->bfd_section != NULL
1504 && (os->bfd_section->flags == 0
1505 || ((s->flags ^ os->bfd_section->flags)
1506 & (SEC_LOAD | SEC_ALLOC)) == 0))
1508 /* We already have an output section statement with this
1509 name, and its bfd section has compatible flags.
1510 If the section already exists but does not have any flags set,
1511 then it has been created by the linker, probably as a result of
1512 a --section-start command line switch. */
1513 lang_add_section (&add_child, s, os);
1517 static struct orphan_save hold[] =
1520 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1523 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1526 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1532 enum orphan_save_index
1539 static int orphan_init_done = 0;
1540 struct orphan_save *place;
1541 lang_output_section_statement_type *after;
1542 etree_type *address;
1544 if (!orphan_init_done)
1546 struct orphan_save *ho;
1547 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1548 if (ho->name != NULL)
1550 ho->os = lang_output_section_find (ho->name);
1551 if (ho->os != NULL && ho->os->flags == 0)
1552 ho->os->flags = ho->flags;
1554 orphan_init_done = 1;
1557 /* Try to put the new output section in a reasonable place based
1558 on the section name and section flags. */
1561 if ((s->flags & SEC_ALLOC) == 0)
1563 else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
1564 place = &hold[orphan_bss];
1565 else if ((s->flags & SEC_READONLY) == 0)
1566 place = &hold[orphan_data];
1567 else if ((s->flags & SEC_CODE) == 0)
1568 place = &hold[orphan_rodata];
1570 place = &hold[orphan_text];
1575 if (place->os == NULL)
1576 place->os = lang_output_section_find (place->name);
1579 after = lang_output_section_find_by_flags (s, &place->os, NULL);
1581 /* *ABS* is always the first output section statement. */
1582 after = (&lang_output_section_statement.head
1583 ->output_section_statement);
1586 /* All sections in an executable must be aligned to a page boundary. */
1587 address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
1588 os = lang_insert_orphan (s, secname, constraint, after, place, address,
1593 lang_statement_union_type **pl = &os->children.head;
1597 bfd_boolean found_dollar;
1599 /* The section name has a '\$'. Sort it with the other '\$'
1601 found_dollar = FALSE;
1602 for ( ; *pl != NULL; pl = &(*pl)->header.next)
1604 lang_input_section_type *ls;
1607 if ((*pl)->header.type != lang_input_section_enum)
1610 ls = &(*pl)->input_section;
1612 lname = bfd_get_section_name (ls->section->owner, ls->section);
1613 if (strchr (lname, '\$') == NULL)
1620 found_dollar = TRUE;
1621 if (strcmp (orig_secname, lname) < 0)
1627 if (add_child.head != NULL)
1629 add_child.head->header.next = *pl;
1630 *pl = add_child.head;
1638 gld_${EMULATION_NAME}_open_dynamic_archive
1639 (const char *arch ATTRIBUTE_UNUSED,
1640 search_dirs_type *search,
1641 lang_input_statement_type *entry)
1645 const char * format;
1646 bfd_boolean use_prefix;
1650 /* Preferred explicit import library for dll's. */
1651 { "lib%s.dll.a", FALSE },
1652 /* Alternate explicit import library for dll's. */
1653 { "%s.dll.a", FALSE },
1654 /* "libfoo.a" could be either an import lib or a static lib.
1655 For backwards compatibility, libfoo.a needs to precede
1656 libfoo.dll and foo.dll in the search. */
1657 { "lib%s.a", FALSE },
1658 /* The 'native' spelling of an import lib name is "foo.lib". */
1659 { "%s.lib", FALSE },
1661 /* Try "<prefix>foo.dll" (preferred dll name, if specified). */
1662 { "%s%s.dll", TRUE },
1664 /* Try "libfoo.dll" (default preferred dll name). */
1665 { "lib%s.dll", FALSE },
1666 /* Finally try 'native' dll name "foo.dll". */
1667 { "%s.dll", FALSE },
1668 /* Note: If adding more formats to this table, make sure to check to
1669 see if their length is longer than libname_fmt[0].format, and if
1670 so, update the call to xmalloc() below. */
1673 static unsigned int format_max_len = 0;
1674 const char * filename;
1680 if (! entry->is_archive)
1683 filename = entry->filename;
1685 if (format_max_len == 0)
1686 /* We need to allow space in the memory that we are going to allocate
1687 for the characters in the format string. Since the format array is
1688 static we only need to calculate this information once. In theory
1689 this value could also be computed statically, but this introduces
1690 the possibility for a discrepancy and hence a possible memory
1691 corruption. The lengths we compute here will be too long because
1692 they will include any formating characters (%s) in the strings, but
1693 this will not matter. */
1694 for (i = 0; libname_fmt[i].format; i++)
1695 if (format_max_len < strlen (libname_fmt[i].format))
1696 format_max_len = strlen (libname_fmt[i].format);
1698 full_string = xmalloc (strlen (search->name)
1702 + (pep_dll_search_prefix
1703 ? strlen (pep_dll_search_prefix) : 0)
1705 /* Allow for the terminating NUL and for the path
1706 separator character that is inserted between
1707 search->name and the start of the format string. */
1710 sprintf (full_string, "%s/", search->name);
1711 base_string = full_string + strlen (full_string);
1713 for (i = 0; libname_fmt[i].format; i++)
1716 if (libname_fmt[i].use_prefix)
1718 if (!pep_dll_search_prefix)
1720 sprintf (base_string, libname_fmt[i].format, pep_dll_search_prefix, filename);
1724 sprintf (base_string, libname_fmt[i].format, filename);
1726 if (ldfile_try_open_bfd (full_string, entry))
1730 if (!libname_fmt[i].format)
1736 entry->filename = full_string;
1742 gld_${EMULATION_NAME}_find_potential_libraries
1743 (char *name, lang_input_statement_type *entry)
1745 return ldfile_open_file_search (name, entry, "", ".lib");
1749 gld_${EMULATION_NAME}_get_script (int *isfile)
1751 # Scripts compiled in.
1752 # sed commands to quote an ld script as a C string.
1753 sc="-f stringify.sed"
1759 if (link_info.relocatable && config.build_constructors)
1762 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1763 echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c
1764 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1765 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1766 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1767 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1768 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1769 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
1770 echo ' ; else if (link_info.pei386_auto_import == 1) return' >> e${EMULATION_NAME}.c
1771 sed $sc ldscripts/${EMULATION_NAME}.xa >> e${EMULATION_NAME}.c
1773 echo ' ; else return' >> e${EMULATION_NAME}.c
1774 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1775 echo '; }' >> e${EMULATION_NAME}.c
1780 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1782 gld_${EMULATION_NAME}_before_parse,
1785 gld_${EMULATION_NAME}_after_parse,
1786 gld_${EMULATION_NAME}_after_open,
1787 after_allocation_default,
1788 set_output_arch_default,
1789 ldemul_default_target,
1790 gld_${EMULATION_NAME}_before_allocation,
1791 gld_${EMULATION_NAME}_get_script,
1792 "${EMULATION_NAME}",
1794 gld_${EMULATION_NAME}_finish,
1795 NULL, /* Create output section statements. */
1796 gld_${EMULATION_NAME}_open_dynamic_archive,
1797 gld_${EMULATION_NAME}_place_orphan,
1798 gld_${EMULATION_NAME}_set_symbols,
1799 NULL, /* parse_args */
1800 gld${EMULATION_NAME}_add_options,
1801 gld${EMULATION_NAME}_handle_option,
1802 gld_${EMULATION_NAME}_unrecognized_file,
1803 gld_${EMULATION_NAME}_list_options,
1804 gld_${EMULATION_NAME}_recognized_file,
1805 gld_${EMULATION_NAME}_find_potential_libraries,
1806 NULL /* new_vers_pattern. */