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-2025 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 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
255 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
256 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
257 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
258 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
259 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
260 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
261 {"stack", required_argument, NULL, OPTION_STACK},
262 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
263 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
264 {"thumb-entry", required_argument, NULL, OPTION_THUMB_ENTRY},
265 {"use-nul-prefixed-import-tables", no_argument, NULL,
266 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
267 {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
268 {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE},
269 {"insert-timestamp", no_argument, NULL, OPTION_INSERT_TIMESTAMP},
270 {"no-insert-timestamp", no_argument, NULL, OPTION_NO_INSERT_TIMESTAMP},
272 /* getopt allows abbreviations, so we do this to stop it
273 from treating -o as an abbreviation for this option. */
274 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
275 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
276 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
277 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
278 {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS},
279 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
280 {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
281 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
282 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
283 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
284 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
285 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
286 /* getopt() allows abbreviations, so we do this to stop it from
287 treating -c as an abbreviation for these --compat-implib. */
288 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
289 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
290 {"enable-auto-image-base", optional_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
291 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
292 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
293 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
294 {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
295 {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
296 {"enable-extra-pe-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
297 {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
298 {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
299 {"enable-runtime-pseudo-reloc-v1", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1},
300 {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2},
302 {"large-address-aware", no_argument, NULL, OPTION_LARGE_ADDRESS_AWARE},
303 {"disable-large-address-aware", no_argument, NULL, OPTION_DISABLE_LARGE_ADDRESS_AWARE},
304 {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
305 {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
306 {"dynamicbase",no_argument, NULL, OPTION_DYNAMIC_BASE},
307 {"disable-dynamicbase",no_argument, NULL, OPTION_DISABLE_DYNAMIC_BASE},
308 {"forceinteg", no_argument, NULL, OPTION_FORCE_INTEGRITY},
309 {"disable-forceinteg", no_argument, NULL, OPTION_DISABLE_FORCE_INTEGRITY},
310 {"nxcompat", no_argument, NULL, OPTION_NX_COMPAT},
311 {"disable-nxcompat", no_argument, NULL, OPTION_DISABLE_NX_COMPAT},
312 {"no-isolation", no_argument, NULL, OPTION_NO_ISOLATION},
313 {"disable-no-isolation", no_argument, NULL, OPTION_DISABLE_NO_ISOLATION},
314 {"no-seh", no_argument, NULL, OPTION_NO_SEH},
315 {"disable-no-seh", no_argument, NULL, OPTION_DISABLE_NO_SEH},
316 {"no-bind", no_argument, NULL, OPTION_NO_BIND},
317 {"disable-no-bind", no_argument, NULL, OPTION_DISABLE_NO_BIND},
318 {"wdmdriver", no_argument, NULL, OPTION_WDM_DRIVER},
319 {"disable-wdmdriver", no_argument, NULL, OPTION_DISABLE_WDM_DRIVER},
320 {"tsaware", no_argument, NULL, OPTION_TERMINAL_SERVER_AWARE},
321 {"disable-tsaware", no_argument, NULL, OPTION_DISABLE_TERMINAL_SERVER_AWARE},
322 {"build-id", optional_argument, NULL, OPTION_BUILD_ID},
324 {"pdb", required_argument, NULL, OPTION_PDB},
326 {"enable-reloc-section", no_argument, NULL, OPTION_ENABLE_RELOC_SECTION},
327 {"disable-reloc-section", no_argument, NULL, OPTION_DISABLE_RELOC_SECTION},
328 {NULL, no_argument, NULL, 0}
332 = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
333 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
336 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
337 parameters which may be input from the command line. */
346 /* FALSE for an assembly level symbol and TRUE for a C visible symbol.
347 C visible symbols can be prefixed by underscore dependent to target's
352 /* Get symbol name dependent to kind and C visible state of
354 #define GET_INIT_SYMBOL_NAME(IDX) \
355 (init[(IDX)].symbol \
356 + ((!init[(IDX)].is_c_symbol || is_underscoring () != 0) ? 0 : 1))
358 /* Decorates the C visible symbol by underscore, if target requires. */
360 ((is_underscoring () == 0) ? CSTR : "_" CSTR)
362 #define D(field,symbol,def,usc) {&pe.field, sizeof (pe.field), def, symbol, 0, usc}
364 static definfo init[] =
366 /* imagebase must be first */
367 #define IMAGEBASEOFF 0
368 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE, false),
370 {&dll, sizeof(dll), 0, "__dll__", 0, false},
371 #define MSIMAGEBASEOFF 2
372 D(ImageBase, "___ImageBase", NT_EXE_IMAGE_BASE, true),
373 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT, false),
374 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT, false),
375 D(MajorOperatingSystemVersion,"__major_os_version__", 4, false),
376 D(MinorOperatingSystemVersion,"__minor_os_version__", 0, false),
377 D(MajorImageVersion,"__major_image_version__", 1, false),
378 D(MinorImageVersion,"__minor_image_version__", 0, false),
379 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
380 D(MajorSubsystemVersion,"__major_subsystem_version__", 3, false),
382 D(MajorSubsystemVersion,"__major_subsystem_version__", 4, false),
384 D(MinorSubsystemVersion,"__minor_subsystem_version__", 0, false),
385 D(Subsystem,"__subsystem__", ${SUBSYSTEM}, false),
386 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000, false),
387 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000, false),
388 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000, false),
389 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000, false),
390 D(LoaderFlags,"__loader_flags__", 0x0, false),
391 D(DllCharacteristics, "__dll_characteristics__", DEFAULT_DLL_CHARACTERISTICS, false),
392 { NULL, 0, 0, NULL, 0 , false}
397 gld${EMULATION_NAME}_list_options (FILE *file)
399 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
400 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
401 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
402 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
403 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
404 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
405 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
406 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
407 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
408 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
409 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
410 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
411 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
412 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
413 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
414 fprintf (file, _(" --[no-]leading-underscore Set explicit symbol underscore prefix mode\n"));
415 fprintf (file, _(" --thumb-entry=<symbol> Set the entry point to be Thumb <symbol>\n"));
416 fprintf (file, _(" --[no-]insert-timestamp Use a real timestamp rather than zero (default).\n"));
417 fprintf (file, _(" This makes binaries non-deterministic\n"));
419 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
420 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
421 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
422 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
423 fprintf (file, _(" --exclude-all-symbols Exclude all symbols from automatic export\n"));
424 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
425 fprintf (file, _(" --exclude-modules-for-implib mod,mod,...\n"));
426 fprintf (file, _(" Exclude objects, archive members from auto\n"));
427 fprintf (file, _(" export, place into import library instead.\n"));
428 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
429 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
430 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
431 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports\n"));
432 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
433 create __imp_<SYMBOL> as well.\n"));
434 fprintf (file, _(" --enable-auto-image-base[=<address>] Automatically choose image base for DLLs\n\
435 (optionally starting with address) unless\n\
436 specifically set with --image-base\n"));
437 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
438 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
439 an importlib, use <string><basename>.dll\n\
440 in preference to lib<basename>.dll \n"));
441 fprintf (file, _(" --enable-auto-import Do sophisticated linking of _sym to\n\
442 __imp_sym for DATA references\n"));
443 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
444 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
445 adding pseudo-relocations resolved at\n\
447 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
448 auto-imported DATA.\n"));
449 fprintf (file, _(" --enable-extra-pe-debug Enable verbose debug output when building\n\
450 or linking to DLLs (esp. auto-import)\n"));
452 fprintf (file, _(" --large-address-aware Executable supports virtual addresses\n\
453 greater than 2 gigabytes\n"));
454 fprintf (file, _(" --disable-large-address-aware Executable does not support virtual\n\
455 addresses greater than 2 gigabytes\n"));
456 fprintf (file, _(" --enable-long-section-names Use long COFF section names even in\n\
457 executable image files\n"));
458 fprintf (file, _(" --disable-long-section-names Never use long COFF section names, even\n\
459 in object files\n"));
460 fprintf (file, _(" --[disable-]dynamicbase Image base address may be relocated using\n\
461 address space layout randomization (ASLR)\n"));
462 fprintf (file, _(" --enable-reloc-section Create the base relocation table\n"));
463 fprintf (file, _(" --disable-reloc-section Do not create the base relocation table\n"));
464 fprintf (file, _(" --[disable-]forceinteg Code integrity checks are enforced\n"));
465 fprintf (file, _(" --[disable-]nxcompat Image is compatible with data execution\n\
467 fprintf (file, _(" --[disable-]no-isolation Image understands isolation but do not\n\
468 isolate the image\n"));
469 fprintf (file, _(" --[disable-]no-seh Image does not use SEH. No SE handler may\n\
470 be called in this image\n"));
471 fprintf (file, _(" --[disable-]no-bind Do not bind this image\n"));
472 fprintf (file, _(" --[disable-]wdmdriver Driver uses the WDM model\n"));
473 fprintf (file, _(" --[disable-]tsaware Image is Terminal Server aware\n"));
474 fprintf (file, _(" --build-id[=STYLE] Generate build ID\n"));
476 fprintf (file, _(" --pdb=[FILENAME] Generate PDB file\n"));
480 /* A case insensitive comparison, regardless of the host platform, used for
481 comparing file extensions. */
483 fileext_cmp (const char *s1, const char *s2)
487 int c1 = TOLOWER (*s1++);
488 int c2 = *s2++; /* Assumed to be lower case from the caller. */
499 set_pe_name (char *name, long val)
504 /* Find the name and set it. */
505 for (i = 0; init[i].ptr; i++)
507 if (strcmp (name, GET_INIT_SYMBOL_NAME (i)) == 0)
511 if (strcmp (name,"__image_base__") == 0)
512 set_pe_name (U ("__ImageBase"), val);
520 set_entry_point (void)
523 const char *initial_symbol_char;
533 { 1, "NtProcessStartup" },
534 { 2, "WinMainCRTStartup" },
535 { 3, "mainCRTStartup" },
536 { 7, "__PosixProcessStartup"},
537 { 9, "WinMainCRTStartup" },
538 {14, "mainCRTStartup" },
542 /* Entry point name for arbitrary subsystem numbers. */
543 static const char default_entry[] = "mainCRTStartup";
545 if (bfd_link_dll (&link_info) || dll)
547 #if defined (TARGET_IS_i386pe)
548 entry = "DllMainCRTStartup@12";
550 entry = "DllMainCRTStartup";
555 for (i = 0; v[i].entry; i++)
556 if (v[i].value == pe_subsystem)
559 /* If no match, use the default. */
560 if (v[i].entry != NULL)
563 entry = default_entry;
566 initial_symbol_char = (is_underscoring () != 0 ? "_" : "");
568 if (*initial_symbol_char != '\0')
572 /* lang_default_entry expects its argument to be permanently
573 allocated, so we don't free this string. */
574 alc_entry = xmalloc (strlen (initial_symbol_char)
577 strcpy (alc_entry, initial_symbol_char);
578 strcat (alc_entry, entry);
582 lang_default_entry (entry);
584 if (bfd_link_executable (&link_info) && ! entry_from_cmdline)
585 ldlang_add_undef (entry, false);
589 set_pe_subsystem (void)
595 unsigned long temp_subsystem;
612 /* Check for the presence of a version number. */
613 sver = strchr (optarg, ':');
615 len = strlen (optarg);
619 set_pe_name ("__major_subsystem_version__",
620 strtoul (sver + 1, &end, 0));
622 set_pe_name ("__minor_subsystem_version__",
623 strtoul (end + 1, &end, 0));
625 einfo (_("%P: warning: bad version number in -subsystem option\n"));
628 /* Check for numeric subsystem. */
629 temp_subsystem = strtoul (optarg, & end, 0);
630 if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
632 /* Search list for a numeric match to use its entry point. */
633 for (i = 0; v[i].name; i++)
634 if (v[i].value == (int) temp_subsystem)
637 /* Use this subsystem. */
638 pe_subsystem = (int) temp_subsystem;
642 /* Search for subsystem by name. */
643 for (i = 0; v[i].name; i++)
644 if (strncmp (optarg, v[i].name, len) == 0
645 && v[i].name[len] == '\0')
648 if (v[i].name == NULL)
650 einfo (_("%F%P: invalid subsystem type %s\n"), optarg);
654 pe_subsystem = v[i].value;
657 set_pe_name ("__subsystem__", pe_subsystem);
664 set_pe_value (char *name)
668 set_pe_name (name, strtoul (optarg, &end, 0));
671 einfo (_("%F%P: invalid hex number for PE parameter '%s'\n"), optarg);
678 set_pe_stack_heap (char *resname, char *comname)
680 set_pe_value (resname);
685 set_pe_value (comname);
688 einfo (_("%F%P: strange hex info for PE parameter '%s'\n"), optarg);
691 #define DEFAULT_BUILD_ID_STYLE "md5"
694 gld${EMULATION_NAME}_handle_option (int optc)
701 case OPTION_BASE_FILE:
702 link_info.base_file = fopen (optarg, FOPEN_WB);
703 if (link_info.base_file == NULL)
704 einfo (_("%F%P: cannot open base file %s\n"), optarg);
709 set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
712 set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
714 case OPTION_SUBSYSTEM:
717 case OPTION_MAJOR_OS_VERSION:
718 set_pe_value ("__major_os_version__");
720 case OPTION_MINOR_OS_VERSION:
721 set_pe_value ("__minor_os_version__");
723 case OPTION_MAJOR_SUBSYSTEM_VERSION:
724 set_pe_value ("__major_subsystem_version__");
726 case OPTION_MINOR_SUBSYSTEM_VERSION:
727 set_pe_value ("__minor_subsystem_version__");
729 case OPTION_MAJOR_IMAGE_VERSION:
730 set_pe_value ("__major_image_version__");
732 case OPTION_MINOR_IMAGE_VERSION:
733 set_pe_value ("__minor_image_version__");
735 case OPTION_FILE_ALIGNMENT:
736 set_pe_value ("__file_alignment__");
738 case OPTION_SECTION_ALIGNMENT:
739 set_pe_value ("__section_alignment__");
742 set_pe_name ("__dll__", 1);
744 case OPTION_IMAGE_BASE:
745 set_pe_value ("__image_base__");
747 case OPTION_SUPPORT_OLD_CODE:
748 support_old_code = 1;
750 case OPTION_THUMB_ENTRY:
751 thumb_entry_symbol = optarg;
753 case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
754 pe_use_nul_prefixed_import_tables = true;
756 case OPTION_NO_LEADING_UNDERSCORE:
757 pe_leading_underscore = 0;
759 case OPTION_LEADING_UNDERSCORE:
760 pe_leading_underscore = 1;
762 case OPTION_INSERT_TIMESTAMP:
763 insert_timestamp = true;
765 case OPTION_NO_INSERT_TIMESTAMP:
766 insert_timestamp = false;
770 pe_out_def_filename = xstrdup (optarg);
772 case OPTION_EXPORT_ALL:
773 pe_dll_export_everything = 1;
775 case OPTION_EXCLUDE_SYMBOLS:
776 pe_dll_add_excludes (optarg, EXCLUDESYMS);
778 case OPTION_EXCLUDE_ALL_SYMBOLS:
779 pe_dll_exclude_all_symbols = 1;
781 case OPTION_EXCLUDE_LIBS:
782 pe_dll_add_excludes (optarg, EXCLUDELIBS);
784 case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
785 pe_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
787 case OPTION_KILL_ATS:
790 case OPTION_STDCALL_ALIASES:
791 pe_dll_stdcall_aliases = 1;
793 case OPTION_ENABLE_STDCALL_FIXUP:
794 pe_enable_stdcall_fixup = 1;
796 case OPTION_DISABLE_STDCALL_FIXUP:
797 pe_enable_stdcall_fixup = 0;
799 case OPTION_WARN_DUPLICATE_EXPORTS:
800 pe_dll_warn_dup_exports = 1;
802 case OPTION_IMP_COMPAT:
803 pe_dll_compat_implib = 1;
805 case OPTION_ENABLE_AUTO_IMAGE_BASE:
806 pe_enable_auto_image_base = 1;
807 if (optarg && *optarg)
810 pe_auto_image_base = strtoul (optarg, &end, 0);
811 /* XXX should check that we actually parsed something */
814 case OPTION_DISABLE_AUTO_IMAGE_BASE:
815 pe_enable_auto_image_base = 0;
817 case OPTION_DLL_SEARCH_PREFIX:
818 pe_dll_search_prefix = xstrdup (optarg);
820 case OPTION_NO_DEFAULT_EXCLUDES:
821 pe_dll_do_default_excludes = 0;
823 case OPTION_DLL_ENABLE_AUTO_IMPORT:
824 link_info.pei386_auto_import = 1;
826 case OPTION_DLL_DISABLE_AUTO_IMPORT:
827 link_info.pei386_auto_import = 0;
829 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
830 link_info.pei386_runtime_pseudo_reloc =
831 DEFAULT_PSEUDO_RELOC_VERSION;
833 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1:
834 link_info.pei386_runtime_pseudo_reloc = 1;
836 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2:
837 link_info.pei386_runtime_pseudo_reloc = 2;
839 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
840 link_info.pei386_runtime_pseudo_reloc = 0;
842 case OPTION_ENABLE_EXTRA_PE_DEBUG:
843 pe_dll_extra_pe_debug = 1;
846 case OPTION_LARGE_ADDRESS_AWARE:
847 real_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
849 case OPTION_DISABLE_LARGE_ADDRESS_AWARE:
850 real_flags &= ~ IMAGE_FILE_LARGE_ADDRESS_AWARE;
852 case OPTION_ENABLE_LONG_SECTION_NAMES:
853 pe_use_coff_long_section_names = 1;
855 case OPTION_DISABLE_LONG_SECTION_NAMES:
856 pe_use_coff_long_section_names = 0;
858 /* Get DLLCharacteristics bits */
859 case OPTION_DYNAMIC_BASE:
860 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
862 case OPTION_ENABLE_RELOC_SECTION:
863 pe_dll_enable_reloc_section = 1;
865 case OPTION_DISABLE_RELOC_SECTION:
866 pe_dll_enable_reloc_section = 0;
868 case OPTION_DISABLE_DYNAMIC_BASE:
869 pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
871 case OPTION_FORCE_INTEGRITY:
872 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
874 case OPTION_DISABLE_FORCE_INTEGRITY:
875 pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
877 case OPTION_NX_COMPAT:
878 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
880 case OPTION_DISABLE_NX_COMPAT:
881 pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
883 case OPTION_NO_ISOLATION:
884 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
886 case OPTION_DISABLE_NO_ISOLATION:
887 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
890 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_SEH;
892 case OPTION_DISABLE_NO_SEH:
893 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_SEH;
896 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_BIND;
898 case OPTION_DISABLE_NO_BIND:
899 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_BIND;
901 case OPTION_WDM_DRIVER:
902 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
904 case OPTION_DISABLE_WDM_DRIVER:
905 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
907 case OPTION_TERMINAL_SERVER_AWARE:
908 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
910 case OPTION_DISABLE_TERMINAL_SERVER_AWARE:
911 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
913 case OPTION_BUILD_ID:
914 free ((char *) emit_build_id);
915 emit_build_id = NULL;
917 optarg = DEFAULT_BUILD_ID_STYLE;
918 if (strcmp (optarg, "none"))
919 emit_build_id = xstrdup (optarg);
924 if (optarg && optarg[0])
925 pdb_name = xstrdup (optarg);
930 /* Set DLLCharacteristics bits */
931 set_pe_name ("__dll_characteristics__", pe_dll_characteristics);
939 strhash (const char *str)
941 const unsigned char *s;
948 s = (const unsigned char *) str;
949 while ((c = *s++) != '\0')
951 hash += c + (c << 17);
955 hash += len + (len << 17);
961 /* Use the output file to create a image base for relocatable DLLs. */
964 compute_dll_image_base (const char *ofile)
966 unsigned long hash = strhash (ofile);
967 return pe_auto_image_base + ((hash << 16) & 0x0FFC0000);
971 /* Assign values to the special symbols before the linker script is
975 gld${EMULATION_NAME}_set_symbols (void)
977 /* Run through and invent symbols for all the
978 names and insert the defaults. */
983 if (!init[IMAGEBASEOFF].inited)
985 if (bfd_link_relocatable (&link_info))
986 init[IMAGEBASEOFF].value = 0;
987 else if (init[DLLOFF].value || bfd_link_dll (&link_info))
990 init[IMAGEBASEOFF].value = (pe_enable_auto_image_base
991 ? compute_dll_image_base (output_filename)
992 : NT_DLL_IMAGE_BASE);
994 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
998 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
999 init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
1002 /* Don't do any symbol assignments if this is a relocatable link. */
1003 if (bfd_link_relocatable (&link_info))
1006 /* Glue the assignments into the abs section. */
1007 push_stat_ptr (&abs_output_section->children);
1009 for (j = 0; init[j].ptr; j++)
1011 long val = init[j].value;
1012 lang_assignment_statement_type *rv;
1014 rv = lang_add_assignment (exp_assign (GET_INIT_SYMBOL_NAME (j),
1015 exp_intop (val), false));
1016 if (init[j].size == sizeof (short))
1017 *(short *) init[j].ptr = val;
1018 else if (init[j].size == sizeof (int))
1019 *(int *) init[j].ptr = val;
1020 else if (init[j].size == sizeof (long))
1021 *(long *) init[j].ptr = val;
1022 /* This might be a long long or other special type. */
1023 else if (init[j].size == sizeof (bfd_vma))
1024 *(bfd_vma *) init[j].ptr = val;
1026 if (j == IMAGEBASEOFF)
1027 image_base_statement = rv;
1029 /* Restore the pointer. */
1032 if (pe.FileAlignment > pe.SectionAlignment)
1034 einfo (_("%P: warning, file alignment > section alignment\n"));
1038 /* This is called after the linker script and the command line options
1042 gld${EMULATION_NAME}_after_parse (void)
1044 /* PR ld/6744: Warn the user if they have used an ELF-only
1045 option hoping it will work on PE. */
1046 if (link_info.export_dynamic)
1047 einfo (_("%P: warning: --export-dynamic is not supported for PE "
1048 "targets, did you mean --export-all-symbols?\n"));
1051 if (pdb && emit_build_id == NULL)
1052 emit_build_id = xstrdup (DEFAULT_BUILD_ID_STYLE);
1057 after_parse_default ();
1061 static struct bfd_link_hash_entry *pe_undef_found_sym;
1064 pe_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
1068 const char *hs = h->root.string;
1070 sl = strlen (string);
1071 if (h->type == bfd_link_hash_defined
1072 && ((*hs == '@' && *string == '_'
1073 && strncmp (hs + 1, string + 1, sl - 1) == 0)
1074 || strncmp (hs, string, sl) == 0)
1075 && h->root.string[sl] == '@')
1077 pe_undef_found_sym = h;
1083 /* Change UNDEF to a defined symbol, taking data from SYM. */
1086 change_undef (struct bfd_link_hash_entry * undef,
1087 struct bfd_link_hash_entry * sym)
1089 static bool gave_warning_message = false;
1091 undef->type = bfd_link_hash_defined;
1092 undef->u.def.value = sym->u.def.value;
1093 undef->u.def.section = sym->u.def.section;
1095 if (pe_enable_stdcall_fixup == -1)
1097 einfo (_("%P: warning: resolving %s by linking to %s\n"),
1098 undef->root.string, sym->root.string);
1100 if (! gave_warning_message)
1102 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
1103 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
1104 gave_warning_message = true;
1108 /* PR 19803: Make sure that the linked symbol is not garbage collected. */
1109 lang_add_gc_name (sym->root.string);
1113 set_decoration (const char *undecorated_name,
1114 struct bfd_link_hash_entry * decoration)
1116 static bool gave_warning_message = false;
1117 struct decoration_hash_entry *entry;
1119 if (is_underscoring () && undecorated_name[0] == '_')
1122 entry = (struct decoration_hash_entry *)
1123 bfd_hash_lookup (&(coff_hash_table (&link_info)->decoration_hash),
1124 undecorated_name, true /* create */, false /* copy */);
1126 if (entry->decorated_link != NULL && !gave_warning_message)
1128 einfo (_("%P: warning: overwriting decorated name %s with %s\n"),
1129 entry->decorated_link->root.string, undecorated_name);
1130 gave_warning_message = true;
1133 entry->decorated_link = decoration;
1137 pe_fixup_stdcalls (void)
1139 struct bfd_link_hash_entry *undef, *sym;
1141 if (pe_dll_extra_pe_debug)
1142 printf ("%s\n", __func__);
1144 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
1145 if (undef->type == bfd_link_hash_undefined)
1147 const char * name = undef->root.string;
1149 int lead_at = (*name == '@');
1152 at = strchr (name + 1, '@');
1154 at = strchr (name, '@');
1158 /* The symbol is a stdcall symbol, so let's look for a
1159 cdecl symbol with the same name and resolve to that. */
1160 char *cname = xstrdup (name);
1165 * strchr (cname, '@') = 0;
1166 sym = bfd_link_hash_lookup (link_info.hash, cname, false, false, true);
1168 if (sym && sym->type == bfd_link_hash_defined)
1169 change_undef (undef, sym);
1173 /* The symbol is a cdecl symbol, so we look for stdcall
1174 symbols - which means scanning the whole symbol table. */
1175 pe_undef_found_sym = NULL;
1176 bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
1178 if (pe_undef_found_sym)
1180 change_undef (undef, pe_undef_found_sym);
1181 set_decoration (undef->root.string, pe_undef_found_sym);
1188 read_addend (arelent *rel, asection *s)
1193 if (!bfd_get_section_contents (s->owner, s, buf, rel->address, sizeof (buf)))
1194 einfo (_("%P: %H: cannot get section contents - auto-import exception\n"),
1195 s->owner, s, rel->address);
1197 addend = bfd_get_32 (s->owner, buf);
1202 make_import_fixup (arelent *rel, asection *s, char *name, const char *symname)
1204 struct bfd_symbol *sym = *rel->sym_ptr_ptr;
1207 if (pe_dll_extra_pe_debug)
1208 printf ("arelent: %s@%#lx: add=%li\n", sym->name,
1209 (unsigned long) rel->address, (long) rel->addend);
1211 addend = read_addend (rel, s);
1213 if (pe_dll_extra_pe_debug)
1215 printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx",
1216 (long) addend, (long) rel->addend, (long) rel->address);
1217 if (rel->howto->pc_relative)
1219 printf (" %d bit rel.\n", (int) rel->howto->bitsize);
1222 pe_create_import_fixup (rel, s, addend, name, symname);
1226 make_runtime_ref (void)
1228 const char *rr = U ("_pei386_runtime_relocator");
1229 struct bfd_link_hash_entry *h
1230 = bfd_wrapped_link_hash_lookup (link_info.output_bfd, &link_info,
1231 rr, true, false, true);
1233 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
1236 if (h->type == bfd_link_hash_new)
1238 h->type = bfd_link_hash_undefined;
1239 h->u.undef.abfd = NULL;
1240 if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
1241 bfd_link_add_undef (link_info.hash, h);
1243 h->non_ir_ref_regular = true;
1248 pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
1250 printf ("+%s\n", h->string);
1254 #endif /* DLL_SUPPORT */
1257 debug_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
1259 int *found = (int *) obj;
1260 if (strncmp (".debug_", sect->name, sizeof (".debug_") - 1) == 0)
1265 pecoff_checksum_contents (bfd *abfd,
1266 void (*process) (const void *, size_t, void *),
1269 file_ptr filepos = (file_ptr) 0;
1276 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
1279 status = bfd_read (&b, 1, abfd);
1285 (*process) (&b, 1, arg);
1293 write_build_id (bfd *abfd)
1295 struct pe_tdata *td = pe_data (abfd);
1297 struct bfd_link_order *link_order = NULL;
1298 unsigned char *contents;
1299 bfd_size_type build_id_size;
1300 unsigned char *build_id;
1301 const char *pdb_base_name = NULL;
1303 /* Find the section the .buildid output section has been merged info. */
1304 for (asec = abfd->sections; asec != NULL; asec = asec->next)
1306 struct bfd_link_order *l = NULL;
1307 for (l = asec->map_head.link_order; l != NULL; l = l->next)
1309 if (l->type == bfd_indirect_link_order)
1311 if (l->u.indirect.section == td->build_id.sec)
1325 einfo (_("%P: warning: .buildid section discarded,"
1326 " --build-id ignored\n"));
1330 if (td->build_id.sec->contents == NULL)
1331 td->build_id.sec->contents = xmalloc (td->build_id.sec->size);
1332 contents = td->build_id.sec->contents;
1334 build_id_size = compute_build_id_size (td->build_id.style);
1335 build_id = xmalloc (build_id_size);
1336 generate_build_id (abfd, td->build_id.style, pecoff_checksum_contents,
1337 build_id, build_id_size);
1339 bfd_vma ib = td->pe_opthdr.ImageBase;
1343 pdb_base_name = lbasename (pdb_name);
1346 /* Construct a debug directory entry which points to an immediately following CodeView record. */
1347 struct internal_IMAGE_DEBUG_DIRECTORY idd;
1348 idd.Characteristics = 0;
1349 idd.TimeDateStamp = 0;
1350 idd.MajorVersion = 0;
1351 idd.MinorVersion = 0;
1352 idd.Type = PE_IMAGE_DEBUG_TYPE_CODEVIEW;
1353 idd.SizeOfData = (sizeof (CV_INFO_PDB70)
1355 + (pdb_base_name ? strlen (pdb_base_name) : 0)
1358 idd.AddressOfRawData = asec->vma - ib + link_order->offset
1359 + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1360 idd.PointerToRawData = asec->filepos + link_order->offset
1361 + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1363 struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *)contents;
1364 _bfd_XXi_swap_debugdir_out (abfd, &idd, ext);
1366 /* Write the debug directory entry. */
1367 if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0)
1370 if (bfd_write (contents, sizeof (*ext), abfd) != sizeof (*ext))
1376 if (!create_pdb_file (abfd, pdb_name, build_id))
1381 /* Construct the CodeView record. */
1382 CODEVIEW_INFO cvinfo;
1383 cvinfo.CVSignature = CVINFO_PDB70_CVSIGNATURE;
1386 /* Zero pad or truncate the generated build_id to fit in the
1388 memset (&(cvinfo.Signature), 0, CV_INFO_SIGNATURE_LENGTH);
1389 memcpy (&(cvinfo.Signature), build_id,
1390 (build_id_size > CV_INFO_SIGNATURE_LENGTH
1391 ? CV_INFO_SIGNATURE_LENGTH : build_id_size));
1395 /* Write the codeview record. */
1396 if (_bfd_XXi_write_codeview_record (abfd, idd.PointerToRawData, &cvinfo,
1397 pdb_base_name) == 0)
1400 /* Record the location of the debug directory in the data directory. */
1401 td->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress
1402 = asec->vma - ib + link_order->offset;
1403 td->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
1404 = sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1409 /* Make .buildid section, and set up coff_tdata->build_id. */
1411 setup_build_id (bfd *ibfd)
1416 if (!validate_build_id_style (emit_build_id))
1418 einfo (_("%P: warning: unrecognized --build-id style ignored\n"));
1422 flags = (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY
1423 | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA);
1424 s = bfd_make_section_anyway_with_flags (ibfd, ".buildid", flags);
1427 struct pe_tdata *td = pe_data (link_info.output_bfd);
1428 td->build_id.after_write_object_contents = &write_build_id;
1429 td->build_id.style = emit_build_id;
1430 td->build_id.sec = s;
1432 /* Section is a fixed size:
1433 One IMAGE_DEBUG_DIRECTORY entry, of type IMAGE_DEBUG_TYPE_CODEVIEW,
1434 pointing at a CV_INFO_PDB70 record containing the build-id, followed by
1435 PdbFileName if relevant. */
1436 s->size = (sizeof (struct external_IMAGE_DEBUG_DIRECTORY)
1437 + sizeof (CV_INFO_PDB70) + 1);
1441 s->size += strlen (lbasename (pdb_name));
1446 einfo (_("%P: warning: cannot create .buildid section,"
1447 " --build-id ignored\n"));
1452 gld${EMULATION_NAME}_before_plugin_all_symbols_read (void)
1455 if (link_info.lto_plugin_active
1456 && link_info.pei386_auto_import)
1457 make_runtime_ref ();
1462 gld${EMULATION_NAME}_after_open (void)
1464 after_open_default ();
1467 if (pe_dll_extra_pe_debug)
1470 struct bfd_link_hash_entry *sym;
1472 printf ("%s()\n", __func__);
1474 for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1475 printf ("-%s\n", sym->root.string);
1476 bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
1478 for (a = link_info.input_bfds; a; a = a->link.next)
1479 printf ("*%s\n", bfd_get_filename (a));
1484 if (pdb && !pdb_name)
1486 const char *base = lbasename (bfd_get_filename (link_info.output_bfd));
1487 size_t len = strlen (base);
1488 static const char suffix[] = ".pdb";
1490 while (len > 0 && base[len] != '.')
1496 len = strlen (base);
1498 pdb_name = xmalloc (len + sizeof (suffix));
1499 memcpy (pdb_name, base, len);
1500 memcpy (pdb_name + len, suffix, sizeof (suffix));
1504 if (emit_build_id != NULL)
1508 /* Find a COFF input. */
1509 for (abfd = link_info.input_bfds;
1510 abfd != (bfd *) NULL; abfd = abfd->link.next)
1511 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
1514 /* If there are no COFF input files do not try to
1515 add a build-id section. */
1517 || !setup_build_id (abfd))
1519 free ((char *) emit_build_id);
1520 emit_build_id = NULL;
1524 /* Pass the wacky PE command line options into the output bfd.
1525 FIXME: This should be done via a function, rather than by
1526 including an internal BFD header. */
1528 if (bfd_get_flavour (link_info.output_bfd) != bfd_target_coff_flavour
1529 || coff_data (link_info.output_bfd) == NULL
1530 || !obj_pe (link_info.output_bfd))
1531 einfo (_("%F%P: cannot perform PE operations on non PE output file '%pB'\n"),
1532 link_info.output_bfd);
1534 pe_data (link_info.output_bfd)->pe_opthdr = pe;
1535 pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
1536 pe_data (link_info.output_bfd)->real_flags |= real_flags;
1537 if (insert_timestamp)
1538 pe_data (link_info.output_bfd)->timestamp = -1;
1540 pe_data (link_info.output_bfd)->timestamp = 0;
1542 /* At this point we must decide whether to use long section names
1543 in the output or not. If the user hasn't explicitly specified
1544 on the command line, we leave it to the default for the format
1545 (object files yes, image files no), except if there is debug
1546 information present; GDB relies on the long section names to
1547 find it, so enable it in that case. */
1548 if (pe_use_coff_long_section_names < 0 && link_info.strip == strip_none)
1550 if (bfd_link_relocatable (&link_info))
1551 pe_use_coff_long_section_names = 1;
1554 /* Iterate over all sections of all input BFDs, checking
1555 for any that begin 'debug_' and are long names. */
1556 LANG_FOR_EACH_INPUT_STATEMENT (is)
1558 int found_debug = 0;
1560 bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug);
1563 pe_use_coff_long_section_names = 1;
1570 pe_output_file_set_long_section_names (link_info.output_bfd);
1573 pe_process_import_defs (link_info.output_bfd, &link_info);
1575 if (link_info.pei386_auto_import) /* -1=warn or 1=enable */
1576 pe_find_data_imports (U ("_head_"), make_import_fixup);
1578 /* The implementation of the feature is rather dumb and would cause the
1579 compilation time to go through the roof if there are many undefined
1580 symbols in the link, so it needs to be run after auto-import. */
1581 if (pe_enable_stdcall_fixup) /* -1=warn or 1=enable */
1582 pe_fixup_stdcalls ();
1584 #if defined (TARGET_IS_i386pe) \
1585 || defined (TARGET_IS_armpe) \
1586 || defined (TARGET_IS_arm_wince_pe)
1587 if (!bfd_link_relocatable (&link_info))
1588 pe_dll_build_sections (link_info.output_bfd, &link_info);
1590 if (bfd_link_pic (&link_info))
1591 pe_dll_build_sections (link_info.output_bfd, &link_info);
1593 pe_exe_build_sections (link_info.output_bfd, &link_info);
1595 #endif /* DLL_SUPPORT */
1597 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
1598 if (strstr (bfd_get_target (link_info.output_bfd), "arm") == NULL)
1600 /* The arm backend needs special fields in the output hash structure.
1601 These will only be created if the output format is an arm format,
1602 hence we do not support linking and changing output formats at the
1603 same time. Use a link followed by objcopy to change output formats. */
1604 einfo (_("%F%P: error: cannot change output format "
1605 "whilst linking %s binaries\n"), "ARM");
1609 /* Find a BFD that can hold the interworking stubs. */
1610 LANG_FOR_EACH_INPUT_STATEMENT (is)
1612 if (bfd_arm_get_bfd_for_interworking (is->the_bfd, & link_info))
1619 /* This next chunk of code tries to detect the case where you have
1620 two import libraries for the same DLL (specifically,
1621 symbolically linking libm.a and libc.a in cygwin to
1622 libcygwin.a). In those cases, it's possible for function
1623 thunks from the second implib to be used but without the
1624 head/tail objects, causing an improper import table. We detect
1625 those cases and rename the "other" import libraries to match
1626 the one the head/tail come from, so that the linker will sort
1627 things nicely and produce a valid import table. */
1629 LANG_FOR_EACH_INPUT_STATEMENT (is)
1631 if (is->the_bfd->my_archive)
1633 int idata2 = 0, reloc_count=0, is_imp = 0;
1636 /* See if this is an import library thunk. */
1637 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1639 if (strcmp (sec->name, ".idata\$2") == 0)
1641 if (startswith (sec->name, ".idata\$"))
1643 reloc_count += sec->reloc_count;
1646 if (is_imp && !idata2 && reloc_count)
1648 /* It is, look for the reference to head and see if it's
1649 from our own library. */
1650 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1658 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1662 if (!bfd_generic_link_read_symbols (is->the_bfd))
1664 einfo (_("%F%P: %pB: could not read symbols: %E\n"),
1668 symbols = bfd_get_outsymbols (is->the_bfd);
1670 relocs = xmalloc ((size_t) relsize);
1671 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1676 einfo (_("%X%P: unable to process relocs: %E\n"));
1680 for (i = 0; i < nrelocs; i++)
1682 struct bfd_symbol *s;
1683 struct bfd_link_hash_entry * blhe;
1685 const char *other_bfd_filename;
1687 s = (relocs[i]->sym_ptr_ptr)[0];
1689 if (s->flags & BSF_LOCAL)
1692 /* Thunk section with reloc to another bfd. */
1693 blhe = bfd_link_hash_lookup (link_info.hash,
1695 false, false, true);
1698 || blhe->type != bfd_link_hash_defined)
1701 other_bfd = blhe->u.def.section->owner;
1702 if (other_bfd->my_archive == is->the_bfd->my_archive)
1706 = (other_bfd->my_archive
1707 ? bfd_get_filename (other_bfd->my_archive)
1708 : bfd_get_filename (other_bfd));
1710 if (filename_cmp (bfd_get_filename
1711 (is->the_bfd->my_archive),
1712 other_bfd_filename) == 0)
1715 /* Sort this implib to match the other one. */
1716 lang_input_statement_type *arch_is
1717 = bfd_usrdata (is->the_bfd->my_archive);
1718 arch_is->sort_key = other_bfd_filename;
1723 /* Note - we do not free the symbols,
1724 they are now cached in the BFD. */
1733 /* Careful - this is a shell script. Watch those dollar signs! */
1734 /* Microsoft import libraries have every member named the same,
1735 and not in the right order for us to link them correctly. We
1736 must detect these and rename the members so that they'll link
1737 correctly. There are three types of objects: the head, the
1738 thunks, and the sentinel(s). The head is easy; it's the one
1739 with idata2. We assume that the sentinels won't have relocs,
1740 and the thunks will. It's easier than checking the symbol
1741 table for external references. */
1742 LANG_FOR_EACH_INPUT_STATEMENT (is)
1744 if (is->the_bfd->my_archive)
1748 /* Microsoft import libraries may contain archive members for
1749 one or more DLLs, together with static object files.
1750 The head and sentinels are regular COFF object files,
1751 while the thunks are special ILF files that get synthesized
1752 by bfd into COFF object files.
1754 As Microsoft import libraries can be for a module with
1755 almost any file name (*.dll, *.exe, etc), we can't easily
1756 know which archive members to inspect.
1758 Inspect all members, except ones named *.o or *.obj (which
1759 is the case both for regular static libraries or for GNU
1760 style import libraries). Archive members with file names other
1761 than *.o or *.obj, that do contain .idata sections, are
1762 considered to be Microsoft style import objects, and are
1763 renamed accordingly.
1765 If this heuristic is wrong and we apply this on archive members
1766 that already have unique names, it shouldn't make any difference
1767 as we only append a suffix on the names. */
1768 pnt = strrchr (bfd_get_filename (is->the_bfd), '.');
1770 if (pnt != NULL && (fileext_cmp (pnt + 1, "o") != 0 &&
1771 fileext_cmp (pnt + 1, "obj") != 0))
1773 int idata2 = 0, reloc_count = 0, idata = 0;
1775 char *new_name, seq;
1777 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1779 if (strcmp (sec->name, ".idata\$2") == 0)
1781 if (strncmp (sec->name, ".idata\$", 6) == 0)
1783 reloc_count += sec->reloc_count;
1786 /* An archive member not named .o or .obj, but not having any
1787 .idata sections - apparently not a Microsoft import object
1788 after all: Skip renaming it. */
1792 if (idata2) /* .idata2 is the TOC */
1794 else if (reloc_count > 0) /* thunks */
1800 = xmalloc (strlen (bfd_get_filename (is->the_bfd)) + 3);
1801 sprintf (new_name, "%s.%c",
1802 bfd_get_filename (is->the_bfd), seq);
1803 is->sort_key = new_name;
1810 /* The following chunk of code tries to identify jump stubs in
1811 import libraries which are dead code and eliminates them
1812 from the final link. For each exported symbol <sym>, there
1813 is a object file in the import library with a .text section
1814 and several .idata\$* sections. The .text section contains the
1815 symbol definition for <sym> which is a jump stub of the form
1816 jmp *__imp_<sym>. The .idata\$5 contains the symbol definition
1817 for __imp_<sym> which is the address of the slot for <sym> in
1818 the import address table. When a symbol is imported explicitly
1819 using __declspec(dllimport) declaration, the compiler generates
1820 a reference to __imp_<sym> which directly resolves to the
1821 symbol in .idata\$5, in which case the jump stub code is not
1822 needed. The following code tries to identify jump stub sections
1823 in import libraries which are not referred to by anyone and
1824 marks them for exclusion from the final link. */
1825 LANG_FOR_EACH_INPUT_STATEMENT (is)
1827 if (is->the_bfd->my_archive)
1830 asection *sec, *stub_sec = NULL;
1832 /* See if this is an import library thunk. */
1833 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1835 if (strncmp (sec->name, ".idata\$", 7) == 0)
1837 /* The section containing the jmp stub has code
1839 if ((sec->flags & SEC_CODE) && sec->reloc_count)
1843 if (is_imp && stub_sec)
1846 long nsyms, src_count;
1847 struct bfd_link_hash_entry * blhe;
1849 if (!bfd_generic_link_read_symbols (is->the_bfd))
1851 einfo (_("%F%P: %pB: could not read symbols: %E\n"),
1855 symbols = bfd_get_outsymbols (is->the_bfd);
1856 nsyms = bfd_get_symcount (is->the_bfd);
1858 for (src_count = 0; src_count < nsyms; src_count++)
1860 if (symbols[src_count]->section->id == stub_sec->id)
1862 /* This symbol belongs to the section containing
1864 blhe = bfd_link_hash_lookup (link_info.hash,
1865 symbols[src_count]->name,
1866 false, false, true);
1867 /* If the symbol in the stub section has no other
1868 undefined references, exclude the stub section
1869 from the final link. */
1871 && blhe->type == bfd_link_hash_defined
1872 && blhe->u.undef.next == NULL
1873 && blhe != link_info.hash->undefs_tail)
1874 stub_sec->flags |= SEC_EXCLUDE;
1884 gld${EMULATION_NAME}_before_allocation (void)
1886 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
1887 /* FIXME: we should be able to set the size of the interworking stub
1890 Here we rummage through the found bfds to collect glue
1891 information. FIXME: should this be based on a command line
1892 option? krk@cygnus.com. */
1894 LANG_FOR_EACH_INPUT_STATEMENT (is)
1896 if (! bfd_arm_process_before_allocation
1897 (is->the_bfd, & link_info, support_old_code))
1899 /* xgettext:c-format */
1900 einfo (_("%P: errors encountered processing file %s for interworking\n"),
1906 /* We have seen it all. Allocate it, and carry on. */
1907 bfd_arm_allocate_interworking_sections (& link_info);
1908 #endif /* TARGET_IS_armpe || TARGET_IS_arm_wince_pe */
1910 before_allocation_default ();
1914 /* This is called when an input file isn't recognized as a BFD. We
1915 check here for .DEF files and pull them in automatically. */
1918 saw_option (char *option)
1922 for (i = 0; init[i].ptr; i++)
1923 if (strcmp (GET_INIT_SYMBOL_NAME (i), option) == 0)
1924 return init[i].inited;
1927 #endif /* DLL_SUPPORT */
1930 gld${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1933 const char *ext = strrchr (entry->filename, '.');
1935 if (ext != NULL && fileext_cmp (ext + 1, "def") == 0)
1937 pe_def_file = def_file_parse (entry->filename, pe_def_file);
1941 int i, buflen=0, len;
1944 for (i = 0; i < pe_def_file->num_exports; i++)
1946 len = strlen (pe_def_file->exports[i].internal_name);
1947 if (buflen < len + 2)
1951 buf = xmalloc (buflen);
1953 for (i = 0; i < pe_def_file->num_exports; i++)
1955 struct bfd_link_hash_entry *h;
1957 sprintf (buf, "%s%s", U (""),
1958 pe_def_file->exports[i].internal_name);
1960 h = bfd_link_hash_lookup (link_info.hash, buf, true, true, true);
1961 if (h == (struct bfd_link_hash_entry *) NULL)
1962 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
1963 if (h->type == bfd_link_hash_new)
1965 h->type = bfd_link_hash_undefined;
1966 h->u.undef.abfd = NULL;
1967 bfd_link_add_undef (link_info.hash, h);
1972 /* def_file_print (stdout, pe_def_file); */
1973 if (pe_def_file->is_dll == 1)
1974 link_info.type = type_dll;
1976 if (pe_def_file->base_address != (bfd_vma)(-1))
1979 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1980 = init[IMAGEBASEOFF].value
1981 = pe_def_file->base_address;
1982 init[IMAGEBASEOFF].inited = 1;
1983 if (image_base_statement)
1984 image_base_statement->exp
1985 = exp_assign ("__image_base__", exp_intop (pe.ImageBase),
1989 if (pe_def_file->stack_reserve != -1
1990 && ! saw_option ("__size_of_stack_reserve__"))
1992 pe.SizeOfStackReserve = pe_def_file->stack_reserve;
1993 if (pe_def_file->stack_commit != -1)
1994 pe.SizeOfStackCommit = pe_def_file->stack_commit;
1996 if (pe_def_file->heap_reserve != -1
1997 && ! saw_option ("__size_of_heap_reserve__"))
1999 pe.SizeOfHeapReserve = pe_def_file->heap_reserve;
2000 if (pe_def_file->heap_commit != -1)
2001 pe.SizeOfHeapCommit = pe_def_file->heap_commit;
2011 gld${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
2014 #ifdef TARGET_IS_i386pe
2015 pe_dll_id_target ("pei-i386");
2017 #ifdef TARGET_IS_shpe
2018 pe_dll_id_target ("pei-shl");
2020 #ifdef TARGET_IS_armpe
2021 pe_dll_id_target ("pei-arm-little");
2023 #ifdef TARGET_IS_arm_wince_pe
2024 pe_dll_id_target ("pei-arm-wince-little");
2026 if (pe_bfd_is_dll (entry->the_bfd))
2027 return pe_implied_import_dll (entry->filename);
2033 gld${EMULATION_NAME}_finish (void)
2035 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
2036 struct bfd_link_hash_entry * h;
2038 if (thumb_entry_symbol != NULL)
2040 h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol,
2041 false, false, true);
2043 if (h != (struct bfd_link_hash_entry *) NULL
2044 && (h->type == bfd_link_hash_defined
2045 || h->type == bfd_link_hash_defweak)
2046 && h->u.def.section->output_section != NULL)
2048 static char buffer[32];
2051 /* Special procesing is required for a Thumb entry symbol. The
2052 bottom bit of its address must be set. */
2053 val = (h->u.def.value
2054 + bfd_section_vma (h->u.def.section->output_section)
2055 + h->u.def.section->output_offset);
2059 /* Now convert this value into a string and store it in entry_symbol
2060 where the lang_finish() function will pick it up. */
2061 sprintf (buffer, "0x%" PRIx64, (uint64_t) val);
2063 if (entry_symbol.name != NULL && entry_from_cmdline)
2064 einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
2065 thumb_entry_symbol, entry_symbol.name);
2066 entry_symbol.name = buffer;
2069 einfo (_("%P: warning: cannot find thumb start symbol %s\n"), thumb_entry_symbol);
2071 #endif /* defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe) */
2076 if (bfd_link_pic (&link_info)
2077 #if !defined(TARGET_IS_shpe)
2078 || pe_dll_enable_reloc_section
2079 || (!bfd_link_relocatable (&link_info)
2080 && pe_def_file->num_exports != 0)
2084 pe_dll_fill_sections (link_info.output_bfd, &link_info);
2085 if (command_line.out_implib_filename
2086 && (pe_def_file->num_exports != 0
2087 || bfd_link_pic (&link_info)))
2088 pe_dll_generate_implib (pe_def_file, command_line.out_implib_filename,
2091 #if defined(TARGET_IS_shpe)
2092 /* ARM doesn't need relocs. */
2095 pe_exe_fill_sections (link_info.output_bfd, &link_info);
2099 if (pe_out_def_filename)
2100 pe_dll_generate_def_file (pe_out_def_filename);
2101 #endif /* DLL_SUPPORT */
2103 /* I don't know where .idata gets set as code, but it shouldn't be. */
2105 asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
2109 asec->flags &= ~SEC_CODE;
2110 asec->flags |= SEC_DATA;
2116 /* Place an orphan section.
2118 We use this to put sections in a reasonable place in the file, and
2119 to ensure that they are aligned as required.
2121 We handle grouped sections here as well. A section named .foo\$nn
2122 goes into the output section .foo. All grouped sections are sorted
2125 Grouped sections for the default sections are handled by the
2126 default linker script using wildcards, and are sorted by
2129 static lang_output_section_statement_type *
2130 gld${EMULATION_NAME}_place_orphan (asection *s,
2131 const char *secname,
2134 const char *orig_secname = secname;
2135 char *dollar = NULL;
2136 lang_output_section_statement_type *os;
2137 lang_statement_list_type add_child;
2138 lang_output_section_statement_type *match_by_name = NULL;
2139 lang_statement_union_type **pl;
2141 /* Look through the script to see where to place this section. */
2142 if (!bfd_link_relocatable (&link_info)
2143 && (dollar = strchr (secname, '\$')) != NULL)
2145 size_t len = dollar - secname;
2146 char *newname = xmalloc (len + 1);
2147 memcpy (newname, secname, len);
2148 newname[len] = '\0';
2152 lang_list_init (&add_child);
2155 if (constraint == 0)
2156 for (os = lang_output_section_find (secname);
2158 os = next_matching_output_section_statement (os, 0))
2160 /* If we don't match an existing output section, tell
2161 lang_insert_orphan to create a new output section. */
2162 constraint = SPECIAL;
2164 if (os->bfd_section != NULL
2165 && (os->bfd_section->flags == 0
2166 || ((s->flags ^ os->bfd_section->flags)
2167 & (SEC_LOAD | SEC_ALLOC)) == 0))
2169 /* We already have an output section statement with this
2170 name, and its bfd section has compatible flags.
2171 If the section already exists but does not have any flags set,
2172 then it has been created by the linker, probably as a result of
2173 a --section-start command line switch. */
2174 lang_add_section (&add_child, s, NULL, NULL, os);
2178 /* Save unused output sections in case we can match them
2179 against orphans later. */
2180 if (os->bfd_section == NULL)
2184 /* If we didn't match an active output section, see if we matched an
2185 unused one and use that. */
2186 if (os == NULL && match_by_name)
2188 lang_add_section (&match_by_name->children, s, NULL, NULL, match_by_name);
2189 return match_by_name;
2194 static struct orphan_save hold[] =
2197 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
2200 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
2203 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
2206 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
2212 enum orphan_save_index
2220 static int orphan_init_done = 0;
2221 struct orphan_save *place;
2222 lang_output_section_statement_type *after;
2223 etree_type *address;
2227 if (!orphan_init_done)
2229 struct orphan_save *ho;
2230 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
2231 if (ho->name != NULL)
2233 ho->os = lang_output_section_find (ho->name);
2234 if (ho->os != NULL && ho->os->flags == 0)
2235 ho->os->flags = ho->flags;
2237 orphan_init_done = 1;
2241 if (!bfd_link_relocatable (&link_info))
2244 while ((nexts = bfd_get_next_section_by_name (nexts->owner,
2246 if (nexts->output_section == NULL
2247 && (nexts->flags & SEC_EXCLUDE) == 0
2248 && ((nexts->flags ^ flags) & (SEC_LOAD | SEC_ALLOC)) == 0
2249 && (nexts->owner->flags & DYNAMIC) == 0
2250 && !bfd_input_just_syms (nexts->owner))
2251 flags = (((flags ^ SEC_READONLY)
2252 | (nexts->flags ^ SEC_READONLY))
2256 /* Try to put the new output section in a reasonable place based
2257 on the section name and section flags. */
2260 if ((flags & SEC_ALLOC) == 0)
2262 else if ((flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2263 place = &hold[orphan_bss];
2264 else if ((flags & SEC_READONLY) == 0)
2265 place = &hold[orphan_data];
2266 else if ((flags & SEC_CODE) == 0)
2268 place = (!strncmp (secname, ".idata\$", 7) ? &hold[orphan_idata]
2269 : &hold[orphan_rodata]);
2272 place = &hold[orphan_text];
2277 if (place->os == NULL)
2278 place->os = lang_output_section_find (place->name);
2281 after = lang_output_section_find_by_flags (s, flags, &place->os,
2284 /* *ABS* is always the first output section statement. */
2285 after = (void *) lang_os_list.head;
2288 /* All sections in an executable must be aligned to a page boundary.
2289 In a relocatable link, just preserve the incoming alignment; the
2290 address is discarded by lang_insert_orphan in that case, anyway. */
2291 address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
2292 os = lang_insert_orphan (s, secname, constraint, after, place, address,
2294 if (bfd_link_relocatable (&link_info))
2296 os->section_alignment = exp_intop (1U << s->alignment_power);
2297 os->bfd_section->alignment_power = s->alignment_power;
2301 /* If the section name has a '\$', sort it with the other '\$'
2303 for (pl = &os->children.head; *pl != NULL; pl = &(*pl)->header.next)
2305 lang_input_section_type *ls;
2308 if ((*pl)->header.type != lang_input_section_enum)
2311 ls = &(*pl)->input_section;
2313 lname = bfd_section_name (ls->section);
2314 if (strchr (lname, '\$') != NULL
2315 && (dollar == NULL || strcmp (orig_secname, lname) < 0))
2319 if (add_child.head != NULL)
2321 *add_child.tail = *pl;
2322 *pl = add_child.head;
2329 gld${EMULATION_NAME}_open_dynamic_archive
2330 (const char *arch ATTRIBUTE_UNUSED,
2331 search_dirs_type *search,
2332 lang_input_statement_type *entry)
2336 const char * format;
2341 /* Preferred explicit import library for dll's. */
2342 { "lib%s.dll.a", false },
2343 /* Alternate explicit import library for dll's. */
2344 { "%s.dll.a", false },
2345 /* "libfoo.a" could be either an import lib or a static lib.
2346 For backwards compatibility, libfoo.a needs to precede
2347 libfoo.dll and foo.dll in the search. */
2348 { "lib%s.a", false },
2349 /* The 'native' spelling of an import lib name is "foo.lib". */
2350 { "%s.lib", false },
2351 /* PR 22948 - Check for an import library. */
2352 { "lib%s.lib", false },
2354 /* Try "<prefix>foo.dll" (preferred dll name, if specified). */
2355 { "%s%s.dll", true },
2357 /* Try "libfoo.dll" (default preferred dll name). */
2358 { "lib%s.dll", false },
2359 /* Finally try 'native' dll name "foo.dll". */
2360 { "%s.dll", false },
2361 /* Note: If adding more formats to this table, make sure to check to
2362 see if their length is longer than libname_fmt[0].format, and if
2363 so, update the call to xmalloc() below. */
2366 static unsigned int format_max_len = 0;
2367 const char * filename;
2373 if (! entry->flags.maybe_archive || entry->flags.full_name_provided)
2376 filename = entry->filename;
2378 if (format_max_len == 0)
2379 /* We need to allow space in the memory that we are going to allocate
2380 for the characters in the format string. Since the format array is
2381 static we only need to calculate this information once. In theory
2382 this value could also be computed statically, but this introduces
2383 the possibility for a discrepancy and hence a possible memory
2384 corruption. The lengths we compute here will be too long because
2385 they will include any formating characters (%s) in the strings, but
2386 this will not matter. */
2387 for (i = 0; libname_fmt[i].format; i++)
2388 if (format_max_len < strlen (libname_fmt[i].format))
2389 format_max_len = strlen (libname_fmt[i].format);
2391 full_string = xmalloc (strlen (search->name)
2395 + (pe_dll_search_prefix
2396 ? strlen (pe_dll_search_prefix) : 0)
2398 /* Allow for the terminating NUL and for the path
2399 separator character that is inserted between
2400 search->name and the start of the format string. */
2403 base_string = stpcpy (full_string, search->name);
2404 *base_string++ = '/';
2406 for (i = 0; libname_fmt[i].format; i++)
2409 if (libname_fmt[i].use_prefix)
2411 if (!pe_dll_search_prefix)
2413 sprintf (base_string, libname_fmt[i].format, pe_dll_search_prefix, filename);
2417 sprintf (base_string, libname_fmt[i].format, filename);
2419 if (ldfile_try_open_bfd (full_string, entry))
2423 if (!libname_fmt[i].format)
2429 entry->filename = full_string;
2435 gld${EMULATION_NAME}_find_potential_libraries
2436 (char *name, lang_input_statement_type *entry)
2438 return ldfile_open_file_search (name, entry, "", ".lib");
2442 gld${EMULATION_NAME}_get_script (int *isfile)
2445 if test x"$COMPILE_IN" = xyes
2447 # Scripts compiled in.
2449 # sed commands to quote an ld script as a C string.
2450 sc="-f ${srcdir}/emultempl/stringify.sed"
2456 if (bfd_link_relocatable (&link_info) && config.build_constructors)
2459 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
2460 echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
2461 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
2462 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
2463 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
2464 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
2465 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
2466 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
2467 echo ' ; else if (link_info.pei386_auto_import == 1 && (MERGE_RDATA_V2 || link_info.pei386_runtime_pseudo_reloc != 2)) return' >> e${EMULATION_NAME}.c
2468 sed $sc ldscripts/${EMULATION_NAME}.xa >> e${EMULATION_NAME}.c
2470 echo ' ; else return' >> e${EMULATION_NAME}.c
2471 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
2472 echo '; }' >> e${EMULATION_NAME}.c
2475 # Scripts read from the filesystem.
2481 if (bfd_link_relocatable (&link_info) && config.build_constructors)
2482 return "ldscripts/${EMULATION_NAME}.xu";
2483 else if (bfd_link_relocatable (&link_info))
2484 return "ldscripts/${EMULATION_NAME}.xr";
2485 else if (!config.text_read_only)
2486 return "ldscripts/${EMULATION_NAME}.xbn";
2487 else if (!config.magic_demand_paged)
2488 return "ldscripts/${EMULATION_NAME}.xn";
2490 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
2492 else if (link_info.pei386_auto_import == 1
2493 && (MERGE_RDATA_V2 || link_info.pei386_runtime_pseudo_reloc != 2))
2494 return "ldscripts/${EMULATION_NAME}.xa";
2499 return "ldscripts/${EMULATION_NAME}.x";
2504 LDEMUL_AFTER_PARSE=gld${EMULATION_NAME}_after_parse
2505 LDEMUL_BEFORE_PLUGIN_ALL_SYMBOLS_READ=gld${EMULATION_NAME}_before_plugin_all_symbols_read
2506 LDEMUL_AFTER_OPEN=gld${EMULATION_NAME}_after_open
2507 LDEMUL_BEFORE_ALLOCATION=gld${EMULATION_NAME}_before_allocation
2508 LDEMUL_FINISH=gld${EMULATION_NAME}_finish
2509 LDEMUL_OPEN_DYNAMIC_ARCHIVE=gld${EMULATION_NAME}_open_dynamic_archive
2510 LDEMUL_PLACE_ORPHAN=gld${EMULATION_NAME}_place_orphan
2511 LDEMUL_SET_SYMBOLS=gld${EMULATION_NAME}_set_symbols
2512 LDEMUL_ADD_OPTIONS=gld${EMULATION_NAME}_add_options
2513 LDEMUL_HANDLE_OPTION=gld${EMULATION_NAME}_handle_option
2514 LDEMUL_UNRECOGNIZED_FILE=gld${EMULATION_NAME}_unrecognized_file
2515 LDEMUL_LIST_OPTIONS=gld${EMULATION_NAME}_list_options
2516 LDEMUL_RECOGNIZED_FILE=gld${EMULATION_NAME}_recognized_file
2517 LDEMUL_FIND_POTENTIAL_LIBRARIES=gld${EMULATION_NAME}_find_potential_libraries
2519 source_em ${srcdir}/emultempl/emulation.em