1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 if [ -z "$MACHINE" ]; then
6 OUTPUT_ARCH=${ARCH}:${MACHINE}
11 move_default_addr_high=1
14 move_default_addr_high=0;
18 rm -f e${EMULATION_NAME}.c
19 (echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
21 /* Copyright (C) 2006-2020 Free Software Foundation, Inc.
22 Written by Kai Tietz, OneVision Software GmbH&CoKg.
24 This file is part of the GNU Binutils.
26 This program is free software; you can redistribute it and/or modify
27 it under the terms of the GNU General Public License as published by
28 the Free Software Foundation; either version 3 of the License, or
29 (at your option) any later version.
31 This program is distributed in the hope that it will be useful,
32 but WITHOUT ANY WARRANTY; without even the implied warranty of
33 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 GNU General Public License for more details.
36 You should have received a copy of the GNU General Public License
37 along with this program; if not, write to the Free Software
38 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
39 MA 02110-1301, USA. */
42 /* For WINDOWS_XP64 and higher */
43 /* Based on pe.em, but modified for 64 bit support. */
45 #define TARGET_IS_${EMULATION_NAME}
47 #define COFF_IMAGE_WITH_PE
49 #define COFF_WITH_pex64
56 #include "libiberty.h"
57 #include "filenames.h"
68 #include "ldbuildid.h"
69 #include "coff/internal.h"
71 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
72 header in generic PE code. */
73 #include "coff/x86_64.h"
76 /* FIXME: These are BFD internal header files, and we should not be
78 #include "../bfd/libcoff.h"
79 #include "../bfd/libpei.h"
82 #define AOUTSZ PEPAOUTSZ
83 #define PEAOUTHDR PEPAOUTHDR
87 #include "safe-ctype.h"
89 /* Permit the emulation parameters to override the default section
90 alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes
91 it seem that include/coff/internal.h should not define
92 PE_DEF_SECTION_ALIGNMENT. */
93 #if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
94 #undef PE_DEF_SECTION_ALIGNMENT
95 #define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
98 #ifdef TARGET_IS_i386pep
102 #if defined(TARGET_IS_i386pep) || ! defined(DLL_SUPPORT)
103 #define PE_DEF_SUBSYSTEM 3
104 #undef NT_EXE_IMAGE_BASE
105 #define NT_EXE_IMAGE_BASE \
106 ((bfd_vma) (${move_default_addr_high} ? 0x100400000LL \
108 #undef NT_DLL_IMAGE_BASE
109 #define NT_DLL_IMAGE_BASE \
110 ((bfd_vma) (${move_default_addr_high} ? 0x400000000LL \
112 #undef NT_DLL_AUTO_IMAGE_BASE
113 #define NT_DLL_AUTO_IMAGE_BASE \
114 ((bfd_vma) (${move_default_addr_high} ? 0x400000000LL \
116 #undef NT_DLL_AUTO_IMAGE_MASK
117 #define NT_DLL_AUTO_IMAGE_MASK \
118 ((bfd_vma) (${move_default_addr_high} ? 0x1ffff0000LL \
121 #undef NT_EXE_IMAGE_BASE
122 #define NT_EXE_IMAGE_BASE \
123 ((bfd_vma) (${move_default_addr_high} ? 0x100010000LL \
125 #undef NT_DLL_IMAGE_BASE
126 #define NT_DLL_IMAGE_BASE \
127 ((bfd_vma) (${move_default_addr_high} ? 0x110000000LL \
129 #undef NT_DLL_AUTO_IMAGE_BASE
130 #define NT_DLL_AUTO_IMAGE_BASE \
131 ((bfd_vma) (${move_default_addr_high} ? 0x120000000LL \
133 #undef NT_DLL_AUTO_IMAGE_MASK
134 #define NT_DLL_AUTO_IMAGE_MASK \
135 ((bfd_vma) (${move_default_addr_high} ? 0x0ffff0000LL \
137 #undef PE_DEF_SECTION_ALIGNMENT
138 #define PE_DEF_SUBSYSTEM 2
139 #undef PE_DEF_FILE_ALIGNMENT
140 #define PE_DEF_FILE_ALIGNMENT 0x00000200
141 #define PE_DEF_SECTION_ALIGNMENT 0x00000400
144 static struct internal_extra_pe_aouthdr pep;
146 static int pep_subsystem = ${SUBSYSTEM};
147 static flagword real_flags = IMAGE_FILE_LARGE_ADDRESS_AWARE;
148 static int support_old_code = 0;
149 static lang_assignment_statement_type *image_base_statement = 0;
150 static unsigned short pe_dll_characteristics = 0;
151 static bfd_boolean insert_timestamp = TRUE;
152 static const char *emit_build_id;
155 static int pep_enable_stdcall_fixup = 1; /* 0=disable 1=enable (default). */
156 static char * pep_out_def_filename = NULL;
157 static int pep_enable_auto_image_base = 0;
158 static char * pep_dll_search_prefix = NULL;
161 extern const char *output_filename;
163 static int is_underscoring (void)
166 if (pep_leading_underscore != -1)
167 return pep_leading_underscore;
168 if (!bfd_get_target_info ("${OUTPUT_FORMAT}", NULL, NULL, &u, NULL))
169 bfd_get_target_info ("${RELOCATEABLE_OUTPUT_FORMAT}", NULL, NULL, &u, NULL);
173 pep_leading_underscore = (u != 0 ? 1 : 0);
174 return pep_leading_underscore;
179 gld_${EMULATION_NAME}_before_parse (void)
182 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
183 output_filename = "${EXECUTABLE_NAME:-a.exe}";
185 input_flags.dynamic = TRUE;
186 config.has_shared = 1;
187 link_info.pei386_auto_import = 1;
188 link_info.pei386_runtime_pseudo_reloc = 2; /* Use by default version 2. */
192 /* PE format extra command line options. */
194 /* Used for setting flags in the PE header. */
197 OPTION_BASE_FILE = 300 + 1,
199 OPTION_FILE_ALIGNMENT,
201 OPTION_MAJOR_IMAGE_VERSION,
202 OPTION_MAJOR_OS_VERSION,
203 OPTION_MAJOR_SUBSYSTEM_VERSION,
204 OPTION_MINOR_IMAGE_VERSION,
205 OPTION_MINOR_OS_VERSION,
206 OPTION_MINOR_SUBSYSTEM_VERSION,
207 OPTION_SECTION_ALIGNMENT,
211 OPTION_SUPPORT_OLD_CODE,
214 OPTION_EXCLUDE_SYMBOLS,
215 OPTION_EXCLUDE_ALL_SYMBOLS,
217 OPTION_STDCALL_ALIASES,
218 OPTION_ENABLE_STDCALL_FIXUP,
219 OPTION_DISABLE_STDCALL_FIXUP,
220 OPTION_WARN_DUPLICATE_EXPORTS,
222 OPTION_ENABLE_AUTO_IMAGE_BASE,
223 OPTION_DISABLE_AUTO_IMAGE_BASE,
224 OPTION_DLL_SEARCH_PREFIX,
225 OPTION_NO_DEFAULT_EXCLUDES,
226 OPTION_DLL_ENABLE_AUTO_IMPORT,
227 OPTION_DLL_DISABLE_AUTO_IMPORT,
228 OPTION_ENABLE_EXTRA_PE_DEBUG,
230 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC,
231 OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC,
232 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2,
233 OPTION_EXCLUDE_MODULES_FOR_IMPLIB,
234 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES,
235 OPTION_NO_LEADING_UNDERSCORE,
236 OPTION_LEADING_UNDERSCORE,
237 OPTION_ENABLE_LONG_SECTION_NAMES,
238 OPTION_DISABLE_LONG_SECTION_NAMES,
239 OPTION_HIGH_ENTROPY_VA,
241 OPTION_FORCE_INTEGRITY,
247 OPTION_INSERT_TIMESTAMP,
248 OPTION_NO_INSERT_TIMESTAMP,
249 OPTION_TERMINAL_SERVER_AWARE,
251 OPTION_ENABLE_RELOC_SECTION
255 gld${EMULATION_NAME}_add_options
256 (int ns ATTRIBUTE_UNUSED,
257 char **shortopts ATTRIBUTE_UNUSED,
259 struct option **longopts,
260 int nrl ATTRIBUTE_UNUSED,
261 struct option **really_longopts ATTRIBUTE_UNUSED)
263 static const struct option xtra_long[] =
266 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
267 {"dll", no_argument, NULL, OPTION_DLL},
268 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
269 {"heap", required_argument, NULL, OPTION_HEAP},
270 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
271 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
272 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
273 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
274 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
275 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
276 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
277 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
278 {"stack", required_argument, NULL, OPTION_STACK},
279 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
280 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
281 {"use-nul-prefixed-import-tables", no_argument, NULL,
282 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
283 {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
284 {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE},
286 /* getopt allows abbreviations, so we do this to stop it
287 from treating -o as an abbreviation for this option. */
288 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
289 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
290 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
291 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
292 {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS},
293 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
294 {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
295 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
296 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
297 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
298 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
299 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
300 /* getopt() allows abbreviations, so we do this to stop it from
301 treating -c as an abbreviation for these --compat-implib. */
302 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
303 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
304 {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
305 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
306 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
307 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
308 {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
309 {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
310 {"enable-extra-pep-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
311 {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
312 {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
313 {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2},
315 {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
316 {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
317 {"high-entropy-va", no_argument, NULL, OPTION_HIGH_ENTROPY_VA},
318 {"dynamicbase",no_argument, NULL, OPTION_DYNAMIC_BASE},
319 {"forceinteg", no_argument, NULL, OPTION_FORCE_INTEGRITY},
320 {"nxcompat", no_argument, NULL, OPTION_NX_COMPAT},
321 {"no-isolation", no_argument, NULL, OPTION_NO_ISOLATION},
322 {"no-seh", no_argument, NULL, OPTION_NO_SEH},
323 {"no-bind", no_argument, NULL, OPTION_NO_BIND},
324 {"wdmdriver", no_argument, NULL, OPTION_WDM_DRIVER},
325 {"tsaware", no_argument, NULL, OPTION_TERMINAL_SERVER_AWARE},
326 {"insert-timestamp", no_argument, NULL, OPTION_INSERT_TIMESTAMP},
327 {"no-insert-timestamp", no_argument, NULL, OPTION_NO_INSERT_TIMESTAMP},
328 {"build-id", optional_argument, NULL, OPTION_BUILD_ID},
329 {"enable-reloc-section", no_argument, NULL, OPTION_ENABLE_RELOC_SECTION},
330 {NULL, no_argument, NULL, 0}
334 = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
335 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
338 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
339 parameters which may be input from the command line. */
348 /* FALSE for an assembly level symbol and TRUE for a C visible symbol.
349 C visible symbols can be prefixed by underscore dependent on target's
351 bfd_boolean is_c_symbol;
354 #define GET_INIT_SYMBOL_NAME(IDX) \
355 (init[(IDX)].symbol \
356 + ((!init[(IDX)].is_c_symbol || is_underscoring () == 1) ? 0 : 1))
358 /* Decorates the C visible symbol by underscore, if target requires. */
360 ((is_underscoring () == 0) ? CSTR : "_" CSTR)
362 #define D(field,symbol,def,usc) {&pep.field, sizeof (pep.field), def, symbol, 0, usc}
364 static definfo init[] =
366 /* imagebase must be first */
367 #define IMAGEBASEOFF 0
368 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE, FALSE),
370 {&dll, sizeof(dll), 0, "__dll__", 0, FALSE},
371 #define MSIMAGEBASEOFF 2
372 D(ImageBase, "___ImageBase", NT_EXE_IMAGE_BASE, TRUE),
373 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT, FALSE),
374 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT, FALSE),
375 D(MajorOperatingSystemVersion,"__major_os_version__", 4, FALSE),
376 D(MinorOperatingSystemVersion,"__minor_os_version__", 0, FALSE),
377 D(MajorImageVersion,"__major_image_version__", 0, FALSE),
378 D(MinorImageVersion,"__minor_image_version__", 0, FALSE),
379 D(MajorSubsystemVersion,"__major_subsystem_version__", 5, FALSE),
380 D(MinorSubsystemVersion,"__minor_subsystem_version__", 2, FALSE),
381 D(Subsystem,"__subsystem__", ${SUBSYSTEM}, FALSE),
382 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000, FALSE),
383 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000, FALSE),
384 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000, FALSE),
385 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000, FALSE),
386 D(LoaderFlags,"__loader_flags__", 0x0, FALSE),
387 D(DllCharacteristics, "__dll_characteristics__", 0x0, FALSE),
388 { NULL, 0, 0, NULL, 0, FALSE}
393 gld_${EMULATION_NAME}_list_options (FILE *file)
395 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
396 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
397 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
398 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
399 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
400 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
401 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
402 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
403 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
404 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
405 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
406 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
407 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
408 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
409 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
410 fprintf (file, _(" --[no-]leading-underscore Set explicit symbol underscore prefix mode\n"));
411 fprintf (file, _(" --[no-]insert-timestamp Use a real timestamp rather than zero (default)\n"));
412 fprintf (file, _(" This makes binaries non-deterministic\n"));
414 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
415 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
416 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
417 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
418 fprintf (file, _(" --exclude-all-symbols Exclude all symbols from automatic export\n"));
419 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
420 fprintf (file, _(" --exclude-modules-for-implib mod,mod,...\n"));
421 fprintf (file, _(" Exclude objects, archive members from auto\n"));
422 fprintf (file, _(" export, place into import library instead\n"));
423 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
424 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
425 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
426 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports\n"));
427 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
428 create __imp_<SYMBOL> as well\n"));
429 fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n\
430 unless user specifies one\n"));
431 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base (default)\n"));
432 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
433 an importlib, use <string><basename>.dll\n\
434 in preference to lib<basename>.dll \n"));
435 fprintf (file, _(" --enable-auto-import Do sophisticated linking of _sym to\n\
436 __imp_sym for DATA references\n"));
437 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
438 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
439 adding pseudo-relocations resolved at\n\
441 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
442 auto-imported DATA\n"));
443 fprintf (file, _(" --enable-extra-pep-debug Enable verbose debug output when building\n\
444 or linking to DLLs (esp. auto-import)\n"));
445 fprintf (file, _(" --enable-long-section-names Use long COFF section names even in\n\
446 executable image files\n"));
447 fprintf (file, _(" --disable-long-section-names Never use long COFF section names, even\n\
448 in object files\n"));
449 fprintf (file, _(" --high-entropy-va Image is compatible with 64-bit address space\n\
450 layout randomization (ASLR)\n"));
451 fprintf (file, _(" --dynamicbase Image base address may be relocated using\n\
452 address space layout randomization (ASLR)\n"));
453 fprintf (file, _(" --enable-reloc-section Create the base relocation table\n"));
454 fprintf (file, _(" --forceinteg Code integrity checks are enforced\n"));
455 fprintf (file, _(" --nxcompat Image is compatible with data execution prevention\n"));
456 fprintf (file, _(" --no-isolation Image understands isolation but do not isolate the image\n"));
457 fprintf (file, _(" --no-seh Image does not use SEH; no SE handler may\n\
458 be called in this image\n"));
459 fprintf (file, _(" --no-bind Do not bind this image\n"));
460 fprintf (file, _(" --wdmdriver Driver uses the WDM model\n"));
461 fprintf (file, _(" --tsaware Image is Terminal Server aware\n"));
462 fprintf (file, _(" --build-id[=STYLE] Generate build ID\n"));
468 set_pep_name (char *name, bfd_vma val)
472 /* Find the name and set it. */
473 for (i = 0; init[i].ptr; i++)
475 if (strcmp (name, GET_INIT_SYMBOL_NAME (i)) == 0)
479 if (strcmp (name,"__image_base__") == 0)
480 set_pep_name (U ("__ImageBase"), val);
488 set_entry_point (void)
491 const char *initial_symbol_char;
501 { 1, "NtProcessStartup" },
502 { 2, "WinMainCRTStartup" },
503 { 3, "mainCRTStartup" },
504 { 7, "__PosixProcessStartup" },
505 { 9, "WinMainCRTStartup" },
506 {14, "mainCRTStartup" },
510 /* Entry point name for arbitrary subsystem numbers. */
511 static const char default_entry[] = "mainCRTStartup";
513 if (bfd_link_pic (&link_info) || dll)
515 entry = "DllMainCRTStartup";
519 for (i = 0; v[i].entry; i++)
520 if (v[i].value == pep_subsystem)
523 /* If no match, use the default. */
524 if (v[i].entry != NULL)
527 entry = default_entry;
530 /* Now we check target's default for getting proper symbol_char. */
531 initial_symbol_char = (is_underscoring () != 0 ? "_" : "");
533 if (*initial_symbol_char != '\0')
537 /* lang_default_entry expects its argument to be permanently
538 allocated, so we don't free this string. */
539 alc_entry = xmalloc (strlen (initial_symbol_char)
542 strcpy (alc_entry, initial_symbol_char);
543 strcat (alc_entry, entry);
547 lang_default_entry (entry);
551 set_pep_subsystem (void)
557 unsigned long temp_subsystem;
574 /* Check for the presence of a version number. */
575 sver = strchr (optarg, ':');
577 len = strlen (optarg);
581 set_pep_name ("__major_subsystem_version__",
582 strtoul (sver + 1, &end, 0));
584 set_pep_name ("__minor_subsystem_version__",
585 strtoul (end + 1, &end, 0));
587 einfo (_("%P: warning: bad version number in -subsystem option\n"));
590 /* Check for numeric subsystem. */
591 temp_subsystem = strtoul (optarg, & end, 0);
592 if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
594 /* Search list for a numeric match to use its entry point. */
595 for (i = 0; v[i].name; i++)
596 if (v[i].value == (int) temp_subsystem)
599 /* Use this subsystem. */
600 pep_subsystem = (int) temp_subsystem;
604 /* Search for subsystem by name. */
605 for (i = 0; v[i].name; i++)
606 if (strncmp (optarg, v[i].name, len) == 0
607 && v[i].name[len] == '\0')
610 if (v[i].name == NULL)
612 einfo (_("%F%P: invalid subsystem type %s\n"), optarg);
616 pep_subsystem = v[i].value;
619 set_pep_name ("__subsystem__", pep_subsystem);
626 set_pep_value (char *name)
630 set_pep_name (name, (bfd_vma) strtoull (optarg, &end, 0));
633 einfo (_("%F%P: invalid hex number for PE parameter '%s'\n"), optarg);
640 set_pep_stack_heap (char *resname, char *comname)
642 set_pep_value (resname);
647 set_pep_value (comname);
650 einfo (_("%F%P: strange hex info for PE parameter '%s'\n"), optarg);
653 #define DEFAULT_BUILD_ID_STYLE "md5"
656 gld${EMULATION_NAME}_handle_option (int optc)
664 case OPTION_BASE_FILE:
665 link_info.base_file = fopen (optarg, FOPEN_WB);
666 if (link_info.base_file == NULL)
667 einfo (_("%F%P: cannot open base file %s\n"), optarg);
672 set_pep_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
675 set_pep_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
677 case OPTION_SUBSYSTEM:
678 set_pep_subsystem ();
680 case OPTION_MAJOR_OS_VERSION:
681 set_pep_value ("__major_os_version__");
683 case OPTION_MINOR_OS_VERSION:
684 set_pep_value ("__minor_os_version__");
686 case OPTION_MAJOR_SUBSYSTEM_VERSION:
687 set_pep_value ("__major_subsystem_version__");
689 case OPTION_MINOR_SUBSYSTEM_VERSION:
690 set_pep_value ("__minor_subsystem_version__");
692 case OPTION_MAJOR_IMAGE_VERSION:
693 set_pep_value ("__major_image_version__");
695 case OPTION_MINOR_IMAGE_VERSION:
696 set_pep_value ("__minor_image_version__");
698 case OPTION_FILE_ALIGNMENT:
699 set_pep_value ("__file_alignment__");
701 case OPTION_SECTION_ALIGNMENT:
702 set_pep_value ("__section_alignment__");
705 set_pep_name ("__dll__", 1);
707 case OPTION_IMAGE_BASE:
708 set_pep_value ("__image_base__");
710 case OPTION_SUPPORT_OLD_CODE:
711 support_old_code = 1;
713 case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
714 pep_use_nul_prefixed_import_tables = TRUE;
716 case OPTION_NO_LEADING_UNDERSCORE:
717 pep_leading_underscore = 0;
719 case OPTION_LEADING_UNDERSCORE:
720 pep_leading_underscore = 1;
722 case OPTION_INSERT_TIMESTAMP:
723 insert_timestamp = TRUE;
725 case OPTION_NO_INSERT_TIMESTAMP:
726 insert_timestamp = FALSE;
730 pep_out_def_filename = xstrdup (optarg);
732 case OPTION_EXPORT_ALL:
733 pep_dll_export_everything = 1;
735 case OPTION_EXCLUDE_SYMBOLS:
736 pep_dll_add_excludes (optarg, EXCLUDESYMS);
738 case OPTION_EXCLUDE_ALL_SYMBOLS:
739 pep_dll_exclude_all_symbols = 1;
741 case OPTION_EXCLUDE_LIBS:
742 pep_dll_add_excludes (optarg, EXCLUDELIBS);
744 case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
745 pep_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
747 case OPTION_KILL_ATS:
748 pep_dll_kill_ats = 1;
750 case OPTION_STDCALL_ALIASES:
751 pep_dll_stdcall_aliases = 1;
753 case OPTION_ENABLE_STDCALL_FIXUP:
754 pep_enable_stdcall_fixup = 1;
756 case OPTION_DISABLE_STDCALL_FIXUP:
757 pep_enable_stdcall_fixup = 0;
759 case OPTION_WARN_DUPLICATE_EXPORTS:
760 pep_dll_warn_dup_exports = 1;
762 case OPTION_IMP_COMPAT:
763 pep_dll_compat_implib = 1;
765 case OPTION_ENABLE_AUTO_IMAGE_BASE:
766 pep_enable_auto_image_base = 1;
768 case OPTION_DISABLE_AUTO_IMAGE_BASE:
769 pep_enable_auto_image_base = 0;
771 case OPTION_DLL_SEARCH_PREFIX:
772 pep_dll_search_prefix = xstrdup (optarg);
774 case OPTION_NO_DEFAULT_EXCLUDES:
775 pep_dll_do_default_excludes = 0;
777 case OPTION_DLL_ENABLE_AUTO_IMPORT:
778 link_info.pei386_auto_import = 1;
780 case OPTION_DLL_DISABLE_AUTO_IMPORT:
781 link_info.pei386_auto_import = 0;
783 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
784 link_info.pei386_runtime_pseudo_reloc = 2;
786 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
787 link_info.pei386_runtime_pseudo_reloc = 0;
789 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2:
790 link_info.pei386_runtime_pseudo_reloc = 2;
792 case OPTION_ENABLE_EXTRA_PE_DEBUG:
793 pep_dll_extra_pe_debug = 1;
796 case OPTION_ENABLE_LONG_SECTION_NAMES:
797 pep_use_coff_long_section_names = 1;
799 case OPTION_DISABLE_LONG_SECTION_NAMES:
800 pep_use_coff_long_section_names = 0;
802 /* Get DLLCharacteristics bits */
803 case OPTION_HIGH_ENTROPY_VA:
804 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA;
806 case OPTION_DYNAMIC_BASE:
807 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
809 case OPTION_ENABLE_RELOC_SECTION:
810 pep_dll_enable_reloc_section = 1;
812 case OPTION_FORCE_INTEGRITY:
813 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
815 case OPTION_NX_COMPAT:
816 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
818 case OPTION_NO_ISOLATION:
819 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
822 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_SEH;
825 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_BIND;
827 case OPTION_WDM_DRIVER:
828 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
830 case OPTION_TERMINAL_SERVER_AWARE:
831 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
833 case OPTION_BUILD_ID:
834 if (emit_build_id != NULL)
836 free ((char *) emit_build_id);
837 emit_build_id = NULL;
840 optarg = DEFAULT_BUILD_ID_STYLE;
841 if (strcmp (optarg, "none"))
842 emit_build_id = xstrdup (optarg);
846 /* Set DLLCharacteristics bits */
847 set_pep_name ("__dll_characteristics__", pe_dll_characteristics);
855 strhash (const char *str)
857 const unsigned char *s;
864 s = (const unsigned char *) str;
865 while ((c = *s++) != '\0')
867 hash += c + (c << 17);
871 hash += len + (len << 17);
877 /* Use the output file to create a image base for relocatable DLLs. */
880 compute_dll_image_base (const char *ofile)
882 bfd_vma hash = (bfd_vma) strhash (ofile);
883 return NT_DLL_AUTO_IMAGE_BASE + ((hash << 16) & NT_DLL_AUTO_IMAGE_MASK);
887 /* Assign values to the special symbols before the linker script is
891 gld_${EMULATION_NAME}_set_symbols (void)
893 /* Run through and invent symbols for all the
894 names and insert the defaults. */
899 if (!init[IMAGEBASEOFF].inited)
901 if (bfd_link_relocatable (&link_info))
902 init[IMAGEBASEOFF].value = 0;
903 else if (init[DLLOFF].value || bfd_link_dll (&link_info))
906 init[IMAGEBASEOFF].value = (pep_enable_auto_image_base
907 ? compute_dll_image_base (output_filename)
908 : NT_DLL_IMAGE_BASE);
910 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
914 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
915 init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
918 /* Don't do any symbol assignments if this is a relocatable link. */
919 if (bfd_link_relocatable (&link_info))
922 /* Glue the assignments into the abs section. */
923 push_stat_ptr (&abs_output_section->children);
925 for (j = 0; init[j].ptr; j++)
927 bfd_vma val = init[j].value;
928 lang_assignment_statement_type *rv;
930 rv = lang_add_assignment (exp_assign (GET_INIT_SYMBOL_NAME (j),
931 exp_intop (val), FALSE));
932 if (init[j].size == sizeof (short))
933 *(short *) init[j].ptr = (short) val;
934 else if (init[j].size == sizeof (int))
935 *(int *) init[j].ptr = (int) val;
936 else if (init[j].size == sizeof (long))
937 *(long *) init[j].ptr = (long) val;
938 /* This might be a long long or other special type. */
939 else if (init[j].size == sizeof (bfd_vma))
940 *(bfd_vma *) init[j].ptr = val;
942 if (j == IMAGEBASEOFF)
943 image_base_statement = rv;
945 /* Restore the pointer. */
948 if (pep.FileAlignment > pep.SectionAlignment)
950 einfo (_("%P: warning, file alignment > section alignment\n"));
954 /* This is called after the linker script and the command line options
958 gld_${EMULATION_NAME}_after_parse (void)
960 /* PR ld/6744: Warn the user if they have used an ELF-only
961 option hoping it will work on PE+. */
962 if (link_info.export_dynamic)
963 einfo (_("%P: warning: --export-dynamic is not supported for PE+ "
964 "targets, did you mean --export-all-symbols?\n"));
968 after_parse_default ();
972 static struct bfd_link_hash_entry *pep_undef_found_sym;
975 pep_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
979 const char *hs = h->root.string;
981 sl = strlen (string);
982 if (h->type == bfd_link_hash_defined
983 && ((*hs == '@' && *string == '_'
984 && strncmp (hs + 1, string + 1, sl - 1) == 0)
985 || strncmp (hs, string, sl) == 0)
986 && h->root.string[sl] == '@')
988 pep_undef_found_sym = h;
995 pep_fixup_stdcalls (void)
997 static int gave_warning_message = 0;
998 struct bfd_link_hash_entry *undef, *sym;
1000 if (pep_dll_extra_pe_debug)
1001 printf ("%s\n", __FUNCTION__);
1003 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
1004 if (undef->type == bfd_link_hash_undefined)
1006 char* at = strchr (undef->root.string, '@');
1007 int lead_at = (*undef->root.string == '@');
1009 at = strchr (undef->root.string + 1, '@');
1012 /* The symbol is a stdcall symbol, so let's look for a
1013 cdecl symbol with the same name and resolve to that. */
1014 char *cname = xstrdup (undef->root.string);
1018 at = strchr (cname, '@');
1021 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
1023 if (sym && sym->type == bfd_link_hash_defined)
1025 undef->type = bfd_link_hash_defined;
1026 undef->u.def.value = sym->u.def.value;
1027 undef->u.def.section = sym->u.def.section;
1029 if (pep_enable_stdcall_fixup == -1)
1031 einfo (_("warning: resolving %s by linking to %s\n"),
1032 undef->root.string, cname);
1033 if (! gave_warning_message)
1035 gave_warning_message = 1;
1036 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
1037 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
1044 /* The symbol is a cdecl symbol, so we look for stdcall
1045 symbols - which means scanning the whole symbol table. */
1046 pep_undef_found_sym = 0;
1047 bfd_link_hash_traverse (link_info.hash, pep_undef_cdecl_match,
1048 (char *) undef->root.string);
1049 sym = pep_undef_found_sym;
1052 undef->type = bfd_link_hash_defined;
1053 undef->u.def.value = sym->u.def.value;
1054 undef->u.def.section = sym->u.def.section;
1056 if (pep_enable_stdcall_fixup == -1)
1058 einfo (_("warning: resolving %s by linking to %s\n"),
1059 undef->root.string, sym->root.string);
1060 if (! gave_warning_message)
1062 gave_warning_message = 1;
1063 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
1064 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
1073 make_import_fixup (arelent *rel, asection *s, char *name, const char *symname)
1075 struct bfd_symbol *sym = *rel->sym_ptr_ptr;
1077 bfd_vma _addend = 0;
1080 if (pep_dll_extra_pe_debug)
1081 printf ("arelent: %s@%#lx: add=%li\n", sym->name,
1082 (unsigned long) rel->address, (long) rel->addend);
1084 memset (addend, 0, sizeof (addend));
1085 switch ((rel->howto->bitsize))
1088 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 1);
1089 if (suc && rel->howto->pc_relative)
1090 _addend = bfd_get_signed_8 (s->owner, addend);
1092 _addend = bfd_get_8 (s->owner, addend);
1095 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 2);
1096 if (suc && rel->howto->pc_relative)
1097 _addend = bfd_get_signed_16 (s->owner, addend);
1099 _addend = bfd_get_16 (s->owner, addend);
1102 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 4);
1103 if (suc && rel->howto->pc_relative)
1104 _addend = bfd_get_signed_32 (s->owner, addend);
1106 _addend = bfd_get_32 (s->owner, addend);
1109 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 8);
1111 _addend = bfd_get_64 (s->owner, addend);
1115 einfo (_("%P: %C: cannot get section contents - auto-import exception\n"),
1116 s->owner, s, rel->address);
1118 if (pep_dll_extra_pe_debug)
1120 printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx",
1121 (long) _addend, (long) rel->addend, (long) rel->address);
1122 if (rel->howto->pc_relative)
1124 printf (" %d bit rel.\n", (int) rel->howto->bitsize);
1127 pep_create_import_fixup (rel, s, _addend, name, symname);
1131 pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
1133 printf ("+%s\n", h->string);
1137 #endif /* DLL_SUPPORT */
1140 debug_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
1142 int *found = (int *) obj;
1144 if (strncmp (".debug_", sect->name, sizeof (".debug_") - 1) == 0)
1149 pecoff_checksum_contents (bfd *abfd,
1150 void (*process) (const void *, size_t, void *),
1153 file_ptr filepos = (file_ptr) 0;
1160 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
1163 status = bfd_bread (&b, (bfd_size_type) 1, abfd);
1169 (*process) (&b, 1, arg);
1177 write_build_id (bfd *abfd)
1179 struct pe_tdata *t = pe_data (abfd);
1181 struct bfd_link_order *link_order = NULL;
1182 unsigned char *contents;
1184 bfd_size_type build_id_size;
1185 unsigned char *build_id;
1187 /* Find the section the .buildid output section has been merged info. */
1188 for (asec = abfd->sections; asec != NULL; asec = asec->next)
1190 struct bfd_link_order *l = NULL;
1191 for (l = asec->map_head.link_order; l != NULL; l = l->next)
1193 if (l->type == bfd_indirect_link_order)
1195 if (l->u.indirect.section == t->build_id.sec)
1209 einfo (_("%P: warning: .buildid section discarded,"
1210 " --build-id ignored\n"));
1214 if (t->build_id.sec->contents == NULL)
1215 t->build_id.sec->contents = (unsigned char *) xmalloc (t->build_id.sec->size);
1216 contents = t->build_id.sec->contents;
1217 size = t->build_id.sec->size;
1219 build_id_size = compute_build_id_size (t->build_id.style);
1220 build_id = xmalloc (build_id_size);
1221 generate_build_id (abfd, t->build_id.style, pecoff_checksum_contents, build_id, build_id_size);
1223 bfd_vma ib = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase;
1225 /* Construct a debug directory entry which points to an immediately following CodeView record. */
1226 struct internal_IMAGE_DEBUG_DIRECTORY idd;
1227 idd.Characteristics = 0;
1228 idd.TimeDateStamp = 0;
1229 idd.MajorVersion = 0;
1230 idd.MinorVersion = 0;
1231 idd.Type = PE_IMAGE_DEBUG_TYPE_CODEVIEW;
1232 idd.SizeOfData = sizeof (CV_INFO_PDB70) + 1;
1233 idd.AddressOfRawData = asec->vma - ib + link_order->offset
1234 + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1235 idd.PointerToRawData = asec->filepos + link_order->offset
1236 + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1238 struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *)contents;
1239 _bfd_XXi_swap_debugdir_out (abfd, &idd, ext);
1241 /* Write the debug directory enttry */
1242 if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0)
1245 if (bfd_bwrite (contents, size, abfd) != size)
1248 /* Construct the CodeView record. */
1249 CODEVIEW_INFO cvinfo;
1250 cvinfo.CVSignature = CVINFO_PDB70_CVSIGNATURE;
1253 /* Zero pad or truncate the generated build_id to fit in the CodeView record. */
1254 memset (&(cvinfo.Signature), 0, CV_INFO_SIGNATURE_LENGTH);
1255 memcpy (&(cvinfo.Signature), build_id, (build_id_size > CV_INFO_SIGNATURE_LENGTH)
1256 ? CV_INFO_SIGNATURE_LENGTH : build_id_size);
1260 /* Write the codeview record. */
1261 if (_bfd_XXi_write_codeview_record (abfd, idd.PointerToRawData, &cvinfo) == 0)
1264 /* Record the location of the debug directory in the data directory. */
1265 pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress
1266 = asec->vma - ib + link_order->offset;
1267 pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
1268 = sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1273 /* Make .buildid section, and set up coff_tdata->build_id. */
1275 setup_build_id (bfd *ibfd)
1280 if (!validate_build_id_style (emit_build_id))
1282 einfo (_("%P: warning: unrecognized --build-id style ignored\n"));
1286 flags = (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY
1287 | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA);
1288 s = bfd_make_section_anyway_with_flags (ibfd, ".buildid", flags);
1291 struct pe_tdata *t = pe_data (link_info.output_bfd);
1292 t->build_id.after_write_object_contents = &write_build_id;
1293 t->build_id.style = emit_build_id;
1294 t->build_id.sec = s;
1296 /* Section is a fixed size:
1297 One IMAGE_DEBUG_DIRECTORY entry, of type IMAGE_DEBUG_TYPE_CODEVIEW,
1298 pointing at a CV_INFO_PDB70 record containing the build-id, with a
1299 null byte for PdbFileName. */
1300 s->size = sizeof (struct external_IMAGE_DEBUG_DIRECTORY)
1301 + sizeof (CV_INFO_PDB70) + 1;
1306 einfo (_("%P: warning: cannot create .buildid section,"
1307 " --build-id ignored\n"));
1312 gld_${EMULATION_NAME}_after_open (void)
1314 after_open_default ();
1318 if (pep_dll_extra_pe_debug)
1321 struct bfd_link_hash_entry *sym;
1323 printf ("%s()\n", __FUNCTION__);
1325 for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1326 printf ("-%s\n", sym->root.string);
1327 bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
1329 for (a = link_info.input_bfds; a; a = a->link.next)
1330 printf ("*%s\n",a->filename);
1334 if (emit_build_id != NULL)
1338 /* Find a COFF input. */
1339 for (abfd = link_info.input_bfds;
1340 abfd != (bfd *) NULL; abfd = abfd->link.next)
1341 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
1344 /* If there are no COFF input files do not try to
1345 add a build-id section. */
1347 || !setup_build_id (abfd))
1349 free ((char *) emit_build_id);
1350 emit_build_id = NULL;
1354 /* Pass the wacky PE command line options into the output bfd.
1355 FIXME: This should be done via a function, rather than by
1356 including an internal BFD header. */
1358 if (bfd_get_flavour (link_info.output_bfd) != bfd_target_coff_flavour
1359 || coff_data (link_info.output_bfd) == NULL
1360 || coff_data (link_info.output_bfd)->pe == 0)
1361 einfo (_("%F%P: cannot perform PE operations on non PE output file '%pB'\n"),
1362 link_info.output_bfd);
1364 pe_data (link_info.output_bfd)->pe_opthdr = pep;
1365 pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
1366 pe_data (link_info.output_bfd)->real_flags |= real_flags;
1367 if (insert_timestamp)
1368 pe_data (link_info.output_bfd)->timestamp = -1;
1370 pe_data (link_info.output_bfd)->timestamp = 0;
1372 /* At this point we must decide whether to use long section names
1373 in the output or not. If the user hasn't explicitly specified
1374 on the command line, we leave it to the default for the format
1375 (object files yes, image files no), except if there is debug
1376 information present; GDB relies on the long section names to
1377 find it, so enable it in that case. */
1378 if (pep_use_coff_long_section_names < 0 && link_info.strip == strip_none)
1380 if (bfd_link_relocatable (&link_info))
1381 pep_use_coff_long_section_names = 1;
1384 /* Iterate over all sections of all input BFDs, checking
1385 for any that begin 'debug_' and are long names. */
1386 LANG_FOR_EACH_INPUT_STATEMENT (is)
1388 int found_debug = 0;
1390 bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug);
1393 pep_use_coff_long_section_names = 1;
1400 pep_output_file_set_long_section_names (link_info.output_bfd);
1403 pep_process_import_defs (link_info.output_bfd, &link_info);
1405 if (link_info.pei386_auto_import) /* -1=warn or 1=enable */
1406 pep_find_data_imports (U ("_head_"), make_import_fixup);
1408 /* The implementation of the feature is rather dumb and would cause the
1409 compilation time to go through the roof if there are many undefined
1410 symbols in the link, so it needs to be run after auto-import. */
1411 if (pep_enable_stdcall_fixup) /* -1=warn or 1=enable */
1412 pep_fixup_stdcalls ();
1414 #ifndef TARGET_IS_i386pep
1415 if (bfd_link_pic (&link_info))
1417 if (!bfd_link_relocatable (&link_info))
1419 pep_dll_build_sections (link_info.output_bfd, &link_info);
1421 #ifndef TARGET_IS_i386pep
1423 pep_exe_build_sections (link_info.output_bfd, &link_info);
1425 #endif /* DLL_SUPPORT */
1428 /* This next chunk of code tries to detect the case where you have
1429 two import libraries for the same DLL (specifically,
1430 symbolically linking libm.a and libc.a in cygwin to
1431 libcygwin.a). In those cases, it's possible for function
1432 thunks from the second implib to be used but without the
1433 head/tail objects, causing an improper import table. We detect
1434 those cases and rename the "other" import libraries to match
1435 the one the head/tail come from, so that the linker will sort
1436 things nicely and produce a valid import table. */
1438 LANG_FOR_EACH_INPUT_STATEMENT (is)
1440 if (is->the_bfd->my_archive)
1442 int idata2 = 0, reloc_count=0, is_imp = 0;
1445 /* See if this is an import library thunk. */
1446 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1448 if (strcmp (sec->name, ".idata\$2") == 0)
1450 if (CONST_STRNEQ (sec->name, ".idata\$"))
1452 reloc_count += sec->reloc_count;
1455 if (is_imp && !idata2 && reloc_count)
1457 /* It is, look for the reference to head and see if it's
1458 from our own library. */
1459 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1467 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1471 if (!bfd_generic_link_read_symbols (is->the_bfd))
1473 einfo (_("%F%P: %pB: could not read symbols: %E\n"),
1477 symbols = bfd_get_outsymbols (is->the_bfd);
1479 relocs = xmalloc ((size_t) relsize);
1480 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1485 einfo (_("%X%P: unable to process relocs: %E\n"));
1489 for (i = 0; i < nrelocs; i++)
1491 struct bfd_symbol *s;
1492 struct bfd_link_hash_entry * blhe;
1493 const char *other_bfd_filename;
1496 s = (relocs[i]->sym_ptr_ptr)[0];
1498 if (s->flags & BSF_LOCAL)
1501 /* Thunk section with reloc to another bfd. */
1502 blhe = bfd_link_hash_lookup (link_info.hash,
1504 FALSE, FALSE, TRUE);
1507 || blhe->type != bfd_link_hash_defined)
1511 = blhe->u.def.section->owner->my_archive
1512 ? bfd_get_filename (blhe->u.def.section->owner->my_archive)
1513 : bfd_get_filename (blhe->u.def.section->owner);
1515 if (filename_cmp (bfd_get_filename
1516 (is->the_bfd->my_archive),
1517 other_bfd_filename) == 0)
1520 /* Rename this implib to match the other one. */
1521 n = xmalloc (strlen (other_bfd_filename) + 1);
1522 strcpy (n, other_bfd_filename);
1523 bfd_set_filename (is->the_bfd->my_archive, n);
1527 /* Note - we do not free the symbols,
1528 they are now cached in the BFD. */
1538 lang_input_statement_type *is2;
1539 lang_input_statement_type *is3;
1541 /* Careful - this is a shell script. Watch those dollar signs! */
1542 /* Microsoft import libraries have every member named the same,
1543 and not in the right order for us to link them correctly. We
1544 must detect these and rename the members so that they'll link
1545 correctly. There are three types of objects: the head, the
1546 thunks, and the sentinel(s). The head is easy; it's the one
1547 with idata2. We assume that the sentinels won't have relocs,
1548 and the thunks will. It's easier than checking the symbol
1549 table for external references. */
1550 LANG_FOR_EACH_INPUT_STATEMENT (is)
1552 if (is->the_bfd->my_archive)
1555 bfd *arch = is->the_bfd->my_archive;
1557 if (cur_arch != arch)
1563 is3 && is3->the_bfd->my_archive == arch;
1564 is3 = (lang_input_statement_type *) is3->next)
1566 /* A MS dynamic import library can also contain static
1567 members, so look for the first element with a .dll
1568 extension, and use that for the remainder of the
1570 pnt = strrchr (is3->the_bfd->filename, '.');
1571 if (pnt != NULL && filename_cmp (pnt, ".dll") == 0)
1579 /* OK, found one. Now look to see if the remaining
1580 (dynamic import) members use the same name. */
1582 is2 && is2->the_bfd->my_archive == arch;
1583 is2 = (lang_input_statement_type *) is2->next)
1585 /* Skip static members, ie anything with a .obj
1587 pnt = strrchr (is2->the_bfd->filename, '.');
1588 if (pnt != NULL && filename_cmp (pnt, ".obj") == 0)
1591 if (filename_cmp (is3->the_bfd->filename,
1592 is2->the_bfd->filename))
1601 /* This fragment might have come from an .obj file in a Microsoft
1602 import, and not an actual import record. If this is the case,
1603 then leave the filename alone. */
1604 pnt = strrchr (is->the_bfd->filename, '.');
1606 if (is_ms_arch && (filename_cmp (pnt, ".dll") == 0))
1608 int idata2 = 0, reloc_count=0;
1610 char *new_name, seq;
1612 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1614 if (strcmp (sec->name, ".idata\$2") == 0)
1616 reloc_count += sec->reloc_count;
1619 if (idata2) /* .idata2 is the TOC */
1621 else if (reloc_count > 0) /* thunks */
1626 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
1627 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
1628 bfd_set_filename (is->the_bfd, new_name);
1630 new_name = xmalloc (strlen (is->filename) + 3);
1631 sprintf (new_name, "%s.%c", is->filename, seq);
1632 is->filename = new_name;
1640 gld_${EMULATION_NAME}_before_allocation (void)
1643 before_allocation_default ();
1647 /* This is called when an input file isn't recognized as a BFD. We
1648 check here for .DEF files and pull them in automatically. */
1651 saw_option (char *option)
1657 for (i = 0; init[i].ptr; i++)
1658 if (strcmp (GET_INIT_SYMBOL_NAME (i), option) == 0)
1659 return init[i].inited;
1662 #endif /* DLL_SUPPORT */
1665 gld_${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1668 const char *ext = entry->filename + strlen (entry->filename) - 4;
1670 if (filename_cmp (ext, ".def") == 0 || filename_cmp (ext, ".DEF") == 0)
1672 pep_def_file = def_file_parse (entry->filename, pep_def_file);
1676 int i, buflen=0, len;
1679 for (i = 0; i < pep_def_file->num_exports; i++)
1681 len = strlen (pep_def_file->exports[i].internal_name);
1682 if (buflen < len + 2)
1686 buf = xmalloc (buflen);
1688 for (i = 0; i < pep_def_file->num_exports; i++)
1690 struct bfd_link_hash_entry *h;
1692 sprintf (buf, "%s%s", U (""),
1693 pep_def_file->exports[i].internal_name);
1695 h = bfd_link_hash_lookup (link_info.hash, buf, TRUE, TRUE, TRUE);
1696 if (h == (struct bfd_link_hash_entry *) NULL)
1697 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
1698 if (h->type == bfd_link_hash_new)
1700 h->type = bfd_link_hash_undefined;
1701 h->u.undef.abfd = NULL;
1702 bfd_link_add_undef (link_info.hash, h);
1707 /* def_file_print (stdout, pep_def_file); */
1708 if (pep_def_file->is_dll == 1)
1709 link_info.type = type_dll;
1711 if (pep_def_file->base_address != (bfd_vma)(-1))
1714 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1715 = init[IMAGEBASEOFF].value
1716 = pep_def_file->base_address;
1717 init[IMAGEBASEOFF].inited = 1;
1718 if (image_base_statement)
1719 image_base_statement->exp
1720 = exp_assign ("__image_base__", exp_intop (pep.ImageBase),
1724 if (pep_def_file->stack_reserve != -1
1725 && ! saw_option ("__size_of_stack_reserve__"))
1727 pep.SizeOfStackReserve = pep_def_file->stack_reserve;
1728 if (pep_def_file->stack_commit != -1)
1729 pep.SizeOfStackCommit = pep_def_file->stack_commit;
1731 if (pep_def_file->heap_reserve != -1
1732 && ! saw_option ("__size_of_heap_reserve__"))
1734 pep.SizeOfHeapReserve = pep_def_file->heap_reserve;
1735 if (pep_def_file->heap_commit != -1)
1736 pep.SizeOfHeapCommit = pep_def_file->heap_commit;
1746 gld_${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1750 #ifdef TARGET_IS_i386pep
1751 pep_dll_id_target ("pei-x86-64");
1753 if (pep_bfd_is_dll (entry->the_bfd))
1754 return pep_implied_import_dll (entry->filename);
1760 gld_${EMULATION_NAME}_finish (void)
1766 if (bfd_link_pic (&link_info)
1767 || pep_dll_enable_reloc_section
1768 || (!bfd_link_relocatable (&link_info)
1769 && pep_def_file->num_exports != 0))
1771 pep_dll_fill_sections (link_info.output_bfd, &link_info);
1772 if (command_line.out_implib_filename)
1773 pep_dll_generate_implib (pep_def_file,
1774 command_line.out_implib_filename, &link_info);
1777 if (pep_out_def_filename)
1778 pep_dll_generate_def_file (pep_out_def_filename);
1779 #endif /* DLL_SUPPORT */
1781 /* I don't know where .idata gets set as code, but it shouldn't be. */
1783 asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
1787 asec->flags &= ~SEC_CODE;
1788 asec->flags |= SEC_DATA;
1794 /* Place an orphan section.
1796 We use this to put sections in a reasonable place in the file, and
1797 to ensure that they are aligned as required.
1799 We handle grouped sections here as well. A section named .foo\$nn
1800 goes into the output section .foo. All grouped sections are sorted
1803 Grouped sections for the default sections are handled by the
1804 default linker script using wildcards, and are sorted by
1807 static lang_output_section_statement_type *
1808 gld_${EMULATION_NAME}_place_orphan (asection *s,
1809 const char *secname,
1812 const char *orig_secname = secname;
1813 char *dollar = NULL;
1814 lang_output_section_statement_type *os;
1815 lang_statement_list_type add_child;
1816 lang_output_section_statement_type *match_by_name = NULL;
1817 lang_statement_union_type **pl;
1819 /* Look through the script to see where to place this section. */
1820 if (!bfd_link_relocatable (&link_info)
1821 && (dollar = strchr (secname, '\$')) != NULL)
1823 size_t len = dollar - secname;
1824 char *newname = xmalloc (len + 1);
1825 memcpy (newname, secname, len);
1826 newname[len] = '\0';
1830 lang_list_init (&add_child);
1833 if (constraint == 0)
1834 for (os = lang_output_section_find (secname);
1836 os = next_matching_output_section_statement (os, 0))
1838 /* If we don't match an existing output section, tell
1839 lang_insert_orphan to create a new output section. */
1840 constraint = SPECIAL;
1842 if (os->bfd_section != NULL
1843 && (os->bfd_section->flags == 0
1844 || ((s->flags ^ os->bfd_section->flags)
1845 & (SEC_LOAD | SEC_ALLOC)) == 0))
1847 /* We already have an output section statement with this
1848 name, and its bfd section has compatible flags.
1849 If the section already exists but does not have any flags set,
1850 then it has been created by the linker, probably as a result of
1851 a --section-start command line switch. */
1852 lang_add_section (&add_child, s, NULL, os);
1856 /* Save unused output sections in case we can match them
1857 against orphans later. */
1858 if (os->bfd_section == NULL)
1862 /* If we didn't match an active output section, see if we matched an
1863 unused one and use that. */
1864 if (os == NULL && match_by_name)
1866 lang_add_section (&match_by_name->children, s, NULL, match_by_name);
1867 return match_by_name;
1872 static struct orphan_save hold[] =
1875 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1878 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1881 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1884 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1890 enum orphan_save_index
1898 static int orphan_init_done = 0;
1899 struct orphan_save *place;
1900 lang_output_section_statement_type *after;
1901 etree_type *address;
1905 if (!orphan_init_done)
1907 struct orphan_save *ho;
1908 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1909 if (ho->name != NULL)
1911 ho->os = lang_output_section_find (ho->name);
1912 if (ho->os != NULL && ho->os->flags == 0)
1913 ho->os->flags = ho->flags;
1915 orphan_init_done = 1;
1919 if (!bfd_link_relocatable (&link_info))
1922 while ((nexts = bfd_get_next_section_by_name (nexts->owner,
1924 if (nexts->output_section == NULL
1925 && (nexts->flags & SEC_EXCLUDE) == 0
1926 && ((nexts->flags ^ flags) & (SEC_LOAD | SEC_ALLOC)) == 0
1927 && (nexts->owner->flags & DYNAMIC) == 0
1928 && !bfd_input_just_syms (nexts->owner))
1929 flags = (((flags ^ SEC_READONLY)
1930 | (nexts->flags ^ SEC_READONLY))
1934 /* Try to put the new output section in a reasonable place based
1935 on the section name and section flags. */
1938 if ((flags & SEC_ALLOC) == 0)
1940 else if ((flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
1941 place = &hold[orphan_bss];
1942 else if ((flags & SEC_READONLY) == 0)
1943 place = &hold[orphan_data];
1944 else if ((flags & SEC_CODE) == 0)
1946 place = (!strncmp (secname, ".idata\$", 7) ? &hold[orphan_idata]
1947 : &hold[orphan_rodata]);
1950 place = &hold[orphan_text];
1955 if (place->os == NULL)
1956 place->os = lang_output_section_find (place->name);
1959 after = lang_output_section_find_by_flags (s, flags, &place->os,
1962 /* *ABS* is always the first output section statement. */
1963 after = (void *) lang_os_list.head;
1966 /* All sections in an executable must be aligned to a page boundary.
1967 In a relocatable link, just preserve the incoming alignment; the
1968 address is discarded by lang_insert_orphan in that case, anyway. */
1969 address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
1970 os = lang_insert_orphan (s, secname, constraint, after, place, address,
1972 if (bfd_link_relocatable (&link_info))
1974 os->section_alignment = exp_intop (1U << s->alignment_power);
1975 os->bfd_section->alignment_power = s->alignment_power;
1979 /* If the section name has a '\$', sort it with the other '\$'
1981 for (pl = &os->children.head; *pl != NULL; pl = &(*pl)->header.next)
1983 lang_input_section_type *ls;
1986 if ((*pl)->header.type != lang_input_section_enum)
1989 ls = &(*pl)->input_section;
1991 lname = bfd_section_name (ls->section);
1992 if (strchr (lname, '\$') != NULL
1993 && (dollar == NULL || strcmp (orig_secname, lname) < 0))
1997 if (add_child.head != NULL)
1999 *add_child.tail = *pl;
2000 *pl = add_child.head;
2007 gld_${EMULATION_NAME}_open_dynamic_archive
2008 (const char *arch ATTRIBUTE_UNUSED,
2009 search_dirs_type *search,
2010 lang_input_statement_type *entry)
2014 const char * format;
2015 bfd_boolean use_prefix;
2019 /* Preferred explicit import library for dll's. */
2020 { "lib%s.dll.a", FALSE },
2021 /* Alternate explicit import library for dll's. */
2022 { "%s.dll.a", FALSE },
2023 /* "libfoo.a" could be either an import lib or a static lib.
2024 For backwards compatibility, libfoo.a needs to precede
2025 libfoo.dll and foo.dll in the search. */
2026 { "lib%s.a", FALSE },
2027 /* The 'native' spelling of an import lib name is "foo.lib". */
2028 { "%s.lib", FALSE },
2029 /* PR 22948 - Check for an import library. */
2030 { "lib%s.lib", FALSE },
2032 /* Try "<prefix>foo.dll" (preferred dll name, if specified). */
2033 { "%s%s.dll", TRUE },
2035 /* Try "libfoo.dll" (default preferred dll name). */
2036 { "lib%s.dll", FALSE },
2037 /* Finally try 'native' dll name "foo.dll". */
2038 { "%s.dll", FALSE },
2039 /* Note: If adding more formats to this table, make sure to check to
2040 see if their length is longer than libname_fmt[0].format, and if
2041 so, update the call to xmalloc() below. */
2044 static unsigned int format_max_len = 0;
2045 const char * filename;
2051 if (! entry->flags.maybe_archive || entry->flags.full_name_provided)
2054 filename = entry->filename;
2056 if (format_max_len == 0)
2057 /* We need to allow space in the memory that we are going to allocate
2058 for the characters in the format string. Since the format array is
2059 static we only need to calculate this information once. In theory
2060 this value could also be computed statically, but this introduces
2061 the possibility for a discrepancy and hence a possible memory
2062 corruption. The lengths we compute here will be too long because
2063 they will include any formating characters (%s) in the strings, but
2064 this will not matter. */
2065 for (i = 0; libname_fmt[i].format; i++)
2066 if (format_max_len < strlen (libname_fmt[i].format))
2067 format_max_len = strlen (libname_fmt[i].format);
2069 full_string = xmalloc (strlen (search->name)
2073 + (pep_dll_search_prefix
2074 ? strlen (pep_dll_search_prefix) : 0)
2076 /* Allow for the terminating NUL and for the path
2077 separator character that is inserted between
2078 search->name and the start of the format string. */
2081 sprintf (full_string, "%s/", search->name);
2082 base_string = full_string + strlen (full_string);
2084 for (i = 0; libname_fmt[i].format; i++)
2087 if (libname_fmt[i].use_prefix)
2089 if (!pep_dll_search_prefix)
2091 sprintf (base_string, libname_fmt[i].format, pep_dll_search_prefix, filename);
2095 sprintf (base_string, libname_fmt[i].format, filename);
2097 if (ldfile_try_open_bfd (full_string, entry))
2101 if (!libname_fmt[i].format)
2107 entry->filename = full_string;
2113 gld_${EMULATION_NAME}_find_potential_libraries
2114 (char *name, lang_input_statement_type *entry)
2116 return ldfile_open_file_search (name, entry, "", ".lib");
2120 gld_${EMULATION_NAME}_get_script (int *isfile)
2122 # Scripts compiled in.
2123 # sed commands to quote an ld script as a C string.
2124 sc="-f stringify.sed"
2130 if (bfd_link_relocatable (&link_info) && config.build_constructors)
2133 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
2134 echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
2135 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
2136 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
2137 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
2138 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
2139 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
2140 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
2141 echo ' ; else if (link_info.pei386_auto_import == 1 && link_info.pei386_runtime_pseudo_reloc != 2) return' >> e${EMULATION_NAME}.c
2142 sed $sc ldscripts/${EMULATION_NAME}.xa >> e${EMULATION_NAME}.c
2144 echo ' ; else return' >> e${EMULATION_NAME}.c
2145 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
2146 echo '; }' >> e${EMULATION_NAME}.c
2151 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
2153 gld_${EMULATION_NAME}_before_parse,
2156 gld_${EMULATION_NAME}_after_parse,
2157 gld_${EMULATION_NAME}_after_open,
2158 after_check_relocs_default,
2159 before_place_orphans_default,
2160 after_allocation_default,
2161 set_output_arch_default,
2162 ldemul_default_target,
2163 gld_${EMULATION_NAME}_before_allocation,
2164 gld_${EMULATION_NAME}_get_script,
2165 "${EMULATION_NAME}",
2167 gld_${EMULATION_NAME}_finish,
2168 NULL, /* Create output section statements. */
2169 gld_${EMULATION_NAME}_open_dynamic_archive,
2170 gld_${EMULATION_NAME}_place_orphan,
2171 gld_${EMULATION_NAME}_set_symbols,
2172 NULL, /* parse_args */
2173 gld${EMULATION_NAME}_add_options,
2174 gld${EMULATION_NAME}_handle_option,
2175 gld_${EMULATION_NAME}_unrecognized_file,
2176 gld_${EMULATION_NAME}_list_options,
2177 gld_${EMULATION_NAME}_recognized_file,
2178 gld_${EMULATION_NAME}_find_potential_libraries,
2179 NULL, /* new_vers_pattern. */
2180 NULL, /* extra_map_file_text */
2181 ${LDEMUL_EMIT_CTF_EARLY-NULL},
2182 ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}