Implement tp_richcompare for gdb.Block
[binutils-gdb.git] / ld / emultempl / pe.em
blob9ab5e6ef481cef9dca021b8660a00daed2f2d1b2
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-2024 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     {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
255     {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
256     {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
257     {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
258     {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
259     {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
260     {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
261     {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
262     {"stack", required_argument, NULL, OPTION_STACK},
263     {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
264     {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
265     {"thumb-entry", required_argument, NULL, OPTION_THUMB_ENTRY},
266     {"use-nul-prefixed-import-tables", no_argument, NULL,
267      OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
268     {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
269     {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE},
270     {"insert-timestamp", no_argument, NULL, OPTION_INSERT_TIMESTAMP},
271     {"no-insert-timestamp", no_argument, NULL, OPTION_NO_INSERT_TIMESTAMP},
272 #ifdef DLL_SUPPORT
273     /* getopt allows abbreviations, so we do this to stop it
274        from treating -o as an abbreviation for this option.  */
275     {"output-def", required_argument, NULL, OPTION_OUT_DEF},
276     {"output-def", required_argument, NULL, OPTION_OUT_DEF},
277     {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
278     {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
279     {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS},
280     {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
281     {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
282     {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
283     {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
284     {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
285     {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
286     {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
287     /* getopt() allows abbreviations, so we do this to stop it from
288        treating -c as an abbreviation for these --compat-implib.  */
289     {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
290     {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
291     {"enable-auto-image-base", optional_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
292     {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
293     {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
294     {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
295     {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
296     {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
297     {"enable-extra-pe-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
298     {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
299     {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
300     {"enable-runtime-pseudo-reloc-v1", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1},
301     {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2},
302 #endif
303     {"large-address-aware", no_argument, NULL, OPTION_LARGE_ADDRESS_AWARE},
304     {"disable-large-address-aware", no_argument, NULL, OPTION_DISABLE_LARGE_ADDRESS_AWARE},
305     {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
306     {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
307     {"dynamicbase",no_argument, NULL, OPTION_DYNAMIC_BASE},
308     {"disable-dynamicbase",no_argument, NULL, OPTION_DISABLE_DYNAMIC_BASE},
309     {"forceinteg", no_argument, NULL, OPTION_FORCE_INTEGRITY},
310     {"disable-forceinteg", no_argument, NULL, OPTION_DISABLE_FORCE_INTEGRITY},
311     {"nxcompat", no_argument, NULL, OPTION_NX_COMPAT},
312     {"disable-nxcompat", no_argument, NULL, OPTION_DISABLE_NX_COMPAT},
313     {"no-isolation", no_argument, NULL, OPTION_NO_ISOLATION},
314     {"disable-no-isolation", no_argument, NULL, OPTION_DISABLE_NO_ISOLATION},
315     {"no-seh", no_argument, NULL, OPTION_NO_SEH},
316     {"disable-no-seh", no_argument, NULL, OPTION_DISABLE_NO_SEH},
317     {"no-bind", no_argument, NULL, OPTION_NO_BIND},
318     {"disable-no-bind", no_argument, NULL, OPTION_DISABLE_NO_BIND},
319     {"wdmdriver", no_argument, NULL, OPTION_WDM_DRIVER},
320     {"disable-wdmdriver", no_argument, NULL, OPTION_DISABLE_WDM_DRIVER},
321     {"tsaware", no_argument, NULL, OPTION_TERMINAL_SERVER_AWARE},
322     {"disable-tsaware", no_argument, NULL, OPTION_DISABLE_TERMINAL_SERVER_AWARE},
323     {"build-id", optional_argument, NULL, OPTION_BUILD_ID},
324 #ifdef PDB_H
325     {"pdb", required_argument, NULL, OPTION_PDB},
326 #endif
327     {"enable-reloc-section", no_argument, NULL, OPTION_ENABLE_RELOC_SECTION},
328     {"disable-reloc-section", no_argument, NULL, OPTION_DISABLE_RELOC_SECTION},
329     {NULL, no_argument, NULL, 0}
330   };
332   *longopts
333     = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
334   memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
337 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
338    parameters which may be input from the command line.  */
340 typedef struct
342   void *ptr;
343   int size;
344   int value;
345   char *symbol;
346   int inited;
347   /* FALSE for an assembly level symbol and TRUE for a C visible symbol.
348      C visible symbols can be prefixed by underscore dependent to target's
349      settings.  */
350   bool is_c_symbol;
351 } definfo;
353 /* Get symbol name dependent to kind and C visible state of
354    underscore.  */
355 #define GET_INIT_SYMBOL_NAME(IDX) \
356   (init[(IDX)].symbol \
357    + ((!init[(IDX)].is_c_symbol || is_underscoring () != 0) ? 0 : 1))
359 /* Decorates the C visible symbol by underscore, if target requires.  */
360 #define U(CSTR) \
361   ((is_underscoring () == 0) ? CSTR : "_" CSTR)
363 #define D(field,symbol,def,usc)  {&pe.field, sizeof (pe.field), def, symbol, 0, usc}
365 static definfo init[] =
367   /* imagebase must be first */
368 #define IMAGEBASEOFF 0
369   D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE, false),
370 #define DLLOFF 1
371   {&dll, sizeof(dll), 0, "__dll__", 0, false},
372 #define MSIMAGEBASEOFF  2
373   D(ImageBase, "___ImageBase", NT_EXE_IMAGE_BASE, true),
374   D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT, false),
375   D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT, false),
376   D(MajorOperatingSystemVersion,"__major_os_version__", 4, false),
377   D(MinorOperatingSystemVersion,"__minor_os_version__", 0, false),
378   D(MajorImageVersion,"__major_image_version__", 1, false),
379   D(MinorImageVersion,"__minor_image_version__", 0, false),
380 #if defined(TARGET_IS_armpe)  || defined(TARGET_IS_arm_wince_pe)
381   D(MajorSubsystemVersion,"__major_subsystem_version__", 3, false),
382 #else
383   D(MajorSubsystemVersion,"__major_subsystem_version__", 4, false),
384 #endif
385   D(MinorSubsystemVersion,"__minor_subsystem_version__", 0, false),
386   D(Subsystem,"__subsystem__", ${SUBSYSTEM}, false),
387   D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000, false),
388   D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000, false),
389   D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000, false),
390   D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000, false),
391   D(LoaderFlags,"__loader_flags__", 0x0, false),
392   D(DllCharacteristics, "__dll_characteristics__", DEFAULT_DLL_CHARACTERISTICS, false),
393   { NULL, 0, 0, NULL, 0 , false}
397 static void
398 gld${EMULATION_NAME}_list_options (FILE *file)
400   fprintf (file, _("  --base_file <basefile>             Generate a base file for relocatable DLLs\n"));
401   fprintf (file, _("  --dll                              Set image base to the default for DLLs\n"));
402   fprintf (file, _("  --file-alignment <size>            Set file alignment\n"));
403   fprintf (file, _("  --heap <size>                      Set initial size of the heap\n"));
404   fprintf (file, _("  --image-base <address>             Set start address of the executable\n"));
405   fprintf (file, _("  --major-image-version <number>     Set version number of the executable\n"));
406   fprintf (file, _("  --major-os-version <number>        Set minimum required OS version\n"));
407   fprintf (file, _("  --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
408   fprintf (file, _("  --minor-image-version <number>     Set revision number of the executable\n"));
409   fprintf (file, _("  --minor-os-version <number>        Set minimum required OS revision\n"));
410   fprintf (file, _("  --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
411   fprintf (file, _("  --section-alignment <size>         Set section alignment\n"));
412   fprintf (file, _("  --stack <size>                     Set size of the initial stack\n"));
413   fprintf (file, _("  --subsystem <name>[:<version>]     Set required OS subsystem [& version]\n"));
414   fprintf (file, _("  --support-old-code                 Support interworking with old code\n"));
415   fprintf (file, _("  --[no-]leading-underscore          Set explicit symbol underscore prefix mode\n"));
416   fprintf (file, _("  --thumb-entry=<symbol>             Set the entry point to be Thumb <symbol>\n"));
417   fprintf (file, _("  --[no-]insert-timestamp            Use a real timestamp rather than zero (default).\n"));
418   fprintf (file, _("                                     This makes binaries non-deterministic\n"));
419 #ifdef DLL_SUPPORT
420   fprintf (file, _("  --add-stdcall-alias                Export symbols with and without @nn\n"));
421   fprintf (file, _("  --disable-stdcall-fixup            Don't link _sym to _sym@nn\n"));
422   fprintf (file, _("  --enable-stdcall-fixup             Link _sym to _sym@nn without warnings\n"));
423   fprintf (file, _("  --exclude-symbols sym,sym,...      Exclude symbols from automatic export\n"));
424   fprintf (file, _("  --exclude-all-symbols              Exclude all symbols from automatic export\n"));
425   fprintf (file, _("  --exclude-libs lib,lib,...         Exclude libraries from automatic export\n"));
426   fprintf (file, _("  --exclude-modules-for-implib mod,mod,...\n"));
427   fprintf (file, _("                                     Exclude objects, archive members from auto\n"));
428   fprintf (file, _("                                     export, place into import library instead.\n"));
429   fprintf (file, _("  --export-all-symbols               Automatically export all globals to DLL\n"));
430   fprintf (file, _("  --kill-at                          Remove @nn from exported symbols\n"));
431   fprintf (file, _("  --output-def <file>                Generate a .DEF file for the built DLL\n"));
432   fprintf (file, _("  --warn-duplicate-exports           Warn about duplicate exports\n"));
433   fprintf (file, _("  --compat-implib                    Create backward compatible import libs;\n\
434                                        create __imp_<SYMBOL> as well.\n"));
435   fprintf (file, _("  --enable-auto-image-base[=<address>] Automatically choose image base for DLLs\n\
436                                        (optionally starting with address) unless\n\
437                                        specifically set with --image-base\n"));
438   fprintf (file, _("  --disable-auto-image-base          Do not auto-choose image base. (default)\n"));
439   fprintf (file, _("  --dll-search-prefix=<string>       When linking dynamically to a dll without\n\
440                                        an importlib, use <string><basename>.dll\n\
441                                        in preference to lib<basename>.dll \n"));
442   fprintf (file, _("  --enable-auto-import               Do sophisticated linking of _sym to\n\
443                                        __imp_sym for DATA references\n"));
444   fprintf (file, _("  --disable-auto-import              Do not auto-import DATA items from DLLs\n"));
445   fprintf (file, _("  --enable-runtime-pseudo-reloc      Work around auto-import limitations by\n\
446                                        adding pseudo-relocations resolved at\n\
447                                        runtime.\n"));
448   fprintf (file, _("  --disable-runtime-pseudo-reloc     Do not add runtime pseudo-relocations for\n\
449                                        auto-imported DATA.\n"));
450   fprintf (file, _("  --enable-extra-pe-debug            Enable verbose debug output when building\n\
451                                        or linking to DLLs (esp. auto-import)\n"));
452 #endif
453   fprintf (file, _("  --large-address-aware              Executable supports virtual addresses\n\
454                                        greater than 2 gigabytes\n"));
455   fprintf (file, _("  --disable-large-address-aware      Executable does not support virtual\n\
456                                        addresses greater than 2 gigabytes\n"));
457   fprintf (file, _("  --enable-long-section-names        Use long COFF section names even in\n\
458                                        executable image files\n"));
459   fprintf (file, _("  --disable-long-section-names       Never use long COFF section names, even\n\
460                                        in object files\n"));
461   fprintf (file, _("  --[disable-]dynamicbase            Image base address may be relocated using\n\
462                                        address space layout randomization (ASLR)\n"));
463   fprintf (file, _("  --enable-reloc-section             Create the base relocation table\n"));
464   fprintf (file, _("  --disable-reloc-section            Do not create the base relocation table\n"));
465   fprintf (file, _("  --[disable-]forceinteg             Code integrity checks are enforced\n"));
466   fprintf (file, _("  --[disable-]nxcompat               Image is compatible with data execution\n\
467                                        prevention\n"));
468   fprintf (file, _("  --[disable-]no-isolation           Image understands isolation but do not\n\
469                                        isolate the image\n"));
470   fprintf (file, _("  --[disable-]no-seh                 Image does not use SEH. No SE handler may\n\
471                                        be called in this image\n"));
472   fprintf (file, _("  --[disable-]no-bind                Do not bind this image\n"));
473   fprintf (file, _("  --[disable-]wdmdriver              Driver uses the WDM model\n"));
474   fprintf (file, _("  --[disable-]tsaware                Image is Terminal Server aware\n"));
475   fprintf (file, _("  --build-id[=STYLE]                 Generate build ID\n"));
476 #ifdef PDB_H
477   fprintf (file, _("  --pdb=[FILENAME]                   Generate PDB file\n"));
478 #endif
481 /* A case insensitive comparison, regardless of the host platform, used for
482    comparing file extensions.  */
483 static int
484 fileext_cmp (const char *s1, const char *s2)
486   for (;;)
487     {
488       int c1 = TOLOWER (*s1++);
489       int c2 = *s2++; /* Assumed to be lower case from the caller.  */
491       if (c1 != c2)
492         return (c1 - c2);
494       if (c1 == '\0')
495         return 0;
496     }
499 static void
500 set_pe_name (char *name, long val)
502   int i;
503   is_underscoring ();
505   /* Find the name and set it.  */
506   for (i = 0; init[i].ptr; i++)
507     {
508       if (strcmp (name, GET_INIT_SYMBOL_NAME (i)) == 0)
509         {
510           init[i].value = val;
511           init[i].inited = 1;
512           if (strcmp (name,"__image_base__") == 0)
513             set_pe_name (U ("__ImageBase"), val);
514           return;
515         }
516     }
517   abort ();
520 static void
521 set_entry_point (void)
523   const char *entry;
524   const char *initial_symbol_char;
525   int i;
527   static const struct
528   {
529     const int value;
530     const char *entry;
531   }
532   v[] =
533     {
534       { 1, "NtProcessStartup"  },
535       { 2, "WinMainCRTStartup" },
536       { 3, "mainCRTStartup"    },
537       { 7, "__PosixProcessStartup"},
538       { 9, "WinMainCRTStartup" },
539       {14, "mainCRTStartup"    },
540       { 0, NULL          }
541     };
543   /* Entry point name for arbitrary subsystem numbers.  */
544   static const char default_entry[] = "mainCRTStartup";
546   if (bfd_link_dll (&link_info) || dll)
547     {
548 #if defined (TARGET_IS_i386pe)
549       entry = "DllMainCRTStartup@12";
550 #else
551       entry = "DllMainCRTStartup";
552 #endif
553     }
554   else
555     {
556       for (i = 0; v[i].entry; i++)
557         if (v[i].value == pe_subsystem)
558           break;
560       /* If no match, use the default.  */
561       if (v[i].entry != NULL)
562         entry = v[i].entry;
563       else
564         entry = default_entry;
565     }
567   initial_symbol_char = (is_underscoring () != 0 ? "_" : "");
569   if (*initial_symbol_char != '\0')
570     {
571       char *alc_entry;
573       /* lang_default_entry expects its argument to be permanently
574          allocated, so we don't free this string.  */
575       alc_entry = xmalloc (strlen (initial_symbol_char)
576                            + strlen (entry)
577                            + 1);
578       strcpy (alc_entry, initial_symbol_char);
579       strcat (alc_entry, entry);
580       entry = alc_entry;
581     }
583   lang_default_entry (entry);
585   if (bfd_link_executable (&link_info) && ! entry_from_cmdline)
586     ldlang_add_undef (entry, false);  
589 static void
590 set_pe_subsystem (void)
592   const char *sver;
593   char *end;
594   int len;
595   int i;
596   unsigned long temp_subsystem;
597   static const struct
598     {
599       const char *name;
600       const int value;
601     }
602   v[] =
603     {
604       { "native",  1},
605       { "windows", 2},
606       { "console", 3},
607       { "posix",   7},
608       { "wince",   9},
609       { "xbox",   14},
610       { NULL, 0 }
611     };
613   /* Check for the presence of a version number.  */
614   sver = strchr (optarg, ':');
615   if (sver == NULL)
616     len = strlen (optarg);
617   else
618     {
619       len = sver - optarg;
620       set_pe_name ("__major_subsystem_version__",
621                     strtoul (sver + 1, &end, 0));
622       if (*end == '.')
623         set_pe_name ("__minor_subsystem_version__",
624                       strtoul (end + 1, &end, 0));
625       if (*end != '\0')
626         einfo (_("%P: warning: bad version number in -subsystem option\n"));
627     }
629   /* Check for numeric subsystem.  */
630   temp_subsystem = strtoul (optarg, & end, 0);
631   if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
632     {
633       /* Search list for a numeric match to use its entry point.  */
634       for (i = 0; v[i].name; i++)
635         if (v[i].value == (int) temp_subsystem)
636           break;
638       /* Use this subsystem.  */
639       pe_subsystem = (int) temp_subsystem;
640     }
641   else
642     {
643       /* Search for subsystem by name.  */
644       for (i = 0; v[i].name; i++)
645         if (strncmp (optarg, v[i].name, len) == 0
646             && v[i].name[len] == '\0')
647           break;
649       if (v[i].name == NULL)
650         {
651           einfo (_("%F%P: invalid subsystem type %s\n"), optarg);
652           return;
653         }
655       pe_subsystem = v[i].value;
656     }
658   set_pe_name ("__subsystem__", pe_subsystem);
660   return;
664 static void
665 set_pe_value (char *name)
667   char *end;
669   set_pe_name (name,  strtoul (optarg, &end, 0));
671   if (end == optarg)
672     einfo (_("%F%P: invalid hex number for PE parameter '%s'\n"), optarg);
674   optarg = end;
678 static void
679 set_pe_stack_heap (char *resname, char *comname)
681   set_pe_value (resname);
683   if (*optarg == ',')
684     {
685       optarg++;
686       set_pe_value (comname);
687     }
688   else if (*optarg)
689     einfo (_("%F%P: strange hex info for PE parameter '%s'\n"), optarg);
692 #define DEFAULT_BUILD_ID_STYLE  "md5"
694 static bool
695 gld${EMULATION_NAME}_handle_option (int optc)
697   switch (optc)
698     {
699     default:
700       return false;
702     case OPTION_BASE_FILE:
703       link_info.base_file = fopen (optarg, FOPEN_WB);
704       if (link_info.base_file == NULL)
705         einfo (_("%F%P: cannot open base file %s\n"), optarg);
706       break;
708       /* PE options.  */
709     case OPTION_HEAP:
710       set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
711       break;
712     case OPTION_STACK:
713       set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
714       break;
715     case OPTION_SUBSYSTEM:
716       set_pe_subsystem ();
717       break;
718     case OPTION_MAJOR_OS_VERSION:
719       set_pe_value ("__major_os_version__");
720       break;
721     case OPTION_MINOR_OS_VERSION:
722       set_pe_value ("__minor_os_version__");
723       break;
724     case OPTION_MAJOR_SUBSYSTEM_VERSION:
725       set_pe_value ("__major_subsystem_version__");
726       break;
727     case OPTION_MINOR_SUBSYSTEM_VERSION:
728       set_pe_value ("__minor_subsystem_version__");
729       break;
730     case OPTION_MAJOR_IMAGE_VERSION:
731       set_pe_value ("__major_image_version__");
732       break;
733     case OPTION_MINOR_IMAGE_VERSION:
734       set_pe_value ("__minor_image_version__");
735       break;
736     case OPTION_FILE_ALIGNMENT:
737       set_pe_value ("__file_alignment__");
738       break;
739     case OPTION_SECTION_ALIGNMENT:
740       set_pe_value ("__section_alignment__");
741       break;
742     case OPTION_DLL:
743       set_pe_name ("__dll__", 1);
744       break;
745     case OPTION_IMAGE_BASE:
746       set_pe_value ("__image_base__");
747       break;
748     case OPTION_SUPPORT_OLD_CODE:
749       support_old_code = 1;
750       break;
751     case OPTION_THUMB_ENTRY:
752       thumb_entry_symbol = optarg;
753       break;
754     case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
755       pe_use_nul_prefixed_import_tables = true;
756       break;
757     case OPTION_NO_LEADING_UNDERSCORE:
758       pe_leading_underscore = 0;
759       break;
760     case OPTION_LEADING_UNDERSCORE:
761       pe_leading_underscore = 1;
762       break;
763     case OPTION_INSERT_TIMESTAMP:
764       insert_timestamp = true;
765       break;
766     case OPTION_NO_INSERT_TIMESTAMP:
767       insert_timestamp = false;
768       break;
769 #ifdef DLL_SUPPORT
770     case OPTION_OUT_DEF:
771       pe_out_def_filename = xstrdup (optarg);
772       break;
773     case OPTION_EXPORT_ALL:
774       pe_dll_export_everything = 1;
775       break;
776     case OPTION_EXCLUDE_SYMBOLS:
777       pe_dll_add_excludes (optarg, EXCLUDESYMS);
778       break;
779     case OPTION_EXCLUDE_ALL_SYMBOLS:
780       pe_dll_exclude_all_symbols = 1;
781       break;
782     case OPTION_EXCLUDE_LIBS:
783       pe_dll_add_excludes (optarg, EXCLUDELIBS);
784       break;
785     case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
786       pe_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
787       break;
788     case OPTION_KILL_ATS:
789       pe_dll_kill_ats = 1;
790       break;
791     case OPTION_STDCALL_ALIASES:
792       pe_dll_stdcall_aliases = 1;
793       break;
794     case OPTION_ENABLE_STDCALL_FIXUP:
795       pe_enable_stdcall_fixup = 1;
796       break;
797     case OPTION_DISABLE_STDCALL_FIXUP:
798       pe_enable_stdcall_fixup = 0;
799       break;
800     case OPTION_WARN_DUPLICATE_EXPORTS:
801       pe_dll_warn_dup_exports = 1;
802       break;
803     case OPTION_IMP_COMPAT:
804       pe_dll_compat_implib = 1;
805       break;
806     case OPTION_ENABLE_AUTO_IMAGE_BASE:
807       pe_enable_auto_image_base = 1;
808       if (optarg && *optarg)
809         {
810           char *end;
811           pe_auto_image_base = strtoul (optarg, &end, 0);
812           /* XXX should check that we actually parsed something */
813         }
814       break;
815     case OPTION_DISABLE_AUTO_IMAGE_BASE:
816       pe_enable_auto_image_base = 0;
817       break;
818     case OPTION_DLL_SEARCH_PREFIX:
819       pe_dll_search_prefix = xstrdup (optarg);
820       break;
821     case OPTION_NO_DEFAULT_EXCLUDES:
822       pe_dll_do_default_excludes = 0;
823       break;
824     case OPTION_DLL_ENABLE_AUTO_IMPORT:
825       link_info.pei386_auto_import = 1;
826       break;
827     case OPTION_DLL_DISABLE_AUTO_IMPORT:
828       link_info.pei386_auto_import = 0;
829       break;
830     case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
831       link_info.pei386_runtime_pseudo_reloc =
832         DEFAULT_PSEUDO_RELOC_VERSION;
833       break;
834     case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1:
835       link_info.pei386_runtime_pseudo_reloc = 1;
836       break;
837     case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2:
838       link_info.pei386_runtime_pseudo_reloc = 2;
839       break;
840     case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
841       link_info.pei386_runtime_pseudo_reloc = 0;
842       break;
843     case OPTION_ENABLE_EXTRA_PE_DEBUG:
844       pe_dll_extra_pe_debug = 1;
845       break;
846 #endif
847     case OPTION_LARGE_ADDRESS_AWARE:
848       real_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
849       break;
850     case OPTION_DISABLE_LARGE_ADDRESS_AWARE:
851       real_flags &= ~ IMAGE_FILE_LARGE_ADDRESS_AWARE;
852       break;
853     case OPTION_ENABLE_LONG_SECTION_NAMES:
854       pe_use_coff_long_section_names = 1;
855       break;
856     case OPTION_DISABLE_LONG_SECTION_NAMES:
857       pe_use_coff_long_section_names = 0;
858       break;
859 /*  Get DLLCharacteristics bits  */
860     case OPTION_DYNAMIC_BASE:
861       pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
862       /* fall through */
863     case OPTION_ENABLE_RELOC_SECTION:
864       pe_dll_enable_reloc_section = 1;
865       break;
866     case OPTION_DISABLE_RELOC_SECTION:
867       pe_dll_enable_reloc_section = 0;
868       /* fall through */
869     case OPTION_DISABLE_DYNAMIC_BASE:
870       pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
871       break;
872     case OPTION_FORCE_INTEGRITY:
873       pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
874       break;
875     case OPTION_DISABLE_FORCE_INTEGRITY:
876       pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
877       break;
878     case OPTION_NX_COMPAT:
879       pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
880       break;
881     case OPTION_DISABLE_NX_COMPAT:
882       pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
883       break;
884     case OPTION_NO_ISOLATION:
885       pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
886       break;
887     case OPTION_DISABLE_NO_ISOLATION:
888       pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
889       break;
890     case OPTION_NO_SEH:
891       pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_SEH;
892       break;
893     case OPTION_DISABLE_NO_SEH:
894       pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_SEH;
895       break;
896     case OPTION_NO_BIND:
897       pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_BIND;
898       break;
899     case OPTION_DISABLE_NO_BIND:
900       pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_BIND;
901       break;
902     case OPTION_WDM_DRIVER:
903       pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
904       break;
905     case OPTION_DISABLE_WDM_DRIVER:
906       pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
907       break;
908     case OPTION_TERMINAL_SERVER_AWARE:
909       pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
910       break;
911     case OPTION_DISABLE_TERMINAL_SERVER_AWARE:
912       pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
913       break;
914     case OPTION_BUILD_ID:
915       free ((char *) emit_build_id);
916       emit_build_id = NULL;
917       if (optarg == NULL)
918         optarg = DEFAULT_BUILD_ID_STYLE;
919       if (strcmp (optarg, "none"))
920         emit_build_id = xstrdup (optarg);
921       break;
922 #ifdef PDB_H
923     case OPTION_PDB:
924       pdb = 1;
925       if (optarg && optarg[0])
926         pdb_name = xstrdup (optarg);
927       break;
928 #endif
929     }
931   /*  Set DLLCharacteristics bits  */
932   set_pe_name ("__dll_characteristics__", pe_dll_characteristics);
934   return true;
938 #ifdef DLL_SUPPORT
939 static unsigned long
940 strhash (const char *str)
942   const unsigned char *s;
943   unsigned long hash;
944   unsigned int c;
945   unsigned int len;
947   hash = 0;
948   len = 0;
949   s = (const unsigned char *) str;
950   while ((c = *s++) != '\0')
951     {
952       hash += c + (c << 17);
953       hash ^= hash >> 2;
954       ++len;
955     }
956   hash += len + (len << 17);
957   hash ^= hash >> 2;
959   return hash;
962 /* Use the output file to create a image base for relocatable DLLs.  */
964 static unsigned long
965 compute_dll_image_base (const char *ofile)
967   unsigned long hash = strhash (ofile);
968   return pe_auto_image_base + ((hash << 16) & 0x0FFC0000);
970 #endif
972 /* Assign values to the special symbols before the linker script is
973    read.  */
975 static void
976 gld${EMULATION_NAME}_set_symbols (void)
978   /* Run through and invent symbols for all the
979      names and insert the defaults.  */
980   int j;
982   is_underscoring ();
984   if (!init[IMAGEBASEOFF].inited)
985     {
986       if (bfd_link_relocatable (&link_info))
987         init[IMAGEBASEOFF].value = 0;
988       else if (init[DLLOFF].value || bfd_link_dll (&link_info))
989         {
990 #ifdef DLL_SUPPORT
991           init[IMAGEBASEOFF].value = (pe_enable_auto_image_base
992                                       ? compute_dll_image_base (output_filename)
993                                       : NT_DLL_IMAGE_BASE);
994 #else
995           init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
996 #endif
997         }
998       else
999         init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
1000       init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
1001     }
1003   /* Don't do any symbol assignments if this is a relocatable link.  */
1004   if (bfd_link_relocatable (&link_info))
1005     return;
1007   /* Glue the assignments into the abs section.  */
1008   push_stat_ptr (&abs_output_section->children);
1010   for (j = 0; init[j].ptr; j++)
1011     {
1012       long val = init[j].value;
1013       lang_assignment_statement_type *rv;
1015       rv = lang_add_assignment (exp_assign (GET_INIT_SYMBOL_NAME (j),
1016                                             exp_intop (val), false));
1017       if (init[j].size == sizeof (short))
1018         *(short *) init[j].ptr = val;
1019       else if (init[j].size == sizeof (int))
1020         *(int *) init[j].ptr = val;
1021       else if (init[j].size == sizeof (long))
1022         *(long *) init[j].ptr = val;
1023       /* This might be a long long or other special type.  */
1024       else if (init[j].size == sizeof (bfd_vma))
1025         *(bfd_vma *) init[j].ptr = val;
1026       else      abort ();
1027       if (j == IMAGEBASEOFF)
1028         image_base_statement = rv;
1029     }
1030   /* Restore the pointer.  */
1031   pop_stat_ptr ();
1033   if (pe.FileAlignment > pe.SectionAlignment)
1034     {
1035       einfo (_("%P: warning, file alignment > section alignment\n"));
1036     }
1039 /* This is called after the linker script and the command line options
1040    have been read.  */
1042 static void
1043 gld${EMULATION_NAME}_after_parse (void)
1045   /* PR ld/6744:  Warn the user if they have used an ELF-only
1046      option hoping it will work on PE.  */
1047   if (link_info.export_dynamic)
1048     einfo (_("%P: warning: --export-dynamic is not supported for PE "
1049       "targets, did you mean --export-all-symbols?\n"));
1051 #ifdef PDB_H
1052   if (pdb && emit_build_id == NULL)
1053     emit_build_id = xstrdup (DEFAULT_BUILD_ID_STYLE);
1054 #endif
1056   set_entry_point ();
1058   after_parse_default ();
1061 #ifdef DLL_SUPPORT
1062 static struct bfd_link_hash_entry *pe_undef_found_sym;
1064 static bool
1065 pe_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
1067   int sl;
1068   char *string = inf;
1069   const char *hs = h->root.string;
1071   sl = strlen (string);
1072   if (h->type == bfd_link_hash_defined
1073       && ((*hs == '@' && *string == '_'
1074                    && strncmp (hs + 1, string + 1, sl - 1) == 0)
1075                   || strncmp (hs, string, sl) == 0)
1076       && h->root.string[sl] == '@')
1077     {
1078       pe_undef_found_sym = h;
1079       return false;
1080     }
1081   return true;
1084 /* Change UNDEF to a defined symbol, taking data from SYM.  */
1086 static void
1087 change_undef (struct bfd_link_hash_entry * undef,
1088               struct bfd_link_hash_entry * sym)
1090   static bool  gave_warning_message = false;
1092   undef->type = bfd_link_hash_defined;
1093   undef->u.def.value = sym->u.def.value;
1094   undef->u.def.section = sym->u.def.section;
1096   if (pe_enable_stdcall_fixup == -1)
1097     {
1098       einfo (_("%P: warning: resolving %s by linking to %s\n"),
1099              undef->root.string, sym->root.string);
1101       if (! gave_warning_message)
1102         {
1103           einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
1104           einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
1105           gave_warning_message = true;
1106         }
1107     }
1109   /* PR 19803: Make sure that the linked symbol is not garbage collected.  */
1110   lang_add_gc_name (sym->root.string);
1113 static void
1114 set_decoration (const char *undecorated_name,
1115                 struct bfd_link_hash_entry * decoration)
1117   static bool  gave_warning_message = false;
1118   struct decoration_hash_entry *entry;
1120   if (is_underscoring () && undecorated_name[0] == '_')
1121     undecorated_name++;
1123   entry = (struct decoration_hash_entry *)
1124           bfd_hash_lookup (&(coff_hash_table (&link_info)->decoration_hash),
1125                            undecorated_name, true /* create */, false /* copy */);
1127   if (entry->decorated_link != NULL && !gave_warning_message)
1128     {
1129       einfo (_("%P: warning: overwriting decorated name %s with %s\n"),
1130              entry->decorated_link->root.string, undecorated_name);
1131       gave_warning_message = true;
1132     }
1134   entry->decorated_link = decoration;
1137 static void
1138 pe_fixup_stdcalls (void)
1140   struct bfd_link_hash_entry *undef, *sym;
1142   if (pe_dll_extra_pe_debug)
1143     printf ("%s\n", __func__);
1145   for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
1146     if (undef->type == bfd_link_hash_undefined)
1147       {
1148         const char * name = undef->root.string;
1149         char * at;
1150         int lead_at = (*name == '@');
1152         if (lead_at)
1153           at = strchr (name + 1, '@');
1154         else
1155           at = strchr (name, '@');
1157         if (at || lead_at)
1158           {
1159             /* The symbol is a stdcall symbol, so let's look for a
1160                cdecl symbol with the same name and resolve to that.  */
1161             char *cname = xstrdup (name);
1163             if (lead_at)
1164               *cname = '_';
1165             if (at)
1166               * strchr (cname, '@') = 0;
1167             sym = bfd_link_hash_lookup (link_info.hash, cname, false, false, true);
1169             if (sym && sym->type == bfd_link_hash_defined)
1170               change_undef (undef, sym);
1171           }
1172         else
1173           {
1174             /* The symbol is a cdecl symbol, so we look for stdcall
1175                symbols - which means scanning the whole symbol table.  */
1176             pe_undef_found_sym = NULL;
1177             bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
1178                                     (char *) name);
1179             if (pe_undef_found_sym)
1180               {
1181                 change_undef (undef, pe_undef_found_sym);
1182                 set_decoration (undef->root.string, pe_undef_found_sym);
1183               }
1184           }
1185       }
1188 static bfd_vma
1189 read_addend (arelent *rel, asection *s)
1191   char buf[4];
1192   bfd_vma addend = 0;
1194   if (!bfd_get_section_contents (s->owner, s, buf, rel->address, sizeof (buf)))
1195     einfo (_("%P: %H: cannot get section contents - auto-import exception\n"),
1196            s->owner, s, rel->address);
1197   else
1198     addend = bfd_get_32 (s->owner, buf);
1199   return addend;
1202 static void
1203 make_import_fixup (arelent *rel, asection *s, char *name, const char *symname)
1205   struct bfd_symbol *sym = *rel->sym_ptr_ptr;
1206   bfd_vma addend;
1208   if (pe_dll_extra_pe_debug)
1209     printf ("arelent: %s@%#lx: add=%li\n", sym->name,
1210             (unsigned long) rel->address, (long) rel->addend);
1212   addend = read_addend (rel, s);
1214   if (pe_dll_extra_pe_debug)
1215     {
1216       printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx",
1217               (long) addend, (long) rel->addend, (long) rel->address);
1218       if (rel->howto->pc_relative)
1219         printf (" pcrel");
1220       printf (" %d bit rel.\n", (int) rel->howto->bitsize);
1221     }
1223   pe_create_import_fixup (rel, s, addend, name, symname);
1226 static void
1227 make_runtime_ref (void)
1229   const char *rr = U ("_pei386_runtime_relocator");
1230   struct bfd_link_hash_entry *h
1231     = bfd_wrapped_link_hash_lookup (link_info.output_bfd, &link_info,
1232                                     rr, true, false, true);
1233   if (!h)
1234     einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
1235   else
1236     {
1237       if (h->type == bfd_link_hash_new)
1238         {
1239           h->type = bfd_link_hash_undefined;
1240           h->u.undef.abfd = NULL;
1241           if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
1242             bfd_link_add_undef (link_info.hash, h);
1243         }
1244       h->non_ir_ref_regular = true;
1245     }
1248 static bool
1249 pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
1251   printf ("+%s\n", h->string);
1253   return true;
1255 #endif /* DLL_SUPPORT */
1257 static void
1258 debug_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
1260   int *found = (int *) obj;
1261   if (strncmp (".debug_", sect->name, sizeof (".debug_") - 1) == 0)
1262     *found = 1;
1265 static bool
1266 pecoff_checksum_contents (bfd *abfd,
1267                           void (*process) (const void *, size_t, void *),
1268                           void *arg)
1270   file_ptr filepos = (file_ptr) 0;
1272   while (1)
1273     {
1274       unsigned char b;
1275       int status;
1277       if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
1278         return 0;
1280       status = bfd_read (&b, 1, abfd);
1281       if (status < 1)
1282         {
1283           break;
1284         }
1286       (*process) (&b, 1, arg);
1287       filepos += 1;
1288     }
1290   return true;
1293 static bool
1294 write_build_id (bfd *abfd)
1296   struct pe_tdata *td = pe_data (abfd);
1297   asection *asec;
1298   struct bfd_link_order *link_order = NULL;
1299   unsigned char *contents;
1300   bfd_size_type build_id_size;
1301   unsigned char *build_id;
1302   const char *pdb_base_name = NULL;
1304   /* Find the section the .buildid output section has been merged info.  */
1305   for (asec = abfd->sections; asec != NULL; asec = asec->next)
1306     {
1307       struct bfd_link_order *l = NULL;
1308       for (l = asec->map_head.link_order; l != NULL; l = l->next)
1309         {
1310           if (l->type == bfd_indirect_link_order)
1311             {
1312               if (l->u.indirect.section == td->build_id.sec)
1313                 {
1314                   link_order = l;
1315                   break;
1316                 }
1317             }
1318         }
1320       if (link_order)
1321         break;
1322     }
1324   if (!link_order)
1325     {
1326       einfo (_("%P: warning: .buildid section discarded,"
1327                " --build-id ignored\n"));
1328       return true;
1329     }
1331   if (td->build_id.sec->contents == NULL)
1332     td->build_id.sec->contents = xmalloc (td->build_id.sec->size);
1333   contents = td->build_id.sec->contents;
1335   build_id_size = compute_build_id_size (td->build_id.style);
1336   build_id = xmalloc (build_id_size);
1337   generate_build_id (abfd, td->build_id.style, pecoff_checksum_contents,
1338                      build_id, build_id_size);
1340   bfd_vma ib = td->pe_opthdr.ImageBase;
1342 #ifdef PDB_H
1343   if (pdb_name)
1344     pdb_base_name = lbasename (pdb_name);
1345 #endif
1347   /* Construct a debug directory entry which points to an immediately following CodeView record.  */
1348   struct internal_IMAGE_DEBUG_DIRECTORY idd;
1349   idd.Characteristics = 0;
1350   idd.TimeDateStamp = 0;
1351   idd.MajorVersion = 0;
1352   idd.MinorVersion = 0;
1353   idd.Type = PE_IMAGE_DEBUG_TYPE_CODEVIEW;
1354   idd.SizeOfData = (sizeof (CV_INFO_PDB70)
1355 #ifdef PDB_H
1356                     + (pdb_base_name ? strlen (pdb_base_name) : 0)
1357 #endif
1358                     + 1);
1359   idd.AddressOfRawData = asec->vma - ib + link_order->offset
1360     + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1361   idd.PointerToRawData = asec->filepos + link_order->offset
1362     + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1364   struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *)contents;
1365   _bfd_XXi_swap_debugdir_out (abfd, &idd, ext);
1367   /* Write the debug directory entry.  */
1368   if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0)
1369     return 0;
1371   if (bfd_write (contents, sizeof (*ext), abfd) != sizeof (*ext))
1372     return 0;
1374 #ifdef PDB_H
1375   if (pdb)
1376     {
1377       if (!create_pdb_file (abfd, pdb_name, build_id))
1378         return 0;
1379     }
1380 #endif
1382   /* Construct the CodeView record.  */
1383   CODEVIEW_INFO cvinfo;
1384   cvinfo.CVSignature = CVINFO_PDB70_CVSIGNATURE;
1385   cvinfo.Age = 1;
1387   /* Zero pad or truncate the generated build_id to fit in the
1388      CodeView record.  */
1389   memset (&(cvinfo.Signature), 0, CV_INFO_SIGNATURE_LENGTH);
1390   memcpy (&(cvinfo.Signature), build_id,
1391           (build_id_size > CV_INFO_SIGNATURE_LENGTH
1392            ? CV_INFO_SIGNATURE_LENGTH : build_id_size));
1394   free (build_id);
1396   /* Write the codeview record.  */
1397   if (_bfd_XXi_write_codeview_record (abfd, idd.PointerToRawData, &cvinfo,
1398                                       pdb_base_name) == 0)
1399     return 0;
1401   /* Record the location of the debug directory in the data directory.  */
1402   td->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress
1403     = asec->vma - ib + link_order->offset;
1404   td->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
1405     = sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1407   return true;
1410 /* Make .buildid section, and set up coff_tdata->build_id. */
1411 static bool
1412 setup_build_id (bfd *ibfd)
1414   asection *s;
1415   flagword flags;
1417   if (!validate_build_id_style (emit_build_id))
1418     {
1419       einfo (_("%P: warning: unrecognized --build-id style ignored\n"));
1420       return false;
1421     }
1423   flags = (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY
1424            | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA);
1425   s = bfd_make_section_anyway_with_flags (ibfd, ".buildid", flags);
1426   if (s != NULL)
1427     {
1428       struct pe_tdata *td = pe_data (link_info.output_bfd);
1429       td->build_id.after_write_object_contents = &write_build_id;
1430       td->build_id.style = emit_build_id;
1431       td->build_id.sec = s;
1433       /* Section is a fixed size:
1434          One IMAGE_DEBUG_DIRECTORY entry, of type IMAGE_DEBUG_TYPE_CODEVIEW,
1435          pointing at a CV_INFO_PDB70 record containing the build-id, followed by
1436          PdbFileName if relevant.  */
1437       s->size = (sizeof (struct external_IMAGE_DEBUG_DIRECTORY)
1438                  + sizeof (CV_INFO_PDB70) + 1);
1440 #ifdef PDB_H
1441       if (pdb_name)
1442         s->size += strlen (lbasename (pdb_name));
1443 #endif
1444       return true;
1445     }
1447   einfo (_("%P: warning: cannot create .buildid section,"
1448            " --build-id ignored\n"));
1449   return false;
1452 static void
1453 gld${EMULATION_NAME}_before_plugin_all_symbols_read (void)
1455 #ifdef DLL_SUPPORT
1456   if (link_info.lto_plugin_active
1457       && link_info.pei386_auto_import)
1458     make_runtime_ref ();
1459 #endif
1462 static void
1463 gld${EMULATION_NAME}_after_open (void)
1465   after_open_default ();
1467 #ifdef DLL_SUPPORT
1468   if (pe_dll_extra_pe_debug)
1469     {
1470       bfd *a;
1471       struct bfd_link_hash_entry *sym;
1473       printf ("%s()\n", __func__);
1475       for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1476         printf ("-%s\n", sym->root.string);
1477       bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
1479       for (a = link_info.input_bfds; a; a = a->link.next)
1480         printf ("*%s\n", bfd_get_filename (a));
1481     }
1482 #endif
1484 #ifdef PDB_H
1485   if (pdb && !pdb_name)
1486     {
1487       const char *base = lbasename (bfd_get_filename (link_info.output_bfd));
1488       size_t len = strlen (base);
1489       static const char suffix[] = ".pdb";
1491       while (len > 0 && base[len] != '.')
1492         {
1493           len--;
1494         }
1496       if (len == 0)
1497         len = strlen (base);
1499       pdb_name = xmalloc (len + sizeof (suffix));
1500       memcpy (pdb_name, base, len);
1501       memcpy (pdb_name + len, suffix, sizeof (suffix));
1502     }
1503 #endif
1505   if (emit_build_id != NULL)
1506     {
1507       bfd *abfd;
1509       /* Find a COFF input.  */
1510       for (abfd = link_info.input_bfds;
1511            abfd != (bfd *) NULL; abfd = abfd->link.next)
1512         if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
1513           break;
1515       /* If there are no COFF input files do not try to
1516          add a build-id section.  */
1517       if (abfd == NULL
1518           || !setup_build_id (abfd))
1519         {
1520           free ((char *) emit_build_id);
1521           emit_build_id = NULL;
1522         }
1523     }
1525   /* Pass the wacky PE command line options into the output bfd.
1526      FIXME: This should be done via a function, rather than by
1527      including an internal BFD header.  */
1529   if (bfd_get_flavour (link_info.output_bfd) != bfd_target_coff_flavour
1530       || coff_data (link_info.output_bfd) == NULL
1531       || !obj_pe (link_info.output_bfd))
1532     einfo (_("%F%P: cannot perform PE operations on non PE output file '%pB'\n"),
1533            link_info.output_bfd);
1535   pe_data (link_info.output_bfd)->pe_opthdr = pe;
1536   pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
1537   pe_data (link_info.output_bfd)->real_flags |= real_flags;
1538   if (insert_timestamp)
1539     pe_data (link_info.output_bfd)->timestamp = -1;
1540   else
1541     pe_data (link_info.output_bfd)->timestamp = 0;
1543   /* At this point we must decide whether to use long section names
1544      in the output or not.  If the user hasn't explicitly specified
1545      on the command line, we leave it to the default for the format
1546      (object files yes, image files no), except if there is debug
1547      information present; GDB relies on the long section names to
1548      find it, so enable it in that case.  */
1549   if (pe_use_coff_long_section_names < 0 && link_info.strip == strip_none)
1550     {
1551       if (bfd_link_relocatable (&link_info))
1552         pe_use_coff_long_section_names = 1;
1553       else
1554         {
1555           /* Iterate over all sections of all input BFDs, checking
1556              for any that begin 'debug_' and are long names.  */
1557           LANG_FOR_EACH_INPUT_STATEMENT (is)
1558           {
1559             int found_debug = 0;
1561             bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug);
1562             if (found_debug)
1563               {
1564                 pe_use_coff_long_section_names = 1;
1565                 break;
1566               }
1567           }
1568         }
1569     }
1571   pe_output_file_set_long_section_names (link_info.output_bfd);
1573 #ifdef DLL_SUPPORT
1574   pe_process_import_defs (link_info.output_bfd, &link_info);
1576   if (link_info.pei386_auto_import) /* -1=warn or 1=enable */
1577     pe_find_data_imports (U ("_head_"), make_import_fixup);
1579   /* The implementation of the feature is rather dumb and would cause the
1580      compilation time to go through the roof if there are many undefined
1581      symbols in the link, so it needs to be run after auto-import.  */
1582   if (pe_enable_stdcall_fixup) /* -1=warn or 1=enable */
1583     pe_fixup_stdcalls ();
1585 #if defined (TARGET_IS_i386pe) \
1586     || defined (TARGET_IS_armpe) \
1587     || defined (TARGET_IS_arm_wince_pe)
1588   if (!bfd_link_relocatable (&link_info))
1589     pe_dll_build_sections (link_info.output_bfd, &link_info);
1590 #else
1591   if (bfd_link_pic (&link_info))
1592     pe_dll_build_sections (link_info.output_bfd, &link_info);
1593   else
1594     pe_exe_build_sections (link_info.output_bfd, &link_info);
1595 #endif
1596 #endif /* DLL_SUPPORT */
1598 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
1599   if (strstr (bfd_get_target (link_info.output_bfd), "arm") == NULL)
1600     {
1601       /* The arm backend needs special fields in the output hash structure.
1602          These will only be created if the output format is an arm format,
1603          hence we do not support linking and changing output formats at the
1604          same time.  Use a link followed by objcopy to change output formats.  */
1605       einfo (_("%F%P: error: cannot change output format "
1606                "whilst linking %s binaries\n"), "ARM");
1607       return;
1608     }
1609   {
1610     /* Find a BFD that can hold the interworking stubs.  */
1611     LANG_FOR_EACH_INPUT_STATEMENT (is)
1612       {
1613         if (bfd_arm_get_bfd_for_interworking (is->the_bfd, & link_info))
1614           break;
1615       }
1616   }
1617 #endif
1619   {
1620     /* This next chunk of code tries to detect the case where you have
1621        two import libraries for the same DLL (specifically,
1622        symbolically linking libm.a and libc.a in cygwin to
1623        libcygwin.a).  In those cases, it's possible for function
1624        thunks from the second implib to be used but without the
1625        head/tail objects, causing an improper import table.  We detect
1626        those cases and rename the "other" import libraries to match
1627        the one the head/tail come from, so that the linker will sort
1628        things nicely and produce a valid import table.  */
1630     LANG_FOR_EACH_INPUT_STATEMENT (is)
1631       {
1632         if (is->the_bfd->my_archive)
1633           {
1634             int idata2 = 0, reloc_count=0, is_imp = 0;
1635             asection *sec;
1637             /* See if this is an import library thunk.  */
1638             for (sec = is->the_bfd->sections; sec; sec = sec->next)
1639               {
1640                 if (strcmp (sec->name, ".idata\$2") == 0)
1641                   idata2 = 1;
1642                 if (startswith (sec->name, ".idata\$"))
1643                   is_imp = 1;
1644                 reloc_count += sec->reloc_count;
1645               }
1647             if (is_imp && !idata2 && reloc_count)
1648               {
1649                 /* It is, look for the reference to head and see if it's
1650                    from our own library.  */
1651                 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1652                   {
1653                     int i;
1654                     long relsize;
1655                     asymbol **symbols;
1656                     arelent **relocs;
1657                     int nrelocs;
1659                     relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1660                     if (relsize < 1)
1661                       break;
1663                     if (!bfd_generic_link_read_symbols (is->the_bfd))
1664                       {
1665                         einfo (_("%F%P: %pB: could not read symbols: %E\n"),
1666                                is->the_bfd);
1667                         return;
1668                       }
1669                     symbols = bfd_get_outsymbols (is->the_bfd);
1671                     relocs = xmalloc ((size_t) relsize);
1672                     nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1673                                                       relocs, symbols);
1674                     if (nrelocs < 0)
1675                       {
1676                         free (relocs);
1677                         einfo (_("%X%P: unable to process relocs: %E\n"));
1678                         return;
1679                       }
1681                     for (i = 0; i < nrelocs; i++)
1682                       {
1683                         struct bfd_symbol *s;
1684                         struct bfd_link_hash_entry * blhe;
1685                         bfd *other_bfd;
1686                         const char *other_bfd_filename;
1688                         s = (relocs[i]->sym_ptr_ptr)[0];
1690                         if (s->flags & BSF_LOCAL)
1691                           continue;
1693                         /* Thunk section with reloc to another bfd.  */
1694                         blhe = bfd_link_hash_lookup (link_info.hash,
1695                                                      s->name,
1696                                                      false, false, true);
1698                         if (blhe == NULL
1699                             || blhe->type != bfd_link_hash_defined)
1700                           continue;
1702                         other_bfd = blhe->u.def.section->owner;
1703                         if (other_bfd->my_archive == is->the_bfd->my_archive)
1704                           continue;
1706                         other_bfd_filename
1707                           = (other_bfd->my_archive
1708                              ? bfd_get_filename (other_bfd->my_archive)
1709                              : bfd_get_filename (other_bfd));
1711                         if (filename_cmp (bfd_get_filename
1712                                             (is->the_bfd->my_archive),
1713                                           other_bfd_filename) == 0)
1714                           continue;
1716                         /* Sort this implib to match the other one.  */
1717                         lang_input_statement_type *arch_is
1718                           = bfd_usrdata (is->the_bfd->my_archive);
1719                         arch_is->sort_key = other_bfd_filename;
1720                         break;
1721                       }
1723                     free (relocs);
1724                     /* Note - we do not free the symbols,
1725                        they are now cached in the BFD.  */
1726                   }
1727               }
1728           }
1729       }
1730   }
1732   {
1734     /* Careful - this is a shell script.  Watch those dollar signs! */
1735     /* Microsoft import libraries have every member named the same,
1736        and not in the right order for us to link them correctly.  We
1737        must detect these and rename the members so that they'll link
1738        correctly.  There are three types of objects: the head, the
1739        thunks, and the sentinel(s).  The head is easy; it's the one
1740        with idata2.  We assume that the sentinels won't have relocs,
1741        and the thunks will.  It's easier than checking the symbol
1742        table for external references.  */
1743     LANG_FOR_EACH_INPUT_STATEMENT (is)
1744       {
1745         if (is->the_bfd->my_archive)
1746           {
1747             char *pnt;
1749             /* Microsoft import libraries may contain archive members for
1750                one or more DLLs, together with static object files.
1751                Inspect all members that are named *.dll - check whether
1752                they contain .idata sections. Do the renaming of all
1753                archive members that seem to be Microsoft style import
1754                objects.  */
1755             pnt = strrchr (bfd_get_filename (is->the_bfd), '.');
1757             if (pnt != NULL && (fileext_cmp (pnt + 1, "dll") == 0))
1758               {
1759                 int idata2 = 0, reloc_count = 0, idata = 0;
1760                 asection *sec;
1761                 char *new_name, seq;
1763                 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1764                   {
1765                     if (strcmp (sec->name, ".idata\$2") == 0)
1766                       idata2 = 1;
1767                     if (strncmp (sec->name, ".idata\$", 6) == 0)
1768                       idata = 1;
1769                     reloc_count += sec->reloc_count;
1770                   }
1772                 /* An archive member named .dll, but not having any .idata
1773                    sections - apparently not a Microsoft import object
1774                    after all: Skip renaming it.  */
1775                 if (!idata)
1776                   continue;
1778                 if (idata2) /* .idata2 is the TOC */
1779                   seq = 'a';
1780                 else if (reloc_count > 0) /* thunks */
1781                   seq = 'b';
1782                 else /* sentinel */
1783                   seq = 'c';
1785                 new_name
1786                   = xmalloc (strlen (bfd_get_filename (is->the_bfd)) + 3);
1787                 sprintf (new_name, "%s.%c",
1788                          bfd_get_filename (is->the_bfd), seq);
1789                 is->sort_key = new_name;
1790               }
1791           }
1792       }
1793   }
1795   {
1796     /* The following chunk of code tries to identify jump stubs in
1797        import libraries which are dead code and eliminates them
1798        from the final link. For each exported symbol <sym>, there
1799        is a object file in the import library with a .text section
1800        and several .idata\$* sections. The .text section contains the
1801        symbol definition for <sym> which is a jump stub of the form
1802        jmp *__imp_<sym>. The .idata\$5 contains the symbol definition
1803        for __imp_<sym> which is the address of the slot for <sym> in
1804        the import address table. When a symbol is imported explicitly
1805        using __declspec(dllimport) declaration, the compiler generates
1806        a reference to __imp_<sym> which directly resolves to the
1807        symbol in .idata\$5, in which case the jump stub code is not
1808        needed. The following code tries to identify jump stub sections
1809        in import libraries which are not referred to by anyone and
1810        marks them for exclusion from the final link.  */
1811     LANG_FOR_EACH_INPUT_STATEMENT (is)
1812       {
1813         if (is->the_bfd->my_archive)
1814           {
1815             int is_imp = 0;
1816             asection *sec, *stub_sec = NULL;
1818             /* See if this is an import library thunk.  */
1819             for (sec = is->the_bfd->sections; sec; sec = sec->next)
1820               {
1821                 if (strncmp (sec->name, ".idata\$", 7) == 0)
1822                   is_imp = 1;
1823                 /* The section containing the jmp stub has code
1824                    and has a reloc.  */
1825                 if ((sec->flags & SEC_CODE) && sec->reloc_count)
1826                   stub_sec = sec;
1827               }
1829             if (is_imp && stub_sec)
1830               {
1831                 asymbol **symbols;
1832                 long nsyms, src_count;
1833                 struct bfd_link_hash_entry * blhe;
1835                 if (!bfd_generic_link_read_symbols (is->the_bfd))
1836                   {
1837                     einfo (_("%F%P: %pB: could not read symbols: %E\n"),
1838                            is->the_bfd);
1839                     return;
1840                   }
1841                 symbols = bfd_get_outsymbols (is->the_bfd);
1842                 nsyms = bfd_get_symcount (is->the_bfd);
1844                 for (src_count = 0; src_count < nsyms; src_count++)
1845                   {
1846                     if (symbols[src_count]->section->id == stub_sec->id)
1847                       {
1848                         /* This symbol belongs to the section containing
1849                            the stub.  */
1850                         blhe = bfd_link_hash_lookup (link_info.hash,
1851                                                      symbols[src_count]->name,
1852                                                      false, false, true);
1853                         /* If the symbol in the stub section has no other
1854                            undefined references, exclude the stub section
1855                            from the final link.  */
1856                         if (blhe != NULL
1857                             && blhe->type == bfd_link_hash_defined
1858                             && blhe->u.undef.next == NULL
1859                             && blhe != link_info.hash->undefs_tail)
1860                           stub_sec->flags |= SEC_EXCLUDE;
1861                       }
1862                   }
1863               }
1864           }
1865       }
1866   }
1869 static void
1870 gld${EMULATION_NAME}_before_allocation (void)
1872 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
1873   /* FIXME: we should be able to set the size of the interworking stub
1874      section.
1876      Here we rummage through the found bfds to collect glue
1877      information.  FIXME: should this be based on a command line
1878      option?  krk@cygnus.com.  */
1879   {
1880     LANG_FOR_EACH_INPUT_STATEMENT (is)
1881       {
1882         if (! bfd_arm_process_before_allocation
1883             (is->the_bfd, & link_info, support_old_code))
1884           {
1885             /* xgettext:c-format */
1886             einfo (_("%P: errors encountered processing file %s for interworking\n"),
1887                    is->filename);
1888           }
1889       }
1890   }
1892   /* We have seen it all. Allocate it, and carry on.  */
1893   bfd_arm_allocate_interworking_sections (& link_info);
1894 #endif /* TARGET_IS_armpe || TARGET_IS_arm_wince_pe */
1896   before_allocation_default ();
1899 #ifdef DLL_SUPPORT
1900 /* This is called when an input file isn't recognized as a BFD.  We
1901    check here for .DEF files and pull them in automatically.  */
1903 static int
1904 saw_option (char *option)
1906   int i;
1908   for (i = 0; init[i].ptr; i++)
1909     if (strcmp (GET_INIT_SYMBOL_NAME (i), option) == 0)
1910       return init[i].inited;
1911   return 0;
1913 #endif /* DLL_SUPPORT */
1915 static bool
1916 gld${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1918 #ifdef DLL_SUPPORT
1919   const char *ext = strrchr (entry->filename, '.');
1921   if (ext != NULL && fileext_cmp (ext + 1, "def") == 0)
1922     {
1923       pe_def_file = def_file_parse (entry->filename, pe_def_file);
1925       if (pe_def_file)
1926         {
1927           int i, buflen=0, len;
1928           char *buf;
1930           for (i = 0; i < pe_def_file->num_exports; i++)
1931             {
1932               len = strlen (pe_def_file->exports[i].internal_name);
1933               if (buflen < len + 2)
1934                 buflen = len + 2;
1935             }
1937           buf = xmalloc (buflen);
1939           for (i = 0; i < pe_def_file->num_exports; i++)
1940             {
1941               struct bfd_link_hash_entry *h;
1943               sprintf (buf, "%s%s", U (""),
1944                        pe_def_file->exports[i].internal_name);
1946               h = bfd_link_hash_lookup (link_info.hash, buf, true, true, true);
1947               if (h == (struct bfd_link_hash_entry *) NULL)
1948                 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
1949               if (h->type == bfd_link_hash_new)
1950                 {
1951                   h->type = bfd_link_hash_undefined;
1952                   h->u.undef.abfd = NULL;
1953                   bfd_link_add_undef (link_info.hash, h);
1954                 }
1955             }
1956           free (buf);
1958           /* def_file_print (stdout, pe_def_file); */
1959           if (pe_def_file->is_dll == 1)
1960             link_info.type = type_dll;
1962           if (pe_def_file->base_address != (bfd_vma)(-1))
1963             {
1964               pe.ImageBase
1965                 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1966                 = init[IMAGEBASEOFF].value
1967                 = pe_def_file->base_address;
1968               init[IMAGEBASEOFF].inited = 1;
1969               if (image_base_statement)
1970                 image_base_statement->exp
1971                   = exp_assign ("__image_base__", exp_intop (pe.ImageBase),
1972                                 false);
1973             }
1975           if (pe_def_file->stack_reserve != -1
1976               && ! saw_option ("__size_of_stack_reserve__"))
1977             {
1978               pe.SizeOfStackReserve = pe_def_file->stack_reserve;
1979               if (pe_def_file->stack_commit != -1)
1980                 pe.SizeOfStackCommit = pe_def_file->stack_commit;
1981             }
1982           if (pe_def_file->heap_reserve != -1
1983               && ! saw_option ("__size_of_heap_reserve__"))
1984             {
1985               pe.SizeOfHeapReserve = pe_def_file->heap_reserve;
1986               if (pe_def_file->heap_commit != -1)
1987                 pe.SizeOfHeapCommit = pe_def_file->heap_commit;
1988             }
1989           return true;
1990         }
1991     }
1992 #endif
1993   return false;
1996 static bool
1997 gld${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1999 #ifdef DLL_SUPPORT
2000 #ifdef TARGET_IS_i386pe
2001   pe_dll_id_target ("pei-i386");
2002 #endif
2003 #ifdef TARGET_IS_shpe
2004   pe_dll_id_target ("pei-shl");
2005 #endif
2006 #ifdef TARGET_IS_armpe
2007   pe_dll_id_target ("pei-arm-little");
2008 #endif
2009 #ifdef TARGET_IS_arm_wince_pe
2010   pe_dll_id_target ("pei-arm-wince-little");
2011 #endif
2012   if (pe_bfd_is_dll (entry->the_bfd))
2013     return pe_implied_import_dll (entry->filename);
2014 #endif
2015   return false;
2018 static void
2019 gld${EMULATION_NAME}_finish (void)
2021 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe)
2022   struct bfd_link_hash_entry * h;
2024   if (thumb_entry_symbol != NULL)
2025     {
2026       h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol,
2027                                 false, false, true);
2029       if (h != (struct bfd_link_hash_entry *) NULL
2030           && (h->type == bfd_link_hash_defined
2031               || h->type == bfd_link_hash_defweak)
2032           && h->u.def.section->output_section != NULL)
2033         {
2034           static char buffer[32];
2035           bfd_vma val;
2037           /* Special procesing is required for a Thumb entry symbol.  The
2038              bottom bit of its address must be set.  */
2039           val = (h->u.def.value
2040                  + bfd_section_vma (h->u.def.section->output_section)
2041                  + h->u.def.section->output_offset);
2043           val |= 1;
2045           /* Now convert this value into a string and store it in entry_symbol
2046              where the lang_finish() function will pick it up.  */
2047           sprintf (buffer, "0x%" PRIx64, (uint64_t) val);
2049           if (entry_symbol.name != NULL && entry_from_cmdline)
2050             einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
2051                    thumb_entry_symbol, entry_symbol.name);
2052           entry_symbol.name = buffer;
2053         }
2054       else
2055         einfo (_("%P: warning: cannot find thumb start symbol %s\n"), thumb_entry_symbol);
2056     }
2057 #endif /* defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_wince_pe) */
2059   finish_default ();
2061 #ifdef DLL_SUPPORT
2062   if (bfd_link_pic (&link_info)
2063 #if !defined(TARGET_IS_shpe)
2064       || pe_dll_enable_reloc_section
2065       || (!bfd_link_relocatable (&link_info)
2066           && pe_def_file->num_exports != 0)
2067 #endif
2068     )
2069     {
2070       pe_dll_fill_sections (link_info.output_bfd, &link_info);
2071       if (command_line.out_implib_filename
2072           && (pe_def_file->num_exports != 0
2073               || bfd_link_pic (&link_info)))
2074         pe_dll_generate_implib (pe_def_file, command_line.out_implib_filename,
2075                                 &link_info);
2076     }
2077 #if defined(TARGET_IS_shpe)
2078   /* ARM doesn't need relocs.  */
2079   else
2080     {
2081       pe_exe_fill_sections (link_info.output_bfd, &link_info);
2082     }
2083 #endif
2085   if (pe_out_def_filename)
2086     pe_dll_generate_def_file (pe_out_def_filename);
2087 #endif /* DLL_SUPPORT */
2089   /* I don't know where .idata gets set as code, but it shouldn't be.  */
2090   {
2091     asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
2093     if (asec)
2094       {
2095         asec->flags &= ~SEC_CODE;
2096         asec->flags |= SEC_DATA;
2097       }
2098   }
2102 /* Place an orphan section.
2104    We use this to put sections in a reasonable place in the file, and
2105    to ensure that they are aligned as required.
2107    We handle grouped sections here as well.  A section named .foo\$nn
2108    goes into the output section .foo.  All grouped sections are sorted
2109    by name.
2111    Grouped sections for the default sections are handled by the
2112    default linker script using wildcards, and are sorted by
2113    sort_sections.  */
2115 static lang_output_section_statement_type *
2116 gld${EMULATION_NAME}_place_orphan (asection *s,
2117                                    const char *secname,
2118                                    int constraint)
2120   const char *orig_secname = secname;
2121   char *dollar = NULL;
2122   lang_output_section_statement_type *os;
2123   lang_statement_list_type add_child;
2124   lang_output_section_statement_type *match_by_name = NULL;
2125   lang_statement_union_type **pl;
2127   /* Look through the script to see where to place this section.  */
2128   if (!bfd_link_relocatable (&link_info)
2129       && (dollar = strchr (secname, '\$')) != NULL)
2130     {
2131       size_t len = dollar - secname;
2132       char *newname = xmalloc (len + 1);
2133       memcpy (newname, secname, len);
2134       newname[len] = '\0';
2135       secname = newname;
2136     }
2138   lang_list_init (&add_child);
2140   os = NULL;
2141   if (constraint == 0)
2142     for (os = lang_output_section_find (secname);
2143          os != NULL;
2144          os = next_matching_output_section_statement (os, 0))
2145       {
2146         /* If we don't match an existing output section, tell
2147            lang_insert_orphan to create a new output section.  */
2148         constraint = SPECIAL;
2150         if (os->bfd_section != NULL
2151             && (os->bfd_section->flags == 0
2152                 || ((s->flags ^ os->bfd_section->flags)
2153                     & (SEC_LOAD | SEC_ALLOC)) == 0))
2154           {
2155             /* We already have an output section statement with this
2156                name, and its bfd section has compatible flags.
2157                If the section already exists but does not have any flags set,
2158                then it has been created by the linker, probably as a result of
2159                a --section-start command line switch.  */
2160             lang_add_section (&add_child, s, NULL, NULL, os);
2161             break;
2162           }
2164         /* Save unused output sections in case we can match them
2165            against orphans later.  */
2166         if (os->bfd_section == NULL)
2167           match_by_name = os;
2168       }
2170   /* If we didn't match an active output section, see if we matched an
2171      unused one and use that.  */
2172   if (os == NULL && match_by_name)
2173     {
2174       lang_add_section (&match_by_name->children, s, NULL, NULL, match_by_name);
2175       return match_by_name;
2176     }
2178   if (os == NULL)
2179     {
2180       static struct orphan_save hold[] =
2181         {
2182           { ".text",
2183             SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
2184             0, 0, 0, 0 },
2185           { ".idata",
2186             SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
2187             0, 0, 0, 0 },
2188           { ".rdata",
2189             SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
2190             0, 0, 0, 0 },
2191           { ".data",
2192             SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
2193             0, 0, 0, 0 },
2194           { ".bss",
2195             SEC_ALLOC,
2196             0, 0, 0, 0 }
2197         };
2198       enum orphan_save_index
2199         {
2200           orphan_text = 0,
2201           orphan_idata,
2202           orphan_rodata,
2203           orphan_data,
2204           orphan_bss
2205         };
2206       static int orphan_init_done = 0;
2207       struct orphan_save *place;
2208       lang_output_section_statement_type *after;
2209       etree_type *address;
2210       flagword flags;
2211       asection *nexts;
2213       if (!orphan_init_done)
2214         {
2215           struct orphan_save *ho;
2216           for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
2217             if (ho->name != NULL)
2218               {
2219                 ho->os = lang_output_section_find (ho->name);
2220                 if (ho->os != NULL && ho->os->flags == 0)
2221                   ho->os->flags = ho->flags;
2222               }
2223           orphan_init_done = 1;
2224         }
2226       flags = s->flags;
2227       if (!bfd_link_relocatable (&link_info))
2228         {
2229           nexts = s;
2230           while ((nexts = bfd_get_next_section_by_name (nexts->owner,
2231                                                         nexts)))
2232             if (nexts->output_section == NULL
2233                 && (nexts->flags & SEC_EXCLUDE) == 0
2234                 && ((nexts->flags ^ flags) & (SEC_LOAD | SEC_ALLOC)) == 0
2235                 && (nexts->owner->flags & DYNAMIC) == 0
2236                 && !bfd_input_just_syms (nexts->owner))
2237               flags = (((flags ^ SEC_READONLY)
2238                         | (nexts->flags ^ SEC_READONLY))
2239                        ^ SEC_READONLY);
2240         }
2242       /* Try to put the new output section in a reasonable place based
2243          on the section name and section flags.  */
2245       place = NULL;
2246       if ((flags & SEC_ALLOC) == 0)
2247         ;
2248       else if ((flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2249         place = &hold[orphan_bss];
2250       else if ((flags & SEC_READONLY) == 0)
2251         place = &hold[orphan_data];
2252       else if ((flags & SEC_CODE) == 0)
2253         {
2254           place = (!strncmp (secname, ".idata\$", 7) ? &hold[orphan_idata]
2255                                                      : &hold[orphan_rodata]);
2256         }
2257       else
2258         place = &hold[orphan_text];
2260       after = NULL;
2261       if (place != NULL)
2262         {
2263           if (place->os == NULL)
2264             place->os = lang_output_section_find (place->name);
2265           after = place->os;
2266           if (after == NULL)
2267             after = lang_output_section_find_by_flags (s, flags, &place->os,
2268                                                        NULL);
2269           if (after == NULL)
2270             /* *ABS* is always the first output section statement.  */
2271             after = (void *) lang_os_list.head;
2272         }
2274       /* All sections in an executable must be aligned to a page boundary.
2275          In a relocatable link, just preserve the incoming alignment; the
2276          address is discarded by lang_insert_orphan in that case, anyway.  */
2277       address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
2278       os = lang_insert_orphan (s, secname, constraint, after, place, address,
2279                                &add_child);
2280       if (bfd_link_relocatable (&link_info))
2281         {
2282           os->section_alignment = exp_intop (1U << s->alignment_power);
2283           os->bfd_section->alignment_power = s->alignment_power;
2284         }
2285     }
2287   /* If the section name has a '\$', sort it with the other '\$'
2288      sections.  */
2289   for (pl = &os->children.head; *pl != NULL; pl = &(*pl)->header.next)
2290     {
2291       lang_input_section_type *ls;
2292       const char *lname;
2294       if ((*pl)->header.type != lang_input_section_enum)
2295         continue;
2297       ls = &(*pl)->input_section;
2299       lname = bfd_section_name (ls->section);
2300       if (strchr (lname, '\$') != NULL
2301           && (dollar == NULL || strcmp (orig_secname, lname) < 0))
2302         break;
2303     }
2305   if (add_child.head != NULL)
2306     {
2307       *add_child.tail = *pl;
2308       *pl = add_child.head;
2309     }
2311   return os;
2314 static bool
2315 gld${EMULATION_NAME}_open_dynamic_archive
2316   (const char *arch ATTRIBUTE_UNUSED,
2317    search_dirs_type *search,
2318    lang_input_statement_type *entry)
2320   static const struct
2321     {
2322       const char * format;
2323       bool use_prefix;
2324     }
2325   libname_fmt [] =
2326     {
2327       /* Preferred explicit import library for dll's.  */
2328       { "lib%s.dll.a", false },
2329       /* Alternate explicit import library for dll's.  */
2330       { "%s.dll.a", false },
2331       /* "libfoo.a" could be either an import lib or a static lib.
2332          For backwards compatibility, libfoo.a needs to precede
2333          libfoo.dll and foo.dll in the search.  */
2334       { "lib%s.a", false },
2335       /* The 'native' spelling of an import lib name is "foo.lib".  */
2336       { "%s.lib", false },
2337       /* PR 22948 - Check for an import library.  */
2338       { "lib%s.lib", false },
2339 #ifdef DLL_SUPPORT
2340       /* Try "<prefix>foo.dll" (preferred dll name, if specified).  */
2341       { "%s%s.dll", true },
2342 #endif
2343       /* Try "libfoo.dll" (default preferred dll name).  */
2344       { "lib%s.dll", false },
2345       /* Finally try 'native' dll name "foo.dll".  */
2346       {  "%s.dll", false },
2347       /* Note: If adding more formats to this table, make sure to check to
2348          see if their length is longer than libname_fmt[0].format, and if
2349          so, update the call to xmalloc() below.  */
2350       { NULL, false }
2351     };
2352   static unsigned int format_max_len = 0;
2353   const char * filename;
2354   char * full_string;
2355   char * base_string;
2356   unsigned int i;
2359   if (! entry->flags.maybe_archive || entry->flags.full_name_provided)
2360     return false;
2362   filename = entry->filename;
2364   if (format_max_len == 0)
2365     /* We need to allow space in the memory that we are going to allocate
2366        for the characters in the format string.  Since the format array is
2367        static we only need to calculate this information once.  In theory
2368        this value could also be computed statically, but this introduces
2369        the possibility for a discrepancy and hence a possible memory
2370        corruption.  The lengths we compute here will be too long because
2371        they will include any formating characters (%s) in the strings, but
2372        this will not matter.  */
2373     for (i = 0; libname_fmt[i].format; i++)
2374       if (format_max_len < strlen (libname_fmt[i].format))
2375         format_max_len = strlen (libname_fmt[i].format);
2377   full_string = xmalloc (strlen (search->name)
2378                          + strlen (filename)
2379                          + format_max_len
2380 #ifdef DLL_SUPPORT
2381                          + (pe_dll_search_prefix
2382                             ? strlen (pe_dll_search_prefix) : 0)
2383 #endif
2384                          /* Allow for the terminating NUL and for the path
2385                             separator character that is inserted between
2386                             search->name and the start of the format string.  */
2387                          + 2);
2389   base_string = stpcpy (full_string, search->name);
2390   *base_string++ = '/';
2392   for (i = 0; libname_fmt[i].format; i++)
2393     {
2394 #ifdef DLL_SUPPORT
2395       if (libname_fmt[i].use_prefix)
2396         {
2397           if (!pe_dll_search_prefix)
2398             continue;
2399           sprintf (base_string, libname_fmt[i].format, pe_dll_search_prefix, filename);
2400         }
2401       else
2402 #endif
2403         sprintf (base_string, libname_fmt[i].format, filename);
2405       if (ldfile_try_open_bfd (full_string, entry))
2406         break;
2407     }
2409   if (!libname_fmt[i].format)
2410     {
2411       free (full_string);
2412       return false;
2413     }
2415   entry->filename = full_string;
2417   return true;
2420 static int
2421 gld${EMULATION_NAME}_find_potential_libraries
2422   (char *name, lang_input_statement_type *entry)
2424   return ldfile_open_file_search (name, entry, "", ".lib");
2427 static char *
2428 gld${EMULATION_NAME}_get_script (int *isfile)
2431 if test x"$COMPILE_IN" = xyes
2432 then
2433 # Scripts compiled in.
2435 # sed commands to quote an ld script as a C string.
2436 sc="-f ${srcdir}/emultempl/stringify.sed"
2438 fragment <<EOF
2440   *isfile = 0;
2442   if (bfd_link_relocatable (&link_info) && config.build_constructors)
2443     return
2445 sed $sc ldscripts/${EMULATION_NAME}.xu                  >> e${EMULATION_NAME}.c
2446 echo '  ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
2447 sed $sc ldscripts/${EMULATION_NAME}.xr                  >> e${EMULATION_NAME}.c
2448 echo '  ; else if (!config.text_read_only) return'      >> e${EMULATION_NAME}.c
2449 sed $sc ldscripts/${EMULATION_NAME}.xbn                 >> e${EMULATION_NAME}.c
2450 echo '  ; else if (!config.magic_demand_paged) return'  >> e${EMULATION_NAME}.c
2451 sed $sc ldscripts/${EMULATION_NAME}.xn                  >> e${EMULATION_NAME}.c
2452 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
2453 echo '  ; else if (link_info.pei386_auto_import == 1 && (MERGE_RDATA_V2 || link_info.pei386_runtime_pseudo_reloc != 2)) return' >> e${EMULATION_NAME}.c
2454 sed $sc ldscripts/${EMULATION_NAME}.xa                  >> e${EMULATION_NAME}.c
2456 echo '  ; else return'                                  >> e${EMULATION_NAME}.c
2457 sed $sc ldscripts/${EMULATION_NAME}.x                   >> e${EMULATION_NAME}.c
2458 echo '; }'                                              >> e${EMULATION_NAME}.c
2460 else
2461 # Scripts read from the filesystem.
2463 fragment <<EOF
2465   *isfile = 1;
2467   if (bfd_link_relocatable (&link_info) && config.build_constructors)
2468     return "ldscripts/${EMULATION_NAME}.xu";
2469   else if (bfd_link_relocatable (&link_info))
2470     return "ldscripts/${EMULATION_NAME}.xr";
2471   else if (!config.text_read_only)
2472     return "ldscripts/${EMULATION_NAME}.xbn";
2473   else if (!config.magic_demand_paged)
2474     return "ldscripts/${EMULATION_NAME}.xn";
2476 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
2477 fragment <<EOF
2478   else if (link_info.pei386_auto_import == 1
2479            && (MERGE_RDATA_V2 || link_info.pei386_runtime_pseudo_reloc != 2))
2480     return "ldscripts/${EMULATION_NAME}.xa";
2483 fragment <<EOF
2484   else
2485     return "ldscripts/${EMULATION_NAME}.x";
2490 LDEMUL_AFTER_PARSE=gld${EMULATION_NAME}_after_parse
2491 LDEMUL_BEFORE_PLUGIN_ALL_SYMBOLS_READ=gld${EMULATION_NAME}_before_plugin_all_symbols_read
2492 LDEMUL_AFTER_OPEN=gld${EMULATION_NAME}_after_open
2493 LDEMUL_BEFORE_ALLOCATION=gld${EMULATION_NAME}_before_allocation
2494 LDEMUL_FINISH=gld${EMULATION_NAME}_finish
2495 LDEMUL_OPEN_DYNAMIC_ARCHIVE=gld${EMULATION_NAME}_open_dynamic_archive
2496 LDEMUL_PLACE_ORPHAN=gld${EMULATION_NAME}_place_orphan
2497 LDEMUL_SET_SYMBOLS=gld${EMULATION_NAME}_set_symbols
2498 LDEMUL_ADD_OPTIONS=gld${EMULATION_NAME}_add_options
2499 LDEMUL_HANDLE_OPTION=gld${EMULATION_NAME}_handle_option
2500 LDEMUL_UNRECOGNIZED_FILE=gld${EMULATION_NAME}_unrecognized_file
2501 LDEMUL_LIST_OPTIONS=gld${EMULATION_NAME}_list_options
2502 LDEMUL_RECOGNIZED_FILE=gld${EMULATION_NAME}_recognized_file
2503 LDEMUL_FIND_POTENTIAL_LIBRARIES=gld${EMULATION_NAME}_find_potential_libraries
2505 source_em ${srcdir}/emultempl/emulation.em