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