* ldlang.c (lang_insert_orphan): Add __start_<section> symbol
[external/binutils.git] / ld / emultempl / pep.em
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 test -z "${ENTRY}" && ENTRY="_mainCRTStartup"
4 if [ -z "$MACHINE" ]; then
5   OUTPUT_ARCH=${ARCH}
6 else
7   OUTPUT_ARCH=${ARCH}:${MACHINE}
8 fi
9 rm -f e${EMULATION_NAME}.c
10 (echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
11 fragment <<EOF
12 /* Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
13    Written by Kai Tietz, OneVision Software GmbH&CoKg.
14
15    This file is part of the GNU Binutils.
16
17    This program is free software; you can redistribute it and/or modify
18    it under the terms of the GNU General Public License as published by
19    the Free Software Foundation; either version 3 of the License, or
20    (at your option) any later version.
21
22    This program is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25    GNU General Public License for more details.
26
27    You should have received a copy of the GNU General Public License
28    along with this program; if not, write to the Free Software
29    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
30    MA 02110-1301, USA.  */
31
32
33 /* For WINDOWS_XP64 and higher */
34 /* Based on pe.em, but modified for 64 bit support.  */
35
36 #define TARGET_IS_${EMULATION_NAME}
37
38 #define COFF_IMAGE_WITH_PE
39 #define COFF_WITH_PE
40 #define COFF_WITH_pex64
41
42 #include "sysdep.h"
43 #include "bfd.h"
44 #include "bfdlink.h"
45 #include "getopt.h"
46 #include "libiberty.h"
47 #include "ld.h"
48 #include "ldmain.h"
49 #include "ldexp.h"
50 #include "ldlang.h"
51 #include "ldfile.h"
52 #include "ldemul.h"
53 #include <ldgram.h>
54 #include "ldlex.h"
55 #include "ldmisc.h"
56 #include "ldctor.h"
57 #include "coff/internal.h"
58
59 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
60    header in generic PE code.  */
61 #include "coff/x86_64.h"
62 #include "coff/pe.h"
63
64 /* FIXME: This is a BFD internal header file, and we should not be
65    using it here.  */
66 #include "../bfd/libcoff.h"
67
68 #undef  AOUTSZ
69 #define AOUTSZ          PEPAOUTSZ
70 #define PEAOUTHDR       PEPAOUTHDR
71
72 #include "deffile.h"
73 #include "pep-dll.h"
74 #include "safe-ctype.h"
75
76 /* Permit the emulation parameters to override the default section
77    alignment by setting OVERRIDE_SECTION_ALIGNMENT.  FIXME: This makes
78    it seem that include/coff/internal.h should not define
79    PE_DEF_SECTION_ALIGNMENT.  */
80 #if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
81 #undef  PE_DEF_SECTION_ALIGNMENT
82 #define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
83 #endif
84
85 #ifdef TARGET_IS_i386pep
86 #define DLL_SUPPORT
87 #endif
88
89 #if defined(TARGET_IS_i386pep) || ! defined(DLL_SUPPORT)
90 #define PE_DEF_SUBSYSTEM                3
91 #else
92 #undef  NT_EXE_IMAGE_BASE
93 #define NT_EXE_IMAGE_BASE               0x00010000
94 #undef  PE_DEF_SECTION_ALIGNMENT
95 #define PE_DEF_SUBSYSTEM                2
96 #undef  PE_DEF_FILE_ALIGNMENT
97 #define PE_DEF_FILE_ALIGNMENT           0x00000200
98 #define PE_DEF_SECTION_ALIGNMENT        0x00000400
99 #endif
100
101
102 static struct internal_extra_pe_aouthdr pep;
103 static int dll;
104 static flagword real_flags = IMAGE_FILE_LARGE_ADDRESS_AWARE;
105 static int support_old_code = 0;
106 static lang_assignment_statement_type *image_base_statement = 0;
107 static unsigned short pe_dll_characteristics = 0;
108
109 #ifdef DLL_SUPPORT
110 static int    pep_enable_stdcall_fixup = 1; /* 0=disable 1=enable (default).  */
111 static char * pep_out_def_filename = NULL;
112 static char * pep_implib_filename = NULL;
113 static int    pep_enable_auto_image_base = 0;
114 static char * pep_dll_search_prefix = NULL;
115 #endif
116
117 extern const char *output_filename;
118
119 static void
120 gld_${EMULATION_NAME}_before_parse (void)
121 {
122   ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
123   output_filename = "${EXECUTABLE_NAME:-a.exe}";
124 #ifdef DLL_SUPPORT
125   config.dynamic_link = TRUE;
126   config.has_shared = 1;
127   link_info.pei386_auto_import = -1;
128   link_info.pei386_runtime_pseudo_reloc = 2; /* Use by default version 2.  */
129
130 #if (PE_DEF_SUBSYSTEM == 9) || (PE_DEF_SUBSYSTEM == 2)
131   lang_default_entry ("_WinMainCRTStartup");
132 #else
133   lang_default_entry ("${ENTRY}");
134 #endif
135 #endif
136 }
137 \f
138 /* PE format extra command line options.  */
139
140 /* Used for setting flags in the PE header.  */
141 enum options
142 {
143   OPTION_BASE_FILE = 300 + 1,
144   OPTION_DLL,
145   OPTION_FILE_ALIGNMENT,
146   OPTION_IMAGE_BASE,
147   OPTION_MAJOR_IMAGE_VERSION,
148   OPTION_MAJOR_OS_VERSION,
149   OPTION_MAJOR_SUBSYSTEM_VERSION,
150   OPTION_MINOR_IMAGE_VERSION,
151   OPTION_MINOR_OS_VERSION,
152   OPTION_MINOR_SUBSYSTEM_VERSION,
153   OPTION_SECTION_ALIGNMENT,
154   OPTION_STACK,
155   OPTION_SUBSYSTEM,
156   OPTION_HEAP,
157   OPTION_SUPPORT_OLD_CODE,
158   OPTION_OUT_DEF,
159   OPTION_EXPORT_ALL,
160   OPTION_EXCLUDE_SYMBOLS,
161   OPTION_KILL_ATS,
162   OPTION_STDCALL_ALIASES,
163   OPTION_ENABLE_STDCALL_FIXUP,
164   OPTION_DISABLE_STDCALL_FIXUP,
165   OPTION_IMPLIB_FILENAME,
166   OPTION_WARN_DUPLICATE_EXPORTS,
167   OPTION_IMP_COMPAT,
168   OPTION_ENABLE_AUTO_IMAGE_BASE,
169   OPTION_DISABLE_AUTO_IMAGE_BASE,
170   OPTION_DLL_SEARCH_PREFIX,
171   OPTION_NO_DEFAULT_EXCLUDES,
172   OPTION_DLL_ENABLE_AUTO_IMPORT,
173   OPTION_DLL_DISABLE_AUTO_IMPORT,
174   OPTION_ENABLE_EXTRA_PE_DEBUG,
175   OPTION_EXCLUDE_LIBS,
176   OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC,
177   OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC,
178   OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1,
179   OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2,
180   OPTION_EXCLUDE_MODULES_FOR_IMPLIB,
181   OPTION_USE_NUL_PREFIXED_IMPORT_TABLES,
182   OPTION_ENABLE_LONG_SECTION_NAMES,
183   OPTION_DISABLE_LONG_SECTION_NAMES,
184   OPTION_DYNAMIC_BASE,
185   OPTION_FORCE_INTEGRITY,
186   OPTION_NX_COMPAT,
187   OPTION_NO_ISOLATION,
188   OPTION_NO_SEH,
189   OPTION_NO_BIND,
190   OPTION_WDM_DRIVER,
191   OPTION_TERMINAL_SERVER_AWARE
192 };
193
194 static void
195 gld${EMULATION_NAME}_add_options
196   (int ns ATTRIBUTE_UNUSED,
197    char **shortopts ATTRIBUTE_UNUSED,
198    int nl,
199    struct option **longopts,
200    int nrl ATTRIBUTE_UNUSED,
201    struct option **really_longopts ATTRIBUTE_UNUSED)
202 {
203   static const struct option xtra_long[] =
204   {
205     /* PE options */
206     {"base-file", required_argument, NULL, OPTION_BASE_FILE},
207     {"dll", no_argument, NULL, OPTION_DLL},
208     {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
209     {"heap", required_argument, NULL, OPTION_HEAP},
210     {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
211     {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
212     {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
213     {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
214     {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
215     {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
216     {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
217     {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
218     {"stack", required_argument, NULL, OPTION_STACK},
219     {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
220     {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
221     {"use-nul-prefixed-import-tables", no_argument, NULL,
222      OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
223 #ifdef DLL_SUPPORT
224     /* getopt allows abbreviations, so we do this to stop it
225        from treating -o as an abbreviation for this option.  */
226     {"output-def", required_argument, NULL, OPTION_OUT_DEF},
227     {"output-def", required_argument, NULL, OPTION_OUT_DEF},
228     {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
229     {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
230     {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
231     {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
232     {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
233     {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
234     {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
235     {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
236     {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
237     {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
238     /* getopt() allows abbreviations, so we do this to stop it from
239        treating -c as an abbreviation for these --compat-implib.  */
240     {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
241     {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
242     {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
243     {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
244     {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
245     {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
246     {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
247     {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
248     {"enable-extra-pep-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
249     {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
250     {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
251     {"enable-runtime-pseudo-reloc-v1", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1},
252     {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2},
253 #endif
254     {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
255     {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
256     {"dynamicbase",no_argument, NULL, OPTION_DYNAMIC_BASE},
257     {"forceinteg", no_argument, NULL, OPTION_FORCE_INTEGRITY},
258     {"nxcompat", no_argument, NULL, OPTION_NX_COMPAT},
259     {"no-isolation", no_argument, NULL, OPTION_NO_ISOLATION},
260     {"no-seh", no_argument, NULL, OPTION_NO_SEH},
261     {"no-bind", no_argument, NULL, OPTION_NO_BIND},
262     {"wdmdriver", no_argument, NULL, OPTION_WDM_DRIVER},
263     {"tsaware", no_argument, NULL, OPTION_TERMINAL_SERVER_AWARE},    {NULL, no_argument, NULL, 0}
264   };
265
266   *longopts = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
267   memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
268 }
269
270 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
271    parameters which may be input from the command line.  */
272
273 typedef struct
274 {
275   void *ptr;
276   int size;
277   bfd_vma value;
278   char *symbol;
279   int inited;
280 } definfo;
281
282 #define D(field,symbol,def)  {&pep.field,sizeof(pep.field), def, symbol,0}
283
284 static definfo init[] =
285 {
286   /* imagebase must be first */
287 #define IMAGEBASEOFF 0
288   D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
289 #define DLLOFF 1
290   {&dll, sizeof(dll), 0, "__dll__", 0},
291 #define MSIMAGEBASEOFF  2
292   D(ImageBase,"___ImageBase", NT_EXE_IMAGE_BASE),
293   D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
294   D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
295   D(MajorOperatingSystemVersion,"__major_os_version__", 4),
296   D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
297   D(MajorImageVersion,"__major_image_version__", 0),
298   D(MinorImageVersion,"__minor_image_version__", 0),
299   D(MajorSubsystemVersion,"__major_subsystem_version__", 5),
300   D(MinorSubsystemVersion,"__minor_subsystem_version__", 2),
301   D(Subsystem,"__subsystem__", ${SUBSYSTEM}),
302   D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000),
303   D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
304   D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
305   D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
306   D(LoaderFlags,"__loader_flags__", 0x0),
307   D(DllCharacteristics, "__dll_characteristics__", 0x0), 
308   { NULL, 0, 0, NULL, 0 }
309 };
310
311
312 static void
313 gld_${EMULATION_NAME}_list_options (FILE *file)
314 {
315   fprintf (file, _("  --base_file <basefile>             Generate a base file for relocatable DLLs\n"));
316   fprintf (file, _("  --dll                              Set image base to the default for DLLs\n"));
317   fprintf (file, _("  --file-alignment <size>            Set file alignment\n"));
318   fprintf (file, _("  --heap <size>                      Set initial size of the heap\n"));
319   fprintf (file, _("  --image-base <address>             Set start address of the executable\n"));
320   fprintf (file, _("  --major-image-version <number>     Set version number of the executable\n"));
321   fprintf (file, _("  --major-os-version <number>        Set minimum required OS version\n"));
322   fprintf (file, _("  --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
323   fprintf (file, _("  --minor-image-version <number>     Set revision number of the executable\n"));
324   fprintf (file, _("  --minor-os-version <number>        Set minimum required OS revision\n"));
325   fprintf (file, _("  --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
326   fprintf (file, _("  --section-alignment <size>         Set section alignment\n"));
327   fprintf (file, _("  --stack <size>                     Set size of the initial stack\n"));
328   fprintf (file, _("  --subsystem <name>[:<version>]     Set required OS subsystem [& version]\n"));
329   fprintf (file, _("  --support-old-code                 Support interworking with old code\n"));
330 #ifdef DLL_SUPPORT
331   fprintf (file, _("  --add-stdcall-alias                Export symbols with and without @nn\n"));
332   fprintf (file, _("  --disable-stdcall-fixup            Don't link _sym to _sym@nn\n"));
333   fprintf (file, _("  --enable-stdcall-fixup             Link _sym to _sym@nn without warnings\n"));
334   fprintf (file, _("  --exclude-symbols sym,sym,...      Exclude symbols from automatic export\n"));
335   fprintf (file, _("  --exclude-libs lib,lib,...         Exclude libraries from automatic export\n"));
336   fprintf (file, _("  --exclude-modules-for-implib mod,mod,...\n"));
337   fprintf (file, _("                                     Exclude objects, archive members from auto\n"));
338   fprintf (file, _("                                     export, place into import library instead.\n"));
339   fprintf (file, _("  --export-all-symbols               Automatically export all globals to DLL\n"));
340   fprintf (file, _("  --kill-at                          Remove @nn from exported symbols\n"));
341   fprintf (file, _("  --out-implib <file>                Generate import library\n"));
342   fprintf (file, _("  --output-def <file>                Generate a .DEF file for the built DLL\n"));
343   fprintf (file, _("  --warn-duplicate-exports           Warn about duplicate exports.\n"));
344   fprintf (file, _("  --compat-implib                    Create backward compatible import libs;\n\
345                                        create __imp_<SYMBOL> as well.\n"));
346   fprintf (file, _("  --enable-auto-image-base           Automatically choose image base for DLLs\n\
347                                        unless user specifies one\n"));
348   fprintf (file, _("  --disable-auto-image-base          Do not auto-choose image base. (default)\n"));
349   fprintf (file, _("  --dll-search-prefix=<string>       When linking dynamically to a dll without\n\
350                                        an importlib, use <string><basename>.dll\n\
351                                        in preference to lib<basename>.dll \n"));
352   fprintf (file, _("  --enable-auto-import               Do sophistcated linking of _sym to\n\
353                                        __imp_sym for DATA references\n"));
354   fprintf (file, _("  --disable-auto-import              Do not auto-import DATA items from DLLs\n"));
355   fprintf (file, _("  --enable-runtime-pseudo-reloc      Work around auto-import limitations by\n\
356                                        adding pseudo-relocations resolved at\n\
357                                        runtime.\n"));
358   fprintf (file, _("  --disable-runtime-pseudo-reloc     Do not add runtime pseudo-relocations for\n\
359                                        auto-imported DATA.\n"));
360   fprintf (file, _("  --enable-extra-pep-debug            Enable verbose debug output when building\n\
361                                        or linking to DLLs (esp. auto-import)\n"));
362   fprintf (file, _("  --enable-long-section-names        Use long COFF section names even in\n\
363                                        executable image files\n"));
364   fprintf (file, _("  --disable-long-section-names       Never use long COFF section names, even\n\
365                                        in object files\n"));
366   fprintf (file, _("  --dynamicbase                      Image base address may be relocated using\n\
367                                        address space layout randomization (ASLR)\n"));
368   fprintf (file, _("  --forceinteg               Code integrity checks are enforced\n"));
369   fprintf (file, _("  --nxcompat                 Image is compatible with data execution prevention\n"));
370   fprintf (file, _("  --no-isolation             Image understands isolation but do not isolate the image\n"));
371   fprintf (file, _("  --no-seh                   Image does not use SEH. No SE handler may\n\
372                                        be called in this image\n"));
373   fprintf (file, _("  --no-bind                  Do not bind this image\n"));
374   fprintf (file, _("  --wdmdriver                Driver uses the WDM model\n"));
375   fprintf (file, _("  --tsaware                  Image is Terminal Server aware\n"));
376 #endif
377 }
378
379
380 static void
381 set_pep_name (char *name, bfd_vma val)
382 {
383   int i;
384
385   /* Find the name and set it.  */
386   for (i = 0; init[i].ptr; i++)
387     {
388       if (strcmp (name, init[i].symbol) == 0)
389         {
390           init[i].value = val;
391           init[i].inited = 1;
392           if (strcmp (name,"__image_base__") == 0)
393             set_pep_name ("___ImageBase", val);
394           return;
395         }
396     }
397   abort ();
398 }
399
400
401 static void
402 set_pep_subsystem (void)
403 {
404   const char *sver;
405   const char *entry;
406   const char *initial_symbol_char;
407   char *end;
408   int len;
409   int i;
410   int subsystem;
411   unsigned long temp_subsystem;
412   static const struct
413     {
414       const char *name;
415       const int value;
416       const char *entry;
417     }
418   v[] =
419     {
420       { "native",  1, "NtProcessStartup" },
421       { "windows", 2, "WinMainCRTStartup" },
422       { "console", 3, "mainCRTStartup" },
423       { "posix",   7, "__PosixProcessStartup"},
424       { "wince",   9, "_WinMainCRTStartup" },
425       { "xbox",   14, "mainCRTStartup" },
426       { NULL, 0, NULL }
427     };
428   /* Entry point name for arbitrary subsystem numbers.  */
429   static const char default_entry[] = "mainCRTStartup";
430
431   /* Check for the presence of a version number.  */
432   sver = strchr (optarg, ':');
433   if (sver == NULL)
434     len = strlen (optarg);
435   else
436     {
437       len = sver - optarg;
438       set_pep_name ("__major_subsystem_version__",
439                     strtoul (sver + 1, &end, 0));
440       if (*end == '.')
441         set_pep_name ("__minor_subsystem_version__",
442                       strtoul (end + 1, &end, 0));
443       if (*end != '\0')
444         einfo (_("%P: warning: bad version number in -subsystem option\n"));
445     }
446
447   /* Check for numeric subsystem.  */
448   temp_subsystem = strtoul (optarg, & end, 0);
449   if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
450     {
451       /* Search list for a numeric match to use its entry point.  */
452       for (i = 0; v[i].name; i++)
453         if (v[i].value == (int) temp_subsystem)
454           break;
455
456       /* If no match, use the default.  */
457       if (v[i].name != NULL)
458         entry = v[i].entry;
459       else
460         entry = default_entry;
461
462       /* Use this subsystem.  */
463       subsystem = (int) temp_subsystem;
464     }
465   else
466     {
467       /* Search for subsystem by name.  */
468       for (i = 0; v[i].name; i++)
469         if (strncmp (optarg, v[i].name, len) == 0
470             && v[i].name[len] == '\0')
471           break;
472
473       if (v[i].name == NULL)
474         {
475           einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
476           return;
477         }
478
479       entry = v[i].entry;
480       subsystem = v[i].value;
481     }
482
483   set_pep_name ("__subsystem__", subsystem);
484
485   initial_symbol_char = ${INITIAL_SYMBOL_CHAR};
486   if (*initial_symbol_char != '\0')
487     {
488       char *alc_entry;
489
490       /* lang_default_entry expects its argument to be permanently
491          allocated, so we don't free this string.  */
492       alc_entry = xmalloc (strlen (initial_symbol_char)
493                            + strlen (entry)
494                            + 1);
495       strcpy (alc_entry, initial_symbol_char);
496       strcat (alc_entry, entry);
497       entry = alc_entry;
498     }
499
500   lang_default_entry (entry);
501
502   return;
503 }
504
505
506 static void
507 set_pep_value (char *name)
508 {
509   char *end;
510
511   set_pep_name (name,  (bfd_vma) strtoull (optarg, &end, 0));
512
513   if (end == optarg)
514     einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
515
516   optarg = end;
517 }
518
519
520 static void
521 set_pep_stack_heap (char *resname, char *comname)
522 {
523   set_pep_value (resname);
524
525   if (*optarg == ',')
526     {
527       optarg++;
528       set_pep_value (comname);
529     }
530   else if (*optarg)
531     einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
532 }
533
534
535 static bfd_boolean
536 gld${EMULATION_NAME}_handle_option (int optc)
537 {
538   switch (optc)
539     {
540     default:
541       return FALSE;
542
543     case OPTION_BASE_FILE:
544       link_info.base_file = fopen (optarg, FOPEN_WB);
545       if (link_info.base_file == NULL)
546         {
547           /* xgettext:c-format */
548           fprintf (stderr, _("%s: Can't open base file %s\n"),
549                    program_name, optarg);
550           xexit (1);
551         }
552       break;
553
554       /* PE options.  */
555     case OPTION_HEAP:
556       set_pep_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
557       break;
558     case OPTION_STACK:
559       set_pep_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
560       break;
561     case OPTION_SUBSYSTEM:
562       set_pep_subsystem ();
563       break;
564     case OPTION_MAJOR_OS_VERSION:
565       set_pep_value ("__major_os_version__");
566       break;
567     case OPTION_MINOR_OS_VERSION:
568       set_pep_value ("__minor_os_version__");
569       break;
570     case OPTION_MAJOR_SUBSYSTEM_VERSION:
571       set_pep_value ("__major_subsystem_version__");
572       break;
573     case OPTION_MINOR_SUBSYSTEM_VERSION:
574       set_pep_value ("__minor_subsystem_version__");
575       break;
576     case OPTION_MAJOR_IMAGE_VERSION:
577       set_pep_value ("__major_image_version__");
578       break;
579     case OPTION_MINOR_IMAGE_VERSION:
580       set_pep_value ("__minor_image_version__");
581       break;
582     case OPTION_FILE_ALIGNMENT:
583       set_pep_value ("__file_alignment__");
584       break;
585     case OPTION_SECTION_ALIGNMENT:
586       set_pep_value ("__section_alignment__");
587       break;
588     case OPTION_DLL:
589       set_pep_name ("__dll__", 1);
590       break;
591     case OPTION_IMAGE_BASE:
592       set_pep_value ("__image_base__");
593       break;
594     case OPTION_SUPPORT_OLD_CODE:
595       support_old_code = 1;
596       break;
597     case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
598       pep_use_nul_prefixed_import_tables = TRUE;
599       break;
600 #ifdef DLL_SUPPORT
601     case OPTION_OUT_DEF:
602       pep_out_def_filename = xstrdup (optarg);
603       break;
604     case OPTION_EXPORT_ALL:
605       pep_dll_export_everything = 1;
606       break;
607     case OPTION_EXCLUDE_SYMBOLS:
608       pep_dll_add_excludes (optarg, EXCLUDESYMS);
609       break;
610     case OPTION_EXCLUDE_LIBS:
611       pep_dll_add_excludes (optarg, EXCLUDELIBS);
612       break;
613     case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
614       pep_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
615       break;
616     case OPTION_KILL_ATS:
617       pep_dll_kill_ats = 1;
618       break;
619     case OPTION_STDCALL_ALIASES:
620       pep_dll_stdcall_aliases = 1;
621       break;
622     case OPTION_ENABLE_STDCALL_FIXUP:
623       pep_enable_stdcall_fixup = 1;
624       break;
625     case OPTION_DISABLE_STDCALL_FIXUP:
626       pep_enable_stdcall_fixup = 0;
627       break;
628     case OPTION_IMPLIB_FILENAME:
629       pep_implib_filename = xstrdup (optarg);
630       break;
631     case OPTION_WARN_DUPLICATE_EXPORTS:
632       pep_dll_warn_dup_exports = 1;
633       break;
634     case OPTION_IMP_COMPAT:
635       pep_dll_compat_implib = 1;
636       break;
637     case OPTION_ENABLE_AUTO_IMAGE_BASE:
638       pep_enable_auto_image_base = 1;
639       break;
640     case OPTION_DISABLE_AUTO_IMAGE_BASE:
641       pep_enable_auto_image_base = 0;
642       break;
643     case OPTION_DLL_SEARCH_PREFIX:
644       pep_dll_search_prefix = xstrdup (optarg);
645       break;
646     case OPTION_NO_DEFAULT_EXCLUDES:
647       pep_dll_do_default_excludes = 0;
648       break;
649     case OPTION_DLL_ENABLE_AUTO_IMPORT:
650       link_info.pei386_auto_import = 1;
651       break;
652     case OPTION_DLL_DISABLE_AUTO_IMPORT:
653       link_info.pei386_auto_import = 0;
654       break;
655     case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
656       link_info.pei386_runtime_pseudo_reloc = 2;
657       break;
658     case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
659       link_info.pei386_runtime_pseudo_reloc = 0;
660       break;
661     case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1:
662       link_info.pei386_runtime_pseudo_reloc = 1;
663       break;
664     case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2:
665       link_info.pei386_runtime_pseudo_reloc = 2;
666       break;
667     case OPTION_ENABLE_EXTRA_PE_DEBUG:
668       pep_dll_extra_pe_debug = 1;
669       break;
670 #endif
671     case OPTION_ENABLE_LONG_SECTION_NAMES:
672       pep_use_coff_long_section_names = 1;
673       break;
674     case OPTION_DISABLE_LONG_SECTION_NAMES:
675       pep_use_coff_long_section_names = 0;
676       break;
677     /*  Get DLLCharacteristics bits  */
678     case OPTION_DYNAMIC_BASE:
679       pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
680       break;
681     case OPTION_FORCE_INTEGRITY:
682       pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
683       break;
684     case OPTION_NX_COMPAT:
685       pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
686       break;
687     case OPTION_NO_ISOLATION:
688       pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
689       break;
690     case OPTION_NO_SEH:
691       pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_SEH;
692       break;
693     case OPTION_NO_BIND:
694       pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_BIND;
695       break;
696     case OPTION_WDM_DRIVER:
697       pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
698       break;
699     case OPTION_TERMINAL_SERVER_AWARE:
700       pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
701       break;
702     }
703
704   /*  Set DLLCharacteristics bits  */
705   set_pep_name ("__dll_characteristics__", pe_dll_characteristics);
706  
707   return TRUE;
708 }
709 \f
710
711 #ifdef DLL_SUPPORT
712 static unsigned long
713 strhash (const char *str)
714 {
715   const unsigned char *s;
716   unsigned long hash;
717   unsigned int c;
718   unsigned int len;
719
720   hash = 0;
721   len = 0;
722   s = (const unsigned char *) str;
723   while ((c = *s++) != '\0')
724     {
725       hash += c + (c << 17);
726       hash ^= hash >> 2;
727       ++len;
728     }
729   hash += len + (len << 17);
730   hash ^= hash >> 2;
731
732   return hash;
733 }
734
735 /* Use the output file to create a image base for relocatable DLLs.  */
736
737 static bfd_vma
738 compute_dll_image_base (const char *ofile)
739 {
740   bfd_vma hash = (bfd_vma) strhash (ofile);
741   return 0x61300000 + ((hash << 16) & 0x0FFC0000);
742 }
743 #endif
744
745 /* Assign values to the special symbols before the linker script is
746    read.  */
747
748 static void
749 gld_${EMULATION_NAME}_set_symbols (void)
750 {
751   /* Run through and invent symbols for all the
752      names and insert the defaults.  */
753   int j;
754
755   if (!init[IMAGEBASEOFF].inited)
756     {
757       if (link_info.relocatable)
758         init[IMAGEBASEOFF].value = 0;
759       else if (init[DLLOFF].value || (link_info.shared && !link_info.pie))
760 #ifdef DLL_SUPPORT
761         init[IMAGEBASEOFF].value = (pep_enable_auto_image_base) ?
762           compute_dll_image_base (output_filename) : NT_DLL_IMAGE_BASE;
763 #else
764         init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
765 #endif
766       else
767         init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
768         init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
769     }
770
771   /* Don't do any symbol assignments if this is a relocatable link.  */
772   if (link_info.relocatable)
773     return;
774
775   /* Glue the assignments into the abs section.  */
776   push_stat_ptr (&abs_output_section->children);
777
778   for (j = 0; init[j].ptr; j++)
779     {
780       bfd_vma val = init[j].value;
781       lang_assignment_statement_type *rv;
782       rv = lang_add_assignment (exp_assop ('=', init[j].symbol,
783                                            exp_intop (val)));
784       if (init[j].size == sizeof (short))
785         *(short *) init[j].ptr = (short) val;
786       else if (init[j].size == sizeof (int))
787         *(int *) init[j].ptr = (int) val;
788       else if (init[j].size == sizeof (long))
789         *(long *) init[j].ptr = (long) val;
790       /* This might be a long long or other special type.  */
791       else if (init[j].size == sizeof (bfd_vma))
792         *(bfd_vma *) init[j].ptr = val;
793       else      abort ();
794       if (j == IMAGEBASEOFF)
795         image_base_statement = rv;
796     }
797   /* Restore the pointer.  */
798   pop_stat_ptr ();
799
800   if (pep.FileAlignment > pep.SectionAlignment)
801     {
802       einfo (_("%P: warning, file alignment > section alignment.\n"));
803     }
804 }
805
806 /* This is called after the linker script and the command line options
807    have been read.  */
808
809 static void
810 gld_${EMULATION_NAME}_after_parse (void)
811 {
812   /* The Windows libraries are designed for the linker to treat the
813      entry point as an undefined symbol.  Otherwise, the .obj that
814      defines mainCRTStartup is brought in because it is the first
815      encountered in libc.lib and it has other symbols in it which will
816      be pulled in by the link process.  To avoid this, we act as
817      though the user specified -u with the entry point symbol.
818
819      This function is called after the linker script and command line
820      options have been read, so at this point we know the right entry
821      point.  This function is called before the input files are
822      opened, so registering the symbol as undefined will make a
823      difference.  */
824
825   if (! link_info.relocatable && entry_symbol.name != NULL)
826     ldlang_add_undef (entry_symbol.name);
827
828   /* PR ld/6744:  Warn the user if they have used an ELF-only
829      option hoping it will work on PE+.  */
830   if (link_info.export_dynamic)
831     einfo (_("%P: warning: --export-dynamic is not supported for PE+ "
832       "targets, did you mean --export-all-symbols?\n"));
833 }
834
835 /* pep-dll.c directly accesses pep_data_import_dll,
836    so it must be defined outside of #ifdef DLL_SUPPORT.
837    Note - this variable is deliberately not initialised.
838    This allows it to be treated as a common varaible, and only
839    exist in one incarnation in a multiple target enabled linker.  */
840 char * pep_data_import_dll;
841
842 #ifdef DLL_SUPPORT
843 static struct bfd_link_hash_entry *pep_undef_found_sym;
844
845 static bfd_boolean
846 pep_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
847 {
848   int sl;
849   char *string = inf;
850
851   sl = strlen (string);
852   if (h->type == bfd_link_hash_defined
853       && strncmp (h->root.string, string, sl) == 0
854       && h->root.string[sl] == '@')
855     {
856       pep_undef_found_sym = h;
857       return FALSE;
858     }
859   return TRUE;
860 }
861
862 static void
863 pep_fixup_stdcalls (void)
864 {
865   static int gave_warning_message = 0;
866   struct bfd_link_hash_entry *undef, *sym;
867
868   if (pep_dll_extra_pe_debug)
869     printf ("%s\n", __FUNCTION__);
870
871   for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
872     if (undef->type == bfd_link_hash_undefined)
873       {
874         char* at = strchr (undef->root.string, '@');
875         int lead_at = (*undef->root.string == '@');
876         /* For now, don't try to fixup fastcall symbols.  */
877
878         if (at && !lead_at)
879           {
880             /* The symbol is a stdcall symbol, so let's look for a
881                cdecl symbol with the same name and resolve to that.  */
882             char *cname = xstrdup (undef->root.string /* + lead_at */);
883             at = strchr (cname, '@');
884             *at = 0;
885             sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
886
887             if (sym && sym->type == bfd_link_hash_defined)
888               {
889                 undef->type = bfd_link_hash_defined;
890                 undef->u.def.value = sym->u.def.value;
891                 undef->u.def.section = sym->u.def.section;
892
893                 if (pep_enable_stdcall_fixup == -1)
894                   {
895                     einfo (_("Warning: resolving %s by linking to %s\n"),
896                            undef->root.string, cname);
897                     if (! gave_warning_message)
898                       {
899                         gave_warning_message = 1;
900                         einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
901                         einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
902                       }
903                   }
904               }
905           }
906         else
907           {
908             /* The symbol is a cdecl symbol, so we look for stdcall
909                symbols - which means scanning the whole symbol table.  */
910             pep_undef_found_sym = 0;
911             bfd_link_hash_traverse (link_info.hash, pep_undef_cdecl_match,
912                                     (char *) undef->root.string);
913             sym = pep_undef_found_sym;
914             if (sym)
915               {
916                 undef->type = bfd_link_hash_defined;
917                 undef->u.def.value = sym->u.def.value;
918                 undef->u.def.section = sym->u.def.section;
919
920                 if (pep_enable_stdcall_fixup == -1)
921                   {
922                     einfo (_("Warning: resolving %s by linking to %s\n"),
923                            undef->root.string, sym->root.string);
924                     if (! gave_warning_message)
925                       {
926                         gave_warning_message = 1;
927                         einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
928                         einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
929                       }
930                   }
931               }
932           }
933       }
934 }
935
936 static int
937 make_import_fixup (arelent *rel, asection *s)
938 {
939   struct bfd_symbol *sym = *rel->sym_ptr_ptr;
940   char addend[8];
941   bfd_vma _addend = 0;
942   int suc = 0;
943
944   if (pep_dll_extra_pe_debug)
945     printf ("arelent: %s@%#lx: add=%li\n", sym->name,
946             (unsigned long) rel->address, (long) rel->addend);
947
948   memset (addend, 0, sizeof (addend));
949   switch ((rel->howto->bitsize))
950     {
951       case 8:
952         suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 1);
953         if (suc && rel->howto->pc_relative)
954           _addend = (bfd_vma) ((bfd_signed_vma) ((char) bfd_get_8 (s->owner, addend)));
955         else if (suc)
956           _addend = ((bfd_vma) bfd_get_8 (s->owner, addend)) & 0xff;
957         break;
958       case 16:
959         suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 2);
960         if (suc && rel->howto->pc_relative)
961           _addend = (bfd_vma) ((bfd_signed_vma) ((short) bfd_get_16 (s->owner, addend)));
962         else if (suc)
963           _addend = ((bfd_vma) bfd_get_16 (s->owner, addend)) & 0xffff;
964         break;
965       case 32:
966         suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 4);
967         if (suc && rel->howto->pc_relative)
968           _addend = (bfd_vma) ((bfd_signed_vma) ((int) bfd_get_32 (s->owner, addend)));
969         else if (suc)
970           _addend = ((bfd_vma) bfd_get_32 (s->owner, addend)) & 0xffffffff;
971         break;
972       case 64:
973         suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 8);
974         if (suc)
975           _addend = ((bfd_vma) bfd_get_64 (s->owner, addend));
976         break;
977     }
978   if (! suc)
979     einfo (_("%C: Cannot get section contents - auto-import exception\n"),
980            s->owner, s, rel->address);
981
982   if (pep_dll_extra_pe_debug)
983     {
984       printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx", (long) _addend, (long) rel->addend, (long) rel->address);
985       if (rel->howto->pc_relative) printf (" pcrel");
986       printf (" %d bit rel.\n",(int) rel->howto->bitsize);
987   }
988   pep_create_import_fixup (rel, s, _addend);
989
990   return 1;
991 }
992
993 static void
994 pep_find_data_imports (void)
995 {
996   struct bfd_link_hash_entry *undef, *sym;
997
998   if (link_info.pei386_auto_import == 0)
999     return;
1000
1001   for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
1002     {
1003       if (undef->type == bfd_link_hash_undefined)
1004         {
1005           /* C++ symbols are *long*.  */
1006           char buf[4096];
1007
1008           if (pep_dll_extra_pe_debug)
1009             printf ("%s:%s\n", __FUNCTION__, undef->root.string);
1010
1011           sprintf (buf, "__imp_%s", undef->root.string);
1012
1013           sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
1014
1015           if (sym && sym->type == bfd_link_hash_defined)
1016             {
1017               bfd *b = sym->u.def.section->owner;
1018               asymbol **symbols;
1019               int nsyms, i;
1020
1021               if (link_info.pei386_auto_import == -1)
1022                 {
1023                   static bfd_boolean warned = FALSE;
1024
1025                   info_msg (_("Info: resolving %s by linking to %s (auto-import)\n"),
1026                             undef->root.string, buf);
1027
1028                   /* PR linker/4844.  */
1029                   if (! warned)
1030                     {
1031                       warned = TRUE;
1032                       einfo (_("%P: warning: auto-importing has been activated without --enable-auto-import specified on the command line.\n\
1033 This should work unless it involves constant data structures referencing symbols from auto-imported DLLs.\n"));
1034                     }
1035                 }
1036
1037               if (!bfd_generic_link_read_symbols (b))
1038                 {
1039                   einfo (_("%B%F: could not read symbols: %E\n"), b);
1040                   return;
1041                 }
1042
1043               symbols = bfd_get_outsymbols (b);
1044               nsyms = bfd_get_symcount (b);
1045
1046               for (i = 0; i < nsyms; i++)
1047                 {
1048                   if (! CONST_STRNEQ (symbols[i]->name, "__head_"))
1049                     continue;
1050
1051                   if (pep_dll_extra_pe_debug)
1052                     printf ("->%s\n", symbols[i]->name);
1053
1054                   pep_data_import_dll = (char*) (symbols[i]->name +
1055                                                 sizeof ("__head_") - 1);
1056                   break;
1057                 }
1058
1059               pep_walk_relocs_of_symbol (&link_info, undef->root.string,
1060                                         make_import_fixup);
1061
1062               /* Let's differentiate it somehow from defined.  */
1063               undef->type = bfd_link_hash_defweak;
1064               /* We replace original name with __imp_ prefixed, this
1065                  1) may trash memory 2) leads to duplicate symbol generation.
1066                  Still, IMHO it's better than having name poluted.  */
1067               undef->root.string = sym->root.string;
1068               undef->u.def.value = sym->u.def.value;
1069               undef->u.def.section = sym->u.def.section;
1070             }
1071         }
1072     }
1073 }
1074
1075 static bfd_boolean
1076 pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
1077 {
1078   if (pep_dll_extra_pe_debug)
1079     printf ("+%s\n", h->string);
1080
1081   return TRUE;
1082 }
1083 #endif /* DLL_SUPPORT */
1084
1085 static void 
1086 debug_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
1087 {
1088   int *found = (int *) obj;
1089   if (strncmp (".debug_", sect->name, sizeof (".debug_") - 1) == 0)
1090     *found = 1;
1091 }
1092
1093 static void
1094 gld_${EMULATION_NAME}_after_open (void)
1095 {
1096 #ifdef DLL_SUPPORT
1097   if (pep_dll_extra_pe_debug)
1098     {
1099       bfd *a;
1100       struct bfd_link_hash_entry *sym;
1101
1102       printf ("%s()\n", __FUNCTION__);
1103
1104       for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1105         printf ("-%s\n", sym->root.string);
1106       bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
1107
1108       for (a = link_info.input_bfds; a; a = a->link_next)
1109         printf ("*%s\n",a->filename);
1110     }
1111 #endif
1112
1113   /* Pass the wacky PE command line options into the output bfd.
1114      FIXME: This should be done via a function, rather than by
1115      including an internal BFD header.  */
1116
1117   if (coff_data (link_info.output_bfd) == NULL
1118       || coff_data (link_info.output_bfd)->pe == 0)
1119     einfo (_("%F%P: cannot perform PE operations on non PE output file '%B'.\n"),
1120            link_info.output_bfd);
1121
1122   pe_data (link_info.output_bfd)->pe_opthdr = pep;
1123   pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
1124   pe_data (link_info.output_bfd)->real_flags |= real_flags;
1125
1126   /* At this point we must decide whether to use long section names
1127      in the output or not.  If the user hasn't explicitly specified
1128      on the command line, we leave it to the default for the format
1129      (object files yes, image files no), except if there is debug
1130      information present; GDB relies on the long section names to
1131      find it, so enable it in that case.  */
1132   if (pep_use_coff_long_section_names < 0 && link_info.strip == strip_none)
1133     {
1134       /* Iterate over all sections of all input BFDs, checking
1135          for any that begin 'debug_' and are long names.  */
1136       LANG_FOR_EACH_INPUT_STATEMENT (is)
1137         {
1138           int found_debug = 0;
1139           bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug);
1140           if (found_debug)
1141             {
1142               pep_use_coff_long_section_names = 1;
1143               break;
1144             }
1145         }
1146     }
1147
1148   pep_output_file_set_long_section_names (link_info.output_bfd);
1149
1150 #ifdef DLL_SUPPORT
1151   if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
1152     pep_fixup_stdcalls ();
1153
1154   pep_process_import_defs (link_info.output_bfd, &link_info);
1155
1156   pep_find_data_imports ();
1157
1158 #ifndef TARGET_IS_i386pep
1159   if (link_info.shared)
1160 #else
1161   if (!link_info.relocatable)
1162 #endif
1163     pep_dll_build_sections (link_info.output_bfd, &link_info);
1164
1165 #ifndef TARGET_IS_i386pep
1166   else
1167     pep_exe_build_sections (link_info.output_bfd, &link_info);
1168 #endif
1169 #endif /* DLL_SUPPORT */
1170
1171   {
1172     /* This next chunk of code tries to detect the case where you have
1173        two import libraries for the same DLL (specifically,
1174        symbolically linking libm.a and libc.a in cygwin to
1175        libcygwin.a).  In those cases, it's possible for function
1176        thunks from the second implib to be used but without the
1177        head/tail objects, causing an improper import table.  We detect
1178        those cases and rename the "other" import libraries to match
1179        the one the head/tail come from, so that the linker will sort
1180        things nicely and produce a valid import table.  */
1181
1182     LANG_FOR_EACH_INPUT_STATEMENT (is)
1183       {
1184         if (is->the_bfd->my_archive)
1185           {
1186             int idata2 = 0, reloc_count=0, is_imp = 0;
1187             asection *sec;
1188
1189             /* See if this is an import library thunk.  */
1190             for (sec = is->the_bfd->sections; sec; sec = sec->next)
1191               {
1192                 if (strcmp (sec->name, ".idata\$2") == 0)
1193                   idata2 = 1;
1194                 if (CONST_STRNEQ (sec->name, ".idata\$"))
1195                   is_imp = 1;
1196                 reloc_count += sec->reloc_count;
1197               }
1198
1199             if (is_imp && !idata2 && reloc_count)
1200               {
1201                 /* It is, look for the reference to head and see if it's
1202                    from our own library.  */
1203                 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1204                   {
1205                     int i;
1206                     long relsize;
1207                     asymbol **symbols;
1208                     arelent **relocs;
1209                     int nrelocs;
1210
1211                     relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1212                     if (relsize < 1)
1213                       break;
1214
1215                     if (!bfd_generic_link_read_symbols (is->the_bfd))
1216                       {
1217                         einfo (_("%B%F: could not read symbols: %E\n"),
1218                                is->the_bfd);
1219                         return;
1220                       }
1221                     symbols = bfd_get_outsymbols (is->the_bfd);
1222
1223                     relocs = xmalloc ((size_t) relsize);
1224                     nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1225                                                       relocs, symbols);
1226                     if (nrelocs < 0)
1227                       {
1228                         free (relocs);
1229                         einfo ("%X%P: unable to process relocs: %E\n");
1230                         return;
1231                       }
1232
1233                     for (i = 0; i < nrelocs; i++)
1234                       {
1235                         struct bfd_symbol *s;
1236                         struct bfd_link_hash_entry * blhe;
1237                         char *other_bfd_filename;
1238                         char *n;
1239
1240                         s = (relocs[i]->sym_ptr_ptr)[0];
1241
1242                         if (s->flags & BSF_LOCAL)
1243                           continue;
1244
1245                         /* Thunk section with reloc to another bfd.  */
1246                         blhe = bfd_link_hash_lookup (link_info.hash,
1247                                                      s->name,
1248                                                      FALSE, FALSE, TRUE);
1249
1250                         if (blhe == NULL
1251                             || blhe->type != bfd_link_hash_defined)
1252                           continue;
1253
1254                         other_bfd_filename
1255                           = blhe->u.def.section->owner->my_archive
1256                             ? bfd_get_filename (blhe->u.def.section->owner->my_archive)
1257                             : bfd_get_filename (blhe->u.def.section->owner);
1258
1259                         if (strcmp (bfd_get_filename (is->the_bfd->my_archive),
1260                                     other_bfd_filename) == 0)
1261                           continue;
1262
1263                         /* Rename this implib to match the other one.  */
1264                         n = xmalloc (strlen (other_bfd_filename) + 1);
1265                         strcpy (n, other_bfd_filename);
1266                         is->the_bfd->my_archive->filename = n;
1267                       }
1268
1269                     free (relocs);
1270                     /* Note - we do not free the symbols,
1271                        they are now cached in the BFD.  */
1272                   }
1273               }
1274           }
1275       }
1276   }
1277
1278   {
1279     int is_ms_arch = 0;
1280     bfd *cur_arch = 0;
1281     lang_input_statement_type *is2;
1282     lang_input_statement_type *is3;
1283
1284     /* Careful - this is a shell script.  Watch those dollar signs! */
1285     /* Microsoft import libraries have every member named the same,
1286        and not in the right order for us to link them correctly.  We
1287        must detect these and rename the members so that they'll link
1288        correctly.  There are three types of objects: the head, the
1289        thunks, and the sentinel(s).  The head is easy; it's the one
1290        with idata2.  We assume that the sentinels won't have relocs,
1291        and the thunks will.  It's easier than checking the symbol
1292        table for external references.  */
1293     LANG_FOR_EACH_INPUT_STATEMENT (is)
1294       {
1295         if (is->the_bfd->my_archive)
1296           {
1297             char *pnt;
1298             bfd *arch = is->the_bfd->my_archive;
1299
1300             if (cur_arch != arch)
1301               {
1302                 cur_arch = arch;
1303                 is_ms_arch = 1;
1304
1305                 for (is3 = is;
1306                      is3 && is3->the_bfd->my_archive == arch;
1307                      is3 = (lang_input_statement_type *) is3->next)
1308                   {
1309                     /* A MS dynamic import library can also contain static
1310                        members, so look for the first element with a .dll
1311                        extension, and use that for the remainder of the
1312                        comparisons.  */
1313                     pnt = strrchr (is3->the_bfd->filename, '.');
1314                     if (pnt != NULL && strcmp (pnt, ".dll") == 0)
1315                       break;
1316                   }
1317
1318                 if (is3 == NULL)
1319                   is_ms_arch = 0;
1320                 else
1321                   {
1322                     /* OK, found one.  Now look to see if the remaining
1323                        (dynamic import) members use the same name.  */
1324                     for (is2 = is;
1325                          is2 && is2->the_bfd->my_archive == arch;
1326                          is2 = (lang_input_statement_type *) is2->next)
1327                       {
1328                         /* Skip static members, ie anything with a .obj
1329                            extension.  */
1330                         pnt = strrchr (is2->the_bfd->filename, '.');
1331                         if (pnt != NULL && strcmp (pnt, ".obj") == 0)
1332                           continue;
1333
1334                         if (strcmp (is3->the_bfd->filename,
1335                                     is2->the_bfd->filename))
1336                           {
1337                             is_ms_arch = 0;
1338                             break;
1339                           }
1340                       }
1341                   }
1342               }
1343
1344             /* This fragment might have come from an .obj file in a Microsoft
1345                import, and not an actual import record. If this is the case,
1346                then leave the filename alone.  */
1347             pnt = strrchr (is->the_bfd->filename, '.');
1348
1349             if (is_ms_arch && (strcmp (pnt, ".dll") == 0))
1350               {
1351                 int idata2 = 0, reloc_count=0;
1352                 asection *sec;
1353                 char *new_name, seq;
1354
1355                 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1356                   {
1357                     if (strcmp (sec->name, ".idata\$2") == 0)
1358                       idata2 = 1;
1359                     reloc_count += sec->reloc_count;
1360                   }
1361
1362                 if (idata2) /* .idata2 is the TOC */
1363                   seq = 'a';
1364                 else if (reloc_count > 0) /* thunks */
1365                   seq = 'b';
1366                 else /* sentinel */
1367                   seq = 'c';
1368
1369                 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
1370                 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
1371                 is->the_bfd->filename = new_name;
1372
1373                 new_name = xmalloc (strlen (is->filename) + 3);
1374                 sprintf (new_name, "%s.%c", is->filename, seq);
1375                 is->filename = new_name;
1376               }
1377           }
1378       }
1379   }
1380 }
1381 \f
1382 static void
1383 gld_${EMULATION_NAME}_before_allocation (void)
1384 {
1385   before_allocation_default ();
1386 }
1387 \f
1388 #ifdef DLL_SUPPORT
1389 /* This is called when an input file isn't recognized as a BFD.  We
1390    check here for .DEF files and pull them in automatically.  */
1391
1392 static int
1393 saw_option (char *option)
1394 {
1395   int i;
1396
1397   for (i = 0; init[i].ptr; i++)
1398     if (strcmp (init[i].symbol, option) == 0)
1399       return init[i].inited;
1400   return 0;
1401 }
1402 #endif /* DLL_SUPPORT */
1403
1404 static bfd_boolean
1405 gld_${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1406 {
1407 #ifdef DLL_SUPPORT
1408   const char *ext = entry->filename + strlen (entry->filename) - 4;
1409
1410   if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
1411     {
1412       pep_def_file = def_file_parse (entry->filename, pep_def_file);
1413
1414       if (pep_def_file)
1415         {
1416           int i, buflen=0, len;
1417           char *buf;
1418
1419           for (i = 0; i < pep_def_file->num_exports; i++)
1420             {
1421               len = strlen (pep_def_file->exports[i].internal_name);
1422               if (buflen < len + 2)
1423                 buflen = len + 2;
1424             }
1425
1426           buf = xmalloc (buflen);
1427
1428           for (i = 0; i < pep_def_file->num_exports; i++)
1429             {
1430               struct bfd_link_hash_entry *h;
1431
1432               sprintf (buf, "_%s", pep_def_file->exports[i].internal_name);
1433
1434               h = bfd_link_hash_lookup (link_info.hash, buf, TRUE, TRUE, TRUE);
1435               if (h == (struct bfd_link_hash_entry *) NULL)
1436                 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1437               if (h->type == bfd_link_hash_new)
1438                 {
1439                   h->type = bfd_link_hash_undefined;
1440                   h->u.undef.abfd = NULL;
1441                   bfd_link_add_undef (link_info.hash, h);
1442                 }
1443             }
1444           free (buf);
1445
1446           /* def_file_print (stdout, pep_def_file); */
1447           if (pep_def_file->is_dll == 1)
1448             link_info.shared = 1;
1449
1450           if (pep_def_file->base_address != (bfd_vma)(-1))
1451             {
1452               pep.ImageBase
1453                 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1454                 = init[IMAGEBASEOFF].value
1455                 = pep_def_file->base_address;
1456               init[IMAGEBASEOFF].inited = 1;
1457               if (image_base_statement)
1458                 image_base_statement->exp = exp_assop ('=', "__image_base__",
1459                                                        exp_intop (pep.ImageBase));
1460             }
1461
1462           if (pep_def_file->stack_reserve != -1
1463               && ! saw_option ("__size_of_stack_reserve__"))
1464             {
1465               pep.SizeOfStackReserve = pep_def_file->stack_reserve;
1466               if (pep_def_file->stack_commit != -1)
1467                 pep.SizeOfStackCommit = pep_def_file->stack_commit;
1468             }
1469           if (pep_def_file->heap_reserve != -1
1470               && ! saw_option ("__size_of_heap_reserve__"))
1471             {
1472               pep.SizeOfHeapReserve = pep_def_file->heap_reserve;
1473               if (pep_def_file->heap_commit != -1)
1474                 pep.SizeOfHeapCommit = pep_def_file->heap_commit;
1475             }
1476           return TRUE;
1477         }
1478     }
1479 #endif
1480   return FALSE;
1481 }
1482
1483 static bfd_boolean
1484 gld_${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1485 {
1486 #ifdef DLL_SUPPORT
1487 #ifdef TARGET_IS_i386pep
1488   pep_dll_id_target ("pei-x86-64");
1489 #endif
1490   if (pep_bfd_is_dll (entry->the_bfd))
1491     return pep_implied_import_dll (entry->filename);
1492 #endif
1493   return FALSE;
1494 }
1495
1496 static void
1497 gld_${EMULATION_NAME}_finish (void)
1498 {
1499   finish_default ();
1500
1501 #ifdef DLL_SUPPORT
1502   if (link_info.shared
1503       || (!link_info.relocatable && pep_def_file->num_exports != 0))
1504     {
1505       pep_dll_fill_sections (link_info.output_bfd, &link_info);
1506       if (pep_implib_filename)
1507         pep_dll_generate_implib (pep_def_file, pep_implib_filename, &link_info);
1508     }
1509
1510   if (pep_out_def_filename)
1511     pep_dll_generate_def_file (pep_out_def_filename);
1512 #endif /* DLL_SUPPORT */
1513
1514   /* I don't know where .idata gets set as code, but it shouldn't be.  */
1515   {
1516     asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
1517
1518     if (asec)
1519       {
1520         asec->flags &= ~SEC_CODE;
1521         asec->flags |= SEC_DATA;
1522       }
1523   }
1524 }
1525
1526 \f
1527 /* Place an orphan section.
1528
1529    We use this to put sections in a reasonable place in the file, and
1530    to ensure that they are aligned as required.
1531
1532    We handle grouped sections here as well.  A section named .foo\$nn
1533    goes into the output section .foo.  All grouped sections are sorted
1534    by name.
1535
1536    Grouped sections for the default sections are handled by the
1537    default linker script using wildcards, and are sorted by
1538    sort_sections.  */
1539
1540 static lang_output_section_statement_type *
1541 gld_${EMULATION_NAME}_place_orphan (asection *s,
1542                                     const char *secname,
1543                                     int constraint)
1544 {
1545   const char *orig_secname = secname;
1546   char *dollar = NULL;
1547   lang_output_section_statement_type *os;
1548   lang_statement_list_type add_child;
1549   lang_statement_union_type **pl;
1550
1551   /* Look through the script to see where to place this section.  */
1552   if (!link_info.relocatable
1553       && (dollar = strchr (secname, '\$')) != NULL)
1554     {
1555       size_t len = dollar - secname;
1556       char *newname = xmalloc (len + 1);
1557       memcpy (newname, secname, len);
1558       newname[len] = '\0';
1559       secname = newname;
1560     }
1561
1562   lang_list_init (&add_child);
1563
1564   if (constraint == 0
1565       && (os = lang_output_section_find (secname)) != NULL
1566       && os->bfd_section != NULL
1567       && (os->bfd_section->flags == 0
1568           || ((s->flags ^ os->bfd_section->flags)
1569               & (SEC_LOAD | SEC_ALLOC)) == 0))
1570     {
1571       /* We already have an output section statement with this
1572          name, and its bfd section has compatible flags.
1573          If the section already exists but does not have any flags set,
1574          then it has been created by the linker, probably as a result of
1575          a --section-start command line switch.  */
1576       lang_add_section (&add_child, s, os);
1577     }
1578   else
1579     {
1580       static struct orphan_save hold[] =
1581         {
1582           { ".text",
1583             SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1584             0, 0, 0, 0 },
1585           { ".rdata",
1586             SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1587             0, 0, 0, 0 },
1588           { ".data",
1589             SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1590             0, 0, 0, 0 },
1591           { ".bss",
1592             SEC_ALLOC,
1593             0, 0, 0, 0 }
1594         };
1595       enum orphan_save_index
1596         {
1597           orphan_text = 0,
1598           orphan_rodata,
1599           orphan_data,
1600           orphan_bss
1601         };
1602       static int orphan_init_done = 0;
1603       struct orphan_save *place;
1604       lang_output_section_statement_type *after;
1605       etree_type *address;
1606
1607       if (!orphan_init_done)
1608         {
1609           struct orphan_save *ho;
1610           for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1611             if (ho->name != NULL)
1612               {
1613                 ho->os = lang_output_section_find (ho->name);
1614                 if (ho->os != NULL && ho->os->flags == 0)
1615                   ho->os->flags = ho->flags;
1616               }
1617           orphan_init_done = 1;
1618         }
1619
1620       /* Try to put the new output section in a reasonable place based
1621          on the section name and section flags.  */
1622
1623       place = NULL;
1624       if ((s->flags & SEC_ALLOC) == 0)
1625         ;
1626       else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
1627         place = &hold[orphan_bss];
1628       else if ((s->flags & SEC_READONLY) == 0)
1629         place = &hold[orphan_data];
1630       else if ((s->flags & SEC_CODE) == 0)
1631         place = &hold[orphan_rodata];
1632       else
1633         place = &hold[orphan_text];
1634
1635       after = NULL;
1636       if (place != NULL)
1637         {
1638           if (place->os == NULL)
1639             place->os = lang_output_section_find (place->name);
1640           after = place->os;
1641           if (after == NULL)
1642             after = lang_output_section_find_by_flags (s, &place->os, NULL);
1643           if (after == NULL)
1644             /* *ABS* is always the first output section statement.  */
1645             after = (&lang_output_section_statement.head
1646                      ->output_section_statement);
1647         }
1648
1649       /* All sections in an executable must be aligned to a page boundary.  */
1650       address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
1651       os = lang_insert_orphan (s, secname, constraint, after, place, address,
1652                                &add_child);
1653     }
1654
1655   /* If the section name has a '\$', sort it with the other '\$'
1656      sections.  */
1657   for (pl = &os->children.head; *pl != NULL; pl = &(*pl)->header.next)
1658     {
1659       lang_input_section_type *ls;
1660       const char *lname;
1661
1662       if ((*pl)->header.type != lang_input_section_enum)
1663         continue;
1664
1665       ls = &(*pl)->input_section;
1666
1667       lname = bfd_get_section_name (ls->section->owner, ls->section);
1668       if (strchr (lname, '\$') != NULL
1669           && (dollar == NULL || strcmp (orig_secname, lname) < 0))
1670         break;
1671     }
1672
1673   if (add_child.head != NULL)
1674     {
1675       *add_child.tail = *pl;
1676       *pl = add_child.head;
1677     }
1678
1679   return os;
1680 }
1681
1682 static bfd_boolean
1683 gld_${EMULATION_NAME}_open_dynamic_archive
1684   (const char *arch ATTRIBUTE_UNUSED,
1685    search_dirs_type *search,
1686    lang_input_statement_type *entry)
1687 {
1688   static const struct
1689     {
1690       const char * format;
1691       bfd_boolean use_prefix;
1692     }
1693   libname_fmt [] =
1694     {
1695       /* Preferred explicit import library for dll's.  */
1696       { "lib%s.dll.a", FALSE },
1697       /* Alternate explicit import library for dll's.  */
1698       { "%s.dll.a", FALSE },
1699       /* "libfoo.a" could be either an import lib or a static lib.
1700           For backwards compatibility, libfoo.a needs to precede
1701           libfoo.dll and foo.dll in the search.  */
1702       { "lib%s.a", FALSE },
1703       /* The 'native' spelling of an import lib name is "foo.lib".  */
1704       { "%s.lib", FALSE },
1705 #ifdef DLL_SUPPORT
1706       /* Try "<prefix>foo.dll" (preferred dll name, if specified).  */
1707       { "%s%s.dll", TRUE },
1708 #endif
1709       /* Try "libfoo.dll" (default preferred dll name).  */
1710       { "lib%s.dll", FALSE },
1711       /* Finally try 'native' dll name "foo.dll".  */
1712       {  "%s.dll", FALSE },
1713       /* Note: If adding more formats to this table, make sure to check to
1714          see if their length is longer than libname_fmt[0].format, and if
1715          so, update the call to xmalloc() below.  */
1716       { NULL, FALSE }
1717     };
1718   static unsigned int format_max_len = 0;
1719   const char * filename;
1720   char * full_string;
1721   char * base_string;
1722   unsigned int i;
1723
1724
1725   if (! entry->is_archive)
1726     return FALSE;
1727
1728   filename = entry->filename;
1729
1730   if (format_max_len == 0)
1731     /* We need to allow space in the memory that we are going to allocate
1732        for the characters in the format string.  Since the format array is
1733        static we only need to calculate this information once.  In theory
1734        this value could also be computed statically, but this introduces
1735        the possibility for a discrepancy and hence a possible memory
1736        corruption.  The lengths we compute here will be too long because
1737        they will include any formating characters (%s) in the strings, but
1738        this will not matter.  */
1739     for (i = 0; libname_fmt[i].format; i++)
1740       if (format_max_len < strlen (libname_fmt[i].format))
1741         format_max_len = strlen (libname_fmt[i].format);
1742
1743   full_string = xmalloc (strlen (search->name)
1744                          + strlen (filename)
1745                          + format_max_len
1746 #ifdef DLL_SUPPORT
1747                          + (pep_dll_search_prefix
1748                             ? strlen (pep_dll_search_prefix) : 0)
1749 #endif
1750                          /* Allow for the terminating NUL and for the path
1751                             separator character that is inserted between
1752                             search->name and the start of the format string.  */
1753                          + 2);
1754
1755   sprintf (full_string, "%s/", search->name);
1756   base_string = full_string + strlen (full_string);
1757
1758   for (i = 0; libname_fmt[i].format; i++)
1759     {
1760 #ifdef DLL_SUPPORT
1761       if (libname_fmt[i].use_prefix)
1762         {
1763           if (!pep_dll_search_prefix)
1764             continue;
1765           sprintf (base_string, libname_fmt[i].format, pep_dll_search_prefix, filename);
1766         }
1767       else
1768 #endif
1769         sprintf (base_string, libname_fmt[i].format, filename);
1770
1771       if (ldfile_try_open_bfd (full_string, entry))
1772         break;
1773     }
1774
1775   if (!libname_fmt[i].format)
1776     {
1777       free (full_string);
1778       return FALSE;
1779     }
1780
1781   entry->filename = full_string;
1782
1783   return TRUE;
1784 }
1785
1786 static int
1787 gld_${EMULATION_NAME}_find_potential_libraries
1788   (char *name, lang_input_statement_type *entry)
1789 {
1790   return ldfile_open_file_search (name, entry, "", ".lib");
1791 }
1792 \f
1793 static char *
1794 gld_${EMULATION_NAME}_get_script (int *isfile)
1795 EOF
1796 # Scripts compiled in.
1797 # sed commands to quote an ld script as a C string.
1798 sc="-f stringify.sed"
1799
1800 fragment <<EOF
1801 {
1802   *isfile = 0;
1803
1804   if (link_info.relocatable && config.build_constructors)
1805     return
1806 EOF
1807 sed $sc ldscripts/${EMULATION_NAME}.xu                  >> e${EMULATION_NAME}.c
1808 echo '  ; else if (link_info.relocatable) return'       >> e${EMULATION_NAME}.c
1809 sed $sc ldscripts/${EMULATION_NAME}.xr                  >> e${EMULATION_NAME}.c
1810 echo '  ; else if (!config.text_read_only) return'      >> e${EMULATION_NAME}.c
1811 sed $sc ldscripts/${EMULATION_NAME}.xbn                 >> e${EMULATION_NAME}.c
1812 echo '  ; else if (!config.magic_demand_paged) return'  >> e${EMULATION_NAME}.c
1813 sed $sc ldscripts/${EMULATION_NAME}.xn                  >> e${EMULATION_NAME}.c
1814 if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
1815 echo '  ; else if (link_info.pei386_auto_import == 1) return'   >> e${EMULATION_NAME}.c
1816 sed $sc ldscripts/${EMULATION_NAME}.xa                  >> e${EMULATION_NAME}.c
1817 fi
1818 echo '  ; else return'                                  >> e${EMULATION_NAME}.c
1819 sed $sc ldscripts/${EMULATION_NAME}.x                   >> e${EMULATION_NAME}.c
1820 echo '; }'                                              >> e${EMULATION_NAME}.c
1821
1822 fragment <<EOF
1823
1824
1825 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1826 {
1827   gld_${EMULATION_NAME}_before_parse,
1828   syslib_default,
1829   hll_default,
1830   gld_${EMULATION_NAME}_after_parse,
1831   gld_${EMULATION_NAME}_after_open,
1832   after_allocation_default,
1833   set_output_arch_default,
1834   ldemul_default_target,
1835   gld_${EMULATION_NAME}_before_allocation,
1836   gld_${EMULATION_NAME}_get_script,
1837   "${EMULATION_NAME}",
1838   "${OUTPUT_FORMAT}",
1839   gld_${EMULATION_NAME}_finish,
1840   NULL, /* Create output section statements.  */
1841   gld_${EMULATION_NAME}_open_dynamic_archive,
1842   gld_${EMULATION_NAME}_place_orphan,
1843   gld_${EMULATION_NAME}_set_symbols,
1844   NULL, /* parse_args */
1845   gld${EMULATION_NAME}_add_options,
1846   gld${EMULATION_NAME}_handle_option,
1847   gld_${EMULATION_NAME}_unrecognized_file,
1848   gld_${EMULATION_NAME}_list_options,
1849   gld_${EMULATION_NAME}_recognized_file,
1850   gld_${EMULATION_NAME}_find_potential_libraries,
1851   NULL  /* new_vers_pattern.  */
1852 };
1853 EOF