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