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-2022 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"
70 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
71 header in generic PE code. */
72 #include "coff/i386.h"
75 /* FIXME: These are BFD internal header files, and we should not be
77 #include "../bfd/libcoff.h"
78 #include "../bfd/libpei.h"
80 #if defined(TARGET_IS_armpe) \
81 || defined(TARGET_IS_arm_wince_pe)
82 #define bfd_arm_allocate_interworking_sections \
83 bfd_${EMULATION_NAME}_allocate_interworking_sections
84 #define bfd_arm_get_bfd_for_interworking \
85 bfd_${EMULATION_NAME}_get_bfd_for_interworking
86 #define bfd_arm_process_before_allocation \
87 bfd_${EMULATION_NAME}_process_before_allocation
93 #include "safe-ctype.h"
95 /* Permit the emulation parameters to override the default section
96 alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes
97 it seem that include/coff/internal.h should not define
98 PE_DEF_SECTION_ALIGNMENT. */
99 #if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
100 #undef PE_DEF_SECTION_ALIGNMENT
101 #define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
104 #if defined(TARGET_IS_i386pe) \
105 || defined(TARGET_IS_shpe) \
106 || defined(TARGET_IS_armpe) \
107 || defined(TARGET_IS_arm_wince_pe)
111 #if defined(TARGET_IS_i386pe)
112 #define DEFAULT_PSEUDO_RELOC_VERSION 2
114 #define DEFAULT_PSEUDO_RELOC_VERSION 1
117 #define DEFAULT_DLL_CHARACTERISTICS (${cygwin_behavior} ? 0 : \
118 IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE \
119 | IMAGE_DLL_CHARACTERISTICS_NX_COMPAT)
121 #if defined(TARGET_IS_i386pe) || ! defined(DLL_SUPPORT)
122 #define PE_DEF_SUBSYSTEM 3
124 #undef NT_EXE_IMAGE_BASE
125 #undef PE_DEF_SECTION_ALIGNMENT
126 #undef PE_DEF_FILE_ALIGNMENT
127 #define NT_EXE_IMAGE_BASE 0x00010000
129 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
130 #define PE_DEF_SECTION_ALIGNMENT 0x00001000
131 #define PE_DEF_SUBSYSTEM 9
133 #define PE_DEF_SECTION_ALIGNMENT 0x00000400
134 #define PE_DEF_SUBSYSTEM 2
136 #define PE_DEF_FILE_ALIGNMENT 0x00000200
139 static struct internal_extra_pe_aouthdr pe;
141 static int pe_subsystem = ${SUBSYSTEM};
142 static flagword real_flags = 0;
143 static int support_old_code = 0;
144 static char * thumb_entry_symbol = NULL;
145 static lang_assignment_statement_type *image_base_statement = 0;
146 static unsigned short pe_dll_characteristics = DEFAULT_DLL_CHARACTERISTICS;
147 static bool insert_timestamp = true;
148 static const char *emit_build_id;
151 static int pe_enable_stdcall_fixup = -1; /* 0=disable 1=enable. */
152 static char *pe_out_def_filename = NULL;
153 static int pe_enable_auto_image_base = 0;
154 static unsigned long pe_auto_image_base = 0x61500000;
155 static char *pe_dll_search_prefix = NULL;
158 extern const char *output_filename;
161 is_underscoring (void)
164 if (pe_leading_underscore != -1)
165 return pe_leading_underscore;
166 if (!bfd_get_target_info ("${OUTPUT_FORMAT}", NULL, NULL, &u, NULL))
167 bfd_get_target_info ("${RELOCATEABLE_OUTPUT_FORMAT}", NULL, NULL, &u, NULL);
171 pe_leading_underscore = (u != 0 ? 1 : 0);
172 return pe_leading_underscore;
176 gld${EMULATION_NAME}_before_parse (void)
179 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
180 output_filename = "${EXECUTABLE_NAME:-a.exe}";
182 input_flags.dynamic = true;
183 config.has_shared = 1;
186 # Cygwin no longer wants these noisy warnings. Other PE
187 # targets might like to consider adding themselves here.
188 # See also the mail thread starting here for the reason why
189 # merge_rdata defaults to 0 for cygwin:
190 # http://cygwin.com/ml/cygwin-apps/2013-04/msg00187.html
193 default_auto_import=1
194 default_merge_rdata=0
196 i[3-7]86-*-mingw* | x86_64-*-mingw*)
197 default_auto_import=1
198 default_merge_rdata=0
201 default_auto_import=-1
202 default_merge_rdata=1
207 link_info.pei386_auto_import = ${default_auto_import};
208 /* Use by default version. */
209 link_info.pei386_runtime_pseudo_reloc = DEFAULT_PSEUDO_RELOC_VERSION;
213 /* Indicates if RDATA shall be merged into DATA when pseudo-relocation
214 version 2 is used and auto-import is enabled. */
215 #define MERGE_RDATA_V2 ${default_merge_rdata}
217 /* PE format extra command line options. */
219 /* Used for setting flags in the PE header. */
220 #define OPTION_BASE_FILE (300 + 1)
221 #define OPTION_DLL (OPTION_BASE_FILE + 1)
222 #define OPTION_FILE_ALIGNMENT (OPTION_DLL + 1)
223 #define OPTION_IMAGE_BASE (OPTION_FILE_ALIGNMENT + 1)
224 #define OPTION_MAJOR_IMAGE_VERSION (OPTION_IMAGE_BASE + 1)
225 #define OPTION_MAJOR_OS_VERSION (OPTION_MAJOR_IMAGE_VERSION + 1)
226 #define OPTION_MAJOR_SUBSYSTEM_VERSION (OPTION_MAJOR_OS_VERSION + 1)
227 #define OPTION_MINOR_IMAGE_VERSION (OPTION_MAJOR_SUBSYSTEM_VERSION + 1)
228 #define OPTION_MINOR_OS_VERSION (OPTION_MINOR_IMAGE_VERSION + 1)
229 #define OPTION_MINOR_SUBSYSTEM_VERSION (OPTION_MINOR_OS_VERSION + 1)
230 #define OPTION_SECTION_ALIGNMENT (OPTION_MINOR_SUBSYSTEM_VERSION + 1)
231 #define OPTION_STACK (OPTION_SECTION_ALIGNMENT + 1)
232 #define OPTION_SUBSYSTEM (OPTION_STACK + 1)
233 #define OPTION_HEAP (OPTION_SUBSYSTEM + 1)
234 #define OPTION_SUPPORT_OLD_CODE (OPTION_HEAP + 1)
235 #define OPTION_OUT_DEF (OPTION_SUPPORT_OLD_CODE + 1)
236 #define OPTION_EXPORT_ALL (OPTION_OUT_DEF + 1)
237 #define OPTION_EXCLUDE_SYMBOLS (OPTION_EXPORT_ALL + 1)
238 #define OPTION_EXCLUDE_ALL_SYMBOLS (OPTION_EXCLUDE_SYMBOLS + 1)
239 #define OPTION_KILL_ATS (OPTION_EXCLUDE_ALL_SYMBOLS + 1)
240 #define OPTION_STDCALL_ALIASES (OPTION_KILL_ATS + 1)
241 #define OPTION_ENABLE_STDCALL_FIXUP (OPTION_STDCALL_ALIASES + 1)
242 #define OPTION_DISABLE_STDCALL_FIXUP (OPTION_ENABLE_STDCALL_FIXUP + 1)
243 #define OPTION_THUMB_ENTRY (OPTION_DISABLE_STDCALL_FIXUP + 1)
244 #define OPTION_WARN_DUPLICATE_EXPORTS (OPTION_THUMB_ENTRY + 1)
245 #define OPTION_IMP_COMPAT (OPTION_WARN_DUPLICATE_EXPORTS + 1)
246 #define OPTION_ENABLE_AUTO_IMAGE_BASE (OPTION_IMP_COMPAT + 1)
247 #define OPTION_DISABLE_AUTO_IMAGE_BASE (OPTION_ENABLE_AUTO_IMAGE_BASE + 1)
248 #define OPTION_DLL_SEARCH_PREFIX (OPTION_DISABLE_AUTO_IMAGE_BASE + 1)
249 #define OPTION_NO_DEFAULT_EXCLUDES (OPTION_DLL_SEARCH_PREFIX + 1)
250 #define OPTION_DLL_ENABLE_AUTO_IMPORT (OPTION_NO_DEFAULT_EXCLUDES + 1)
251 #define OPTION_DLL_DISABLE_AUTO_IMPORT (OPTION_DLL_ENABLE_AUTO_IMPORT + 1)
252 #define OPTION_ENABLE_EXTRA_PE_DEBUG (OPTION_DLL_DISABLE_AUTO_IMPORT + 1)
253 #define OPTION_EXCLUDE_LIBS (OPTION_ENABLE_EXTRA_PE_DEBUG + 1)
254 #define OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC \
255 (OPTION_EXCLUDE_LIBS + 1)
256 #define OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC \
257 (OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC + 1)
258 #define OPTION_LARGE_ADDRESS_AWARE (OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC + 1)
259 #define OPTION_DISABLE_LARGE_ADDRESS_AWARE \
260 (OPTION_LARGE_ADDRESS_AWARE + 1)
261 #define OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1 \
262 (OPTION_DISABLE_LARGE_ADDRESS_AWARE + 1)
263 #define OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2 \
264 (OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1 + 1)
265 #define OPTION_EXCLUDE_MODULES_FOR_IMPLIB \
266 (OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2 + 1)
267 #define OPTION_USE_NUL_PREFIXED_IMPORT_TABLES \
268 (OPTION_EXCLUDE_MODULES_FOR_IMPLIB + 1)
269 #define OPTION_NO_LEADING_UNDERSCORE (OPTION_USE_NUL_PREFIXED_IMPORT_TABLES + 1)
270 #define OPTION_LEADING_UNDERSCORE (OPTION_NO_LEADING_UNDERSCORE + 1)
271 #define OPTION_ENABLE_LONG_SECTION_NAMES \
272 (OPTION_LEADING_UNDERSCORE + 1)
273 #define OPTION_DISABLE_LONG_SECTION_NAMES \
274 (OPTION_ENABLE_LONG_SECTION_NAMES + 1)
275 /* DLLCharacteristics flags. */
276 #define OPTION_DYNAMIC_BASE (OPTION_DISABLE_LONG_SECTION_NAMES + 1)
277 #define OPTION_FORCE_INTEGRITY (OPTION_DYNAMIC_BASE + 1)
278 #define OPTION_NX_COMPAT (OPTION_FORCE_INTEGRITY + 1)
279 #define OPTION_NO_ISOLATION (OPTION_NX_COMPAT + 1)
280 #define OPTION_NO_SEH (OPTION_NO_ISOLATION + 1)
281 #define OPTION_NO_BIND (OPTION_NO_SEH + 1)
282 #define OPTION_WDM_DRIVER (OPTION_NO_BIND + 1)
283 #define OPTION_TERMINAL_SERVER_AWARE (OPTION_WDM_DRIVER + 1)
285 #define OPTION_INSERT_TIMESTAMP (OPTION_TERMINAL_SERVER_AWARE + 1)
286 #define OPTION_NO_INSERT_TIMESTAMP (OPTION_INSERT_TIMESTAMP + 1)
287 #define OPTION_BUILD_ID (OPTION_NO_INSERT_TIMESTAMP + 1)
288 #define OPTION_ENABLE_RELOC_SECTION (OPTION_BUILD_ID + 1)
289 #define OPTION_DISABLE_RELOC_SECTION (OPTION_ENABLE_RELOC_SECTION + 1)
290 /* DLL Characteristics flags. */
291 #define OPTION_DISABLE_DYNAMIC_BASE (OPTION_DISABLE_RELOC_SECTION + 1)
292 #define OPTION_DISABLE_FORCE_INTEGRITY (OPTION_DISABLE_DYNAMIC_BASE + 1)
293 #define OPTION_DISABLE_NX_COMPAT (OPTION_DISABLE_FORCE_INTEGRITY + 1)
294 #define OPTION_DISABLE_NO_ISOLATION (OPTION_DISABLE_NX_COMPAT + 1)
295 #define OPTION_DISABLE_NO_SEH (OPTION_DISABLE_NO_ISOLATION + 1)
296 #define OPTION_DISABLE_NO_BIND (OPTION_DISABLE_NO_SEH + 1)
297 #define OPTION_DISABLE_WDM_DRIVER (OPTION_DISABLE_NO_BIND + 1)
298 #define OPTION_DISABLE_TERMINAL_SERVER_AWARE \
299 (OPTION_DISABLE_WDM_DRIVER + 1)
302 gld${EMULATION_NAME}_add_options
303 (int ns ATTRIBUTE_UNUSED,
304 char **shortopts ATTRIBUTE_UNUSED,
306 struct option **longopts,
307 int nrl ATTRIBUTE_UNUSED,
308 struct option **really_longopts ATTRIBUTE_UNUSED)
310 static const struct option xtra_long[] =
313 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
314 {"dll", no_argument, NULL, OPTION_DLL},
315 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
316 {"heap", required_argument, NULL, OPTION_HEAP},
317 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
318 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
319 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
320 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
321 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
322 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
323 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
324 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
325 {"stack", required_argument, NULL, OPTION_STACK},
326 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
327 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
328 {"thumb-entry", required_argument, NULL, OPTION_THUMB_ENTRY},
329 {"use-nul-prefixed-import-tables", no_argument, NULL,
330 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
331 {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
332 {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE},
333 {"insert-timestamp", no_argument, NULL, OPTION_INSERT_TIMESTAMP},
334 {"no-insert-timestamp", no_argument, NULL, OPTION_NO_INSERT_TIMESTAMP},
336 /* getopt allows abbreviations, so we do this to stop it
337 from treating -o as an abbreviation for this option. */
338 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
339 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
340 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
341 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
342 {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS},
343 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
344 {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
345 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
346 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
347 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
348 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
349 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
350 /* getopt() allows abbreviations, so we do this to stop it from
351 treating -c as an abbreviation for these --compat-implib. */
352 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
353 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
354 {"enable-auto-image-base", optional_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
355 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
356 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
357 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
358 {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
359 {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
360 {"enable-extra-pe-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
361 {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
362 {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
363 {"enable-runtime-pseudo-reloc-v1", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1},
364 {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2},
366 {"large-address-aware", no_argument, NULL, OPTION_LARGE_ADDRESS_AWARE},
367 {"disable-large-address-aware", no_argument, NULL, OPTION_DISABLE_LARGE_ADDRESS_AWARE},
368 {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
369 {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
370 {"dynamicbase",no_argument, NULL, OPTION_DYNAMIC_BASE},
371 {"disable-dynamicbase",no_argument, NULL, OPTION_DISABLE_DYNAMIC_BASE},
372 {"forceinteg", no_argument, NULL, OPTION_FORCE_INTEGRITY},
373 {"disable-forceinteg", no_argument, NULL, OPTION_DISABLE_FORCE_INTEGRITY},
374 {"nxcompat", no_argument, NULL, OPTION_NX_COMPAT},
375 {"disable-nxcompat", no_argument, NULL, OPTION_DISABLE_NX_COMPAT},
376 {"no-isolation", no_argument, NULL, OPTION_NO_ISOLATION},
377 {"disable-no-isolation", no_argument, NULL, OPTION_DISABLE_NO_ISOLATION},
378 {"no-seh", no_argument, NULL, OPTION_NO_SEH},
379 {"disable-no-seh", no_argument, NULL, OPTION_DISABLE_NO_SEH},
380 {"no-bind", no_argument, NULL, OPTION_NO_BIND},
381 {"disable-no-bind", no_argument, NULL, OPTION_DISABLE_NO_BIND},
382 {"wdmdriver", no_argument, NULL, OPTION_WDM_DRIVER},
383 {"disable-wdmdriver", no_argument, NULL, OPTION_DISABLE_WDM_DRIVER},
384 {"tsaware", no_argument, NULL, OPTION_TERMINAL_SERVER_AWARE},
385 {"disable-tsaware", no_argument, NULL, OPTION_DISABLE_TERMINAL_SERVER_AWARE},
386 {"build-id", optional_argument, NULL, OPTION_BUILD_ID},
387 {"enable-reloc-section", no_argument, NULL, OPTION_ENABLE_RELOC_SECTION},
388 {"disable-reloc-section", no_argument, NULL, OPTION_DISABLE_RELOC_SECTION},
389 {NULL, no_argument, NULL, 0}
393 = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
394 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
397 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
398 parameters which may be input from the command line. */
407 /* FALSE for an assembly level symbol and TRUE for a C visible symbol.
408 C visible symbols can be prefixed by underscore dependent to target's
413 /* Get symbol name dependent to kind and C visible state of
415 #define GET_INIT_SYMBOL_NAME(IDX) \
416 (init[(IDX)].symbol \
417 + ((!init[(IDX)].is_c_symbol || is_underscoring () != 0) ? 0 : 1))
419 /* Decorates the C visible symbol by underscore, if target requires. */
421 ((is_underscoring () == 0) ? CSTR : "_" CSTR)
423 #define D(field,symbol,def,usc) {&pe.field, sizeof (pe.field), def, symbol, 0, usc}
425 static definfo init[] =
427 /* imagebase must be first */
428 #define IMAGEBASEOFF 0
429 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE, false),
431 {&dll, sizeof(dll), 0, "__dll__", 0, false},
432 #define MSIMAGEBASEOFF 2
433 D(ImageBase, "___ImageBase", NT_EXE_IMAGE_BASE, true),
434 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT, false),
435 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT, false),
436 D(MajorOperatingSystemVersion,"__major_os_version__", 4, false),
437 D(MinorOperatingSystemVersion,"__minor_os_version__", 0, false),
438 D(MajorImageVersion,"__major_image_version__", 1, false),
439 D(MinorImageVersion,"__minor_image_version__", 0, false),
440 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
441 D(MajorSubsystemVersion,"__major_subsystem_version__", 3, false),
443 D(MajorSubsystemVersion,"__major_subsystem_version__", 4, false),
445 D(MinorSubsystemVersion,"__minor_subsystem_version__", 0, false),
446 D(Subsystem,"__subsystem__", ${SUBSYSTEM}, false),
447 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000, false),
448 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000, false),
449 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000, false),
450 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000, false),
451 D(LoaderFlags,"__loader_flags__", 0x0, false),
452 D(DllCharacteristics, "__dll_characteristics__", DEFAULT_DLL_CHARACTERISTICS, false),
453 { NULL, 0, 0, NULL, 0 , false}
458 gld${EMULATION_NAME}_list_options (FILE *file)
460 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
461 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
462 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
463 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
464 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
465 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
466 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
467 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
468 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
469 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
470 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
471 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
472 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
473 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
474 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
475 fprintf (file, _(" --[no-]leading-underscore Set explicit symbol underscore prefix mode\n"));
476 fprintf (file, _(" --thumb-entry=<symbol> Set the entry point to be Thumb <symbol>\n"));
477 fprintf (file, _(" --[no-]insert-timestamp Use a real timestamp rather than zero (default).\n"));
478 fprintf (file, _(" This makes binaries non-deterministic\n"));
480 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
481 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
482 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
483 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
484 fprintf (file, _(" --exclude-all-symbols Exclude all symbols from automatic export\n"));
485 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
486 fprintf (file, _(" --exclude-modules-for-implib mod,mod,...\n"));
487 fprintf (file, _(" Exclude objects, archive members from auto\n"));
488 fprintf (file, _(" export, place into import library instead.\n"));
489 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
490 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
491 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
492 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports\n"));
493 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
494 create __imp_<SYMBOL> as well.\n"));
495 fprintf (file, _(" --enable-auto-image-base[=<address>] Automatically choose image base for DLLs\n\
496 (optionally starting with address) unless\n\
497 specifically set with --image-base\n"));
498 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
499 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
500 an importlib, use <string><basename>.dll\n\
501 in preference to lib<basename>.dll \n"));
502 fprintf (file, _(" --enable-auto-import Do sophisticated linking of _sym to\n\
503 __imp_sym for DATA references\n"));
504 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
505 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
506 adding pseudo-relocations resolved at\n\
508 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
509 auto-imported DATA.\n"));
510 fprintf (file, _(" --enable-extra-pe-debug Enable verbose debug output when building\n\
511 or linking to DLLs (esp. auto-import)\n"));
513 fprintf (file, _(" --large-address-aware Executable supports virtual addresses\n\
514 greater than 2 gigabytes\n"));
515 fprintf (file, _(" --disable-large-address-aware Executable does not support virtual\n\
516 addresses greater than 2 gigabytes\n"));
517 fprintf (file, _(" --enable-long-section-names Use long COFF section names even in\n\
518 executable image files\n"));
519 fprintf (file, _(" --disable-long-section-names Never use long COFF section names, even\n\
520 in object files\n"));
521 fprintf (file, _(" --[disable-]dynamicbase Image base address may be relocated using\n\
522 address space layout randomization (ASLR)\n"));
523 fprintf (file, _(" --enable-reloc-section Create the base relocation table\n"));
524 fprintf (file, _(" --disable-reloc-section Do not create the base relocation table\n"));
525 fprintf (file, _(" --[disable-]forceinteg Code integrity checks are enforced\n"));
526 fprintf (file, _(" --[disable-]nxcompat Image is compatible with data execution\n\
528 fprintf (file, _(" --[disable-]no-isolation Image understands isolation but do not\n\
529 isolate the image\n"));
530 fprintf (file, _(" --[disable-]no-seh Image does not use SEH. No SE handler may\n\
531 be called in this image\n"));
532 fprintf (file, _(" --[disable-]no-bind Do not bind this image\n"));
533 fprintf (file, _(" --[disable-]wdmdriver Driver uses the WDM model\n"));
534 fprintf (file, _(" --[disable-]tsaware Image is Terminal Server aware\n"));
535 fprintf (file, _(" --build-id[=STYLE] Generate build ID\n"));
538 /* A case insensitive comparison, regardless of the host platform, used for
539 comparing file extensions. */
541 fileext_cmp (const char *s1, const char *s2)
545 int c1 = TOLOWER (*s1++);
546 int c2 = *s2++; /* Assumed to be lower case from the caller. */
557 set_pe_name (char *name, long val)
562 /* Find the name and set it. */
563 for (i = 0; init[i].ptr; i++)
565 if (strcmp (name, GET_INIT_SYMBOL_NAME (i)) == 0)
569 if (strcmp (name,"__image_base__") == 0)
570 set_pe_name (U ("__ImageBase"), val);
578 set_entry_point (void)
581 const char *initial_symbol_char;
591 { 1, "NtProcessStartup" },
592 { 2, "WinMainCRTStartup" },
593 { 3, "mainCRTStartup" },
594 { 7, "__PosixProcessStartup"},
595 { 9, "WinMainCRTStartup" },
596 {14, "mainCRTStartup" },
600 /* Entry point name for arbitrary subsystem numbers. */
601 static const char default_entry[] = "mainCRTStartup";
603 if (bfd_link_dll (&link_info) || dll)
605 #if defined (TARGET_IS_i386pe)
606 entry = "DllMainCRTStartup@12";
608 entry = "DllMainCRTStartup";
613 for (i = 0; v[i].entry; i++)
614 if (v[i].value == pe_subsystem)
617 /* If no match, use the default. */
618 if (v[i].entry != NULL)
621 entry = default_entry;
624 initial_symbol_char = (is_underscoring () != 0 ? "_" : "");
626 if (*initial_symbol_char != '\0')
630 /* lang_default_entry expects its argument to be permanently
631 allocated, so we don't free this string. */
632 alc_entry = xmalloc (strlen (initial_symbol_char)
635 strcpy (alc_entry, initial_symbol_char);
636 strcat (alc_entry, entry);
640 lang_default_entry (entry);
644 set_pe_subsystem (void)
650 unsigned long temp_subsystem;
667 /* Check for the presence of a version number. */
668 sver = strchr (optarg, ':');
670 len = strlen (optarg);
674 set_pe_name ("__major_subsystem_version__",
675 strtoul (sver + 1, &end, 0));
677 set_pe_name ("__minor_subsystem_version__",
678 strtoul (end + 1, &end, 0));
680 einfo (_("%P: warning: bad version number in -subsystem option\n"));
683 /* Check for numeric subsystem. */
684 temp_subsystem = strtoul (optarg, & end, 0);
685 if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
687 /* Search list for a numeric match to use its entry point. */
688 for (i = 0; v[i].name; i++)
689 if (v[i].value == (int) temp_subsystem)
692 /* Use this subsystem. */
693 pe_subsystem = (int) temp_subsystem;
697 /* Search for subsystem by name. */
698 for (i = 0; v[i].name; i++)
699 if (strncmp (optarg, v[i].name, len) == 0
700 && v[i].name[len] == '\0')
703 if (v[i].name == NULL)
705 einfo (_("%F%P: invalid subsystem type %s\n"), optarg);
709 pe_subsystem = v[i].value;
712 set_pe_name ("__subsystem__", pe_subsystem);
719 set_pe_value (char *name)
723 set_pe_name (name, strtoul (optarg, &end, 0));
726 einfo (_("%F%P: invalid hex number for PE parameter '%s'\n"), optarg);
733 set_pe_stack_heap (char *resname, char *comname)
735 set_pe_value (resname);
740 set_pe_value (comname);
743 einfo (_("%F%P: strange hex info for PE parameter '%s'\n"), optarg);
746 #define DEFAULT_BUILD_ID_STYLE "md5"
749 gld${EMULATION_NAME}_handle_option (int optc)
756 case OPTION_BASE_FILE:
757 link_info.base_file = fopen (optarg, FOPEN_WB);
758 if (link_info.base_file == NULL)
759 einfo (_("%F%P: cannot open base file %s\n"), optarg);
764 set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
767 set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
769 case OPTION_SUBSYSTEM:
772 case OPTION_MAJOR_OS_VERSION:
773 set_pe_value ("__major_os_version__");
775 case OPTION_MINOR_OS_VERSION:
776 set_pe_value ("__minor_os_version__");
778 case OPTION_MAJOR_SUBSYSTEM_VERSION:
779 set_pe_value ("__major_subsystem_version__");
781 case OPTION_MINOR_SUBSYSTEM_VERSION:
782 set_pe_value ("__minor_subsystem_version__");
784 case OPTION_MAJOR_IMAGE_VERSION:
785 set_pe_value ("__major_image_version__");
787 case OPTION_MINOR_IMAGE_VERSION:
788 set_pe_value ("__minor_image_version__");
790 case OPTION_FILE_ALIGNMENT:
791 set_pe_value ("__file_alignment__");
793 case OPTION_SECTION_ALIGNMENT:
794 set_pe_value ("__section_alignment__");
797 set_pe_name ("__dll__", 1);
799 case OPTION_IMAGE_BASE:
800 set_pe_value ("__image_base__");
802 case OPTION_SUPPORT_OLD_CODE:
803 support_old_code = 1;
805 case OPTION_THUMB_ENTRY:
806 thumb_entry_symbol = optarg;
808 case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
809 pe_use_nul_prefixed_import_tables = true;
811 case OPTION_NO_LEADING_UNDERSCORE:
812 pe_leading_underscore = 0;
814 case OPTION_LEADING_UNDERSCORE:
815 pe_leading_underscore = 1;
817 case OPTION_INSERT_TIMESTAMP:
818 insert_timestamp = true;
820 case OPTION_NO_INSERT_TIMESTAMP:
821 insert_timestamp = false;
825 pe_out_def_filename = xstrdup (optarg);
827 case OPTION_EXPORT_ALL:
828 pe_dll_export_everything = 1;
830 case OPTION_EXCLUDE_SYMBOLS:
831 pe_dll_add_excludes (optarg, EXCLUDESYMS);
833 case OPTION_EXCLUDE_ALL_SYMBOLS:
834 pe_dll_exclude_all_symbols = 1;
836 case OPTION_EXCLUDE_LIBS:
837 pe_dll_add_excludes (optarg, EXCLUDELIBS);
839 case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
840 pe_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
842 case OPTION_KILL_ATS:
845 case OPTION_STDCALL_ALIASES:
846 pe_dll_stdcall_aliases = 1;
848 case OPTION_ENABLE_STDCALL_FIXUP:
849 pe_enable_stdcall_fixup = 1;
851 case OPTION_DISABLE_STDCALL_FIXUP:
852 pe_enable_stdcall_fixup = 0;
854 case OPTION_WARN_DUPLICATE_EXPORTS:
855 pe_dll_warn_dup_exports = 1;
857 case OPTION_IMP_COMPAT:
858 pe_dll_compat_implib = 1;
860 case OPTION_ENABLE_AUTO_IMAGE_BASE:
861 pe_enable_auto_image_base = 1;
862 if (optarg && *optarg)
865 pe_auto_image_base = strtoul (optarg, &end, 0);
866 /* XXX should check that we actually parsed something */
869 case OPTION_DISABLE_AUTO_IMAGE_BASE:
870 pe_enable_auto_image_base = 0;
872 case OPTION_DLL_SEARCH_PREFIX:
873 pe_dll_search_prefix = xstrdup (optarg);
875 case OPTION_NO_DEFAULT_EXCLUDES:
876 pe_dll_do_default_excludes = 0;
878 case OPTION_DLL_ENABLE_AUTO_IMPORT:
879 link_info.pei386_auto_import = 1;
881 case OPTION_DLL_DISABLE_AUTO_IMPORT:
882 link_info.pei386_auto_import = 0;
884 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
885 link_info.pei386_runtime_pseudo_reloc =
886 DEFAULT_PSEUDO_RELOC_VERSION;
888 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1:
889 link_info.pei386_runtime_pseudo_reloc = 1;
891 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2:
892 link_info.pei386_runtime_pseudo_reloc = 2;
894 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
895 link_info.pei386_runtime_pseudo_reloc = 0;
897 case OPTION_ENABLE_EXTRA_PE_DEBUG:
898 pe_dll_extra_pe_debug = 1;
901 case OPTION_LARGE_ADDRESS_AWARE:
902 real_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
904 case OPTION_DISABLE_LARGE_ADDRESS_AWARE:
905 real_flags &= ~ IMAGE_FILE_LARGE_ADDRESS_AWARE;
907 case OPTION_ENABLE_LONG_SECTION_NAMES:
908 pe_use_coff_long_section_names = 1;
910 case OPTION_DISABLE_LONG_SECTION_NAMES:
911 pe_use_coff_long_section_names = 0;
913 /* Get DLLCharacteristics bits */
914 case OPTION_DYNAMIC_BASE:
915 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
917 case OPTION_ENABLE_RELOC_SECTION:
918 pe_dll_enable_reloc_section = 1;
920 case OPTION_DISABLE_RELOC_SECTION:
921 pe_dll_enable_reloc_section = 0;
923 case OPTION_DISABLE_DYNAMIC_BASE:
924 pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
926 case OPTION_FORCE_INTEGRITY:
927 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
929 case OPTION_DISABLE_FORCE_INTEGRITY:
930 pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
932 case OPTION_NX_COMPAT:
933 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
935 case OPTION_DISABLE_NX_COMPAT:
936 pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
938 case OPTION_NO_ISOLATION:
939 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
941 case OPTION_DISABLE_NO_ISOLATION:
942 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
945 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_SEH;
947 case OPTION_DISABLE_NO_SEH:
948 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_SEH;
951 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_BIND;
953 case OPTION_DISABLE_NO_BIND:
954 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_BIND;
956 case OPTION_WDM_DRIVER:
957 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
959 case OPTION_DISABLE_WDM_DRIVER:
960 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
962 case OPTION_TERMINAL_SERVER_AWARE:
963 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
965 case OPTION_DISABLE_TERMINAL_SERVER_AWARE:
966 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
968 case OPTION_BUILD_ID:
969 free ((char *) emit_build_id);
970 emit_build_id = NULL;
972 optarg = DEFAULT_BUILD_ID_STYLE;
973 if (strcmp (optarg, "none"))
974 emit_build_id = xstrdup (optarg);
978 /* Set DLLCharacteristics bits */
979 set_pe_name ("__dll_characteristics__", pe_dll_characteristics);
987 strhash (const char *str)
989 const unsigned char *s;
996 s = (const unsigned char *) str;
997 while ((c = *s++) != '\0')
999 hash += c + (c << 17);
1003 hash += len + (len << 17);
1009 /* Use the output file to create a image base for relocatable DLLs. */
1011 static unsigned long
1012 compute_dll_image_base (const char *ofile)
1014 unsigned long hash = strhash (ofile);
1015 return pe_auto_image_base + ((hash << 16) & 0x0FFC0000);
1019 /* Assign values to the special symbols before the linker script is
1023 gld${EMULATION_NAME}_set_symbols (void)
1025 /* Run through and invent symbols for all the
1026 names and insert the defaults. */
1031 if (!init[IMAGEBASEOFF].inited)
1033 if (bfd_link_relocatable (&link_info))
1034 init[IMAGEBASEOFF].value = 0;
1035 else if (init[DLLOFF].value || bfd_link_dll (&link_info))
1038 init[IMAGEBASEOFF].value = (pe_enable_auto_image_base
1039 ? compute_dll_image_base (output_filename)
1040 : NT_DLL_IMAGE_BASE);
1042 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
1046 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
1047 init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
1050 /* Don't do any symbol assignments if this is a relocatable link. */
1051 if (bfd_link_relocatable (&link_info))
1054 /* Glue the assignments into the abs section. */
1055 push_stat_ptr (&abs_output_section->children);
1057 for (j = 0; init[j].ptr; j++)
1059 long val = init[j].value;
1060 lang_assignment_statement_type *rv;
1062 rv = lang_add_assignment (exp_assign (GET_INIT_SYMBOL_NAME (j),
1063 exp_intop (val), false));
1064 if (init[j].size == sizeof (short))
1065 *(short *) init[j].ptr = val;
1066 else if (init[j].size == sizeof (int))
1067 *(int *) init[j].ptr = val;
1068 else if (init[j].size == sizeof (long))
1069 *(long *) init[j].ptr = val;
1070 /* This might be a long long or other special type. */
1071 else if (init[j].size == sizeof (bfd_vma))
1072 *(bfd_vma *) init[j].ptr = val;
1074 if (j == IMAGEBASEOFF)
1075 image_base_statement = rv;
1077 /* Restore the pointer. */
1080 if (pe.FileAlignment > pe.SectionAlignment)
1082 einfo (_("%P: warning, file alignment > section alignment\n"));
1086 /* This is called after the linker script and the command line options
1090 gld${EMULATION_NAME}_after_parse (void)
1092 /* PR ld/6744: Warn the user if they have used an ELF-only
1093 option hoping it will work on PE. */
1094 if (link_info.export_dynamic)
1095 einfo (_("%P: warning: --export-dynamic is not supported for PE "
1096 "targets, did you mean --export-all-symbols?\n"));
1100 after_parse_default ();
1104 static struct bfd_link_hash_entry *pe_undef_found_sym;
1107 pe_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
1111 const char *hs = h->root.string;
1113 sl = strlen (string);
1114 if (h->type == bfd_link_hash_defined
1115 && ((*hs == '@' && *string == '_'
1116 && strncmp (hs + 1, string + 1, sl - 1) == 0)
1117 || strncmp (hs, string, sl) == 0)
1118 && h->root.string[sl] == '@')
1120 pe_undef_found_sym = h;
1126 /* Change UNDEF to a defined symbol, taking data from SYM. */
1129 change_undef (struct bfd_link_hash_entry * undef,
1130 struct bfd_link_hash_entry * sym)
1132 static bool gave_warning_message = false;
1134 undef->type = bfd_link_hash_defined;
1135 undef->u.def.value = sym->u.def.value;
1136 undef->u.def.section = sym->u.def.section;
1138 if (pe_enable_stdcall_fixup == -1)
1140 einfo (_("%P: warning: resolving %s by linking to %s\n"),
1141 undef->root.string, sym->root.string);
1143 if (! gave_warning_message)
1145 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
1146 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
1147 gave_warning_message = true;
1151 /* PR 19803: Make sure that the linked symbol is not garbage collected. */
1152 lang_add_gc_name (sym->root.string);
1156 pe_fixup_stdcalls (void)
1158 struct bfd_link_hash_entry *undef, *sym;
1160 if (pe_dll_extra_pe_debug)
1161 printf ("%s\n", __FUNCTION__);
1163 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
1164 if (undef->type == bfd_link_hash_undefined)
1166 const char * name = undef->root.string;
1168 int lead_at = (*name == '@');
1171 at = strchr (name + 1, '@');
1173 at = strchr (name, '@');
1177 /* The symbol is a stdcall symbol, so let's look for a
1178 cdecl symbol with the same name and resolve to that. */
1179 char *cname = xstrdup (name);
1184 * strchr (cname, '@') = 0;
1185 sym = bfd_link_hash_lookup (link_info.hash, cname, false, false, true);
1187 if (sym && sym->type == bfd_link_hash_defined)
1188 change_undef (undef, sym);
1192 /* The symbol is a cdecl symbol, so we look for stdcall
1193 symbols - which means scanning the whole symbol table. */
1194 pe_undef_found_sym = NULL;
1195 bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
1197 if (pe_undef_found_sym)
1198 change_undef (undef, pe_undef_found_sym);
1204 make_import_fixup (arelent *rel, asection *s, char *name, const char *symname)
1206 struct bfd_symbol *sym = *rel->sym_ptr_ptr;
1210 if (pe_dll_extra_pe_debug)
1211 printf ("arelent: %s@%#lx: add=%li\n", sym->name,
1212 (unsigned long) rel->address, (long) rel->addend);
1214 if (! bfd_get_section_contents (s->owner, s, addend, rel->address, sizeof (addend)))
1215 einfo (_("%P: %C: cannot get section contents - auto-import exception\n"),
1216 s->owner, s, rel->address);
1218 _addend = bfd_get_32 (s->owner, addend);
1219 pe_create_import_fixup (rel, s, _addend, name, symname);
1223 pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
1225 printf ("+%s\n", h->string);
1229 #endif /* DLL_SUPPORT */
1232 debug_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
1234 int *found = (int *) obj;
1235 if (strncmp (".debug_", sect->name, sizeof (".debug_") - 1) == 0)
1240 pecoff_checksum_contents (bfd *abfd,
1241 void (*process) (const void *, size_t, void *),
1244 file_ptr filepos = (file_ptr) 0;
1251 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
1254 status = bfd_bread (&b, (bfd_size_type) 1, abfd);
1260 (*process) (&b, 1, arg);
1268 write_build_id (bfd *abfd)
1270 struct pe_tdata *t = pe_data (abfd);
1272 struct bfd_link_order *link_order = NULL;
1273 unsigned char *contents;
1275 bfd_size_type build_id_size;
1276 unsigned char *build_id;
1278 /* Find the section the .buildid output section has been merged info. */
1279 for (asec = abfd->sections; asec != NULL; asec = asec->next)
1281 struct bfd_link_order *l = NULL;
1282 for (l = asec->map_head.link_order; l != NULL; l = l->next)
1284 if (l->type == bfd_indirect_link_order)
1286 if (l->u.indirect.section == t->build_id.sec)
1300 einfo (_("%P: warning: .buildid section discarded,"
1301 " --build-id ignored\n"));
1305 if (t->build_id.sec->contents == NULL)
1306 t->build_id.sec->contents = (unsigned char *) xmalloc (t->build_id.sec->size);
1307 contents = t->build_id.sec->contents;
1308 size = t->build_id.sec->size;
1310 build_id_size = compute_build_id_size (t->build_id.style);
1311 build_id = xmalloc (build_id_size);
1312 generate_build_id (abfd, t->build_id.style, pecoff_checksum_contents, build_id, build_id_size);
1314 bfd_vma ib = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase;
1316 /* Construct a debug directory entry which points to an immediately following CodeView record. */
1317 struct internal_IMAGE_DEBUG_DIRECTORY idd;
1318 idd.Characteristics = 0;
1319 idd.TimeDateStamp = 0;
1320 idd.MajorVersion = 0;
1321 idd.MinorVersion = 0;
1322 idd.Type = PE_IMAGE_DEBUG_TYPE_CODEVIEW;
1323 idd.SizeOfData = sizeof (CV_INFO_PDB70) + 1;
1324 idd.AddressOfRawData = asec->vma - ib + link_order->offset
1325 + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1326 idd.PointerToRawData = asec->filepos + link_order->offset
1327 + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1329 struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *)contents;
1330 _bfd_XXi_swap_debugdir_out (abfd, &idd, ext);
1332 /* Write the debug directory entry. */
1333 if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0)
1336 if (bfd_bwrite (contents, size, abfd) != size)
1339 /* Construct the CodeView record. */
1340 CODEVIEW_INFO cvinfo;
1341 cvinfo.CVSignature = CVINFO_PDB70_CVSIGNATURE;
1344 /* Zero pad or truncate the generated build_id to fit in the CodeView record. */
1345 memset (&(cvinfo.Signature), 0, CV_INFO_SIGNATURE_LENGTH);
1346 memcpy (&(cvinfo.Signature), build_id, (build_id_size > CV_INFO_SIGNATURE_LENGTH)
1347 ? CV_INFO_SIGNATURE_LENGTH : build_id_size);
1351 /* Write the codeview record. */
1352 if (_bfd_XXi_write_codeview_record (abfd, idd.PointerToRawData, &cvinfo) == 0)
1355 /* Record the location of the debug directory in the data directory. */
1356 pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress
1357 = asec->vma - ib + link_order->offset;
1358 pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
1359 = sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1364 /* Make .buildid section, and set up coff_tdata->build_id. */
1366 setup_build_id (bfd *ibfd)
1371 if (!validate_build_id_style (emit_build_id))
1373 einfo (_("%P: warning: unrecognized --build-id style ignored\n"));
1377 flags = (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY
1378 | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA);
1379 s = bfd_make_section_anyway_with_flags (ibfd, ".buildid", flags);
1382 struct pe_tdata *t = pe_data (link_info.output_bfd);
1383 t->build_id.after_write_object_contents = &write_build_id;
1384 t->build_id.style = emit_build_id;
1385 t->build_id.sec = s;
1387 /* Section is a fixed size:
1388 One IMAGE_DEBUG_DIRECTORY entry, of type IMAGE_DEBUG_TYPE_CODEVIEW,
1389 pointing at a CV_INFO_PDB70 record containing the build-id, with a
1390 null byte for PdbFileName. */
1391 s->size = sizeof (struct external_IMAGE_DEBUG_DIRECTORY)
1392 + sizeof (CV_INFO_PDB70) + 1;
1397 einfo (_("%P: warning: cannot create .buildid section,"
1398 " --build-id ignored\n"));
1403 gld${EMULATION_NAME}_after_open (void)
1405 after_open_default ();
1408 if (pe_dll_extra_pe_debug)
1411 struct bfd_link_hash_entry *sym;
1413 printf ("%s()\n", __FUNCTION__);
1415 for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1416 printf ("-%s\n", sym->root.string);
1417 bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
1419 for (a = link_info.input_bfds; a; a = a->link.next)
1420 printf ("*%s\n", bfd_get_filename (a));
1424 if (emit_build_id != NULL)
1428 /* Find a COFF input. */
1429 for (abfd = link_info.input_bfds;
1430 abfd != (bfd *) NULL; abfd = abfd->link.next)
1431 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
1434 /* If there are no COFF input files do not try to
1435 add a build-id section. */
1437 || !setup_build_id (abfd))
1439 free ((char *) emit_build_id);
1440 emit_build_id = NULL;
1444 /* Pass the wacky PE command line options into the output bfd.
1445 FIXME: This should be done via a function, rather than by
1446 including an internal BFD header. */
1448 if (bfd_get_flavour (link_info.output_bfd) != bfd_target_coff_flavour
1449 || coff_data (link_info.output_bfd) == NULL
1450 || coff_data (link_info.output_bfd)->pe == 0)
1451 einfo (_("%F%P: cannot perform PE operations on non PE output file '%pB'\n"),
1452 link_info.output_bfd);
1454 pe_data (link_info.output_bfd)->pe_opthdr = pe;
1455 pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
1456 pe_data (link_info.output_bfd)->real_flags |= real_flags;
1457 if (insert_timestamp)
1458 pe_data (link_info.output_bfd)->timestamp = -1;
1460 pe_data (link_info.output_bfd)->timestamp = 0;
1462 /* At this point we must decide whether to use long section names
1463 in the output or not. If the user hasn't explicitly specified
1464 on the command line, we leave it to the default for the format
1465 (object files yes, image files no), except if there is debug
1466 information present; GDB relies on the long section names to
1467 find it, so enable it in that case. */
1468 if (pe_use_coff_long_section_names < 0 && link_info.strip == strip_none)
1470 if (bfd_link_relocatable (&link_info))
1471 pe_use_coff_long_section_names = 1;
1474 /* Iterate over all sections of all input BFDs, checking
1475 for any that begin 'debug_' and are long names. */
1476 LANG_FOR_EACH_INPUT_STATEMENT (is)
1478 int found_debug = 0;
1480 bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug);
1483 pe_use_coff_long_section_names = 1;
1490 pe_output_file_set_long_section_names (link_info.output_bfd);
1493 pe_process_import_defs (link_info.output_bfd, &link_info);
1495 if (link_info.pei386_auto_import) /* -1=warn or 1=enable */
1496 pe_find_data_imports (U ("_head_"), make_import_fixup);
1498 /* The implementation of the feature is rather dumb and would cause the
1499 compilation time to go through the roof if there are many undefined
1500 symbols in the link, so it needs to be run after auto-import. */
1501 if (pe_enable_stdcall_fixup) /* -1=warn or 1=enable */
1502 pe_fixup_stdcalls ();
1504 #if defined (TARGET_IS_i386pe) \
1505 || defined (TARGET_IS_armpe) \
1506 || defined (TARGET_IS_arm_wince_pe)
1507 if (!bfd_link_relocatable (&link_info))
1508 pe_dll_build_sections (link_info.output_bfd, &link_info);
1510 if (bfd_link_pic (&link_info))
1511 pe_dll_build_sections (link_info.output_bfd, &link_info);
1513 pe_exe_build_sections (link_info.output_bfd, &link_info);
1515 #endif /* DLL_SUPPORT */
1517 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
1518 if (strstr (bfd_get_target (link_info.output_bfd), "arm") == NULL)
1520 /* The arm backend needs special fields in the output hash structure.
1521 These will only be created if the output format is an arm format,
1522 hence we do not support linking and changing output formats at the
1523 same time. Use a link followed by objcopy to change output formats. */
1524 einfo (_("%F%P: error: cannot change output format "
1525 "whilst linking %s binaries\n"), "ARM");
1529 /* Find a BFD that can hold the interworking stubs. */
1530 LANG_FOR_EACH_INPUT_STATEMENT (is)
1532 if (bfd_arm_get_bfd_for_interworking (is->the_bfd, & link_info))
1539 /* This next chunk of code tries to detect the case where you have
1540 two import libraries for the same DLL (specifically,
1541 symbolically linking libm.a and libc.a in cygwin to
1542 libcygwin.a). In those cases, it's possible for function
1543 thunks from the second implib to be used but without the
1544 head/tail objects, causing an improper import table. We detect
1545 those cases and rename the "other" import libraries to match
1546 the one the head/tail come from, so that the linker will sort
1547 things nicely and produce a valid import table. */
1549 LANG_FOR_EACH_INPUT_STATEMENT (is)
1551 if (is->the_bfd->my_archive)
1553 int idata2 = 0, reloc_count=0, is_imp = 0;
1556 /* See if this is an import library thunk. */
1557 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1559 if (strcmp (sec->name, ".idata\$2") == 0)
1561 if (startswith (sec->name, ".idata\$"))
1563 reloc_count += sec->reloc_count;
1566 if (is_imp && !idata2 && reloc_count)
1568 /* It is, look for the reference to head and see if it's
1569 from our own library. */
1570 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1578 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1582 if (!bfd_generic_link_read_symbols (is->the_bfd))
1584 einfo (_("%F%P: %pB: could not read symbols: %E\n"),
1588 symbols = bfd_get_outsymbols (is->the_bfd);
1590 relocs = xmalloc ((size_t) relsize);
1591 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1596 einfo (_("%X%P: unable to process relocs: %E\n"));
1600 for (i = 0; i < nrelocs; i++)
1602 struct bfd_symbol *s;
1603 struct bfd_link_hash_entry * blhe;
1605 const char *other_bfd_filename;
1607 s = (relocs[i]->sym_ptr_ptr)[0];
1609 if (s->flags & BSF_LOCAL)
1612 /* Thunk section with reloc to another bfd. */
1613 blhe = bfd_link_hash_lookup (link_info.hash,
1615 false, false, true);
1618 || blhe->type != bfd_link_hash_defined)
1621 other_bfd = blhe->u.def.section->owner;
1622 if (other_bfd->my_archive == is->the_bfd->my_archive)
1626 = (other_bfd->my_archive
1627 ? bfd_get_filename (other_bfd->my_archive)
1628 : bfd_get_filename (other_bfd));
1630 if (filename_cmp (bfd_get_filename
1631 (is->the_bfd->my_archive),
1632 other_bfd_filename) == 0)
1635 /* Sort this implib to match the other one. */
1636 lang_input_statement_type *arch_is
1637 = bfd_usrdata (is->the_bfd->my_archive);
1638 arch_is->sort_key = other_bfd_filename;
1643 /* Note - we do not free the symbols,
1644 they are now cached in the BFD. */
1653 /* Careful - this is a shell script. Watch those dollar signs! */
1654 /* Microsoft import libraries have every member named the same,
1655 and not in the right order for us to link them correctly. We
1656 must detect these and rename the members so that they'll link
1657 correctly. There are three types of objects: the head, the
1658 thunks, and the sentinel(s). The head is easy; it's the one
1659 with idata2. We assume that the sentinels won't have relocs,
1660 and the thunks will. It's easier than checking the symbol
1661 table for external references. */
1662 LANG_FOR_EACH_INPUT_STATEMENT (is)
1664 if (is->the_bfd->my_archive)
1668 /* Microsoft import libraries may contain archive members for
1669 one or more DLLs, together with static object files.
1670 Inspect all members that are named *.dll - check whether
1671 they contain .idata sections. Do the renaming of all
1672 archive members that seem to be Microsoft style import
1674 pnt = strrchr (bfd_get_filename (is->the_bfd), '.');
1676 if (pnt != NULL && (fileext_cmp (pnt + 1, "dll") == 0))
1678 int idata2 = 0, reloc_count = 0, idata = 0;
1680 char *new_name, seq;
1682 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1684 if (strcmp (sec->name, ".idata\$2") == 0)
1686 if (strncmp (sec->name, ".idata\$", 6) == 0)
1688 reloc_count += sec->reloc_count;
1691 /* An archive member named .dll, but not having any .idata
1692 sections - apparently not a Microsoft import object
1693 after all: Skip renaming it. */
1697 if (idata2) /* .idata2 is the TOC */
1699 else if (reloc_count > 0) /* thunks */
1705 = xmalloc (strlen (bfd_get_filename (is->the_bfd)) + 3);
1706 sprintf (new_name, "%s.%c",
1707 bfd_get_filename (is->the_bfd), seq);
1708 is->sort_key = new_name;
1715 /* The following chunk of code tries to identify jump stubs in
1716 import libraries which are dead code and eliminates them
1717 from the final link. For each exported symbol <sym>, there
1718 is a object file in the import library with a .text section
1719 and several .idata\$* sections. The .text section contains the
1720 symbol definition for <sym> which is a jump stub of the form
1721 jmp *__imp_<sym>. The .idata\$5 contains the symbol definition
1722 for __imp_<sym> which is the address of the slot for <sym> in
1723 the import address table. When a symbol is imported explicitly
1724 using __declspec(dllimport) declaration, the compiler generates
1725 a reference to __imp_<sym> which directly resolves to the
1726 symbol in .idata\$5, in which case the jump stub code is not
1727 needed. The following code tries to identify jump stub sections
1728 in import libraries which are not referred to by anyone and
1729 marks them for exclusion from the final link. */
1730 LANG_FOR_EACH_INPUT_STATEMENT (is)
1732 if (is->the_bfd->my_archive)
1735 asection *sec, *stub_sec = NULL;
1737 /* See if this is an import library thunk. */
1738 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1740 if (strncmp (sec->name, ".idata\$", 7) == 0)
1742 /* The section containing the jmp stub has code
1744 if ((sec->flags & SEC_CODE) && sec->reloc_count)
1748 if (is_imp && stub_sec)
1751 long nsyms, src_count;
1752 struct bfd_link_hash_entry * blhe;
1754 if (!bfd_generic_link_read_symbols (is->the_bfd))
1756 einfo (_("%F%P: %pB: could not read symbols: %E\n"),
1760 symbols = bfd_get_outsymbols (is->the_bfd);
1761 nsyms = bfd_get_symcount (is->the_bfd);
1763 for (src_count = 0; src_count < nsyms; src_count++)
1765 if (symbols[src_count]->section->id == stub_sec->id)
1767 /* This symbol belongs to the section containing
1769 blhe = bfd_link_hash_lookup (link_info.hash,
1770 symbols[src_count]->name,
1771 false, false, true);
1772 /* If the symbol in the stub section has no other
1773 undefined references, exclude the stub section
1774 from the final link. */
1776 && blhe->type == bfd_link_hash_defined
1777 && blhe->u.undef.next == NULL
1778 && blhe != link_info.hash->undefs_tail)
1779 stub_sec->flags |= SEC_EXCLUDE;
1789 gld${EMULATION_NAME}_before_allocation (void)
1791 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
1792 /* FIXME: we should be able to set the size of the interworking stub
1795 Here we rummage through the found bfds to collect glue
1796 information. FIXME: should this be based on a command line
1797 option? krk@cygnus.com. */
1799 LANG_FOR_EACH_INPUT_STATEMENT (is)
1801 if (! bfd_arm_process_before_allocation
1802 (is->the_bfd, & link_info, support_old_code))
1804 /* xgettext:c-format */
1805 einfo (_("%P: errors encountered processing file %s for interworking\n"),
1811 /* We have seen it all. Allocate it, and carry on. */
1812 bfd_arm_allocate_interworking_sections (& link_info);
1813 #endif /* TARGET_IS_armpe || TARGET_IS_arm_wince_pe */
1815 before_allocation_default ();
1819 /* This is called when an input file isn't recognized as a BFD. We
1820 check here for .DEF files and pull them in automatically. */
1823 saw_option (char *option)
1827 for (i = 0; init[i].ptr; i++)
1828 if (strcmp (GET_INIT_SYMBOL_NAME (i), option) == 0)
1829 return init[i].inited;
1832 #endif /* DLL_SUPPORT */
1835 gld${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1838 const char *ext = strrchr (entry->filename, '.');
1840 if (ext != NULL && fileext_cmp (ext + 1, "def") == 0)
1842 pe_def_file = def_file_parse (entry->filename, pe_def_file);
1846 int i, buflen=0, len;
1849 for (i = 0; i < pe_def_file->num_exports; i++)
1851 len = strlen (pe_def_file->exports[i].internal_name);
1852 if (buflen < len + 2)
1856 buf = xmalloc (buflen);
1858 for (i = 0; i < pe_def_file->num_exports; i++)
1860 struct bfd_link_hash_entry *h;
1862 sprintf (buf, "%s%s", U (""),
1863 pe_def_file->exports[i].internal_name);
1865 h = bfd_link_hash_lookup (link_info.hash, buf, true, true, true);
1866 if (h == (struct bfd_link_hash_entry *) NULL)
1867 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
1868 if (h->type == bfd_link_hash_new)
1870 h->type = bfd_link_hash_undefined;
1871 h->u.undef.abfd = NULL;
1872 bfd_link_add_undef (link_info.hash, h);
1877 /* def_file_print (stdout, pe_def_file); */
1878 if (pe_def_file->is_dll == 1)
1879 link_info.type = type_dll;
1881 if (pe_def_file->base_address != (bfd_vma)(-1))
1884 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1885 = init[IMAGEBASEOFF].value
1886 = pe_def_file->base_address;
1887 init[IMAGEBASEOFF].inited = 1;
1888 if (image_base_statement)
1889 image_base_statement->exp
1890 = exp_assign ("__image_base__", exp_intop (pe.ImageBase),
1894 if (pe_def_file->stack_reserve != -1
1895 && ! saw_option ("__size_of_stack_reserve__"))
1897 pe.SizeOfStackReserve = pe_def_file->stack_reserve;
1898 if (pe_def_file->stack_commit != -1)
1899 pe.SizeOfStackCommit = pe_def_file->stack_commit;
1901 if (pe_def_file->heap_reserve != -1
1902 && ! saw_option ("__size_of_heap_reserve__"))
1904 pe.SizeOfHeapReserve = pe_def_file->heap_reserve;
1905 if (pe_def_file->heap_commit != -1)
1906 pe.SizeOfHeapCommit = pe_def_file->heap_commit;
1916 gld${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1919 #ifdef TARGET_IS_i386pe
1920 pe_dll_id_target ("pei-i386");
1922 #ifdef TARGET_IS_shpe
1923 pe_dll_id_target ("pei-shl");
1925 #ifdef TARGET_IS_armpe
1926 pe_dll_id_target ("pei-arm-little");
1928 #ifdef TARGET_IS_arm_wince_pe
1929 pe_dll_id_target ("pei-arm-wince-little");
1931 if (pe_bfd_is_dll (entry->the_bfd))
1932 return pe_implied_import_dll (entry->filename);
1938 gld${EMULATION_NAME}_finish (void)
1940 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
1941 struct bfd_link_hash_entry * h;
1943 if (thumb_entry_symbol != NULL)
1945 h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol,
1946 false, false, true);
1948 if (h != (struct bfd_link_hash_entry *) NULL
1949 && (h->type == bfd_link_hash_defined
1950 || h->type == bfd_link_hash_defweak)
1951 && h->u.def.section->output_section != NULL)
1953 static char buffer[32];
1956 /* Special procesing is required for a Thumb entry symbol. The
1957 bottom bit of its address must be set. */
1958 val = (h->u.def.value
1959 + bfd_section_vma (h->u.def.section->output_section)
1960 + h->u.def.section->output_offset);
1964 /* Now convert this value into a string and store it in entry_symbol
1965 where the lang_finish() function will pick it up. */
1966 sprintf (buffer, "0x%" PRIx64, (uint64_t) val);
1968 if (entry_symbol.name != NULL && entry_from_cmdline)
1969 einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
1970 thumb_entry_symbol, entry_symbol.name);
1971 entry_symbol.name = buffer;
1974 einfo (_("%P: warning: cannot find thumb start symbol %s\n"), thumb_entry_symbol);
1976 #endif /* defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe) */
1981 if (bfd_link_pic (&link_info)
1982 #if !defined(TARGET_IS_shpe)
1983 || pe_dll_enable_reloc_section
1984 || (!bfd_link_relocatable (&link_info)
1985 && pe_def_file->num_exports != 0)
1989 pe_dll_fill_sections (link_info.output_bfd, &link_info);
1990 if (command_line.out_implib_filename
1991 && (pe_def_file->num_exports != 0
1992 || bfd_link_pic (&link_info)))
1993 pe_dll_generate_implib (pe_def_file, command_line.out_implib_filename,
1996 #if defined(TARGET_IS_shpe)
1997 /* ARM doesn't need relocs. */
2000 pe_exe_fill_sections (link_info.output_bfd, &link_info);
2004 if (pe_out_def_filename)
2005 pe_dll_generate_def_file (pe_out_def_filename);
2006 #endif /* DLL_SUPPORT */
2008 /* I don't know where .idata gets set as code, but it shouldn't be. */
2010 asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
2014 asec->flags &= ~SEC_CODE;
2015 asec->flags |= SEC_DATA;
2021 /* Place an orphan section.
2023 We use this to put sections in a reasonable place in the file, and
2024 to ensure that they are aligned as required.
2026 We handle grouped sections here as well. A section named .foo\$nn
2027 goes into the output section .foo. All grouped sections are sorted
2030 Grouped sections for the default sections are handled by the
2031 default linker script using wildcards, and are sorted by
2034 static lang_output_section_statement_type *
2035 gld${EMULATION_NAME}_place_orphan (asection *s,
2036 const char *secname,
2039 const char *orig_secname = secname;
2040 char *dollar = NULL;
2041 lang_output_section_statement_type *os;
2042 lang_statement_list_type add_child;
2043 lang_output_section_statement_type *match_by_name = NULL;
2044 lang_statement_union_type **pl;
2046 /* Look through the script to see where to place this section. */
2047 if (!bfd_link_relocatable (&link_info)
2048 && (dollar = strchr (secname, '\$')) != NULL)
2050 size_t len = dollar - secname;
2051 char *newname = xmalloc (len + 1);
2052 memcpy (newname, secname, len);
2053 newname[len] = '\0';
2057 lang_list_init (&add_child);
2060 if (constraint == 0)
2061 for (os = lang_output_section_find (secname);
2063 os = next_matching_output_section_statement (os, 0))
2065 /* If we don't match an existing output section, tell
2066 lang_insert_orphan to create a new output section. */
2067 constraint = SPECIAL;
2069 if (os->bfd_section != NULL
2070 && (os->bfd_section->flags == 0
2071 || ((s->flags ^ os->bfd_section->flags)
2072 & (SEC_LOAD | SEC_ALLOC)) == 0))
2074 /* We already have an output section statement with this
2075 name, and its bfd section has compatible flags.
2076 If the section already exists but does not have any flags set,
2077 then it has been created by the linker, probably as a result of
2078 a --section-start command line switch. */
2079 lang_add_section (&add_child, s, NULL, NULL, os);
2083 /* Save unused output sections in case we can match them
2084 against orphans later. */
2085 if (os->bfd_section == NULL)
2089 /* If we didn't match an active output section, see if we matched an
2090 unused one and use that. */
2091 if (os == NULL && match_by_name)
2093 lang_add_section (&match_by_name->children, s, NULL, NULL, match_by_name);
2094 return match_by_name;
2099 static struct orphan_save hold[] =
2102 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
2105 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
2108 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
2111 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
2117 enum orphan_save_index
2125 static int orphan_init_done = 0;
2126 struct orphan_save *place;
2127 lang_output_section_statement_type *after;
2128 etree_type *address;
2132 if (!orphan_init_done)
2134 struct orphan_save *ho;
2135 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
2136 if (ho->name != NULL)
2138 ho->os = lang_output_section_find (ho->name);
2139 if (ho->os != NULL && ho->os->flags == 0)
2140 ho->os->flags = ho->flags;
2142 orphan_init_done = 1;
2146 if (!bfd_link_relocatable (&link_info))
2149 while ((nexts = bfd_get_next_section_by_name (nexts->owner,
2151 if (nexts->output_section == NULL
2152 && (nexts->flags & SEC_EXCLUDE) == 0
2153 && ((nexts->flags ^ flags) & (SEC_LOAD | SEC_ALLOC)) == 0
2154 && (nexts->owner->flags & DYNAMIC) == 0
2155 && !bfd_input_just_syms (nexts->owner))
2156 flags = (((flags ^ SEC_READONLY)
2157 | (nexts->flags ^ SEC_READONLY))
2161 /* Try to put the new output section in a reasonable place based
2162 on the section name and section flags. */
2165 if ((flags & SEC_ALLOC) == 0)
2167 else if ((flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2168 place = &hold[orphan_bss];
2169 else if ((flags & SEC_READONLY) == 0)
2170 place = &hold[orphan_data];
2171 else if ((flags & SEC_CODE) == 0)
2173 place = (!strncmp (secname, ".idata\$", 7) ? &hold[orphan_idata]
2174 : &hold[orphan_rodata]);
2177 place = &hold[orphan_text];
2182 if (place->os == NULL)
2183 place->os = lang_output_section_find (place->name);
2186 after = lang_output_section_find_by_flags (s, flags, &place->os,
2189 /* *ABS* is always the first output section statement. */
2190 after = (void *) lang_os_list.head;
2193 /* All sections in an executable must be aligned to a page boundary.
2194 In a relocatable link, just preserve the incoming alignment; the
2195 address is discarded by lang_insert_orphan in that case, anyway. */
2196 address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
2197 os = lang_insert_orphan (s, secname, constraint, after, place, address,
2199 if (bfd_link_relocatable (&link_info))
2201 os->section_alignment = exp_intop (1U << s->alignment_power);
2202 os->bfd_section->alignment_power = s->alignment_power;
2206 /* If the section name has a '\$', sort it with the other '\$'
2208 for (pl = &os->children.head; *pl != NULL; pl = &(*pl)->header.next)
2210 lang_input_section_type *ls;
2213 if ((*pl)->header.type != lang_input_section_enum)
2216 ls = &(*pl)->input_section;
2218 lname = bfd_section_name (ls->section);
2219 if (strchr (lname, '\$') != NULL
2220 && (dollar == NULL || strcmp (orig_secname, lname) < 0))
2224 if (add_child.head != NULL)
2226 *add_child.tail = *pl;
2227 *pl = add_child.head;
2234 gld${EMULATION_NAME}_open_dynamic_archive
2235 (const char *arch ATTRIBUTE_UNUSED,
2236 search_dirs_type *search,
2237 lang_input_statement_type *entry)
2241 const char * format;
2246 /* Preferred explicit import library for dll's. */
2247 { "lib%s.dll.a", false },
2248 /* Alternate explicit import library for dll's. */
2249 { "%s.dll.a", false },
2250 /* "libfoo.a" could be either an import lib or a static lib.
2251 For backwards compatibility, libfoo.a needs to precede
2252 libfoo.dll and foo.dll in the search. */
2253 { "lib%s.a", false },
2254 /* The 'native' spelling of an import lib name is "foo.lib". */
2255 { "%s.lib", false },
2256 /* PR 22948 - Check for an import library. */
2257 { "lib%s.lib", false },
2259 /* Try "<prefix>foo.dll" (preferred dll name, if specified). */
2260 { "%s%s.dll", true },
2262 /* Try "libfoo.dll" (default preferred dll name). */
2263 { "lib%s.dll", false },
2264 /* Finally try 'native' dll name "foo.dll". */
2265 { "%s.dll", false },
2266 /* Note: If adding more formats to this table, make sure to check to
2267 see if their length is longer than libname_fmt[0].format, and if
2268 so, update the call to xmalloc() below. */
2271 static unsigned int format_max_len = 0;
2272 const char * filename;
2278 if (! entry->flags.maybe_archive || entry->flags.full_name_provided)
2281 filename = entry->filename;
2283 if (format_max_len == 0)
2284 /* We need to allow space in the memory that we are going to allocate
2285 for the characters in the format string. Since the format array is
2286 static we only need to calculate this information once. In theory
2287 this value could also be computed statically, but this introduces
2288 the possibility for a discrepancy and hence a possible memory
2289 corruption. The lengths we compute here will be too long because
2290 they will include any formating characters (%s) in the strings, but
2291 this will not matter. */
2292 for (i = 0; libname_fmt[i].format; i++)
2293 if (format_max_len < strlen (libname_fmt[i].format))
2294 format_max_len = strlen (libname_fmt[i].format);
2296 full_string = xmalloc (strlen (search->name)
2300 + (pe_dll_search_prefix
2301 ? strlen (pe_dll_search_prefix) : 0)
2303 /* Allow for the terminating NUL and for the path
2304 separator character that is inserted between
2305 search->name and the start of the format string. */
2308 sprintf (full_string, "%s/", search->name);
2309 base_string = full_string + strlen (full_string);
2311 for (i = 0; libname_fmt[i].format; i++)
2314 if (libname_fmt[i].use_prefix)
2316 if (!pe_dll_search_prefix)
2318 sprintf (base_string, libname_fmt[i].format, pe_dll_search_prefix, filename);
2322 sprintf (base_string, libname_fmt[i].format, filename);
2324 if (ldfile_try_open_bfd (full_string, entry))
2328 if (!libname_fmt[i].format)
2334 entry->filename = full_string;
2340 gld${EMULATION_NAME}_find_potential_libraries
2341 (char *name, lang_input_statement_type *entry)
2343 return ldfile_open_file_search (name, entry, "", ".lib");
2347 gld${EMULATION_NAME}_get_script (int *isfile)
2350 if test x"$COMPILE_IN" = xyes
2352 # Scripts compiled in.
2354 # sed commands to quote an ld script as a C string.
2355 sc="-f stringify.sed"
2361 if (bfd_link_relocatable (&link_info) && config.build_constructors)
2364 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
2365 echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
2366 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
2367 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
2368 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
2369 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
2370 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
2371 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
2372 echo ' ; else if (link_info.pei386_auto_import == 1 && (MERGE_RDATA_V2 || link_info.pei386_runtime_pseudo_reloc != 2)) return' >> e${EMULATION_NAME}.c
2373 sed $sc ldscripts/${EMULATION_NAME}.xa >> e${EMULATION_NAME}.c
2375 echo ' ; else return' >> e${EMULATION_NAME}.c
2376 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
2377 echo '; }' >> e${EMULATION_NAME}.c
2380 # Scripts read from the filesystem.
2386 if (bfd_link_relocatable (&link_info) && config.build_constructors)
2387 return "ldscripts/${EMULATION_NAME}.xu";
2388 else if (bfd_link_relocatable (&link_info))
2389 return "ldscripts/${EMULATION_NAME}.xr";
2390 else if (!config.text_read_only)
2391 return "ldscripts/${EMULATION_NAME}.xbn";
2392 else if (!config.magic_demand_paged)
2393 return "ldscripts/${EMULATION_NAME}.xn";
2395 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
2397 else if (link_info.pei386_auto_import == 1
2398 && (MERGE_RDATA_V2 || link_info.pei386_runtime_pseudo_reloc != 2))
2399 return "ldscripts/${EMULATION_NAME}.xa";
2404 return "ldscripts/${EMULATION_NAME}.x";
2409 LDEMUL_AFTER_PARSE=gld${EMULATION_NAME}_after_parse
2410 LDEMUL_AFTER_OPEN=gld${EMULATION_NAME}_after_open
2411 LDEMUL_BEFORE_ALLOCATION=gld${EMULATION_NAME}_before_allocation
2412 LDEMUL_FINISH=gld${EMULATION_NAME}_finish
2413 LDEMUL_OPEN_DYNAMIC_ARCHIVE=gld${EMULATION_NAME}_open_dynamic_archive
2414 LDEMUL_PLACE_ORPHAN=gld${EMULATION_NAME}_place_orphan
2415 LDEMUL_SET_SYMBOLS=gld${EMULATION_NAME}_set_symbols
2416 LDEMUL_ADD_OPTIONS=gld${EMULATION_NAME}_add_options
2417 LDEMUL_HANDLE_OPTION=gld${EMULATION_NAME}_handle_option
2418 LDEMUL_UNRECOGNIZED_FILE=gld${EMULATION_NAME}_unrecognized_file
2419 LDEMUL_LIST_OPTIONS=gld${EMULATION_NAME}_list_options
2420 LDEMUL_RECOGNIZED_FILE=gld${EMULATION_NAME}_recognized_file
2421 LDEMUL_FIND_POTENTIAL_LIBRARIES=gld${EMULATION_NAME}_find_potential_libraries
2423 source_em ${srcdir}/emultempl/emulation.em