Updated Malay translation for the bfd sub-directory
[binutils-gdb.git] / ld / emultempl / pe.em
blob1a458aa774d9841b3885795877f901bfb98ed721
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 if [ -z "$MACHINE" ]; then
4   OUTPUT_ARCH=${ARCH}
5 else
6   OUTPUT_ARCH=${ARCH}:${MACHINE}
7 fi
9 case ${target} in
10   *-*-cygwin*)
11     cygwin_behavior=1
12     ;;
13   *)
14     cygwin_behavior=0;
15     ;;
16 esac
18 rm -f e${EMULATION_NAME}.c
19 (echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
20 fragment <<EOF
21 /* Copyright (C) 1995-2025 Free Software Foundation, Inc.
23    This file is part of the GNU Binutils.
25    This program is free software; you can redistribute it and/or modify
26    it under the terms of the GNU General Public License as published by
27    the Free Software Foundation; either version 3 of the License, or
28    (at your option) any later version.
30    This program is distributed in the hope that it will be useful,
31    but WITHOUT ANY WARRANTY; without even the implied warranty of
32    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33    GNU General Public License for more details.
35    You should have received a copy of the GNU General Public License
36    along with this program; if not, write to the Free Software
37    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
38    MA 02110-1301, USA.  */
41 /* For WINDOWS_NT */
42 /* The original file generated returned different default scripts depending
43    on whether certain switches were set, but these switches pertain to the
44    Linux system and that particular version of coff.  In the NT case, we
45    only determine if the subsystem is console or windows in order to select
46    the correct entry point by default. */
48 #define TARGET_IS_${EMULATION_NAME}
50 #include "sysdep.h"
51 #include "bfd.h"
52 #include "bfdlink.h"
53 #include "ctf-api.h"
54 #include "getopt.h"
55 #include "libiberty.h"
56 #include "filenames.h"
57 #include "ld.h"
58 #include "ldmain.h"
59 #include "ldexp.h"
60 #include "ldlang.h"
61 #include "ldfile.h"
62 #include "ldemul.h"
63 #include <ldgram.h>
64 #include "ldlex.h"
65 #include "ldmisc.h"
66 #include "ldctor.h"
67 #include "ldbuildid.h"
68 #include "coff/internal.h"
69 EOF
71 case ${target} in
72   x86_64-*-mingw* | x86_64-*-pe | x86_64-*-pep | x86_64-*-cygwin | \
73   i[3-7]86-*-mingw32* | i[3-7]86-*-cygwin* | i[3-7]86-*-winnt | i[3-7]86-*-pe)
74 fragment <<EOF
75 #include "pdb.h"
76 EOF
77     ;;
78 esac
80 fragment <<EOF
82 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
83    header in generic PE code.  */
84 #include "coff/i386.h"
85 #include "coff/pe.h"
87 /* FIXME: These are BFD internal header files, and we should not be
88    using it here.  */
89 #include "../bfd/libcoff.h"
90 #include "../bfd/libpei.h"
92 #if defined(TARGET_IS_armpe) \
93     || defined(TARGET_IS_arm_wince_pe)
94 #define bfd_arm_allocate_interworking_sections \
95         bfd_${EMULATION_NAME}_allocate_interworking_sections
96 #define bfd_arm_get_bfd_for_interworking \
97         bfd_${EMULATION_NAME}_get_bfd_for_interworking
98 #define bfd_arm_process_before_allocation \
99         bfd_${EMULATION_NAME}_process_before_allocation
100 #include "coff-arm.h"
101 #endif
103 #include "deffile.h"
104 #include "pe-dll.h"
105 #include "safe-ctype.h"
107 /* Permit the emulation parameters to override the default section
108    alignment by setting OVERRIDE_SECTION_ALIGNMENT.  FIXME: This makes
109    it seem that include/coff/internal.h should not define
110    PE_DEF_SECTION_ALIGNMENT.  */
111 #if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
112 #undef PE_DEF_SECTION_ALIGNMENT
113 #define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
114 #endif
116 #if defined(TARGET_IS_i386pe) \
117     || defined(TARGET_IS_shpe) \
118     || defined(TARGET_IS_armpe) \
119     || defined(TARGET_IS_arm_wince_pe)
120 #define DLL_SUPPORT
121 #endif
123 #if defined(TARGET_IS_i386pe)
124 #define DEFAULT_PSEUDO_RELOC_VERSION 2
125 #else
126 #define DEFAULT_PSEUDO_RELOC_VERSION 1
127 #endif
129 #define DEFAULT_DLL_CHARACTERISTICS     (${cygwin_behavior} ? 0 : \
130                                            IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE \
131                                          | IMAGE_DLL_CHARACTERISTICS_NX_COMPAT)
133 #if defined(TARGET_IS_i386pe) || ! defined(DLL_SUPPORT)
134 #define PE_DEF_SUBSYSTEM                IMAGE_SUBSYSTEM_WINDOWS_CUI
135 #else
136 #undef NT_EXE_IMAGE_BASE
137 #undef PE_DEF_SECTION_ALIGNMENT
138 #undef PE_DEF_FILE_ALIGNMENT
139 #define NT_EXE_IMAGE_BASE               0x00010000
141 #if defined(TARGET_IS_armpe)
142 #define PE_DEF_SECTION_ALIGNMENT        0x00001000
143 #define PE_DEF_SUBSYSTEM                IMAGE_SUBSYSTEM_WINDOWS_GUI
144 #elif defined(TARGET_IS_arm_wince_pe)
145 #define PE_DEF_SECTION_ALIGNMENT        0x00001000
146 #define PE_DEF_SUBSYSTEM                IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
147 #else
148 #define PE_DEF_SECTION_ALIGNMENT        0x00000400
149 #define PE_DEF_SUBSYSTEM                IMAGE_SUBSYSTEM_WINDOWS_GUI
150 #endif
151 #define PE_DEF_FILE_ALIGNMENT           0x00000200
152 #endif
154 static struct internal_extra_pe_aouthdr pe;
155 static int dll;
156 static int pe_subsystem = ${SUBSYSTEM};
157 static flagword real_flags = 0;
158 static int support_old_code = 0;
159 static char * thumb_entry_symbol = NULL;
160 static lang_assignment_statement_type *image_base_statement = 0;
161 static unsigned short pe_dll_characteristics = DEFAULT_DLL_CHARACTERISTICS;
162 static bool insert_timestamp = true;
163 static const char *emit_build_id;
164 #ifdef PDB_H
165 static int pdb;
166 static char *pdb_name;
167 #endif
169 #ifdef DLL_SUPPORT
170 static int pe_enable_stdcall_fixup = -1; /* 0=disable 1=enable.  */
171 static char *pe_out_def_filename = NULL;
172 static int pe_enable_auto_image_base = 0;
173 static unsigned long pe_auto_image_base = 0x61500000;
174 static char *pe_dll_search_prefix = NULL;
175 #endif
177 extern const char *output_filename;
179 static int
180 is_underscoring (void)
182   int u = 0;
183   if (pe_leading_underscore != -1)
184     return pe_leading_underscore;
185   if (!bfd_get_target_info ("${OUTPUT_FORMAT}", NULL, NULL, &u, NULL))
186     bfd_get_target_info ("${RELOCATEABLE_OUTPUT_FORMAT}", NULL, NULL, &u, NULL);
188   if (u == -1)
189     abort ();
190   pe_leading_underscore = (u != 0 ? 1 : 0);
191   return pe_leading_underscore;
194 static void
195 gld${EMULATION_NAME}_before_parse (void)
197   is_underscoring ();
198   ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
199   output_filename = "${EXECUTABLE_NAME:-a.exe}";
200 #ifdef DLL_SUPPORT
201   input_flags.dynamic = true;
202   config.has_shared = 1;
205 # Cygwin no longer wants these noisy warnings.  Other PE
206 # targets might like to consider adding themselves here.
207 # See also the mail thread starting here for the reason why
208 # merge_rdata defaults to 0 for cygwin:
209 #  http://cygwin.com/ml/cygwin-apps/2013-04/msg00187.html
210 case ${target} in
211   *-*-cygwin*)
212     default_auto_import=1
213     default_merge_rdata=0
214     ;;
215   i[3-7]86-*-mingw* | x86_64-*-mingw*)
216     default_auto_import=1
217     default_merge_rdata=0
218     ;;
219   *)
220     default_auto_import=-1
221     default_merge_rdata=1
222     ;;
223 esac
225 fragment <<EOF
226   link_info.pei386_auto_import = ${default_auto_import};
227   /* Use by default version.  */
228   link_info.pei386_runtime_pseudo_reloc = DEFAULT_PSEUDO_RELOC_VERSION;
229 #endif
232 /* Indicates if RDATA shall be merged into DATA when pseudo-relocation
233    version 2 is used and auto-import is enabled.  */
234 #define MERGE_RDATA_V2 ${default_merge_rdata}
236 /* PE format extra command line options.  */
238 static void
239 gld${EMULATION_NAME}_add_options
240   (int ns ATTRIBUTE_UNUSED,
241    char **shortopts ATTRIBUTE_UNUSED,
242    int nl,
243    struct option **longopts,
244    int nrl ATTRIBUTE_UNUSED,
245    struct option **really_longopts ATTRIBUTE_UNUSED)
247   static const struct option xtra_long[] =
248   {
249     /* PE options.  */
250     {"base-file", required_argument, NULL, OPTION_BASE_FILE},
251     {"dll", no_argument, NULL, OPTION_DLL},
252     {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
253     {"heap", required_argument, NULL, OPTION_HEAP},
254     {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
255     {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
256     {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
257     {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
258     {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
259     {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
260     {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
261     {"stack", required_argument, NULL, OPTION_STACK},
262     {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
263     {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
264     {"thumb-entry", required_argument, NULL, OPTION_THUMB_ENTRY},
265     {"use-nul-prefixed-import-tables", no_argument, NULL,
266      OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
267     {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
268     {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE},
269     {"insert-timestamp", no_argument, NULL, OPTION_INSERT_TIMESTAMP},
270     {"no-insert-timestamp", no_argument, NULL, OPTION_NO_INSERT_TIMESTAMP},
271 #ifdef DLL_SUPPORT
272     /* getopt allows abbreviations, so we do this to stop it
273        from treating -o as an abbreviation for this option.  */
274     {"output-def", required_argument, NULL, OPTION_OUT_DEF},
275     {"output-def", required_argument, NULL, OPTION_OUT_DEF},
276     {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
277     {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
278     {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS},
279     {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
280     {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
281     {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
282     {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
283     {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
284     {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
285     {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
286     /* getopt() allows abbreviations, so we do this to stop it from
287        treating -c as an abbreviation for these --compat-implib.  */
288     {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
289     {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
290     {"enable-auto-image-base", optional_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
291     {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
292     {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
293     {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
294     {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
295     {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
296     {"enable-extra-pe-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
297     {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
298     {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
299     {"enable-runtime-pseudo-reloc-v1", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1},
300     {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2},
301 #endif
302     {"large-address-aware", no_argument, NULL, OPTION_LARGE_ADDRESS_AWARE},
303     {"disable-large-address-aware", no_argument, NULL, OPTION_DISABLE_LARGE_ADDRESS_AWARE},
304     {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
305     {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
306     {"dynamicbase",no_argument, NULL, OPTION_DYNAMIC_BASE},
307     {"disable-dynamicbase",no_argument, NULL, OPTION_DISABLE_DYNAMIC_BASE},
308     {"forceinteg", no_argument, NULL, OPTION_FORCE_INTEGRITY},
309     {"disable-forceinteg", no_argument, NULL, OPTION_DISABLE_FORCE_INTEGRITY},
310     {"nxcompat", no_argument, NULL, OPTION_NX_COMPAT},
311     {"disable-nxcompat", no_argument, NULL, OPTION_DISABLE_NX_COMPAT},
312     {"no-isolation", no_argument, NULL, OPTION_NO_ISOLATION},
313     {"disable-no-isolation", no_argument, NULL, OPTION_DISABLE_NO_ISOLATION},
314     {"no-seh", no_argument, NULL, OPTION_NO_SEH},
315     {"disable-no-seh", no_argument, NULL, OPTION_DISABLE_NO_SEH},
316     {"no-bind", no_argument, NULL, OPTION_NO_BIND},
317     {"disable-no-bind", no_argument, NULL, OPTION_DISABLE_NO_BIND},
318     {"wdmdriver", no_argument, NULL, OPTION_WDM_DRIVER},
319     {"disable-wdmdriver", no_argument, NULL, OPTION_DISABLE_WDM_DRIVER},
320     {"tsaware", no_argument, NULL, OPTION_TERMINAL_SERVER_AWARE},
321     {"disable-tsaware", no_argument, NULL, OPTION_DISABLE_TERMINAL_SERVER_AWARE},
322     {"build-id", optional_argument, NULL, OPTION_BUILD_ID},
323 #ifdef PDB_H
324     {"pdb", required_argument, NULL, OPTION_PDB},
325 #endif
326     {"enable-reloc-section", no_argument, NULL, OPTION_ENABLE_RELOC_SECTION},
327     {"disable-reloc-section", no_argument, NULL, OPTION_DISABLE_RELOC_SECTION},
328     {NULL, no_argument, NULL, 0}
329   };
331   *longopts
332     = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
333   memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
336 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
337    parameters which may be input from the command line.  */
339 typedef struct
341   void *ptr;
342   int size;
343   int value;
344   char *symbol;
345   int inited;
346   /* FALSE for an assembly level symbol and TRUE for a C visible symbol.
347      C visible symbols can be prefixed by underscore dependent to target's
348      settings.  */
349   bool is_c_symbol;
350 } definfo;
352 /* Get symbol name dependent to kind and C visible state of
353    underscore.  */
354 #define GET_INIT_SYMBOL_NAME(IDX) \
355   (init[(IDX)].symbol \
356    + ((!init[(IDX)].is_c_symbol || is_underscoring () != 0) ? 0 : 1))
358 /* Decorates the C visible symbol by underscore, if target requires.  */
359 #define U(CSTR) \
360   ((is_underscoring () == 0) ? CSTR : "_" CSTR)
362 #define D(field,symbol,def,usc)  {&pe.field, sizeof (pe.field), def, symbol, 0, usc}
364 static definfo init[] =
366   /* imagebase must be first */
367 #define IMAGEBASEOFF 0
368   D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE, false),
369 #define DLLOFF 1
370   {&dll, sizeof(dll), 0, "__dll__", 0, false},
371 #define MSIMAGEBASEOFF  2
372   D(ImageBase, "___ImageBase", NT_EXE_IMAGE_BASE, true),
373   D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT, false),
374   D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT, false),
375   D(MajorOperatingSystemVersion,"__major_os_version__", 4, false),
376   D(MinorOperatingSystemVersion,"__minor_os_version__", 0, false),
377   D(MajorImageVersion,"__major_image_version__", 1, false),
378   D(MinorImageVersion,"__minor_image_version__", 0, false),
379 #if defined(TARGET_IS_armpe)  || defined(TARGET_IS_arm_wince_pe)
380   D(MajorSubsystemVersion,"__major_subsystem_version__", 3, false),
381 #else
382   D(MajorSubsystemVersion,"__major_subsystem_version__", 4, false),
383 #endif
384   D(MinorSubsystemVersion,"__minor_subsystem_version__", 0, false),
385   D(Subsystem,"__subsystem__", ${SUBSYSTEM}, false),
386   D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000, false),
387   D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000, false),
388   D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000, false),
389   D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000, false),
390   D(LoaderFlags,"__loader_flags__", 0x0, false),
391   D(DllCharacteristics, "__dll_characteristics__", DEFAULT_DLL_CHARACTERISTICS, false),
392   { NULL, 0, 0, NULL, 0 , false}
396 static void
397 gld${EMULATION_NAME}_list_options (FILE *file)
399   fprintf (file, _("  --base_file <basefile>             Generate a base file for relocatable DLLs\n"));
400   fprintf (file, _("  --dll                              Set image base to the default for DLLs\n"));
401   fprintf (file, _("  --file-alignment <size>            Set file alignment\n"));
402   fprintf (file, _("  --heap <size>                      Set initial size of the heap\n"));
403   fprintf (file, _("  --image-base <address>             Set start address of the executable\n"));
404   fprintf (file, _("  --major-image-version <number>     Set version number of the executable\n"));
405   fprintf (file, _("  --major-os-version <number>        Set minimum required OS version\n"));
406   fprintf (file, _("  --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
407   fprintf (file, _("  --minor-image-version <number>     Set revision number of the executable\n"));
408   fprintf (file, _("  --minor-os-version <number>        Set minimum required OS revision\n"));
409   fprintf (file, _("  --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
410   fprintf (file, _("  --section-alignment <size>         Set section alignment\n"));
411   fprintf (file, _("  --stack <size>                     Set size of the initial stack\n"));
412   fprintf (file, _("  --subsystem <name>[:<version>]     Set required OS subsystem [& version]\n"));
413   fprintf (file, _("  --support-old-code                 Support interworking with old code\n"));
414   fprintf (file, _("  --[no-]leading-underscore          Set explicit symbol underscore prefix mode\n"));
415   fprintf (file, _("  --thumb-entry=<symbol>             Set the entry point to be Thumb <symbol>\n"));
416   fprintf (file, _("  --[no-]insert-timestamp            Use a real timestamp rather than zero (default).\n"));
417   fprintf (file, _("                                     This makes binaries non-deterministic\n"));
418 #ifdef DLL_SUPPORT
419   fprintf (file, _("  --add-stdcall-alias                Export symbols with and without @nn\n"));
420   fprintf (file, _("  --disable-stdcall-fixup            Don't link _sym to _sym@nn\n"));
421   fprintf (file, _("  --enable-stdcall-fixup             Link _sym to _sym@nn without warnings\n"));
422   fprintf (file, _("  --exclude-symbols sym,sym,...      Exclude symbols from automatic export\n"));
423   fprintf (file, _("  --exclude-all-symbols              Exclude all symbols from automatic export\n"));
424   fprintf (file, _("  --exclude-libs lib,lib,...         Exclude libraries from automatic export\n"));
425   fprintf (file, _("  --exclude-modules-for-implib mod,mod,...\n"));
426   fprintf (file, _("                                     Exclude objects, archive members from auto\n"));
427   fprintf (file, _("                                     export, place into import library instead.\n"));
428   fprintf (file, _("  --export-all-symbols               Automatically export all globals to DLL\n"));
429   fprintf (file, _("  --kill-at                          Remove @nn from exported symbols\n"));
430   fprintf (file, _("  --output-def <file>                Generate a .DEF file for the built DLL\n"));
431   fprintf (file, _("  --warn-duplicate-exports           Warn about duplicate exports\n"));
432   fprintf (file, _("  --compat-implib                    Create backward compatible import libs;\n\
433                                        create __imp_<SYMBOL> as well.\n"));
434   fprintf (file, _("  --enable-auto-image-base[=<address>] Automatically choose image base for DLLs\n\
435                                        (optionally starting with address) unless\n\
436                                        specifically set with --image-base\n"));
437   fprintf (file, _("  --disable-auto-image-base          Do not auto-choose image base. (default)\n"));
438   fprintf (file, _("  --dll-search-prefix=<string>       When linking dynamically to a dll without\n\
439                                        an importlib, use <string><basename>.dll\n\
440                                        in preference to lib<basename>.dll \n"));
441   fprintf (file, _("  --enable-auto-import               Do sophisticated linking of _sym to\n\
442                                        __imp_sym for DATA references\n"));
443   fprintf (file, _("  --disable-auto-import              Do not auto-import DATA items from DLLs\n"));
444   fprintf (file, _("  --enable-runtime-pseudo-reloc      Work around auto-import limitations by\n\
445                                        adding pseudo-relocations resolved at\n\
446                                        runtime.\n"));
447   fprintf (file, _("  --disable-runtime-pseudo-reloc     Do not add runtime pseudo-relocations for\n\
448                                        auto-imported DATA.\n"));
449   fprintf (file, _("  --enable-extra-pe-debug            Enable verbose debug output when building\n\
450                                        or linking to DLLs (esp. auto-import)\n"));
451 #endif
452   fprintf (file, _("  --large-address-aware              Executable supports virtual addresses\n\
453                                        greater than 2 gigabytes\n"));
454   fprintf (file, _("  --disable-large-address-aware      Executable does not support virtual\n\
455                                        addresses greater than 2 gigabytes\n"));
456   fprintf (file, _("  --enable-long-section-names        Use long COFF section names even in\n\
457                                        executable image files\n"));
458   fprintf (file, _("  --disable-long-section-names       Never use long COFF section names, even\n\
459                                        in object files\n"));
460   fprintf (file, _("  --[disable-]dynamicbase            Image base address may be relocated using\n\
461                                        address space layout randomization (ASLR)\n"));
462   fprintf (file, _("  --enable-reloc-section             Create the base relocation table\n"));
463   fprintf (file, _("  --disable-reloc-section            Do not create the base relocation table\n"));
464   fprintf (file, _("  --[disable-]forceinteg             Code integrity checks are enforced\n"));
465   fprintf (file, _("  --[disable-]nxcompat               Image is compatible with data execution\n\
466                                        prevention\n"));
467   fprintf (file, _("  --[disable-]no-isolation           Image understands isolation but do not\n\
468                                        isolate the image\n"));
469   fprintf (file, _("  --[disable-]no-seh                 Image does not use SEH. No SE handler may\n\
470                                        be called in this image\n"));
471   fprintf (file, _("  --[disable-]no-bind                Do not bind this image\n"));
472   fprintf (file, _("  --[disable-]wdmdriver              Driver uses the WDM model\n"));
473   fprintf (file, _("  --[disable-]tsaware                Image is Terminal Server aware\n"));
474   fprintf (file, _("  --build-id[=STYLE]                 Generate build ID\n"));
475 #ifdef PDB_H
476   fprintf (file, _("  --pdb=[FILENAME]                   Generate PDB file\n"));
477 #endif
480 /* A case insensitive comparison, regardless of the host platform, used for
481    comparing file extensions.  */
482 static int
483 fileext_cmp (const char *s1, const char *s2)
485   for (;;)
486     {
487       int c1 = TOLOWER (*s1++);
488       int c2 = *s2++; /* Assumed to be lower case from the caller.  */
490       if (c1 != c2)
491         return (c1 - c2);
493       if (c1 == '\0')
494         return 0;
495     }
498 static void
499 set_pe_name (char *name, long val)
501   int i;
502   is_underscoring ();
504   /* Find the name and set it.  */
505   for (i = 0; init[i].ptr; i++)
506     {
507       if (strcmp (name, GET_INIT_SYMBOL_NAME (i)) == 0)
508         {
509           init[i].value = val;
510           init[i].inited = 1;
511           if (strcmp (name,"__image_base__") == 0)
512             set_pe_name (U ("__ImageBase"), val);
513           return;
514         }
515     }
516   abort ();
519 static void
520 set_entry_point (void)
522   const char *entry;
523   const char *initial_symbol_char;
524   int i;
526   static const struct
527   {
528     const int value;
529     const char *entry;
530   }
531   v[] =
532     {
533       { 1, "NtProcessStartup"  },
534       { 2, "WinMainCRTStartup" },
535       { 3, "mainCRTStartup"    },
536       { 7, "__PosixProcessStartup"},
537       { 9, "WinMainCRTStartup" },
538       {14, "mainCRTStartup"    },
539       { 0, NULL          }
540     };
542   /* Entry point name for arbitrary subsystem numbers.  */
543   static const char default_entry[] = "mainCRTStartup";
545   if (bfd_link_dll (&link_info) || dll)
546     {
547 #if defined (TARGET_IS_i386pe)
548       entry = "DllMainCRTStartup@12";
549 #else
550       entry = "DllMainCRTStartup";
551 #endif
552     }
553   else
554     {
555       for (i = 0; v[i].entry; i++)
556         if (v[i].value == pe_subsystem)
557           break;
559       /* If no match, use the default.  */
560       if (v[i].entry != NULL)
561         entry = v[i].entry;
562       else
563         entry = default_entry;
564     }
566   initial_symbol_char = (is_underscoring () != 0 ? "_" : "");
568   if (*initial_symbol_char != '\0')
569     {
570       char *alc_entry;
572       /* lang_default_entry expects its argument to be permanently
573          allocated, so we don't free this string.  */
574       alc_entry = xmalloc (strlen (initial_symbol_char)
575                            + strlen (entry)
576                            + 1);
577       strcpy (alc_entry, initial_symbol_char);
578       strcat (alc_entry, entry);
579       entry = alc_entry;
580     }
582   lang_default_entry (entry);
584   if (bfd_link_executable (&link_info) && ! entry_from_cmdline)
585     ldlang_add_undef (entry, false);  
588 static void
589 set_pe_subsystem (void)
591   const char *sver;
592   char *end;
593   int len;
594   int i;
595   unsigned long temp_subsystem;
596   static const struct
597     {
598       const char *name;
599       const int value;
600     }
601   v[] =
602     {
603       { "native",  1},
604       { "windows", 2},
605       { "console", 3},
606       { "posix",   7},
607       { "wince",   9},
608       { "xbox",   14},
609       { NULL, 0 }
610     };
612   /* Check for the presence of a version number.  */
613   sver = strchr (optarg, ':');
614   if (sver == NULL)
615     len = strlen (optarg);
616   else
617     {
618       len = sver - optarg;
619       set_pe_name ("__major_subsystem_version__",
620                     strtoul (sver + 1, &end, 0));
621       if (*end == '.')
622         set_pe_name ("__minor_subsystem_version__",
623                       strtoul (end + 1, &end, 0));
624       if (*end != '\0')
625         einfo (_("%P: warning: bad version number in -subsystem option\n"));
626     }
628   /* Check for numeric subsystem.  */
629   temp_subsystem = strtoul (optarg, & end, 0);
630   if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
631     {
632       /* Search list for a numeric match to use its entry point.  */
633       for (i = 0; v[i].name; i++)
634         if (v[i].value == (int) temp_subsystem)
635           break;
637       /* Use this subsystem.  */
638       pe_subsystem = (int) temp_subsystem;
639     }
640   else
641     {
642       /* Search for subsystem by name.  */
643       for (i = 0; v[i].name; i++)
644         if (strncmp (optarg, v[i].name, len) == 0
645             && v[i].name[len] == '\0')
646           break;
648       if (v[i].name == NULL)
649         {
650           einfo (_("%F%P: invalid subsystem type %s\n"), optarg);
651           return;
652         }
654       pe_subsystem = v[i].value;
655     }
657   set_pe_name ("__subsystem__", pe_subsystem);
659   return;
663 static void
664 set_pe_value (char *name)
666   char *end;
668   set_pe_name (name,  strtoul (optarg, &end, 0));
670   if (end == optarg)
671     einfo (_("%F%P: invalid hex number for PE parameter '%s'\n"), optarg);
673   optarg = end;
677 static void
678 set_pe_stack_heap (char *resname, char *comname)
680   set_pe_value (resname);
682   if (*optarg == ',')
683     {
684       optarg++;
685       set_pe_value (comname);
686     }
687   else if (*optarg)
688     einfo (_("%F%P: strange hex info for PE parameter '%s'\n"), optarg);
691 #define DEFAULT_BUILD_ID_STYLE  "md5"
693 static bool
694 gld${EMULATION_NAME}_handle_option (int optc)
696   switch (optc)
697     {
698     default:
699       return false;
701     case OPTION_BASE_FILE:
702       link_info.base_file = fopen (optarg, FOPEN_WB);
703       if (link_info.base_file == NULL)
704         einfo (_("%F%P: cannot open base file %s\n"), optarg);
705       break;
707       /* PE options.  */
708     case OPTION_HEAP:
709       set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
710       break;
711     case OPTION_STACK:
712       set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
713       break;
714     case OPTION_SUBSYSTEM:
715       set_pe_subsystem ();
716       break;
717     case OPTION_MAJOR_OS_VERSION:
718       set_pe_value ("__major_os_version__");
719       break;
720     case OPTION_MINOR_OS_VERSION:
721       set_pe_value ("__minor_os_version__");
722       break;
723     case OPTION_MAJOR_SUBSYSTEM_VERSION:
724       set_pe_value ("__major_subsystem_version__");
725       break;
726     case OPTION_MINOR_SUBSYSTEM_VERSION:
727       set_pe_value ("__minor_subsystem_version__");
728       break;
729     case OPTION_MAJOR_IMAGE_VERSION:
730       set_pe_value ("__major_image_version__");
731       break;
732     case OPTION_MINOR_IMAGE_VERSION:
733       set_pe_value ("__minor_image_version__");
734       break;
735     case OPTION_FILE_ALIGNMENT:
736       set_pe_value ("__file_alignment__");
737       break;
738     case OPTION_SECTION_ALIGNMENT:
739       set_pe_value ("__section_alignment__");
740       break;
741     case OPTION_DLL:
742       set_pe_name ("__dll__", 1);
743       break;
744     case OPTION_IMAGE_BASE:
745       set_pe_value ("__image_base__");
746       break;
747     case OPTION_SUPPORT_OLD_CODE:
748       support_old_code = 1;
749       break;
750     case OPTION_THUMB_ENTRY:
751       thumb_entry_symbol = optarg;
752       break;
753     case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
754       pe_use_nul_prefixed_import_tables = true;
755       break;
756     case OPTION_NO_LEADING_UNDERSCORE:
757       pe_leading_underscore = 0;
758       break;
759     case OPTION_LEADING_UNDERSCORE:
760       pe_leading_underscore = 1;
761       break;
762     case OPTION_INSERT_TIMESTAMP:
763       insert_timestamp = true;
764       break;
765     case OPTION_NO_INSERT_TIMESTAMP:
766       insert_timestamp = false;
767       break;
768 #ifdef DLL_SUPPORT
769     case OPTION_OUT_DEF:
770       pe_out_def_filename = xstrdup (optarg);
771       break;
772     case OPTION_EXPORT_ALL:
773       pe_dll_export_everything = 1;
774       break;
775     case OPTION_EXCLUDE_SYMBOLS:
776       pe_dll_add_excludes (optarg, EXCLUDESYMS);
777       break;
778     case OPTION_EXCLUDE_ALL_SYMBOLS:
779       pe_dll_exclude_all_symbols = 1;
780       break;
781     case OPTION_EXCLUDE_LIBS:
782       pe_dll_add_excludes (optarg, EXCLUDELIBS);
783       break;
784     case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
785       pe_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
786       break;
787     case OPTION_KILL_ATS:
788       pe_dll_kill_ats = 1;
789       break;
790     case OPTION_STDCALL_ALIASES:
791       pe_dll_stdcall_aliases = 1;
792       break;
793     case OPTION_ENABLE_STDCALL_FIXUP:
794       pe_enable_stdcall_fixup = 1;
795       break;
796     case OPTION_DISABLE_STDCALL_FIXUP:
797       pe_enable_stdcall_fixup = 0;
798       break;
799     case OPTION_WARN_DUPLICATE_EXPORTS:
800       pe_dll_warn_dup_exports = 1;
801       break;
802     case OPTION_IMP_COMPAT:
803       pe_dll_compat_implib = 1;
804       break;
805     case OPTION_ENABLE_AUTO_IMAGE_BASE:
806       pe_enable_auto_image_base = 1;
807       if (optarg && *optarg)
808         {
809           char *end;
810           pe_auto_image_base = strtoul (optarg, &end, 0);
811           /* XXX should check that we actually parsed something */
812         }
813       break;
814     case OPTION_DISABLE_AUTO_IMAGE_BASE:
815       pe_enable_auto_image_base = 0;
816       break;
817     case OPTION_DLL_SEARCH_PREFIX:
818       pe_dll_search_prefix = xstrdup (optarg);
819       break;
820     case OPTION_NO_DEFAULT_EXCLUDES:
821       pe_dll_do_default_excludes = 0;
822       break;
823     case OPTION_DLL_ENABLE_AUTO_IMPORT:
824       link_info.pei386_auto_import = 1;
825       break;
826     case OPTION_DLL_DISABLE_AUTO_IMPORT:
827       link_info.pei386_auto_import = 0;
828       break;
829     case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
830       link_info.pei386_runtime_pseudo_reloc =
831         DEFAULT_PSEUDO_RELOC_VERSION;
832       break;
833     case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1:
834       link_info.pei386_runtime_pseudo_reloc = 1;
835       break;
836     case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2:
837       link_info.pei386_runtime_pseudo_reloc = 2;
838       break;
839     case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
840       link_info.pei386_runtime_pseudo_reloc = 0;
841       break;
842     case OPTION_ENABLE_EXTRA_PE_DEBUG:
843       pe_dll_extra_pe_debug = 1;
844       break;
845 #endif
846     case OPTION_LARGE_ADDRESS_AWARE:
847       real_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
848       break;
849     case OPTION_DISABLE_LARGE_ADDRESS_AWARE:
850       real_flags &= ~ IMAGE_FILE_LARGE_ADDRESS_AWARE;
851       break;
852     case OPTION_ENABLE_LONG_SECTION_NAMES:
853       pe_use_coff_long_section_names = 1;
854       break;
855     case OPTION_DISABLE_LONG_SECTION_NAMES:
856       pe_use_coff_long_section_names = 0;
857       break;
858 /*  Get DLLCharacteristics bits  */
859     case OPTION_DYNAMIC_BASE:
860       pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
861       /* fall through */
862     case OPTION_ENABLE_RELOC_SECTION:
863       pe_dll_enable_reloc_section = 1;
864       break;
865     case OPTION_DISABLE_RELOC_SECTION:
866       pe_dll_enable_reloc_section = 0;
867       /* fall through */
868     case OPTION_DISABLE_DYNAMIC_BASE:
869       pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
870       break;
871     case OPTION_FORCE_INTEGRITY:
872       pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
873       break;
874     case OPTION_DISABLE_FORCE_INTEGRITY:
875       pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
876       break;
877     case OPTION_NX_COMPAT:
878       pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
879       break;
880     case OPTION_DISABLE_NX_COMPAT:
881       pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
882       break;
883     case OPTION_NO_ISOLATION:
884       pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
885       break;
886     case OPTION_DISABLE_NO_ISOLATION:
887       pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
888       break;
889     case OPTION_NO_SEH:
890       pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_SEH;
891       break;
892     case OPTION_DISABLE_NO_SEH:
893       pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_SEH;
894       break;
895     case OPTION_NO_BIND:
896       pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_BIND;
897       break;
898     case OPTION_DISABLE_NO_BIND:
899       pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_BIND;
900       break;
901     case OPTION_WDM_DRIVER:
902       pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
903       break;
904     case OPTION_DISABLE_WDM_DRIVER:
905       pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
906       break;
907     case OPTION_TERMINAL_SERVER_AWARE:
908       pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
909       break;
910     case OPTION_DISABLE_TERMINAL_SERVER_AWARE:
911       pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
912       break;
913     case OPTION_BUILD_ID:
914       free ((char *) emit_build_id);
915       emit_build_id = NULL;
916       if (optarg == NULL)
917         optarg = DEFAULT_BUILD_ID_STYLE;
918       if (strcmp (optarg, "none"))
919         emit_build_id = xstrdup (optarg);
920       break;
921 #ifdef PDB_H
922     case OPTION_PDB:
923       pdb = 1;
924       if (optarg && optarg[0])
925         pdb_name = xstrdup (optarg);
926       break;
927 #endif
928     }
930   /*  Set DLLCharacteristics bits  */
931   set_pe_name ("__dll_characteristics__", pe_dll_characteristics);
933   return true;
937 #ifdef DLL_SUPPORT
938 static unsigned long
939 strhash (const char *str)
941   const unsigned char *s;
942   unsigned long hash;
943   unsigned int c;
944   unsigned int len;
946   hash = 0;
947   len = 0;
948   s = (const unsigned char *) str;
949   while ((c = *s++) != '\0')
950     {
951       hash += c + (c << 17);
952       hash ^= hash >> 2;
953       ++len;
954     }
955   hash += len + (len << 17);
956   hash ^= hash >> 2;
958   return hash;
961 /* Use the output file to create a image base for relocatable DLLs.  */
963 static unsigned long
964 compute_dll_image_base (const char *ofile)
966   unsigned long hash = strhash (ofile);
967   return pe_auto_image_base + ((hash << 16) & 0x0FFC0000);
969 #endif
971 /* Assign values to the special symbols before the linker script is
972    read.  */
974 static void
975 gld${EMULATION_NAME}_set_symbols (void)
977   /* Run through and invent symbols for all the
978      names and insert the defaults.  */
979   int j;
981   is_underscoring ();
983   if (!init[IMAGEBASEOFF].inited)
984     {
985       if (bfd_link_relocatable (&link_info))
986         init[IMAGEBASEOFF].value = 0;
987       else if (init[DLLOFF].value || bfd_link_dll (&link_info))
988         {
989 #ifdef DLL_SUPPORT
990           init[IMAGEBASEOFF].value = (pe_enable_auto_image_base
991                                       ? compute_dll_image_base (output_filename)
992                                       : NT_DLL_IMAGE_BASE);
993 #else
994           init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
995 #endif
996         }
997       else
998         init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
999       init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
1000     }
1002   /* Don't do any symbol assignments if this is a relocatable link.  */
1003   if (bfd_link_relocatable (&link_info))
1004     return;
1006   /* Glue the assignments into the abs section.  */
1007   push_stat_ptr (&abs_output_section->children);
1009   for (j = 0; init[j].ptr; j++)
1010     {
1011       long val = init[j].value;
1012       lang_assignment_statement_type *rv;
1014       rv = lang_add_assignment (exp_assign (GET_INIT_SYMBOL_NAME (j),
1015                                             exp_intop (val), false));
1016       if (init[j].size == sizeof (short))
1017         *(short *) init[j].ptr = val;
1018       else if (init[j].size == sizeof (int))
1019         *(int *) init[j].ptr = val;
1020       else if (init[j].size == sizeof (long))
1021         *(long *) init[j].ptr = val;
1022       /* This might be a long long or other special type.  */
1023       else if (init[j].size == sizeof (bfd_vma))
1024         *(bfd_vma *) init[j].ptr = val;
1025       else      abort ();
1026       if (j == IMAGEBASEOFF)
1027         image_base_statement = rv;
1028     }
1029   /* Restore the pointer.  */
1030   pop_stat_ptr ();
1032   if (pe.FileAlignment > pe.SectionAlignment)
1033     {
1034       einfo (_("%P: warning, file alignment > section alignment\n"));
1035     }
1038 /* This is called after the linker script and the command line options
1039    have been read.  */
1041 static void
1042 gld${EMULATION_NAME}_after_parse (void)
1044   /* PR ld/6744:  Warn the user if they have used an ELF-only
1045      option hoping it will work on PE.  */
1046   if (link_info.export_dynamic)
1047     einfo (_("%P: warning: --export-dynamic is not supported for PE "
1048       "targets, did you mean --export-all-symbols?\n"));
1050 #ifdef PDB_H
1051   if (pdb && emit_build_id == NULL)
1052     emit_build_id = xstrdup (DEFAULT_BUILD_ID_STYLE);
1053 #endif
1055   set_entry_point ();
1057   after_parse_default ();
1060 #ifdef DLL_SUPPORT
1061 static struct bfd_link_hash_entry *pe_undef_found_sym;
1063 static bool
1064 pe_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
1066   int sl;
1067   char *string = inf;
1068   const char *hs = h->root.string;
1070   sl = strlen (string);
1071   if (h->type == bfd_link_hash_defined
1072       && ((*hs == '@' && *string == '_'
1073                    && strncmp (hs + 1, string + 1, sl - 1) == 0)
1074                   || strncmp (hs, string, sl) == 0)
1075       && h->root.string[sl] == '@')
1076     {
1077       pe_undef_found_sym = h;
1078       return false;
1079     }
1080   return true;
1083 /* Change UNDEF to a defined symbol, taking data from SYM.  */
1085 static void
1086 change_undef (struct bfd_link_hash_entry * undef,
1087               struct bfd_link_hash_entry * sym)
1089   static bool  gave_warning_message = false;
1091   undef->type = bfd_link_hash_defined;
1092   undef->u.def.value = sym->u.def.value;
1093   undef->u.def.section = sym->u.def.section;
1095   if (pe_enable_stdcall_fixup == -1)
1096     {
1097       einfo (_("%P: warning: resolving %s by linking to %s\n"),
1098              undef->root.string, sym->root.string);
1100       if (! gave_warning_message)
1101         {
1102           einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
1103           einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
1104           gave_warning_message = true;
1105         }
1106     }
1108   /* PR 19803: Make sure that the linked symbol is not garbage collected.  */
1109   lang_add_gc_name (sym->root.string);
1112 static void
1113 set_decoration (const char *undecorated_name,
1114                 struct bfd_link_hash_entry * decoration)
1116   static bool  gave_warning_message = false;
1117   struct decoration_hash_entry *entry;
1119   if (is_underscoring () && undecorated_name[0] == '_')
1120     undecorated_name++;
1122   entry = (struct decoration_hash_entry *)
1123           bfd_hash_lookup (&(coff_hash_table (&link_info)->decoration_hash),
1124                            undecorated_name, true /* create */, false /* copy */);
1126   if (entry->decorated_link != NULL && !gave_warning_message)
1127     {
1128       einfo (_("%P: warning: overwriting decorated name %s with %s\n"),
1129              entry->decorated_link->root.string, undecorated_name);
1130       gave_warning_message = true;
1131     }
1133   entry->decorated_link = decoration;
1136 static void
1137 pe_fixup_stdcalls (void)
1139   struct bfd_link_hash_entry *undef, *sym;
1141   if (pe_dll_extra_pe_debug)
1142     printf ("%s\n", __func__);
1144   for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
1145     if (undef->type == bfd_link_hash_undefined)
1146       {
1147         const char * name = undef->root.string;
1148         char * at;
1149         int lead_at = (*name == '@');
1151         if (lead_at)
1152           at = strchr (name + 1, '@');
1153         else
1154           at = strchr (name, '@');
1156         if (at || lead_at)
1157           {
1158             /* The symbol is a stdcall symbol, so let's look for a
1159                cdecl symbol with the same name and resolve to that.  */
1160             char *cname = xstrdup (name);
1162             if (lead_at)
1163               *cname = '_';
1164             if (at)
1165               * strchr (cname, '@') = 0;
1166             sym = bfd_link_hash_lookup (link_info.hash, cname, false, false, true);
1168             if (sym && sym->type == bfd_link_hash_defined)
1169               change_undef (undef, sym);
1170           }
1171         else
1172           {
1173             /* The symbol is a cdecl symbol, so we look for stdcall
1174                symbols - which means scanning the whole symbol table.  */
1175             pe_undef_found_sym = NULL;
1176             bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
1177                                     (char *) name);
1178             if (pe_undef_found_sym)
1179               {
1180                 change_undef (undef, pe_undef_found_sym);
1181                 set_decoration (undef->root.string, pe_undef_found_sym);
1182               }
1183           }
1184       }
1187 static bfd_vma
1188 read_addend (arelent *rel, asection *s)
1190   char buf[4];
1191   bfd_vma addend = 0;
1193   if (!bfd_get_section_contents (s->owner, s, buf, rel->address, sizeof (buf)))
1194     einfo (_("%P: %H: cannot get section contents - auto-import exception\n"),
1195            s->owner, s, rel->address);
1196   else
1197     addend = bfd_get_32 (s->owner, buf);
1198   return addend;
1201 static void
1202 make_import_fixup (arelent *rel, asection *s, char *name, const char *symname)
1204   struct bfd_symbol *sym = *rel->sym_ptr_ptr;
1205   bfd_vma addend;
1207   if (pe_dll_extra_pe_debug)
1208     printf ("arelent: %s@%#lx: add=%li\n", sym->name,
1209             (unsigned long) rel->address, (long) rel->addend);
1211   addend = read_addend (rel, s);
1213   if (pe_dll_extra_pe_debug)
1214     {
1215       printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx",
1216               (long) addend, (long) rel->addend, (long) rel->address);
1217       if (rel->howto->pc_relative)
1218         printf (" pcrel");
1219       printf (" %d bit rel.\n", (int) rel->howto->bitsize);
1220     }
1222   pe_create_import_fixup (rel, s, addend, name, symname);
1225 static void
1226 make_runtime_ref (void)
1228   const char *rr = U ("_pei386_runtime_relocator");
1229   struct bfd_link_hash_entry *h
1230     = bfd_wrapped_link_hash_lookup (link_info.output_bfd, &link_info,
1231                                     rr, true, false, true);
1232   if (!h)
1233     einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
1234   else
1235     {
1236       if (h->type == bfd_link_hash_new)
1237         {
1238           h->type = bfd_link_hash_undefined;
1239           h->u.undef.abfd = NULL;
1240           if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
1241             bfd_link_add_undef (link_info.hash, h);
1242         }
1243       h->non_ir_ref_regular = true;
1244     }
1247 static bool
1248 pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
1250   printf ("+%s\n", h->string);
1252   return true;
1254 #endif /* DLL_SUPPORT */
1256 static void
1257 debug_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
1259   int *found = (int *) obj;
1260   if (strncmp (".debug_", sect->name, sizeof (".debug_") - 1) == 0)
1261     *found = 1;
1264 static bool
1265 pecoff_checksum_contents (bfd *abfd,
1266                           void (*process) (const void *, size_t, void *),
1267                           void *arg)
1269   file_ptr filepos = (file_ptr) 0;
1271   while (1)
1272     {
1273       unsigned char b;
1274       int status;
1276       if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
1277         return 0;
1279       status = bfd_read (&b, 1, abfd);
1280       if (status < 1)
1281         {
1282           break;
1283         }
1285       (*process) (&b, 1, arg);
1286       filepos += 1;
1287     }
1289   return true;
1292 static bool
1293 write_build_id (bfd *abfd)
1295   struct pe_tdata *td = pe_data (abfd);
1296   asection *asec;
1297   struct bfd_link_order *link_order = NULL;
1298   unsigned char *contents;
1299   bfd_size_type build_id_size;
1300   unsigned char *build_id;
1301   const char *pdb_base_name = NULL;
1303   /* Find the section the .buildid output section has been merged info.  */
1304   for (asec = abfd->sections; asec != NULL; asec = asec->next)
1305     {
1306       struct bfd_link_order *l = NULL;
1307       for (l = asec->map_head.link_order; l != NULL; l = l->next)
1308         {
1309           if (l->type == bfd_indirect_link_order)
1310             {
1311               if (l->u.indirect.section == td->build_id.sec)
1312                 {
1313                   link_order = l;
1314                   break;
1315                 }
1316             }
1317         }
1319       if (link_order)
1320         break;
1321     }
1323   if (!link_order)
1324     {
1325       einfo (_("%P: warning: .buildid section discarded,"
1326                " --build-id ignored\n"));
1327       return true;
1328     }
1330   if (td->build_id.sec->contents == NULL)
1331     td->build_id.sec->contents = xmalloc (td->build_id.sec->size);
1332   contents = td->build_id.sec->contents;
1334   build_id_size = compute_build_id_size (td->build_id.style);
1335   build_id = xmalloc (build_id_size);
1336   generate_build_id (abfd, td->build_id.style, pecoff_checksum_contents,
1337                      build_id, build_id_size);
1339   bfd_vma ib = td->pe_opthdr.ImageBase;
1341 #ifdef PDB_H
1342   if (pdb_name)
1343     pdb_base_name = lbasename (pdb_name);
1344 #endif
1346   /* Construct a debug directory entry which points to an immediately following CodeView record.  */
1347   struct internal_IMAGE_DEBUG_DIRECTORY idd;
1348   idd.Characteristics = 0;
1349   idd.TimeDateStamp = 0;
1350   idd.MajorVersion = 0;
1351   idd.MinorVersion = 0;
1352   idd.Type = PE_IMAGE_DEBUG_TYPE_CODEVIEW;
1353   idd.SizeOfData = (sizeof (CV_INFO_PDB70)
1354 #ifdef PDB_H
1355                     + (pdb_base_name ? strlen (pdb_base_name) : 0)
1356 #endif
1357                     + 1);
1358   idd.AddressOfRawData = asec->vma - ib + link_order->offset
1359     + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1360   idd.PointerToRawData = asec->filepos + link_order->offset
1361     + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1363   struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *)contents;
1364   _bfd_XXi_swap_debugdir_out (abfd, &idd, ext);
1366   /* Write the debug directory entry.  */
1367   if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0)
1368     return 0;
1370   if (bfd_write (contents, sizeof (*ext), abfd) != sizeof (*ext))
1371     return 0;
1373 #ifdef PDB_H
1374   if (pdb)
1375     {
1376       if (!create_pdb_file (abfd, pdb_name, build_id))
1377         return 0;
1378     }
1379 #endif
1381   /* Construct the CodeView record.  */
1382   CODEVIEW_INFO cvinfo;
1383   cvinfo.CVSignature = CVINFO_PDB70_CVSIGNATURE;
1384   cvinfo.Age = 1;
1386   /* Zero pad or truncate the generated build_id to fit in the
1387      CodeView record.  */
1388   memset (&(cvinfo.Signature), 0, CV_INFO_SIGNATURE_LENGTH);
1389   memcpy (&(cvinfo.Signature), build_id,
1390           (build_id_size > CV_INFO_SIGNATURE_LENGTH
1391            ? CV_INFO_SIGNATURE_LENGTH : build_id_size));
1393   free (build_id);
1395   /* Write the codeview record.  */
1396   if (_bfd_XXi_write_codeview_record (abfd, idd.PointerToRawData, &cvinfo,
1397                                       pdb_base_name) == 0)
1398     return 0;
1400   /* Record the location of the debug directory in the data directory.  */
1401   td->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress
1402     = asec->vma - ib + link_order->offset;
1403   td->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
1404     = sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1406   return true;
1409 /* Make .buildid section, and set up coff_tdata->build_id. */
1410 static bool
1411 setup_build_id (bfd *ibfd)
1413   asection *s;
1414   flagword flags;
1416   if (!validate_build_id_style (emit_build_id))
1417     {
1418       einfo (_("%P: warning: unrecognized --build-id style ignored\n"));
1419       return false;
1420     }
1422   flags = (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY
1423            | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA);
1424   s = bfd_make_section_anyway_with_flags (ibfd, ".buildid", flags);
1425   if (s != NULL)
1426     {
1427       struct pe_tdata *td = pe_data (link_info.output_bfd);
1428       td->build_id.after_write_object_contents = &write_build_id;
1429       td->build_id.style = emit_build_id;
1430       td->build_id.sec = s;
1432       /* Section is a fixed size:
1433          One IMAGE_DEBUG_DIRECTORY entry, of type IMAGE_DEBUG_TYPE_CODEVIEW,
1434          pointing at a CV_INFO_PDB70 record containing the build-id, followed by
1435          PdbFileName if relevant.  */
1436       s->size = (sizeof (struct external_IMAGE_DEBUG_DIRECTORY)
1437                  + sizeof (CV_INFO_PDB70) + 1);
1439 #ifdef PDB_H
1440       if (pdb_name)
1441         s->size += strlen (lbasename (pdb_name));
1442 #endif
1443       return true;
1444     }
1446   einfo (_("%P: warning: cannot create .buildid section,"
1447            " --build-id ignored\n"));
1448   return false;
1451 static void
1452 gld${EMULATION_NAME}_before_plugin_all_symbols_read (void)
1454 #ifdef DLL_SUPPORT
1455   if (link_info.lto_plugin_active
1456       && link_info.pei386_auto_import)
1457     make_runtime_ref ();
1458 #endif
1461 static void
1462 gld${EMULATION_NAME}_after_open (void)
1464   after_open_default ();
1466 #ifdef DLL_SUPPORT
1467   if (pe_dll_extra_pe_debug)
1468     {
1469       bfd *a;
1470       struct bfd_link_hash_entry *sym;
1472       printf ("%s()\n", __func__);
1474       for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1475         printf ("-%s\n", sym->root.string);
1476       bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
1478       for (a = link_info.input_bfds; a; a = a->link.next)
1479         printf ("*%s\n", bfd_get_filename (a));
1480     }
1481 #endif
1483 #ifdef PDB_H
1484   if (pdb && !pdb_name)
1485     {
1486       const char *base = lbasename (bfd_get_filename (link_info.output_bfd));
1487       size_t len = strlen (base);
1488       static const char suffix[] = ".pdb";
1490       while (len > 0 && base[len] != '.')
1491         {
1492           len--;
1493         }
1495       if (len == 0)
1496         len = strlen (base);
1498       pdb_name = xmalloc (len + sizeof (suffix));
1499       memcpy (pdb_name, base, len);
1500       memcpy (pdb_name + len, suffix, sizeof (suffix));
1501     }
1502 #endif
1504   if (emit_build_id != NULL)
1505     {
1506       bfd *abfd;
1508       /* Find a COFF input.  */
1509       for (abfd = link_info.input_bfds;
1510            abfd != (bfd *) NULL; abfd = abfd->link.next)
1511         if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
1512           break;
1514       /* If there are no COFF input files do not try to
1515          add a build-id section.  */
1516       if (abfd == NULL
1517           || !setup_build_id (abfd))
1518         {
1519           free ((char *) emit_build_id);
1520           emit_build_id = NULL;
1521         }
1522     }
1524   /* Pass the wacky PE command line options into the output bfd.
1525      FIXME: This should be done via a function, rather than by
1526      including an internal BFD header.  */
1528   if (bfd_get_flavour (link_info.output_bfd) != bfd_target_coff_flavour
1529       || coff_data (link_info.output_bfd) == NULL
1530       || !obj_pe (link_info.output_bfd))
1531     einfo (_("%F%P: cannot perform PE operations on non PE output file '%pB'\n"),
1532            link_info.output_bfd);
1534   pe_data (link_info.output_bfd)->pe_opthdr = pe;
1535   pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
1536   pe_data (link_info.output_bfd)->real_flags |= real_flags;
1537   if (insert_timestamp)
1538     pe_data (link_info.output_bfd)->timestamp = -1;
1539   else
1540     pe_data (link_info.output_bfd)->timestamp = 0;
1542   /* At this point we must decide whether to use long section names
1543      in the output or not.  If the user hasn't explicitly specified
1544      on the command line, we leave it to the default for the format
1545      (object files yes, image files no), except if there is debug
1546      information present; GDB relies on the long section names to
1547      find it, so enable it in that case.  */
1548   if (pe_use_coff_long_section_names < 0 && link_info.strip == strip_none)
1549     {
1550       if (bfd_link_relocatable (&link_info))
1551         pe_use_coff_long_section_names = 1;
1552       else
1553         {
1554           /* Iterate over all sections of all input BFDs, checking
1555              for any that begin 'debug_' and are long names.  */
1556           LANG_FOR_EACH_INPUT_STATEMENT (is)
1557           {
1558             int found_debug = 0;
1560             bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug);
1561             if (found_debug)
1562               {
1563                 pe_use_coff_long_section_names = 1;
1564                 break;
1565               }
1566           }
1567         }
1568     }
1570   pe_output_file_set_long_section_names (link_info.output_bfd);
1572 #ifdef DLL_SUPPORT
1573   pe_process_import_defs (link_info.output_bfd, &link_info);
1575   if (link_info.pei386_auto_import) /* -1=warn or 1=enable */
1576     pe_find_data_imports (U ("_head_"), make_import_fixup);
1578   /* The implementation of the feature is rather dumb and would cause the
1579      compilation time to go through the roof if there are many undefined
1580      symbols in the link, so it needs to be run after auto-import.  */
1581   if (pe_enable_stdcall_fixup) /* -1=warn or 1=enable */
1582     pe_fixup_stdcalls ();
1584 #if defined (TARGET_IS_i386pe) \
1585     || defined (TARGET_IS_armpe) \
1586     || defined (TARGET_IS_arm_wince_pe)
1587   if (!bfd_link_relocatable (&link_info))
1588     pe_dll_build_sections (link_info.output_bfd, &link_info);
1589 #else
1590   if (bfd_link_pic (&link_info))
1591     pe_dll_build_sections (link_info.output_bfd, &link_info);
1592   else
1593     pe_exe_build_sections (link_info.output_bfd, &link_info);
1594 #endif
1595 #endif /* DLL_SUPPORT */
1597 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
1598   if (strstr (bfd_get_target (link_info.output_bfd), "arm") == NULL)
1599     {
1600       /* The arm backend needs special fields in the output hash structure.
1601          These will only be created if the output format is an arm format,
1602          hence we do not support linking and changing output formats at the
1603          same time.  Use a link followed by objcopy to change output formats.  */
1604       einfo (_("%F%P: error: cannot change output format "
1605                "whilst linking %s binaries\n"), "ARM");
1606       return;
1607     }
1608   {
1609     /* Find a BFD that can hold the interworking stubs.  */
1610     LANG_FOR_EACH_INPUT_STATEMENT (is)
1611       {
1612         if (bfd_arm_get_bfd_for_interworking (is->the_bfd, & link_info))
1613           break;
1614       }
1615   }
1616 #endif
1618   {
1619     /* This next chunk of code tries to detect the case where you have
1620        two import libraries for the same DLL (specifically,
1621        symbolically linking libm.a and libc.a in cygwin to
1622        libcygwin.a).  In those cases, it's possible for function
1623        thunks from the second implib to be used but without the
1624        head/tail objects, causing an improper import table.  We detect
1625        those cases and rename the "other" import libraries to match
1626        the one the head/tail come from, so that the linker will sort
1627        things nicely and produce a valid import table.  */
1629     LANG_FOR_EACH_INPUT_STATEMENT (is)
1630       {
1631         if (is->the_bfd->my_archive)
1632           {
1633             int idata2 = 0, reloc_count=0, is_imp = 0;
1634             asection *sec;
1636             /* See if this is an import library thunk.  */
1637             for (sec = is->the_bfd->sections; sec; sec = sec->next)
1638               {
1639                 if (strcmp (sec->name, ".idata\$2") == 0)
1640                   idata2 = 1;
1641                 if (startswith (sec->name, ".idata\$"))
1642                   is_imp = 1;
1643                 reloc_count += sec->reloc_count;
1644               }
1646             if (is_imp && !idata2 && reloc_count)
1647               {
1648                 /* It is, look for the reference to head and see if it's
1649                    from our own library.  */
1650                 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1651                   {
1652                     int i;
1653                     long relsize;
1654                     asymbol **symbols;
1655                     arelent **relocs;
1656                     int nrelocs;
1658                     relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1659                     if (relsize < 1)
1660                       break;
1662                     if (!bfd_generic_link_read_symbols (is->the_bfd))
1663                       {
1664                         einfo (_("%F%P: %pB: could not read symbols: %E\n"),
1665                                is->the_bfd);
1666                         return;
1667                       }
1668                     symbols = bfd_get_outsymbols (is->the_bfd);
1670                     relocs = xmalloc ((size_t) relsize);
1671                     nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1672                                                       relocs, symbols);
1673                     if (nrelocs < 0)
1674                       {
1675                         free (relocs);
1676                         einfo (_("%X%P: unable to process relocs: %E\n"));
1677                         return;
1678                       }
1680                     for (i = 0; i < nrelocs; i++)
1681                       {
1682                         struct bfd_symbol *s;
1683                         struct bfd_link_hash_entry * blhe;
1684                         bfd *other_bfd;
1685                         const char *other_bfd_filename;
1687                         s = (relocs[i]->sym_ptr_ptr)[0];
1689                         if (s->flags & BSF_LOCAL)
1690                           continue;
1692                         /* Thunk section with reloc to another bfd.  */
1693                         blhe = bfd_link_hash_lookup (link_info.hash,
1694                                                      s->name,
1695                                                      false, false, true);
1697                         if (blhe == NULL
1698                             || blhe->type != bfd_link_hash_defined)
1699                           continue;
1701                         other_bfd = blhe->u.def.section->owner;
1702                         if (other_bfd->my_archive == is->the_bfd->my_archive)
1703                           continue;
1705                         other_bfd_filename
1706                           = (other_bfd->my_archive
1707                              ? bfd_get_filename (other_bfd->my_archive)
1708                              : bfd_get_filename (other_bfd));
1710                         if (filename_cmp (bfd_get_filename
1711                                             (is->the_bfd->my_archive),
1712                                           other_bfd_filename) == 0)
1713                           continue;
1715                         /* Sort this implib to match the other one.  */
1716                         lang_input_statement_type *arch_is
1717                           = bfd_usrdata (is->the_bfd->my_archive);
1718                         arch_is->sort_key = other_bfd_filename;
1719                         break;
1720                       }
1722                     free (relocs);
1723                     /* Note - we do not free the symbols,
1724                        they are now cached in the BFD.  */
1725                   }
1726               }
1727           }
1728       }
1729   }
1731   {
1733     /* Careful - this is a shell script.  Watch those dollar signs! */
1734     /* Microsoft import libraries have every member named the same,
1735        and not in the right order for us to link them correctly.  We
1736        must detect these and rename the members so that they'll link
1737        correctly.  There are three types of objects: the head, the
1738        thunks, and the sentinel(s).  The head is easy; it's the one
1739        with idata2.  We assume that the sentinels won't have relocs,
1740        and the thunks will.  It's easier than checking the symbol
1741        table for external references.  */
1742     LANG_FOR_EACH_INPUT_STATEMENT (is)
1743       {
1744         if (is->the_bfd->my_archive)
1745           {
1746             char *pnt;
1748             /* Microsoft import libraries may contain archive members for
1749                one or more DLLs, together with static object files.
1750                The head and sentinels are regular COFF object files,
1751                while the thunks are special ILF files that get synthesized
1752                by bfd into COFF object files.
1754                As Microsoft import libraries can be for a module with
1755                almost any file name (*.dll, *.exe, etc), we can't easily
1756                know which archive members to inspect.
1758                Inspect all members, except ones named *.o or *.obj (which
1759                is the case both for regular static libraries or for GNU
1760                style import libraries). Archive members with file names other
1761                than *.o or *.obj, that do contain .idata sections, are
1762                considered to be Microsoft style import objects, and are
1763                renamed accordingly.
1765                If this heuristic is wrong and we apply this on archive members
1766                that already have unique names, it shouldn't make any difference
1767                as we only append a suffix on the names.  */
1768             pnt = strrchr (bfd_get_filename (is->the_bfd), '.');
1770             if (pnt != NULL && (fileext_cmp (pnt + 1, "o") != 0 &&
1771                                 fileext_cmp (pnt + 1, "obj") != 0))
1772               {
1773                 int idata2 = 0, reloc_count = 0, idata = 0;
1774                 asection *sec;
1775                 char *new_name, seq;
1777                 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1778                   {
1779                     if (strcmp (sec->name, ".idata\$2") == 0)
1780                       idata2 = 1;
1781                     if (strncmp (sec->name, ".idata\$", 6) == 0)
1782                       idata = 1;
1783                     reloc_count += sec->reloc_count;
1784                   }
1786                 /* An archive member not named .o or .obj, but not having any
1787                    .idata sections - apparently not a Microsoft import object
1788                    after all: Skip renaming it.  */
1789                 if (!idata)
1790                   continue;
1792                 if (idata2) /* .idata2 is the TOC */
1793                   seq = 'a';
1794                 else if (reloc_count > 0) /* thunks */
1795                   seq = 'b';
1796                 else /* sentinel */
1797                   seq = 'c';
1799                 new_name
1800                   = xmalloc (strlen (bfd_get_filename (is->the_bfd)) + 3);
1801                 sprintf (new_name, "%s.%c",
1802                          bfd_get_filename (is->the_bfd), seq);
1803                 is->sort_key = new_name;
1804               }
1805           }
1806       }
1807   }
1809   {
1810     /* The following chunk of code tries to identify jump stubs in
1811        import libraries which are dead code and eliminates them
1812        from the final link. For each exported symbol <sym>, there
1813        is a object file in the import library with a .text section
1814        and several .idata\$* sections. The .text section contains the
1815        symbol definition for <sym> which is a jump stub of the form
1816        jmp *__imp_<sym>. The .idata\$5 contains the symbol definition
1817        for __imp_<sym> which is the address of the slot for <sym> in
1818        the import address table. When a symbol is imported explicitly
1819        using __declspec(dllimport) declaration, the compiler generates
1820        a reference to __imp_<sym> which directly resolves to the
1821        symbol in .idata\$5, in which case the jump stub code is not
1822        needed. The following code tries to identify jump stub sections
1823        in import libraries which are not referred to by anyone and
1824        marks them for exclusion from the final link.  */
1825     LANG_FOR_EACH_INPUT_STATEMENT (is)
1826       {
1827         if (is->the_bfd->my_archive)
1828           {
1829             int is_imp = 0;
1830             asection *sec, *stub_sec = NULL;
1832             /* See if this is an import library thunk.  */
1833             for (sec = is->the_bfd->sections; sec; sec = sec->next)
1834               {
1835                 if (strncmp (sec->name, ".idata\$", 7) == 0)
1836                   is_imp = 1;
1837                 /* The section containing the jmp stub has code
1838                    and has a reloc.  */
1839                 if ((sec->flags & SEC_CODE) && sec->reloc_count)
1840                   stub_sec = sec;
1841               }
1843             if (is_imp && stub_sec)
1844               {
1845                 asymbol **symbols;
1846                 long nsyms, src_count;
1847                 struct bfd_link_hash_entry * blhe;
1849                 if (!bfd_generic_link_read_symbols (is->the_bfd))
1850                   {
1851                     einfo (_("%F%P: %pB: could not read symbols: %E\n"),
1852                            is->the_bfd);
1853                     return;
1854                   }
1855                 symbols = bfd_get_outsymbols (is->the_bfd);
1856                 nsyms = bfd_get_symcount (is->the_bfd);
1858                 for (src_count = 0; src_count < nsyms; src_count++)
1859                   {
1860                     if (symbols[src_count]->section->id == stub_sec->id)
1861                       {
1862                         /* This symbol belongs to the section containing
1863                            the stub.  */
1864                         blhe = bfd_link_hash_lookup (link_info.hash,
1865                                                      symbols[src_count]->name,
1866                                                      false, false, true);
1867                         /* If the symbol in the stub section has no other
1868                            undefined references, exclude the stub section
1869                            from the final link.  */
1870                         if (blhe != NULL
1871                             && blhe->type == bfd_link_hash_defined
1872                             && blhe->u.undef.next == NULL
1873                             && blhe != link_info.hash->undefs_tail)
1874                           stub_sec->flags |= SEC_EXCLUDE;
1875                       }
1876                   }
1877               }
1878           }
1879       }
1880   }
1883 static void
1884 gld${EMULATION_NAME}_before_allocation (void)
1886 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
1887   /* FIXME: we should be able to set the size of the interworking stub
1888      section.
1890      Here we rummage through the found bfds to collect glue
1891      information.  FIXME: should this be based on a command line
1892      option?  krk@cygnus.com.  */
1893   {
1894     LANG_FOR_EACH_INPUT_STATEMENT (is)
1895       {
1896         if (! bfd_arm_process_before_allocation
1897             (is->the_bfd, & link_info, support_old_code))
1898           {
1899             /* xgettext:c-format */
1900             einfo (_("%P: errors encountered processing file %s for interworking\n"),
1901                    is->filename);
1902           }
1903       }
1904   }
1906   /* We have seen it all. Allocate it, and carry on.  */
1907   bfd_arm_allocate_interworking_sections (& link_info);
1908 #endif /* TARGET_IS_armpe || TARGET_IS_arm_wince_pe */
1910   before_allocation_default ();
1913 #ifdef DLL_SUPPORT
1914 /* This is called when an input file isn't recognized as a BFD.  We
1915    check here for .DEF files and pull them in automatically.  */
1917 static int
1918 saw_option (char *option)
1920   int i;
1922   for (i = 0; init[i].ptr; i++)
1923     if (strcmp (GET_INIT_SYMBOL_NAME (i), option) == 0)
1924       return init[i].inited;
1925   return 0;
1927 #endif /* DLL_SUPPORT */
1929 static bool
1930 gld${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1932 #ifdef DLL_SUPPORT
1933   const char *ext = strrchr (entry->filename, '.');
1935   if (ext != NULL && fileext_cmp (ext + 1, "def") == 0)
1936     {
1937       pe_def_file = def_file_parse (entry->filename, pe_def_file);
1939       if (pe_def_file)
1940         {
1941           int i, buflen=0, len;
1942           char *buf;
1944           for (i = 0; i < pe_def_file->num_exports; i++)
1945             {
1946               len = strlen (pe_def_file->exports[i].internal_name);
1947               if (buflen < len + 2)
1948                 buflen = len + 2;
1949             }
1951           buf = xmalloc (buflen);
1953           for (i = 0; i < pe_def_file->num_exports; i++)
1954             {
1955               struct bfd_link_hash_entry *h;
1957               sprintf (buf, "%s%s", U (""),
1958                        pe_def_file->exports[i].internal_name);
1960               h = bfd_link_hash_lookup (link_info.hash, buf, true, true, true);
1961               if (h == (struct bfd_link_hash_entry *) NULL)
1962                 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
1963               if (h->type == bfd_link_hash_new)
1964                 {
1965                   h->type = bfd_link_hash_undefined;
1966                   h->u.undef.abfd = NULL;
1967                   bfd_link_add_undef (link_info.hash, h);
1968                 }
1969             }
1970           free (buf);
1972           /* def_file_print (stdout, pe_def_file); */
1973           if (pe_def_file->is_dll == 1)
1974             link_info.type = type_dll;
1976           if (pe_def_file->base_address != (bfd_vma)(-1))
1977             {
1978               pe.ImageBase
1979                 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1980                 = init[IMAGEBASEOFF].value
1981                 = pe_def_file->base_address;
1982               init[IMAGEBASEOFF].inited = 1;
1983               if (image_base_statement)
1984                 image_base_statement->exp
1985                   = exp_assign ("__image_base__", exp_intop (pe.ImageBase),
1986                                 false);
1987             }
1989           if (pe_def_file->stack_reserve != -1
1990               && ! saw_option ("__size_of_stack_reserve__"))
1991             {
1992               pe.SizeOfStackReserve = pe_def_file->stack_reserve;
1993               if (pe_def_file->stack_commit != -1)
1994                 pe.SizeOfStackCommit = pe_def_file->stack_commit;
1995             }
1996           if (pe_def_file->heap_reserve != -1
1997               && ! saw_option ("__size_of_heap_reserve__"))
1998             {
1999               pe.SizeOfHeapReserve = pe_def_file->heap_reserve;
2000               if (pe_def_file->heap_commit != -1)
2001                 pe.SizeOfHeapCommit = pe_def_file->heap_commit;
2002             }
2003           return true;
2004         }
2005     }
2006 #endif
2007   return false;
2010 static bool
2011 gld${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
2013 #ifdef DLL_SUPPORT
2014 #ifdef TARGET_IS_i386pe
2015   pe_dll_id_target ("pei-i386");
2016 #endif
2017 #ifdef TARGET_IS_shpe
2018   pe_dll_id_target ("pei-shl");
2019 #endif
2020 #ifdef TARGET_IS_armpe
2021   pe_dll_id_target ("pei-arm-little");
2022 #endif
2023 #ifdef TARGET_IS_arm_wince_pe
2024   pe_dll_id_target ("pei-arm-wince-little");
2025 #endif
2026   if (pe_bfd_is_dll (entry->the_bfd))
2027     return pe_implied_import_dll (entry->filename);
2028 #endif
2029   return false;
2032 static void
2033 gld${EMULATION_NAME}_finish (void)
2035 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
2036   struct bfd_link_hash_entry * h;
2038   if (thumb_entry_symbol != NULL)
2039     {
2040       h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol,
2041                                 false, false, true);
2043       if (h != (struct bfd_link_hash_entry *) NULL
2044           && (h->type == bfd_link_hash_defined
2045               || h->type == bfd_link_hash_defweak)
2046           && h->u.def.section->output_section != NULL)
2047         {
2048           static char buffer[32];
2049           bfd_vma val;
2051           /* Special procesing is required for a Thumb entry symbol.  The
2052              bottom bit of its address must be set.  */
2053           val = (h->u.def.value
2054                  + bfd_section_vma (h->u.def.section->output_section)
2055                  + h->u.def.section->output_offset);
2057           val |= 1;
2059           /* Now convert this value into a string and store it in entry_symbol
2060              where the lang_finish() function will pick it up.  */
2061           sprintf (buffer, "0x%" PRIx64, (uint64_t) val);
2063           if (entry_symbol.name != NULL && entry_from_cmdline)
2064             einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
2065                    thumb_entry_symbol, entry_symbol.name);
2066           entry_symbol.name = buffer;
2067         }
2068       else
2069         einfo (_("%P: warning: cannot find thumb start symbol %s\n"), thumb_entry_symbol);
2070     }
2071 #endif /* defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe) */
2073   finish_default ();
2075 #ifdef DLL_SUPPORT
2076   if (bfd_link_pic (&link_info)
2077 #if !defined(TARGET_IS_shpe)
2078       || pe_dll_enable_reloc_section
2079       || (!bfd_link_relocatable (&link_info)
2080           && pe_def_file->num_exports != 0)
2081 #endif
2082     )
2083     {
2084       pe_dll_fill_sections (link_info.output_bfd, &link_info);
2085       if (command_line.out_implib_filename
2086           && (pe_def_file->num_exports != 0
2087               || bfd_link_pic (&link_info)))
2088         pe_dll_generate_implib (pe_def_file, command_line.out_implib_filename,
2089                                 &link_info);
2090     }
2091 #if defined(TARGET_IS_shpe)
2092   /* ARM doesn't need relocs.  */
2093   else
2094     {
2095       pe_exe_fill_sections (link_info.output_bfd, &link_info);
2096     }
2097 #endif
2099   if (pe_out_def_filename)
2100     pe_dll_generate_def_file (pe_out_def_filename);
2101 #endif /* DLL_SUPPORT */
2103   /* I don't know where .idata gets set as code, but it shouldn't be.  */
2104   {
2105     asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
2107     if (asec)
2108       {
2109         asec->flags &= ~SEC_CODE;
2110         asec->flags |= SEC_DATA;
2111       }
2112   }
2116 /* Place an orphan section.
2118    We use this to put sections in a reasonable place in the file, and
2119    to ensure that they are aligned as required.
2121    We handle grouped sections here as well.  A section named .foo\$nn
2122    goes into the output section .foo.  All grouped sections are sorted
2123    by name.
2125    Grouped sections for the default sections are handled by the
2126    default linker script using wildcards, and are sorted by
2127    sort_sections.  */
2129 static lang_output_section_statement_type *
2130 gld${EMULATION_NAME}_place_orphan (asection *s,
2131                                    const char *secname,
2132                                    int constraint)
2134   const char *orig_secname = secname;
2135   char *dollar = NULL;
2136   lang_output_section_statement_type *os;
2137   lang_statement_list_type add_child;
2138   lang_output_section_statement_type *match_by_name = NULL;
2139   lang_statement_union_type **pl;
2141   /* Look through the script to see where to place this section.  */
2142   if (!bfd_link_relocatable (&link_info)
2143       && (dollar = strchr (secname, '\$')) != NULL)
2144     {
2145       size_t len = dollar - secname;
2146       char *newname = xmalloc (len + 1);
2147       memcpy (newname, secname, len);
2148       newname[len] = '\0';
2149       secname = newname;
2150     }
2152   lang_list_init (&add_child);
2154   os = NULL;
2155   if (constraint == 0)
2156     for (os = lang_output_section_find (secname);
2157          os != NULL;
2158          os = next_matching_output_section_statement (os, 0))
2159       {
2160         /* If we don't match an existing output section, tell
2161            lang_insert_orphan to create a new output section.  */
2162         constraint = SPECIAL;
2164         if (os->bfd_section != NULL
2165             && (os->bfd_section->flags == 0
2166                 || ((s->flags ^ os->bfd_section->flags)
2167                     & (SEC_LOAD | SEC_ALLOC)) == 0))
2168           {
2169             /* We already have an output section statement with this
2170                name, and its bfd section has compatible flags.
2171                If the section already exists but does not have any flags set,
2172                then it has been created by the linker, probably as a result of
2173                a --section-start command line switch.  */
2174             lang_add_section (&add_child, s, NULL, NULL, os);
2175             break;
2176           }
2178         /* Save unused output sections in case we can match them
2179            against orphans later.  */
2180         if (os->bfd_section == NULL)
2181           match_by_name = os;
2182       }
2184   /* If we didn't match an active output section, see if we matched an
2185      unused one and use that.  */
2186   if (os == NULL && match_by_name)
2187     {
2188       lang_add_section (&match_by_name->children, s, NULL, NULL, match_by_name);
2189       return match_by_name;
2190     }
2192   if (os == NULL)
2193     {
2194       static struct orphan_save hold[] =
2195         {
2196           { ".text",
2197             SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
2198             0, 0, 0, 0 },
2199           { ".idata",
2200             SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
2201             0, 0, 0, 0 },
2202           { ".rdata",
2203             SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
2204             0, 0, 0, 0 },
2205           { ".data",
2206             SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
2207             0, 0, 0, 0 },
2208           { ".bss",
2209             SEC_ALLOC,
2210             0, 0, 0, 0 }
2211         };
2212       enum orphan_save_index
2213         {
2214           orphan_text = 0,
2215           orphan_idata,
2216           orphan_rodata,
2217           orphan_data,
2218           orphan_bss
2219         };
2220       static int orphan_init_done = 0;
2221       struct orphan_save *place;
2222       lang_output_section_statement_type *after;
2223       etree_type *address;
2224       flagword flags;
2225       asection *nexts;
2227       if (!orphan_init_done)
2228         {
2229           struct orphan_save *ho;
2230           for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
2231             if (ho->name != NULL)
2232               {
2233                 ho->os = lang_output_section_find (ho->name);
2234                 if (ho->os != NULL && ho->os->flags == 0)
2235                   ho->os->flags = ho->flags;
2236               }
2237           orphan_init_done = 1;
2238         }
2240       flags = s->flags;
2241       if (!bfd_link_relocatable (&link_info))
2242         {
2243           nexts = s;
2244           while ((nexts = bfd_get_next_section_by_name (nexts->owner,
2245                                                         nexts)))
2246             if (nexts->output_section == NULL
2247                 && (nexts->flags & SEC_EXCLUDE) == 0
2248                 && ((nexts->flags ^ flags) & (SEC_LOAD | SEC_ALLOC)) == 0
2249                 && (nexts->owner->flags & DYNAMIC) == 0
2250                 && !bfd_input_just_syms (nexts->owner))
2251               flags = (((flags ^ SEC_READONLY)
2252                         | (nexts->flags ^ SEC_READONLY))
2253                        ^ SEC_READONLY);
2254         }
2256       /* Try to put the new output section in a reasonable place based
2257          on the section name and section flags.  */
2259       place = NULL;
2260       if ((flags & SEC_ALLOC) == 0)
2261         ;
2262       else if ((flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2263         place = &hold[orphan_bss];
2264       else if ((flags & SEC_READONLY) == 0)
2265         place = &hold[orphan_data];
2266       else if ((flags & SEC_CODE) == 0)
2267         {
2268           place = (!strncmp (secname, ".idata\$", 7) ? &hold[orphan_idata]
2269                                                      : &hold[orphan_rodata]);
2270         }
2271       else
2272         place = &hold[orphan_text];
2274       after = NULL;
2275       if (place != NULL)
2276         {
2277           if (place->os == NULL)
2278             place->os = lang_output_section_find (place->name);
2279           after = place->os;
2280           if (after == NULL)
2281             after = lang_output_section_find_by_flags (s, flags, &place->os,
2282                                                        NULL);
2283           if (after == NULL)
2284             /* *ABS* is always the first output section statement.  */
2285             after = (void *) lang_os_list.head;
2286         }
2288       /* All sections in an executable must be aligned to a page boundary.
2289          In a relocatable link, just preserve the incoming alignment; the
2290          address is discarded by lang_insert_orphan in that case, anyway.  */
2291       address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
2292       os = lang_insert_orphan (s, secname, constraint, after, place, address,
2293                                &add_child);
2294       if (bfd_link_relocatable (&link_info))
2295         {
2296           os->section_alignment = exp_intop (1U << s->alignment_power);
2297           os->bfd_section->alignment_power = s->alignment_power;
2298         }
2299     }
2301   /* If the section name has a '\$', sort it with the other '\$'
2302      sections.  */
2303   for (pl = &os->children.head; *pl != NULL; pl = &(*pl)->header.next)
2304     {
2305       lang_input_section_type *ls;
2306       const char *lname;
2308       if ((*pl)->header.type != lang_input_section_enum)
2309         continue;
2311       ls = &(*pl)->input_section;
2313       lname = bfd_section_name (ls->section);
2314       if (strchr (lname, '\$') != NULL
2315           && (dollar == NULL || strcmp (orig_secname, lname) < 0))
2316         break;
2317     }
2319   if (add_child.head != NULL)
2320     {
2321       *add_child.tail = *pl;
2322       *pl = add_child.head;
2323     }
2325   return os;
2328 static bool
2329 gld${EMULATION_NAME}_open_dynamic_archive
2330   (const char *arch ATTRIBUTE_UNUSED,
2331    search_dirs_type *search,
2332    lang_input_statement_type *entry)
2334   static const struct
2335     {
2336       const char * format;
2337       bool use_prefix;
2338     }
2339   libname_fmt [] =
2340     {
2341       /* Preferred explicit import library for dll's.  */
2342       { "lib%s.dll.a", false },
2343       /* Alternate explicit import library for dll's.  */
2344       { "%s.dll.a", false },
2345       /* "libfoo.a" could be either an import lib or a static lib.
2346          For backwards compatibility, libfoo.a needs to precede
2347          libfoo.dll and foo.dll in the search.  */
2348       { "lib%s.a", false },
2349       /* The 'native' spelling of an import lib name is "foo.lib".  */
2350       { "%s.lib", false },
2351       /* PR 22948 - Check for an import library.  */
2352       { "lib%s.lib", false },
2353 #ifdef DLL_SUPPORT
2354       /* Try "<prefix>foo.dll" (preferred dll name, if specified).  */
2355       { "%s%s.dll", true },
2356 #endif
2357       /* Try "libfoo.dll" (default preferred dll name).  */
2358       { "lib%s.dll", false },
2359       /* Finally try 'native' dll name "foo.dll".  */
2360       {  "%s.dll", false },
2361       /* Note: If adding more formats to this table, make sure to check to
2362          see if their length is longer than libname_fmt[0].format, and if
2363          so, update the call to xmalloc() below.  */
2364       { NULL, false }
2365     };
2366   static unsigned int format_max_len = 0;
2367   const char * filename;
2368   char * full_string;
2369   char * base_string;
2370   unsigned int i;
2373   if (! entry->flags.maybe_archive || entry->flags.full_name_provided)
2374     return false;
2376   filename = entry->filename;
2378   if (format_max_len == 0)
2379     /* We need to allow space in the memory that we are going to allocate
2380        for the characters in the format string.  Since the format array is
2381        static we only need to calculate this information once.  In theory
2382        this value could also be computed statically, but this introduces
2383        the possibility for a discrepancy and hence a possible memory
2384        corruption.  The lengths we compute here will be too long because
2385        they will include any formating characters (%s) in the strings, but
2386        this will not matter.  */
2387     for (i = 0; libname_fmt[i].format; i++)
2388       if (format_max_len < strlen (libname_fmt[i].format))
2389         format_max_len = strlen (libname_fmt[i].format);
2391   full_string = xmalloc (strlen (search->name)
2392                          + strlen (filename)
2393                          + format_max_len
2394 #ifdef DLL_SUPPORT
2395                          + (pe_dll_search_prefix
2396                             ? strlen (pe_dll_search_prefix) : 0)
2397 #endif
2398                          /* Allow for the terminating NUL and for the path
2399                             separator character that is inserted between
2400                             search->name and the start of the format string.  */
2401                          + 2);
2403   base_string = stpcpy (full_string, search->name);
2404   *base_string++ = '/';
2406   for (i = 0; libname_fmt[i].format; i++)
2407     {
2408 #ifdef DLL_SUPPORT
2409       if (libname_fmt[i].use_prefix)
2410         {
2411           if (!pe_dll_search_prefix)
2412             continue;
2413           sprintf (base_string, libname_fmt[i].format, pe_dll_search_prefix, filename);
2414         }
2415       else
2416 #endif
2417         sprintf (base_string, libname_fmt[i].format, filename);
2419       if (ldfile_try_open_bfd (full_string, entry))
2420         break;
2421     }
2423   if (!libname_fmt[i].format)
2424     {
2425       free (full_string);
2426       return false;
2427     }
2429   entry->filename = full_string;
2431   return true;
2434 static int
2435 gld${EMULATION_NAME}_find_potential_libraries
2436   (char *name, lang_input_statement_type *entry)
2438   return ldfile_open_file_search (name, entry, "", ".lib");
2441 static char *
2442 gld${EMULATION_NAME}_get_script (int *isfile)
2445 if test x"$COMPILE_IN" = xyes
2446 then
2447 # Scripts compiled in.
2449 # sed commands to quote an ld script as a C string.
2450 sc="-f ${srcdir}/emultempl/stringify.sed"
2452 fragment <<EOF
2454   *isfile = 0;
2456   if (bfd_link_relocatable (&link_info) && config.build_constructors)
2457     return
2459 sed $sc ldscripts/${EMULATION_NAME}.xu                  >> e${EMULATION_NAME}.c
2460 echo '  ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
2461 sed $sc ldscripts/${EMULATION_NAME}.xr                  >> e${EMULATION_NAME}.c
2462 echo '  ; else if (!config.text_read_only) return'      >> e${EMULATION_NAME}.c
2463 sed $sc ldscripts/${EMULATION_NAME}.xbn                 >> e${EMULATION_NAME}.c
2464 echo '  ; else if (!config.magic_demand_paged) return'  >> e${EMULATION_NAME}.c
2465 sed $sc ldscripts/${EMULATION_NAME}.xn                  >> e${EMULATION_NAME}.c
2466 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
2467 echo '  ; else if (link_info.pei386_auto_import == 1 && (MERGE_RDATA_V2 || link_info.pei386_runtime_pseudo_reloc != 2)) return' >> e${EMULATION_NAME}.c
2468 sed $sc ldscripts/${EMULATION_NAME}.xa                  >> e${EMULATION_NAME}.c
2470 echo '  ; else return'                                  >> e${EMULATION_NAME}.c
2471 sed $sc ldscripts/${EMULATION_NAME}.x                   >> e${EMULATION_NAME}.c
2472 echo '; }'                                              >> e${EMULATION_NAME}.c
2474 else
2475 # Scripts read from the filesystem.
2477 fragment <<EOF
2479   *isfile = 1;
2481   if (bfd_link_relocatable (&link_info) && config.build_constructors)
2482     return "ldscripts/${EMULATION_NAME}.xu";
2483   else if (bfd_link_relocatable (&link_info))
2484     return "ldscripts/${EMULATION_NAME}.xr";
2485   else if (!config.text_read_only)
2486     return "ldscripts/${EMULATION_NAME}.xbn";
2487   else if (!config.magic_demand_paged)
2488     return "ldscripts/${EMULATION_NAME}.xn";
2490 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
2491 fragment <<EOF
2492   else if (link_info.pei386_auto_import == 1
2493            && (MERGE_RDATA_V2 || link_info.pei386_runtime_pseudo_reloc != 2))
2494     return "ldscripts/${EMULATION_NAME}.xa";
2497 fragment <<EOF
2498   else
2499     return "ldscripts/${EMULATION_NAME}.x";
2504 LDEMUL_AFTER_PARSE=gld${EMULATION_NAME}_after_parse
2505 LDEMUL_BEFORE_PLUGIN_ALL_SYMBOLS_READ=gld${EMULATION_NAME}_before_plugin_all_symbols_read
2506 LDEMUL_AFTER_OPEN=gld${EMULATION_NAME}_after_open
2507 LDEMUL_BEFORE_ALLOCATION=gld${EMULATION_NAME}_before_allocation
2508 LDEMUL_FINISH=gld${EMULATION_NAME}_finish
2509 LDEMUL_OPEN_DYNAMIC_ARCHIVE=gld${EMULATION_NAME}_open_dynamic_archive
2510 LDEMUL_PLACE_ORPHAN=gld${EMULATION_NAME}_place_orphan
2511 LDEMUL_SET_SYMBOLS=gld${EMULATION_NAME}_set_symbols
2512 LDEMUL_ADD_OPTIONS=gld${EMULATION_NAME}_add_options
2513 LDEMUL_HANDLE_OPTION=gld${EMULATION_NAME}_handle_option
2514 LDEMUL_UNRECOGNIZED_FILE=gld${EMULATION_NAME}_unrecognized_file
2515 LDEMUL_LIST_OPTIONS=gld${EMULATION_NAME}_list_options
2516 LDEMUL_RECOGNIZED_FILE=gld${EMULATION_NAME}_recognized_file
2517 LDEMUL_FIND_POTENTIAL_LIBRARIES=gld${EMULATION_NAME}_find_potential_libraries
2519 source_em ${srcdir}/emultempl/emulation.em