1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 if [ -z "$MACHINE" ]; then
6 OUTPUT_ARCH=${ARCH}:${MACHINE}
8 rm -f e${EMULATION_NAME}.c
9 (echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
11 /* Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
12 Written by Kai Tietz, OneVision Software GmbH&CoKg.
14 This file is part of the GNU Binutils.
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
29 MA 02110-1301, USA. */
32 /* For WINDOWS_XP64 and higher */
33 /* Based on pe.em, but modified for 64 bit support. */
35 #define TARGET_IS_${EMULATION_NAME}
37 #define COFF_IMAGE_WITH_PE
39 #define COFF_WITH_pex64
45 #include "libiberty.h"
56 #include "coff/internal.h"
58 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
59 header in generic PE code. */
60 #include "coff/x86_64.h"
63 /* FIXME: This is a BFD internal header file, and we should not be
65 #include "../bfd/libcoff.h"
68 #define AOUTSZ PEPAOUTSZ
69 #define PEAOUTHDR PEPAOUTHDR
73 #include "safe-ctype.h"
75 /* Permit the emulation parameters to override the default section
76 alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes
77 it seem that include/coff/internal.h should not define
78 PE_DEF_SECTION_ALIGNMENT. */
79 #if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
80 #undef PE_DEF_SECTION_ALIGNMENT
81 #define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
84 #ifdef TARGET_IS_i386pep
88 #if defined(TARGET_IS_i386pep) || ! defined(DLL_SUPPORT)
89 #define PE_DEF_SUBSYSTEM 3
91 #undef NT_EXE_IMAGE_BASE
92 #define NT_EXE_IMAGE_BASE 0x00010000
93 #undef PE_DEF_SECTION_ALIGNMENT
94 #define PE_DEF_SUBSYSTEM 2
95 #undef PE_DEF_FILE_ALIGNMENT
96 #define PE_DEF_FILE_ALIGNMENT 0x00000200
97 #define PE_DEF_SECTION_ALIGNMENT 0x00000400
100 static struct internal_extra_pe_aouthdr pep;
102 static int pep_subsystem = ${SUBSYSTEM};
103 static flagword real_flags = IMAGE_FILE_LARGE_ADDRESS_AWARE;
104 static int support_old_code = 0;
105 static lang_assignment_statement_type *image_base_statement = 0;
106 static unsigned short pe_dll_characteristics = 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;
118 static int is_underscoring (void)
121 if (pep_leading_underscore != -1)
122 return pep_leading_underscore;
123 if (!bfd_get_target_info ("${OUTPUT_FORMAT}", NULL, NULL, &u, NULL))
124 bfd_get_target_info ("${RELOCATEABLE_OUTPUT_FORMAT}", NULL, NULL, &u, NULL);
128 pep_leading_underscore = (u != 0 ? 1 : 0);
129 return pep_leading_underscore;
134 gld_${EMULATION_NAME}_before_parse (void)
137 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
138 output_filename = "${EXECUTABLE_NAME:-a.exe}";
140 config.dynamic_link = TRUE;
141 config.has_shared = 1;
142 link_info.pei386_auto_import = 1;
143 link_info.pei386_runtime_pseudo_reloc = 2; /* Use by default version 2. */
147 /* PE format extra command line options. */
149 /* Used for setting flags in the PE header. */
152 OPTION_BASE_FILE = 300 + 1,
154 OPTION_FILE_ALIGNMENT,
156 OPTION_MAJOR_IMAGE_VERSION,
157 OPTION_MAJOR_OS_VERSION,
158 OPTION_MAJOR_SUBSYSTEM_VERSION,
159 OPTION_MINOR_IMAGE_VERSION,
160 OPTION_MINOR_OS_VERSION,
161 OPTION_MINOR_SUBSYSTEM_VERSION,
162 OPTION_SECTION_ALIGNMENT,
166 OPTION_SUPPORT_OLD_CODE,
169 OPTION_EXCLUDE_SYMBOLS,
170 OPTION_EXCLUDE_ALL_SYMBOLS,
172 OPTION_STDCALL_ALIASES,
173 OPTION_ENABLE_STDCALL_FIXUP,
174 OPTION_DISABLE_STDCALL_FIXUP,
175 OPTION_IMPLIB_FILENAME,
176 OPTION_WARN_DUPLICATE_EXPORTS,
178 OPTION_ENABLE_AUTO_IMAGE_BASE,
179 OPTION_DISABLE_AUTO_IMAGE_BASE,
180 OPTION_DLL_SEARCH_PREFIX,
181 OPTION_NO_DEFAULT_EXCLUDES,
182 OPTION_DLL_ENABLE_AUTO_IMPORT,
183 OPTION_DLL_DISABLE_AUTO_IMPORT,
184 OPTION_ENABLE_EXTRA_PE_DEBUG,
186 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC,
187 OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC,
188 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2,
189 OPTION_EXCLUDE_MODULES_FOR_IMPLIB,
190 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES,
191 OPTION_NO_LEADING_UNDERSCORE,
192 OPTION_LEADING_UNDERSCORE,
193 OPTION_ENABLE_LONG_SECTION_NAMES,
194 OPTION_DISABLE_LONG_SECTION_NAMES,
196 OPTION_FORCE_INTEGRITY,
202 OPTION_TERMINAL_SERVER_AWARE
206 gld${EMULATION_NAME}_add_options
207 (int ns ATTRIBUTE_UNUSED,
208 char **shortopts ATTRIBUTE_UNUSED,
210 struct option **longopts,
211 int nrl ATTRIBUTE_UNUSED,
212 struct option **really_longopts ATTRIBUTE_UNUSED)
214 static const struct option xtra_long[] =
217 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
218 {"dll", no_argument, NULL, OPTION_DLL},
219 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
220 {"heap", required_argument, NULL, OPTION_HEAP},
221 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
222 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
223 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
224 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
225 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
226 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
227 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
228 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
229 {"stack", required_argument, NULL, OPTION_STACK},
230 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
231 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
232 {"use-nul-prefixed-import-tables", no_argument, NULL,
233 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
234 {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
235 {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE},
237 /* getopt allows abbreviations, so we do this to stop it
238 from treating -o as an abbreviation for this option. */
239 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
240 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
241 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
242 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
243 {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS},
244 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
245 {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
246 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
247 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
248 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
249 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
250 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
251 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
252 /* getopt() allows abbreviations, so we do this to stop it from
253 treating -c as an abbreviation for these --compat-implib. */
254 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
255 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
256 {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
257 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
258 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
259 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
260 {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
261 {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
262 {"enable-extra-pep-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
263 {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
264 {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
265 {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2},
267 {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
268 {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
269 {"dynamicbase",no_argument, NULL, OPTION_DYNAMIC_BASE},
270 {"forceinteg", no_argument, NULL, OPTION_FORCE_INTEGRITY},
271 {"nxcompat", no_argument, NULL, OPTION_NX_COMPAT},
272 {"no-isolation", no_argument, NULL, OPTION_NO_ISOLATION},
273 {"no-seh", no_argument, NULL, OPTION_NO_SEH},
274 {"no-bind", no_argument, NULL, OPTION_NO_BIND},
275 {"wdmdriver", no_argument, NULL, OPTION_WDM_DRIVER},
276 {"tsaware", no_argument, NULL, OPTION_TERMINAL_SERVER_AWARE},
277 {NULL, no_argument, NULL, 0}
281 = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
282 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
285 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
286 parameters which may be input from the command line. */
295 /* FALSE for an assembly level symbol and TRUE for a C visible symbol.
296 C visible symbols can be prefixed by underscore dependent on target's
298 bfd_boolean is_c_symbol;
301 #define GET_INIT_SYMBOL_NAME(IDX) \
302 (init[(IDX)].symbol \
303 + ((init[(IDX)].is_c_symbol == FALSE || (is_underscoring () == 1)) ? 0 : 1))
305 /* Decorates the C visible symbol by underscore, if target requires. */
307 ((is_underscoring () == 0) ? CSTR : "_" CSTR)
309 /* Get size of constant string for a possible underscore prefixed
311 #define U_SIZE(CSTR) \
312 (sizeof (CSTR) + (is_underscoring () == 0 ? 0 : 1))
314 #define D(field,symbol,def,usc) {&pep.field,sizeof(pep.field), def, symbol,0, usc}
316 static definfo init[] =
318 /* imagebase must be first */
319 #define IMAGEBASEOFF 0
320 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE, FALSE),
322 {&dll, sizeof(dll), 0, "__dll__", 0, FALSE},
323 #define MSIMAGEBASEOFF 2
324 D(ImageBase, "___ImageBase", NT_EXE_IMAGE_BASE, TRUE),
325 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT, FALSE),
326 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT, FALSE),
327 D(MajorOperatingSystemVersion,"__major_os_version__", 4, FALSE),
328 D(MinorOperatingSystemVersion,"__minor_os_version__", 0, FALSE),
329 D(MajorImageVersion,"__major_image_version__", 0, FALSE),
330 D(MinorImageVersion,"__minor_image_version__", 0, FALSE),
331 D(MajorSubsystemVersion,"__major_subsystem_version__", 5, FALSE),
332 D(MinorSubsystemVersion,"__minor_subsystem_version__", 2, FALSE),
333 D(Subsystem,"__subsystem__", ${SUBSYSTEM}, FALSE),
334 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000, FALSE),
335 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000, FALSE),
336 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000, FALSE),
337 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000, FALSE),
338 D(LoaderFlags,"__loader_flags__", 0x0, FALSE),
339 D(DllCharacteristics, "__dll_characteristics__", 0x0, FALSE),
340 { NULL, 0, 0, NULL, 0, FALSE}
345 gld_${EMULATION_NAME}_list_options (FILE *file)
347 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
348 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
349 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
350 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
351 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
352 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
353 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
354 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
355 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
356 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
357 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
358 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
359 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
360 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
361 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
362 fprintf (file, _(" --[no-]leading-underscore Set explicit symbol underscore prefix mode\n"));
364 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
365 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
366 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
367 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
368 fprintf (file, _(" --exclude-all-symbols Exclude all symbols from automatic export\n"));
369 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
370 fprintf (file, _(" --exclude-modules-for-implib mod,mod,...\n"));
371 fprintf (file, _(" Exclude objects, archive members from auto\n"));
372 fprintf (file, _(" export, place into import library instead.\n"));
373 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
374 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
375 fprintf (file, _(" --out-implib <file> Generate import library\n"));
376 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
377 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
378 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
379 create __imp_<SYMBOL> as well.\n"));
380 fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n\
381 unless user specifies one\n"));
382 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
383 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
384 an importlib, use <string><basename>.dll\n\
385 in preference to lib<basename>.dll \n"));
386 fprintf (file, _(" --enable-auto-import Do sophisticated linking of _sym to\n\
387 __imp_sym for DATA references\n"));
388 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
389 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
390 adding pseudo-relocations resolved at\n\
392 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
393 auto-imported DATA.\n"));
394 fprintf (file, _(" --enable-extra-pep-debug Enable verbose debug output when building\n\
395 or linking to DLLs (esp. auto-import)\n"));
396 fprintf (file, _(" --enable-long-section-names Use long COFF section names even in\n\
397 executable image files\n"));
398 fprintf (file, _(" --disable-long-section-names Never use long COFF section names, even\n\
399 in object files\n"));
400 fprintf (file, _(" --dynamicbase Image base address may be relocated using\n\
401 address space layout randomization (ASLR)\n"));
402 fprintf (file, _(" --forceinteg Code integrity checks are enforced\n"));
403 fprintf (file, _(" --nxcompat Image is compatible with data execution prevention\n"));
404 fprintf (file, _(" --no-isolation Image understands isolation but do not isolate the image\n"));
405 fprintf (file, _(" --no-seh Image does not use SEH. No SE handler may\n\
406 be called in this image\n"));
407 fprintf (file, _(" --no-bind Do not bind this image\n"));
408 fprintf (file, _(" --wdmdriver Driver uses the WDM model\n"));
409 fprintf (file, _(" --tsaware Image is Terminal Server aware\n"));
415 set_pep_name (char *name, bfd_vma val)
419 /* Find the name and set it. */
420 for (i = 0; init[i].ptr; i++)
422 if (strcmp (name, GET_INIT_SYMBOL_NAME (i)) == 0)
426 if (strcmp (name,"__image_base__") == 0)
427 set_pep_name (U ("__ImageBase"), val);
435 set_entry_point (void)
438 const char *initial_symbol_char;
448 { 1, "NtProcessStartup" },
449 { 2, "WinMainCRTStartup" },
450 { 3, "mainCRTStartup" },
451 { 7, "__PosixProcessStartup" },
452 { 9, "WinMainCRTStartup" },
453 {14, "mainCRTStartup" },
457 /* Entry point name for arbitrary subsystem numbers. */
458 static const char default_entry[] = "mainCRTStartup";
460 if (link_info.shared || dll)
462 entry = "DllMainCRTStartup";
466 for (i = 0; v[i].entry; i++)
467 if (v[i].value == pep_subsystem)
470 /* If no match, use the default. */
471 if (v[i].entry != NULL)
474 entry = default_entry;
477 /* Now we check target's default for getting proper symbol_char. */
478 initial_symbol_char = (is_underscoring () != 0 ? "_" : "");
480 if (*initial_symbol_char != '\0')
484 /* lang_default_entry expects its argument to be permanently
485 allocated, so we don't free this string. */
486 alc_entry = xmalloc (strlen (initial_symbol_char)
489 strcpy (alc_entry, initial_symbol_char);
490 strcat (alc_entry, entry);
494 lang_default_entry (entry);
498 set_pep_subsystem (void)
504 unsigned long temp_subsystem;
521 /* Check for the presence of a version number. */
522 sver = strchr (optarg, ':');
524 len = strlen (optarg);
528 set_pep_name ("__major_subsystem_version__",
529 strtoul (sver + 1, &end, 0));
531 set_pep_name ("__minor_subsystem_version__",
532 strtoul (end + 1, &end, 0));
534 einfo (_("%P: warning: bad version number in -subsystem option\n"));
537 /* Check for numeric subsystem. */
538 temp_subsystem = strtoul (optarg, & end, 0);
539 if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
541 /* Search list for a numeric match to use its entry point. */
542 for (i = 0; v[i].name; i++)
543 if (v[i].value == (int) temp_subsystem)
546 /* Use this subsystem. */
547 pep_subsystem = (int) temp_subsystem;
551 /* Search for subsystem by name. */
552 for (i = 0; v[i].name; i++)
553 if (strncmp (optarg, v[i].name, len) == 0
554 && v[i].name[len] == '\0')
557 if (v[i].name == NULL)
559 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
563 pep_subsystem = v[i].value;
566 set_pep_name ("__subsystem__", pep_subsystem);
573 set_pep_value (char *name)
577 set_pep_name (name, (bfd_vma) strtoull (optarg, &end, 0));
580 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
587 set_pep_stack_heap (char *resname, char *comname)
589 set_pep_value (resname);
594 set_pep_value (comname);
597 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
602 gld${EMULATION_NAME}_handle_option (int optc)
610 case OPTION_BASE_FILE:
611 link_info.base_file = fopen (optarg, FOPEN_WB);
612 if (link_info.base_file == NULL)
614 /* xgettext:c-format */
615 fprintf (stderr, _("%s: Can't open base file %s\n"),
616 program_name, optarg);
623 set_pep_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
626 set_pep_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
628 case OPTION_SUBSYSTEM:
629 set_pep_subsystem ();
631 case OPTION_MAJOR_OS_VERSION:
632 set_pep_value ("__major_os_version__");
634 case OPTION_MINOR_OS_VERSION:
635 set_pep_value ("__minor_os_version__");
637 case OPTION_MAJOR_SUBSYSTEM_VERSION:
638 set_pep_value ("__major_subsystem_version__");
640 case OPTION_MINOR_SUBSYSTEM_VERSION:
641 set_pep_value ("__minor_subsystem_version__");
643 case OPTION_MAJOR_IMAGE_VERSION:
644 set_pep_value ("__major_image_version__");
646 case OPTION_MINOR_IMAGE_VERSION:
647 set_pep_value ("__minor_image_version__");
649 case OPTION_FILE_ALIGNMENT:
650 set_pep_value ("__file_alignment__");
652 case OPTION_SECTION_ALIGNMENT:
653 set_pep_value ("__section_alignment__");
656 set_pep_name ("__dll__", 1);
658 case OPTION_IMAGE_BASE:
659 set_pep_value ("__image_base__");
661 case OPTION_SUPPORT_OLD_CODE:
662 support_old_code = 1;
664 case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
665 pep_use_nul_prefixed_import_tables = TRUE;
667 case OPTION_NO_LEADING_UNDERSCORE:
668 pep_leading_underscore = 0;
670 case OPTION_LEADING_UNDERSCORE:
671 pep_leading_underscore = 1;
675 pep_out_def_filename = xstrdup (optarg);
677 case OPTION_EXPORT_ALL:
678 pep_dll_export_everything = 1;
680 case OPTION_EXCLUDE_SYMBOLS:
681 pep_dll_add_excludes (optarg, EXCLUDESYMS);
683 case OPTION_EXCLUDE_ALL_SYMBOLS:
684 pep_dll_exclude_all_symbols = 1;
686 case OPTION_EXCLUDE_LIBS:
687 pep_dll_add_excludes (optarg, EXCLUDELIBS);
689 case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
690 pep_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
692 case OPTION_KILL_ATS:
693 pep_dll_kill_ats = 1;
695 case OPTION_STDCALL_ALIASES:
696 pep_dll_stdcall_aliases = 1;
698 case OPTION_ENABLE_STDCALL_FIXUP:
699 pep_enable_stdcall_fixup = 1;
701 case OPTION_DISABLE_STDCALL_FIXUP:
702 pep_enable_stdcall_fixup = 0;
704 case OPTION_IMPLIB_FILENAME:
705 pep_implib_filename = xstrdup (optarg);
707 case OPTION_WARN_DUPLICATE_EXPORTS:
708 pep_dll_warn_dup_exports = 1;
710 case OPTION_IMP_COMPAT:
711 pep_dll_compat_implib = 1;
713 case OPTION_ENABLE_AUTO_IMAGE_BASE:
714 pep_enable_auto_image_base = 1;
716 case OPTION_DISABLE_AUTO_IMAGE_BASE:
717 pep_enable_auto_image_base = 0;
719 case OPTION_DLL_SEARCH_PREFIX:
720 pep_dll_search_prefix = xstrdup (optarg);
722 case OPTION_NO_DEFAULT_EXCLUDES:
723 pep_dll_do_default_excludes = 0;
725 case OPTION_DLL_ENABLE_AUTO_IMPORT:
726 link_info.pei386_auto_import = 1;
728 case OPTION_DLL_DISABLE_AUTO_IMPORT:
729 link_info.pei386_auto_import = 0;
731 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
732 link_info.pei386_runtime_pseudo_reloc = 2;
734 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
735 link_info.pei386_runtime_pseudo_reloc = 0;
737 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2:
738 link_info.pei386_runtime_pseudo_reloc = 2;
740 case OPTION_ENABLE_EXTRA_PE_DEBUG:
741 pep_dll_extra_pe_debug = 1;
744 case OPTION_ENABLE_LONG_SECTION_NAMES:
745 pep_use_coff_long_section_names = 1;
747 case OPTION_DISABLE_LONG_SECTION_NAMES:
748 pep_use_coff_long_section_names = 0;
750 /* Get DLLCharacteristics bits */
751 case OPTION_DYNAMIC_BASE:
752 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
754 case OPTION_FORCE_INTEGRITY:
755 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
757 case OPTION_NX_COMPAT:
758 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
760 case OPTION_NO_ISOLATION:
761 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
764 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_SEH;
767 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_BIND;
769 case OPTION_WDM_DRIVER:
770 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
772 case OPTION_TERMINAL_SERVER_AWARE:
773 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
777 /* Set DLLCharacteristics bits */
778 set_pep_name ("__dll_characteristics__", pe_dll_characteristics);
786 strhash (const char *str)
788 const unsigned char *s;
795 s = (const unsigned char *) str;
796 while ((c = *s++) != '\0')
798 hash += c + (c << 17);
802 hash += len + (len << 17);
808 /* Use the output file to create a image base for relocatable DLLs. */
811 compute_dll_image_base (const char *ofile)
813 bfd_vma hash = (bfd_vma) strhash (ofile);
814 return 0x61300000 + ((hash << 16) & 0x0FFC0000);
818 /* Assign values to the special symbols before the linker script is
822 gld_${EMULATION_NAME}_set_symbols (void)
824 /* Run through and invent symbols for all the
825 names and insert the defaults. */
830 if (!init[IMAGEBASEOFF].inited)
832 if (link_info.relocatable)
833 init[IMAGEBASEOFF].value = 0;
834 else if (init[DLLOFF].value || (link_info.shared && !link_info.pie))
837 init[IMAGEBASEOFF].value = (pep_enable_auto_image_base
838 ? compute_dll_image_base (output_filename)
839 : NT_DLL_IMAGE_BASE);
841 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
845 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
846 init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
849 /* Don't do any symbol assignments if this is a relocatable link. */
850 if (link_info.relocatable)
853 /* Glue the assignments into the abs section. */
854 push_stat_ptr (&abs_output_section->children);
856 for (j = 0; init[j].ptr; j++)
858 bfd_vma val = init[j].value;
859 lang_assignment_statement_type *rv;
861 rv = lang_add_assignment (exp_assop ('=', GET_INIT_SYMBOL_NAME (j),
863 if (init[j].size == sizeof (short))
864 *(short *) init[j].ptr = (short) val;
865 else if (init[j].size == sizeof (int))
866 *(int *) init[j].ptr = (int) val;
867 else if (init[j].size == sizeof (long))
868 *(long *) init[j].ptr = (long) val;
869 /* This might be a long long or other special type. */
870 else if (init[j].size == sizeof (bfd_vma))
871 *(bfd_vma *) init[j].ptr = val;
873 if (j == IMAGEBASEOFF)
874 image_base_statement = rv;
876 /* Restore the pointer. */
879 if (pep.FileAlignment > pep.SectionAlignment)
881 einfo (_("%P: warning, file alignment > section alignment.\n"));
885 /* This is called after the linker script and the command line options
889 gld_${EMULATION_NAME}_after_parse (void)
891 /* PR ld/6744: Warn the user if they have used an ELF-only
892 option hoping it will work on PE+. */
893 if (link_info.export_dynamic)
894 einfo (_("%P: warning: --export-dynamic is not supported for PE+ "
895 "targets, did you mean --export-all-symbols?\n"));
899 after_parse_default ();
902 /* pep-dll.c directly accesses pep_data_import_dll,
903 so it must be defined outside of #ifdef DLL_SUPPORT.
904 Note - this variable is deliberately not initialised.
905 This allows it to be treated as a common varaible, and only
906 exist in one incarnation in a multiple target enabled linker. */
907 char * pep_data_import_dll;
910 static struct bfd_link_hash_entry *pep_undef_found_sym;
913 pep_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
917 const char *hs = h->root.string;
919 sl = strlen (string);
920 if (h->type == bfd_link_hash_defined
921 && ((*hs == '@' && *string == '_'
922 && strncmp (hs + 1, string + 1, sl - 1) == 0)
923 || strncmp (hs, string, sl) == 0)
924 && h->root.string[sl] == '@')
926 pep_undef_found_sym = h;
933 pep_fixup_stdcalls (void)
935 static int gave_warning_message = 0;
936 struct bfd_link_hash_entry *undef, *sym;
938 if (pep_dll_extra_pe_debug)
939 printf ("%s\n", __FUNCTION__);
941 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
942 if (undef->type == bfd_link_hash_undefined)
944 char* at = strchr (undef->root.string, '@');
945 int lead_at = (*undef->root.string == '@');
947 at = strchr (undef->root.string + 1, '@');
950 /* The symbol is a stdcall symbol, so let's look for a
951 cdecl symbol with the same name and resolve to that. */
952 char *cname = xstrdup (undef->root.string);
956 at = strchr (cname, '@');
959 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
961 if (sym && sym->type == bfd_link_hash_defined)
963 undef->type = bfd_link_hash_defined;
964 undef->u.def.value = sym->u.def.value;
965 undef->u.def.section = sym->u.def.section;
967 if (pep_enable_stdcall_fixup == -1)
969 einfo (_("Warning: resolving %s by linking to %s\n"),
970 undef->root.string, cname);
971 if (! gave_warning_message)
973 gave_warning_message = 1;
974 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
975 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
982 /* The symbol is a cdecl symbol, so we look for stdcall
983 symbols - which means scanning the whole symbol table. */
984 pep_undef_found_sym = 0;
985 bfd_link_hash_traverse (link_info.hash, pep_undef_cdecl_match,
986 (char *) undef->root.string);
987 sym = pep_undef_found_sym;
990 undef->type = bfd_link_hash_defined;
991 undef->u.def.value = sym->u.def.value;
992 undef->u.def.section = sym->u.def.section;
994 if (pep_enable_stdcall_fixup == -1)
996 einfo (_("Warning: resolving %s by linking to %s\n"),
997 undef->root.string, sym->root.string);
998 if (! gave_warning_message)
1000 gave_warning_message = 1;
1001 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
1002 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
1011 make_import_fixup (arelent *rel, asection *s)
1013 struct bfd_symbol *sym = *rel->sym_ptr_ptr;
1015 bfd_vma _addend = 0;
1018 if (pep_dll_extra_pe_debug)
1019 printf ("arelent: %s@%#lx: add=%li\n", sym->name,
1020 (unsigned long) rel->address, (long) rel->addend);
1022 memset (addend, 0, sizeof (addend));
1023 switch ((rel->howto->bitsize))
1026 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 1);
1027 if (suc && rel->howto->pc_relative)
1028 _addend = (bfd_vma) ((bfd_signed_vma) ((char) bfd_get_8 (s->owner, addend)));
1030 _addend = ((bfd_vma) bfd_get_8 (s->owner, addend)) & 0xff;
1033 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 2);
1034 if (suc && rel->howto->pc_relative)
1035 _addend = (bfd_vma) ((bfd_signed_vma) ((short) bfd_get_16 (s->owner, addend)));
1037 _addend = ((bfd_vma) bfd_get_16 (s->owner, addend)) & 0xffff;
1040 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 4);
1041 if (suc && rel->howto->pc_relative)
1042 _addend = (bfd_vma) ((bfd_signed_vma) ((int) bfd_get_32 (s->owner, addend)));
1044 _addend = ((bfd_vma) bfd_get_32 (s->owner, addend)) & 0xffffffff;
1047 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 8);
1049 _addend = ((bfd_vma) bfd_get_64 (s->owner, addend));
1053 einfo (_("%C: Cannot get section contents - auto-import exception\n"),
1054 s->owner, s, rel->address);
1056 if (pep_dll_extra_pe_debug)
1058 printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx", (long) _addend, (long) rel->addend, (long) rel->address);
1059 if (rel->howto->pc_relative) printf (" pcrel");
1060 printf (" %d bit rel.\n",(int) rel->howto->bitsize);
1062 pep_create_import_fixup (rel, s, _addend);
1068 pep_find_data_imports (void)
1070 struct bfd_link_hash_entry *undef, *sym;
1072 if (link_info.pei386_auto_import == 0)
1075 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
1077 if (undef->type == bfd_link_hash_undefined)
1079 /* C++ symbols are *long*. */
1082 if (pep_dll_extra_pe_debug)
1083 printf ("%s:%s\n", __FUNCTION__, undef->root.string);
1085 sprintf (buf, "__imp_%s", undef->root.string);
1087 sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
1089 if (sym && sym->type == bfd_link_hash_defined)
1091 bfd *b = sym->u.def.section->owner;
1095 if (!bfd_generic_link_read_symbols (b))
1097 einfo (_("%B%F: could not read symbols: %E\n"), b);
1101 symbols = bfd_get_outsymbols (b);
1102 nsyms = bfd_get_symcount (b);
1104 for (i = 0; i < nsyms; i++)
1106 if (! CONST_STRNEQ (symbols[i]->name, U ("_head_")))
1109 if (pep_dll_extra_pe_debug)
1110 printf ("->%s\n", symbols[i]->name);
1112 pep_data_import_dll = (char*) (symbols[i]->name +
1113 U_SIZE ("_head_") - 1);
1117 pep_walk_relocs_of_symbol (&link_info, undef->root.string,
1120 /* Let's differentiate it somehow from defined. */
1121 undef->type = bfd_link_hash_defweak;
1122 /* We replace original name with __imp_ prefixed, this
1123 1) may trash memory 2) leads to duplicate symbol generation.
1124 Still, IMHO it's better than having name poluted. */
1125 undef->root.string = sym->root.string;
1126 undef->u.def.value = sym->u.def.value;
1127 undef->u.def.section = sym->u.def.section;
1134 pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
1136 if (pep_dll_extra_pe_debug)
1137 printf ("+%s\n", h->string);
1141 #endif /* DLL_SUPPORT */
1144 debug_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
1146 int *found = (int *) obj;
1147 if (strncmp (".debug_", sect->name, sizeof (".debug_") - 1) == 0)
1152 gld_${EMULATION_NAME}_after_open (void)
1154 after_open_default ();
1158 if (pep_dll_extra_pe_debug)
1161 struct bfd_link_hash_entry *sym;
1163 printf ("%s()\n", __FUNCTION__);
1165 for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1166 printf ("-%s\n", sym->root.string);
1167 bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
1169 for (a = link_info.input_bfds; a; a = a->link_next)
1170 printf ("*%s\n",a->filename);
1174 /* Pass the wacky PE command line options into the output bfd.
1175 FIXME: This should be done via a function, rather than by
1176 including an internal BFD header. */
1178 if (coff_data (link_info.output_bfd) == NULL
1179 || coff_data (link_info.output_bfd)->pe == 0)
1180 einfo (_("%F%P: cannot perform PE operations on non PE output file '%B'.\n"),
1181 link_info.output_bfd);
1183 pe_data (link_info.output_bfd)->pe_opthdr = pep;
1184 pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
1185 pe_data (link_info.output_bfd)->real_flags |= real_flags;
1187 /* At this point we must decide whether to use long section names
1188 in the output or not. If the user hasn't explicitly specified
1189 on the command line, we leave it to the default for the format
1190 (object files yes, image files no), except if there is debug
1191 information present; GDB relies on the long section names to
1192 find it, so enable it in that case. */
1193 if (pep_use_coff_long_section_names < 0 && link_info.strip == strip_none)
1195 /* Iterate over all sections of all input BFDs, checking
1196 for any that begin 'debug_' and are long names. */
1197 LANG_FOR_EACH_INPUT_STATEMENT (is)
1199 int found_debug = 0;
1200 bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug);
1203 pep_use_coff_long_section_names = 1;
1209 pep_output_file_set_long_section_names (link_info.output_bfd);
1212 if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
1213 pep_fixup_stdcalls ();
1215 pep_process_import_defs (link_info.output_bfd, &link_info);
1217 pep_find_data_imports ();
1219 /* As possibly new symbols are added by imports, we rerun
1220 stdcall/fastcall fixup here. */
1221 if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
1222 pep_fixup_stdcalls ();
1224 #ifndef TARGET_IS_i386pep
1225 if (link_info.shared)
1227 if (!link_info.relocatable)
1229 pep_dll_build_sections (link_info.output_bfd, &link_info);
1231 #ifndef TARGET_IS_i386pep
1233 pep_exe_build_sections (link_info.output_bfd, &link_info);
1235 #endif /* DLL_SUPPORT */
1238 /* This next chunk of code tries to detect the case where you have
1239 two import libraries for the same DLL (specifically,
1240 symbolically linking libm.a and libc.a in cygwin to
1241 libcygwin.a). In those cases, it's possible for function
1242 thunks from the second implib to be used but without the
1243 head/tail objects, causing an improper import table. We detect
1244 those cases and rename the "other" import libraries to match
1245 the one the head/tail come from, so that the linker will sort
1246 things nicely and produce a valid import table. */
1248 LANG_FOR_EACH_INPUT_STATEMENT (is)
1250 if (is->the_bfd->my_archive)
1252 int idata2 = 0, reloc_count=0, is_imp = 0;
1255 /* See if this is an import library thunk. */
1256 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1258 if (strcmp (sec->name, ".idata\$2") == 0)
1260 if (CONST_STRNEQ (sec->name, ".idata\$"))
1262 reloc_count += sec->reloc_count;
1265 if (is_imp && !idata2 && reloc_count)
1267 /* It is, look for the reference to head and see if it's
1268 from our own library. */
1269 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1277 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1281 if (!bfd_generic_link_read_symbols (is->the_bfd))
1283 einfo (_("%B%F: could not read symbols: %E\n"),
1287 symbols = bfd_get_outsymbols (is->the_bfd);
1289 relocs = xmalloc ((size_t) relsize);
1290 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1295 einfo ("%X%P: unable to process relocs: %E\n");
1299 for (i = 0; i < nrelocs; i++)
1301 struct bfd_symbol *s;
1302 struct bfd_link_hash_entry * blhe;
1303 char *other_bfd_filename;
1306 s = (relocs[i]->sym_ptr_ptr)[0];
1308 if (s->flags & BSF_LOCAL)
1311 /* Thunk section with reloc to another bfd. */
1312 blhe = bfd_link_hash_lookup (link_info.hash,
1314 FALSE, FALSE, TRUE);
1317 || blhe->type != bfd_link_hash_defined)
1321 = blhe->u.def.section->owner->my_archive
1322 ? bfd_get_filename (blhe->u.def.section->owner->my_archive)
1323 : bfd_get_filename (blhe->u.def.section->owner);
1325 if (strcmp (bfd_get_filename (is->the_bfd->my_archive),
1326 other_bfd_filename) == 0)
1329 /* Rename this implib to match the other one. */
1330 n = xmalloc (strlen (other_bfd_filename) + 1);
1331 strcpy (n, other_bfd_filename);
1332 is->the_bfd->my_archive->filename = n;
1336 /* Note - we do not free the symbols,
1337 they are now cached in the BFD. */
1347 lang_input_statement_type *is2;
1348 lang_input_statement_type *is3;
1350 /* Careful - this is a shell script. Watch those dollar signs! */
1351 /* Microsoft import libraries have every member named the same,
1352 and not in the right order for us to link them correctly. We
1353 must detect these and rename the members so that they'll link
1354 correctly. There are three types of objects: the head, the
1355 thunks, and the sentinel(s). The head is easy; it's the one
1356 with idata2. We assume that the sentinels won't have relocs,
1357 and the thunks will. It's easier than checking the symbol
1358 table for external references. */
1359 LANG_FOR_EACH_INPUT_STATEMENT (is)
1361 if (is->the_bfd->my_archive)
1364 bfd *arch = is->the_bfd->my_archive;
1366 if (cur_arch != arch)
1372 is3 && is3->the_bfd->my_archive == arch;
1373 is3 = (lang_input_statement_type *) is3->next)
1375 /* A MS dynamic import library can also contain static
1376 members, so look for the first element with a .dll
1377 extension, and use that for the remainder of the
1379 pnt = strrchr (is3->the_bfd->filename, '.');
1380 if (pnt != NULL && strcmp (pnt, ".dll") == 0)
1388 /* OK, found one. Now look to see if the remaining
1389 (dynamic import) members use the same name. */
1391 is2 && is2->the_bfd->my_archive == arch;
1392 is2 = (lang_input_statement_type *) is2->next)
1394 /* Skip static members, ie anything with a .obj
1396 pnt = strrchr (is2->the_bfd->filename, '.');
1397 if (pnt != NULL && strcmp (pnt, ".obj") == 0)
1400 if (strcmp (is3->the_bfd->filename,
1401 is2->the_bfd->filename))
1410 /* This fragment might have come from an .obj file in a Microsoft
1411 import, and not an actual import record. If this is the case,
1412 then leave the filename alone. */
1413 pnt = strrchr (is->the_bfd->filename, '.');
1415 if (is_ms_arch && (strcmp (pnt, ".dll") == 0))
1417 int idata2 = 0, reloc_count=0;
1419 char *new_name, seq;
1421 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1423 if (strcmp (sec->name, ".idata\$2") == 0)
1425 reloc_count += sec->reloc_count;
1428 if (idata2) /* .idata2 is the TOC */
1430 else if (reloc_count > 0) /* thunks */
1435 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
1436 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
1437 is->the_bfd->filename = new_name;
1439 new_name = xmalloc (strlen (is->filename) + 3);
1440 sprintf (new_name, "%s.%c", is->filename, seq);
1441 is->filename = new_name;
1449 gld_${EMULATION_NAME}_before_allocation (void)
1452 before_allocation_default ();
1456 /* This is called when an input file isn't recognized as a BFD. We
1457 check here for .DEF files and pull them in automatically. */
1460 saw_option (char *option)
1466 for (i = 0; init[i].ptr; i++)
1467 if (strcmp (GET_INIT_SYMBOL_NAME (i), option) == 0)
1468 return init[i].inited;
1471 #endif /* DLL_SUPPORT */
1474 gld_${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1477 const char *ext = entry->filename + strlen (entry->filename) - 4;
1479 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
1481 pep_def_file = def_file_parse (entry->filename, pep_def_file);
1485 int i, buflen=0, len;
1488 for (i = 0; i < pep_def_file->num_exports; i++)
1490 len = strlen (pep_def_file->exports[i].internal_name);
1491 if (buflen < len + 2)
1495 buf = xmalloc (buflen);
1497 for (i = 0; i < pep_def_file->num_exports; i++)
1499 struct bfd_link_hash_entry *h;
1501 sprintf (buf, "%s%s", U (""),
1502 pep_def_file->exports[i].internal_name);
1504 h = bfd_link_hash_lookup (link_info.hash, buf, TRUE, TRUE, TRUE);
1505 if (h == (struct bfd_link_hash_entry *) NULL)
1506 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1507 if (h->type == bfd_link_hash_new)
1509 h->type = bfd_link_hash_undefined;
1510 h->u.undef.abfd = NULL;
1511 bfd_link_add_undef (link_info.hash, h);
1516 /* def_file_print (stdout, pep_def_file); */
1517 if (pep_def_file->is_dll == 1)
1518 link_info.shared = 1;
1520 if (pep_def_file->base_address != (bfd_vma)(-1))
1523 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1524 = init[IMAGEBASEOFF].value
1525 = pep_def_file->base_address;
1526 init[IMAGEBASEOFF].inited = 1;
1527 if (image_base_statement)
1528 image_base_statement->exp = exp_assop ('=', "__image_base__",
1529 exp_intop (pep.ImageBase));
1532 if (pep_def_file->stack_reserve != -1
1533 && ! saw_option ("__size_of_stack_reserve__"))
1535 pep.SizeOfStackReserve = pep_def_file->stack_reserve;
1536 if (pep_def_file->stack_commit != -1)
1537 pep.SizeOfStackCommit = pep_def_file->stack_commit;
1539 if (pep_def_file->heap_reserve != -1
1540 && ! saw_option ("__size_of_heap_reserve__"))
1542 pep.SizeOfHeapReserve = pep_def_file->heap_reserve;
1543 if (pep_def_file->heap_commit != -1)
1544 pep.SizeOfHeapCommit = pep_def_file->heap_commit;
1554 gld_${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1558 #ifdef TARGET_IS_i386pep
1559 pep_dll_id_target ("pei-x86-64");
1561 if (pep_bfd_is_dll (entry->the_bfd))
1562 return pep_implied_import_dll (entry->filename);
1568 gld_${EMULATION_NAME}_finish (void)
1574 if (link_info.shared
1575 || (!link_info.relocatable && pep_def_file->num_exports != 0))
1577 pep_dll_fill_sections (link_info.output_bfd, &link_info);
1578 if (pep_implib_filename)
1579 pep_dll_generate_implib (pep_def_file, pep_implib_filename, &link_info);
1582 if (pep_out_def_filename)
1583 pep_dll_generate_def_file (pep_out_def_filename);
1584 #endif /* DLL_SUPPORT */
1586 /* I don't know where .idata gets set as code, but it shouldn't be. */
1588 asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
1592 asec->flags &= ~SEC_CODE;
1593 asec->flags |= SEC_DATA;
1599 /* Place an orphan section.
1601 We use this to put sections in a reasonable place in the file, and
1602 to ensure that they are aligned as required.
1604 We handle grouped sections here as well. A section named .foo\$nn
1605 goes into the output section .foo. All grouped sections are sorted
1608 Grouped sections for the default sections are handled by the
1609 default linker script using wildcards, and are sorted by
1612 static lang_output_section_statement_type *
1613 gld_${EMULATION_NAME}_place_orphan (asection *s,
1614 const char *secname,
1617 const char *orig_secname = secname;
1618 char *dollar = NULL;
1619 lang_output_section_statement_type *os;
1620 lang_statement_list_type add_child;
1621 lang_output_section_statement_type *match_by_name = NULL;
1622 lang_statement_union_type **pl;
1624 /* Look through the script to see where to place this section. */
1625 if (!link_info.relocatable
1626 && (dollar = strchr (secname, '\$')) != NULL)
1628 size_t len = dollar - secname;
1629 char *newname = xmalloc (len + 1);
1630 memcpy (newname, secname, len);
1631 newname[len] = '\0';
1635 lang_list_init (&add_child);
1638 if (constraint == 0)
1639 for (os = lang_output_section_find (secname);
1641 os = next_matching_output_section_statement (os, 0))
1643 /* If we don't match an existing output section, tell
1644 lang_insert_orphan to create a new output section. */
1645 constraint = SPECIAL;
1647 if (os->bfd_section != NULL
1648 && (os->bfd_section->flags == 0
1649 || ((s->flags ^ os->bfd_section->flags)
1650 & (SEC_LOAD | SEC_ALLOC)) == 0))
1652 /* We already have an output section statement with this
1653 name, and its bfd section has compatible flags.
1654 If the section already exists but does not have any flags set,
1655 then it has been created by the linker, probably as a result of
1656 a --section-start command line switch. */
1657 lang_add_section (&add_child, s, os);
1661 /* Save unused output sections in case we can match them
1662 against orphans later. */
1663 if (os->bfd_section == NULL)
1667 /* If we didn't match an active output section, see if we matched an
1668 unused one and use that. */
1669 if (os == NULL && match_by_name)
1671 lang_add_section (&match_by_name->children, s, match_by_name);
1672 return match_by_name;
1677 static struct orphan_save hold[] =
1680 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1683 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1686 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1689 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1695 enum orphan_save_index
1703 static int orphan_init_done = 0;
1704 struct orphan_save *place;
1705 lang_output_section_statement_type *after;
1706 etree_type *address;
1708 if (!orphan_init_done)
1710 struct orphan_save *ho;
1711 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1712 if (ho->name != NULL)
1714 ho->os = lang_output_section_find (ho->name);
1715 if (ho->os != NULL && ho->os->flags == 0)
1716 ho->os->flags = ho->flags;
1718 orphan_init_done = 1;
1721 /* Try to put the new output section in a reasonable place based
1722 on the section name and section flags. */
1725 if ((s->flags & SEC_ALLOC) == 0)
1727 else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
1728 place = &hold[orphan_bss];
1729 else if ((s->flags & SEC_READONLY) == 0)
1730 place = &hold[orphan_data];
1731 else if ((s->flags & SEC_CODE) == 0)
1733 place = (!strncmp (secname, ".idata\$", 7) ? &hold[orphan_idata]
1734 : &hold[orphan_rodata]);
1737 place = &hold[orphan_text];
1742 if (place->os == NULL)
1743 place->os = lang_output_section_find (place->name);
1746 after = lang_output_section_find_by_flags (s, &place->os, NULL);
1748 /* *ABS* is always the first output section statement. */
1749 after = (&lang_output_section_statement.head
1750 ->output_section_statement);
1753 /* All sections in an executable must be aligned to a page boundary. */
1754 address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
1755 os = lang_insert_orphan (s, secname, constraint, after, place, address,
1759 /* If the section name has a '\$', sort it with the other '\$'
1761 for (pl = &os->children.head; *pl != NULL; pl = &(*pl)->header.next)
1763 lang_input_section_type *ls;
1766 if ((*pl)->header.type != lang_input_section_enum)
1769 ls = &(*pl)->input_section;
1771 lname = bfd_get_section_name (ls->section->owner, ls->section);
1772 if (strchr (lname, '\$') != NULL
1773 && (dollar == NULL || strcmp (orig_secname, lname) < 0))
1777 if (add_child.head != NULL)
1779 *add_child.tail = *pl;
1780 *pl = add_child.head;
1787 gld_${EMULATION_NAME}_open_dynamic_archive
1788 (const char *arch ATTRIBUTE_UNUSED,
1789 search_dirs_type *search,
1790 lang_input_statement_type *entry)
1794 const char * format;
1795 bfd_boolean use_prefix;
1799 /* Preferred explicit import library for dll's. */
1800 { "lib%s.dll.a", FALSE },
1801 /* Alternate explicit import library for dll's. */
1802 { "%s.dll.a", FALSE },
1803 /* "libfoo.a" could be either an import lib or a static lib.
1804 For backwards compatibility, libfoo.a needs to precede
1805 libfoo.dll and foo.dll in the search. */
1806 { "lib%s.a", FALSE },
1807 /* The 'native' spelling of an import lib name is "foo.lib". */
1808 { "%s.lib", FALSE },
1810 /* Try "<prefix>foo.dll" (preferred dll name, if specified). */
1811 { "%s%s.dll", TRUE },
1813 /* Try "libfoo.dll" (default preferred dll name). */
1814 { "lib%s.dll", FALSE },
1815 /* Finally try 'native' dll name "foo.dll". */
1816 { "%s.dll", FALSE },
1817 /* Note: If adding more formats to this table, make sure to check to
1818 see if their length is longer than libname_fmt[0].format, and if
1819 so, update the call to xmalloc() below. */
1822 static unsigned int format_max_len = 0;
1823 const char * filename;
1829 if (! entry->maybe_archive)
1832 filename = entry->filename;
1834 if (format_max_len == 0)
1835 /* We need to allow space in the memory that we are going to allocate
1836 for the characters in the format string. Since the format array is
1837 static we only need to calculate this information once. In theory
1838 this value could also be computed statically, but this introduces
1839 the possibility for a discrepancy and hence a possible memory
1840 corruption. The lengths we compute here will be too long because
1841 they will include any formating characters (%s) in the strings, but
1842 this will not matter. */
1843 for (i = 0; libname_fmt[i].format; i++)
1844 if (format_max_len < strlen (libname_fmt[i].format))
1845 format_max_len = strlen (libname_fmt[i].format);
1847 full_string = xmalloc (strlen (search->name)
1851 + (pep_dll_search_prefix
1852 ? strlen (pep_dll_search_prefix) : 0)
1854 /* Allow for the terminating NUL and for the path
1855 separator character that is inserted between
1856 search->name and the start of the format string. */
1859 sprintf (full_string, "%s/", search->name);
1860 base_string = full_string + strlen (full_string);
1862 for (i = 0; libname_fmt[i].format; i++)
1865 if (libname_fmt[i].use_prefix)
1867 if (!pep_dll_search_prefix)
1869 sprintf (base_string, libname_fmt[i].format, pep_dll_search_prefix, filename);
1873 sprintf (base_string, libname_fmt[i].format, filename);
1875 if (ldfile_try_open_bfd (full_string, entry))
1879 if (!libname_fmt[i].format)
1885 entry->filename = full_string;
1891 gld_${EMULATION_NAME}_find_potential_libraries
1892 (char *name, lang_input_statement_type *entry)
1894 return ldfile_open_file_search (name, entry, "", ".lib");
1898 gld_${EMULATION_NAME}_get_script (int *isfile)
1900 # Scripts compiled in.
1901 # sed commands to quote an ld script as a C string.
1902 sc="-f stringify.sed"
1908 if (link_info.relocatable && config.build_constructors)
1911 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1912 echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c
1913 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1914 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1915 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1916 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1917 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1918 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
1919 echo ' ; else if (link_info.pei386_auto_import == 1 && link_info.pei386_runtime_pseudo_reloc != 2) return' >> e${EMULATION_NAME}.c
1920 sed $sc ldscripts/${EMULATION_NAME}.xa >> e${EMULATION_NAME}.c
1922 echo ' ; else return' >> e${EMULATION_NAME}.c
1923 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1924 echo '; }' >> e${EMULATION_NAME}.c
1929 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1931 gld_${EMULATION_NAME}_before_parse,
1934 gld_${EMULATION_NAME}_after_parse,
1935 gld_${EMULATION_NAME}_after_open,
1936 after_allocation_default,
1937 set_output_arch_default,
1938 ldemul_default_target,
1939 gld_${EMULATION_NAME}_before_allocation,
1940 gld_${EMULATION_NAME}_get_script,
1941 "${EMULATION_NAME}",
1943 gld_${EMULATION_NAME}_finish,
1944 NULL, /* Create output section statements. */
1945 gld_${EMULATION_NAME}_open_dynamic_archive,
1946 gld_${EMULATION_NAME}_place_orphan,
1947 gld_${EMULATION_NAME}_set_symbols,
1948 NULL, /* parse_args */
1949 gld${EMULATION_NAME}_add_options,
1950 gld${EMULATION_NAME}_handle_option,
1951 gld_${EMULATION_NAME}_unrecognized_file,
1952 gld_${EMULATION_NAME}_list_options,
1953 gld_${EMULATION_NAME}_recognized_file,
1954 gld_${EMULATION_NAME}_find_potential_libraries,
1955 NULL /* new_vers_pattern. */