1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 if [ -z "$MACHINE" ]; then
6 OUTPUT_ARCH=${ARCH}:${MACHINE}
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) 1995-2024 Free Software Foundation, Inc.
23 This file is part of the GNU Binutils.
25 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 3 of the License, or
28 (at your option) any later version.
30 This program is distributed in the hope that it will be useful,
31 but WITHOUT ANY WARRANTY; without even the implied warranty of
32 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 GNU General Public License for more details.
35 You should have received a copy of the GNU General Public License
36 along with this program; if not, write to the Free Software
37 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
38 MA 02110-1301, USA. */
42 /* The original file generated returned different default scripts depending
43 on whether certain switches were set, but these switches pertain to the
44 Linux system and that particular version of coff. In the NT case, we
45 only determine if the subsystem is console or windows in order to select
46 the correct entry point by default. */
48 #define TARGET_IS_${EMULATION_NAME}
55 #include "libiberty.h"
56 #include "filenames.h"
67 #include "ldbuildid.h"
68 #include "coff/internal.h"
72 x86_64-*-mingw* | x86_64-*-pe | x86_64-*-pep | x86_64-*-cygwin | \
73 i[3-7]86-*-mingw32* | i[3-7]86-*-cygwin* | i[3-7]86-*-winnt | i[3-7]86-*-pe)
82 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
83 header in generic PE code. */
84 #include "coff/i386.h"
87 /* FIXME: These are BFD internal header files, and we should not be
89 #include "../bfd/libcoff.h"
90 #include "../bfd/libpei.h"
92 #if defined(TARGET_IS_armpe) \
93 || defined(TARGET_IS_arm_wince_pe)
94 #define bfd_arm_allocate_interworking_sections \
95 bfd_${EMULATION_NAME}_allocate_interworking_sections
96 #define bfd_arm_get_bfd_for_interworking \
97 bfd_${EMULATION_NAME}_get_bfd_for_interworking
98 #define bfd_arm_process_before_allocation \
99 bfd_${EMULATION_NAME}_process_before_allocation
100 #include "coff-arm.h"
105 #include "safe-ctype.h"
107 /* Permit the emulation parameters to override the default section
108 alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes
109 it seem that include/coff/internal.h should not define
110 PE_DEF_SECTION_ALIGNMENT. */
111 #if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
112 #undef PE_DEF_SECTION_ALIGNMENT
113 #define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
116 #if defined(TARGET_IS_i386pe) \
117 || defined(TARGET_IS_shpe) \
118 || defined(TARGET_IS_armpe) \
119 || defined(TARGET_IS_arm_wince_pe)
123 #if defined(TARGET_IS_i386pe)
124 #define DEFAULT_PSEUDO_RELOC_VERSION 2
126 #define DEFAULT_PSEUDO_RELOC_VERSION 1
129 #define DEFAULT_DLL_CHARACTERISTICS (${cygwin_behavior} ? 0 : \
130 IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE \
131 | IMAGE_DLL_CHARACTERISTICS_NX_COMPAT)
133 #if defined(TARGET_IS_i386pe) || ! defined(DLL_SUPPORT)
134 #define PE_DEF_SUBSYSTEM IMAGE_SUBSYSTEM_WINDOWS_CUI
136 #undef NT_EXE_IMAGE_BASE
137 #undef PE_DEF_SECTION_ALIGNMENT
138 #undef PE_DEF_FILE_ALIGNMENT
139 #define NT_EXE_IMAGE_BASE 0x00010000
141 #if defined(TARGET_IS_armpe)
142 #define PE_DEF_SECTION_ALIGNMENT 0x00001000
143 #define PE_DEF_SUBSYSTEM IMAGE_SUBSYSTEM_WINDOWS_GUI
144 #elif defined(TARGET_IS_arm_wince_pe)
145 #define PE_DEF_SECTION_ALIGNMENT 0x00001000
146 #define PE_DEF_SUBSYSTEM IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
148 #define PE_DEF_SECTION_ALIGNMENT 0x00000400
149 #define PE_DEF_SUBSYSTEM IMAGE_SUBSYSTEM_WINDOWS_GUI
151 #define PE_DEF_FILE_ALIGNMENT 0x00000200
154 static struct internal_extra_pe_aouthdr pe;
156 static int pe_subsystem = ${SUBSYSTEM};
157 static flagword real_flags = 0;
158 static int support_old_code = 0;
159 static char * thumb_entry_symbol = NULL;
160 static lang_assignment_statement_type *image_base_statement = 0;
161 static unsigned short pe_dll_characteristics = DEFAULT_DLL_CHARACTERISTICS;
162 static bool insert_timestamp = true;
163 static const char *emit_build_id;
166 static char *pdb_name;
170 static int pe_enable_stdcall_fixup = -1; /* 0=disable 1=enable. */
171 static char *pe_out_def_filename = NULL;
172 static int pe_enable_auto_image_base = 0;
173 static unsigned long pe_auto_image_base = 0x61500000;
174 static char *pe_dll_search_prefix = NULL;
177 extern const char *output_filename;
180 is_underscoring (void)
183 if (pe_leading_underscore != -1)
184 return pe_leading_underscore;
185 if (!bfd_get_target_info ("${OUTPUT_FORMAT}", NULL, NULL, &u, NULL))
186 bfd_get_target_info ("${RELOCATEABLE_OUTPUT_FORMAT}", NULL, NULL, &u, NULL);
190 pe_leading_underscore = (u != 0 ? 1 : 0);
191 return pe_leading_underscore;
195 gld${EMULATION_NAME}_before_parse (void)
198 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
199 output_filename = "${EXECUTABLE_NAME:-a.exe}";
201 input_flags.dynamic = true;
202 config.has_shared = 1;
205 # Cygwin no longer wants these noisy warnings. Other PE
206 # targets might like to consider adding themselves here.
207 # See also the mail thread starting here for the reason why
208 # merge_rdata defaults to 0 for cygwin:
209 # http://cygwin.com/ml/cygwin-apps/2013-04/msg00187.html
212 default_auto_import=1
213 default_merge_rdata=0
215 i[3-7]86-*-mingw* | x86_64-*-mingw*)
216 default_auto_import=1
217 default_merge_rdata=0
220 default_auto_import=-1
221 default_merge_rdata=1
226 link_info.pei386_auto_import = ${default_auto_import};
227 /* Use by default version. */
228 link_info.pei386_runtime_pseudo_reloc = DEFAULT_PSEUDO_RELOC_VERSION;
232 /* Indicates if RDATA shall be merged into DATA when pseudo-relocation
233 version 2 is used and auto-import is enabled. */
234 #define MERGE_RDATA_V2 ${default_merge_rdata}
236 /* PE format extra command line options. */
239 gld${EMULATION_NAME}_add_options
240 (int ns ATTRIBUTE_UNUSED,
241 char **shortopts ATTRIBUTE_UNUSED,
243 struct option **longopts,
244 int nrl ATTRIBUTE_UNUSED,
245 struct option **really_longopts ATTRIBUTE_UNUSED)
247 static const struct option xtra_long[] =
250 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
251 {"dll", no_argument, NULL, OPTION_DLL},
252 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
253 {"heap", required_argument, NULL, OPTION_HEAP},
254 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
255 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
256 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
257 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
258 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
259 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
260 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
261 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
262 {"stack", required_argument, NULL, OPTION_STACK},
263 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
264 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
265 {"thumb-entry", required_argument, NULL, OPTION_THUMB_ENTRY},
266 {"use-nul-prefixed-import-tables", no_argument, NULL,
267 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
268 {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
269 {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE},
270 {"insert-timestamp", no_argument, NULL, OPTION_INSERT_TIMESTAMP},
271 {"no-insert-timestamp", no_argument, NULL, OPTION_NO_INSERT_TIMESTAMP},
273 /* getopt allows abbreviations, so we do this to stop it
274 from treating -o as an abbreviation for this option. */
275 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
276 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
277 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
278 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
279 {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS},
280 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
281 {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
282 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
283 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
284 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
285 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
286 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
287 /* getopt() allows abbreviations, so we do this to stop it from
288 treating -c as an abbreviation for these --compat-implib. */
289 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
290 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
291 {"enable-auto-image-base", optional_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
292 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
293 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
294 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
295 {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
296 {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
297 {"enable-extra-pe-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
298 {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
299 {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
300 {"enable-runtime-pseudo-reloc-v1", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1},
301 {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2},
303 {"large-address-aware", no_argument, NULL, OPTION_LARGE_ADDRESS_AWARE},
304 {"disable-large-address-aware", no_argument, NULL, OPTION_DISABLE_LARGE_ADDRESS_AWARE},
305 {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
306 {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
307 {"dynamicbase",no_argument, NULL, OPTION_DYNAMIC_BASE},
308 {"disable-dynamicbase",no_argument, NULL, OPTION_DISABLE_DYNAMIC_BASE},
309 {"forceinteg", no_argument, NULL, OPTION_FORCE_INTEGRITY},
310 {"disable-forceinteg", no_argument, NULL, OPTION_DISABLE_FORCE_INTEGRITY},
311 {"nxcompat", no_argument, NULL, OPTION_NX_COMPAT},
312 {"disable-nxcompat", no_argument, NULL, OPTION_DISABLE_NX_COMPAT},
313 {"no-isolation", no_argument, NULL, OPTION_NO_ISOLATION},
314 {"disable-no-isolation", no_argument, NULL, OPTION_DISABLE_NO_ISOLATION},
315 {"no-seh", no_argument, NULL, OPTION_NO_SEH},
316 {"disable-no-seh", no_argument, NULL, OPTION_DISABLE_NO_SEH},
317 {"no-bind", no_argument, NULL, OPTION_NO_BIND},
318 {"disable-no-bind", no_argument, NULL, OPTION_DISABLE_NO_BIND},
319 {"wdmdriver", no_argument, NULL, OPTION_WDM_DRIVER},
320 {"disable-wdmdriver", no_argument, NULL, OPTION_DISABLE_WDM_DRIVER},
321 {"tsaware", no_argument, NULL, OPTION_TERMINAL_SERVER_AWARE},
322 {"disable-tsaware", no_argument, NULL, OPTION_DISABLE_TERMINAL_SERVER_AWARE},
323 {"build-id", optional_argument, NULL, OPTION_BUILD_ID},
325 {"pdb", required_argument, NULL, OPTION_PDB},
327 {"enable-reloc-section", no_argument, NULL, OPTION_ENABLE_RELOC_SECTION},
328 {"disable-reloc-section", no_argument, NULL, OPTION_DISABLE_RELOC_SECTION},
329 {NULL, no_argument, NULL, 0}
333 = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
334 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
337 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
338 parameters which may be input from the command line. */
347 /* FALSE for an assembly level symbol and TRUE for a C visible symbol.
348 C visible symbols can be prefixed by underscore dependent to target's
353 /* Get symbol name dependent to kind and C visible state of
355 #define GET_INIT_SYMBOL_NAME(IDX) \
356 (init[(IDX)].symbol \
357 + ((!init[(IDX)].is_c_symbol || is_underscoring () != 0) ? 0 : 1))
359 /* Decorates the C visible symbol by underscore, if target requires. */
361 ((is_underscoring () == 0) ? CSTR : "_" CSTR)
363 #define D(field,symbol,def,usc) {&pe.field, sizeof (pe.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__", 1, false),
379 D(MinorImageVersion,"__minor_image_version__", 0, false),
380 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
381 D(MajorSubsystemVersion,"__major_subsystem_version__", 3, false),
383 D(MajorSubsystemVersion,"__major_subsystem_version__", 4, false),
385 D(MinorSubsystemVersion,"__minor_subsystem_version__", 0, false),
386 D(Subsystem,"__subsystem__", ${SUBSYSTEM}, false),
387 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000, false),
388 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000, false),
389 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000, false),
390 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000, false),
391 D(LoaderFlags,"__loader_flags__", 0x0, false),
392 D(DllCharacteristics, "__dll_characteristics__", DEFAULT_DLL_CHARACTERISTICS, false),
393 { NULL, 0, 0, NULL, 0 , false}
398 gld${EMULATION_NAME}_list_options (FILE *file)
400 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
401 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
402 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
403 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
404 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
405 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
406 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
407 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
408 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
409 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
410 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
411 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
412 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
413 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
414 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
415 fprintf (file, _(" --[no-]leading-underscore Set explicit symbol underscore prefix mode\n"));
416 fprintf (file, _(" --thumb-entry=<symbol> Set the entry point to be Thumb <symbol>\n"));
417 fprintf (file, _(" --[no-]insert-timestamp Use a real timestamp rather than zero (default).\n"));
418 fprintf (file, _(" This makes binaries non-deterministic\n"));
420 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
421 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
422 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
423 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
424 fprintf (file, _(" --exclude-all-symbols Exclude all symbols from automatic export\n"));
425 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
426 fprintf (file, _(" --exclude-modules-for-implib mod,mod,...\n"));
427 fprintf (file, _(" Exclude objects, archive members from auto\n"));
428 fprintf (file, _(" export, place into import library instead.\n"));
429 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
430 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
431 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
432 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports\n"));
433 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
434 create __imp_<SYMBOL> as well.\n"));
435 fprintf (file, _(" --enable-auto-image-base[=<address>] Automatically choose image base for DLLs\n\
436 (optionally starting with address) unless\n\
437 specifically set with --image-base\n"));
438 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
439 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
440 an importlib, use <string><basename>.dll\n\
441 in preference to lib<basename>.dll \n"));
442 fprintf (file, _(" --enable-auto-import Do sophisticated linking of _sym to\n\
443 __imp_sym for DATA references\n"));
444 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
445 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
446 adding pseudo-relocations resolved at\n\
448 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
449 auto-imported DATA.\n"));
450 fprintf (file, _(" --enable-extra-pe-debug Enable verbose debug output when building\n\
451 or linking to DLLs (esp. auto-import)\n"));
453 fprintf (file, _(" --large-address-aware Executable supports virtual addresses\n\
454 greater than 2 gigabytes\n"));
455 fprintf (file, _(" --disable-large-address-aware Executable does not support virtual\n\
456 addresses greater than 2 gigabytes\n"));
457 fprintf (file, _(" --enable-long-section-names Use long COFF section names even in\n\
458 executable image files\n"));
459 fprintf (file, _(" --disable-long-section-names Never use long COFF section names, even\n\
460 in object files\n"));
461 fprintf (file, _(" --[disable-]dynamicbase Image base address may be relocated using\n\
462 address space layout randomization (ASLR)\n"));
463 fprintf (file, _(" --enable-reloc-section Create the base relocation table\n"));
464 fprintf (file, _(" --disable-reloc-section Do not create the base relocation table\n"));
465 fprintf (file, _(" --[disable-]forceinteg Code integrity checks are enforced\n"));
466 fprintf (file, _(" --[disable-]nxcompat Image is compatible with data execution\n\
468 fprintf (file, _(" --[disable-]no-isolation Image understands isolation but do not\n\
469 isolate the image\n"));
470 fprintf (file, _(" --[disable-]no-seh Image does not use SEH. No SE handler may\n\
471 be called in this image\n"));
472 fprintf (file, _(" --[disable-]no-bind Do not bind this image\n"));
473 fprintf (file, _(" --[disable-]wdmdriver Driver uses the WDM model\n"));
474 fprintf (file, _(" --[disable-]tsaware Image is Terminal Server aware\n"));
475 fprintf (file, _(" --build-id[=STYLE] Generate build ID\n"));
477 fprintf (file, _(" --pdb=[FILENAME] Generate PDB file\n"));
481 /* A case insensitive comparison, regardless of the host platform, used for
482 comparing file extensions. */
484 fileext_cmp (const char *s1, const char *s2)
488 int c1 = TOLOWER (*s1++);
489 int c2 = *s2++; /* Assumed to be lower case from the caller. */
500 set_pe_name (char *name, long val)
505 /* Find the name and set it. */
506 for (i = 0; init[i].ptr; i++)
508 if (strcmp (name, GET_INIT_SYMBOL_NAME (i)) == 0)
512 if (strcmp (name,"__image_base__") == 0)
513 set_pe_name (U ("__ImageBase"), val);
521 set_entry_point (void)
524 const char *initial_symbol_char;
534 { 1, "NtProcessStartup" },
535 { 2, "WinMainCRTStartup" },
536 { 3, "mainCRTStartup" },
537 { 7, "__PosixProcessStartup"},
538 { 9, "WinMainCRTStartup" },
539 {14, "mainCRTStartup" },
543 /* Entry point name for arbitrary subsystem numbers. */
544 static const char default_entry[] = "mainCRTStartup";
546 if (bfd_link_dll (&link_info) || dll)
548 #if defined (TARGET_IS_i386pe)
549 entry = "DllMainCRTStartup@12";
551 entry = "DllMainCRTStartup";
556 for (i = 0; v[i].entry; i++)
557 if (v[i].value == pe_subsystem)
560 /* If no match, use the default. */
561 if (v[i].entry != NULL)
564 entry = default_entry;
567 initial_symbol_char = (is_underscoring () != 0 ? "_" : "");
569 if (*initial_symbol_char != '\0')
573 /* lang_default_entry expects its argument to be permanently
574 allocated, so we don't free this string. */
575 alc_entry = xmalloc (strlen (initial_symbol_char)
578 strcpy (alc_entry, initial_symbol_char);
579 strcat (alc_entry, entry);
583 lang_default_entry (entry);
585 if (bfd_link_executable (&link_info) && ! entry_from_cmdline)
586 ldlang_add_undef (entry, false);
590 set_pe_subsystem (void)
596 unsigned long temp_subsystem;
613 /* Check for the presence of a version number. */
614 sver = strchr (optarg, ':');
616 len = strlen (optarg);
620 set_pe_name ("__major_subsystem_version__",
621 strtoul (sver + 1, &end, 0));
623 set_pe_name ("__minor_subsystem_version__",
624 strtoul (end + 1, &end, 0));
626 einfo (_("%P: warning: bad version number in -subsystem option\n"));
629 /* Check for numeric subsystem. */
630 temp_subsystem = strtoul (optarg, & end, 0);
631 if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
633 /* Search list for a numeric match to use its entry point. */
634 for (i = 0; v[i].name; i++)
635 if (v[i].value == (int) temp_subsystem)
638 /* Use this subsystem. */
639 pe_subsystem = (int) temp_subsystem;
643 /* Search for subsystem by name. */
644 for (i = 0; v[i].name; i++)
645 if (strncmp (optarg, v[i].name, len) == 0
646 && v[i].name[len] == '\0')
649 if (v[i].name == NULL)
651 einfo (_("%F%P: invalid subsystem type %s\n"), optarg);
655 pe_subsystem = v[i].value;
658 set_pe_name ("__subsystem__", pe_subsystem);
665 set_pe_value (char *name)
669 set_pe_name (name, strtoul (optarg, &end, 0));
672 einfo (_("%F%P: invalid hex number for PE parameter '%s'\n"), optarg);
679 set_pe_stack_heap (char *resname, char *comname)
681 set_pe_value (resname);
686 set_pe_value (comname);
689 einfo (_("%F%P: strange hex info for PE parameter '%s'\n"), optarg);
692 #define DEFAULT_BUILD_ID_STYLE "md5"
695 gld${EMULATION_NAME}_handle_option (int optc)
702 case OPTION_BASE_FILE:
703 link_info.base_file = fopen (optarg, FOPEN_WB);
704 if (link_info.base_file == NULL)
705 einfo (_("%F%P: cannot open base file %s\n"), optarg);
710 set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
713 set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
715 case OPTION_SUBSYSTEM:
718 case OPTION_MAJOR_OS_VERSION:
719 set_pe_value ("__major_os_version__");
721 case OPTION_MINOR_OS_VERSION:
722 set_pe_value ("__minor_os_version__");
724 case OPTION_MAJOR_SUBSYSTEM_VERSION:
725 set_pe_value ("__major_subsystem_version__");
727 case OPTION_MINOR_SUBSYSTEM_VERSION:
728 set_pe_value ("__minor_subsystem_version__");
730 case OPTION_MAJOR_IMAGE_VERSION:
731 set_pe_value ("__major_image_version__");
733 case OPTION_MINOR_IMAGE_VERSION:
734 set_pe_value ("__minor_image_version__");
736 case OPTION_FILE_ALIGNMENT:
737 set_pe_value ("__file_alignment__");
739 case OPTION_SECTION_ALIGNMENT:
740 set_pe_value ("__section_alignment__");
743 set_pe_name ("__dll__", 1);
745 case OPTION_IMAGE_BASE:
746 set_pe_value ("__image_base__");
748 case OPTION_SUPPORT_OLD_CODE:
749 support_old_code = 1;
751 case OPTION_THUMB_ENTRY:
752 thumb_entry_symbol = optarg;
754 case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
755 pe_use_nul_prefixed_import_tables = true;
757 case OPTION_NO_LEADING_UNDERSCORE:
758 pe_leading_underscore = 0;
760 case OPTION_LEADING_UNDERSCORE:
761 pe_leading_underscore = 1;
763 case OPTION_INSERT_TIMESTAMP:
764 insert_timestamp = true;
766 case OPTION_NO_INSERT_TIMESTAMP:
767 insert_timestamp = false;
771 pe_out_def_filename = xstrdup (optarg);
773 case OPTION_EXPORT_ALL:
774 pe_dll_export_everything = 1;
776 case OPTION_EXCLUDE_SYMBOLS:
777 pe_dll_add_excludes (optarg, EXCLUDESYMS);
779 case OPTION_EXCLUDE_ALL_SYMBOLS:
780 pe_dll_exclude_all_symbols = 1;
782 case OPTION_EXCLUDE_LIBS:
783 pe_dll_add_excludes (optarg, EXCLUDELIBS);
785 case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
786 pe_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
788 case OPTION_KILL_ATS:
791 case OPTION_STDCALL_ALIASES:
792 pe_dll_stdcall_aliases = 1;
794 case OPTION_ENABLE_STDCALL_FIXUP:
795 pe_enable_stdcall_fixup = 1;
797 case OPTION_DISABLE_STDCALL_FIXUP:
798 pe_enable_stdcall_fixup = 0;
800 case OPTION_WARN_DUPLICATE_EXPORTS:
801 pe_dll_warn_dup_exports = 1;
803 case OPTION_IMP_COMPAT:
804 pe_dll_compat_implib = 1;
806 case OPTION_ENABLE_AUTO_IMAGE_BASE:
807 pe_enable_auto_image_base = 1;
808 if (optarg && *optarg)
811 pe_auto_image_base = strtoul (optarg, &end, 0);
812 /* XXX should check that we actually parsed something */
815 case OPTION_DISABLE_AUTO_IMAGE_BASE:
816 pe_enable_auto_image_base = 0;
818 case OPTION_DLL_SEARCH_PREFIX:
819 pe_dll_search_prefix = xstrdup (optarg);
821 case OPTION_NO_DEFAULT_EXCLUDES:
822 pe_dll_do_default_excludes = 0;
824 case OPTION_DLL_ENABLE_AUTO_IMPORT:
825 link_info.pei386_auto_import = 1;
827 case OPTION_DLL_DISABLE_AUTO_IMPORT:
828 link_info.pei386_auto_import = 0;
830 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
831 link_info.pei386_runtime_pseudo_reloc =
832 DEFAULT_PSEUDO_RELOC_VERSION;
834 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1:
835 link_info.pei386_runtime_pseudo_reloc = 1;
837 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2:
838 link_info.pei386_runtime_pseudo_reloc = 2;
840 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
841 link_info.pei386_runtime_pseudo_reloc = 0;
843 case OPTION_ENABLE_EXTRA_PE_DEBUG:
844 pe_dll_extra_pe_debug = 1;
847 case OPTION_LARGE_ADDRESS_AWARE:
848 real_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
850 case OPTION_DISABLE_LARGE_ADDRESS_AWARE:
851 real_flags &= ~ IMAGE_FILE_LARGE_ADDRESS_AWARE;
853 case OPTION_ENABLE_LONG_SECTION_NAMES:
854 pe_use_coff_long_section_names = 1;
856 case OPTION_DISABLE_LONG_SECTION_NAMES:
857 pe_use_coff_long_section_names = 0;
859 /* Get DLLCharacteristics bits */
860 case OPTION_DYNAMIC_BASE:
861 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
863 case OPTION_ENABLE_RELOC_SECTION:
864 pe_dll_enable_reloc_section = 1;
866 case OPTION_DISABLE_RELOC_SECTION:
867 pe_dll_enable_reloc_section = 0;
869 case OPTION_DISABLE_DYNAMIC_BASE:
870 pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
872 case OPTION_FORCE_INTEGRITY:
873 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
875 case OPTION_DISABLE_FORCE_INTEGRITY:
876 pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
878 case OPTION_NX_COMPAT:
879 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
881 case OPTION_DISABLE_NX_COMPAT:
882 pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
884 case OPTION_NO_ISOLATION:
885 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
887 case OPTION_DISABLE_NO_ISOLATION:
888 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
891 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_SEH;
893 case OPTION_DISABLE_NO_SEH:
894 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_SEH;
897 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_BIND;
899 case OPTION_DISABLE_NO_BIND:
900 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_BIND;
902 case OPTION_WDM_DRIVER:
903 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
905 case OPTION_DISABLE_WDM_DRIVER:
906 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
908 case OPTION_TERMINAL_SERVER_AWARE:
909 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
911 case OPTION_DISABLE_TERMINAL_SERVER_AWARE:
912 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
914 case OPTION_BUILD_ID:
915 free ((char *) emit_build_id);
916 emit_build_id = NULL;
918 optarg = DEFAULT_BUILD_ID_STYLE;
919 if (strcmp (optarg, "none"))
920 emit_build_id = xstrdup (optarg);
925 if (optarg && optarg[0])
926 pdb_name = xstrdup (optarg);
931 /* Set DLLCharacteristics bits */
932 set_pe_name ("__dll_characteristics__", pe_dll_characteristics);
940 strhash (const char *str)
942 const unsigned char *s;
949 s = (const unsigned char *) str;
950 while ((c = *s++) != '\0')
952 hash += c + (c << 17);
956 hash += len + (len << 17);
962 /* Use the output file to create a image base for relocatable DLLs. */
965 compute_dll_image_base (const char *ofile)
967 unsigned long hash = strhash (ofile);
968 return pe_auto_image_base + ((hash << 16) & 0x0FFC0000);
972 /* Assign values to the special symbols before the linker script is
976 gld${EMULATION_NAME}_set_symbols (void)
978 /* Run through and invent symbols for all the
979 names and insert the defaults. */
984 if (!init[IMAGEBASEOFF].inited)
986 if (bfd_link_relocatable (&link_info))
987 init[IMAGEBASEOFF].value = 0;
988 else if (init[DLLOFF].value || bfd_link_dll (&link_info))
991 init[IMAGEBASEOFF].value = (pe_enable_auto_image_base
992 ? compute_dll_image_base (output_filename)
993 : NT_DLL_IMAGE_BASE);
995 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
999 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
1000 init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
1003 /* Don't do any symbol assignments if this is a relocatable link. */
1004 if (bfd_link_relocatable (&link_info))
1007 /* Glue the assignments into the abs section. */
1008 push_stat_ptr (&abs_output_section->children);
1010 for (j = 0; init[j].ptr; j++)
1012 long val = init[j].value;
1013 lang_assignment_statement_type *rv;
1015 rv = lang_add_assignment (exp_assign (GET_INIT_SYMBOL_NAME (j),
1016 exp_intop (val), false));
1017 if (init[j].size == sizeof (short))
1018 *(short *) init[j].ptr = val;
1019 else if (init[j].size == sizeof (int))
1020 *(int *) init[j].ptr = val;
1021 else if (init[j].size == sizeof (long))
1022 *(long *) init[j].ptr = val;
1023 /* This might be a long long or other special type. */
1024 else if (init[j].size == sizeof (bfd_vma))
1025 *(bfd_vma *) init[j].ptr = val;
1027 if (j == IMAGEBASEOFF)
1028 image_base_statement = rv;
1030 /* Restore the pointer. */
1033 if (pe.FileAlignment > pe.SectionAlignment)
1035 einfo (_("%P: warning, file alignment > section alignment\n"));
1039 /* This is called after the linker script and the command line options
1043 gld${EMULATION_NAME}_after_parse (void)
1045 /* PR ld/6744: Warn the user if they have used an ELF-only
1046 option hoping it will work on PE. */
1047 if (link_info.export_dynamic)
1048 einfo (_("%P: warning: --export-dynamic is not supported for PE "
1049 "targets, did you mean --export-all-symbols?\n"));
1052 if (pdb && emit_build_id == NULL)
1053 emit_build_id = xstrdup (DEFAULT_BUILD_ID_STYLE);
1058 after_parse_default ();
1062 static struct bfd_link_hash_entry *pe_undef_found_sym;
1065 pe_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
1069 const char *hs = h->root.string;
1071 sl = strlen (string);
1072 if (h->type == bfd_link_hash_defined
1073 && ((*hs == '@' && *string == '_'
1074 && strncmp (hs + 1, string + 1, sl - 1) == 0)
1075 || strncmp (hs, string, sl) == 0)
1076 && h->root.string[sl] == '@')
1078 pe_undef_found_sym = h;
1084 /* Change UNDEF to a defined symbol, taking data from SYM. */
1087 change_undef (struct bfd_link_hash_entry * undef,
1088 struct bfd_link_hash_entry * sym)
1090 static bool gave_warning_message = false;
1092 undef->type = bfd_link_hash_defined;
1093 undef->u.def.value = sym->u.def.value;
1094 undef->u.def.section = sym->u.def.section;
1096 if (pe_enable_stdcall_fixup == -1)
1098 einfo (_("%P: warning: resolving %s by linking to %s\n"),
1099 undef->root.string, sym->root.string);
1101 if (! gave_warning_message)
1103 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
1104 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
1105 gave_warning_message = true;
1109 /* PR 19803: Make sure that the linked symbol is not garbage collected. */
1110 lang_add_gc_name (sym->root.string);
1114 set_decoration (const char *undecorated_name,
1115 struct bfd_link_hash_entry * decoration)
1117 static bool gave_warning_message = false;
1118 struct decoration_hash_entry *entry;
1120 if (is_underscoring () && undecorated_name[0] == '_')
1123 entry = (struct decoration_hash_entry *)
1124 bfd_hash_lookup (&(coff_hash_table (&link_info)->decoration_hash),
1125 undecorated_name, true /* create */, false /* copy */);
1127 if (entry->decorated_link != NULL && !gave_warning_message)
1129 einfo (_("%P: warning: overwriting decorated name %s with %s\n"),
1130 entry->decorated_link->root.string, undecorated_name);
1131 gave_warning_message = true;
1134 entry->decorated_link = decoration;
1138 pe_fixup_stdcalls (void)
1140 struct bfd_link_hash_entry *undef, *sym;
1142 if (pe_dll_extra_pe_debug)
1143 printf ("%s\n", __func__);
1145 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
1146 if (undef->type == bfd_link_hash_undefined)
1148 const char * name = undef->root.string;
1150 int lead_at = (*name == '@');
1153 at = strchr (name + 1, '@');
1155 at = strchr (name, '@');
1159 /* The symbol is a stdcall symbol, so let's look for a
1160 cdecl symbol with the same name and resolve to that. */
1161 char *cname = xstrdup (name);
1166 * strchr (cname, '@') = 0;
1167 sym = bfd_link_hash_lookup (link_info.hash, cname, false, false, true);
1169 if (sym && sym->type == bfd_link_hash_defined)
1170 change_undef (undef, sym);
1174 /* The symbol is a cdecl symbol, so we look for stdcall
1175 symbols - which means scanning the whole symbol table. */
1176 pe_undef_found_sym = NULL;
1177 bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
1179 if (pe_undef_found_sym)
1181 change_undef (undef, pe_undef_found_sym);
1182 set_decoration (undef->root.string, pe_undef_found_sym);
1189 read_addend (arelent *rel, asection *s)
1194 if (!bfd_get_section_contents (s->owner, s, buf, rel->address, sizeof (buf)))
1195 einfo (_("%P: %H: cannot get section contents - auto-import exception\n"),
1196 s->owner, s, rel->address);
1198 addend = bfd_get_32 (s->owner, buf);
1203 make_import_fixup (arelent *rel, asection *s, char *name, const char *symname)
1205 struct bfd_symbol *sym = *rel->sym_ptr_ptr;
1208 if (pe_dll_extra_pe_debug)
1209 printf ("arelent: %s@%#lx: add=%li\n", sym->name,
1210 (unsigned long) rel->address, (long) rel->addend);
1212 addend = read_addend (rel, s);
1214 if (pe_dll_extra_pe_debug)
1216 printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx",
1217 (long) addend, (long) rel->addend, (long) rel->address);
1218 if (rel->howto->pc_relative)
1220 printf (" %d bit rel.\n", (int) rel->howto->bitsize);
1223 pe_create_import_fixup (rel, s, addend, name, symname);
1227 make_runtime_ref (void)
1229 const char *rr = U ("_pei386_runtime_relocator");
1230 struct bfd_link_hash_entry *h
1231 = bfd_wrapped_link_hash_lookup (link_info.output_bfd, &link_info,
1232 rr, true, false, true);
1234 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
1237 if (h->type == bfd_link_hash_new)
1239 h->type = bfd_link_hash_undefined;
1240 h->u.undef.abfd = NULL;
1241 if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
1242 bfd_link_add_undef (link_info.hash, h);
1244 h->non_ir_ref_regular = true;
1249 pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
1251 printf ("+%s\n", h->string);
1255 #endif /* DLL_SUPPORT */
1258 debug_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
1260 int *found = (int *) obj;
1261 if (strncmp (".debug_", sect->name, sizeof (".debug_") - 1) == 0)
1266 pecoff_checksum_contents (bfd *abfd,
1267 void (*process) (const void *, size_t, void *),
1270 file_ptr filepos = (file_ptr) 0;
1277 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
1280 status = bfd_read (&b, 1, abfd);
1286 (*process) (&b, 1, arg);
1294 write_build_id (bfd *abfd)
1296 struct pe_tdata *td = pe_data (abfd);
1298 struct bfd_link_order *link_order = NULL;
1299 unsigned char *contents;
1300 bfd_size_type build_id_size;
1301 unsigned char *build_id;
1302 const char *pdb_base_name = NULL;
1304 /* Find the section the .buildid output section has been merged info. */
1305 for (asec = abfd->sections; asec != NULL; asec = asec->next)
1307 struct bfd_link_order *l = NULL;
1308 for (l = asec->map_head.link_order; l != NULL; l = l->next)
1310 if (l->type == bfd_indirect_link_order)
1312 if (l->u.indirect.section == td->build_id.sec)
1326 einfo (_("%P: warning: .buildid section discarded,"
1327 " --build-id ignored\n"));
1331 if (td->build_id.sec->contents == NULL)
1332 td->build_id.sec->contents = xmalloc (td->build_id.sec->size);
1333 contents = td->build_id.sec->contents;
1335 build_id_size = compute_build_id_size (td->build_id.style);
1336 build_id = xmalloc (build_id_size);
1337 generate_build_id (abfd, td->build_id.style, pecoff_checksum_contents,
1338 build_id, build_id_size);
1340 bfd_vma ib = td->pe_opthdr.ImageBase;
1344 pdb_base_name = lbasename (pdb_name);
1347 /* Construct a debug directory entry which points to an immediately following CodeView record. */
1348 struct internal_IMAGE_DEBUG_DIRECTORY idd;
1349 idd.Characteristics = 0;
1350 idd.TimeDateStamp = 0;
1351 idd.MajorVersion = 0;
1352 idd.MinorVersion = 0;
1353 idd.Type = PE_IMAGE_DEBUG_TYPE_CODEVIEW;
1354 idd.SizeOfData = (sizeof (CV_INFO_PDB70)
1356 + (pdb_base_name ? strlen (pdb_base_name) : 0)
1359 idd.AddressOfRawData = asec->vma - ib + link_order->offset
1360 + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1361 idd.PointerToRawData = asec->filepos + link_order->offset
1362 + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1364 struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *)contents;
1365 _bfd_XXi_swap_debugdir_out (abfd, &idd, ext);
1367 /* Write the debug directory entry. */
1368 if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0)
1371 if (bfd_write (contents, sizeof (*ext), abfd) != sizeof (*ext))
1377 if (!create_pdb_file (abfd, pdb_name, build_id))
1382 /* Construct the CodeView record. */
1383 CODEVIEW_INFO cvinfo;
1384 cvinfo.CVSignature = CVINFO_PDB70_CVSIGNATURE;
1387 /* Zero pad or truncate the generated build_id to fit in the
1389 memset (&(cvinfo.Signature), 0, CV_INFO_SIGNATURE_LENGTH);
1390 memcpy (&(cvinfo.Signature), build_id,
1391 (build_id_size > CV_INFO_SIGNATURE_LENGTH
1392 ? CV_INFO_SIGNATURE_LENGTH : build_id_size));
1396 /* Write the codeview record. */
1397 if (_bfd_XXi_write_codeview_record (abfd, idd.PointerToRawData, &cvinfo,
1398 pdb_base_name) == 0)
1401 /* Record the location of the debug directory in the data directory. */
1402 td->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress
1403 = asec->vma - ib + link_order->offset;
1404 td->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
1405 = sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1410 /* Make .buildid section, and set up coff_tdata->build_id. */
1412 setup_build_id (bfd *ibfd)
1417 if (!validate_build_id_style (emit_build_id))
1419 einfo (_("%P: warning: unrecognized --build-id style ignored\n"));
1423 flags = (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY
1424 | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA);
1425 s = bfd_make_section_anyway_with_flags (ibfd, ".buildid", flags);
1428 struct pe_tdata *td = pe_data (link_info.output_bfd);
1429 td->build_id.after_write_object_contents = &write_build_id;
1430 td->build_id.style = emit_build_id;
1431 td->build_id.sec = s;
1433 /* Section is a fixed size:
1434 One IMAGE_DEBUG_DIRECTORY entry, of type IMAGE_DEBUG_TYPE_CODEVIEW,
1435 pointing at a CV_INFO_PDB70 record containing the build-id, followed by
1436 PdbFileName if relevant. */
1437 s->size = (sizeof (struct external_IMAGE_DEBUG_DIRECTORY)
1438 + sizeof (CV_INFO_PDB70) + 1);
1442 s->size += strlen (lbasename (pdb_name));
1447 einfo (_("%P: warning: cannot create .buildid section,"
1448 " --build-id ignored\n"));
1453 gld${EMULATION_NAME}_before_plugin_all_symbols_read (void)
1456 if (link_info.lto_plugin_active
1457 && link_info.pei386_auto_import)
1458 make_runtime_ref ();
1463 gld${EMULATION_NAME}_after_open (void)
1465 after_open_default ();
1468 if (pe_dll_extra_pe_debug)
1471 struct bfd_link_hash_entry *sym;
1473 printf ("%s()\n", __func__);
1475 for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1476 printf ("-%s\n", sym->root.string);
1477 bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
1479 for (a = link_info.input_bfds; a; a = a->link.next)
1480 printf ("*%s\n", bfd_get_filename (a));
1485 if (pdb && !pdb_name)
1487 const char *base = lbasename (bfd_get_filename (link_info.output_bfd));
1488 size_t len = strlen (base);
1489 static const char suffix[] = ".pdb";
1491 while (len > 0 && base[len] != '.')
1497 len = strlen (base);
1499 pdb_name = xmalloc (len + sizeof (suffix));
1500 memcpy (pdb_name, base, len);
1501 memcpy (pdb_name + len, suffix, sizeof (suffix));
1505 if (emit_build_id != NULL)
1509 /* Find a COFF input. */
1510 for (abfd = link_info.input_bfds;
1511 abfd != (bfd *) NULL; abfd = abfd->link.next)
1512 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
1515 /* If there are no COFF input files do not try to
1516 add a build-id section. */
1518 || !setup_build_id (abfd))
1520 free ((char *) emit_build_id);
1521 emit_build_id = NULL;
1525 /* Pass the wacky PE command line options into the output bfd.
1526 FIXME: This should be done via a function, rather than by
1527 including an internal BFD header. */
1529 if (bfd_get_flavour (link_info.output_bfd) != bfd_target_coff_flavour
1530 || coff_data (link_info.output_bfd) == NULL
1531 || !obj_pe (link_info.output_bfd))
1532 einfo (_("%F%P: cannot perform PE operations on non PE output file '%pB'\n"),
1533 link_info.output_bfd);
1535 pe_data (link_info.output_bfd)->pe_opthdr = pe;
1536 pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
1537 pe_data (link_info.output_bfd)->real_flags |= real_flags;
1538 if (insert_timestamp)
1539 pe_data (link_info.output_bfd)->timestamp = -1;
1541 pe_data (link_info.output_bfd)->timestamp = 0;
1543 /* At this point we must decide whether to use long section names
1544 in the output or not. If the user hasn't explicitly specified
1545 on the command line, we leave it to the default for the format
1546 (object files yes, image files no), except if there is debug
1547 information present; GDB relies on the long section names to
1548 find it, so enable it in that case. */
1549 if (pe_use_coff_long_section_names < 0 && link_info.strip == strip_none)
1551 if (bfd_link_relocatable (&link_info))
1552 pe_use_coff_long_section_names = 1;
1555 /* Iterate over all sections of all input BFDs, checking
1556 for any that begin 'debug_' and are long names. */
1557 LANG_FOR_EACH_INPUT_STATEMENT (is)
1559 int found_debug = 0;
1561 bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug);
1564 pe_use_coff_long_section_names = 1;
1571 pe_output_file_set_long_section_names (link_info.output_bfd);
1574 pe_process_import_defs (link_info.output_bfd, &link_info);
1576 if (link_info.pei386_auto_import) /* -1=warn or 1=enable */
1577 pe_find_data_imports (U ("_head_"), make_import_fixup);
1579 /* The implementation of the feature is rather dumb and would cause the
1580 compilation time to go through the roof if there are many undefined
1581 symbols in the link, so it needs to be run after auto-import. */
1582 if (pe_enable_stdcall_fixup) /* -1=warn or 1=enable */
1583 pe_fixup_stdcalls ();
1585 #if defined (TARGET_IS_i386pe) \
1586 || defined (TARGET_IS_armpe) \
1587 || defined (TARGET_IS_arm_wince_pe)
1588 if (!bfd_link_relocatable (&link_info))
1589 pe_dll_build_sections (link_info.output_bfd, &link_info);
1591 if (bfd_link_pic (&link_info))
1592 pe_dll_build_sections (link_info.output_bfd, &link_info);
1594 pe_exe_build_sections (link_info.output_bfd, &link_info);
1596 #endif /* DLL_SUPPORT */
1598 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
1599 if (strstr (bfd_get_target (link_info.output_bfd), "arm") == NULL)
1601 /* The arm backend needs special fields in the output hash structure.
1602 These will only be created if the output format is an arm format,
1603 hence we do not support linking and changing output formats at the
1604 same time. Use a link followed by objcopy to change output formats. */
1605 einfo (_("%F%P: error: cannot change output format "
1606 "whilst linking %s binaries\n"), "ARM");
1610 /* Find a BFD that can hold the interworking stubs. */
1611 LANG_FOR_EACH_INPUT_STATEMENT (is)
1613 if (bfd_arm_get_bfd_for_interworking (is->the_bfd, & link_info))
1620 /* This next chunk of code tries to detect the case where you have
1621 two import libraries for the same DLL (specifically,
1622 symbolically linking libm.a and libc.a in cygwin to
1623 libcygwin.a). In those cases, it's possible for function
1624 thunks from the second implib to be used but without the
1625 head/tail objects, causing an improper import table. We detect
1626 those cases and rename the "other" import libraries to match
1627 the one the head/tail come from, so that the linker will sort
1628 things nicely and produce a valid import table. */
1630 LANG_FOR_EACH_INPUT_STATEMENT (is)
1632 if (is->the_bfd->my_archive)
1634 int idata2 = 0, reloc_count=0, is_imp = 0;
1637 /* See if this is an import library thunk. */
1638 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1640 if (strcmp (sec->name, ".idata\$2") == 0)
1642 if (startswith (sec->name, ".idata\$"))
1644 reloc_count += sec->reloc_count;
1647 if (is_imp && !idata2 && reloc_count)
1649 /* It is, look for the reference to head and see if it's
1650 from our own library. */
1651 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1659 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1663 if (!bfd_generic_link_read_symbols (is->the_bfd))
1665 einfo (_("%F%P: %pB: could not read symbols: %E\n"),
1669 symbols = bfd_get_outsymbols (is->the_bfd);
1671 relocs = xmalloc ((size_t) relsize);
1672 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1677 einfo (_("%X%P: unable to process relocs: %E\n"));
1681 for (i = 0; i < nrelocs; i++)
1683 struct bfd_symbol *s;
1684 struct bfd_link_hash_entry * blhe;
1686 const char *other_bfd_filename;
1688 s = (relocs[i]->sym_ptr_ptr)[0];
1690 if (s->flags & BSF_LOCAL)
1693 /* Thunk section with reloc to another bfd. */
1694 blhe = bfd_link_hash_lookup (link_info.hash,
1696 false, false, true);
1699 || blhe->type != bfd_link_hash_defined)
1702 other_bfd = blhe->u.def.section->owner;
1703 if (other_bfd->my_archive == is->the_bfd->my_archive)
1707 = (other_bfd->my_archive
1708 ? bfd_get_filename (other_bfd->my_archive)
1709 : bfd_get_filename (other_bfd));
1711 if (filename_cmp (bfd_get_filename
1712 (is->the_bfd->my_archive),
1713 other_bfd_filename) == 0)
1716 /* Sort this implib to match the other one. */
1717 lang_input_statement_type *arch_is
1718 = bfd_usrdata (is->the_bfd->my_archive);
1719 arch_is->sort_key = other_bfd_filename;
1724 /* Note - we do not free the symbols,
1725 they are now cached in the BFD. */
1734 /* Careful - this is a shell script. Watch those dollar signs! */
1735 /* Microsoft import libraries have every member named the same,
1736 and not in the right order for us to link them correctly. We
1737 must detect these and rename the members so that they'll link
1738 correctly. There are three types of objects: the head, the
1739 thunks, and the sentinel(s). The head is easy; it's the one
1740 with idata2. We assume that the sentinels won't have relocs,
1741 and the thunks will. It's easier than checking the symbol
1742 table for external references. */
1743 LANG_FOR_EACH_INPUT_STATEMENT (is)
1745 if (is->the_bfd->my_archive)
1749 /* Microsoft import libraries may contain archive members for
1750 one or more DLLs, together with static object files.
1751 Inspect all members that are named *.dll - check whether
1752 they contain .idata sections. Do the renaming of all
1753 archive members that seem to be Microsoft style import
1755 pnt = strrchr (bfd_get_filename (is->the_bfd), '.');
1757 if (pnt != NULL && (fileext_cmp (pnt + 1, "dll") == 0))
1759 int idata2 = 0, reloc_count = 0, idata = 0;
1761 char *new_name, seq;
1763 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1765 if (strcmp (sec->name, ".idata\$2") == 0)
1767 if (strncmp (sec->name, ".idata\$", 6) == 0)
1769 reloc_count += sec->reloc_count;
1772 /* An archive member named .dll, but not having any .idata
1773 sections - apparently not a Microsoft import object
1774 after all: Skip renaming it. */
1778 if (idata2) /* .idata2 is the TOC */
1780 else if (reloc_count > 0) /* thunks */
1786 = xmalloc (strlen (bfd_get_filename (is->the_bfd)) + 3);
1787 sprintf (new_name, "%s.%c",
1788 bfd_get_filename (is->the_bfd), seq);
1789 is->sort_key = new_name;
1796 /* The following chunk of code tries to identify jump stubs in
1797 import libraries which are dead code and eliminates them
1798 from the final link. For each exported symbol <sym>, there
1799 is a object file in the import library with a .text section
1800 and several .idata\$* sections. The .text section contains the
1801 symbol definition for <sym> which is a jump stub of the form
1802 jmp *__imp_<sym>. The .idata\$5 contains the symbol definition
1803 for __imp_<sym> which is the address of the slot for <sym> in
1804 the import address table. When a symbol is imported explicitly
1805 using __declspec(dllimport) declaration, the compiler generates
1806 a reference to __imp_<sym> which directly resolves to the
1807 symbol in .idata\$5, in which case the jump stub code is not
1808 needed. The following code tries to identify jump stub sections
1809 in import libraries which are not referred to by anyone and
1810 marks them for exclusion from the final link. */
1811 LANG_FOR_EACH_INPUT_STATEMENT (is)
1813 if (is->the_bfd->my_archive)
1816 asection *sec, *stub_sec = NULL;
1818 /* See if this is an import library thunk. */
1819 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1821 if (strncmp (sec->name, ".idata\$", 7) == 0)
1823 /* The section containing the jmp stub has code
1825 if ((sec->flags & SEC_CODE) && sec->reloc_count)
1829 if (is_imp && stub_sec)
1832 long nsyms, src_count;
1833 struct bfd_link_hash_entry * blhe;
1835 if (!bfd_generic_link_read_symbols (is->the_bfd))
1837 einfo (_("%F%P: %pB: could not read symbols: %E\n"),
1841 symbols = bfd_get_outsymbols (is->the_bfd);
1842 nsyms = bfd_get_symcount (is->the_bfd);
1844 for (src_count = 0; src_count < nsyms; src_count++)
1846 if (symbols[src_count]->section->id == stub_sec->id)
1848 /* This symbol belongs to the section containing
1850 blhe = bfd_link_hash_lookup (link_info.hash,
1851 symbols[src_count]->name,
1852 false, false, true);
1853 /* If the symbol in the stub section has no other
1854 undefined references, exclude the stub section
1855 from the final link. */
1857 && blhe->type == bfd_link_hash_defined
1858 && blhe->u.undef.next == NULL
1859 && blhe != link_info.hash->undefs_tail)
1860 stub_sec->flags |= SEC_EXCLUDE;
1870 gld${EMULATION_NAME}_before_allocation (void)
1872 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
1873 /* FIXME: we should be able to set the size of the interworking stub
1876 Here we rummage through the found bfds to collect glue
1877 information. FIXME: should this be based on a command line
1878 option? krk@cygnus.com. */
1880 LANG_FOR_EACH_INPUT_STATEMENT (is)
1882 if (! bfd_arm_process_before_allocation
1883 (is->the_bfd, & link_info, support_old_code))
1885 /* xgettext:c-format */
1886 einfo (_("%P: errors encountered processing file %s for interworking\n"),
1892 /* We have seen it all. Allocate it, and carry on. */
1893 bfd_arm_allocate_interworking_sections (& link_info);
1894 #endif /* TARGET_IS_armpe || TARGET_IS_arm_wince_pe */
1896 before_allocation_default ();
1900 /* This is called when an input file isn't recognized as a BFD. We
1901 check here for .DEF files and pull them in automatically. */
1904 saw_option (char *option)
1908 for (i = 0; init[i].ptr; i++)
1909 if (strcmp (GET_INIT_SYMBOL_NAME (i), option) == 0)
1910 return init[i].inited;
1913 #endif /* DLL_SUPPORT */
1916 gld${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1919 const char *ext = strrchr (entry->filename, '.');
1921 if (ext != NULL && fileext_cmp (ext + 1, "def") == 0)
1923 pe_def_file = def_file_parse (entry->filename, pe_def_file);
1927 int i, buflen=0, len;
1930 for (i = 0; i < pe_def_file->num_exports; i++)
1932 len = strlen (pe_def_file->exports[i].internal_name);
1933 if (buflen < len + 2)
1937 buf = xmalloc (buflen);
1939 for (i = 0; i < pe_def_file->num_exports; i++)
1941 struct bfd_link_hash_entry *h;
1943 sprintf (buf, "%s%s", U (""),
1944 pe_def_file->exports[i].internal_name);
1946 h = bfd_link_hash_lookup (link_info.hash, buf, true, true, true);
1947 if (h == (struct bfd_link_hash_entry *) NULL)
1948 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
1949 if (h->type == bfd_link_hash_new)
1951 h->type = bfd_link_hash_undefined;
1952 h->u.undef.abfd = NULL;
1953 bfd_link_add_undef (link_info.hash, h);
1958 /* def_file_print (stdout, pe_def_file); */
1959 if (pe_def_file->is_dll == 1)
1960 link_info.type = type_dll;
1962 if (pe_def_file->base_address != (bfd_vma)(-1))
1965 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1966 = init[IMAGEBASEOFF].value
1967 = pe_def_file->base_address;
1968 init[IMAGEBASEOFF].inited = 1;
1969 if (image_base_statement)
1970 image_base_statement->exp
1971 = exp_assign ("__image_base__", exp_intop (pe.ImageBase),
1975 if (pe_def_file->stack_reserve != -1
1976 && ! saw_option ("__size_of_stack_reserve__"))
1978 pe.SizeOfStackReserve = pe_def_file->stack_reserve;
1979 if (pe_def_file->stack_commit != -1)
1980 pe.SizeOfStackCommit = pe_def_file->stack_commit;
1982 if (pe_def_file->heap_reserve != -1
1983 && ! saw_option ("__size_of_heap_reserve__"))
1985 pe.SizeOfHeapReserve = pe_def_file->heap_reserve;
1986 if (pe_def_file->heap_commit != -1)
1987 pe.SizeOfHeapCommit = pe_def_file->heap_commit;
1997 gld${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
2000 #ifdef TARGET_IS_i386pe
2001 pe_dll_id_target ("pei-i386");
2003 #ifdef TARGET_IS_shpe
2004 pe_dll_id_target ("pei-shl");
2006 #ifdef TARGET_IS_armpe
2007 pe_dll_id_target ("pei-arm-little");
2009 #ifdef TARGET_IS_arm_wince_pe
2010 pe_dll_id_target ("pei-arm-wince-little");
2012 if (pe_bfd_is_dll (entry->the_bfd))
2013 return pe_implied_import_dll (entry->filename);
2019 gld${EMULATION_NAME}_finish (void)
2021 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
2022 struct bfd_link_hash_entry * h;
2024 if (thumb_entry_symbol != NULL)
2026 h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol,
2027 false, false, true);
2029 if (h != (struct bfd_link_hash_entry *) NULL
2030 && (h->type == bfd_link_hash_defined
2031 || h->type == bfd_link_hash_defweak)
2032 && h->u.def.section->output_section != NULL)
2034 static char buffer[32];
2037 /* Special procesing is required for a Thumb entry symbol. The
2038 bottom bit of its address must be set. */
2039 val = (h->u.def.value
2040 + bfd_section_vma (h->u.def.section->output_section)
2041 + h->u.def.section->output_offset);
2045 /* Now convert this value into a string and store it in entry_symbol
2046 where the lang_finish() function will pick it up. */
2047 sprintf (buffer, "0x%" PRIx64, (uint64_t) val);
2049 if (entry_symbol.name != NULL && entry_from_cmdline)
2050 einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
2051 thumb_entry_symbol, entry_symbol.name);
2052 entry_symbol.name = buffer;
2055 einfo (_("%P: warning: cannot find thumb start symbol %s\n"), thumb_entry_symbol);
2057 #endif /* defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe) */
2062 if (bfd_link_pic (&link_info)
2063 #if !defined(TARGET_IS_shpe)
2064 || pe_dll_enable_reloc_section
2065 || (!bfd_link_relocatable (&link_info)
2066 && pe_def_file->num_exports != 0)
2070 pe_dll_fill_sections (link_info.output_bfd, &link_info);
2071 if (command_line.out_implib_filename
2072 && (pe_def_file->num_exports != 0
2073 || bfd_link_pic (&link_info)))
2074 pe_dll_generate_implib (pe_def_file, command_line.out_implib_filename,
2077 #if defined(TARGET_IS_shpe)
2078 /* ARM doesn't need relocs. */
2081 pe_exe_fill_sections (link_info.output_bfd, &link_info);
2085 if (pe_out_def_filename)
2086 pe_dll_generate_def_file (pe_out_def_filename);
2087 #endif /* DLL_SUPPORT */
2089 /* I don't know where .idata gets set as code, but it shouldn't be. */
2091 asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
2095 asec->flags &= ~SEC_CODE;
2096 asec->flags |= SEC_DATA;
2102 /* Place an orphan section.
2104 We use this to put sections in a reasonable place in the file, and
2105 to ensure that they are aligned as required.
2107 We handle grouped sections here as well. A section named .foo\$nn
2108 goes into the output section .foo. All grouped sections are sorted
2111 Grouped sections for the default sections are handled by the
2112 default linker script using wildcards, and are sorted by
2115 static lang_output_section_statement_type *
2116 gld${EMULATION_NAME}_place_orphan (asection *s,
2117 const char *secname,
2120 const char *orig_secname = secname;
2121 char *dollar = NULL;
2122 lang_output_section_statement_type *os;
2123 lang_statement_list_type add_child;
2124 lang_output_section_statement_type *match_by_name = NULL;
2125 lang_statement_union_type **pl;
2127 /* Look through the script to see where to place this section. */
2128 if (!bfd_link_relocatable (&link_info)
2129 && (dollar = strchr (secname, '\$')) != NULL)
2131 size_t len = dollar - secname;
2132 char *newname = xmalloc (len + 1);
2133 memcpy (newname, secname, len);
2134 newname[len] = '\0';
2138 lang_list_init (&add_child);
2141 if (constraint == 0)
2142 for (os = lang_output_section_find (secname);
2144 os = next_matching_output_section_statement (os, 0))
2146 /* If we don't match an existing output section, tell
2147 lang_insert_orphan to create a new output section. */
2148 constraint = SPECIAL;
2150 if (os->bfd_section != NULL
2151 && (os->bfd_section->flags == 0
2152 || ((s->flags ^ os->bfd_section->flags)
2153 & (SEC_LOAD | SEC_ALLOC)) == 0))
2155 /* We already have an output section statement with this
2156 name, and its bfd section has compatible flags.
2157 If the section already exists but does not have any flags set,
2158 then it has been created by the linker, probably as a result of
2159 a --section-start command line switch. */
2160 lang_add_section (&add_child, s, NULL, NULL, os);
2164 /* Save unused output sections in case we can match them
2165 against orphans later. */
2166 if (os->bfd_section == NULL)
2170 /* If we didn't match an active output section, see if we matched an
2171 unused one and use that. */
2172 if (os == NULL && match_by_name)
2174 lang_add_section (&match_by_name->children, s, NULL, NULL, match_by_name);
2175 return match_by_name;
2180 static struct orphan_save hold[] =
2183 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
2186 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
2189 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
2192 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
2198 enum orphan_save_index
2206 static int orphan_init_done = 0;
2207 struct orphan_save *place;
2208 lang_output_section_statement_type *after;
2209 etree_type *address;
2213 if (!orphan_init_done)
2215 struct orphan_save *ho;
2216 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
2217 if (ho->name != NULL)
2219 ho->os = lang_output_section_find (ho->name);
2220 if (ho->os != NULL && ho->os->flags == 0)
2221 ho->os->flags = ho->flags;
2223 orphan_init_done = 1;
2227 if (!bfd_link_relocatable (&link_info))
2230 while ((nexts = bfd_get_next_section_by_name (nexts->owner,
2232 if (nexts->output_section == NULL
2233 && (nexts->flags & SEC_EXCLUDE) == 0
2234 && ((nexts->flags ^ flags) & (SEC_LOAD | SEC_ALLOC)) == 0
2235 && (nexts->owner->flags & DYNAMIC) == 0
2236 && !bfd_input_just_syms (nexts->owner))
2237 flags = (((flags ^ SEC_READONLY)
2238 | (nexts->flags ^ SEC_READONLY))
2242 /* Try to put the new output section in a reasonable place based
2243 on the section name and section flags. */
2246 if ((flags & SEC_ALLOC) == 0)
2248 else if ((flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2249 place = &hold[orphan_bss];
2250 else if ((flags & SEC_READONLY) == 0)
2251 place = &hold[orphan_data];
2252 else if ((flags & SEC_CODE) == 0)
2254 place = (!strncmp (secname, ".idata\$", 7) ? &hold[orphan_idata]
2255 : &hold[orphan_rodata]);
2258 place = &hold[orphan_text];
2263 if (place->os == NULL)
2264 place->os = lang_output_section_find (place->name);
2267 after = lang_output_section_find_by_flags (s, flags, &place->os,
2270 /* *ABS* is always the first output section statement. */
2271 after = (void *) lang_os_list.head;
2274 /* All sections in an executable must be aligned to a page boundary.
2275 In a relocatable link, just preserve the incoming alignment; the
2276 address is discarded by lang_insert_orphan in that case, anyway. */
2277 address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
2278 os = lang_insert_orphan (s, secname, constraint, after, place, address,
2280 if (bfd_link_relocatable (&link_info))
2282 os->section_alignment = exp_intop (1U << s->alignment_power);
2283 os->bfd_section->alignment_power = s->alignment_power;
2287 /* If the section name has a '\$', sort it with the other '\$'
2289 for (pl = &os->children.head; *pl != NULL; pl = &(*pl)->header.next)
2291 lang_input_section_type *ls;
2294 if ((*pl)->header.type != lang_input_section_enum)
2297 ls = &(*pl)->input_section;
2299 lname = bfd_section_name (ls->section);
2300 if (strchr (lname, '\$') != NULL
2301 && (dollar == NULL || strcmp (orig_secname, lname) < 0))
2305 if (add_child.head != NULL)
2307 *add_child.tail = *pl;
2308 *pl = add_child.head;
2315 gld${EMULATION_NAME}_open_dynamic_archive
2316 (const char *arch ATTRIBUTE_UNUSED,
2317 search_dirs_type *search,
2318 lang_input_statement_type *entry)
2322 const char * format;
2327 /* Preferred explicit import library for dll's. */
2328 { "lib%s.dll.a", false },
2329 /* Alternate explicit import library for dll's. */
2330 { "%s.dll.a", false },
2331 /* "libfoo.a" could be either an import lib or a static lib.
2332 For backwards compatibility, libfoo.a needs to precede
2333 libfoo.dll and foo.dll in the search. */
2334 { "lib%s.a", false },
2335 /* The 'native' spelling of an import lib name is "foo.lib". */
2336 { "%s.lib", false },
2337 /* PR 22948 - Check for an import library. */
2338 { "lib%s.lib", false },
2340 /* Try "<prefix>foo.dll" (preferred dll name, if specified). */
2341 { "%s%s.dll", true },
2343 /* Try "libfoo.dll" (default preferred dll name). */
2344 { "lib%s.dll", false },
2345 /* Finally try 'native' dll name "foo.dll". */
2346 { "%s.dll", false },
2347 /* Note: If adding more formats to this table, make sure to check to
2348 see if their length is longer than libname_fmt[0].format, and if
2349 so, update the call to xmalloc() below. */
2352 static unsigned int format_max_len = 0;
2353 const char * filename;
2359 if (! entry->flags.maybe_archive || entry->flags.full_name_provided)
2362 filename = entry->filename;
2364 if (format_max_len == 0)
2365 /* We need to allow space in the memory that we are going to allocate
2366 for the characters in the format string. Since the format array is
2367 static we only need to calculate this information once. In theory
2368 this value could also be computed statically, but this introduces
2369 the possibility for a discrepancy and hence a possible memory
2370 corruption. The lengths we compute here will be too long because
2371 they will include any formating characters (%s) in the strings, but
2372 this will not matter. */
2373 for (i = 0; libname_fmt[i].format; i++)
2374 if (format_max_len < strlen (libname_fmt[i].format))
2375 format_max_len = strlen (libname_fmt[i].format);
2377 full_string = xmalloc (strlen (search->name)
2381 + (pe_dll_search_prefix
2382 ? strlen (pe_dll_search_prefix) : 0)
2384 /* Allow for the terminating NUL and for the path
2385 separator character that is inserted between
2386 search->name and the start of the format string. */
2389 base_string = stpcpy (full_string, search->name);
2390 *base_string++ = '/';
2392 for (i = 0; libname_fmt[i].format; i++)
2395 if (libname_fmt[i].use_prefix)
2397 if (!pe_dll_search_prefix)
2399 sprintf (base_string, libname_fmt[i].format, pe_dll_search_prefix, filename);
2403 sprintf (base_string, libname_fmt[i].format, filename);
2405 if (ldfile_try_open_bfd (full_string, entry))
2409 if (!libname_fmt[i].format)
2415 entry->filename = full_string;
2421 gld${EMULATION_NAME}_find_potential_libraries
2422 (char *name, lang_input_statement_type *entry)
2424 return ldfile_open_file_search (name, entry, "", ".lib");
2428 gld${EMULATION_NAME}_get_script (int *isfile)
2431 if test x"$COMPILE_IN" = xyes
2433 # Scripts compiled in.
2435 # sed commands to quote an ld script as a C string.
2436 sc="-f ${srcdir}/emultempl/stringify.sed"
2442 if (bfd_link_relocatable (&link_info) && config.build_constructors)
2445 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
2446 echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
2447 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
2448 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
2449 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
2450 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
2451 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
2452 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
2453 echo ' ; else if (link_info.pei386_auto_import == 1 && (MERGE_RDATA_V2 || link_info.pei386_runtime_pseudo_reloc != 2)) return' >> e${EMULATION_NAME}.c
2454 sed $sc ldscripts/${EMULATION_NAME}.xa >> e${EMULATION_NAME}.c
2456 echo ' ; else return' >> e${EMULATION_NAME}.c
2457 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
2458 echo '; }' >> e${EMULATION_NAME}.c
2461 # Scripts read from the filesystem.
2467 if (bfd_link_relocatable (&link_info) && config.build_constructors)
2468 return "ldscripts/${EMULATION_NAME}.xu";
2469 else if (bfd_link_relocatable (&link_info))
2470 return "ldscripts/${EMULATION_NAME}.xr";
2471 else if (!config.text_read_only)
2472 return "ldscripts/${EMULATION_NAME}.xbn";
2473 else if (!config.magic_demand_paged)
2474 return "ldscripts/${EMULATION_NAME}.xn";
2476 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
2478 else if (link_info.pei386_auto_import == 1
2479 && (MERGE_RDATA_V2 || link_info.pei386_runtime_pseudo_reloc != 2))
2480 return "ldscripts/${EMULATION_NAME}.xa";
2485 return "ldscripts/${EMULATION_NAME}.x";
2490 LDEMUL_AFTER_PARSE=gld${EMULATION_NAME}_after_parse
2491 LDEMUL_BEFORE_PLUGIN_ALL_SYMBOLS_READ=gld${EMULATION_NAME}_before_plugin_all_symbols_read
2492 LDEMUL_AFTER_OPEN=gld${EMULATION_NAME}_after_open
2493 LDEMUL_BEFORE_ALLOCATION=gld${EMULATION_NAME}_before_allocation
2494 LDEMUL_FINISH=gld${EMULATION_NAME}_finish
2495 LDEMUL_OPEN_DYNAMIC_ARCHIVE=gld${EMULATION_NAME}_open_dynamic_archive
2496 LDEMUL_PLACE_ORPHAN=gld${EMULATION_NAME}_place_orphan
2497 LDEMUL_SET_SYMBOLS=gld${EMULATION_NAME}_set_symbols
2498 LDEMUL_ADD_OPTIONS=gld${EMULATION_NAME}_add_options
2499 LDEMUL_HANDLE_OPTION=gld${EMULATION_NAME}_handle_option
2500 LDEMUL_UNRECOGNIZED_FILE=gld${EMULATION_NAME}_unrecognized_file
2501 LDEMUL_LIST_OPTIONS=gld${EMULATION_NAME}_list_options
2502 LDEMUL_RECOGNIZED_FILE=gld${EMULATION_NAME}_recognized_file
2503 LDEMUL_FIND_POTENTIAL_LIBRARIES=gld${EMULATION_NAME}_find_potential_libraries
2505 source_em ${srcdir}/emultempl/emulation.em