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 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 = -1;
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
180 gld${EMULATION_NAME}_add_options
181 (int ns ATTRIBUTE_UNUSED,
182 char **shortopts ATTRIBUTE_UNUSED,
184 struct option **longopts,
185 int nrl ATTRIBUTE_UNUSED,
186 struct option **really_longopts ATTRIBUTE_UNUSED)
188 static const struct option xtra_long[] =
191 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
192 {"dll", no_argument, NULL, OPTION_DLL},
193 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
194 {"heap", required_argument, NULL, OPTION_HEAP},
195 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
196 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
197 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
198 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
199 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
200 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
201 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
202 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
203 {"stack", required_argument, NULL, OPTION_STACK},
204 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
205 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
207 /* getopt allows abbreviations, so we do this to stop it
208 from treating -o as an abbreviation for this option. */
209 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
210 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
211 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
212 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
213 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
214 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
215 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
216 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
217 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
218 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
219 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
220 /* getopt() allows abbreviations, so we do this to stop it from
221 treating -c as an abbreviation for these --compat-implib. */
222 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
223 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
224 {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
225 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
226 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
227 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
228 {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
229 {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
230 {"enable-extra-pep-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
231 {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
232 {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
234 {NULL, no_argument, NULL, 0}
237 *longopts = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
238 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
241 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
242 parameters which may be input from the command line. */
253 #define D(field,symbol,def) {&pep.field,sizeof(pep.field), def, symbol,0}
255 static definfo init[] =
257 /* imagebase must be first */
258 #define IMAGEBASEOFF 0
259 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
261 {&dll, sizeof(dll), 0, "__dll__", 0},
262 #define MSIMAGEBASEOFF 2
263 D(ImageBase,"___ImageBase", NT_EXE_IMAGE_BASE),
264 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
265 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
266 D(MajorOperatingSystemVersion,"__major_os_version__", 4),
267 D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
268 D(MajorImageVersion,"__major_image_version__", 0),
269 D(MinorImageVersion,"__minor_image_version__", 0),
270 D(MajorSubsystemVersion,"__major_subsystem_version__", 5),
271 D(MinorSubsystemVersion,"__minor_subsystem_version__", 2),
272 D(Subsystem,"__subsystem__", ${SUBSYSTEM}),
273 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000),
274 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
275 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
276 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
277 D(LoaderFlags,"__loader_flags__", 0x0),
278 { NULL, 0, 0, NULL, 0 }
283 gld_${EMULATION_NAME}_list_options (FILE *file)
285 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
286 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
287 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
288 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
289 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
290 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
291 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
292 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
293 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
294 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
295 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
296 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
297 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
298 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
299 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
301 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
302 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
303 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
304 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
305 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
306 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
307 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
308 fprintf (file, _(" --out-implib <file> Generate import library\n"));
309 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
310 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
311 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
312 create __imp_<SYMBOL> as well.\n"));
313 fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n\
314 unless user specifies one\n"));
315 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
316 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
317 an importlib, use <string><basename>.dll\n\
318 in preference to lib<basename>.dll \n"));
319 fprintf (file, _(" --enable-auto-import Do sophistcated linking of _sym to\n\
320 __imp_sym for DATA references\n"));
321 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
322 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
323 adding pseudo-relocations resolved at\n\
325 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
326 auto-imported DATA.\n"));
327 fprintf (file, _(" --enable-extra-pep-debug Enable verbose debug output when building\n\
328 or linking to DLLs (esp. auto-import)\n"));
334 set_pep_name (char *name, bfd_vma val)
338 /* Find the name and set it. */
339 for (i = 0; init[i].ptr; i++)
341 if (strcmp (name, init[i].symbol) == 0)
345 if (strcmp (name,"__image_base__") == 0)
346 set_pep_name ("___ImageBase", val);
355 set_pep_subsystem (void)
359 const char *initial_symbol_char;
364 unsigned long temp_subsystem;
373 { "native", 1, "NtProcessStartup" },
374 { "windows", 2, "WinMainCRTStartup" },
375 { "console", 3, "mainCRTStartup" },
376 { "posix", 7, "__PosixProcessStartup"},
377 { "wince", 9, "_WinMainCRTStartup" },
378 { "xbox", 14, "mainCRTStartup" },
381 /* Entry point name for arbitrary subsystem numbers. */
382 static const char default_entry[] = "mainCRTStartup";
384 /* Check for the presence of a version number. */
385 sver = strchr (optarg, ':');
387 len = strlen (optarg);
391 set_pep_name ("__major_subsystem_version__",
392 strtoul (sver + 1, &end, 0));
394 set_pep_name ("__minor_subsystem_version__",
395 strtoul (end + 1, &end, 0));
397 einfo (_("%P: warning: bad version number in -subsystem option\n"));
400 /* Check for numeric subsystem. */
401 temp_subsystem = strtoul (optarg, & end, 0);
402 if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
404 /* Search list for a numeric match to use its entry point. */
405 for (i = 0; v[i].name; i++)
406 if (v[i].value == (int) temp_subsystem)
409 /* If no match, use the default. */
410 if (v[i].name != NULL)
413 entry = default_entry;
415 /* Use this subsystem. */
416 subsystem = (int) temp_subsystem;
420 /* Search for subsystem by name. */
421 for (i = 0; v[i].name; i++)
422 if (strncmp (optarg, v[i].name, len) == 0
423 && v[i].name[len] == '\0')
426 if (v[i].name == NULL)
428 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
433 subsystem = v[i].value;
436 set_pep_name ("__subsystem__", subsystem);
438 initial_symbol_char = ${INITIAL_SYMBOL_CHAR};
439 if (*initial_symbol_char != '\0')
443 /* lang_default_entry expects its argument to be permanently
444 allocated, so we don't free this string. */
445 alc_entry = xmalloc (strlen (initial_symbol_char)
448 strcpy (alc_entry, initial_symbol_char);
449 strcat (alc_entry, entry);
453 lang_default_entry (entry);
460 set_pep_value (char *name)
464 set_pep_name (name, (bfd_vma) strtoull (optarg, &end, 0));
467 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
474 set_pep_stack_heap (char *resname, char *comname)
476 set_pep_value (resname);
481 set_pep_value (comname);
484 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
489 gld${EMULATION_NAME}_handle_option (int optc)
496 case OPTION_BASE_FILE:
497 link_info.base_file = fopen (optarg, FOPEN_WB);
498 if (link_info.base_file == NULL)
500 /* xgettext:c-format */
501 fprintf (stderr, _("%s: Can't open base file %s\n"),
502 program_name, optarg);
509 set_pep_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
512 set_pep_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
514 case OPTION_SUBSYSTEM:
515 set_pep_subsystem ();
517 case OPTION_MAJOR_OS_VERSION:
518 set_pep_value ("__major_os_version__");
520 case OPTION_MINOR_OS_VERSION:
521 set_pep_value ("__minor_os_version__");
523 case OPTION_MAJOR_SUBSYSTEM_VERSION:
524 set_pep_value ("__major_subsystem_version__");
526 case OPTION_MINOR_SUBSYSTEM_VERSION:
527 set_pep_value ("__minor_subsystem_version__");
529 case OPTION_MAJOR_IMAGE_VERSION:
530 set_pep_value ("__major_image_version__");
532 case OPTION_MINOR_IMAGE_VERSION:
533 set_pep_value ("__minor_image_version__");
535 case OPTION_FILE_ALIGNMENT:
536 set_pep_value ("__file_alignment__");
538 case OPTION_SECTION_ALIGNMENT:
539 set_pep_value ("__section_alignment__");
542 set_pep_name ("__dll__", 1);
544 case OPTION_IMAGE_BASE:
545 set_pep_value ("__image_base__");
547 case OPTION_SUPPORT_OLD_CODE:
548 support_old_code = 1;
552 pep_out_def_filename = xstrdup (optarg);
554 case OPTION_EXPORT_ALL:
555 pep_dll_export_everything = 1;
557 case OPTION_EXCLUDE_SYMBOLS:
558 pep_dll_add_excludes (optarg, 0);
560 case OPTION_EXCLUDE_LIBS:
561 pep_dll_add_excludes (optarg, 1);
563 case OPTION_KILL_ATS:
564 pep_dll_kill_ats = 1;
566 case OPTION_STDCALL_ALIASES:
567 pep_dll_stdcall_aliases = 1;
569 case OPTION_ENABLE_STDCALL_FIXUP:
570 pep_enable_stdcall_fixup = 1;
572 case OPTION_DISABLE_STDCALL_FIXUP:
573 pep_enable_stdcall_fixup = 0;
575 case OPTION_IMPLIB_FILENAME:
576 pep_implib_filename = xstrdup (optarg);
578 case OPTION_WARN_DUPLICATE_EXPORTS:
579 pep_dll_warn_dup_exports = 1;
581 case OPTION_IMP_COMPAT:
582 pep_dll_compat_implib = 1;
584 case OPTION_ENABLE_AUTO_IMAGE_BASE:
585 pep_enable_auto_image_base = 1;
587 case OPTION_DISABLE_AUTO_IMAGE_BASE:
588 pep_enable_auto_image_base = 0;
590 case OPTION_DLL_SEARCH_PREFIX:
591 pep_dll_search_prefix = xstrdup (optarg);
593 case OPTION_NO_DEFAULT_EXCLUDES:
594 pep_dll_do_default_excludes = 0;
596 case OPTION_DLL_ENABLE_AUTO_IMPORT:
597 link_info.pei386_auto_import = 1;
599 case OPTION_DLL_DISABLE_AUTO_IMPORT:
600 link_info.pei386_auto_import = 0;
602 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
603 link_info.pei386_runtime_pseudo_reloc = 1;
605 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
606 link_info.pei386_runtime_pseudo_reloc = 0;
608 case OPTION_ENABLE_EXTRA_PE_DEBUG:
609 pep_dll_extra_pe_debug = 1;
619 strhash (const char *str)
621 const unsigned char *s;
628 s = (const unsigned char *) str;
629 while ((c = *s++) != '\0')
631 hash += c + (c << 17);
635 hash += len + (len << 17);
641 /* Use the output file to create a image base for relocatable DLLs. */
644 compute_dll_image_base (const char *ofile)
646 bfd_vma hash = (bfd_vma) strhash (ofile);
647 return 0x61300000 + ((hash << 16) & 0x0FFC0000);
651 /* Assign values to the special symbols before the linker script is
655 gld_${EMULATION_NAME}_set_symbols (void)
657 /* Run through and invent symbols for all the
658 names and insert the defaults. */
660 lang_statement_list_type *save;
662 if (!init[IMAGEBASEOFF].inited)
664 if (link_info.relocatable)
665 init[IMAGEBASEOFF].value = 0;
666 else if (init[DLLOFF].value || (link_info.shared && !link_info.pie))
668 init[IMAGEBASEOFF].value = (pep_enable_auto_image_base) ?
669 compute_dll_image_base (output_filename) : NT_DLL_IMAGE_BASE;
671 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
674 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
675 init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
678 /* Don't do any symbol assignments if this is a relocatable link. */
679 if (link_info.relocatable)
682 /* Glue the assignments into the abs section. */
685 stat_ptr = &(abs_output_section->children);
687 for (j = 0; init[j].ptr; j++)
689 bfd_vma val = init[j].value;
690 lang_assignment_statement_type *rv;
691 rv = lang_add_assignment (exp_assop ('=', init[j].symbol,
693 if (init[j].size == sizeof (short))
694 *(short *) init[j].ptr = (short) val;
695 else if (init[j].size == sizeof (int))
696 *(int *) init[j].ptr = (int) val;
697 else if (init[j].size == sizeof (long))
698 *(long *) init[j].ptr = (long) val;
699 /* This might be a long long or other special type. */
700 else if (init[j].size == sizeof (bfd_vma))
701 *(bfd_vma *) init[j].ptr = val;
703 if (j == IMAGEBASEOFF)
704 image_base_statement = rv;
706 /* Restore the pointer. */
709 if (pep.FileAlignment > pep.SectionAlignment)
711 einfo (_("%P: warning, file alignment > section alignment.\n"));
715 /* This is called after the linker script and the command line options
719 gld_${EMULATION_NAME}_after_parse (void)
721 /* The Windows libraries are designed for the linker to treat the
722 entry point as an undefined symbol. Otherwise, the .obj that
723 defines mainCRTStartup is brought in because it is the first
724 encountered in libc.lib and it has other symbols in it which will
725 be pulled in by the link process. To avoid this, we act as
726 though the user specified -u with the entry point symbol.
728 This function is called after the linker script and command line
729 options have been read, so at this point we know the right entry
730 point. This function is called before the input files are
731 opened, so registering the symbol as undefined will make a
734 if (! link_info.relocatable && entry_symbol.name != NULL)
735 ldlang_add_undef (entry_symbol.name);
738 /* pep-dll.c directly accesses pep_data_import_dll,
739 so it must be defined outside of #ifdef DLL_SUPPORT.
740 Note - this variable is deliberately not initialised.
741 This allows it to be treated as a common varaible, and only
742 exist in one incarnation in a multiple target enabled linker. */
743 char * pep_data_import_dll;
746 static struct bfd_link_hash_entry *pep_undef_found_sym;
749 pep_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
754 sl = strlen (string);
755 if (h->type == bfd_link_hash_defined
756 && strncmp (h->root.string, string, sl) == 0
757 && h->root.string[sl] == '@')
759 pep_undef_found_sym = h;
766 pep_fixup_stdcalls (void)
768 static int gave_warning_message = 0;
769 struct bfd_link_hash_entry *undef, *sym;
771 if (pep_dll_extra_pe_debug)
772 printf ("%s\n", __FUNCTION__);
774 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
775 if (undef->type == bfd_link_hash_undefined)
777 char* at = strchr (undef->root.string, '@');
778 int lead_at = (*undef->root.string == '@');
779 /* For now, don't try to fixup fastcall symbols. */
783 /* The symbol is a stdcall symbol, so let's look for a
784 cdecl symbol with the same name and resolve to that. */
785 char *cname = xstrdup (undef->root.string /* + lead_at */);
786 at = strchr (cname, '@');
788 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
790 if (sym && sym->type == bfd_link_hash_defined)
792 undef->type = bfd_link_hash_defined;
793 undef->u.def.value = sym->u.def.value;
794 undef->u.def.section = sym->u.def.section;
796 if (pep_enable_stdcall_fixup == -1)
798 einfo (_("Warning: resolving %s by linking to %s\n"),
799 undef->root.string, cname);
800 if (! gave_warning_message)
802 gave_warning_message = 1;
803 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
804 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
811 /* The symbol is a cdecl symbol, so we look for stdcall
812 symbols - which means scanning the whole symbol table. */
813 pep_undef_found_sym = 0;
814 bfd_link_hash_traverse (link_info.hash, pep_undef_cdecl_match,
815 (char *) undef->root.string);
816 sym = pep_undef_found_sym;
819 undef->type = bfd_link_hash_defined;
820 undef->u.def.value = sym->u.def.value;
821 undef->u.def.section = sym->u.def.section;
823 if (pep_enable_stdcall_fixup == -1)
825 einfo (_("Warning: resolving %s by linking to %s\n"),
826 undef->root.string, sym->root.string);
827 if (! gave_warning_message)
829 gave_warning_message = 1;
830 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
831 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
840 make_import_fixup (arelent *rel, asection *s)
842 struct bfd_symbol *sym = *rel->sym_ptr_ptr;
845 if (pep_dll_extra_pe_debug)
846 printf ("arelent: %s@%#lx: add=%li\n", sym->name,
847 (unsigned long) rel->address, (long) rel->addend);
849 if (! bfd_get_section_contents (s->owner, s, addend, rel->address, sizeof (addend)))
850 einfo (_("%C: Cannot get section contents - auto-import exception\n"),
851 s->owner, s, rel->address);
853 pep_create_import_fixup (rel, s, bfd_get_32 (s->owner, addend));
859 pep_find_data_imports (void)
861 struct bfd_link_hash_entry *undef, *sym;
863 if (link_info.pei386_auto_import == 0)
866 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
868 if (undef->type == bfd_link_hash_undefined)
870 /* C++ symbols are *long*. */
873 if (pep_dll_extra_pe_debug)
874 printf ("%s:%s\n", __FUNCTION__, undef->root.string);
876 sprintf (buf, "__imp_%s", undef->root.string);
878 sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
880 if (sym && sym->type == bfd_link_hash_defined)
882 bfd *b = sym->u.def.section->owner;
886 if (link_info.pei386_auto_import == -1)
888 static bfd_boolean warned = FALSE;
890 info_msg (_("Info: resolving %s by linking to %s (auto-import)\n"),
891 undef->root.string, buf);
893 /* PR linker/4844. */
897 einfo (_("%P: warning: auto-importing has been activated without --enable-auto-import specified on the command line.\n\
898 This should work unless it involves constant data structures referencing symbols from auto-imported DLLs.\n"));
902 if (!bfd_generic_link_read_symbols (b))
904 einfo (_("%B%F: could not read symbols: %E\n"), b);
908 symbols = bfd_get_outsymbols (b);
909 nsyms = bfd_get_symcount (b);
911 for (i = 0; i < nsyms; i++)
913 if (! CONST_STRNEQ (symbols[i]->name, "__head_"))
916 if (pep_dll_extra_pe_debug)
917 printf ("->%s\n", symbols[i]->name);
919 pep_data_import_dll = (char*) (symbols[i]->name +
920 sizeof ("__head_") - 1);
924 pep_walk_relocs_of_symbol (&link_info, undef->root.string,
927 /* Let's differentiate it somehow from defined. */
928 undef->type = bfd_link_hash_defweak;
929 /* We replace original name with __imp_ prefixed, this
930 1) may trash memory 2) leads to duplicate symbol generation.
931 Still, IMHO it's better than having name poluted. */
932 undef->root.string = sym->root.string;
933 undef->u.def.value = sym->u.def.value;
934 undef->u.def.section = sym->u.def.section;
941 pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
943 if (pep_dll_extra_pe_debug)
944 printf ("+%s\n", h->string);
948 #endif /* DLL_SUPPORT */
952 gld_${EMULATION_NAME}_after_open (void)
955 if (pep_dll_extra_pe_debug)
958 struct bfd_link_hash_entry *sym;
960 printf ("%s()\n", __FUNCTION__);
962 for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
963 printf ("-%s\n", sym->root.string);
964 bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
966 for (a = link_info.input_bfds; a; a = a->link_next)
967 printf ("*%s\n",a->filename);
971 /* Pass the wacky PE command line options into the output bfd.
972 FIXME: This should be done via a function, rather than by
973 including an internal BFD header. */
975 if (coff_data (link_info.output_bfd) == NULL
976 || coff_data (link_info.output_bfd)->pe == 0)
977 einfo (_("%F%P: cannot perform PE operations on non PE output file '%B'.\n"),
978 link_info.output_bfd);
980 pe_data (link_info.output_bfd)->pe_opthdr = pep;
981 pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
982 pe_data (link_info.output_bfd)->real_flags |= real_flags;
985 if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
986 pep_fixup_stdcalls ();
988 pep_process_import_defs (link_info.output_bfd, &link_info);
990 pep_find_data_imports ();
992 #ifndef TARGET_IS_i386pep
993 if (link_info.shared)
995 if (!link_info.relocatable)
997 pep_dll_build_sections (link_info.output_bfd, &link_info);
999 #ifndef TARGET_IS_i386pep
1001 pep_exe_build_sections (link_info.output_bfd, &link_info);
1003 #endif /* DLL_SUPPORT */
1006 /* This next chunk of code tries to detect the case where you have
1007 two import libraries for the same DLL (specifically,
1008 symbolically linking libm.a and libc.a in cygwin to
1009 libcygwin.a). In those cases, it's possible for function
1010 thunks from the second implib to be used but without the
1011 head/tail objects, causing an improper import table. We detect
1012 those cases and rename the "other" import libraries to match
1013 the one the head/tail come from, so that the linker will sort
1014 things nicely and produce a valid import table. */
1016 LANG_FOR_EACH_INPUT_STATEMENT (is)
1018 if (is->the_bfd->my_archive)
1020 int idata2 = 0, reloc_count=0, is_imp = 0;
1023 /* See if this is an import library thunk. */
1024 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1026 if (strcmp (sec->name, ".idata\$2") == 0)
1028 if (CONST_STRNEQ (sec->name, ".idata\$"))
1030 reloc_count += sec->reloc_count;
1033 if (is_imp && !idata2 && reloc_count)
1035 /* It is, look for the reference to head and see if it's
1036 from our own library. */
1037 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1045 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1049 if (!bfd_generic_link_read_symbols (is->the_bfd))
1051 einfo (_("%B%F: could not read symbols: %E\n"),
1055 symbols = bfd_get_outsymbols (is->the_bfd);
1057 relocs = xmalloc ((size_t) relsize);
1058 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1063 einfo ("%X%P: unable to process relocs: %E\n");
1067 for (i = 0; i < nrelocs; i++)
1069 struct bfd_symbol *s;
1070 struct bfd_link_hash_entry * blhe;
1071 char *other_bfd_filename;
1074 s = (relocs[i]->sym_ptr_ptr)[0];
1076 if (s->flags & BSF_LOCAL)
1079 /* Thunk section with reloc to another bfd. */
1080 blhe = bfd_link_hash_lookup (link_info.hash,
1082 FALSE, FALSE, TRUE);
1085 || blhe->type != bfd_link_hash_defined)
1089 = blhe->u.def.section->owner->my_archive
1090 ? bfd_get_filename (blhe->u.def.section->owner->my_archive)
1091 : bfd_get_filename (blhe->u.def.section->owner);
1093 if (strcmp (bfd_get_filename (is->the_bfd->my_archive),
1094 other_bfd_filename) == 0)
1097 /* Rename this implib to match the other one. */
1098 n = xmalloc (strlen (other_bfd_filename) + 1);
1099 strcpy (n, other_bfd_filename);
1100 is->the_bfd->my_archive->filename = n;
1104 /* Note - we do not free the symbols,
1105 they are now cached in the BFD. */
1115 lang_input_statement_type *is2;
1116 lang_input_statement_type *is3;
1118 /* Careful - this is a shell script. Watch those dollar signs! */
1119 /* Microsoft import libraries have every member named the same,
1120 and not in the right order for us to link them correctly. We
1121 must detect these and rename the members so that they'll link
1122 correctly. There are three types of objects: the head, the
1123 thunks, and the sentinel(s). The head is easy; it's the one
1124 with idata2. We assume that the sentinels won't have relocs,
1125 and the thunks will. It's easier than checking the symbol
1126 table for external references. */
1127 LANG_FOR_EACH_INPUT_STATEMENT (is)
1129 if (is->the_bfd->my_archive)
1132 bfd *arch = is->the_bfd->my_archive;
1134 if (cur_arch != arch)
1140 is3 && is3->the_bfd->my_archive == arch;
1141 is3 = (lang_input_statement_type *) is3->next)
1143 /* A MS dynamic import library can also contain static
1144 members, so look for the first element with a .dll
1145 extension, and use that for the remainder of the
1147 pnt = strrchr (is3->the_bfd->filename, '.');
1148 if (pnt != NULL && strcmp (pnt, ".dll") == 0)
1156 /* OK, found one. Now look to see if the remaining
1157 (dynamic import) members use the same name. */
1159 is2 && is2->the_bfd->my_archive == arch;
1160 is2 = (lang_input_statement_type *) is2->next)
1162 /* Skip static members, ie anything with a .obj
1164 pnt = strrchr (is2->the_bfd->filename, '.');
1165 if (pnt != NULL && strcmp (pnt, ".obj") == 0)
1168 if (strcmp (is3->the_bfd->filename,
1169 is2->the_bfd->filename))
1178 /* This fragment might have come from an .obj file in a Microsoft
1179 import, and not an actual import record. If this is the case,
1180 then leave the filename alone. */
1181 pnt = strrchr (is->the_bfd->filename, '.');
1183 if (is_ms_arch && (strcmp (pnt, ".dll") == 0))
1185 int idata2 = 0, reloc_count=0;
1187 char *new_name, seq;
1189 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1191 if (strcmp (sec->name, ".idata\$2") == 0)
1193 reloc_count += sec->reloc_count;
1196 if (idata2) /* .idata2 is the TOC */
1198 else if (reloc_count > 0) /* thunks */
1203 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
1204 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
1205 is->the_bfd->filename = new_name;
1207 new_name = xmalloc (strlen (is->filename) + 3);
1208 sprintf (new_name, "%s.%c", is->filename, seq);
1209 is->filename = new_name;
1217 gld_${EMULATION_NAME}_before_allocation (void)
1219 before_allocation_default ();
1223 /* This is called when an input file isn't recognized as a BFD. We
1224 check here for .DEF files and pull them in automatically. */
1227 saw_option (char *option)
1231 for (i = 0; init[i].ptr; i++)
1232 if (strcmp (init[i].symbol, option) == 0)
1233 return init[i].inited;
1236 #endif /* DLL_SUPPORT */
1239 gld_${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1242 const char *ext = entry->filename + strlen (entry->filename) - 4;
1244 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
1246 pep_def_file = def_file_parse (entry->filename, pep_def_file);
1250 int i, buflen=0, len;
1253 for (i = 0; i < pep_def_file->num_exports; i++)
1255 len = strlen (pep_def_file->exports[i].internal_name);
1256 if (buflen < len + 2)
1260 buf = xmalloc (buflen);
1262 for (i = 0; i < pep_def_file->num_exports; i++)
1264 struct bfd_link_hash_entry *h;
1266 sprintf (buf, "_%s", pep_def_file->exports[i].internal_name);
1268 h = bfd_link_hash_lookup (link_info.hash, buf, TRUE, TRUE, TRUE);
1269 if (h == (struct bfd_link_hash_entry *) NULL)
1270 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1271 if (h->type == bfd_link_hash_new)
1273 h->type = bfd_link_hash_undefined;
1274 h->u.undef.abfd = NULL;
1275 bfd_link_add_undef (link_info.hash, h);
1280 /* def_file_print (stdout, pep_def_file); */
1281 if (pep_def_file->is_dll == 1)
1282 link_info.shared = 1;
1284 if (pep_def_file->base_address != (bfd_vma)(-1))
1287 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1288 = init[IMAGEBASEOFF].value
1289 = pep_def_file->base_address;
1290 init[IMAGEBASEOFF].inited = 1;
1291 if (image_base_statement)
1292 image_base_statement->exp = exp_assop ('=', "__image_base__",
1293 exp_intop (pep.ImageBase));
1296 if (pep_def_file->stack_reserve != -1
1297 && ! saw_option ("__size_of_stack_reserve__"))
1299 pep.SizeOfStackReserve = pep_def_file->stack_reserve;
1300 if (pep_def_file->stack_commit != -1)
1301 pep.SizeOfStackCommit = pep_def_file->stack_commit;
1303 if (pep_def_file->heap_reserve != -1
1304 && ! saw_option ("__size_of_heap_reserve__"))
1306 pep.SizeOfHeapReserve = pep_def_file->heap_reserve;
1307 if (pep_def_file->heap_commit != -1)
1308 pep.SizeOfHeapCommit = pep_def_file->heap_commit;
1318 gld_${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1321 #ifdef TARGET_IS_i386pep
1322 pep_dll_id_target ("pei-x86-64");
1324 if (pep_bfd_is_dll (entry->the_bfd))
1325 return pep_implied_import_dll (entry->filename);
1331 gld_${EMULATION_NAME}_finish (void)
1336 if (link_info.shared
1337 || (!link_info.relocatable && pep_def_file->num_exports != 0))
1339 pep_dll_fill_sections (link_info.output_bfd, &link_info);
1340 if (pep_implib_filename)
1341 pep_dll_generate_implib (pep_def_file, pep_implib_filename);
1344 if (pep_out_def_filename)
1345 pep_dll_generate_def_file (pep_out_def_filename);
1346 #endif /* DLL_SUPPORT */
1348 /* I don't know where .idata gets set as code, but it shouldn't be. */
1350 asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
1354 asec->flags &= ~SEC_CODE;
1355 asec->flags |= SEC_DATA;
1361 /* Place an orphan section.
1363 We use this to put sections in a reasonable place in the file, and
1364 to ensure that they are aligned as required.
1366 We handle grouped sections here as well. A section named .foo$nn
1367 goes into the output section .foo. All grouped sections are sorted
1370 Grouped sections for the default sections are handled by the
1371 default linker script using wildcards, and are sorted by
1375 gld_${EMULATION_NAME}_place_orphan (asection *s,
1376 const char *secname,
1379 const char *orig_secname = secname;
1380 char *dollar = NULL;
1381 lang_output_section_statement_type *os;
1382 lang_statement_list_type add_child;
1384 /* Look through the script to see where to place this section. */
1385 if (!link_info.relocatable
1386 && (dollar = strchr (secname, '$')) != NULL)
1388 size_t len = dollar - secname;
1389 char *newname = xmalloc (len + 1);
1390 memcpy (newname, secname, len);
1391 newname[len] = '\0';
1395 lang_list_init (&add_child);
1398 && (os = lang_output_section_find (secname)) != NULL
1399 && os->bfd_section != NULL
1400 && (os->bfd_section->flags == 0
1401 || ((s->flags ^ os->bfd_section->flags)
1402 & (SEC_LOAD | SEC_ALLOC)) == 0))
1404 /* We already have an output section statement with this
1405 name, and its bfd section has compatible flags.
1406 If the section already exists but does not have any flags set,
1407 then it has been created by the linker, probably as a result of
1408 a --section-start command line switch. */
1409 lang_add_section (&add_child, s, os);
1413 static struct orphan_save hold[] =
1416 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1419 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1422 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1428 enum orphan_save_index
1435 static int orphan_init_done = 0;
1436 struct orphan_save *place;
1437 lang_output_section_statement_type *after;
1438 etree_type *address;
1440 if (!orphan_init_done)
1442 struct orphan_save *ho;
1443 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1444 if (ho->name != NULL)
1446 ho->os = lang_output_section_find (ho->name);
1447 if (ho->os != NULL && ho->os->flags == 0)
1448 ho->os->flags = ho->flags;
1450 orphan_init_done = 1;
1453 /* Try to put the new output section in a reasonable place based
1454 on the section name and section flags. */
1457 if ((s->flags & SEC_ALLOC) == 0)
1459 else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
1460 place = &hold[orphan_bss];
1461 else if ((s->flags & SEC_READONLY) == 0)
1462 place = &hold[orphan_data];
1463 else if ((s->flags & SEC_CODE) == 0)
1464 place = &hold[orphan_rodata];
1466 place = &hold[orphan_text];
1471 if (place->os == NULL)
1472 place->os = lang_output_section_find (place->name);
1475 after = lang_output_section_find_by_flags (s, &place->os, NULL);
1477 /* *ABS* is always the first output section statement. */
1478 after = (&lang_output_section_statement.head
1479 ->output_section_statement);
1482 /* All sections in an executable must be aligned to a page boundary. */
1483 address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
1484 os = lang_insert_orphan (s, secname, constraint, after, place, address,
1489 lang_statement_union_type **pl = &os->children.head;
1493 bfd_boolean found_dollar;
1495 /* The section name has a '$'. Sort it with the other '$'
1497 found_dollar = FALSE;
1498 for ( ; *pl != NULL; pl = &(*pl)->header.next)
1500 lang_input_section_type *ls;
1503 if ((*pl)->header.type != lang_input_section_enum)
1506 ls = &(*pl)->input_section;
1508 lname = bfd_get_section_name (ls->section->owner, ls->section);
1509 if (strchr (lname, '$') == NULL)
1516 found_dollar = TRUE;
1517 if (strcmp (orig_secname, lname) < 0)
1523 if (add_child.head != NULL)
1525 add_child.head->header.next = *pl;
1526 *pl = add_child.head;
1534 gld_${EMULATION_NAME}_open_dynamic_archive
1535 (const char *arch ATTRIBUTE_UNUSED,
1536 search_dirs_type *search,
1537 lang_input_statement_type *entry)
1541 const char * format;
1542 bfd_boolean use_prefix;
1546 /* Preferred explicit import library for dll's. */
1547 { "lib%s.dll.a", FALSE },
1548 /* Alternate explicit import library for dll's. */
1549 { "%s.dll.a", FALSE },
1550 /* "libfoo.a" could be either an import lib or a static lib.
1551 For backwards compatibility, libfoo.a needs to precede
1552 libfoo.dll and foo.dll in the search. */
1553 { "lib%s.a", FALSE },
1554 /* The 'native' spelling of an import lib name is "foo.lib". */
1555 { "%s.lib", FALSE },
1557 /* Try "<prefix>foo.dll" (preferred dll name, if specified). */
1558 { "%s%s.dll", TRUE },
1560 /* Try "libfoo.dll" (default preferred dll name). */
1561 { "lib%s.dll", FALSE },
1562 /* Finally try 'native' dll name "foo.dll". */
1563 { "%s.dll", FALSE },
1564 /* Note: If adding more formats to this table, make sure to check to
1565 see if their length is longer than libname_fmt[0].format, and if
1566 so, update the call to xmalloc() below. */
1569 static unsigned int format_max_len = 0;
1570 const char * filename;
1576 if (! entry->is_archive)
1579 filename = entry->filename;
1581 if (format_max_len == 0)
1582 /* We need to allow space in the memory that we are going to allocate
1583 for the characters in the format string. Since the format array is
1584 static we only need to calculate this information once. In theory
1585 this value could also be computed statically, but this introduces
1586 the possibility for a discrepancy and hence a possible memory
1587 corruption. The lengths we compute here will be too long because
1588 they will include any formating characters (%s) in the strings, but
1589 this will not matter. */
1590 for (i = 0; libname_fmt[i].format; i++)
1591 if (format_max_len < strlen (libname_fmt[i].format))
1592 format_max_len = strlen (libname_fmt[i].format);
1594 full_string = xmalloc (strlen (search->name)
1598 + (pep_dll_search_prefix
1599 ? strlen (pep_dll_search_prefix) : 0)
1601 /* Allow for the terminating NUL and for the path
1602 separator character that is inserted between
1603 search->name and the start of the format string. */
1606 sprintf (full_string, "%s/", search->name);
1607 base_string = full_string + strlen (full_string);
1609 for (i = 0; libname_fmt[i].format; i++)
1612 if (libname_fmt[i].use_prefix)
1614 if (!pep_dll_search_prefix)
1616 sprintf (base_string, libname_fmt[i].format, pep_dll_search_prefix, filename);
1620 sprintf (base_string, libname_fmt[i].format, filename);
1622 if (ldfile_try_open_bfd (full_string, entry))
1626 if (!libname_fmt[i].format)
1632 entry->filename = full_string;
1638 gld_${EMULATION_NAME}_find_potential_libraries
1639 (char *name, lang_input_statement_type *entry)
1641 return ldfile_open_file_search (name, entry, "", ".lib");
1645 gld_${EMULATION_NAME}_get_script (int *isfile)
1647 # Scripts compiled in.
1648 # sed commands to quote an ld script as a C string.
1649 sc="-f stringify.sed"
1655 if (link_info.relocatable && config.build_constructors)
1658 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1659 echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c
1660 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1661 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1662 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1663 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1664 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1665 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
1666 echo ' ; else if (link_info.pei386_auto_import == 1) return' >> e${EMULATION_NAME}.c
1667 sed $sc ldscripts/${EMULATION_NAME}.xa >> e${EMULATION_NAME}.c
1669 echo ' ; else return' >> e${EMULATION_NAME}.c
1670 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1671 echo '; }' >> e${EMULATION_NAME}.c
1676 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1678 gld_${EMULATION_NAME}_before_parse,
1681 gld_${EMULATION_NAME}_after_parse,
1682 gld_${EMULATION_NAME}_after_open,
1683 after_allocation_default,
1684 set_output_arch_default,
1685 ldemul_default_target,
1686 gld_${EMULATION_NAME}_before_allocation,
1687 gld_${EMULATION_NAME}_get_script,
1688 "${EMULATION_NAME}",
1690 gld_${EMULATION_NAME}_finish,
1691 NULL, /* Create output section statements. */
1692 gld_${EMULATION_NAME}_open_dynamic_archive,
1693 gld_${EMULATION_NAME}_place_orphan,
1694 gld_${EMULATION_NAME}_set_symbols,
1695 NULL, /* parse_args */
1696 gld${EMULATION_NAME}_add_options,
1697 gld${EMULATION_NAME}_handle_option,
1698 gld_${EMULATION_NAME}_unrecognized_file,
1699 gld_${EMULATION_NAME}_list_options,
1700 gld_${EMULATION_NAME}_recognized_file,
1701 gld_${EMULATION_NAME}_find_potential_libraries,
1702 NULL /* new_vers_pattern. */