1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 if [ -z "$MACHINE" ]; then
6 OUTPUT_ARCH=${ARCH}:${MACHINE}
11 move_default_addr_high=1
14 move_default_addr_high=0;
18 rm -f e${EMULATION_NAME}.c
19 (echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
21 /* Copyright (C) 2006-2014 Free Software Foundation, Inc.
22 Written by Kai Tietz, OneVision Software GmbH&CoKg.
24 This file is part of the GNU Binutils.
26 This program is free software; you can redistribute it and/or modify
27 it under the terms of the GNU General Public License as published by
28 the Free Software Foundation; either version 3 of the License, or
29 (at your option) any later version.
31 This program is distributed in the hope that it will be useful,
32 but WITHOUT ANY WARRANTY; without even the implied warranty of
33 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 GNU General Public License for more details.
36 You should have received a copy of the GNU General Public License
37 along with this program; if not, write to the Free Software
38 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
39 MA 02110-1301, USA. */
42 /* For WINDOWS_XP64 and higher */
43 /* Based on pe.em, but modified for 64 bit support. */
45 #define TARGET_IS_${EMULATION_NAME}
47 #define COFF_IMAGE_WITH_PE
49 #define COFF_WITH_pex64
55 #include "libiberty.h"
56 #include "filenames.h"
67 #include "coff/internal.h"
69 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
70 header in generic PE code. */
71 #include "coff/x86_64.h"
74 /* FIXME: This is a BFD internal header file, and we should not be
76 #include "../bfd/libcoff.h"
79 #define AOUTSZ PEPAOUTSZ
80 #define PEAOUTHDR PEPAOUTHDR
84 #include "safe-ctype.h"
86 /* Permit the emulation parameters to override the default section
87 alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes
88 it seem that include/coff/internal.h should not define
89 PE_DEF_SECTION_ALIGNMENT. */
90 #if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
91 #undef PE_DEF_SECTION_ALIGNMENT
92 #define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
95 #ifdef TARGET_IS_i386pep
99 #if defined(TARGET_IS_i386pep) || ! defined(DLL_SUPPORT)
100 #define PE_DEF_SUBSYSTEM 3
101 #undef NT_EXE_IMAGE_BASE
102 #define NT_EXE_IMAGE_BASE \
103 ((bfd_vma) (${move_default_addr_high} ? 0x100400000LL \
105 #undef NT_DLL_IMAGE_BASE
106 #define NT_DLL_IMAGE_BASE \
107 ((bfd_vma) (${move_default_addr_high} ? 0x400000000LL \
109 #undef NT_DLL_AUTO_IMAGE_BASE
110 #define NT_DLL_AUTO_IMAGE_BASE \
111 ((bfd_vma) (${move_default_addr_high} ? 0x400000000LL \
113 #undef NT_DLL_AUTO_IMAGE_MASK
114 #define NT_DLL_AUTO_IMAGE_MASK \
115 ((bfd_vma) (${move_default_addr_high} ? 0x1ffff0000LL \
118 #undef NT_EXE_IMAGE_BASE
119 #define NT_EXE_IMAGE_BASE \
120 ((bfd_vma) (${move_default_addr_high} ? 0x100010000LL \
122 #undef NT_DLL_IMAGE_BASE
123 #define NT_DLL_IMAGE_BASE \
124 ((bfd_vma) (${move_default_addr_high} ? 0x110000000LL \
126 #undef NT_DLL_AUTO_IMAGE_BASE
127 #define NT_DLL_AUTO_IMAGE_BASE \
128 ((bfd_vma) (${move_default_addr_high} ? 0x120000000LL \
130 #undef NT_DLL_AUTO_IMAGE_MASK
131 #define NT_DLL_AUTO_IMAGE_MASK \
132 ((bfd_vma) (${move_default_addr_high} ? 0x0ffff0000LL \
134 #undef PE_DEF_SECTION_ALIGNMENT
135 #define PE_DEF_SUBSYSTEM 2
136 #undef PE_DEF_FILE_ALIGNMENT
137 #define PE_DEF_FILE_ALIGNMENT 0x00000200
138 #define PE_DEF_SECTION_ALIGNMENT 0x00000400
141 static struct internal_extra_pe_aouthdr pep;
143 static int pep_subsystem = ${SUBSYSTEM};
144 static flagword real_flags = IMAGE_FILE_LARGE_ADDRESS_AWARE;
145 static int support_old_code = 0;
146 static lang_assignment_statement_type *image_base_statement = 0;
147 static unsigned short pe_dll_characteristics = 0;
148 static bfd_boolean insert_timestamp = FALSE;
151 static int pep_enable_stdcall_fixup = 1; /* 0=disable 1=enable (default). */
152 static char * pep_out_def_filename = NULL;
153 static char * pep_implib_filename = NULL;
154 static int pep_enable_auto_image_base = 0;
155 static char * pep_dll_search_prefix = NULL;
158 extern const char *output_filename;
160 static int is_underscoring (void)
163 if (pep_leading_underscore != -1)
164 return pep_leading_underscore;
165 if (!bfd_get_target_info ("${OUTPUT_FORMAT}", NULL, NULL, &u, NULL))
166 bfd_get_target_info ("${RELOCATEABLE_OUTPUT_FORMAT}", NULL, NULL, &u, NULL);
170 pep_leading_underscore = (u != 0 ? 1 : 0);
171 return pep_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;
184 link_info.pei386_auto_import = 1;
185 link_info.pei386_runtime_pseudo_reloc = 2; /* Use by default version 2. */
189 /* PE format extra command line options. */
191 /* Used for setting flags in the PE header. */
194 OPTION_BASE_FILE = 300 + 1,
196 OPTION_FILE_ALIGNMENT,
198 OPTION_MAJOR_IMAGE_VERSION,
199 OPTION_MAJOR_OS_VERSION,
200 OPTION_MAJOR_SUBSYSTEM_VERSION,
201 OPTION_MINOR_IMAGE_VERSION,
202 OPTION_MINOR_OS_VERSION,
203 OPTION_MINOR_SUBSYSTEM_VERSION,
204 OPTION_SECTION_ALIGNMENT,
208 OPTION_SUPPORT_OLD_CODE,
211 OPTION_EXCLUDE_SYMBOLS,
212 OPTION_EXCLUDE_ALL_SYMBOLS,
214 OPTION_STDCALL_ALIASES,
215 OPTION_ENABLE_STDCALL_FIXUP,
216 OPTION_DISABLE_STDCALL_FIXUP,
217 OPTION_IMPLIB_FILENAME,
218 OPTION_WARN_DUPLICATE_EXPORTS,
220 OPTION_ENABLE_AUTO_IMAGE_BASE,
221 OPTION_DISABLE_AUTO_IMAGE_BASE,
222 OPTION_DLL_SEARCH_PREFIX,
223 OPTION_NO_DEFAULT_EXCLUDES,
224 OPTION_DLL_ENABLE_AUTO_IMPORT,
225 OPTION_DLL_DISABLE_AUTO_IMPORT,
226 OPTION_ENABLE_EXTRA_PE_DEBUG,
228 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC,
229 OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC,
230 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2,
231 OPTION_EXCLUDE_MODULES_FOR_IMPLIB,
232 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES,
233 OPTION_NO_LEADING_UNDERSCORE,
234 OPTION_LEADING_UNDERSCORE,
235 OPTION_ENABLE_LONG_SECTION_NAMES,
236 OPTION_DISABLE_LONG_SECTION_NAMES,
238 OPTION_FORCE_INTEGRITY,
244 OPTION_INSERT_TIMESTAMP,
245 OPTION_TERMINAL_SERVER_AWARE
249 gld${EMULATION_NAME}_add_options
250 (int ns ATTRIBUTE_UNUSED,
251 char **shortopts ATTRIBUTE_UNUSED,
253 struct option **longopts,
254 int nrl ATTRIBUTE_UNUSED,
255 struct option **really_longopts ATTRIBUTE_UNUSED)
257 static const struct option xtra_long[] =
260 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
261 {"dll", no_argument, NULL, OPTION_DLL},
262 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
263 {"heap", required_argument, NULL, OPTION_HEAP},
264 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
265 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
266 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
267 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
268 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
269 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
270 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
271 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
272 {"stack", required_argument, NULL, OPTION_STACK},
273 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
274 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
275 {"use-nul-prefixed-import-tables", no_argument, NULL,
276 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
277 {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
278 {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE},
280 /* getopt allows abbreviations, so we do this to stop it
281 from treating -o as an abbreviation for this option. */
282 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
283 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
284 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
285 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
286 {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS},
287 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
288 {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
289 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
290 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
291 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
292 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
293 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
294 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
295 /* getopt() allows abbreviations, so we do this to stop it from
296 treating -c as an abbreviation for these --compat-implib. */
297 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
298 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
299 {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
300 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
301 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
302 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
303 {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
304 {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
305 {"enable-extra-pep-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
306 {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
307 {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
308 {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2},
310 {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
311 {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
312 {"dynamicbase",no_argument, NULL, OPTION_DYNAMIC_BASE},
313 {"forceinteg", no_argument, NULL, OPTION_FORCE_INTEGRITY},
314 {"nxcompat", no_argument, NULL, OPTION_NX_COMPAT},
315 {"no-isolation", no_argument, NULL, OPTION_NO_ISOLATION},
316 {"no-seh", no_argument, NULL, OPTION_NO_SEH},
317 {"no-bind", no_argument, NULL, OPTION_NO_BIND},
318 {"wdmdriver", no_argument, NULL, OPTION_WDM_DRIVER},
319 {"tsaware", no_argument, NULL, OPTION_TERMINAL_SERVER_AWARE},
320 {"insert-timestamp", no_argument, NULL, OPTION_INSERT_TIMESTAMP},
321 {NULL, no_argument, NULL, 0}
325 = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
326 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
329 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
330 parameters which may be input from the command line. */
339 /* FALSE for an assembly level symbol and TRUE for a C visible symbol.
340 C visible symbols can be prefixed by underscore dependent on target's
342 bfd_boolean is_c_symbol;
345 #define GET_INIT_SYMBOL_NAME(IDX) \
346 (init[(IDX)].symbol \
347 + ((init[(IDX)].is_c_symbol == FALSE || (is_underscoring () == 1)) ? 0 : 1))
349 /* Decorates the C visible symbol by underscore, if target requires. */
351 ((is_underscoring () == 0) ? CSTR : "_" CSTR)
353 /* Get size of constant string for a possible underscore prefixed
355 #define U_SIZE(CSTR) \
356 (sizeof (CSTR) + (is_underscoring () == 0 ? 0 : 1))
358 #define D(field,symbol,def,usc) {&pep.field,sizeof(pep.field), def, symbol,0, usc}
360 static definfo init[] =
362 /* imagebase must be first */
363 #define IMAGEBASEOFF 0
364 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE, FALSE),
366 {&dll, sizeof(dll), 0, "__dll__", 0, FALSE},
367 #define MSIMAGEBASEOFF 2
368 D(ImageBase, "___ImageBase", NT_EXE_IMAGE_BASE, TRUE),
369 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT, FALSE),
370 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT, FALSE),
371 D(MajorOperatingSystemVersion,"__major_os_version__", 4, FALSE),
372 D(MinorOperatingSystemVersion,"__minor_os_version__", 0, FALSE),
373 D(MajorImageVersion,"__major_image_version__", 0, FALSE),
374 D(MinorImageVersion,"__minor_image_version__", 0, FALSE),
375 D(MajorSubsystemVersion,"__major_subsystem_version__", 5, FALSE),
376 D(MinorSubsystemVersion,"__minor_subsystem_version__", 2, FALSE),
377 D(Subsystem,"__subsystem__", ${SUBSYSTEM}, FALSE),
378 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000, FALSE),
379 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000, FALSE),
380 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000, FALSE),
381 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000, FALSE),
382 D(LoaderFlags,"__loader_flags__", 0x0, FALSE),
383 D(DllCharacteristics, "__dll_characteristics__", 0x0, FALSE),
384 { NULL, 0, 0, NULL, 0, FALSE}
389 gld_${EMULATION_NAME}_list_options (FILE *file)
391 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
392 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
393 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
394 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
395 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
396 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
397 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
398 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
399 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
400 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
401 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
402 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
403 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
404 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
405 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
406 fprintf (file, _(" --[no-]leading-underscore Set explicit symbol underscore prefix mode\n"));
407 fprintf (file, _(" --insert-timestamp Use a real timestamp rather than zero.\n"));
408 fprintf (file, _(" This makes binaries non-deterministic\n"));
410 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
411 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
412 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
413 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
414 fprintf (file, _(" --exclude-all-symbols Exclude all symbols from automatic export\n"));
415 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
416 fprintf (file, _(" --exclude-modules-for-implib mod,mod,...\n"));
417 fprintf (file, _(" Exclude objects, archive members from auto\n"));
418 fprintf (file, _(" export, place into import library instead.\n"));
419 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
420 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
421 fprintf (file, _(" --out-implib <file> Generate import library\n"));
422 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
423 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
424 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
425 create __imp_<SYMBOL> as well.\n"));
426 fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n\
427 unless user specifies one\n"));
428 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
429 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
430 an importlib, use <string><basename>.dll\n\
431 in preference to lib<basename>.dll \n"));
432 fprintf (file, _(" --enable-auto-import Do sophisticated linking of _sym to\n\
433 __imp_sym for DATA references\n"));
434 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
435 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
436 adding pseudo-relocations resolved at\n\
438 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
439 auto-imported DATA.\n"));
440 fprintf (file, _(" --enable-extra-pep-debug Enable verbose debug output when building\n\
441 or linking to DLLs (esp. auto-import)\n"));
442 fprintf (file, _(" --enable-long-section-names Use long COFF section names even in\n\
443 executable image files\n"));
444 fprintf (file, _(" --disable-long-section-names Never use long COFF section names, even\n\
445 in object files\n"));
446 fprintf (file, _(" --dynamicbase Image base address may be relocated using\n\
447 address space layout randomization (ASLR)\n"));
448 fprintf (file, _(" --forceinteg Code integrity checks are enforced\n"));
449 fprintf (file, _(" --nxcompat Image is compatible with data execution prevention\n"));
450 fprintf (file, _(" --no-isolation Image understands isolation but do not isolate the image\n"));
451 fprintf (file, _(" --no-seh Image does not use SEH. No SE handler may\n\
452 be called in this image\n"));
453 fprintf (file, _(" --no-bind Do not bind this image\n"));
454 fprintf (file, _(" --wdmdriver Driver uses the WDM model\n"));
455 fprintf (file, _(" --tsaware Image is Terminal Server aware\n"));
461 set_pep_name (char *name, bfd_vma val)
465 /* Find the name and set it. */
466 for (i = 0; init[i].ptr; i++)
468 if (strcmp (name, GET_INIT_SYMBOL_NAME (i)) == 0)
472 if (strcmp (name,"__image_base__") == 0)
473 set_pep_name (U ("__ImageBase"), val);
481 set_entry_point (void)
484 const char *initial_symbol_char;
494 { 1, "NtProcessStartup" },
495 { 2, "WinMainCRTStartup" },
496 { 3, "mainCRTStartup" },
497 { 7, "__PosixProcessStartup" },
498 { 9, "WinMainCRTStartup" },
499 {14, "mainCRTStartup" },
503 /* Entry point name for arbitrary subsystem numbers. */
504 static const char default_entry[] = "mainCRTStartup";
506 if (link_info.shared || dll)
508 entry = "DllMainCRTStartup";
512 for (i = 0; v[i].entry; i++)
513 if (v[i].value == pep_subsystem)
516 /* If no match, use the default. */
517 if (v[i].entry != NULL)
520 entry = default_entry;
523 /* Now we check target's default for getting proper symbol_char. */
524 initial_symbol_char = (is_underscoring () != 0 ? "_" : "");
526 if (*initial_symbol_char != '\0')
530 /* lang_default_entry expects its argument to be permanently
531 allocated, so we don't free this string. */
532 alc_entry = xmalloc (strlen (initial_symbol_char)
535 strcpy (alc_entry, initial_symbol_char);
536 strcat (alc_entry, entry);
540 lang_default_entry (entry);
544 set_pep_subsystem (void)
550 unsigned long temp_subsystem;
567 /* Check for the presence of a version number. */
568 sver = strchr (optarg, ':');
570 len = strlen (optarg);
574 set_pep_name ("__major_subsystem_version__",
575 strtoul (sver + 1, &end, 0));
577 set_pep_name ("__minor_subsystem_version__",
578 strtoul (end + 1, &end, 0));
580 einfo (_("%P: warning: bad version number in -subsystem option\n"));
583 /* Check for numeric subsystem. */
584 temp_subsystem = strtoul (optarg, & end, 0);
585 if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
587 /* Search list for a numeric match to use its entry point. */
588 for (i = 0; v[i].name; i++)
589 if (v[i].value == (int) temp_subsystem)
592 /* Use this subsystem. */
593 pep_subsystem = (int) temp_subsystem;
597 /* Search for subsystem by name. */
598 for (i = 0; v[i].name; i++)
599 if (strncmp (optarg, v[i].name, len) == 0
600 && v[i].name[len] == '\0')
603 if (v[i].name == NULL)
605 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
609 pep_subsystem = v[i].value;
612 set_pep_name ("__subsystem__", pep_subsystem);
619 set_pep_value (char *name)
623 set_pep_name (name, (bfd_vma) strtoull (optarg, &end, 0));
626 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
633 set_pep_stack_heap (char *resname, char *comname)
635 set_pep_value (resname);
640 set_pep_value (comname);
643 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
648 gld${EMULATION_NAME}_handle_option (int optc)
656 case OPTION_BASE_FILE:
657 link_info.base_file = fopen (optarg, FOPEN_WB);
658 if (link_info.base_file == NULL)
659 einfo (_("%F%P: cannot open base file %s\n"), optarg);
664 set_pep_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
667 set_pep_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
669 case OPTION_SUBSYSTEM:
670 set_pep_subsystem ();
672 case OPTION_MAJOR_OS_VERSION:
673 set_pep_value ("__major_os_version__");
675 case OPTION_MINOR_OS_VERSION:
676 set_pep_value ("__minor_os_version__");
678 case OPTION_MAJOR_SUBSYSTEM_VERSION:
679 set_pep_value ("__major_subsystem_version__");
681 case OPTION_MINOR_SUBSYSTEM_VERSION:
682 set_pep_value ("__minor_subsystem_version__");
684 case OPTION_MAJOR_IMAGE_VERSION:
685 set_pep_value ("__major_image_version__");
687 case OPTION_MINOR_IMAGE_VERSION:
688 set_pep_value ("__minor_image_version__");
690 case OPTION_FILE_ALIGNMENT:
691 set_pep_value ("__file_alignment__");
693 case OPTION_SECTION_ALIGNMENT:
694 set_pep_value ("__section_alignment__");
697 set_pep_name ("__dll__", 1);
699 case OPTION_IMAGE_BASE:
700 set_pep_value ("__image_base__");
702 case OPTION_SUPPORT_OLD_CODE:
703 support_old_code = 1;
705 case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
706 pep_use_nul_prefixed_import_tables = TRUE;
708 case OPTION_NO_LEADING_UNDERSCORE:
709 pep_leading_underscore = 0;
711 case OPTION_LEADING_UNDERSCORE:
712 pep_leading_underscore = 1;
714 case OPTION_INSERT_TIMESTAMP:
715 insert_timestamp = TRUE;
719 pep_out_def_filename = xstrdup (optarg);
721 case OPTION_EXPORT_ALL:
722 pep_dll_export_everything = 1;
724 case OPTION_EXCLUDE_SYMBOLS:
725 pep_dll_add_excludes (optarg, EXCLUDESYMS);
727 case OPTION_EXCLUDE_ALL_SYMBOLS:
728 pep_dll_exclude_all_symbols = 1;
730 case OPTION_EXCLUDE_LIBS:
731 pep_dll_add_excludes (optarg, EXCLUDELIBS);
733 case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
734 pep_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
736 case OPTION_KILL_ATS:
737 pep_dll_kill_ats = 1;
739 case OPTION_STDCALL_ALIASES:
740 pep_dll_stdcall_aliases = 1;
742 case OPTION_ENABLE_STDCALL_FIXUP:
743 pep_enable_stdcall_fixup = 1;
745 case OPTION_DISABLE_STDCALL_FIXUP:
746 pep_enable_stdcall_fixup = 0;
748 case OPTION_IMPLIB_FILENAME:
749 pep_implib_filename = xstrdup (optarg);
751 case OPTION_WARN_DUPLICATE_EXPORTS:
752 pep_dll_warn_dup_exports = 1;
754 case OPTION_IMP_COMPAT:
755 pep_dll_compat_implib = 1;
757 case OPTION_ENABLE_AUTO_IMAGE_BASE:
758 pep_enable_auto_image_base = 1;
760 case OPTION_DISABLE_AUTO_IMAGE_BASE:
761 pep_enable_auto_image_base = 0;
763 case OPTION_DLL_SEARCH_PREFIX:
764 pep_dll_search_prefix = xstrdup (optarg);
766 case OPTION_NO_DEFAULT_EXCLUDES:
767 pep_dll_do_default_excludes = 0;
769 case OPTION_DLL_ENABLE_AUTO_IMPORT:
770 link_info.pei386_auto_import = 1;
772 case OPTION_DLL_DISABLE_AUTO_IMPORT:
773 link_info.pei386_auto_import = 0;
775 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
776 link_info.pei386_runtime_pseudo_reloc = 2;
778 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
779 link_info.pei386_runtime_pseudo_reloc = 0;
781 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2:
782 link_info.pei386_runtime_pseudo_reloc = 2;
784 case OPTION_ENABLE_EXTRA_PE_DEBUG:
785 pep_dll_extra_pe_debug = 1;
788 case OPTION_ENABLE_LONG_SECTION_NAMES:
789 pep_use_coff_long_section_names = 1;
791 case OPTION_DISABLE_LONG_SECTION_NAMES:
792 pep_use_coff_long_section_names = 0;
794 /* Get DLLCharacteristics bits */
795 case OPTION_DYNAMIC_BASE:
796 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
798 case OPTION_FORCE_INTEGRITY:
799 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
801 case OPTION_NX_COMPAT:
802 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
804 case OPTION_NO_ISOLATION:
805 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
808 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_SEH;
811 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_BIND;
813 case OPTION_WDM_DRIVER:
814 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
816 case OPTION_TERMINAL_SERVER_AWARE:
817 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
821 /* Set DLLCharacteristics bits */
822 set_pep_name ("__dll_characteristics__", pe_dll_characteristics);
830 strhash (const char *str)
832 const unsigned char *s;
839 s = (const unsigned char *) str;
840 while ((c = *s++) != '\0')
842 hash += c + (c << 17);
846 hash += len + (len << 17);
852 /* Use the output file to create a image base for relocatable DLLs. */
855 compute_dll_image_base (const char *ofile)
857 bfd_vma hash = (bfd_vma) strhash (ofile);
858 return NT_DLL_AUTO_IMAGE_BASE + ((hash << 16) & NT_DLL_AUTO_IMAGE_MASK);
862 /* Assign values to the special symbols before the linker script is
866 gld_${EMULATION_NAME}_set_symbols (void)
868 /* Run through and invent symbols for all the
869 names and insert the defaults. */
874 if (!init[IMAGEBASEOFF].inited)
876 if (link_info.relocatable)
877 init[IMAGEBASEOFF].value = 0;
878 else if (init[DLLOFF].value || (link_info.shared && !link_info.pie))
881 init[IMAGEBASEOFF].value = (pep_enable_auto_image_base
882 ? compute_dll_image_base (output_filename)
883 : NT_DLL_IMAGE_BASE);
885 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
889 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
890 init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
893 /* Don't do any symbol assignments if this is a relocatable link. */
894 if (link_info.relocatable)
897 /* Glue the assignments into the abs section. */
898 push_stat_ptr (&abs_output_section->children);
900 for (j = 0; init[j].ptr; j++)
902 bfd_vma val = init[j].value;
903 lang_assignment_statement_type *rv;
905 rv = lang_add_assignment (exp_assign (GET_INIT_SYMBOL_NAME (j),
906 exp_intop (val), FALSE));
907 if (init[j].size == sizeof (short))
908 *(short *) init[j].ptr = (short) val;
909 else if (init[j].size == sizeof (int))
910 *(int *) init[j].ptr = (int) val;
911 else if (init[j].size == sizeof (long))
912 *(long *) init[j].ptr = (long) val;
913 /* This might be a long long or other special type. */
914 else if (init[j].size == sizeof (bfd_vma))
915 *(bfd_vma *) init[j].ptr = val;
917 if (j == IMAGEBASEOFF)
918 image_base_statement = rv;
920 /* Restore the pointer. */
923 if (pep.FileAlignment > pep.SectionAlignment)
925 einfo (_("%P: warning, file alignment > section alignment.\n"));
929 /* This is called after the linker script and the command line options
933 gld_${EMULATION_NAME}_after_parse (void)
935 /* PR ld/6744: Warn the user if they have used an ELF-only
936 option hoping it will work on PE+. */
937 if (link_info.export_dynamic)
938 einfo (_("%P: warning: --export-dynamic is not supported for PE+ "
939 "targets, did you mean --export-all-symbols?\n"));
943 after_parse_default ();
946 /* pep-dll.c directly accesses pep_data_import_dll,
947 so it must be defined outside of #ifdef DLL_SUPPORT.
948 Note - this variable is deliberately not initialised.
949 This allows it to be treated as a common varaible, and only
950 exist in one incarnation in a multiple target enabled linker. */
951 char * pep_data_import_dll;
954 static struct bfd_link_hash_entry *pep_undef_found_sym;
957 pep_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
961 const char *hs = h->root.string;
963 sl = strlen (string);
964 if (h->type == bfd_link_hash_defined
965 && ((*hs == '@' && *string == '_'
966 && strncmp (hs + 1, string + 1, sl - 1) == 0)
967 || strncmp (hs, string, sl) == 0)
968 && h->root.string[sl] == '@')
970 pep_undef_found_sym = h;
977 pep_fixup_stdcalls (void)
979 static int gave_warning_message = 0;
980 struct bfd_link_hash_entry *undef, *sym;
982 if (pep_dll_extra_pe_debug)
983 printf ("%s\n", __FUNCTION__);
985 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
986 if (undef->type == bfd_link_hash_undefined)
988 char* at = strchr (undef->root.string, '@');
989 int lead_at = (*undef->root.string == '@');
991 at = strchr (undef->root.string + 1, '@');
994 /* The symbol is a stdcall symbol, so let's look for a
995 cdecl symbol with the same name and resolve to that. */
996 char *cname = xstrdup (undef->root.string);
1000 at = strchr (cname, '@');
1003 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
1005 if (sym && sym->type == bfd_link_hash_defined)
1007 undef->type = bfd_link_hash_defined;
1008 undef->u.def.value = sym->u.def.value;
1009 undef->u.def.section = sym->u.def.section;
1011 if (pep_enable_stdcall_fixup == -1)
1013 einfo (_("Warning: resolving %s by linking to %s\n"),
1014 undef->root.string, cname);
1015 if (! gave_warning_message)
1017 gave_warning_message = 1;
1018 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
1019 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
1026 /* The symbol is a cdecl symbol, so we look for stdcall
1027 symbols - which means scanning the whole symbol table. */
1028 pep_undef_found_sym = 0;
1029 bfd_link_hash_traverse (link_info.hash, pep_undef_cdecl_match,
1030 (char *) undef->root.string);
1031 sym = pep_undef_found_sym;
1034 undef->type = bfd_link_hash_defined;
1035 undef->u.def.value = sym->u.def.value;
1036 undef->u.def.section = sym->u.def.section;
1038 if (pep_enable_stdcall_fixup == -1)
1040 einfo (_("Warning: resolving %s by linking to %s\n"),
1041 undef->root.string, sym->root.string);
1042 if (! gave_warning_message)
1044 gave_warning_message = 1;
1045 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
1046 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
1055 make_import_fixup (arelent *rel, asection *s)
1057 struct bfd_symbol *sym = *rel->sym_ptr_ptr;
1059 bfd_vma _addend = 0;
1062 if (pep_dll_extra_pe_debug)
1063 printf ("arelent: %s@%#lx: add=%li\n", sym->name,
1064 (unsigned long) rel->address, (long) rel->addend);
1066 memset (addend, 0, sizeof (addend));
1067 switch ((rel->howto->bitsize))
1070 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 1);
1071 if (suc && rel->howto->pc_relative)
1072 _addend = (bfd_vma) ((bfd_signed_vma) ((char) bfd_get_8 (s->owner, addend)));
1074 _addend = ((bfd_vma) bfd_get_8 (s->owner, addend)) & 0xff;
1077 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 2);
1078 if (suc && rel->howto->pc_relative)
1079 _addend = (bfd_vma) ((bfd_signed_vma) ((short) bfd_get_16 (s->owner, addend)));
1081 _addend = ((bfd_vma) bfd_get_16 (s->owner, addend)) & 0xffff;
1084 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 4);
1085 if (suc && rel->howto->pc_relative)
1086 _addend = (bfd_vma) ((bfd_signed_vma) ((int) bfd_get_32 (s->owner, addend)));
1088 _addend = ((bfd_vma) bfd_get_32 (s->owner, addend)) & 0xffffffff;
1091 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 8);
1093 _addend = ((bfd_vma) bfd_get_64 (s->owner, addend));
1097 einfo (_("%C: Cannot get section contents - auto-import exception\n"),
1098 s->owner, s, rel->address);
1100 if (pep_dll_extra_pe_debug)
1102 printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx", (long) _addend, (long) rel->addend, (long) rel->address);
1103 if (rel->howto->pc_relative) printf (" pcrel");
1104 printf (" %d bit rel.\n",(int) rel->howto->bitsize);
1106 pep_create_import_fixup (rel, s, _addend);
1112 pep_find_data_imports (void)
1114 struct bfd_link_hash_entry *undef, *sym;
1116 if (link_info.pei386_auto_import == 0)
1119 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
1121 if (undef->type == bfd_link_hash_undefined)
1123 /* C++ symbols are *long*. */
1126 if (pep_dll_extra_pe_debug)
1127 printf ("%s:%s\n", __FUNCTION__, undef->root.string);
1129 sprintf (buf, "__imp_%s", undef->root.string);
1131 sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
1133 if (sym && sym->type == bfd_link_hash_defined)
1135 bfd *b = sym->u.def.section->owner;
1139 if (!bfd_generic_link_read_symbols (b))
1141 einfo (_("%B%F: could not read symbols: %E\n"), b);
1145 symbols = bfd_get_outsymbols (b);
1146 nsyms = bfd_get_symcount (b);
1148 for (i = 0; i < nsyms; i++)
1150 if (! CONST_STRNEQ (symbols[i]->name, U ("_head_")))
1153 if (pep_dll_extra_pe_debug)
1154 printf ("->%s\n", symbols[i]->name);
1156 pep_data_import_dll = (char*) (symbols[i]->name +
1157 U_SIZE ("_head_") - 1);
1161 pep_walk_relocs_of_symbol (&link_info, undef->root.string,
1164 /* Let's differentiate it somehow from defined. */
1165 undef->type = bfd_link_hash_defweak;
1166 /* We replace original name with __imp_ prefixed, this
1167 1) may trash memory 2) leads to duplicate symbol generation.
1168 Still, IMHO it's better than having name poluted. */
1169 undef->root.string = sym->root.string;
1170 undef->u.def.value = sym->u.def.value;
1171 undef->u.def.section = sym->u.def.section;
1178 pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
1180 printf ("+%s\n", h->string);
1184 #endif /* DLL_SUPPORT */
1187 debug_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
1189 int *found = (int *) obj;
1190 if (strncmp (".debug_", sect->name, sizeof (".debug_") - 1) == 0)
1195 gld_${EMULATION_NAME}_after_open (void)
1197 after_open_default ();
1201 if (pep_dll_extra_pe_debug)
1204 struct bfd_link_hash_entry *sym;
1206 printf ("%s()\n", __FUNCTION__);
1208 for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1209 printf ("-%s\n", sym->root.string);
1210 bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
1212 for (a = link_info.input_bfds; a; a = a->link_next)
1213 printf ("*%s\n",a->filename);
1217 /* Pass the wacky PE command line options into the output bfd.
1218 FIXME: This should be done via a function, rather than by
1219 including an internal BFD header. */
1221 if (coff_data (link_info.output_bfd) == NULL
1222 || coff_data (link_info.output_bfd)->pe == 0)
1223 einfo (_("%F%P: cannot perform PE operations on non PE output file '%B'.\n"),
1224 link_info.output_bfd);
1226 pe_data (link_info.output_bfd)->pe_opthdr = pep;
1227 pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
1228 pe_data (link_info.output_bfd)->real_flags |= real_flags;
1229 pe_data (link_info.output_bfd)->insert_timestamp = insert_timestamp;
1231 /* At this point we must decide whether to use long section names
1232 in the output or not. If the user hasn't explicitly specified
1233 on the command line, we leave it to the default for the format
1234 (object files yes, image files no), except if there is debug
1235 information present; GDB relies on the long section names to
1236 find it, so enable it in that case. */
1237 if (pep_use_coff_long_section_names < 0 && link_info.strip == strip_none)
1239 /* Iterate over all sections of all input BFDs, checking
1240 for any that begin 'debug_' and are long names. */
1241 LANG_FOR_EACH_INPUT_STATEMENT (is)
1243 int found_debug = 0;
1244 bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug);
1247 pep_use_coff_long_section_names = 1;
1253 pep_output_file_set_long_section_names (link_info.output_bfd);
1256 if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
1257 pep_fixup_stdcalls ();
1259 pep_process_import_defs (link_info.output_bfd, &link_info);
1261 pep_find_data_imports ();
1263 /* As possibly new symbols are added by imports, we rerun
1264 stdcall/fastcall fixup here. */
1265 if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
1266 pep_fixup_stdcalls ();
1268 #ifndef TARGET_IS_i386pep
1269 if (link_info.shared)
1271 if (!link_info.relocatable)
1273 pep_dll_build_sections (link_info.output_bfd, &link_info);
1275 #ifndef TARGET_IS_i386pep
1277 pep_exe_build_sections (link_info.output_bfd, &link_info);
1279 #endif /* DLL_SUPPORT */
1282 /* This next chunk of code tries to detect the case where you have
1283 two import libraries for the same DLL (specifically,
1284 symbolically linking libm.a and libc.a in cygwin to
1285 libcygwin.a). In those cases, it's possible for function
1286 thunks from the second implib to be used but without the
1287 head/tail objects, causing an improper import table. We detect
1288 those cases and rename the "other" import libraries to match
1289 the one the head/tail come from, so that the linker will sort
1290 things nicely and produce a valid import table. */
1292 LANG_FOR_EACH_INPUT_STATEMENT (is)
1294 if (is->the_bfd->my_archive)
1296 int idata2 = 0, reloc_count=0, is_imp = 0;
1299 /* See if this is an import library thunk. */
1300 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1302 if (strcmp (sec->name, ".idata\$2") == 0)
1304 if (CONST_STRNEQ (sec->name, ".idata\$"))
1306 reloc_count += sec->reloc_count;
1309 if (is_imp && !idata2 && reloc_count)
1311 /* It is, look for the reference to head and see if it's
1312 from our own library. */
1313 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1321 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1325 if (!bfd_generic_link_read_symbols (is->the_bfd))
1327 einfo (_("%B%F: could not read symbols: %E\n"),
1331 symbols = bfd_get_outsymbols (is->the_bfd);
1333 relocs = xmalloc ((size_t) relsize);
1334 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1339 einfo ("%X%P: unable to process relocs: %E\n");
1343 for (i = 0; i < nrelocs; i++)
1345 struct bfd_symbol *s;
1346 struct bfd_link_hash_entry * blhe;
1347 char *other_bfd_filename;
1350 s = (relocs[i]->sym_ptr_ptr)[0];
1352 if (s->flags & BSF_LOCAL)
1355 /* Thunk section with reloc to another bfd. */
1356 blhe = bfd_link_hash_lookup (link_info.hash,
1358 FALSE, FALSE, TRUE);
1361 || blhe->type != bfd_link_hash_defined)
1365 = blhe->u.def.section->owner->my_archive
1366 ? bfd_get_filename (blhe->u.def.section->owner->my_archive)
1367 : bfd_get_filename (blhe->u.def.section->owner);
1369 if (filename_cmp (bfd_get_filename
1370 (is->the_bfd->my_archive),
1371 other_bfd_filename) == 0)
1374 /* Rename this implib to match the other one. */
1375 n = xmalloc (strlen (other_bfd_filename) + 1);
1376 strcpy (n, other_bfd_filename);
1377 is->the_bfd->my_archive->filename = n;
1381 /* Note - we do not free the symbols,
1382 they are now cached in the BFD. */
1392 lang_input_statement_type *is2;
1393 lang_input_statement_type *is3;
1395 /* Careful - this is a shell script. Watch those dollar signs! */
1396 /* Microsoft import libraries have every member named the same,
1397 and not in the right order for us to link them correctly. We
1398 must detect these and rename the members so that they'll link
1399 correctly. There are three types of objects: the head, the
1400 thunks, and the sentinel(s). The head is easy; it's the one
1401 with idata2. We assume that the sentinels won't have relocs,
1402 and the thunks will. It's easier than checking the symbol
1403 table for external references. */
1404 LANG_FOR_EACH_INPUT_STATEMENT (is)
1406 if (is->the_bfd->my_archive)
1409 bfd *arch = is->the_bfd->my_archive;
1411 if (cur_arch != arch)
1417 is3 && is3->the_bfd->my_archive == arch;
1418 is3 = (lang_input_statement_type *) is3->next)
1420 /* A MS dynamic import library can also contain static
1421 members, so look for the first element with a .dll
1422 extension, and use that for the remainder of the
1424 pnt = strrchr (is3->the_bfd->filename, '.');
1425 if (pnt != NULL && filename_cmp (pnt, ".dll") == 0)
1433 /* OK, found one. Now look to see if the remaining
1434 (dynamic import) members use the same name. */
1436 is2 && is2->the_bfd->my_archive == arch;
1437 is2 = (lang_input_statement_type *) is2->next)
1439 /* Skip static members, ie anything with a .obj
1441 pnt = strrchr (is2->the_bfd->filename, '.');
1442 if (pnt != NULL && filename_cmp (pnt, ".obj") == 0)
1445 if (filename_cmp (is3->the_bfd->filename,
1446 is2->the_bfd->filename))
1455 /* This fragment might have come from an .obj file in a Microsoft
1456 import, and not an actual import record. If this is the case,
1457 then leave the filename alone. */
1458 pnt = strrchr (is->the_bfd->filename, '.');
1460 if (is_ms_arch && (filename_cmp (pnt, ".dll") == 0))
1462 int idata2 = 0, reloc_count=0;
1464 char *new_name, seq;
1466 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1468 if (strcmp (sec->name, ".idata\$2") == 0)
1470 reloc_count += sec->reloc_count;
1473 if (idata2) /* .idata2 is the TOC */
1475 else if (reloc_count > 0) /* thunks */
1480 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
1481 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
1482 is->the_bfd->filename = new_name;
1484 new_name = xmalloc (strlen (is->filename) + 3);
1485 sprintf (new_name, "%s.%c", is->filename, seq);
1486 is->filename = new_name;
1494 gld_${EMULATION_NAME}_before_allocation (void)
1497 before_allocation_default ();
1501 /* This is called when an input file isn't recognized as a BFD. We
1502 check here for .DEF files and pull them in automatically. */
1505 saw_option (char *option)
1511 for (i = 0; init[i].ptr; i++)
1512 if (strcmp (GET_INIT_SYMBOL_NAME (i), option) == 0)
1513 return init[i].inited;
1516 #endif /* DLL_SUPPORT */
1519 gld_${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1522 const char *ext = entry->filename + strlen (entry->filename) - 4;
1524 if (filename_cmp (ext, ".def") == 0 || filename_cmp (ext, ".DEF") == 0)
1526 pep_def_file = def_file_parse (entry->filename, pep_def_file);
1530 int i, buflen=0, len;
1533 for (i = 0; i < pep_def_file->num_exports; i++)
1535 len = strlen (pep_def_file->exports[i].internal_name);
1536 if (buflen < len + 2)
1540 buf = xmalloc (buflen);
1542 for (i = 0; i < pep_def_file->num_exports; i++)
1544 struct bfd_link_hash_entry *h;
1546 sprintf (buf, "%s%s", U (""),
1547 pep_def_file->exports[i].internal_name);
1549 h = bfd_link_hash_lookup (link_info.hash, buf, TRUE, TRUE, TRUE);
1550 if (h == (struct bfd_link_hash_entry *) NULL)
1551 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1552 if (h->type == bfd_link_hash_new)
1554 h->type = bfd_link_hash_undefined;
1555 h->u.undef.abfd = NULL;
1556 bfd_link_add_undef (link_info.hash, h);
1561 /* def_file_print (stdout, pep_def_file); */
1562 if (pep_def_file->is_dll == 1)
1563 link_info.shared = 1;
1565 if (pep_def_file->base_address != (bfd_vma)(-1))
1568 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1569 = init[IMAGEBASEOFF].value
1570 = pep_def_file->base_address;
1571 init[IMAGEBASEOFF].inited = 1;
1572 if (image_base_statement)
1573 image_base_statement->exp
1574 = exp_assign ("__image_base__", exp_intop (pep.ImageBase),
1578 if (pep_def_file->stack_reserve != -1
1579 && ! saw_option ("__size_of_stack_reserve__"))
1581 pep.SizeOfStackReserve = pep_def_file->stack_reserve;
1582 if (pep_def_file->stack_commit != -1)
1583 pep.SizeOfStackCommit = pep_def_file->stack_commit;
1585 if (pep_def_file->heap_reserve != -1
1586 && ! saw_option ("__size_of_heap_reserve__"))
1588 pep.SizeOfHeapReserve = pep_def_file->heap_reserve;
1589 if (pep_def_file->heap_commit != -1)
1590 pep.SizeOfHeapCommit = pep_def_file->heap_commit;
1600 gld_${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1604 #ifdef TARGET_IS_i386pep
1605 pep_dll_id_target ("pei-x86-64");
1607 if (pep_bfd_is_dll (entry->the_bfd))
1608 return pep_implied_import_dll (entry->filename);
1614 gld_${EMULATION_NAME}_finish (void)
1620 if (link_info.shared
1621 || (!link_info.relocatable && pep_def_file->num_exports != 0))
1623 pep_dll_fill_sections (link_info.output_bfd, &link_info);
1624 if (pep_implib_filename)
1625 pep_dll_generate_implib (pep_def_file, pep_implib_filename, &link_info);
1628 if (pep_out_def_filename)
1629 pep_dll_generate_def_file (pep_out_def_filename);
1630 #endif /* DLL_SUPPORT */
1632 /* I don't know where .idata gets set as code, but it shouldn't be. */
1634 asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
1638 asec->flags &= ~SEC_CODE;
1639 asec->flags |= SEC_DATA;
1645 /* Place an orphan section.
1647 We use this to put sections in a reasonable place in the file, and
1648 to ensure that they are aligned as required.
1650 We handle grouped sections here as well. A section named .foo\$nn
1651 goes into the output section .foo. All grouped sections are sorted
1654 Grouped sections for the default sections are handled by the
1655 default linker script using wildcards, and are sorted by
1658 static lang_output_section_statement_type *
1659 gld_${EMULATION_NAME}_place_orphan (asection *s,
1660 const char *secname,
1663 const char *orig_secname = secname;
1664 char *dollar = NULL;
1665 lang_output_section_statement_type *os;
1666 lang_statement_list_type add_child;
1667 lang_output_section_statement_type *match_by_name = NULL;
1668 lang_statement_union_type **pl;
1670 /* Look through the script to see where to place this section. */
1671 if (!link_info.relocatable
1672 && (dollar = strchr (secname, '\$')) != NULL)
1674 size_t len = dollar - secname;
1675 char *newname = xmalloc (len + 1);
1676 memcpy (newname, secname, len);
1677 newname[len] = '\0';
1681 lang_list_init (&add_child);
1684 if (constraint == 0)
1685 for (os = lang_output_section_find (secname);
1687 os = next_matching_output_section_statement (os, 0))
1689 /* If we don't match an existing output section, tell
1690 lang_insert_orphan to create a new output section. */
1691 constraint = SPECIAL;
1693 if (os->bfd_section != NULL
1694 && (os->bfd_section->flags == 0
1695 || ((s->flags ^ os->bfd_section->flags)
1696 & (SEC_LOAD | SEC_ALLOC)) == 0))
1698 /* We already have an output section statement with this
1699 name, and its bfd section has compatible flags.
1700 If the section already exists but does not have any flags set,
1701 then it has been created by the linker, probably as a result of
1702 a --section-start command line switch. */
1703 lang_add_section (&add_child, s, NULL, os);
1707 /* Save unused output sections in case we can match them
1708 against orphans later. */
1709 if (os->bfd_section == NULL)
1713 /* If we didn't match an active output section, see if we matched an
1714 unused one and use that. */
1715 if (os == NULL && match_by_name)
1717 lang_add_section (&match_by_name->children, s, NULL, match_by_name);
1718 return match_by_name;
1723 static struct orphan_save hold[] =
1726 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1729 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1732 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1735 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1741 enum orphan_save_index
1749 static int orphan_init_done = 0;
1750 struct orphan_save *place;
1751 lang_output_section_statement_type *after;
1752 etree_type *address;
1754 if (!orphan_init_done)
1756 struct orphan_save *ho;
1757 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1758 if (ho->name != NULL)
1760 ho->os = lang_output_section_find (ho->name);
1761 if (ho->os != NULL && ho->os->flags == 0)
1762 ho->os->flags = ho->flags;
1764 orphan_init_done = 1;
1767 /* Try to put the new output section in a reasonable place based
1768 on the section name and section flags. */
1771 if ((s->flags & SEC_ALLOC) == 0)
1773 else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
1774 place = &hold[orphan_bss];
1775 else if ((s->flags & SEC_READONLY) == 0)
1776 place = &hold[orphan_data];
1777 else if ((s->flags & SEC_CODE) == 0)
1779 place = (!strncmp (secname, ".idata\$", 7) ? &hold[orphan_idata]
1780 : &hold[orphan_rodata]);
1783 place = &hold[orphan_text];
1788 if (place->os == NULL)
1789 place->os = lang_output_section_find (place->name);
1792 after = lang_output_section_find_by_flags (s, &place->os, NULL);
1794 /* *ABS* is always the first output section statement. */
1795 after = (&lang_output_section_statement.head
1796 ->output_section_statement);
1799 /* All sections in an executable must be aligned to a page boundary.
1800 In a relocatable link, just preserve the incoming alignment; the
1801 address is discarded by lang_insert_orphan in that case, anyway. */
1802 address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
1803 os = lang_insert_orphan (s, secname, constraint, after, place, address,
1805 if (link_info.relocatable)
1807 os->section_alignment = s->alignment_power;
1808 os->bfd_section->alignment_power = s->alignment_power;
1812 /* If the section name has a '\$', sort it with the other '\$'
1814 for (pl = &os->children.head; *pl != NULL; pl = &(*pl)->header.next)
1816 lang_input_section_type *ls;
1819 if ((*pl)->header.type != lang_input_section_enum)
1822 ls = &(*pl)->input_section;
1824 lname = bfd_get_section_name (ls->section->owner, ls->section);
1825 if (strchr (lname, '\$') != NULL
1826 && (dollar == NULL || strcmp (orig_secname, lname) < 0))
1830 if (add_child.head != NULL)
1832 *add_child.tail = *pl;
1833 *pl = add_child.head;
1840 gld_${EMULATION_NAME}_open_dynamic_archive
1841 (const char *arch ATTRIBUTE_UNUSED,
1842 search_dirs_type *search,
1843 lang_input_statement_type *entry)
1847 const char * format;
1848 bfd_boolean use_prefix;
1852 /* Preferred explicit import library for dll's. */
1853 { "lib%s.dll.a", FALSE },
1854 /* Alternate explicit import library for dll's. */
1855 { "%s.dll.a", FALSE },
1856 /* "libfoo.a" could be either an import lib or a static lib.
1857 For backwards compatibility, libfoo.a needs to precede
1858 libfoo.dll and foo.dll in the search. */
1859 { "lib%s.a", FALSE },
1860 /* The 'native' spelling of an import lib name is "foo.lib". */
1861 { "%s.lib", FALSE },
1863 /* Try "<prefix>foo.dll" (preferred dll name, if specified). */
1864 { "%s%s.dll", TRUE },
1866 /* Try "libfoo.dll" (default preferred dll name). */
1867 { "lib%s.dll", FALSE },
1868 /* Finally try 'native' dll name "foo.dll". */
1869 { "%s.dll", FALSE },
1870 /* Note: If adding more formats to this table, make sure to check to
1871 see if their length is longer than libname_fmt[0].format, and if
1872 so, update the call to xmalloc() below. */
1875 static unsigned int format_max_len = 0;
1876 const char * filename;
1882 if (! entry->flags.maybe_archive || entry->flags.full_name_provided)
1885 filename = entry->filename;
1887 if (format_max_len == 0)
1888 /* We need to allow space in the memory that we are going to allocate
1889 for the characters in the format string. Since the format array is
1890 static we only need to calculate this information once. In theory
1891 this value could also be computed statically, but this introduces
1892 the possibility for a discrepancy and hence a possible memory
1893 corruption. The lengths we compute here will be too long because
1894 they will include any formating characters (%s) in the strings, but
1895 this will not matter. */
1896 for (i = 0; libname_fmt[i].format; i++)
1897 if (format_max_len < strlen (libname_fmt[i].format))
1898 format_max_len = strlen (libname_fmt[i].format);
1900 full_string = xmalloc (strlen (search->name)
1904 + (pep_dll_search_prefix
1905 ? strlen (pep_dll_search_prefix) : 0)
1907 /* Allow for the terminating NUL and for the path
1908 separator character that is inserted between
1909 search->name and the start of the format string. */
1912 sprintf (full_string, "%s/", search->name);
1913 base_string = full_string + strlen (full_string);
1915 for (i = 0; libname_fmt[i].format; i++)
1918 if (libname_fmt[i].use_prefix)
1920 if (!pep_dll_search_prefix)
1922 sprintf (base_string, libname_fmt[i].format, pep_dll_search_prefix, filename);
1926 sprintf (base_string, libname_fmt[i].format, filename);
1928 if (ldfile_try_open_bfd (full_string, entry))
1932 if (!libname_fmt[i].format)
1938 entry->filename = full_string;
1944 gld_${EMULATION_NAME}_find_potential_libraries
1945 (char *name, lang_input_statement_type *entry)
1947 return ldfile_open_file_search (name, entry, "", ".lib");
1951 gld_${EMULATION_NAME}_get_script (int *isfile)
1953 # Scripts compiled in.
1954 # sed commands to quote an ld script as a C string.
1955 sc="-f stringify.sed"
1961 if (link_info.relocatable && config.build_constructors)
1964 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1965 echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c
1966 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1967 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1968 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1969 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1970 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1971 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
1972 echo ' ; else if (link_info.pei386_auto_import == 1 && link_info.pei386_runtime_pseudo_reloc != 2) return' >> e${EMULATION_NAME}.c
1973 sed $sc ldscripts/${EMULATION_NAME}.xa >> e${EMULATION_NAME}.c
1975 echo ' ; else return' >> e${EMULATION_NAME}.c
1976 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1977 echo '; }' >> e${EMULATION_NAME}.c
1982 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1984 gld_${EMULATION_NAME}_before_parse,
1987 gld_${EMULATION_NAME}_after_parse,
1988 gld_${EMULATION_NAME}_after_open,
1989 after_allocation_default,
1990 set_output_arch_default,
1991 ldemul_default_target,
1992 gld_${EMULATION_NAME}_before_allocation,
1993 gld_${EMULATION_NAME}_get_script,
1994 "${EMULATION_NAME}",
1996 gld_${EMULATION_NAME}_finish,
1997 NULL, /* Create output section statements. */
1998 gld_${EMULATION_NAME}_open_dynamic_archive,
1999 gld_${EMULATION_NAME}_place_orphan,
2000 gld_${EMULATION_NAME}_set_symbols,
2001 NULL, /* parse_args */
2002 gld${EMULATION_NAME}_add_options,
2003 gld${EMULATION_NAME}_handle_option,
2004 gld_${EMULATION_NAME}_unrecognized_file,
2005 gld_${EMULATION_NAME}_list_options,
2006 gld_${EMULATION_NAME}_recognized_file,
2007 gld_${EMULATION_NAME}_find_potential_libraries,
2008 NULL /* new_vers_pattern. */