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-2014 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
55 #include "libiberty.h"
56 #include "filenames.h"
67 #include "ldbuildid.h"
68 #include "coff/internal.h"
70 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
71 header in generic PE code. */
72 #include "coff/x86_64.h"
75 /* FIXME: These are BFD internal header files, and we should not be
77 #include "../bfd/libcoff.h"
78 #include "../bfd/libpei.h"
81 #define AOUTSZ PEPAOUTSZ
82 #define PEAOUTHDR PEPAOUTHDR
86 #include "safe-ctype.h"
88 /* Permit the emulation parameters to override the default section
89 alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes
90 it seem that include/coff/internal.h should not define
91 PE_DEF_SECTION_ALIGNMENT. */
92 #if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
93 #undef PE_DEF_SECTION_ALIGNMENT
94 #define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
97 #ifdef TARGET_IS_i386pep
101 #if defined(TARGET_IS_i386pep) || ! defined(DLL_SUPPORT)
102 #define PE_DEF_SUBSYSTEM 3
103 #undef NT_EXE_IMAGE_BASE
104 #define NT_EXE_IMAGE_BASE \
105 ((bfd_vma) (${move_default_addr_high} ? 0x100400000LL \
107 #undef NT_DLL_IMAGE_BASE
108 #define NT_DLL_IMAGE_BASE \
109 ((bfd_vma) (${move_default_addr_high} ? 0x400000000LL \
111 #undef NT_DLL_AUTO_IMAGE_BASE
112 #define NT_DLL_AUTO_IMAGE_BASE \
113 ((bfd_vma) (${move_default_addr_high} ? 0x400000000LL \
115 #undef NT_DLL_AUTO_IMAGE_MASK
116 #define NT_DLL_AUTO_IMAGE_MASK \
117 ((bfd_vma) (${move_default_addr_high} ? 0x1ffff0000LL \
120 #undef NT_EXE_IMAGE_BASE
121 #define NT_EXE_IMAGE_BASE \
122 ((bfd_vma) (${move_default_addr_high} ? 0x100010000LL \
124 #undef NT_DLL_IMAGE_BASE
125 #define NT_DLL_IMAGE_BASE \
126 ((bfd_vma) (${move_default_addr_high} ? 0x110000000LL \
128 #undef NT_DLL_AUTO_IMAGE_BASE
129 #define NT_DLL_AUTO_IMAGE_BASE \
130 ((bfd_vma) (${move_default_addr_high} ? 0x120000000LL \
132 #undef NT_DLL_AUTO_IMAGE_MASK
133 #define NT_DLL_AUTO_IMAGE_MASK \
134 ((bfd_vma) (${move_default_addr_high} ? 0x0ffff0000LL \
136 #undef PE_DEF_SECTION_ALIGNMENT
137 #define PE_DEF_SUBSYSTEM 2
138 #undef PE_DEF_FILE_ALIGNMENT
139 #define PE_DEF_FILE_ALIGNMENT 0x00000200
140 #define PE_DEF_SECTION_ALIGNMENT 0x00000400
143 static struct internal_extra_pe_aouthdr pep;
145 static int pep_subsystem = ${SUBSYSTEM};
146 static flagword real_flags = IMAGE_FILE_LARGE_ADDRESS_AWARE;
147 static int support_old_code = 0;
148 static lang_assignment_statement_type *image_base_statement = 0;
149 static unsigned short pe_dll_characteristics = 0;
150 static bfd_boolean insert_timestamp = FALSE;
151 static const char *emit_build_id;
154 static int pep_enable_stdcall_fixup = 1; /* 0=disable 1=enable (default). */
155 static char * pep_out_def_filename = NULL;
156 static char * pep_implib_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_IMPLIB_FILENAME,
221 OPTION_WARN_DUPLICATE_EXPORTS,
223 OPTION_ENABLE_AUTO_IMAGE_BASE,
224 OPTION_DISABLE_AUTO_IMAGE_BASE,
225 OPTION_DLL_SEARCH_PREFIX,
226 OPTION_NO_DEFAULT_EXCLUDES,
227 OPTION_DLL_ENABLE_AUTO_IMPORT,
228 OPTION_DLL_DISABLE_AUTO_IMPORT,
229 OPTION_ENABLE_EXTRA_PE_DEBUG,
231 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC,
232 OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC,
233 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2,
234 OPTION_EXCLUDE_MODULES_FOR_IMPLIB,
235 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES,
236 OPTION_NO_LEADING_UNDERSCORE,
237 OPTION_LEADING_UNDERSCORE,
238 OPTION_ENABLE_LONG_SECTION_NAMES,
239 OPTION_DISABLE_LONG_SECTION_NAMES,
241 OPTION_FORCE_INTEGRITY,
247 OPTION_INSERT_TIMESTAMP,
248 OPTION_TERMINAL_SERVER_AWARE,
253 gld${EMULATION_NAME}_add_options
254 (int ns ATTRIBUTE_UNUSED,
255 char **shortopts ATTRIBUTE_UNUSED,
257 struct option **longopts,
258 int nrl ATTRIBUTE_UNUSED,
259 struct option **really_longopts ATTRIBUTE_UNUSED)
261 static const struct option xtra_long[] =
264 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
265 {"dll", no_argument, NULL, OPTION_DLL},
266 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
267 {"heap", required_argument, NULL, OPTION_HEAP},
268 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
269 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
270 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
271 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
272 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
273 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
274 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
275 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
276 {"stack", required_argument, NULL, OPTION_STACK},
277 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
278 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
279 {"use-nul-prefixed-import-tables", no_argument, NULL,
280 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
281 {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
282 {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE},
284 /* getopt allows abbreviations, so we do this to stop it
285 from treating -o as an abbreviation for this option. */
286 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
287 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
288 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
289 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
290 {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS},
291 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
292 {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
293 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
294 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
295 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
296 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
297 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
298 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
299 /* getopt() allows abbreviations, so we do this to stop it from
300 treating -c as an abbreviation for these --compat-implib. */
301 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
302 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
303 {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
304 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
305 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
306 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
307 {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
308 {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
309 {"enable-extra-pep-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
310 {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
311 {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
312 {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2},
314 {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
315 {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
316 {"dynamicbase",no_argument, NULL, OPTION_DYNAMIC_BASE},
317 {"forceinteg", no_argument, NULL, OPTION_FORCE_INTEGRITY},
318 {"nxcompat", no_argument, NULL, OPTION_NX_COMPAT},
319 {"no-isolation", no_argument, NULL, OPTION_NO_ISOLATION},
320 {"no-seh", no_argument, NULL, OPTION_NO_SEH},
321 {"no-bind", no_argument, NULL, OPTION_NO_BIND},
322 {"wdmdriver", no_argument, NULL, OPTION_WDM_DRIVER},
323 {"tsaware", no_argument, NULL, OPTION_TERMINAL_SERVER_AWARE},
324 {"insert-timestamp", no_argument, NULL, OPTION_INSERT_TIMESTAMP},
325 {"build-id", optional_argument, NULL, OPTION_BUILD_ID},
326 {NULL, no_argument, NULL, 0}
330 = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
331 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
334 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
335 parameters which may be input from the command line. */
344 /* FALSE for an assembly level symbol and TRUE for a C visible symbol.
345 C visible symbols can be prefixed by underscore dependent on target's
347 bfd_boolean is_c_symbol;
350 #define GET_INIT_SYMBOL_NAME(IDX) \
351 (init[(IDX)].symbol \
352 + ((init[(IDX)].is_c_symbol == FALSE || (is_underscoring () == 1)) ? 0 : 1))
354 /* Decorates the C visible symbol by underscore, if target requires. */
356 ((is_underscoring () == 0) ? CSTR : "_" CSTR)
358 /* Get size of constant string for a possible underscore prefixed
360 #define U_SIZE(CSTR) \
361 (sizeof (CSTR) + (is_underscoring () == 0 ? 0 : 1))
363 #define D(field,symbol,def,usc) {&pep.field, sizeof (pep.field), def, symbol, 0, usc}
365 static definfo init[] =
367 /* imagebase must be first */
368 #define IMAGEBASEOFF 0
369 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE, FALSE),
371 {&dll, sizeof(dll), 0, "__dll__", 0, FALSE},
372 #define MSIMAGEBASEOFF 2
373 D(ImageBase, "___ImageBase", NT_EXE_IMAGE_BASE, TRUE),
374 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT, FALSE),
375 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT, FALSE),
376 D(MajorOperatingSystemVersion,"__major_os_version__", 4, FALSE),
377 D(MinorOperatingSystemVersion,"__minor_os_version__", 0, FALSE),
378 D(MajorImageVersion,"__major_image_version__", 0, FALSE),
379 D(MinorImageVersion,"__minor_image_version__", 0, FALSE),
380 D(MajorSubsystemVersion,"__major_subsystem_version__", 5, FALSE),
381 D(MinorSubsystemVersion,"__minor_subsystem_version__", 2, FALSE),
382 D(Subsystem,"__subsystem__", ${SUBSYSTEM}, FALSE),
383 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000, FALSE),
384 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000, FALSE),
385 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000, FALSE),
386 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000, FALSE),
387 D(LoaderFlags,"__loader_flags__", 0x0, FALSE),
388 D(DllCharacteristics, "__dll_characteristics__", 0x0, FALSE),
389 { NULL, 0, 0, NULL, 0, FALSE}
394 gld_${EMULATION_NAME}_list_options (FILE *file)
396 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
397 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
398 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
399 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
400 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
401 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
402 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
403 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
404 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
405 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
406 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
407 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
408 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
409 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
410 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
411 fprintf (file, _(" --[no-]leading-underscore Set explicit symbol underscore prefix mode\n"));
412 fprintf (file, _(" --insert-timestamp Use a real timestamp rather than zero.\n"));
413 fprintf (file, _(" This makes binaries non-deterministic\n"));
415 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
416 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
417 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
418 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
419 fprintf (file, _(" --exclude-all-symbols Exclude all symbols from automatic export\n"));
420 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
421 fprintf (file, _(" --exclude-modules-for-implib mod,mod,...\n"));
422 fprintf (file, _(" Exclude objects, archive members from auto\n"));
423 fprintf (file, _(" export, place into import library instead.\n"));
424 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
425 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
426 fprintf (file, _(" --out-implib <file> Generate import library\n"));
427 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
428 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
429 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
430 create __imp_<SYMBOL> as well.\n"));
431 fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n\
432 unless user specifies one\n"));
433 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
434 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
435 an importlib, use <string><basename>.dll\n\
436 in preference to lib<basename>.dll \n"));
437 fprintf (file, _(" --enable-auto-import Do sophisticated linking of _sym to\n\
438 __imp_sym for DATA references\n"));
439 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
440 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
441 adding pseudo-relocations resolved at\n\
443 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
444 auto-imported DATA.\n"));
445 fprintf (file, _(" --enable-extra-pep-debug Enable verbose debug output when building\n\
446 or linking to DLLs (esp. auto-import)\n"));
447 fprintf (file, _(" --enable-long-section-names Use long COFF section names even in\n\
448 executable image files\n"));
449 fprintf (file, _(" --disable-long-section-names Never use long COFF section names, even\n\
450 in object files\n"));
451 fprintf (file, _(" --dynamicbase Image base address may be relocated using\n\
452 address space layout randomization (ASLR)\n"));
453 fprintf (file, _(" --forceinteg Code integrity checks are enforced\n"));
454 fprintf (file, _(" --nxcompat Image is compatible with data execution prevention\n"));
455 fprintf (file, _(" --no-isolation Image understands isolation but do not isolate the image\n"));
456 fprintf (file, _(" --no-seh Image does not use SEH. No SE handler may\n\
457 be called in this image\n"));
458 fprintf (file, _(" --no-bind Do not bind this image\n"));
459 fprintf (file, _(" --wdmdriver Driver uses the WDM model\n"));
460 fprintf (file, _(" --tsaware Image is Terminal Server aware\n"));
461 fprintf (file, _(" --build-id[=STYLE] Generate build ID\n"));
467 set_pep_name (char *name, bfd_vma val)
471 /* Find the name and set it. */
472 for (i = 0; init[i].ptr; i++)
474 if (strcmp (name, GET_INIT_SYMBOL_NAME (i)) == 0)
478 if (strcmp (name,"__image_base__") == 0)
479 set_pep_name (U ("__ImageBase"), val);
487 set_entry_point (void)
490 const char *initial_symbol_char;
500 { 1, "NtProcessStartup" },
501 { 2, "WinMainCRTStartup" },
502 { 3, "mainCRTStartup" },
503 { 7, "__PosixProcessStartup" },
504 { 9, "WinMainCRTStartup" },
505 {14, "mainCRTStartup" },
509 /* Entry point name for arbitrary subsystem numbers. */
510 static const char default_entry[] = "mainCRTStartup";
512 if (link_info.shared || dll)
514 entry = "DllMainCRTStartup";
518 for (i = 0; v[i].entry; i++)
519 if (v[i].value == pep_subsystem)
522 /* If no match, use the default. */
523 if (v[i].entry != NULL)
526 entry = default_entry;
529 /* Now we check target's default for getting proper symbol_char. */
530 initial_symbol_char = (is_underscoring () != 0 ? "_" : "");
532 if (*initial_symbol_char != '\0')
536 /* lang_default_entry expects its argument to be permanently
537 allocated, so we don't free this string. */
538 alc_entry = xmalloc (strlen (initial_symbol_char)
541 strcpy (alc_entry, initial_symbol_char);
542 strcat (alc_entry, entry);
546 lang_default_entry (entry);
550 set_pep_subsystem (void)
556 unsigned long temp_subsystem;
573 /* Check for the presence of a version number. */
574 sver = strchr (optarg, ':');
576 len = strlen (optarg);
580 set_pep_name ("__major_subsystem_version__",
581 strtoul (sver + 1, &end, 0));
583 set_pep_name ("__minor_subsystem_version__",
584 strtoul (end + 1, &end, 0));
586 einfo (_("%P: warning: bad version number in -subsystem option\n"));
589 /* Check for numeric subsystem. */
590 temp_subsystem = strtoul (optarg, & end, 0);
591 if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
593 /* Search list for a numeric match to use its entry point. */
594 for (i = 0; v[i].name; i++)
595 if (v[i].value == (int) temp_subsystem)
598 /* Use this subsystem. */
599 pep_subsystem = (int) temp_subsystem;
603 /* Search for subsystem by name. */
604 for (i = 0; v[i].name; i++)
605 if (strncmp (optarg, v[i].name, len) == 0
606 && v[i].name[len] == '\0')
609 if (v[i].name == NULL)
611 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
615 pep_subsystem = v[i].value;
618 set_pep_name ("__subsystem__", pep_subsystem);
625 set_pep_value (char *name)
629 set_pep_name (name, (bfd_vma) strtoull (optarg, &end, 0));
632 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
639 set_pep_stack_heap (char *resname, char *comname)
641 set_pep_value (resname);
646 set_pep_value (comname);
649 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
652 #define DEFAULT_BUILD_ID_STYLE "md5"
655 gld${EMULATION_NAME}_handle_option (int optc)
663 case OPTION_BASE_FILE:
664 link_info.base_file = fopen (optarg, FOPEN_WB);
665 if (link_info.base_file == NULL)
666 einfo (_("%F%P: cannot open base file %s\n"), optarg);
671 set_pep_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
674 set_pep_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
676 case OPTION_SUBSYSTEM:
677 set_pep_subsystem ();
679 case OPTION_MAJOR_OS_VERSION:
680 set_pep_value ("__major_os_version__");
682 case OPTION_MINOR_OS_VERSION:
683 set_pep_value ("__minor_os_version__");
685 case OPTION_MAJOR_SUBSYSTEM_VERSION:
686 set_pep_value ("__major_subsystem_version__");
688 case OPTION_MINOR_SUBSYSTEM_VERSION:
689 set_pep_value ("__minor_subsystem_version__");
691 case OPTION_MAJOR_IMAGE_VERSION:
692 set_pep_value ("__major_image_version__");
694 case OPTION_MINOR_IMAGE_VERSION:
695 set_pep_value ("__minor_image_version__");
697 case OPTION_FILE_ALIGNMENT:
698 set_pep_value ("__file_alignment__");
700 case OPTION_SECTION_ALIGNMENT:
701 set_pep_value ("__section_alignment__");
704 set_pep_name ("__dll__", 1);
706 case OPTION_IMAGE_BASE:
707 set_pep_value ("__image_base__");
709 case OPTION_SUPPORT_OLD_CODE:
710 support_old_code = 1;
712 case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
713 pep_use_nul_prefixed_import_tables = TRUE;
715 case OPTION_NO_LEADING_UNDERSCORE:
716 pep_leading_underscore = 0;
718 case OPTION_LEADING_UNDERSCORE:
719 pep_leading_underscore = 1;
721 case OPTION_INSERT_TIMESTAMP:
722 insert_timestamp = TRUE;
726 pep_out_def_filename = xstrdup (optarg);
728 case OPTION_EXPORT_ALL:
729 pep_dll_export_everything = 1;
731 case OPTION_EXCLUDE_SYMBOLS:
732 pep_dll_add_excludes (optarg, EXCLUDESYMS);
734 case OPTION_EXCLUDE_ALL_SYMBOLS:
735 pep_dll_exclude_all_symbols = 1;
737 case OPTION_EXCLUDE_LIBS:
738 pep_dll_add_excludes (optarg, EXCLUDELIBS);
740 case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
741 pep_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
743 case OPTION_KILL_ATS:
744 pep_dll_kill_ats = 1;
746 case OPTION_STDCALL_ALIASES:
747 pep_dll_stdcall_aliases = 1;
749 case OPTION_ENABLE_STDCALL_FIXUP:
750 pep_enable_stdcall_fixup = 1;
752 case OPTION_DISABLE_STDCALL_FIXUP:
753 pep_enable_stdcall_fixup = 0;
755 case OPTION_IMPLIB_FILENAME:
756 pep_implib_filename = xstrdup (optarg);
758 case OPTION_WARN_DUPLICATE_EXPORTS:
759 pep_dll_warn_dup_exports = 1;
761 case OPTION_IMP_COMPAT:
762 pep_dll_compat_implib = 1;
764 case OPTION_ENABLE_AUTO_IMAGE_BASE:
765 pep_enable_auto_image_base = 1;
767 case OPTION_DISABLE_AUTO_IMAGE_BASE:
768 pep_enable_auto_image_base = 0;
770 case OPTION_DLL_SEARCH_PREFIX:
771 pep_dll_search_prefix = xstrdup (optarg);
773 case OPTION_NO_DEFAULT_EXCLUDES:
774 pep_dll_do_default_excludes = 0;
776 case OPTION_DLL_ENABLE_AUTO_IMPORT:
777 link_info.pei386_auto_import = 1;
779 case OPTION_DLL_DISABLE_AUTO_IMPORT:
780 link_info.pei386_auto_import = 0;
782 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
783 link_info.pei386_runtime_pseudo_reloc = 2;
785 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
786 link_info.pei386_runtime_pseudo_reloc = 0;
788 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2:
789 link_info.pei386_runtime_pseudo_reloc = 2;
791 case OPTION_ENABLE_EXTRA_PE_DEBUG:
792 pep_dll_extra_pe_debug = 1;
795 case OPTION_ENABLE_LONG_SECTION_NAMES:
796 pep_use_coff_long_section_names = 1;
798 case OPTION_DISABLE_LONG_SECTION_NAMES:
799 pep_use_coff_long_section_names = 0;
801 /* Get DLLCharacteristics bits */
802 case OPTION_DYNAMIC_BASE:
803 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
805 case OPTION_FORCE_INTEGRITY:
806 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
808 case OPTION_NX_COMPAT:
809 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
811 case OPTION_NO_ISOLATION:
812 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
815 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_SEH;
818 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_BIND;
820 case OPTION_WDM_DRIVER:
821 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
823 case OPTION_TERMINAL_SERVER_AWARE:
824 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
826 case OPTION_BUILD_ID:
827 if (emit_build_id != NULL)
829 free ((char *) emit_build_id);
830 emit_build_id = NULL;
833 optarg = DEFAULT_BUILD_ID_STYLE;
834 if (strcmp (optarg, "none"))
835 emit_build_id = xstrdup (optarg);
839 /* Set DLLCharacteristics bits */
840 set_pep_name ("__dll_characteristics__", pe_dll_characteristics);
848 strhash (const char *str)
850 const unsigned char *s;
857 s = (const unsigned char *) str;
858 while ((c = *s++) != '\0')
860 hash += c + (c << 17);
864 hash += len + (len << 17);
870 /* Use the output file to create a image base for relocatable DLLs. */
873 compute_dll_image_base (const char *ofile)
875 bfd_vma hash = (bfd_vma) strhash (ofile);
876 return NT_DLL_AUTO_IMAGE_BASE + ((hash << 16) & NT_DLL_AUTO_IMAGE_MASK);
880 /* Assign values to the special symbols before the linker script is
884 gld_${EMULATION_NAME}_set_symbols (void)
886 /* Run through and invent symbols for all the
887 names and insert the defaults. */
892 if (!init[IMAGEBASEOFF].inited)
894 if (link_info.relocatable)
895 init[IMAGEBASEOFF].value = 0;
896 else if (init[DLLOFF].value || (link_info.shared && !link_info.pie))
899 init[IMAGEBASEOFF].value = (pep_enable_auto_image_base
900 ? compute_dll_image_base (output_filename)
901 : NT_DLL_IMAGE_BASE);
903 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
907 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
908 init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
911 /* Don't do any symbol assignments if this is a relocatable link. */
912 if (link_info.relocatable)
915 /* Glue the assignments into the abs section. */
916 push_stat_ptr (&abs_output_section->children);
918 for (j = 0; init[j].ptr; j++)
920 bfd_vma val = init[j].value;
921 lang_assignment_statement_type *rv;
923 rv = lang_add_assignment (exp_assign (GET_INIT_SYMBOL_NAME (j),
924 exp_intop (val), FALSE));
925 if (init[j].size == sizeof (short))
926 *(short *) init[j].ptr = (short) val;
927 else if (init[j].size == sizeof (int))
928 *(int *) init[j].ptr = (int) val;
929 else if (init[j].size == sizeof (long))
930 *(long *) init[j].ptr = (long) val;
931 /* This might be a long long or other special type. */
932 else if (init[j].size == sizeof (bfd_vma))
933 *(bfd_vma *) init[j].ptr = val;
935 if (j == IMAGEBASEOFF)
936 image_base_statement = rv;
938 /* Restore the pointer. */
941 if (pep.FileAlignment > pep.SectionAlignment)
943 einfo (_("%P: warning, file alignment > section alignment.\n"));
947 /* This is called after the linker script and the command line options
951 gld_${EMULATION_NAME}_after_parse (void)
953 /* PR ld/6744: Warn the user if they have used an ELF-only
954 option hoping it will work on PE+. */
955 if (link_info.export_dynamic)
956 einfo (_("%P: warning: --export-dynamic is not supported for PE+ "
957 "targets, did you mean --export-all-symbols?\n"));
961 after_parse_default ();
964 /* pep-dll.c directly accesses pep_data_import_dll,
965 so it must be defined outside of #ifdef DLL_SUPPORT.
966 Note - this variable is deliberately not initialised.
967 This allows it to be treated as a common varaible, and only
968 exist in one incarnation in a multiple target enabled linker. */
969 char * pep_data_import_dll;
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)
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_vma) ((bfd_signed_vma) ((char) bfd_get_8 (s->owner, addend)));
1092 _addend = ((bfd_vma) bfd_get_8 (s->owner, addend)) & 0xff;
1095 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 2);
1096 if (suc && rel->howto->pc_relative)
1097 _addend = (bfd_vma) ((bfd_signed_vma) ((short) bfd_get_16 (s->owner, addend)));
1099 _addend = ((bfd_vma) bfd_get_16 (s->owner, addend)) & 0xffff;
1102 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 4);
1103 if (suc && rel->howto->pc_relative)
1104 _addend = (bfd_vma) ((bfd_signed_vma) ((int) bfd_get_32 (s->owner, addend)));
1106 _addend = ((bfd_vma) bfd_get_32 (s->owner, addend)) & 0xffffffff;
1109 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 8);
1111 _addend = ((bfd_vma) bfd_get_64 (s->owner, addend));
1115 einfo (_("%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", (long) _addend, (long) rel->addend, (long) rel->address);
1121 if (rel->howto->pc_relative) printf (" pcrel");
1122 printf (" %d bit rel.\n",(int) rel->howto->bitsize);
1124 pep_create_import_fixup (rel, s, _addend);
1130 pep_find_data_imports (void)
1132 struct bfd_link_hash_entry *undef, *sym;
1134 if (link_info.pei386_auto_import == 0)
1137 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
1139 if (undef->type == bfd_link_hash_undefined)
1141 /* C++ symbols are *long*. */
1144 if (pep_dll_extra_pe_debug)
1145 printf ("%s:%s\n", __FUNCTION__, undef->root.string);
1147 sprintf (buf, "__imp_%s", undef->root.string);
1149 sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
1151 if (sym && sym->type == bfd_link_hash_defined)
1153 bfd *b = sym->u.def.section->owner;
1157 if (!bfd_generic_link_read_symbols (b))
1159 einfo (_("%B%F: could not read symbols: %E\n"), b);
1163 symbols = bfd_get_outsymbols (b);
1164 nsyms = bfd_get_symcount (b);
1166 for (i = 0; i < nsyms; i++)
1168 if (! CONST_STRNEQ (symbols[i]->name, U ("_head_")))
1171 if (pep_dll_extra_pe_debug)
1172 printf ("->%s\n", symbols[i]->name);
1174 pep_data_import_dll = (char*) (symbols[i]->name +
1175 U_SIZE ("_head_") - 1);
1179 pep_walk_relocs_of_symbol (&link_info, undef->root.string,
1182 /* Let's differentiate it somehow from defined. */
1183 undef->type = bfd_link_hash_defweak;
1184 /* We replace original name with __imp_ prefixed, this
1185 1) may trash memory 2) leads to duplicate symbol generation.
1186 Still, IMHO it's better than having name poluted. */
1187 undef->root.string = sym->root.string;
1188 undef->u.def.value = sym->u.def.value;
1189 undef->u.def.section = sym->u.def.section;
1196 pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
1198 printf ("+%s\n", h->string);
1202 #endif /* DLL_SUPPORT */
1205 debug_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
1207 int *found = (int *) obj;
1209 if (strncmp (".debug_", sect->name, sizeof (".debug_") - 1) == 0)
1214 pecoff_checksum_contents (bfd *abfd,
1215 void (*process) (const void *, size_t, void *),
1218 file_ptr filepos = (file_ptr) 0;
1225 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
1228 status = bfd_bread (&b, (bfd_size_type) 1, abfd);
1234 (*process) (&b, 1, arg);
1242 write_build_id (bfd *abfd)
1244 struct pe_tdata *t = pe_data (abfd);
1246 struct bfd_link_order *link_order = NULL;
1247 unsigned char *contents;
1249 bfd_size_type build_id_size;
1250 unsigned char *build_id;
1252 /* Find the section the .build-id output section has been merged info. */
1253 for (asec = abfd->sections; asec != NULL; asec = asec->next)
1255 struct bfd_link_order *l = NULL;
1256 for (l = asec->map_head.link_order; l != NULL; l = l->next)
1258 if ((l->type == bfd_indirect_link_order))
1260 if (l->u.indirect.section == t->build_id.sec)
1274 einfo (_("%P: warning: .build-id section discarded,"
1275 " --build-id ignored.\n"));
1279 if (t->build_id.sec->contents == NULL)
1280 t->build_id.sec->contents = (unsigned char *) xmalloc (t->build_id.sec->size);
1281 contents = t->build_id.sec->contents;
1282 size = t->build_id.sec->size;
1284 build_id_size = compute_build_id_size (t->build_id.style);
1285 build_id = xmalloc (build_id_size);
1286 generate_build_id (abfd, t->build_id.style, pecoff_checksum_contents, build_id, build_id_size);
1288 bfd_vma ib = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase;
1290 /* Construct a debug directory entry which points to an immediately following CodeView record. */
1291 struct internal_IMAGE_DEBUG_DIRECTORY idd;
1292 idd.Characteristics = 0;
1293 idd.TimeDateStamp = 0;
1294 idd.MajorVersion = 0;
1295 idd.MinorVersion = 0;
1296 idd.Type = PE_IMAGE_DEBUG_TYPE_CODEVIEW;
1297 idd.SizeOfData = sizeof (CV_INFO_PDB70) + 1;
1298 idd.AddressOfRawData = asec->vma - ib + link_order->offset
1299 + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1300 idd.PointerToRawData = asec->filepos + link_order->offset
1301 + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1303 struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *)contents;
1304 _bfd_XXi_swap_debugdir_out (abfd, &idd, ext);
1306 /* Write the debug directory enttry */
1307 if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0)
1310 if ((bfd_bwrite (contents, size, abfd) != size))
1313 /* Construct the CodeView record. */
1314 CODEVIEW_INFO cvinfo;
1315 cvinfo.CVSignature = CVINFO_PDB70_CVSIGNATURE;
1318 /* Zero pad or truncate the generated build_id to fit in the CodeView record. */
1319 memset (&(cvinfo.Signature), 0, CV_INFO_SIGNATURE_LENGTH);
1320 memcpy (&(cvinfo.Signature), build_id, (build_id_size > CV_INFO_SIGNATURE_LENGTH)
1321 ? CV_INFO_SIGNATURE_LENGTH : build_id_size);
1325 /* Write the codeview record. */
1326 if (_bfd_XXi_write_codeview_record (abfd, idd.PointerToRawData, &cvinfo) == 0)
1329 /* Record the location of the debug directory in the data directory. */
1330 pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress
1331 = asec->vma - ib + link_order->offset;
1332 pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
1333 = sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1338 /* Make .build-id section, and set up coff_tdata->build_id. */
1340 setup_build_id (bfd *ibfd)
1345 if (!validate_build_id_style (emit_build_id))
1347 einfo ("%P: warning: unrecognized --build-id style ignored.\n");
1351 flags = (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY
1352 | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA);
1353 s = bfd_make_section_anyway_with_flags (ibfd, ".build-id", flags);
1356 struct pe_tdata *t = pe_data (link_info.output_bfd);
1357 t->build_id.after_write_object_contents = &write_build_id;
1358 t->build_id.style = emit_build_id;
1359 t->build_id.sec = s;
1361 /* Section is a fixed size:
1362 One IMAGE_DEBUG_DIRECTORY entry, of type IMAGE_DEBUG_TYPE_CODEVIEW,
1363 pointing at a CV_INFO_PDB70 record containing the build-id, with a
1364 null byte for PdbFileName. */
1365 s->size = sizeof (struct external_IMAGE_DEBUG_DIRECTORY)
1366 + sizeof (CV_INFO_PDB70) + 1;
1371 einfo ("%P: warning: Cannot create .build-id section,"
1372 " --build-id ignored.\n");
1377 gld_${EMULATION_NAME}_after_open (void)
1379 after_open_default ();
1383 if (pep_dll_extra_pe_debug)
1386 struct bfd_link_hash_entry *sym;
1388 printf ("%s()\n", __FUNCTION__);
1390 for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1391 printf ("-%s\n", sym->root.string);
1392 bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
1394 for (a = link_info.input_bfds; a; a = a->link_next)
1395 printf ("*%s\n",a->filename);
1399 if (emit_build_id != NULL)
1403 /* Find a COFF input. */
1404 for (abfd = link_info.input_bfds;
1405 abfd != (bfd *) NULL; abfd = abfd->link_next)
1406 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
1409 /* If there are no COFF input files do not try to
1410 add a build-id section. */
1412 || !setup_build_id (abfd))
1414 free ((char *) emit_build_id);
1415 emit_build_id = NULL;
1419 /* Pass the wacky PE command line options into the output bfd.
1420 FIXME: This should be done via a function, rather than by
1421 including an internal BFD header. */
1423 if (coff_data (link_info.output_bfd) == NULL
1424 || coff_data (link_info.output_bfd)->pe == 0)
1425 einfo (_("%F%P: cannot perform PE operations on non PE output file '%B'.\n"),
1426 link_info.output_bfd);
1428 pe_data (link_info.output_bfd)->pe_opthdr = pep;
1429 pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
1430 pe_data (link_info.output_bfd)->real_flags |= real_flags;
1431 pe_data (link_info.output_bfd)->insert_timestamp = insert_timestamp;
1433 /* At this point we must decide whether to use long section names
1434 in the output or not. If the user hasn't explicitly specified
1435 on the command line, we leave it to the default for the format
1436 (object files yes, image files no), except if there is debug
1437 information present; GDB relies on the long section names to
1438 find it, so enable it in that case. */
1439 if (pep_use_coff_long_section_names < 0 && link_info.strip == strip_none)
1441 if (link_info.relocatable)
1442 pep_use_coff_long_section_names = 1;
1445 /* Iterate over all sections of all input BFDs, checking
1446 for any that begin 'debug_' and are long names. */
1447 LANG_FOR_EACH_INPUT_STATEMENT (is)
1449 int found_debug = 0;
1451 bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug);
1454 pep_use_coff_long_section_names = 1;
1461 pep_output_file_set_long_section_names (link_info.output_bfd);
1464 if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
1465 pep_fixup_stdcalls ();
1467 pep_process_import_defs (link_info.output_bfd, &link_info);
1469 pep_find_data_imports ();
1471 /* As possibly new symbols are added by imports, we rerun
1472 stdcall/fastcall fixup here. */
1473 if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
1474 pep_fixup_stdcalls ();
1476 #ifndef TARGET_IS_i386pep
1477 if (link_info.shared)
1479 if (!link_info.relocatable)
1481 pep_dll_build_sections (link_info.output_bfd, &link_info);
1483 #ifndef TARGET_IS_i386pep
1485 pep_exe_build_sections (link_info.output_bfd, &link_info);
1487 #endif /* DLL_SUPPORT */
1490 /* This next chunk of code tries to detect the case where you have
1491 two import libraries for the same DLL (specifically,
1492 symbolically linking libm.a and libc.a in cygwin to
1493 libcygwin.a). In those cases, it's possible for function
1494 thunks from the second implib to be used but without the
1495 head/tail objects, causing an improper import table. We detect
1496 those cases and rename the "other" import libraries to match
1497 the one the head/tail come from, so that the linker will sort
1498 things nicely and produce a valid import table. */
1500 LANG_FOR_EACH_INPUT_STATEMENT (is)
1502 if (is->the_bfd->my_archive)
1504 int idata2 = 0, reloc_count=0, is_imp = 0;
1507 /* See if this is an import library thunk. */
1508 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1510 if (strcmp (sec->name, ".idata\$2") == 0)
1512 if (CONST_STRNEQ (sec->name, ".idata\$"))
1514 reloc_count += sec->reloc_count;
1517 if (is_imp && !idata2 && reloc_count)
1519 /* It is, look for the reference to head and see if it's
1520 from our own library. */
1521 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1529 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1533 if (!bfd_generic_link_read_symbols (is->the_bfd))
1535 einfo (_("%B%F: could not read symbols: %E\n"),
1539 symbols = bfd_get_outsymbols (is->the_bfd);
1541 relocs = xmalloc ((size_t) relsize);
1542 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1547 einfo ("%X%P: unable to process relocs: %E\n");
1551 for (i = 0; i < nrelocs; i++)
1553 struct bfd_symbol *s;
1554 struct bfd_link_hash_entry * blhe;
1555 char *other_bfd_filename;
1558 s = (relocs[i]->sym_ptr_ptr)[0];
1560 if (s->flags & BSF_LOCAL)
1563 /* Thunk section with reloc to another bfd. */
1564 blhe = bfd_link_hash_lookup (link_info.hash,
1566 FALSE, FALSE, TRUE);
1569 || blhe->type != bfd_link_hash_defined)
1573 = blhe->u.def.section->owner->my_archive
1574 ? bfd_get_filename (blhe->u.def.section->owner->my_archive)
1575 : bfd_get_filename (blhe->u.def.section->owner);
1577 if (filename_cmp (bfd_get_filename
1578 (is->the_bfd->my_archive),
1579 other_bfd_filename) == 0)
1582 /* Rename this implib to match the other one. */
1583 n = xmalloc (strlen (other_bfd_filename) + 1);
1584 strcpy (n, other_bfd_filename);
1585 is->the_bfd->my_archive->filename = n;
1589 /* Note - we do not free the symbols,
1590 they are now cached in the BFD. */
1600 lang_input_statement_type *is2;
1601 lang_input_statement_type *is3;
1603 /* Careful - this is a shell script. Watch those dollar signs! */
1604 /* Microsoft import libraries have every member named the same,
1605 and not in the right order for us to link them correctly. We
1606 must detect these and rename the members so that they'll link
1607 correctly. There are three types of objects: the head, the
1608 thunks, and the sentinel(s). The head is easy; it's the one
1609 with idata2. We assume that the sentinels won't have relocs,
1610 and the thunks will. It's easier than checking the symbol
1611 table for external references. */
1612 LANG_FOR_EACH_INPUT_STATEMENT (is)
1614 if (is->the_bfd->my_archive)
1617 bfd *arch = is->the_bfd->my_archive;
1619 if (cur_arch != arch)
1625 is3 && is3->the_bfd->my_archive == arch;
1626 is3 = (lang_input_statement_type *) is3->next)
1628 /* A MS dynamic import library can also contain static
1629 members, so look for the first element with a .dll
1630 extension, and use that for the remainder of the
1632 pnt = strrchr (is3->the_bfd->filename, '.');
1633 if (pnt != NULL && filename_cmp (pnt, ".dll") == 0)
1641 /* OK, found one. Now look to see if the remaining
1642 (dynamic import) members use the same name. */
1644 is2 && is2->the_bfd->my_archive == arch;
1645 is2 = (lang_input_statement_type *) is2->next)
1647 /* Skip static members, ie anything with a .obj
1649 pnt = strrchr (is2->the_bfd->filename, '.');
1650 if (pnt != NULL && filename_cmp (pnt, ".obj") == 0)
1653 if (filename_cmp (is3->the_bfd->filename,
1654 is2->the_bfd->filename))
1663 /* This fragment might have come from an .obj file in a Microsoft
1664 import, and not an actual import record. If this is the case,
1665 then leave the filename alone. */
1666 pnt = strrchr (is->the_bfd->filename, '.');
1668 if (is_ms_arch && (filename_cmp (pnt, ".dll") == 0))
1670 int idata2 = 0, reloc_count=0;
1672 char *new_name, seq;
1674 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1676 if (strcmp (sec->name, ".idata\$2") == 0)
1678 reloc_count += sec->reloc_count;
1681 if (idata2) /* .idata2 is the TOC */
1683 else if (reloc_count > 0) /* thunks */
1688 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
1689 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
1690 is->the_bfd->filename = new_name;
1692 new_name = xmalloc (strlen (is->filename) + 3);
1693 sprintf (new_name, "%s.%c", is->filename, seq);
1694 is->filename = new_name;
1702 gld_${EMULATION_NAME}_before_allocation (void)
1705 before_allocation_default ();
1709 /* This is called when an input file isn't recognized as a BFD. We
1710 check here for .DEF files and pull them in automatically. */
1713 saw_option (char *option)
1719 for (i = 0; init[i].ptr; i++)
1720 if (strcmp (GET_INIT_SYMBOL_NAME (i), option) == 0)
1721 return init[i].inited;
1724 #endif /* DLL_SUPPORT */
1727 gld_${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1730 const char *ext = entry->filename + strlen (entry->filename) - 4;
1732 if (filename_cmp (ext, ".def") == 0 || filename_cmp (ext, ".DEF") == 0)
1734 pep_def_file = def_file_parse (entry->filename, pep_def_file);
1738 int i, buflen=0, len;
1741 for (i = 0; i < pep_def_file->num_exports; i++)
1743 len = strlen (pep_def_file->exports[i].internal_name);
1744 if (buflen < len + 2)
1748 buf = xmalloc (buflen);
1750 for (i = 0; i < pep_def_file->num_exports; i++)
1752 struct bfd_link_hash_entry *h;
1754 sprintf (buf, "%s%s", U (""),
1755 pep_def_file->exports[i].internal_name);
1757 h = bfd_link_hash_lookup (link_info.hash, buf, TRUE, TRUE, TRUE);
1758 if (h == (struct bfd_link_hash_entry *) NULL)
1759 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1760 if (h->type == bfd_link_hash_new)
1762 h->type = bfd_link_hash_undefined;
1763 h->u.undef.abfd = NULL;
1764 bfd_link_add_undef (link_info.hash, h);
1769 /* def_file_print (stdout, pep_def_file); */
1770 if (pep_def_file->is_dll == 1)
1771 link_info.shared = 1;
1773 if (pep_def_file->base_address != (bfd_vma)(-1))
1776 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1777 = init[IMAGEBASEOFF].value
1778 = pep_def_file->base_address;
1779 init[IMAGEBASEOFF].inited = 1;
1780 if (image_base_statement)
1781 image_base_statement->exp
1782 = exp_assign ("__image_base__", exp_intop (pep.ImageBase),
1786 if (pep_def_file->stack_reserve != -1
1787 && ! saw_option ("__size_of_stack_reserve__"))
1789 pep.SizeOfStackReserve = pep_def_file->stack_reserve;
1790 if (pep_def_file->stack_commit != -1)
1791 pep.SizeOfStackCommit = pep_def_file->stack_commit;
1793 if (pep_def_file->heap_reserve != -1
1794 && ! saw_option ("__size_of_heap_reserve__"))
1796 pep.SizeOfHeapReserve = pep_def_file->heap_reserve;
1797 if (pep_def_file->heap_commit != -1)
1798 pep.SizeOfHeapCommit = pep_def_file->heap_commit;
1808 gld_${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1812 #ifdef TARGET_IS_i386pep
1813 pep_dll_id_target ("pei-x86-64");
1815 if (pep_bfd_is_dll (entry->the_bfd))
1816 return pep_implied_import_dll (entry->filename);
1822 gld_${EMULATION_NAME}_finish (void)
1828 if (link_info.shared
1829 || (!link_info.relocatable && pep_def_file->num_exports != 0))
1831 pep_dll_fill_sections (link_info.output_bfd, &link_info);
1832 if (pep_implib_filename)
1833 pep_dll_generate_implib (pep_def_file, pep_implib_filename, &link_info);
1836 if (pep_out_def_filename)
1837 pep_dll_generate_def_file (pep_out_def_filename);
1838 #endif /* DLL_SUPPORT */
1840 /* I don't know where .idata gets set as code, but it shouldn't be. */
1842 asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
1846 asec->flags &= ~SEC_CODE;
1847 asec->flags |= SEC_DATA;
1853 /* Place an orphan section.
1855 We use this to put sections in a reasonable place in the file, and
1856 to ensure that they are aligned as required.
1858 We handle grouped sections here as well. A section named .foo\$nn
1859 goes into the output section .foo. All grouped sections are sorted
1862 Grouped sections for the default sections are handled by the
1863 default linker script using wildcards, and are sorted by
1866 static lang_output_section_statement_type *
1867 gld_${EMULATION_NAME}_place_orphan (asection *s,
1868 const char *secname,
1871 const char *orig_secname = secname;
1872 char *dollar = NULL;
1873 lang_output_section_statement_type *os;
1874 lang_statement_list_type add_child;
1875 lang_output_section_statement_type *match_by_name = NULL;
1876 lang_statement_union_type **pl;
1878 /* Look through the script to see where to place this section. */
1879 if (!link_info.relocatable
1880 && (dollar = strchr (secname, '\$')) != NULL)
1882 size_t len = dollar - secname;
1883 char *newname = xmalloc (len + 1);
1884 memcpy (newname, secname, len);
1885 newname[len] = '\0';
1889 lang_list_init (&add_child);
1892 if (constraint == 0)
1893 for (os = lang_output_section_find (secname);
1895 os = next_matching_output_section_statement (os, 0))
1897 /* If we don't match an existing output section, tell
1898 lang_insert_orphan to create a new output section. */
1899 constraint = SPECIAL;
1901 if (os->bfd_section != NULL
1902 && (os->bfd_section->flags == 0
1903 || ((s->flags ^ os->bfd_section->flags)
1904 & (SEC_LOAD | SEC_ALLOC)) == 0))
1906 /* We already have an output section statement with this
1907 name, and its bfd section has compatible flags.
1908 If the section already exists but does not have any flags set,
1909 then it has been created by the linker, probably as a result of
1910 a --section-start command line switch. */
1911 lang_add_section (&add_child, s, NULL, os);
1915 /* Save unused output sections in case we can match them
1916 against orphans later. */
1917 if (os->bfd_section == NULL)
1921 /* If we didn't match an active output section, see if we matched an
1922 unused one and use that. */
1923 if (os == NULL && match_by_name)
1925 lang_add_section (&match_by_name->children, s, NULL, match_by_name);
1926 return match_by_name;
1931 static struct orphan_save hold[] =
1934 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1937 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1940 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1943 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1949 enum orphan_save_index
1957 static int orphan_init_done = 0;
1958 struct orphan_save *place;
1959 lang_output_section_statement_type *after;
1960 etree_type *address;
1962 if (!orphan_init_done)
1964 struct orphan_save *ho;
1965 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1966 if (ho->name != NULL)
1968 ho->os = lang_output_section_find (ho->name);
1969 if (ho->os != NULL && ho->os->flags == 0)
1970 ho->os->flags = ho->flags;
1972 orphan_init_done = 1;
1975 /* Try to put the new output section in a reasonable place based
1976 on the section name and section flags. */
1979 if ((s->flags & SEC_ALLOC) == 0)
1981 else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
1982 place = &hold[orphan_bss];
1983 else if ((s->flags & SEC_READONLY) == 0)
1984 place = &hold[orphan_data];
1985 else if ((s->flags & SEC_CODE) == 0)
1987 place = (!strncmp (secname, ".idata\$", 7) ? &hold[orphan_idata]
1988 : &hold[orphan_rodata]);
1991 place = &hold[orphan_text];
1996 if (place->os == NULL)
1997 place->os = lang_output_section_find (place->name);
2000 after = lang_output_section_find_by_flags (s, &place->os, NULL);
2002 /* *ABS* is always the first output section statement. */
2003 after = (&lang_output_section_statement.head
2004 ->output_section_statement);
2007 /* All sections in an executable must be aligned to a page boundary.
2008 In a relocatable link, just preserve the incoming alignment; the
2009 address is discarded by lang_insert_orphan in that case, anyway. */
2010 address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
2011 os = lang_insert_orphan (s, secname, constraint, after, place, address,
2013 if (link_info.relocatable)
2015 os->section_alignment = s->alignment_power;
2016 os->bfd_section->alignment_power = s->alignment_power;
2020 /* If the section name has a '\$', sort it with the other '\$'
2022 for (pl = &os->children.head; *pl != NULL; pl = &(*pl)->header.next)
2024 lang_input_section_type *ls;
2027 if ((*pl)->header.type != lang_input_section_enum)
2030 ls = &(*pl)->input_section;
2032 lname = bfd_get_section_name (ls->section->owner, ls->section);
2033 if (strchr (lname, '\$') != NULL
2034 && (dollar == NULL || strcmp (orig_secname, lname) < 0))
2038 if (add_child.head != NULL)
2040 *add_child.tail = *pl;
2041 *pl = add_child.head;
2048 gld_${EMULATION_NAME}_open_dynamic_archive
2049 (const char *arch ATTRIBUTE_UNUSED,
2050 search_dirs_type *search,
2051 lang_input_statement_type *entry)
2055 const char * format;
2056 bfd_boolean use_prefix;
2060 /* Preferred explicit import library for dll's. */
2061 { "lib%s.dll.a", FALSE },
2062 /* Alternate explicit import library for dll's. */
2063 { "%s.dll.a", FALSE },
2064 /* "libfoo.a" could be either an import lib or a static lib.
2065 For backwards compatibility, libfoo.a needs to precede
2066 libfoo.dll and foo.dll in the search. */
2067 { "lib%s.a", FALSE },
2068 /* The 'native' spelling of an import lib name is "foo.lib". */
2069 { "%s.lib", FALSE },
2071 /* Try "<prefix>foo.dll" (preferred dll name, if specified). */
2072 { "%s%s.dll", TRUE },
2074 /* Try "libfoo.dll" (default preferred dll name). */
2075 { "lib%s.dll", FALSE },
2076 /* Finally try 'native' dll name "foo.dll". */
2077 { "%s.dll", FALSE },
2078 /* Note: If adding more formats to this table, make sure to check to
2079 see if their length is longer than libname_fmt[0].format, and if
2080 so, update the call to xmalloc() below. */
2083 static unsigned int format_max_len = 0;
2084 const char * filename;
2090 if (! entry->flags.maybe_archive || entry->flags.full_name_provided)
2093 filename = entry->filename;
2095 if (format_max_len == 0)
2096 /* We need to allow space in the memory that we are going to allocate
2097 for the characters in the format string. Since the format array is
2098 static we only need to calculate this information once. In theory
2099 this value could also be computed statically, but this introduces
2100 the possibility for a discrepancy and hence a possible memory
2101 corruption. The lengths we compute here will be too long because
2102 they will include any formating characters (%s) in the strings, but
2103 this will not matter. */
2104 for (i = 0; libname_fmt[i].format; i++)
2105 if (format_max_len < strlen (libname_fmt[i].format))
2106 format_max_len = strlen (libname_fmt[i].format);
2108 full_string = xmalloc (strlen (search->name)
2112 + (pep_dll_search_prefix
2113 ? strlen (pep_dll_search_prefix) : 0)
2115 /* Allow for the terminating NUL and for the path
2116 separator character that is inserted between
2117 search->name and the start of the format string. */
2120 sprintf (full_string, "%s/", search->name);
2121 base_string = full_string + strlen (full_string);
2123 for (i = 0; libname_fmt[i].format; i++)
2126 if (libname_fmt[i].use_prefix)
2128 if (!pep_dll_search_prefix)
2130 sprintf (base_string, libname_fmt[i].format, pep_dll_search_prefix, filename);
2134 sprintf (base_string, libname_fmt[i].format, filename);
2136 if (ldfile_try_open_bfd (full_string, entry))
2140 if (!libname_fmt[i].format)
2146 entry->filename = full_string;
2152 gld_${EMULATION_NAME}_find_potential_libraries
2153 (char *name, lang_input_statement_type *entry)
2155 return ldfile_open_file_search (name, entry, "", ".lib");
2159 gld_${EMULATION_NAME}_get_script (int *isfile)
2161 # Scripts compiled in.
2162 # sed commands to quote an ld script as a C string.
2163 sc="-f stringify.sed"
2169 if (link_info.relocatable && config.build_constructors)
2172 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
2173 echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c
2174 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
2175 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
2176 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
2177 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
2178 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
2179 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
2180 echo ' ; else if (link_info.pei386_auto_import == 1 && link_info.pei386_runtime_pseudo_reloc != 2) return' >> e${EMULATION_NAME}.c
2181 sed $sc ldscripts/${EMULATION_NAME}.xa >> e${EMULATION_NAME}.c
2183 echo ' ; else return' >> e${EMULATION_NAME}.c
2184 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
2185 echo '; }' >> e${EMULATION_NAME}.c
2190 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
2192 gld_${EMULATION_NAME}_before_parse,
2195 gld_${EMULATION_NAME}_after_parse,
2196 gld_${EMULATION_NAME}_after_open,
2197 after_allocation_default,
2198 set_output_arch_default,
2199 ldemul_default_target,
2200 gld_${EMULATION_NAME}_before_allocation,
2201 gld_${EMULATION_NAME}_get_script,
2202 "${EMULATION_NAME}",
2204 gld_${EMULATION_NAME}_finish,
2205 NULL, /* Create output section statements. */
2206 gld_${EMULATION_NAME}_open_dynamic_archive,
2207 gld_${EMULATION_NAME}_place_orphan,
2208 gld_${EMULATION_NAME}_set_symbols,
2209 NULL, /* parse_args */
2210 gld${EMULATION_NAME}_add_options,
2211 gld${EMULATION_NAME}_handle_option,
2212 gld_${EMULATION_NAME}_unrecognized_file,
2213 gld_${EMULATION_NAME}_list_options,
2214 gld_${EMULATION_NAME}_recognized_file,
2215 gld_${EMULATION_NAME}_find_potential_libraries,
2216 NULL, /* new_vers_pattern. */
2217 NULL /* extra_map_file_text */