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 Free Software Foundation, Inc.
12 Written by Kai Tietz, OneVision Software GmbH&CoKg.
14 This file is part of the GNU Binutils.
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
29 MA 02110-1301, USA. */
32 /* For WINDOWS_XP64 and higher */
33 /* Based on pe.em, but modified for 64 bit support. */
35 #define TARGET_IS_${EMULATION_NAME}
37 #define COFF_IMAGE_WITH_PE
39 #define COFF_WITH_pex64
45 #include "libiberty.h"
56 #include "coff/internal.h"
58 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
59 header in generic PE code. */
60 #include "coff/x86_64.h"
63 /* FIXME: This is a BFD internal header file, and we should not be
65 #include "../bfd/libcoff.h"
68 #define AOUTSZ PEPAOUTSZ
69 #define PEAOUTHDR PEPAOUTHDR
73 #include "safe-ctype.h"
75 /* Permit the emulation parameters to override the default section
76 alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes
77 it seem that include/coff/internal.h should not define
78 PE_DEF_SECTION_ALIGNMENT. */
79 #if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
80 #undef PE_DEF_SECTION_ALIGNMENT
81 #define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
84 #ifdef TARGET_IS_i386pep
88 #if defined(TARGET_IS_i386pep) || ! defined(DLL_SUPPORT)
89 #define PE_DEF_SUBSYSTEM 3
91 #undef NT_EXE_IMAGE_BASE
92 #define NT_EXE_IMAGE_BASE 0x00010000
93 #undef PE_DEF_SECTION_ALIGNMENT
94 #define PE_DEF_SUBSYSTEM 2
95 #undef PE_DEF_FILE_ALIGNMENT
96 #define PE_DEF_FILE_ALIGNMENT 0x00000200
97 #define PE_DEF_SECTION_ALIGNMENT 0x00000400
100 #define U(S) ${INITIAL_SYMBOL_CHAR} S
102 static struct internal_extra_pe_aouthdr pep;
104 static int pep_subsystem = ${SUBSYSTEM};
105 static flagword real_flags = IMAGE_FILE_LARGE_ADDRESS_AWARE;
106 static int support_old_code = 0;
107 static lang_assignment_statement_type *image_base_statement = 0;
108 static unsigned short pe_dll_characteristics = 0;
111 static int pep_enable_stdcall_fixup = 1; /* 0=disable 1=enable (default). */
112 static char * pep_out_def_filename = NULL;
113 static char * pep_implib_filename = NULL;
114 static int pep_enable_auto_image_base = 0;
115 static char * pep_dll_search_prefix = NULL;
118 extern const char *output_filename;
121 gld_${EMULATION_NAME}_before_parse (void)
123 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
124 output_filename = "${EXECUTABLE_NAME:-a.exe}";
126 config.dynamic_link = TRUE;
127 config.has_shared = 1;
128 link_info.pei386_auto_import = -1;
129 link_info.pei386_runtime_pseudo_reloc = 2; /* Use by default version 2. */
133 /* PE format extra command line options. */
135 /* Used for setting flags in the PE header. */
138 OPTION_BASE_FILE = 300 + 1,
140 OPTION_FILE_ALIGNMENT,
142 OPTION_MAJOR_IMAGE_VERSION,
143 OPTION_MAJOR_OS_VERSION,
144 OPTION_MAJOR_SUBSYSTEM_VERSION,
145 OPTION_MINOR_IMAGE_VERSION,
146 OPTION_MINOR_OS_VERSION,
147 OPTION_MINOR_SUBSYSTEM_VERSION,
148 OPTION_SECTION_ALIGNMENT,
152 OPTION_SUPPORT_OLD_CODE,
155 OPTION_EXCLUDE_SYMBOLS,
156 OPTION_EXCLUDE_ALL_SYMBOLS,
158 OPTION_STDCALL_ALIASES,
159 OPTION_ENABLE_STDCALL_FIXUP,
160 OPTION_DISABLE_STDCALL_FIXUP,
161 OPTION_IMPLIB_FILENAME,
162 OPTION_WARN_DUPLICATE_EXPORTS,
164 OPTION_ENABLE_AUTO_IMAGE_BASE,
165 OPTION_DISABLE_AUTO_IMAGE_BASE,
166 OPTION_DLL_SEARCH_PREFIX,
167 OPTION_NO_DEFAULT_EXCLUDES,
168 OPTION_DLL_ENABLE_AUTO_IMPORT,
169 OPTION_DLL_DISABLE_AUTO_IMPORT,
170 OPTION_ENABLE_EXTRA_PE_DEBUG,
172 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC,
173 OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC,
174 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1,
175 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2,
176 OPTION_EXCLUDE_MODULES_FOR_IMPLIB,
177 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES,
178 OPTION_ENABLE_LONG_SECTION_NAMES,
179 OPTION_DISABLE_LONG_SECTION_NAMES,
181 OPTION_FORCE_INTEGRITY,
187 OPTION_TERMINAL_SERVER_AWARE
191 gld${EMULATION_NAME}_add_options
192 (int ns ATTRIBUTE_UNUSED,
193 char **shortopts ATTRIBUTE_UNUSED,
195 struct option **longopts,
196 int nrl ATTRIBUTE_UNUSED,
197 struct option **really_longopts ATTRIBUTE_UNUSED)
199 static const struct option xtra_long[] =
202 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
203 {"dll", no_argument, NULL, OPTION_DLL},
204 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
205 {"heap", required_argument, NULL, OPTION_HEAP},
206 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
207 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
208 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
209 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
210 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
211 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
212 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
213 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
214 {"stack", required_argument, NULL, OPTION_STACK},
215 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
216 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
217 {"use-nul-prefixed-import-tables", no_argument, NULL,
218 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
220 /* getopt allows abbreviations, so we do this to stop it
221 from treating -o as an abbreviation for this option. */
222 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
223 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
224 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
225 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
226 {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS},
227 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
228 {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
229 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
230 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
231 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
232 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
233 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
234 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
235 /* getopt() allows abbreviations, so we do this to stop it from
236 treating -c as an abbreviation for these --compat-implib. */
237 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
238 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
239 {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
240 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
241 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
242 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
243 {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
244 {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
245 {"enable-extra-pep-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
246 {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
247 {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
248 {"enable-runtime-pseudo-reloc-v1", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1},
249 {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2},
251 {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
252 {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
253 {"dynamicbase",no_argument, NULL, OPTION_DYNAMIC_BASE},
254 {"forceinteg", no_argument, NULL, OPTION_FORCE_INTEGRITY},
255 {"nxcompat", no_argument, NULL, OPTION_NX_COMPAT},
256 {"no-isolation", no_argument, NULL, OPTION_NO_ISOLATION},
257 {"no-seh", no_argument, NULL, OPTION_NO_SEH},
258 {"no-bind", no_argument, NULL, OPTION_NO_BIND},
259 {"wdmdriver", no_argument, NULL, OPTION_WDM_DRIVER},
260 {"tsaware", no_argument, NULL, OPTION_TERMINAL_SERVER_AWARE},
261 {NULL, no_argument, NULL, 0}
265 = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
266 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
269 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
270 parameters which may be input from the command line. */
281 #define D(field,symbol,def) {&pep.field,sizeof(pep.field), def, symbol,0}
283 static definfo init[] =
285 /* imagebase must be first */
286 #define IMAGEBASEOFF 0
287 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
289 {&dll, sizeof(dll), 0, "__dll__", 0},
290 #define MSIMAGEBASEOFF 2
291 D(ImageBase, U("__ImageBase"), NT_EXE_IMAGE_BASE),
292 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
293 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
294 D(MajorOperatingSystemVersion,"__major_os_version__", 4),
295 D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
296 D(MajorImageVersion,"__major_image_version__", 0),
297 D(MinorImageVersion,"__minor_image_version__", 0),
298 D(MajorSubsystemVersion,"__major_subsystem_version__", 5),
299 D(MinorSubsystemVersion,"__minor_subsystem_version__", 2),
300 D(Subsystem,"__subsystem__", ${SUBSYSTEM}),
301 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000),
302 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
303 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
304 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
305 D(LoaderFlags,"__loader_flags__", 0x0),
306 D(DllCharacteristics, "__dll_characteristics__", 0x0),
307 { NULL, 0, 0, NULL, 0 }
312 gld_${EMULATION_NAME}_list_options (FILE *file)
314 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
315 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
316 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
317 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
318 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
319 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
320 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
321 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
322 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
323 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
324 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
325 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
326 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
327 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
328 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
330 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
331 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
332 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
333 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
334 fprintf (file, _(" --exclude-all-symbols Exclude all symbols from automatic export\n"));
335 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
336 fprintf (file, _(" --exclude-modules-for-implib mod,mod,...\n"));
337 fprintf (file, _(" Exclude objects, archive members from auto\n"));
338 fprintf (file, _(" export, place into import library instead.\n"));
339 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
340 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
341 fprintf (file, _(" --out-implib <file> Generate import library\n"));
342 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
343 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
344 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
345 create __imp_<SYMBOL> as well.\n"));
346 fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n\
347 unless user specifies one\n"));
348 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
349 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
350 an importlib, use <string><basename>.dll\n\
351 in preference to lib<basename>.dll \n"));
352 fprintf (file, _(" --enable-auto-import Do sophistcated linking of _sym to\n\
353 __imp_sym for DATA references\n"));
354 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
355 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
356 adding pseudo-relocations resolved at\n\
358 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
359 auto-imported DATA.\n"));
360 fprintf (file, _(" --enable-extra-pep-debug Enable verbose debug output when building\n\
361 or linking to DLLs (esp. auto-import)\n"));
362 fprintf (file, _(" --enable-long-section-names Use long COFF section names even in\n\
363 executable image files\n"));
364 fprintf (file, _(" --disable-long-section-names Never use long COFF section names, even\n\
365 in object files\n"));
366 fprintf (file, _(" --dynamicbase Image base address may be relocated using\n\
367 address space layout randomization (ASLR)\n"));
368 fprintf (file, _(" --forceinteg Code integrity checks are enforced\n"));
369 fprintf (file, _(" --nxcompat Image is compatible with data execution prevention\n"));
370 fprintf (file, _(" --no-isolation Image understands isolation but do not isolate the image\n"));
371 fprintf (file, _(" --no-seh Image does not use SEH. No SE handler may\n\
372 be called in this image\n"));
373 fprintf (file, _(" --no-bind Do not bind this image\n"));
374 fprintf (file, _(" --wdmdriver Driver uses the WDM model\n"));
375 fprintf (file, _(" --tsaware Image is Terminal Server aware\n"));
381 set_pep_name (char *name, bfd_vma val)
385 /* Find the name and set it. */
386 for (i = 0; init[i].ptr; i++)
388 if (strcmp (name, init[i].symbol) == 0)
392 if (strcmp (name,"__image_base__") == 0)
393 set_pep_name ("___ImageBase", val);
401 set_entry_point (void)
404 const char *initial_symbol_char;
414 { 1, "NtProcessStartup" },
415 { 2, "WinMainCRTStartup" },
416 { 3, "mainCRTStartup" },
417 { 7, "__PosixProcessStartup" },
418 { 9, "WinMainCRTStartup" },
419 {14, "mainCRTStartup" },
423 /* Entry point name for arbitrary subsystem numbers. */
424 static const char default_entry[] = "mainCRTStartup";
426 if (link_info.shared || dll)
428 entry = "DllMainCRTStartup";
432 for (i = 0; v[i].entry; i++)
433 if (v[i].value == pep_subsystem)
436 /* If no match, use the default. */
437 if (v[i].entry != NULL)
440 entry = default_entry;
443 initial_symbol_char = ${INITIAL_SYMBOL_CHAR};
444 if (*initial_symbol_char != '\0')
448 /* lang_default_entry expects its argument to be permanently
449 allocated, so we don't free this string. */
450 alc_entry = xmalloc (strlen (initial_symbol_char)
453 strcpy (alc_entry, initial_symbol_char);
454 strcat (alc_entry, entry);
458 lang_default_entry (entry);
462 set_pep_subsystem (void)
468 unsigned long temp_subsystem;
485 /* Check for the presence of a version number. */
486 sver = strchr (optarg, ':');
488 len = strlen (optarg);
492 set_pep_name ("__major_subsystem_version__",
493 strtoul (sver + 1, &end, 0));
495 set_pep_name ("__minor_subsystem_version__",
496 strtoul (end + 1, &end, 0));
498 einfo (_("%P: warning: bad version number in -subsystem option\n"));
501 /* Check for numeric subsystem. */
502 temp_subsystem = strtoul (optarg, & end, 0);
503 if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
505 /* Search list for a numeric match to use its entry point. */
506 for (i = 0; v[i].name; i++)
507 if (v[i].value == (int) temp_subsystem)
510 /* Use this subsystem. */
511 pep_subsystem = (int) temp_subsystem;
515 /* Search for subsystem by name. */
516 for (i = 0; v[i].name; i++)
517 if (strncmp (optarg, v[i].name, len) == 0
518 && v[i].name[len] == '\0')
521 if (v[i].name == NULL)
523 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
527 pep_subsystem = v[i].value;
530 set_pep_name ("__subsystem__", pep_subsystem);
537 set_pep_value (char *name)
541 set_pep_name (name, (bfd_vma) strtoull (optarg, &end, 0));
544 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
551 set_pep_stack_heap (char *resname, char *comname)
553 set_pep_value (resname);
558 set_pep_value (comname);
561 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
566 gld${EMULATION_NAME}_handle_option (int optc)
573 case OPTION_BASE_FILE:
574 link_info.base_file = fopen (optarg, FOPEN_WB);
575 if (link_info.base_file == NULL)
577 /* xgettext:c-format */
578 fprintf (stderr, _("%s: Can't open base file %s\n"),
579 program_name, optarg);
586 set_pep_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
589 set_pep_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
591 case OPTION_SUBSYSTEM:
592 set_pep_subsystem ();
594 case OPTION_MAJOR_OS_VERSION:
595 set_pep_value ("__major_os_version__");
597 case OPTION_MINOR_OS_VERSION:
598 set_pep_value ("__minor_os_version__");
600 case OPTION_MAJOR_SUBSYSTEM_VERSION:
601 set_pep_value ("__major_subsystem_version__");
603 case OPTION_MINOR_SUBSYSTEM_VERSION:
604 set_pep_value ("__minor_subsystem_version__");
606 case OPTION_MAJOR_IMAGE_VERSION:
607 set_pep_value ("__major_image_version__");
609 case OPTION_MINOR_IMAGE_VERSION:
610 set_pep_value ("__minor_image_version__");
612 case OPTION_FILE_ALIGNMENT:
613 set_pep_value ("__file_alignment__");
615 case OPTION_SECTION_ALIGNMENT:
616 set_pep_value ("__section_alignment__");
619 set_pep_name ("__dll__", 1);
621 case OPTION_IMAGE_BASE:
622 set_pep_value ("__image_base__");
624 case OPTION_SUPPORT_OLD_CODE:
625 support_old_code = 1;
627 case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
628 pep_use_nul_prefixed_import_tables = TRUE;
632 pep_out_def_filename = xstrdup (optarg);
634 case OPTION_EXPORT_ALL:
635 pep_dll_export_everything = 1;
637 case OPTION_EXCLUDE_SYMBOLS:
638 pep_dll_add_excludes (optarg, EXCLUDESYMS);
640 case OPTION_EXCLUDE_ALL_SYMBOLS:
641 pep_dll_exclude_all_symbols = 1;
643 case OPTION_EXCLUDE_LIBS:
644 pep_dll_add_excludes (optarg, EXCLUDELIBS);
646 case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
647 pep_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
649 case OPTION_KILL_ATS:
650 pep_dll_kill_ats = 1;
652 case OPTION_STDCALL_ALIASES:
653 pep_dll_stdcall_aliases = 1;
655 case OPTION_ENABLE_STDCALL_FIXUP:
656 pep_enable_stdcall_fixup = 1;
658 case OPTION_DISABLE_STDCALL_FIXUP:
659 pep_enable_stdcall_fixup = 0;
661 case OPTION_IMPLIB_FILENAME:
662 pep_implib_filename = xstrdup (optarg);
664 case OPTION_WARN_DUPLICATE_EXPORTS:
665 pep_dll_warn_dup_exports = 1;
667 case OPTION_IMP_COMPAT:
668 pep_dll_compat_implib = 1;
670 case OPTION_ENABLE_AUTO_IMAGE_BASE:
671 pep_enable_auto_image_base = 1;
673 case OPTION_DISABLE_AUTO_IMAGE_BASE:
674 pep_enable_auto_image_base = 0;
676 case OPTION_DLL_SEARCH_PREFIX:
677 pep_dll_search_prefix = xstrdup (optarg);
679 case OPTION_NO_DEFAULT_EXCLUDES:
680 pep_dll_do_default_excludes = 0;
682 case OPTION_DLL_ENABLE_AUTO_IMPORT:
683 link_info.pei386_auto_import = 1;
685 case OPTION_DLL_DISABLE_AUTO_IMPORT:
686 link_info.pei386_auto_import = 0;
688 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
689 link_info.pei386_runtime_pseudo_reloc = 2;
691 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
692 link_info.pei386_runtime_pseudo_reloc = 0;
694 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1:
695 link_info.pei386_runtime_pseudo_reloc = 1;
697 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2:
698 link_info.pei386_runtime_pseudo_reloc = 2;
700 case OPTION_ENABLE_EXTRA_PE_DEBUG:
701 pep_dll_extra_pe_debug = 1;
704 case OPTION_ENABLE_LONG_SECTION_NAMES:
705 pep_use_coff_long_section_names = 1;
707 case OPTION_DISABLE_LONG_SECTION_NAMES:
708 pep_use_coff_long_section_names = 0;
710 /* Get DLLCharacteristics bits */
711 case OPTION_DYNAMIC_BASE:
712 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
714 case OPTION_FORCE_INTEGRITY:
715 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
717 case OPTION_NX_COMPAT:
718 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
720 case OPTION_NO_ISOLATION:
721 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
724 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_SEH;
727 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_BIND;
729 case OPTION_WDM_DRIVER:
730 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
732 case OPTION_TERMINAL_SERVER_AWARE:
733 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
737 /* Set DLLCharacteristics bits */
738 set_pep_name ("__dll_characteristics__", pe_dll_characteristics);
746 strhash (const char *str)
748 const unsigned char *s;
755 s = (const unsigned char *) str;
756 while ((c = *s++) != '\0')
758 hash += c + (c << 17);
762 hash += len + (len << 17);
768 /* Use the output file to create a image base for relocatable DLLs. */
771 compute_dll_image_base (const char *ofile)
773 bfd_vma hash = (bfd_vma) strhash (ofile);
774 return 0x61300000 + ((hash << 16) & 0x0FFC0000);
778 /* Assign values to the special symbols before the linker script is
782 gld_${EMULATION_NAME}_set_symbols (void)
784 /* Run through and invent symbols for all the
785 names and insert the defaults. */
788 if (!init[IMAGEBASEOFF].inited)
790 if (link_info.relocatable)
791 init[IMAGEBASEOFF].value = 0;
792 else if (init[DLLOFF].value || (link_info.shared && !link_info.pie))
795 init[IMAGEBASEOFF].value = (pep_enable_auto_image_base
796 ? compute_dll_image_base (output_filename)
797 : NT_DLL_IMAGE_BASE);
799 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
803 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
804 init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
807 /* Don't do any symbol assignments if this is a relocatable link. */
808 if (link_info.relocatable)
811 /* Glue the assignments into the abs section. */
812 push_stat_ptr (&abs_output_section->children);
814 for (j = 0; init[j].ptr; j++)
816 bfd_vma val = init[j].value;
817 lang_assignment_statement_type *rv;
818 rv = lang_add_assignment (exp_assop ('=', init[j].symbol,
820 if (init[j].size == sizeof (short))
821 *(short *) init[j].ptr = (short) val;
822 else if (init[j].size == sizeof (int))
823 *(int *) init[j].ptr = (int) val;
824 else if (init[j].size == sizeof (long))
825 *(long *) init[j].ptr = (long) val;
826 /* This might be a long long or other special type. */
827 else if (init[j].size == sizeof (bfd_vma))
828 *(bfd_vma *) init[j].ptr = val;
830 if (j == IMAGEBASEOFF)
831 image_base_statement = rv;
833 /* Restore the pointer. */
836 if (pep.FileAlignment > pep.SectionAlignment)
838 einfo (_("%P: warning, file alignment > section alignment.\n"));
842 /* This is called after the linker script and the command line options
846 gld_${EMULATION_NAME}_after_parse (void)
848 /* PR ld/6744: Warn the user if they have used an ELF-only
849 option hoping it will work on PE+. */
850 if (link_info.export_dynamic)
851 einfo (_("%P: warning: --export-dynamic is not supported for PE+ "
852 "targets, did you mean --export-all-symbols?\n"));
856 after_parse_default ();
859 /* pep-dll.c directly accesses pep_data_import_dll,
860 so it must be defined outside of #ifdef DLL_SUPPORT.
861 Note - this variable is deliberately not initialised.
862 This allows it to be treated as a common varaible, and only
863 exist in one incarnation in a multiple target enabled linker. */
864 char * pep_data_import_dll;
867 static struct bfd_link_hash_entry *pep_undef_found_sym;
870 pep_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
875 sl = strlen (string);
876 if (h->type == bfd_link_hash_defined
877 && strncmp (h->root.string, string, sl) == 0
878 && h->root.string[sl] == '@')
880 pep_undef_found_sym = h;
887 pep_fixup_stdcalls (void)
889 static int gave_warning_message = 0;
890 struct bfd_link_hash_entry *undef, *sym;
892 if (pep_dll_extra_pe_debug)
893 printf ("%s\n", __FUNCTION__);
895 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
896 if (undef->type == bfd_link_hash_undefined)
898 char* at = strchr (undef->root.string, '@');
899 int lead_at = (*undef->root.string == '@');
900 /* For now, don't try to fixup fastcall symbols. */
904 /* The symbol is a stdcall symbol, so let's look for a
905 cdecl symbol with the same name and resolve to that. */
906 char *cname = xstrdup (undef->root.string /* + lead_at */);
907 at = strchr (cname, '@');
909 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
911 if (sym && sym->type == bfd_link_hash_defined)
913 undef->type = bfd_link_hash_defined;
914 undef->u.def.value = sym->u.def.value;
915 undef->u.def.section = sym->u.def.section;
917 if (pep_enable_stdcall_fixup == -1)
919 einfo (_("Warning: resolving %s by linking to %s\n"),
920 undef->root.string, cname);
921 if (! gave_warning_message)
923 gave_warning_message = 1;
924 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
925 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
932 /* The symbol is a cdecl symbol, so we look for stdcall
933 symbols - which means scanning the whole symbol table. */
934 pep_undef_found_sym = 0;
935 bfd_link_hash_traverse (link_info.hash, pep_undef_cdecl_match,
936 (char *) undef->root.string);
937 sym = pep_undef_found_sym;
940 undef->type = bfd_link_hash_defined;
941 undef->u.def.value = sym->u.def.value;
942 undef->u.def.section = sym->u.def.section;
944 if (pep_enable_stdcall_fixup == -1)
946 einfo (_("Warning: resolving %s by linking to %s\n"),
947 undef->root.string, sym->root.string);
948 if (! gave_warning_message)
950 gave_warning_message = 1;
951 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
952 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
961 make_import_fixup (arelent *rel, asection *s)
963 struct bfd_symbol *sym = *rel->sym_ptr_ptr;
968 if (pep_dll_extra_pe_debug)
969 printf ("arelent: %s@%#lx: add=%li\n", sym->name,
970 (unsigned long) rel->address, (long) rel->addend);
972 memset (addend, 0, sizeof (addend));
973 switch ((rel->howto->bitsize))
976 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 1);
977 if (suc && rel->howto->pc_relative)
978 _addend = (bfd_vma) ((bfd_signed_vma) ((char) bfd_get_8 (s->owner, addend)));
980 _addend = ((bfd_vma) bfd_get_8 (s->owner, addend)) & 0xff;
983 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 2);
984 if (suc && rel->howto->pc_relative)
985 _addend = (bfd_vma) ((bfd_signed_vma) ((short) bfd_get_16 (s->owner, addend)));
987 _addend = ((bfd_vma) bfd_get_16 (s->owner, addend)) & 0xffff;
990 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 4);
991 if (suc && rel->howto->pc_relative)
992 _addend = (bfd_vma) ((bfd_signed_vma) ((int) bfd_get_32 (s->owner, addend)));
994 _addend = ((bfd_vma) bfd_get_32 (s->owner, addend)) & 0xffffffff;
997 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 8);
999 _addend = ((bfd_vma) bfd_get_64 (s->owner, addend));
1003 einfo (_("%C: Cannot get section contents - auto-import exception\n"),
1004 s->owner, s, rel->address);
1006 if (pep_dll_extra_pe_debug)
1008 printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx", (long) _addend, (long) rel->addend, (long) rel->address);
1009 if (rel->howto->pc_relative) printf (" pcrel");
1010 printf (" %d bit rel.\n",(int) rel->howto->bitsize);
1012 pep_create_import_fixup (rel, s, _addend);
1018 pep_find_data_imports (void)
1020 struct bfd_link_hash_entry *undef, *sym;
1022 if (link_info.pei386_auto_import == 0)
1025 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
1027 if (undef->type == bfd_link_hash_undefined)
1029 /* C++ symbols are *long*. */
1032 if (pep_dll_extra_pe_debug)
1033 printf ("%s:%s\n", __FUNCTION__, undef->root.string);
1035 sprintf (buf, "__imp_%s", undef->root.string);
1037 sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
1039 if (sym && sym->type == bfd_link_hash_defined)
1041 bfd *b = sym->u.def.section->owner;
1045 if (link_info.pei386_auto_import == -1)
1047 static bfd_boolean warned = FALSE;
1049 info_msg (_("Info: resolving %s by linking to %s (auto-import)\n"),
1050 undef->root.string, buf);
1052 /* PR linker/4844. */
1056 einfo (_("%P: warning: auto-importing has been activated without --enable-auto-import specified on the command line.\n\
1057 This should work unless it involves constant data structures referencing symbols from auto-imported DLLs.\n"));
1061 if (!bfd_generic_link_read_symbols (b))
1063 einfo (_("%B%F: could not read symbols: %E\n"), b);
1067 symbols = bfd_get_outsymbols (b);
1068 nsyms = bfd_get_symcount (b);
1070 for (i = 0; i < nsyms; i++)
1072 if (! CONST_STRNEQ (symbols[i]->name, "__head_"))
1075 if (pep_dll_extra_pe_debug)
1076 printf ("->%s\n", symbols[i]->name);
1078 pep_data_import_dll = (char*) (symbols[i]->name +
1079 sizeof ("__head_") - 1);
1083 pep_walk_relocs_of_symbol (&link_info, undef->root.string,
1086 /* Let's differentiate it somehow from defined. */
1087 undef->type = bfd_link_hash_defweak;
1088 /* We replace original name with __imp_ prefixed, this
1089 1) may trash memory 2) leads to duplicate symbol generation.
1090 Still, IMHO it's better than having name poluted. */
1091 undef->root.string = sym->root.string;
1092 undef->u.def.value = sym->u.def.value;
1093 undef->u.def.section = sym->u.def.section;
1100 pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
1102 if (pep_dll_extra_pe_debug)
1103 printf ("+%s\n", h->string);
1107 #endif /* DLL_SUPPORT */
1110 debug_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
1112 int *found = (int *) obj;
1113 if (strncmp (".debug_", sect->name, sizeof (".debug_") - 1) == 0)
1118 gld_${EMULATION_NAME}_after_open (void)
1121 if (pep_dll_extra_pe_debug)
1124 struct bfd_link_hash_entry *sym;
1126 printf ("%s()\n", __FUNCTION__);
1128 for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1129 printf ("-%s\n", sym->root.string);
1130 bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
1132 for (a = link_info.input_bfds; a; a = a->link_next)
1133 printf ("*%s\n",a->filename);
1137 /* Pass the wacky PE command line options into the output bfd.
1138 FIXME: This should be done via a function, rather than by
1139 including an internal BFD header. */
1141 if (coff_data (link_info.output_bfd) == NULL
1142 || coff_data (link_info.output_bfd)->pe == 0)
1143 einfo (_("%F%P: cannot perform PE operations on non PE output file '%B'.\n"),
1144 link_info.output_bfd);
1146 pe_data (link_info.output_bfd)->pe_opthdr = pep;
1147 pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
1148 pe_data (link_info.output_bfd)->real_flags |= real_flags;
1150 /* At this point we must decide whether to use long section names
1151 in the output or not. If the user hasn't explicitly specified
1152 on the command line, we leave it to the default for the format
1153 (object files yes, image files no), except if there is debug
1154 information present; GDB relies on the long section names to
1155 find it, so enable it in that case. */
1156 if (pep_use_coff_long_section_names < 0 && link_info.strip == strip_none)
1158 /* Iterate over all sections of all input BFDs, checking
1159 for any that begin 'debug_' and are long names. */
1160 LANG_FOR_EACH_INPUT_STATEMENT (is)
1162 int found_debug = 0;
1163 bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug);
1166 pep_use_coff_long_section_names = 1;
1172 pep_output_file_set_long_section_names (link_info.output_bfd);
1175 if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
1176 pep_fixup_stdcalls ();
1178 pep_process_import_defs (link_info.output_bfd, &link_info);
1180 pep_find_data_imports ();
1182 #ifndef TARGET_IS_i386pep
1183 if (link_info.shared)
1185 if (!link_info.relocatable)
1187 pep_dll_build_sections (link_info.output_bfd, &link_info);
1189 #ifndef TARGET_IS_i386pep
1191 pep_exe_build_sections (link_info.output_bfd, &link_info);
1193 #endif /* DLL_SUPPORT */
1196 /* This next chunk of code tries to detect the case where you have
1197 two import libraries for the same DLL (specifically,
1198 symbolically linking libm.a and libc.a in cygwin to
1199 libcygwin.a). In those cases, it's possible for function
1200 thunks from the second implib to be used but without the
1201 head/tail objects, causing an improper import table. We detect
1202 those cases and rename the "other" import libraries to match
1203 the one the head/tail come from, so that the linker will sort
1204 things nicely and produce a valid import table. */
1206 LANG_FOR_EACH_INPUT_STATEMENT (is)
1208 if (is->the_bfd->my_archive)
1210 int idata2 = 0, reloc_count=0, is_imp = 0;
1213 /* See if this is an import library thunk. */
1214 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1216 if (strcmp (sec->name, ".idata\$2") == 0)
1218 if (CONST_STRNEQ (sec->name, ".idata\$"))
1220 reloc_count += sec->reloc_count;
1223 if (is_imp && !idata2 && reloc_count)
1225 /* It is, look for the reference to head and see if it's
1226 from our own library. */
1227 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1235 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1239 if (!bfd_generic_link_read_symbols (is->the_bfd))
1241 einfo (_("%B%F: could not read symbols: %E\n"),
1245 symbols = bfd_get_outsymbols (is->the_bfd);
1247 relocs = xmalloc ((size_t) relsize);
1248 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1253 einfo ("%X%P: unable to process relocs: %E\n");
1257 for (i = 0; i < nrelocs; i++)
1259 struct bfd_symbol *s;
1260 struct bfd_link_hash_entry * blhe;
1261 char *other_bfd_filename;
1264 s = (relocs[i]->sym_ptr_ptr)[0];
1266 if (s->flags & BSF_LOCAL)
1269 /* Thunk section with reloc to another bfd. */
1270 blhe = bfd_link_hash_lookup (link_info.hash,
1272 FALSE, FALSE, TRUE);
1275 || blhe->type != bfd_link_hash_defined)
1279 = blhe->u.def.section->owner->my_archive
1280 ? bfd_get_filename (blhe->u.def.section->owner->my_archive)
1281 : bfd_get_filename (blhe->u.def.section->owner);
1283 if (strcmp (bfd_get_filename (is->the_bfd->my_archive),
1284 other_bfd_filename) == 0)
1287 /* Rename this implib to match the other one. */
1288 n = xmalloc (strlen (other_bfd_filename) + 1);
1289 strcpy (n, other_bfd_filename);
1290 is->the_bfd->my_archive->filename = n;
1294 /* Note - we do not free the symbols,
1295 they are now cached in the BFD. */
1305 lang_input_statement_type *is2;
1306 lang_input_statement_type *is3;
1308 /* Careful - this is a shell script. Watch those dollar signs! */
1309 /* Microsoft import libraries have every member named the same,
1310 and not in the right order for us to link them correctly. We
1311 must detect these and rename the members so that they'll link
1312 correctly. There are three types of objects: the head, the
1313 thunks, and the sentinel(s). The head is easy; it's the one
1314 with idata2. We assume that the sentinels won't have relocs,
1315 and the thunks will. It's easier than checking the symbol
1316 table for external references. */
1317 LANG_FOR_EACH_INPUT_STATEMENT (is)
1319 if (is->the_bfd->my_archive)
1322 bfd *arch = is->the_bfd->my_archive;
1324 if (cur_arch != arch)
1330 is3 && is3->the_bfd->my_archive == arch;
1331 is3 = (lang_input_statement_type *) is3->next)
1333 /* A MS dynamic import library can also contain static
1334 members, so look for the first element with a .dll
1335 extension, and use that for the remainder of the
1337 pnt = strrchr (is3->the_bfd->filename, '.');
1338 if (pnt != NULL && strcmp (pnt, ".dll") == 0)
1346 /* OK, found one. Now look to see if the remaining
1347 (dynamic import) members use the same name. */
1349 is2 && is2->the_bfd->my_archive == arch;
1350 is2 = (lang_input_statement_type *) is2->next)
1352 /* Skip static members, ie anything with a .obj
1354 pnt = strrchr (is2->the_bfd->filename, '.');
1355 if (pnt != NULL && strcmp (pnt, ".obj") == 0)
1358 if (strcmp (is3->the_bfd->filename,
1359 is2->the_bfd->filename))
1368 /* This fragment might have come from an .obj file in a Microsoft
1369 import, and not an actual import record. If this is the case,
1370 then leave the filename alone. */
1371 pnt = strrchr (is->the_bfd->filename, '.');
1373 if (is_ms_arch && (strcmp (pnt, ".dll") == 0))
1375 int idata2 = 0, reloc_count=0;
1377 char *new_name, seq;
1379 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1381 if (strcmp (sec->name, ".idata\$2") == 0)
1383 reloc_count += sec->reloc_count;
1386 if (idata2) /* .idata2 is the TOC */
1388 else if (reloc_count > 0) /* thunks */
1393 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
1394 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
1395 is->the_bfd->filename = new_name;
1397 new_name = xmalloc (strlen (is->filename) + 3);
1398 sprintf (new_name, "%s.%c", is->filename, seq);
1399 is->filename = new_name;
1407 gld_${EMULATION_NAME}_before_allocation (void)
1409 before_allocation_default ();
1413 /* This is called when an input file isn't recognized as a BFD. We
1414 check here for .DEF files and pull them in automatically. */
1417 saw_option (char *option)
1421 for (i = 0; init[i].ptr; i++)
1422 if (strcmp (init[i].symbol, option) == 0)
1423 return init[i].inited;
1426 #endif /* DLL_SUPPORT */
1429 gld_${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1432 const char *ext = entry->filename + strlen (entry->filename) - 4;
1434 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
1436 pep_def_file = def_file_parse (entry->filename, pep_def_file);
1440 int i, buflen=0, len;
1443 for (i = 0; i < pep_def_file->num_exports; i++)
1445 len = strlen (pep_def_file->exports[i].internal_name);
1446 if (buflen < len + 2)
1450 buf = xmalloc (buflen);
1452 for (i = 0; i < pep_def_file->num_exports; i++)
1454 struct bfd_link_hash_entry *h;
1456 sprintf (buf, "_%s", pep_def_file->exports[i].internal_name);
1458 h = bfd_link_hash_lookup (link_info.hash, buf, TRUE, TRUE, TRUE);
1459 if (h == (struct bfd_link_hash_entry *) NULL)
1460 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1461 if (h->type == bfd_link_hash_new)
1463 h->type = bfd_link_hash_undefined;
1464 h->u.undef.abfd = NULL;
1465 bfd_link_add_undef (link_info.hash, h);
1470 /* def_file_print (stdout, pep_def_file); */
1471 if (pep_def_file->is_dll == 1)
1472 link_info.shared = 1;
1474 if (pep_def_file->base_address != (bfd_vma)(-1))
1477 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1478 = init[IMAGEBASEOFF].value
1479 = pep_def_file->base_address;
1480 init[IMAGEBASEOFF].inited = 1;
1481 if (image_base_statement)
1482 image_base_statement->exp = exp_assop ('=', "__image_base__",
1483 exp_intop (pep.ImageBase));
1486 if (pep_def_file->stack_reserve != -1
1487 && ! saw_option ("__size_of_stack_reserve__"))
1489 pep.SizeOfStackReserve = pep_def_file->stack_reserve;
1490 if (pep_def_file->stack_commit != -1)
1491 pep.SizeOfStackCommit = pep_def_file->stack_commit;
1493 if (pep_def_file->heap_reserve != -1
1494 && ! saw_option ("__size_of_heap_reserve__"))
1496 pep.SizeOfHeapReserve = pep_def_file->heap_reserve;
1497 if (pep_def_file->heap_commit != -1)
1498 pep.SizeOfHeapCommit = pep_def_file->heap_commit;
1508 gld_${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1511 #ifdef TARGET_IS_i386pep
1512 pep_dll_id_target ("pei-x86-64");
1514 if (pep_bfd_is_dll (entry->the_bfd))
1515 return pep_implied_import_dll (entry->filename);
1521 gld_${EMULATION_NAME}_finish (void)
1526 if (link_info.shared
1527 || (!link_info.relocatable && pep_def_file->num_exports != 0))
1529 pep_dll_fill_sections (link_info.output_bfd, &link_info);
1530 if (pep_implib_filename)
1531 pep_dll_generate_implib (pep_def_file, pep_implib_filename, &link_info);
1534 if (pep_out_def_filename)
1535 pep_dll_generate_def_file (pep_out_def_filename);
1536 #endif /* DLL_SUPPORT */
1538 /* I don't know where .idata gets set as code, but it shouldn't be. */
1540 asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
1544 asec->flags &= ~SEC_CODE;
1545 asec->flags |= SEC_DATA;
1551 /* Place an orphan section.
1553 We use this to put sections in a reasonable place in the file, and
1554 to ensure that they are aligned as required.
1556 We handle grouped sections here as well. A section named .foo\$nn
1557 goes into the output section .foo. All grouped sections are sorted
1560 Grouped sections for the default sections are handled by the
1561 default linker script using wildcards, and are sorted by
1564 static lang_output_section_statement_type *
1565 gld_${EMULATION_NAME}_place_orphan (asection *s,
1566 const char *secname,
1569 const char *orig_secname = secname;
1570 char *dollar = NULL;
1571 lang_output_section_statement_type *os;
1572 lang_statement_list_type add_child;
1573 lang_statement_union_type **pl;
1575 /* Look through the script to see where to place this section. */
1576 if (!link_info.relocatable
1577 && (dollar = strchr (secname, '\$')) != NULL)
1579 size_t len = dollar - secname;
1580 char *newname = xmalloc (len + 1);
1581 memcpy (newname, secname, len);
1582 newname[len] = '\0';
1586 lang_list_init (&add_child);
1589 if (constraint == 0)
1590 for (os = lang_output_section_find (secname);
1592 os = next_matching_output_section_statement (os, 0))
1594 /* If we don't match an existing output section, tell
1595 lang_insert_orphan to create a new output section. */
1596 constraint = SPECIAL;
1598 if (os->bfd_section != NULL
1599 && (os->bfd_section->flags == 0
1600 || ((s->flags ^ os->bfd_section->flags)
1601 & (SEC_LOAD | SEC_ALLOC)) == 0))
1603 /* We already have an output section statement with this
1604 name, and its bfd section has compatible flags.
1605 If the section already exists but does not have any flags set,
1606 then it has been created by the linker, probably as a result of
1607 a --section-start command line switch. */
1608 lang_add_section (&add_child, s, os);
1615 static struct orphan_save hold[] =
1618 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1621 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1624 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1630 enum orphan_save_index
1637 static int orphan_init_done = 0;
1638 struct orphan_save *place;
1639 lang_output_section_statement_type *after;
1640 etree_type *address;
1642 if (!orphan_init_done)
1644 struct orphan_save *ho;
1645 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1646 if (ho->name != NULL)
1648 ho->os = lang_output_section_find (ho->name);
1649 if (ho->os != NULL && ho->os->flags == 0)
1650 ho->os->flags = ho->flags;
1652 orphan_init_done = 1;
1655 /* Try to put the new output section in a reasonable place based
1656 on the section name and section flags. */
1659 if ((s->flags & SEC_ALLOC) == 0)
1661 else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
1662 place = &hold[orphan_bss];
1663 else if ((s->flags & SEC_READONLY) == 0)
1664 place = &hold[orphan_data];
1665 else if ((s->flags & SEC_CODE) == 0)
1666 place = &hold[orphan_rodata];
1668 place = &hold[orphan_text];
1673 if (place->os == NULL)
1674 place->os = lang_output_section_find (place->name);
1677 after = lang_output_section_find_by_flags (s, &place->os, NULL);
1679 /* *ABS* is always the first output section statement. */
1680 after = (&lang_output_section_statement.head
1681 ->output_section_statement);
1684 /* All sections in an executable must be aligned to a page boundary. */
1685 address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
1686 os = lang_insert_orphan (s, secname, constraint, after, place, address,
1690 /* If the section name has a '\$', sort it with the other '\$'
1692 for (pl = &os->children.head; *pl != NULL; pl = &(*pl)->header.next)
1694 lang_input_section_type *ls;
1697 if ((*pl)->header.type != lang_input_section_enum)
1700 ls = &(*pl)->input_section;
1702 lname = bfd_get_section_name (ls->section->owner, ls->section);
1703 if (strchr (lname, '\$') != NULL
1704 && (dollar == NULL || strcmp (orig_secname, lname) < 0))
1708 if (add_child.head != NULL)
1710 *add_child.tail = *pl;
1711 *pl = add_child.head;
1718 gld_${EMULATION_NAME}_open_dynamic_archive
1719 (const char *arch ATTRIBUTE_UNUSED,
1720 search_dirs_type *search,
1721 lang_input_statement_type *entry)
1725 const char * format;
1726 bfd_boolean use_prefix;
1730 /* Preferred explicit import library for dll's. */
1731 { "lib%s.dll.a", FALSE },
1732 /* Alternate explicit import library for dll's. */
1733 { "%s.dll.a", FALSE },
1734 /* "libfoo.a" could be either an import lib or a static lib.
1735 For backwards compatibility, libfoo.a needs to precede
1736 libfoo.dll and foo.dll in the search. */
1737 { "lib%s.a", FALSE },
1738 /* The 'native' spelling of an import lib name is "foo.lib". */
1739 { "%s.lib", FALSE },
1741 /* Try "<prefix>foo.dll" (preferred dll name, if specified). */
1742 { "%s%s.dll", TRUE },
1744 /* Try "libfoo.dll" (default preferred dll name). */
1745 { "lib%s.dll", FALSE },
1746 /* Finally try 'native' dll name "foo.dll". */
1747 { "%s.dll", FALSE },
1748 /* Note: If adding more formats to this table, make sure to check to
1749 see if their length is longer than libname_fmt[0].format, and if
1750 so, update the call to xmalloc() below. */
1753 static unsigned int format_max_len = 0;
1754 const char * filename;
1760 if (! entry->is_archive)
1763 filename = entry->filename;
1765 if (format_max_len == 0)
1766 /* We need to allow space in the memory that we are going to allocate
1767 for the characters in the format string. Since the format array is
1768 static we only need to calculate this information once. In theory
1769 this value could also be computed statically, but this introduces
1770 the possibility for a discrepancy and hence a possible memory
1771 corruption. The lengths we compute here will be too long because
1772 they will include any formating characters (%s) in the strings, but
1773 this will not matter. */
1774 for (i = 0; libname_fmt[i].format; i++)
1775 if (format_max_len < strlen (libname_fmt[i].format))
1776 format_max_len = strlen (libname_fmt[i].format);
1778 full_string = xmalloc (strlen (search->name)
1782 + (pep_dll_search_prefix
1783 ? strlen (pep_dll_search_prefix) : 0)
1785 /* Allow for the terminating NUL and for the path
1786 separator character that is inserted between
1787 search->name and the start of the format string. */
1790 sprintf (full_string, "%s/", search->name);
1791 base_string = full_string + strlen (full_string);
1793 for (i = 0; libname_fmt[i].format; i++)
1796 if (libname_fmt[i].use_prefix)
1798 if (!pep_dll_search_prefix)
1800 sprintf (base_string, libname_fmt[i].format, pep_dll_search_prefix, filename);
1804 sprintf (base_string, libname_fmt[i].format, filename);
1806 if (ldfile_try_open_bfd (full_string, entry))
1810 if (!libname_fmt[i].format)
1816 entry->filename = full_string;
1822 gld_${EMULATION_NAME}_find_potential_libraries
1823 (char *name, lang_input_statement_type *entry)
1825 return ldfile_open_file_search (name, entry, "", ".lib");
1829 gld_${EMULATION_NAME}_get_script (int *isfile)
1831 # Scripts compiled in.
1832 # sed commands to quote an ld script as a C string.
1833 sc="-f stringify.sed"
1839 if (link_info.relocatable && config.build_constructors)
1842 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1843 echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c
1844 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1845 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1846 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1847 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1848 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1849 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
1850 echo ' ; else if (link_info.pei386_auto_import == 1) return' >> e${EMULATION_NAME}.c
1851 sed $sc ldscripts/${EMULATION_NAME}.xa >> e${EMULATION_NAME}.c
1853 echo ' ; else return' >> e${EMULATION_NAME}.c
1854 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1855 echo '; }' >> e${EMULATION_NAME}.c
1860 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1862 gld_${EMULATION_NAME}_before_parse,
1865 gld_${EMULATION_NAME}_after_parse,
1866 gld_${EMULATION_NAME}_after_open,
1867 after_allocation_default,
1868 set_output_arch_default,
1869 ldemul_default_target,
1870 gld_${EMULATION_NAME}_before_allocation,
1871 gld_${EMULATION_NAME}_get_script,
1872 "${EMULATION_NAME}",
1874 gld_${EMULATION_NAME}_finish,
1875 NULL, /* Create output section statements. */
1876 gld_${EMULATION_NAME}_open_dynamic_archive,
1877 gld_${EMULATION_NAME}_place_orphan,
1878 gld_${EMULATION_NAME}_set_symbols,
1879 NULL, /* parse_args */
1880 gld${EMULATION_NAME}_add_options,
1881 gld${EMULATION_NAME}_handle_option,
1882 gld_${EMULATION_NAME}_unrecognized_file,
1883 gld_${EMULATION_NAME}_list_options,
1884 gld_${EMULATION_NAME}_recognized_file,
1885 gld_${EMULATION_NAME}_find_potential_libraries,
1886 NULL /* new_vers_pattern. */