1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 if [ -z "$MACHINE" ]; then
6 OUTPUT_ARCH=${ARCH}:${MACHINE}
8 rm -f e${EMULATION_NAME}.c
9 (echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
11 /* Copyright 2006, 2007, 2008, 2009, 2010, 2011
12 Free Software Foundation, Inc.
13 Written by Kai Tietz, OneVision Software GmbH&CoKg.
15 This file is part of the GNU Binutils.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 3 of the License, or
20 (at your option) any later version.
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
30 MA 02110-1301, USA. */
33 /* For WINDOWS_XP64 and higher */
34 /* Based on pe.em, but modified for 64 bit support. */
36 #define TARGET_IS_${EMULATION_NAME}
38 #define COFF_IMAGE_WITH_PE
40 #define COFF_WITH_pex64
46 #include "libiberty.h"
57 #include "coff/internal.h"
59 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
60 header in generic PE code. */
61 #include "coff/x86_64.h"
64 /* FIXME: This is a BFD internal header file, and we should not be
66 #include "../bfd/libcoff.h"
69 #define AOUTSZ PEPAOUTSZ
70 #define PEAOUTHDR PEPAOUTHDR
74 #include "safe-ctype.h"
76 /* Permit the emulation parameters to override the default section
77 alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes
78 it seem that include/coff/internal.h should not define
79 PE_DEF_SECTION_ALIGNMENT. */
80 #if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
81 #undef PE_DEF_SECTION_ALIGNMENT
82 #define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
85 #ifdef TARGET_IS_i386pep
89 #if defined(TARGET_IS_i386pep) || ! defined(DLL_SUPPORT)
90 #define PE_DEF_SUBSYSTEM 3
92 #undef NT_EXE_IMAGE_BASE
93 #define NT_EXE_IMAGE_BASE 0x00010000
94 #undef PE_DEF_SECTION_ALIGNMENT
95 #define PE_DEF_SUBSYSTEM 2
96 #undef PE_DEF_FILE_ALIGNMENT
97 #define PE_DEF_FILE_ALIGNMENT 0x00000200
98 #define PE_DEF_SECTION_ALIGNMENT 0x00000400
101 static struct internal_extra_pe_aouthdr pep;
103 static int pep_subsystem = ${SUBSYSTEM};
104 static flagword real_flags = IMAGE_FILE_LARGE_ADDRESS_AWARE;
105 static int support_old_code = 0;
106 static lang_assignment_statement_type *image_base_statement = 0;
107 static unsigned short pe_dll_characteristics = 0;
110 static int pep_enable_stdcall_fixup = 1; /* 0=disable 1=enable (default). */
111 static char * pep_out_def_filename = NULL;
112 static char * pep_implib_filename = NULL;
113 static int pep_enable_auto_image_base = 0;
114 static char * pep_dll_search_prefix = NULL;
117 extern const char *output_filename;
119 static int is_underscoring (void)
122 if (pep_leading_underscore != -1)
123 return pep_leading_underscore;
124 if (!bfd_get_target_info ("${OUTPUT_FORMAT}", NULL, NULL, &u, NULL))
125 bfd_get_target_info ("${RELOCATEABLE_OUTPUT_FORMAT}", NULL, NULL, &u, NULL);
129 pep_leading_underscore = (u != 0 ? 1 : 0);
130 return pep_leading_underscore;
135 gld_${EMULATION_NAME}_before_parse (void)
138 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
139 output_filename = "${EXECUTABLE_NAME:-a.exe}";
141 config.dynamic_link = TRUE;
142 config.has_shared = 1;
143 link_info.pei386_auto_import = 1;
144 link_info.pei386_runtime_pseudo_reloc = 2; /* Use by default version 2. */
148 /* PE format extra command line options. */
150 /* Used for setting flags in the PE header. */
153 OPTION_BASE_FILE = 300 + 1,
155 OPTION_FILE_ALIGNMENT,
157 OPTION_MAJOR_IMAGE_VERSION,
158 OPTION_MAJOR_OS_VERSION,
159 OPTION_MAJOR_SUBSYSTEM_VERSION,
160 OPTION_MINOR_IMAGE_VERSION,
161 OPTION_MINOR_OS_VERSION,
162 OPTION_MINOR_SUBSYSTEM_VERSION,
163 OPTION_SECTION_ALIGNMENT,
167 OPTION_SUPPORT_OLD_CODE,
170 OPTION_EXCLUDE_SYMBOLS,
171 OPTION_EXCLUDE_ALL_SYMBOLS,
173 OPTION_STDCALL_ALIASES,
174 OPTION_ENABLE_STDCALL_FIXUP,
175 OPTION_DISABLE_STDCALL_FIXUP,
176 OPTION_IMPLIB_FILENAME,
177 OPTION_WARN_DUPLICATE_EXPORTS,
179 OPTION_ENABLE_AUTO_IMAGE_BASE,
180 OPTION_DISABLE_AUTO_IMAGE_BASE,
181 OPTION_DLL_SEARCH_PREFIX,
182 OPTION_NO_DEFAULT_EXCLUDES,
183 OPTION_DLL_ENABLE_AUTO_IMPORT,
184 OPTION_DLL_DISABLE_AUTO_IMPORT,
185 OPTION_ENABLE_EXTRA_PE_DEBUG,
187 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC,
188 OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC,
189 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2,
190 OPTION_EXCLUDE_MODULES_FOR_IMPLIB,
191 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES,
192 OPTION_NO_LEADING_UNDERSCORE,
193 OPTION_LEADING_UNDERSCORE,
194 OPTION_ENABLE_LONG_SECTION_NAMES,
195 OPTION_DISABLE_LONG_SECTION_NAMES,
197 OPTION_FORCE_INTEGRITY,
203 OPTION_TERMINAL_SERVER_AWARE
207 gld${EMULATION_NAME}_add_options
208 (int ns ATTRIBUTE_UNUSED,
209 char **shortopts ATTRIBUTE_UNUSED,
211 struct option **longopts,
212 int nrl ATTRIBUTE_UNUSED,
213 struct option **really_longopts ATTRIBUTE_UNUSED)
215 static const struct option xtra_long[] =
218 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
219 {"dll", no_argument, NULL, OPTION_DLL},
220 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
221 {"heap", required_argument, NULL, OPTION_HEAP},
222 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
223 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
224 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
225 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
226 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
227 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
228 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
229 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
230 {"stack", required_argument, NULL, OPTION_STACK},
231 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
232 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
233 {"use-nul-prefixed-import-tables", no_argument, NULL,
234 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
235 {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
236 {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE},
238 /* getopt allows abbreviations, so we do this to stop it
239 from treating -o as an abbreviation for this option. */
240 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
241 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
242 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
243 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
244 {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS},
245 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
246 {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
247 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
248 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
249 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
250 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
251 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
252 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
253 /* getopt() allows abbreviations, so we do this to stop it from
254 treating -c as an abbreviation for these --compat-implib. */
255 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
256 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
257 {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
258 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
259 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
260 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
261 {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
262 {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
263 {"enable-extra-pep-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
264 {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
265 {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
266 {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2},
268 {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
269 {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
270 {"dynamicbase",no_argument, NULL, OPTION_DYNAMIC_BASE},
271 {"forceinteg", no_argument, NULL, OPTION_FORCE_INTEGRITY},
272 {"nxcompat", no_argument, NULL, OPTION_NX_COMPAT},
273 {"no-isolation", no_argument, NULL, OPTION_NO_ISOLATION},
274 {"no-seh", no_argument, NULL, OPTION_NO_SEH},
275 {"no-bind", no_argument, NULL, OPTION_NO_BIND},
276 {"wdmdriver", no_argument, NULL, OPTION_WDM_DRIVER},
277 {"tsaware", no_argument, NULL, OPTION_TERMINAL_SERVER_AWARE},
278 {NULL, no_argument, NULL, 0}
282 = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
283 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
286 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
287 parameters which may be input from the command line. */
296 /* FALSE for an assembly level symbol and TRUE for a C visible symbol.
297 C visible symbols can be prefixed by underscore dependent on target's
299 bfd_boolean is_c_symbol;
302 #define GET_INIT_SYMBOL_NAME(IDX) \
303 (init[(IDX)].symbol \
304 + ((init[(IDX)].is_c_symbol == FALSE || (is_underscoring () == 1)) ? 0 : 1))
306 /* Decorates the C visible symbol by underscore, if target requires. */
308 ((is_underscoring () == 0) ? CSTR : "_" CSTR)
310 /* Get size of constant string for a possible underscore prefixed
312 #define U_SIZE(CSTR) \
313 (sizeof (CSTR) + (is_underscoring () == 0 ? 0 : 1))
315 #define D(field,symbol,def,usc) {&pep.field,sizeof(pep.field), def, symbol,0, usc}
317 static definfo init[] =
319 /* imagebase must be first */
320 #define IMAGEBASEOFF 0
321 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE, FALSE),
323 {&dll, sizeof(dll), 0, "__dll__", 0, FALSE},
324 #define MSIMAGEBASEOFF 2
325 D(ImageBase, "___ImageBase", NT_EXE_IMAGE_BASE, TRUE),
326 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT, FALSE),
327 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT, FALSE),
328 D(MajorOperatingSystemVersion,"__major_os_version__", 4, FALSE),
329 D(MinorOperatingSystemVersion,"__minor_os_version__", 0, FALSE),
330 D(MajorImageVersion,"__major_image_version__", 0, FALSE),
331 D(MinorImageVersion,"__minor_image_version__", 0, FALSE),
332 D(MajorSubsystemVersion,"__major_subsystem_version__", 5, FALSE),
333 D(MinorSubsystemVersion,"__minor_subsystem_version__", 2, FALSE),
334 D(Subsystem,"__subsystem__", ${SUBSYSTEM}, FALSE),
335 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000, FALSE),
336 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000, FALSE),
337 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000, FALSE),
338 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000, FALSE),
339 D(LoaderFlags,"__loader_flags__", 0x0, FALSE),
340 D(DllCharacteristics, "__dll_characteristics__", 0x0, FALSE),
341 { NULL, 0, 0, NULL, 0, FALSE}
346 gld_${EMULATION_NAME}_list_options (FILE *file)
348 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
349 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
350 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
351 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
352 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
353 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
354 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
355 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
356 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
357 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
358 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
359 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
360 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
361 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
362 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
363 fprintf (file, _(" --[no-]leading-underscore Set explicit symbol underscore prefix mode\n"));
365 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
366 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
367 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
368 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
369 fprintf (file, _(" --exclude-all-symbols Exclude all symbols from automatic export\n"));
370 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
371 fprintf (file, _(" --exclude-modules-for-implib mod,mod,...\n"));
372 fprintf (file, _(" Exclude objects, archive members from auto\n"));
373 fprintf (file, _(" export, place into import library instead.\n"));
374 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
375 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
376 fprintf (file, _(" --out-implib <file> Generate import library\n"));
377 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
378 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
379 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
380 create __imp_<SYMBOL> as well.\n"));
381 fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n\
382 unless user specifies one\n"));
383 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
384 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
385 an importlib, use <string><basename>.dll\n\
386 in preference to lib<basename>.dll \n"));
387 fprintf (file, _(" --enable-auto-import Do sophisticated linking of _sym to\n\
388 __imp_sym for DATA references\n"));
389 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
390 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
391 adding pseudo-relocations resolved at\n\
393 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
394 auto-imported DATA.\n"));
395 fprintf (file, _(" --enable-extra-pep-debug Enable verbose debug output when building\n\
396 or linking to DLLs (esp. auto-import)\n"));
397 fprintf (file, _(" --enable-long-section-names Use long COFF section names even in\n\
398 executable image files\n"));
399 fprintf (file, _(" --disable-long-section-names Never use long COFF section names, even\n\
400 in object files\n"));
401 fprintf (file, _(" --dynamicbase Image base address may be relocated using\n\
402 address space layout randomization (ASLR)\n"));
403 fprintf (file, _(" --forceinteg Code integrity checks are enforced\n"));
404 fprintf (file, _(" --nxcompat Image is compatible with data execution prevention\n"));
405 fprintf (file, _(" --no-isolation Image understands isolation but do not isolate the image\n"));
406 fprintf (file, _(" --no-seh Image does not use SEH. No SE handler may\n\
407 be called in this image\n"));
408 fprintf (file, _(" --no-bind Do not bind this image\n"));
409 fprintf (file, _(" --wdmdriver Driver uses the WDM model\n"));
410 fprintf (file, _(" --tsaware Image is Terminal Server aware\n"));
416 set_pep_name (char *name, bfd_vma val)
420 /* Find the name and set it. */
421 for (i = 0; init[i].ptr; i++)
423 if (strcmp (name, GET_INIT_SYMBOL_NAME (i)) == 0)
427 if (strcmp (name,"__image_base__") == 0)
428 set_pep_name (U ("__ImageBase"), val);
436 set_entry_point (void)
439 const char *initial_symbol_char;
449 { 1, "NtProcessStartup" },
450 { 2, "WinMainCRTStartup" },
451 { 3, "mainCRTStartup" },
452 { 7, "__PosixProcessStartup" },
453 { 9, "WinMainCRTStartup" },
454 {14, "mainCRTStartup" },
458 /* Entry point name for arbitrary subsystem numbers. */
459 static const char default_entry[] = "mainCRTStartup";
461 if (link_info.shared || dll)
463 entry = "DllMainCRTStartup";
467 for (i = 0; v[i].entry; i++)
468 if (v[i].value == pep_subsystem)
471 /* If no match, use the default. */
472 if (v[i].entry != NULL)
475 entry = default_entry;
478 /* Now we check target's default for getting proper symbol_char. */
479 initial_symbol_char = (is_underscoring () != 0 ? "_" : "");
481 if (*initial_symbol_char != '\0')
485 /* lang_default_entry expects its argument to be permanently
486 allocated, so we don't free this string. */
487 alc_entry = xmalloc (strlen (initial_symbol_char)
490 strcpy (alc_entry, initial_symbol_char);
491 strcat (alc_entry, entry);
495 lang_default_entry (entry);
499 set_pep_subsystem (void)
505 unsigned long temp_subsystem;
522 /* Check for the presence of a version number. */
523 sver = strchr (optarg, ':');
525 len = strlen (optarg);
529 set_pep_name ("__major_subsystem_version__",
530 strtoul (sver + 1, &end, 0));
532 set_pep_name ("__minor_subsystem_version__",
533 strtoul (end + 1, &end, 0));
535 einfo (_("%P: warning: bad version number in -subsystem option\n"));
538 /* Check for numeric subsystem. */
539 temp_subsystem = strtoul (optarg, & end, 0);
540 if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
542 /* Search list for a numeric match to use its entry point. */
543 for (i = 0; v[i].name; i++)
544 if (v[i].value == (int) temp_subsystem)
547 /* Use this subsystem. */
548 pep_subsystem = (int) temp_subsystem;
552 /* Search for subsystem by name. */
553 for (i = 0; v[i].name; i++)
554 if (strncmp (optarg, v[i].name, len) == 0
555 && v[i].name[len] == '\0')
558 if (v[i].name == NULL)
560 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
564 pep_subsystem = v[i].value;
567 set_pep_name ("__subsystem__", pep_subsystem);
574 set_pep_value (char *name)
578 set_pep_name (name, (bfd_vma) strtoull (optarg, &end, 0));
581 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
588 set_pep_stack_heap (char *resname, char *comname)
590 set_pep_value (resname);
595 set_pep_value (comname);
598 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
603 gld${EMULATION_NAME}_handle_option (int optc)
611 case OPTION_BASE_FILE:
612 link_info.base_file = fopen (optarg, FOPEN_WB);
613 if (link_info.base_file == NULL)
615 /* xgettext:c-format */
616 fprintf (stderr, _("%s: Can't open base file %s\n"),
617 program_name, optarg);
624 set_pep_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
627 set_pep_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
629 case OPTION_SUBSYSTEM:
630 set_pep_subsystem ();
632 case OPTION_MAJOR_OS_VERSION:
633 set_pep_value ("__major_os_version__");
635 case OPTION_MINOR_OS_VERSION:
636 set_pep_value ("__minor_os_version__");
638 case OPTION_MAJOR_SUBSYSTEM_VERSION:
639 set_pep_value ("__major_subsystem_version__");
641 case OPTION_MINOR_SUBSYSTEM_VERSION:
642 set_pep_value ("__minor_subsystem_version__");
644 case OPTION_MAJOR_IMAGE_VERSION:
645 set_pep_value ("__major_image_version__");
647 case OPTION_MINOR_IMAGE_VERSION:
648 set_pep_value ("__minor_image_version__");
650 case OPTION_FILE_ALIGNMENT:
651 set_pep_value ("__file_alignment__");
653 case OPTION_SECTION_ALIGNMENT:
654 set_pep_value ("__section_alignment__");
657 set_pep_name ("__dll__", 1);
659 case OPTION_IMAGE_BASE:
660 set_pep_value ("__image_base__");
662 case OPTION_SUPPORT_OLD_CODE:
663 support_old_code = 1;
665 case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
666 pep_use_nul_prefixed_import_tables = TRUE;
668 case OPTION_NO_LEADING_UNDERSCORE:
669 pep_leading_underscore = 0;
671 case OPTION_LEADING_UNDERSCORE:
672 pep_leading_underscore = 1;
676 pep_out_def_filename = xstrdup (optarg);
678 case OPTION_EXPORT_ALL:
679 pep_dll_export_everything = 1;
681 case OPTION_EXCLUDE_SYMBOLS:
682 pep_dll_add_excludes (optarg, EXCLUDESYMS);
684 case OPTION_EXCLUDE_ALL_SYMBOLS:
685 pep_dll_exclude_all_symbols = 1;
687 case OPTION_EXCLUDE_LIBS:
688 pep_dll_add_excludes (optarg, EXCLUDELIBS);
690 case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
691 pep_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
693 case OPTION_KILL_ATS:
694 pep_dll_kill_ats = 1;
696 case OPTION_STDCALL_ALIASES:
697 pep_dll_stdcall_aliases = 1;
699 case OPTION_ENABLE_STDCALL_FIXUP:
700 pep_enable_stdcall_fixup = 1;
702 case OPTION_DISABLE_STDCALL_FIXUP:
703 pep_enable_stdcall_fixup = 0;
705 case OPTION_IMPLIB_FILENAME:
706 pep_implib_filename = xstrdup (optarg);
708 case OPTION_WARN_DUPLICATE_EXPORTS:
709 pep_dll_warn_dup_exports = 1;
711 case OPTION_IMP_COMPAT:
712 pep_dll_compat_implib = 1;
714 case OPTION_ENABLE_AUTO_IMAGE_BASE:
715 pep_enable_auto_image_base = 1;
717 case OPTION_DISABLE_AUTO_IMAGE_BASE:
718 pep_enable_auto_image_base = 0;
720 case OPTION_DLL_SEARCH_PREFIX:
721 pep_dll_search_prefix = xstrdup (optarg);
723 case OPTION_NO_DEFAULT_EXCLUDES:
724 pep_dll_do_default_excludes = 0;
726 case OPTION_DLL_ENABLE_AUTO_IMPORT:
727 link_info.pei386_auto_import = 1;
729 case OPTION_DLL_DISABLE_AUTO_IMPORT:
730 link_info.pei386_auto_import = 0;
732 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
733 link_info.pei386_runtime_pseudo_reloc = 2;
735 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
736 link_info.pei386_runtime_pseudo_reloc = 0;
738 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2:
739 link_info.pei386_runtime_pseudo_reloc = 2;
741 case OPTION_ENABLE_EXTRA_PE_DEBUG:
742 pep_dll_extra_pe_debug = 1;
745 case OPTION_ENABLE_LONG_SECTION_NAMES:
746 pep_use_coff_long_section_names = 1;
748 case OPTION_DISABLE_LONG_SECTION_NAMES:
749 pep_use_coff_long_section_names = 0;
751 /* Get DLLCharacteristics bits */
752 case OPTION_DYNAMIC_BASE:
753 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
755 case OPTION_FORCE_INTEGRITY:
756 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
758 case OPTION_NX_COMPAT:
759 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
761 case OPTION_NO_ISOLATION:
762 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
765 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_SEH;
768 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_BIND;
770 case OPTION_WDM_DRIVER:
771 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
773 case OPTION_TERMINAL_SERVER_AWARE:
774 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
778 /* Set DLLCharacteristics bits */
779 set_pep_name ("__dll_characteristics__", pe_dll_characteristics);
787 strhash (const char *str)
789 const unsigned char *s;
796 s = (const unsigned char *) str;
797 while ((c = *s++) != '\0')
799 hash += c + (c << 17);
803 hash += len + (len << 17);
809 /* Use the output file to create a image base for relocatable DLLs. */
812 compute_dll_image_base (const char *ofile)
814 bfd_vma hash = (bfd_vma) strhash (ofile);
815 return 0x61300000 + ((hash << 16) & 0x0FFC0000);
819 /* Assign values to the special symbols before the linker script is
823 gld_${EMULATION_NAME}_set_symbols (void)
825 /* Run through and invent symbols for all the
826 names and insert the defaults. */
831 if (!init[IMAGEBASEOFF].inited)
833 if (link_info.relocatable)
834 init[IMAGEBASEOFF].value = 0;
835 else if (init[DLLOFF].value || (link_info.shared && !link_info.pie))
838 init[IMAGEBASEOFF].value = (pep_enable_auto_image_base
839 ? compute_dll_image_base (output_filename)
840 : NT_DLL_IMAGE_BASE);
842 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
846 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
847 init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
850 /* Don't do any symbol assignments if this is a relocatable link. */
851 if (link_info.relocatable)
854 /* Glue the assignments into the abs section. */
855 push_stat_ptr (&abs_output_section->children);
857 for (j = 0; init[j].ptr; j++)
859 bfd_vma val = init[j].value;
860 lang_assignment_statement_type *rv;
862 rv = lang_add_assignment (exp_assign (GET_INIT_SYMBOL_NAME (j),
864 if (init[j].size == sizeof (short))
865 *(short *) init[j].ptr = (short) val;
866 else if (init[j].size == sizeof (int))
867 *(int *) init[j].ptr = (int) val;
868 else if (init[j].size == sizeof (long))
869 *(long *) init[j].ptr = (long) val;
870 /* This might be a long long or other special type. */
871 else if (init[j].size == sizeof (bfd_vma))
872 *(bfd_vma *) init[j].ptr = val;
874 if (j == IMAGEBASEOFF)
875 image_base_statement = rv;
877 /* Restore the pointer. */
880 if (pep.FileAlignment > pep.SectionAlignment)
882 einfo (_("%P: warning, file alignment > section alignment.\n"));
886 /* This is called after the linker script and the command line options
890 gld_${EMULATION_NAME}_after_parse (void)
892 /* PR ld/6744: Warn the user if they have used an ELF-only
893 option hoping it will work on PE+. */
894 if (link_info.export_dynamic)
895 einfo (_("%P: warning: --export-dynamic is not supported for PE+ "
896 "targets, did you mean --export-all-symbols?\n"));
900 after_parse_default ();
903 /* pep-dll.c directly accesses pep_data_import_dll,
904 so it must be defined outside of #ifdef DLL_SUPPORT.
905 Note - this variable is deliberately not initialised.
906 This allows it to be treated as a common varaible, and only
907 exist in one incarnation in a multiple target enabled linker. */
908 char * pep_data_import_dll;
911 static struct bfd_link_hash_entry *pep_undef_found_sym;
914 pep_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
918 const char *hs = h->root.string;
920 sl = strlen (string);
921 if (h->type == bfd_link_hash_defined
922 && ((*hs == '@' && *string == '_'
923 && strncmp (hs + 1, string + 1, sl - 1) == 0)
924 || strncmp (hs, string, sl) == 0)
925 && h->root.string[sl] == '@')
927 pep_undef_found_sym = h;
934 pep_fixup_stdcalls (void)
936 static int gave_warning_message = 0;
937 struct bfd_link_hash_entry *undef, *sym;
939 if (pep_dll_extra_pe_debug)
940 printf ("%s\n", __FUNCTION__);
942 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
943 if (undef->type == bfd_link_hash_undefined)
945 char* at = strchr (undef->root.string, '@');
946 int lead_at = (*undef->root.string == '@');
948 at = strchr (undef->root.string + 1, '@');
951 /* The symbol is a stdcall symbol, so let's look for a
952 cdecl symbol with the same name and resolve to that. */
953 char *cname = xstrdup (undef->root.string);
957 at = strchr (cname, '@');
960 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
962 if (sym && sym->type == bfd_link_hash_defined)
964 undef->type = bfd_link_hash_defined;
965 undef->u.def.value = sym->u.def.value;
966 undef->u.def.section = sym->u.def.section;
968 if (pep_enable_stdcall_fixup == -1)
970 einfo (_("Warning: resolving %s by linking to %s\n"),
971 undef->root.string, cname);
972 if (! gave_warning_message)
974 gave_warning_message = 1;
975 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
976 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
983 /* The symbol is a cdecl symbol, so we look for stdcall
984 symbols - which means scanning the whole symbol table. */
985 pep_undef_found_sym = 0;
986 bfd_link_hash_traverse (link_info.hash, pep_undef_cdecl_match,
987 (char *) undef->root.string);
988 sym = pep_undef_found_sym;
991 undef->type = bfd_link_hash_defined;
992 undef->u.def.value = sym->u.def.value;
993 undef->u.def.section = sym->u.def.section;
995 if (pep_enable_stdcall_fixup == -1)
997 einfo (_("Warning: resolving %s by linking to %s\n"),
998 undef->root.string, sym->root.string);
999 if (! gave_warning_message)
1001 gave_warning_message = 1;
1002 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
1003 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
1012 make_import_fixup (arelent *rel, asection *s)
1014 struct bfd_symbol *sym = *rel->sym_ptr_ptr;
1016 bfd_vma _addend = 0;
1019 if (pep_dll_extra_pe_debug)
1020 printf ("arelent: %s@%#lx: add=%li\n", sym->name,
1021 (unsigned long) rel->address, (long) rel->addend);
1023 memset (addend, 0, sizeof (addend));
1024 switch ((rel->howto->bitsize))
1027 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 1);
1028 if (suc && rel->howto->pc_relative)
1029 _addend = (bfd_vma) ((bfd_signed_vma) ((char) bfd_get_8 (s->owner, addend)));
1031 _addend = ((bfd_vma) bfd_get_8 (s->owner, addend)) & 0xff;
1034 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 2);
1035 if (suc && rel->howto->pc_relative)
1036 _addend = (bfd_vma) ((bfd_signed_vma) ((short) bfd_get_16 (s->owner, addend)));
1038 _addend = ((bfd_vma) bfd_get_16 (s->owner, addend)) & 0xffff;
1041 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 4);
1042 if (suc && rel->howto->pc_relative)
1043 _addend = (bfd_vma) ((bfd_signed_vma) ((int) bfd_get_32 (s->owner, addend)));
1045 _addend = ((bfd_vma) bfd_get_32 (s->owner, addend)) & 0xffffffff;
1048 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 8);
1050 _addend = ((bfd_vma) bfd_get_64 (s->owner, addend));
1054 einfo (_("%C: Cannot get section contents - auto-import exception\n"),
1055 s->owner, s, rel->address);
1057 if (pep_dll_extra_pe_debug)
1059 printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx", (long) _addend, (long) rel->addend, (long) rel->address);
1060 if (rel->howto->pc_relative) printf (" pcrel");
1061 printf (" %d bit rel.\n",(int) rel->howto->bitsize);
1063 pep_create_import_fixup (rel, s, _addend);
1069 pep_find_data_imports (void)
1071 struct bfd_link_hash_entry *undef, *sym;
1073 if (link_info.pei386_auto_import == 0)
1076 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
1078 if (undef->type == bfd_link_hash_undefined)
1080 /* C++ symbols are *long*. */
1083 if (pep_dll_extra_pe_debug)
1084 printf ("%s:%s\n", __FUNCTION__, undef->root.string);
1086 sprintf (buf, "__imp_%s", undef->root.string);
1088 sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
1090 if (sym && sym->type == bfd_link_hash_defined)
1092 bfd *b = sym->u.def.section->owner;
1096 if (!bfd_generic_link_read_symbols (b))
1098 einfo (_("%B%F: could not read symbols: %E\n"), b);
1102 symbols = bfd_get_outsymbols (b);
1103 nsyms = bfd_get_symcount (b);
1105 for (i = 0; i < nsyms; i++)
1107 if (! CONST_STRNEQ (symbols[i]->name, U ("_head_")))
1110 if (pep_dll_extra_pe_debug)
1111 printf ("->%s\n", symbols[i]->name);
1113 pep_data_import_dll = (char*) (symbols[i]->name +
1114 U_SIZE ("_head_") - 1);
1118 pep_walk_relocs_of_symbol (&link_info, undef->root.string,
1121 /* Let's differentiate it somehow from defined. */
1122 undef->type = bfd_link_hash_defweak;
1123 /* We replace original name with __imp_ prefixed, this
1124 1) may trash memory 2) leads to duplicate symbol generation.
1125 Still, IMHO it's better than having name poluted. */
1126 undef->root.string = sym->root.string;
1127 undef->u.def.value = sym->u.def.value;
1128 undef->u.def.section = sym->u.def.section;
1135 pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
1137 if (pep_dll_extra_pe_debug)
1138 printf ("+%s\n", h->string);
1142 #endif /* DLL_SUPPORT */
1145 debug_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
1147 int *found = (int *) obj;
1148 if (strncmp (".debug_", sect->name, sizeof (".debug_") - 1) == 0)
1153 gld_${EMULATION_NAME}_after_open (void)
1155 after_open_default ();
1159 if (pep_dll_extra_pe_debug)
1162 struct bfd_link_hash_entry *sym;
1164 printf ("%s()\n", __FUNCTION__);
1166 for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1167 printf ("-%s\n", sym->root.string);
1168 bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
1170 for (a = link_info.input_bfds; a; a = a->link_next)
1171 printf ("*%s\n",a->filename);
1175 /* Pass the wacky PE command line options into the output bfd.
1176 FIXME: This should be done via a function, rather than by
1177 including an internal BFD header. */
1179 if (coff_data (link_info.output_bfd) == NULL
1180 || coff_data (link_info.output_bfd)->pe == 0)
1181 einfo (_("%F%P: cannot perform PE operations on non PE output file '%B'.\n"),
1182 link_info.output_bfd);
1184 pe_data (link_info.output_bfd)->pe_opthdr = pep;
1185 pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
1186 pe_data (link_info.output_bfd)->real_flags |= real_flags;
1188 /* At this point we must decide whether to use long section names
1189 in the output or not. If the user hasn't explicitly specified
1190 on the command line, we leave it to the default for the format
1191 (object files yes, image files no), except if there is debug
1192 information present; GDB relies on the long section names to
1193 find it, so enable it in that case. */
1194 if (pep_use_coff_long_section_names < 0 && link_info.strip == strip_none)
1196 /* Iterate over all sections of all input BFDs, checking
1197 for any that begin 'debug_' and are long names. */
1198 LANG_FOR_EACH_INPUT_STATEMENT (is)
1200 int found_debug = 0;
1201 bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug);
1204 pep_use_coff_long_section_names = 1;
1210 pep_output_file_set_long_section_names (link_info.output_bfd);
1213 if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
1214 pep_fixup_stdcalls ();
1216 pep_process_import_defs (link_info.output_bfd, &link_info);
1218 pep_find_data_imports ();
1220 /* As possibly new symbols are added by imports, we rerun
1221 stdcall/fastcall fixup here. */
1222 if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
1223 pep_fixup_stdcalls ();
1225 #ifndef TARGET_IS_i386pep
1226 if (link_info.shared)
1228 if (!link_info.relocatable)
1230 pep_dll_build_sections (link_info.output_bfd, &link_info);
1232 #ifndef TARGET_IS_i386pep
1234 pep_exe_build_sections (link_info.output_bfd, &link_info);
1236 #endif /* DLL_SUPPORT */
1239 /* This next chunk of code tries to detect the case where you have
1240 two import libraries for the same DLL (specifically,
1241 symbolically linking libm.a and libc.a in cygwin to
1242 libcygwin.a). In those cases, it's possible for function
1243 thunks from the second implib to be used but without the
1244 head/tail objects, causing an improper import table. We detect
1245 those cases and rename the "other" import libraries to match
1246 the one the head/tail come from, so that the linker will sort
1247 things nicely and produce a valid import table. */
1249 LANG_FOR_EACH_INPUT_STATEMENT (is)
1251 if (is->the_bfd->my_archive)
1253 int idata2 = 0, reloc_count=0, is_imp = 0;
1256 /* See if this is an import library thunk. */
1257 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1259 if (strcmp (sec->name, ".idata\$2") == 0)
1261 if (CONST_STRNEQ (sec->name, ".idata\$"))
1263 reloc_count += sec->reloc_count;
1266 if (is_imp && !idata2 && reloc_count)
1268 /* It is, look for the reference to head and see if it's
1269 from our own library. */
1270 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1278 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1282 if (!bfd_generic_link_read_symbols (is->the_bfd))
1284 einfo (_("%B%F: could not read symbols: %E\n"),
1288 symbols = bfd_get_outsymbols (is->the_bfd);
1290 relocs = xmalloc ((size_t) relsize);
1291 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1296 einfo ("%X%P: unable to process relocs: %E\n");
1300 for (i = 0; i < nrelocs; i++)
1302 struct bfd_symbol *s;
1303 struct bfd_link_hash_entry * blhe;
1304 char *other_bfd_filename;
1307 s = (relocs[i]->sym_ptr_ptr)[0];
1309 if (s->flags & BSF_LOCAL)
1312 /* Thunk section with reloc to another bfd. */
1313 blhe = bfd_link_hash_lookup (link_info.hash,
1315 FALSE, FALSE, TRUE);
1318 || blhe->type != bfd_link_hash_defined)
1322 = blhe->u.def.section->owner->my_archive
1323 ? bfd_get_filename (blhe->u.def.section->owner->my_archive)
1324 : bfd_get_filename (blhe->u.def.section->owner);
1326 if (strcmp (bfd_get_filename (is->the_bfd->my_archive),
1327 other_bfd_filename) == 0)
1330 /* Rename this implib to match the other one. */
1331 n = xmalloc (strlen (other_bfd_filename) + 1);
1332 strcpy (n, other_bfd_filename);
1333 is->the_bfd->my_archive->filename = n;
1337 /* Note - we do not free the symbols,
1338 they are now cached in the BFD. */
1348 lang_input_statement_type *is2;
1349 lang_input_statement_type *is3;
1351 /* Careful - this is a shell script. Watch those dollar signs! */
1352 /* Microsoft import libraries have every member named the same,
1353 and not in the right order for us to link them correctly. We
1354 must detect these and rename the members so that they'll link
1355 correctly. There are three types of objects: the head, the
1356 thunks, and the sentinel(s). The head is easy; it's the one
1357 with idata2. We assume that the sentinels won't have relocs,
1358 and the thunks will. It's easier than checking the symbol
1359 table for external references. */
1360 LANG_FOR_EACH_INPUT_STATEMENT (is)
1362 if (is->the_bfd->my_archive)
1365 bfd *arch = is->the_bfd->my_archive;
1367 if (cur_arch != arch)
1373 is3 && is3->the_bfd->my_archive == arch;
1374 is3 = (lang_input_statement_type *) is3->next)
1376 /* A MS dynamic import library can also contain static
1377 members, so look for the first element with a .dll
1378 extension, and use that for the remainder of the
1380 pnt = strrchr (is3->the_bfd->filename, '.');
1381 if (pnt != NULL && strcmp (pnt, ".dll") == 0)
1389 /* OK, found one. Now look to see if the remaining
1390 (dynamic import) members use the same name. */
1392 is2 && is2->the_bfd->my_archive == arch;
1393 is2 = (lang_input_statement_type *) is2->next)
1395 /* Skip static members, ie anything with a .obj
1397 pnt = strrchr (is2->the_bfd->filename, '.');
1398 if (pnt != NULL && strcmp (pnt, ".obj") == 0)
1401 if (strcmp (is3->the_bfd->filename,
1402 is2->the_bfd->filename))
1411 /* This fragment might have come from an .obj file in a Microsoft
1412 import, and not an actual import record. If this is the case,
1413 then leave the filename alone. */
1414 pnt = strrchr (is->the_bfd->filename, '.');
1416 if (is_ms_arch && (strcmp (pnt, ".dll") == 0))
1418 int idata2 = 0, reloc_count=0;
1420 char *new_name, seq;
1422 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1424 if (strcmp (sec->name, ".idata\$2") == 0)
1426 reloc_count += sec->reloc_count;
1429 if (idata2) /* .idata2 is the TOC */
1431 else if (reloc_count > 0) /* thunks */
1436 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
1437 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
1438 is->the_bfd->filename = new_name;
1440 new_name = xmalloc (strlen (is->filename) + 3);
1441 sprintf (new_name, "%s.%c", is->filename, seq);
1442 is->filename = new_name;
1450 gld_${EMULATION_NAME}_before_allocation (void)
1453 before_allocation_default ();
1457 /* This is called when an input file isn't recognized as a BFD. We
1458 check here for .DEF files and pull them in automatically. */
1461 saw_option (char *option)
1467 for (i = 0; init[i].ptr; i++)
1468 if (strcmp (GET_INIT_SYMBOL_NAME (i), option) == 0)
1469 return init[i].inited;
1472 #endif /* DLL_SUPPORT */
1475 gld_${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1478 const char *ext = entry->filename + strlen (entry->filename) - 4;
1480 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
1482 pep_def_file = def_file_parse (entry->filename, pep_def_file);
1486 int i, buflen=0, len;
1489 for (i = 0; i < pep_def_file->num_exports; i++)
1491 len = strlen (pep_def_file->exports[i].internal_name);
1492 if (buflen < len + 2)
1496 buf = xmalloc (buflen);
1498 for (i = 0; i < pep_def_file->num_exports; i++)
1500 struct bfd_link_hash_entry *h;
1502 sprintf (buf, "%s%s", U (""),
1503 pep_def_file->exports[i].internal_name);
1505 h = bfd_link_hash_lookup (link_info.hash, buf, TRUE, TRUE, TRUE);
1506 if (h == (struct bfd_link_hash_entry *) NULL)
1507 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1508 if (h->type == bfd_link_hash_new)
1510 h->type = bfd_link_hash_undefined;
1511 h->u.undef.abfd = NULL;
1512 bfd_link_add_undef (link_info.hash, h);
1517 /* def_file_print (stdout, pep_def_file); */
1518 if (pep_def_file->is_dll == 1)
1519 link_info.shared = 1;
1521 if (pep_def_file->base_address != (bfd_vma)(-1))
1524 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1525 = init[IMAGEBASEOFF].value
1526 = pep_def_file->base_address;
1527 init[IMAGEBASEOFF].inited = 1;
1528 if (image_base_statement)
1529 image_base_statement->exp = exp_assign ("__image_base__",
1530 exp_intop (pep.ImageBase));
1533 if (pep_def_file->stack_reserve != -1
1534 && ! saw_option ("__size_of_stack_reserve__"))
1536 pep.SizeOfStackReserve = pep_def_file->stack_reserve;
1537 if (pep_def_file->stack_commit != -1)
1538 pep.SizeOfStackCommit = pep_def_file->stack_commit;
1540 if (pep_def_file->heap_reserve != -1
1541 && ! saw_option ("__size_of_heap_reserve__"))
1543 pep.SizeOfHeapReserve = pep_def_file->heap_reserve;
1544 if (pep_def_file->heap_commit != -1)
1545 pep.SizeOfHeapCommit = pep_def_file->heap_commit;
1555 gld_${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1559 #ifdef TARGET_IS_i386pep
1560 pep_dll_id_target ("pei-x86-64");
1562 if (pep_bfd_is_dll (entry->the_bfd))
1563 return pep_implied_import_dll (entry->filename);
1569 gld_${EMULATION_NAME}_finish (void)
1575 if (link_info.shared
1576 || (!link_info.relocatable && pep_def_file->num_exports != 0))
1578 pep_dll_fill_sections (link_info.output_bfd, &link_info);
1579 if (pep_implib_filename)
1580 pep_dll_generate_implib (pep_def_file, pep_implib_filename, &link_info);
1583 if (pep_out_def_filename)
1584 pep_dll_generate_def_file (pep_out_def_filename);
1585 #endif /* DLL_SUPPORT */
1587 /* I don't know where .idata gets set as code, but it shouldn't be. */
1589 asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
1593 asec->flags &= ~SEC_CODE;
1594 asec->flags |= SEC_DATA;
1600 /* Place an orphan section.
1602 We use this to put sections in a reasonable place in the file, and
1603 to ensure that they are aligned as required.
1605 We handle grouped sections here as well. A section named .foo\$nn
1606 goes into the output section .foo. All grouped sections are sorted
1609 Grouped sections for the default sections are handled by the
1610 default linker script using wildcards, and are sorted by
1613 static lang_output_section_statement_type *
1614 gld_${EMULATION_NAME}_place_orphan (asection *s,
1615 const char *secname,
1618 const char *orig_secname = secname;
1619 char *dollar = NULL;
1620 lang_output_section_statement_type *os;
1621 lang_statement_list_type add_child;
1622 lang_output_section_statement_type *match_by_name = NULL;
1623 lang_statement_union_type **pl;
1625 /* Look through the script to see where to place this section. */
1626 if (!link_info.relocatable
1627 && (dollar = strchr (secname, '\$')) != NULL)
1629 size_t len = dollar - secname;
1630 char *newname = xmalloc (len + 1);
1631 memcpy (newname, secname, len);
1632 newname[len] = '\0';
1636 lang_list_init (&add_child);
1639 if (constraint == 0)
1640 for (os = lang_output_section_find (secname);
1642 os = next_matching_output_section_statement (os, 0))
1644 /* If we don't match an existing output section, tell
1645 lang_insert_orphan to create a new output section. */
1646 constraint = SPECIAL;
1648 if (os->bfd_section != NULL
1649 && (os->bfd_section->flags == 0
1650 || ((s->flags ^ os->bfd_section->flags)
1651 & (SEC_LOAD | SEC_ALLOC)) == 0))
1653 /* We already have an output section statement with this
1654 name, and its bfd section has compatible flags.
1655 If the section already exists but does not have any flags set,
1656 then it has been created by the linker, probably as a result of
1657 a --section-start command line switch. */
1658 lang_add_section (&add_child, s, os);
1662 /* Save unused output sections in case we can match them
1663 against orphans later. */
1664 if (os->bfd_section == NULL)
1668 /* If we didn't match an active output section, see if we matched an
1669 unused one and use that. */
1670 if (os == NULL && match_by_name)
1672 lang_add_section (&match_by_name->children, s, match_by_name);
1673 return match_by_name;
1678 static struct orphan_save hold[] =
1681 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1684 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1687 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1690 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1696 enum orphan_save_index
1704 static int orphan_init_done = 0;
1705 struct orphan_save *place;
1706 lang_output_section_statement_type *after;
1707 etree_type *address;
1709 if (!orphan_init_done)
1711 struct orphan_save *ho;
1712 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1713 if (ho->name != NULL)
1715 ho->os = lang_output_section_find (ho->name);
1716 if (ho->os != NULL && ho->os->flags == 0)
1717 ho->os->flags = ho->flags;
1719 orphan_init_done = 1;
1722 /* Try to put the new output section in a reasonable place based
1723 on the section name and section flags. */
1726 if ((s->flags & SEC_ALLOC) == 0)
1728 else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
1729 place = &hold[orphan_bss];
1730 else if ((s->flags & SEC_READONLY) == 0)
1731 place = &hold[orphan_data];
1732 else if ((s->flags & SEC_CODE) == 0)
1734 place = (!strncmp (secname, ".idata\$", 7) ? &hold[orphan_idata]
1735 : &hold[orphan_rodata]);
1738 place = &hold[orphan_text];
1743 if (place->os == NULL)
1744 place->os = lang_output_section_find (place->name);
1747 after = lang_output_section_find_by_flags (s, &place->os, NULL);
1749 /* *ABS* is always the first output section statement. */
1750 after = (&lang_output_section_statement.head
1751 ->output_section_statement);
1754 /* All sections in an executable must be aligned to a page boundary. */
1755 address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
1756 os = lang_insert_orphan (s, secname, constraint, after, place, address,
1760 /* If the section name has a '\$', sort it with the other '\$'
1762 for (pl = &os->children.head; *pl != NULL; pl = &(*pl)->header.next)
1764 lang_input_section_type *ls;
1767 if ((*pl)->header.type != lang_input_section_enum)
1770 ls = &(*pl)->input_section;
1772 lname = bfd_get_section_name (ls->section->owner, ls->section);
1773 if (strchr (lname, '\$') != NULL
1774 && (dollar == NULL || strcmp (orig_secname, lname) < 0))
1778 if (add_child.head != NULL)
1780 *add_child.tail = *pl;
1781 *pl = add_child.head;
1788 gld_${EMULATION_NAME}_open_dynamic_archive
1789 (const char *arch ATTRIBUTE_UNUSED,
1790 search_dirs_type *search,
1791 lang_input_statement_type *entry)
1795 const char * format;
1796 bfd_boolean use_prefix;
1800 /* Preferred explicit import library for dll's. */
1801 { "lib%s.dll.a", FALSE },
1802 /* Alternate explicit import library for dll's. */
1803 { "%s.dll.a", FALSE },
1804 /* "libfoo.a" could be either an import lib or a static lib.
1805 For backwards compatibility, libfoo.a needs to precede
1806 libfoo.dll and foo.dll in the search. */
1807 { "lib%s.a", FALSE },
1808 /* The 'native' spelling of an import lib name is "foo.lib". */
1809 { "%s.lib", FALSE },
1811 /* Try "<prefix>foo.dll" (preferred dll name, if specified). */
1812 { "%s%s.dll", TRUE },
1814 /* Try "libfoo.dll" (default preferred dll name). */
1815 { "lib%s.dll", FALSE },
1816 /* Finally try 'native' dll name "foo.dll". */
1817 { "%s.dll", FALSE },
1818 /* Note: If adding more formats to this table, make sure to check to
1819 see if their length is longer than libname_fmt[0].format, and if
1820 so, update the call to xmalloc() below. */
1823 static unsigned int format_max_len = 0;
1824 const char * filename;
1830 if (! entry->maybe_archive)
1833 filename = entry->filename;
1835 if (format_max_len == 0)
1836 /* We need to allow space in the memory that we are going to allocate
1837 for the characters in the format string. Since the format array is
1838 static we only need to calculate this information once. In theory
1839 this value could also be computed statically, but this introduces
1840 the possibility for a discrepancy and hence a possible memory
1841 corruption. The lengths we compute here will be too long because
1842 they will include any formating characters (%s) in the strings, but
1843 this will not matter. */
1844 for (i = 0; libname_fmt[i].format; i++)
1845 if (format_max_len < strlen (libname_fmt[i].format))
1846 format_max_len = strlen (libname_fmt[i].format);
1848 full_string = xmalloc (strlen (search->name)
1852 + (pep_dll_search_prefix
1853 ? strlen (pep_dll_search_prefix) : 0)
1855 /* Allow for the terminating NUL and for the path
1856 separator character that is inserted between
1857 search->name and the start of the format string. */
1860 sprintf (full_string, "%s/", search->name);
1861 base_string = full_string + strlen (full_string);
1863 for (i = 0; libname_fmt[i].format; i++)
1866 if (libname_fmt[i].use_prefix)
1868 if (!pep_dll_search_prefix)
1870 sprintf (base_string, libname_fmt[i].format, pep_dll_search_prefix, filename);
1874 sprintf (base_string, libname_fmt[i].format, filename);
1876 if (ldfile_try_open_bfd (full_string, entry))
1880 if (!libname_fmt[i].format)
1886 entry->filename = full_string;
1892 gld_${EMULATION_NAME}_find_potential_libraries
1893 (char *name, lang_input_statement_type *entry)
1895 return ldfile_open_file_search (name, entry, "", ".lib");
1899 gld_${EMULATION_NAME}_get_script (int *isfile)
1901 # Scripts compiled in.
1902 # sed commands to quote an ld script as a C string.
1903 sc="-f stringify.sed"
1909 if (link_info.relocatable && config.build_constructors)
1912 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1913 echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c
1914 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1915 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1916 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1917 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1918 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1919 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
1920 echo ' ; else if (link_info.pei386_auto_import == 1 && link_info.pei386_runtime_pseudo_reloc != 2) return' >> e${EMULATION_NAME}.c
1921 sed $sc ldscripts/${EMULATION_NAME}.xa >> e${EMULATION_NAME}.c
1923 echo ' ; else return' >> e${EMULATION_NAME}.c
1924 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1925 echo '; }' >> e${EMULATION_NAME}.c
1930 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1932 gld_${EMULATION_NAME}_before_parse,
1935 gld_${EMULATION_NAME}_after_parse,
1936 gld_${EMULATION_NAME}_after_open,
1937 after_allocation_default,
1938 set_output_arch_default,
1939 ldemul_default_target,
1940 gld_${EMULATION_NAME}_before_allocation,
1941 gld_${EMULATION_NAME}_get_script,
1942 "${EMULATION_NAME}",
1944 gld_${EMULATION_NAME}_finish,
1945 NULL, /* Create output section statements. */
1946 gld_${EMULATION_NAME}_open_dynamic_archive,
1947 gld_${EMULATION_NAME}_place_orphan,
1948 gld_${EMULATION_NAME}_set_symbols,
1949 NULL, /* parse_args */
1950 gld${EMULATION_NAME}_add_options,
1951 gld${EMULATION_NAME}_handle_option,
1952 gld_${EMULATION_NAME}_unrecognized_file,
1953 gld_${EMULATION_NAME}_list_options,
1954 gld_${EMULATION_NAME}_recognized_file,
1955 gld_${EMULATION_NAME}_find_potential_libraries,
1956 NULL /* new_vers_pattern. */