]> Git Repo - binutils.git/blob - ld/emultempl/pe.em
This commit was generated by cvs2svn to track changes on a CVS vendor
[binutils.git] / ld / emultempl / pe.em
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 cat >e${EMULATION_NAME}.c <<EOF
4 /* This file is part of GLD, the Gnu Linker.
5    Copyright 1995, 96, 97, 1998 Free Software Foundation, Inc.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /* For WINDOWS_NT */
22 /* The original file generated returned different default scripts depending
23    on whether certain switches were set, but these switches pertain to the
24    Linux system and that particular version of coff.  In the NT case, we
25    only determine if the subsystem is console or windows in order to select
26    the correct entry point by default. */ 
27   
28 #include "bfd.h"
29 #include "sysdep.h"
30 #include "bfdlink.h"
31 #include "getopt.h"
32 #include "libiberty.h"
33 #include "ld.h"
34 #include "ldmain.h"
35 #include "ldgram.h"
36 #include "ldexp.h"
37 #include "ldlang.h"
38 #include "ldemul.h"
39 #include "ldlex.h"
40 #include "ldmisc.h"
41 #include "ldctor.h"
42 #include "ldfile.h"
43 #include "coff/internal.h"
44 #include "../bfd/libcoff.h"
45 #include "deffile.h"
46
47 #define TARGET_IS_${EMULATION_NAME}
48
49 static void gld_${EMULATION_NAME}_set_symbols PARAMS ((void));
50 static void gld_${EMULATION_NAME}_after_open PARAMS ((void));
51 static void gld_${EMULATION_NAME}_before_parse PARAMS ((void));
52 static void gld_${EMULATION_NAME}_after_parse PARAMS ((void));
53 static void gld_${EMULATION_NAME}_before_allocation PARAMS ((void));
54 static boolean gld_${EMULATION_NAME}_place_orphan
55   PARAMS ((lang_input_statement_type *, asection *));
56 static void gld${EMULATION_NAME}_place_section
57   PARAMS ((lang_statement_union_type *));
58 static char *gld_${EMULATION_NAME}_get_script PARAMS ((int *));
59 static int gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **));
60
61 static struct internal_extra_pe_aouthdr pe;
62 static int dll;
63 static int support_old_code = 0;
64 extern def_file *pe_def_file;
65 static lang_assignment_statement_type *image_base_statement = 0;
66
67 static char *pe_out_def_filename = 0;
68 extern int pe_dll_export_everything;
69 extern int pe_dll_kill_ats;
70 extern int pe_dll_stdcall_aliases;
71 static int pe_enable_stdcall_fixup = -1; /* 0=disable 1=enable */
72 static char *pe_implib_filename = 0;
73
74 extern const char *output_filename;
75
76 static void
77 gld_${EMULATION_NAME}_before_parse()
78 {
79   output_filename = "a.exe";
80   ldfile_output_architecture = bfd_arch_${ARCH};
81 #ifdef TARGET_IS_i386pe
82   config.has_shared = 1;
83 #endif
84 }
85 \f
86 /* PE format extra command line options.  */
87
88 /* Used for setting flags in the PE header. */
89 #define OPTION_BASE_FILE                (300  + 1)
90 #define OPTION_DLL                      (OPTION_BASE_FILE + 1)
91 #define OPTION_FILE_ALIGNMENT           (OPTION_DLL + 1)
92 #define OPTION_IMAGE_BASE               (OPTION_FILE_ALIGNMENT + 1)
93 #define OPTION_MAJOR_IMAGE_VERSION      (OPTION_IMAGE_BASE + 1)
94 #define OPTION_MAJOR_OS_VERSION         (OPTION_MAJOR_IMAGE_VERSION + 1)
95 #define OPTION_MAJOR_SUBSYSTEM_VERSION  (OPTION_MAJOR_OS_VERSION + 1)
96 #define OPTION_MINOR_IMAGE_VERSION      (OPTION_MAJOR_SUBSYSTEM_VERSION + 1)
97 #define OPTION_MINOR_OS_VERSION         (OPTION_MINOR_IMAGE_VERSION + 1)
98 #define OPTION_MINOR_SUBSYSTEM_VERSION  (OPTION_MINOR_OS_VERSION + 1)
99 #define OPTION_SECTION_ALIGNMENT        (OPTION_MINOR_SUBSYSTEM_VERSION + 1)
100 #define OPTION_STACK                    (OPTION_SECTION_ALIGNMENT + 1)
101 #define OPTION_SUBSYSTEM                (OPTION_STACK + 1)
102 #define OPTION_HEAP                     (OPTION_SUBSYSTEM + 1)
103 #define OPTION_SUPPORT_OLD_CODE         (OPTION_HEAP + 1)
104 #define OPTION_OUT_DEF                  (OPTION_SUPPORT_OLD_CODE + 1)
105 #define OPTION_EXPORT_ALL               (OPTION_OUT_DEF + 1)
106 #define OPTION_EXCLUDE_SYMBOLS          (OPTION_EXPORT_ALL + 1)
107 #define OPTION_KILL_ATS                 (OPTION_EXCLUDE_SYMBOLS + 1)
108 #define OPTION_STDCALL_ALIASES          (OPTION_KILL_ATS + 1)
109 #define OPTION_ENABLE_STDCALL_FIXUP     (OPTION_STDCALL_ALIASES + 1)
110 #define OPTION_DISABLE_STDCALL_FIXUP    (OPTION_ENABLE_STDCALL_FIXUP + 1)
111 #define OPTION_IMPLIB_FILENAME          (OPTION_DISABLE_STDCALL_FIXUP + 1)
112
113 static struct option longopts[] =
114 {
115   /* PE options */
116   {"base-file", required_argument, NULL, OPTION_BASE_FILE},
117   {"dll", no_argument, NULL, OPTION_DLL},
118   {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
119   {"heap", required_argument, NULL, OPTION_HEAP}, 
120   {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
121   {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
122   {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
123   {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
124   {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
125   {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
126   {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
127   {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
128   {"stack", required_argument, NULL, OPTION_STACK},
129   {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
130   {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
131 #ifdef TARGET_IS_i386pe
132   /* getopt allows abbreviations, so we do this to stop it from treating -o
133      as an abbreviation for this option */
134   {"output-def", required_argument, NULL, OPTION_OUT_DEF},
135   {"output-def", required_argument, NULL, OPTION_OUT_DEF},
136   {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
137   {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
138   {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
139   {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
140   {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
141   {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
142   {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
143 #endif
144   {NULL, no_argument, NULL, 0}
145 };
146
147
148 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
149    parameters which may be input from the command line */
150
151 typedef struct
152 {
153   void *ptr;
154   int size;
155   int value;
156   char *symbol;
157   int inited;
158 } definfo;
159
160 #define D(field,symbol,def)  {&pe.field,sizeof(pe.field), def, symbol,0}
161
162 static definfo init[] =
163 {
164   /* imagebase must be first */
165 #define IMAGEBASEOFF 0
166   D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
167 #define DLLOFF 1
168   {&dll, sizeof(dll), 0, "__dll__"},
169   D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
170   D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
171   D(MajorOperatingSystemVersion,"__major_os_version__", 4),
172   D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
173   D(MajorImageVersion,"__major_image_version__", 1),
174   D(MinorImageVersion,"__minor_image_version__", 0),
175   D(MajorSubsystemVersion,"__major_subsystem_version__", 4),
176   D(MinorSubsystemVersion,"__minor_subsystem_version__", 0),
177   D(Subsystem,"__subsystem__", 3),
178   D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x2000000),
179   D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
180   D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
181   D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
182   D(LoaderFlags,"__loader_flags__", 0x0),
183   { NULL, 0, 0, NULL, 0 }
184 };
185
186 static void
187 gld_${EMULATION_NAME}_list_options (file)
188      FILE * file;
189 {
190   fprintf (file, _("  --base_file <basefile>             Generate a base file for relocatable DLLs\n"));
191   fprintf (file, _("  --dll                              Set image base to the default for DLLs\n"));
192   fprintf (file, _("  --file-alignment <size>            Set file alignment\n"));
193   fprintf (file, _("  --heap <size>                      Set initial size of the heap\n"));
194   fprintf (file, _("  --image-base <address>             Set start address of the executable\n"));
195   fprintf (file, _("  --major-image-version <number>     Set version number of the executable\n"));
196   fprintf (file, _("  --major-os-version <number>        Set minimum required OS version\n"));
197   fprintf (file, _("  --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
198   fprintf (file, _("  --minor-image-version <number>     Set revision number of the executable\n"));
199   fprintf (file, _("  --minor-os-version <number>        Set minimum required OS revision\n"));
200   fprintf (file, _("  --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
201   fprintf (file, _("  --section-alignment <size>         Set section alignment\n"));
202   fprintf (file, _("  --stack <size>                     Set size of the initial stack\n"));
203   fprintf (file, _("  --subsystem <name>[:<version>]     Set required OS subsystem [& version]\n"));
204   fprintf (file, _("  --support-old-code                 Support interworking with old code\n"));
205 #ifdef TARGET_IS_i386pe
206   fprintf (file, _("  --add-stdcall-alias                Export symbols with and without @nn\n"));
207   fprintf (file, _("  --disable-stdcall-fixup            Don't link _sym to _sym@nn\n"));
208   fprintf (file, _("  --enable-stdcall-fixup             Link _sym to _sym@nn without warnings\n"));
209   fprintf (file, _("  --exclude-symbols sym,sym,...      Exclude symbols from automatic export\n"));
210   fprintf (file, _("  --export-all-symbols               Automatically export all globals to DLL\n"));
211   fprintf (file, _("  --kill-at                          Remove @nn from exported symbols\n"));
212   fprintf (file, _("  --out-implib <file>                Generate import library\n"));
213   fprintf (file, _("  --output-def <file>                Generate a .DEF file for the built DLL\n"));
214 #endif
215 }
216
217 static void
218 set_pe_name (name, val)
219      char *name;
220      long val;
221 {
222   int i;
223   /* Find the name and set it. */
224   for (i = 0; init[i].ptr; i++)
225     {
226       if (strcmp (name, init[i].symbol) == 0)
227         {
228           init[i].value = val;
229           init[i].inited = 1;
230           return;
231         }
232     }
233   abort();
234 }
235
236
237 static void
238 set_pe_subsystem ()
239 {
240   const char *sver;
241   int len;
242   int i;
243   static const struct 
244     {
245       const char *name;
246       const int value;
247       const char *entry;
248     }
249   v[] =
250     {
251       { "native", 1, "_NtProcessStartup" },
252       { "windows", 2, "_WinMainCRTStartup" },
253       { "console", 3, "_mainCRTStartup" },
254 #if 0
255       /* The Microsoft linker does not recognize this.  */
256       { "os2", 5, "" },
257 #endif
258       { "posix", 7, "___PosixProcessStartup"},
259       { 0, 0, 0 }
260     };
261
262   sver = strchr (optarg, ':');
263   if (sver == NULL)
264     len = strlen (optarg);
265   else
266     {
267       char *end;
268
269       len = sver - optarg;
270       set_pe_name ("__major_subsystem_version__",
271                    strtoul (sver + 1, &end, 0));
272       if (*end == '.')
273         set_pe_name ("__minor_subsystem_version__",
274                      strtoul (end + 1, &end, 0));
275       if (*end != '\0')
276         einfo (_("%P: warning: bad version number in -subsystem option\n"));
277     }
278
279   for (i = 0; v[i].name; i++)
280     {
281       if (strncmp (optarg, v[i].name, len) == 0
282           && v[i].name[len] == '\0')
283         {
284           set_pe_name ("__subsystem__", v[i].value);
285
286           lang_add_entry (v[i].entry, 1);
287
288           return;
289         }
290     }
291   
292   einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
293 }
294
295
296
297 static void
298 set_pe_value (name)
299      char *name;
300      
301 {
302   char *end;
303   
304   set_pe_name (name,  strtoul (optarg, &end, 0));
305   
306   if (end == optarg)
307     einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
308
309   optarg = end;
310 }
311
312 static void
313 set_pe_stack_heap (resname, comname)
314      char *resname;
315      char *comname;
316 {
317   set_pe_value (resname);
318   
319   if (*optarg == ',')
320     {
321       optarg++;
322       set_pe_value (comname);
323     }
324   else if (*optarg)
325     einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
326 }
327
328
329
330 static int
331 gld_${EMULATION_NAME}_parse_args(argc, argv)
332      int argc;
333      char **argv;
334 {
335   int longind;
336   int optc;
337   int prevoptind = optind;
338   int prevopterr = opterr;
339   int wanterror;
340   static int lastoptind = -1;
341
342   if (lastoptind != optind)
343     opterr = 0;
344   wanterror = opterr;
345
346   lastoptind = optind;
347
348   optc = getopt_long_only (argc, argv, "-", longopts, &longind);
349   opterr = prevopterr;
350
351   switch (optc)
352     {
353     default:
354       if (wanterror)
355         xexit (1);
356       optind =  prevoptind;
357       return 0;
358
359     case OPTION_BASE_FILE:
360       link_info.base_file = (PTR) fopen (optarg, FOPEN_WB);
361       if (link_info.base_file == NULL)
362         {
363           /* xgettext:c-format */
364           fprintf (stderr, _("%s: Can't open base file %s\n"),
365                    program_name, optarg);
366           xexit (1);
367         }
368       break;
369
370       /* PE options */
371     case OPTION_HEAP: 
372       set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
373       break;
374     case OPTION_STACK: 
375       set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
376       break;
377     case OPTION_SUBSYSTEM:
378       set_pe_subsystem ();
379       break;
380     case OPTION_MAJOR_OS_VERSION:
381       set_pe_value ("__major_os_version__");
382       break;
383     case OPTION_MINOR_OS_VERSION:
384       set_pe_value ("__minor_os_version__");
385       break;
386     case OPTION_MAJOR_SUBSYSTEM_VERSION:
387       set_pe_value ("__major_subsystem_version__");
388       break;
389     case OPTION_MINOR_SUBSYSTEM_VERSION:
390       set_pe_value ("__minor_subsystem_version__");
391       break;
392     case OPTION_MAJOR_IMAGE_VERSION:
393       set_pe_value ("__major_image_version__");
394       break;
395     case OPTION_MINOR_IMAGE_VERSION:
396       set_pe_value ("__minor_image_version__");
397       break;
398     case OPTION_FILE_ALIGNMENT:
399       set_pe_value ("__file_alignment__");
400       break;
401     case OPTION_SECTION_ALIGNMENT:
402       set_pe_value ("__section_alignment__");
403       break;
404     case OPTION_DLL:
405       set_pe_name ("__dll__", 1);
406       break;
407     case OPTION_IMAGE_BASE:
408       set_pe_value ("__image_base__");
409       break;
410     case OPTION_SUPPORT_OLD_CODE:
411       support_old_code = 1;
412       break;
413     case OPTION_OUT_DEF:
414       pe_out_def_filename = xstrdup (optarg);
415       break;
416     case OPTION_EXPORT_ALL:
417       pe_dll_export_everything = 1;
418       break;
419     case OPTION_EXCLUDE_SYMBOLS:
420 #ifdef TARGET_IS_i386pe
421       pe_dll_add_excludes (optarg);
422 #endif
423       break;
424     case OPTION_KILL_ATS:
425       pe_dll_kill_ats = 1;
426       break;
427     case OPTION_STDCALL_ALIASES:
428       pe_dll_stdcall_aliases = 1;
429       break;
430     case OPTION_ENABLE_STDCALL_FIXUP:
431       pe_enable_stdcall_fixup = 1;
432       break;
433     case OPTION_DISABLE_STDCALL_FIXUP:
434       pe_enable_stdcall_fixup = 0;
435       break;
436     case OPTION_IMPLIB_FILENAME:
437       pe_implib_filename = xstrdup (optarg);
438       break;
439     }
440   return 1;
441 }
442 \f
443 /* Assign values to the special symbols before the linker script is
444    read.  */
445
446 static void
447 gld_${EMULATION_NAME}_set_symbols ()
448 {
449   /* Run through and invent symbols for all the
450      names and insert the defaults. */
451   int j;
452   lang_statement_list_type *save;
453
454   if (!init[IMAGEBASEOFF].inited)
455     {
456       if (link_info.relocateable)
457         init[IMAGEBASEOFF].value = 0;
458       else if (init[DLLOFF].value || link_info.shared)
459         init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
460       else
461         init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
462     }
463
464   /* Don't do any symbol assignments if this is a relocateable link.  */
465   if (link_info.relocateable)
466     return;
467
468   /* Glue the assignments into the abs section */
469   save = stat_ptr;
470
471   stat_ptr = &(abs_output_section->children);
472
473   for (j = 0; init[j].ptr; j++)
474     {
475       long val = init[j].value;
476       lang_assignment_statement_type *rv;
477       rv = lang_add_assignment (exp_assop ('=' ,init[j].symbol, exp_intop (val)));
478       if (init[j].size == sizeof(short))
479         *(short *)init[j].ptr = val;
480       else if (init[j].size == sizeof(int))
481         *(int *)init[j].ptr = val;
482       else if (init[j].size == sizeof(long))
483         *(long *)init[j].ptr = val;
484       /* This might be a long long or other special type.  */
485       else if (init[j].size == sizeof(bfd_vma))
486         *(bfd_vma *)init[j].ptr = val;
487       else      abort();
488       if (j == IMAGEBASEOFF)
489         image_base_statement = rv;
490     }
491   /* Restore the pointer. */
492   stat_ptr = save;
493   
494   if (pe.FileAlignment >
495       pe.SectionAlignment)
496     {
497       einfo (_("%P: warning, file alignment > section alignment.\n"));
498     }
499 }
500
501 /* This is called after the linker script and the command line options
502    have been read.  */
503
504 static void
505 gld_${EMULATION_NAME}_after_parse ()
506 {
507   /* The Windows libraries are designed for the linker to treat the
508      entry point as an undefined symbol.  Otherwise, the .obj that
509      defines mainCRTStartup is brought in because it is the first
510      encountered in libc.lib and it has other symbols in it which will
511      be pulled in by the link process.  To avoid this, we act as
512      though the user specified -u with the entry point symbol.
513
514      This function is called after the linker script and command line
515      options have been read, so at this point we know the right entry
516      point.  This function is called before the input files are
517      opened, so registering the symbol as undefined will make a
518      difference.  */
519
520   if (entry_symbol)
521     ldlang_add_undef (entry_symbol);
522 }
523
524 static struct bfd_link_hash_entry *pe_undef_found_sym;
525
526 static boolean
527 pe_undef_cdecl_match (h, string)
528   struct bfd_link_hash_entry *h;
529   PTR string;
530 {
531   int sl = strlen (string);
532   if (h->type == bfd_link_hash_defined
533       && strncmp (h->root.string, string, sl) == 0
534       && h->root.string[sl] == '@')
535   {
536     pe_undef_found_sym = h;
537     return false;
538   }
539   return true;
540 }
541
542 static void
543 pe_fixup_stdcalls ()
544 {
545   static int gave_warning_message = 0;
546   struct bfd_link_hash_entry *undef, *sym;
547   char *at;
548   for (undef = link_info.hash->undefs; undef; undef=undef->next)
549     if (undef->type == bfd_link_hash_undefined)
550     {
551       at = strchr (undef->root.string, '@');
552       if (at)
553       {
554         /* The symbol is a stdcall symbol, so let's look for a cdecl
555            symbol with the same name and resolve to that */
556         char *cname = xstrdup (undef->root.string);
557         at = strchr (cname, '@');
558         *at = 0;
559         sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
560         if (sym && sym->type == bfd_link_hash_defined)
561         {
562           undef->type = bfd_link_hash_defined;
563           undef->u.def.value = sym->u.def.value;
564           undef->u.def.section = sym->u.def.section;
565           if (pe_enable_stdcall_fixup == -1)
566             {
567               einfo (_("Warning: resolving %s by linking to %s\n"),
568                      undef->root.string, cname);
569               if (! gave_warning_message)
570                 {
571                   gave_warning_message = 1;
572                   einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
573                   einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
574                 }
575             }
576         }
577       }
578       else
579       {
580         /* The symbol is a cdecl symbol, so we look for stdcall
581            symbols - which means scanning the whole symbol table */
582         pe_undef_found_sym = 0;
583         bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
584                                 (PTR) undef->root.string);
585         sym = pe_undef_found_sym;
586         if (sym)
587         {
588           undef->type = bfd_link_hash_defined;
589           undef->u.def.value = sym->u.def.value;
590           undef->u.def.section = sym->u.def.section;
591           if (pe_enable_stdcall_fixup == -1)
592             {
593               einfo (_("Warning: resolving %s by linking to %s\n"),
594                      undef->root.string, sym->root.string);
595               if (! gave_warning_message)
596                 {
597                   gave_warning_message = 1;
598                   einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
599                   einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
600                 }
601             }
602         }
603       }
604     }
605 }
606
607 static void
608 gld_${EMULATION_NAME}_after_open ()
609 {
610   /* Pass the wacky PE command line options into the output bfd.
611      FIXME: This should be done via a function, rather than by
612      including an internal BFD header.  */
613   
614   if (!coff_data (output_bfd)->pe)
615     einfo (_("%F%P: PE operations on non PE file.\n"));
616
617   pe_data (output_bfd)->pe_opthdr = pe;
618   pe_data (output_bfd)->dll = init[DLLOFF].value;
619
620 #ifdef TARGET_IS_i386pe
621   if (pe_enable_stdcall_fixup) /* -1=warn or 1=disable */
622     pe_fixup_stdcalls ();
623
624   pe_process_import_defs(output_bfd, &link_info);
625   if (link_info.shared)
626     pe_dll_build_sections (output_bfd, &link_info);
627 #endif
628
629 #ifdef TARGET_IS_armpe
630   {
631     /* Find a BFD that can hold the interworking stubs.  */
632     LANG_FOR_EACH_INPUT_STATEMENT (is)
633       {
634         if (bfd_arm_get_bfd_for_interworking (is->the_bfd, & link_info))
635           break;
636       }
637   }
638 #endif
639 }
640 \f
641 static void  
642 gld_${EMULATION_NAME}_before_allocation()
643 {
644 #ifdef TARGET_IS_ppcpe
645   /* Here we rummage through the found bfds to collect toc information */
646   {
647     LANG_FOR_EACH_INPUT_STATEMENT (is)
648       {
649         if (!ppc_process_before_allocation (is->the_bfd, &link_info))
650           {
651             /* xgettext:c-format */
652             einfo (_("Errors encountered processing file %s\n"), is->filename);
653           }
654       }
655   }
656
657   /* We have seen it all. Allocate it, and carry on */
658   ppc_allocate_toc_section (&link_info);
659 #endif /* TARGET_IS_ppcpe */
660
661 #ifdef TARGET_IS_armpe
662   /* FIXME: we should be able to set the size of the interworking stub
663      section.
664
665      Here we rummage through the found bfds to collect glue
666      information.  FIXME: should this be based on a command line
667      option?  [email protected] */
668   {
669     LANG_FOR_EACH_INPUT_STATEMENT (is)
670       {
671         if (! bfd_arm_process_before_allocation
672             (is->the_bfd, & link_info, support_old_code))
673           {
674             /* xgettext:c-format */
675             einfo (_("Errors encountered processing file %s for interworking"),
676                    is->filename);
677           }
678       }
679   }
680
681   /* We have seen it all. Allocate it, and carry on */
682   bfd_arm_allocate_interworking_sections (& link_info);
683 #endif /* TARGET_IS_armpe */
684 }
685 \f
686
687 /* This is called when an input file isn't recognized as a BFD.  We
688    check here for .DEF files and pull them in automatically. */
689
690 static int
691 saw_option(char *option)
692 {
693   int i;
694   for (i=0; init[i].ptr; i++)
695     if (strcmp (init[i].symbol, option) == 0)
696       return init[i].inited;
697   return 0;
698 }
699
700 static boolean
701 gld_${EMULATION_NAME}_unrecognized_file(entry)
702   lang_input_statement_type *entry;
703 {
704 #ifdef TARGET_IS_i386pe
705   const char *ext = entry->filename + strlen (entry->filename) - 4;
706
707   if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
708   {
709     if (pe_def_file == 0)
710       pe_def_file = def_file_empty ();
711     def_file_parse (entry->filename, pe_def_file);
712     if (pe_def_file)
713     {
714       int i, buflen=0, len;
715       char *buf;
716       for (i=0; i<pe_def_file->num_exports; i++)
717         {
718           len = strlen(pe_def_file->exports[i].internal_name);
719           if (buflen < len+2)
720             buflen = len+2;
721         }
722       buf = (char *) xmalloc (buflen);
723       for (i=0; i<pe_def_file->num_exports; i++)
724         {
725           struct bfd_link_hash_entry *h;
726           sprintf(buf, "_%s", pe_def_file->exports[i].internal_name);
727
728           h = bfd_link_hash_lookup (link_info.hash, buf, true, true, true);
729           if (h == (struct bfd_link_hash_entry *) NULL)
730             einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
731           if (h->type == bfd_link_hash_new)
732             {
733               h->type = bfd_link_hash_undefined;
734               h->u.undef.abfd = NULL;
735               bfd_link_add_undef (link_info.hash, h);
736             }
737         }
738       free (buf);
739
740       /* def_file_print (stdout, pe_def_file); */
741       if (pe_def_file->is_dll == 1)
742         link_info.shared = 1;
743
744       if (pe_def_file->base_address != (bfd_vma)(-1))
745       {
746         pe.ImageBase =
747         pe_data (output_bfd)->pe_opthdr.ImageBase =
748         init[IMAGEBASEOFF].value = pe_def_file->base_address;
749         init[IMAGEBASEOFF].inited = 1;
750         if (image_base_statement)
751           image_base_statement->exp =
752             exp_assop ('=', "__image_base__", exp_intop (pe.ImageBase));
753       }
754
755 #if 0
756       /* Not sure if these *should* be set */
757       if (pe_def_file->version_major != -1)
758       {
759         pe.MajorImageVersion = pe_def_file->version_major;
760         pe.MinorImageVersion = pe_def_file->version_minor;
761       }
762 #endif
763       if (pe_def_file->stack_reserve != -1
764           && ! saw_option ("__size_of_stack_reserve__"))
765       {
766         pe.SizeOfStackReserve = pe_def_file->stack_reserve;
767         if (pe_def_file->stack_commit != -1)
768           pe.SizeOfStackCommit = pe_def_file->stack_commit;
769       }
770       if (pe_def_file->heap_reserve != -1
771           && ! saw_option ("__size_of_heap_reserve__"))
772       {
773         pe.SizeOfHeapReserve = pe_def_file->heap_reserve;
774         if (pe_def_file->heap_commit != -1)
775           pe.SizeOfHeapCommit = pe_def_file->heap_commit;
776       }
777       return true;
778     }
779   }
780 #endif
781   return false;
782   
783 }
784
785 static boolean
786 gld_${EMULATION_NAME}_recognized_file(entry)
787   lang_input_statement_type *entry;
788 {
789 #ifdef TARGET_IS_i386pe
790   if (bfd_get_format (entry->the_bfd) == bfd_object)
791     {
792       const char *ext = entry->filename + strlen (entry->filename) - 4;
793       if (strcmp (ext, ".dll") == 0 || strcmp (ext, ".DLL") == 0)
794         return pe_implied_import_dll (entry->filename);
795     }
796 #endif
797   return false;
798 }
799
800 static void
801 gld_${EMULATION_NAME}_finish ()
802 {
803 #ifdef TARGET_IS_i386pe
804   if (link_info.shared)
805     {
806       pe_dll_fill_sections (output_bfd, &link_info);
807       if (pe_implib_filename)
808         pe_dll_generate_implib (pe_def_file, pe_implib_filename);
809     }
810   if (pe_out_def_filename)
811     pe_dll_generate_def_file (pe_out_def_filename);
812 #endif
813 }
814
815 \f
816 /* Place an orphan section.
817
818    We use this to put sections in a reasonable place in the file, and
819    to ensure that they are aligned as required.
820
821    We handle grouped sections here as well.  A section named .foo$nn
822    goes into the output section .foo.  All grouped sections are sorted
823    by name.
824
825    Grouped sections for the default sections are handled by the
826    default linker script using wildcards, and are sorted by
827    sort_sections.  */
828
829 static asection *hold_section;
830 static char *hold_section_name;
831 static lang_output_section_statement_type *hold_use;
832 static lang_output_section_statement_type *hold_text;
833 static lang_output_section_statement_type *hold_rdata;
834 static lang_output_section_statement_type *hold_data;
835 static lang_output_section_statement_type *hold_bss;
836
837 /* Place an orphan section.  We use this to put random SHF_ALLOC
838    sections in the right segment.  */
839
840 /*ARGSUSED*/
841 static boolean
842 gld_${EMULATION_NAME}_place_orphan (file, s)
843      lang_input_statement_type *file;
844      asection *s;
845 {
846   const char *secname;
847   char *dollar;
848
849   if ((s->flags & SEC_ALLOC) == 0)
850     return false;
851
852   secname = bfd_get_section_name (s->owner, s);
853
854   /* Look through the script to see where to place this section.  */
855
856   hold_section = s;
857
858   hold_section_name = xstrdup (secname);
859   dollar = strchr (hold_section_name, '$');
860   if (dollar != NULL)
861     *dollar = '\0';
862
863   hold_use = NULL;
864   lang_for_each_statement (gld${EMULATION_NAME}_place_section);
865
866   if (hold_use == NULL)
867     {
868       lang_output_section_statement_type *place;
869       char *outsecname;
870       asection *snew, **pps;
871       lang_statement_list_type *old;
872       lang_statement_list_type add;
873       etree_type *address;
874
875       /* Try to put the new output section in a reasonable place based
876          on the section name and section flags.  */
877       place = NULL;
878       if ((s->flags & SEC_HAS_CONTENTS) == 0
879           && hold_bss != NULL)
880         place = hold_bss;
881       else if ((s->flags & SEC_READONLY) == 0
882                && hold_data != NULL)
883         place = hold_data;
884       else if ((s->flags & SEC_CODE) == 0
885                && (s->flags & SEC_READONLY) != 0
886                && hold_rdata != NULL)
887         place = hold_rdata;
888       else if ((s->flags & SEC_READONLY) != 0
889                && hold_text != NULL)
890         place = hold_text;
891
892       /* Choose a unique name for the section.  This will be needed if
893          the same section name appears in the input file with
894          different loadable or allocateable characteristics.  */
895       outsecname = xstrdup (hold_section_name);
896       if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
897         {
898           unsigned int len;
899           char *newname;
900           unsigned int i;
901
902           len = strlen (outsecname);
903           newname = xmalloc (len + 5);
904           strcpy (newname, outsecname);
905           i = 0;
906           do
907             {
908               sprintf (newname + len, "%d", i);
909               ++i;
910             }
911           while (bfd_get_section_by_name (output_bfd, newname) != NULL);
912
913           free (outsecname);
914           outsecname = newname;
915         }
916
917       /* We don't want to free OUTSECNAME, as it may get attached to
918          the output section statement.  */
919
920       /* Create the section in the output file, and put it in the
921          right place.  This shuffling is to make the output file look
922          neater.  */
923       snew = bfd_make_section (output_bfd, outsecname);
924       if (snew == NULL)
925         einfo ("%P%F: output format %s cannot represent section called %s\n",
926                output_bfd->xvec->name, outsecname);
927       if (place != NULL && place->bfd_section != NULL)
928         {
929           for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
930             ;
931           *pps = snew->next;
932           snew->next = place->bfd_section->next;
933           place->bfd_section->next = snew;
934         }
935
936       /* Start building a list of statements for this section.  */
937       old = stat_ptr;
938       stat_ptr = &add;
939       lang_list_init (stat_ptr);
940
941       if (link_info.relocateable)
942         address = NULL;
943       else
944         {
945           /* All sections in an executable must be aligned to a page
946              boundary.  */
947           address = exp_unop (ALIGN_K,
948                               exp_nameop (NAME, "__section_alignment__"));
949         }
950
951       lang_enter_output_section_statement (outsecname, address, 0,
952                                            (bfd_vma) 0,
953                                            (etree_type *) NULL,
954                                            (etree_type *) NULL,
955                                            (etree_type *) NULL);
956
957       hold_use = lang_output_section_statement_lookup (outsecname);
958
959       lang_leave_output_section_statement
960         ((bfd_vma) 0, "*default*",
961          (struct lang_output_section_phdr_list *) NULL);
962
963       /* Now stick the new statement list right after PLACE.  */
964       if (place != NULL)
965         {
966           *add.tail = place->header.next;
967           place->header.next = add.head;
968         }
969
970       stat_ptr = old;
971     }
972
973   if (dollar == NULL)
974     wild_doit (&hold_use->children, s, hold_use, file);
975   else
976     {
977       lang_statement_union_type **pl;
978       boolean found_dollar;
979       lang_statement_list_type list;
980
981       /* The section name has a '$'.  Sort it with the other '$'
982          sections.  */
983
984       found_dollar = false;
985       for (pl = &hold_use->children.head; *pl != NULL; pl = &(*pl)->next)
986         {
987           lang_input_section_type *ls;
988           const char *lname;
989
990           if ((*pl)->header.type != lang_input_section_enum)
991             continue;
992
993           ls = &(*pl)->input_section;
994
995           lname = bfd_get_section_name (ls->ifile->the_bfd, ls->section);
996           if (strchr (lname, '$') == NULL)
997             {
998               if (found_dollar)
999                 break;
1000             }
1001           else
1002             {
1003               found_dollar = true;
1004               if (strcmp (secname, lname) < 0)
1005                 break;
1006             }
1007         }
1008
1009       lang_list_init (&list);
1010       wild_doit (&list, s, hold_use, file);
1011       if (list.head != NULL)
1012         {
1013           ASSERT (list.head->next == NULL);
1014           list.head->next = *pl;
1015           *pl = list.head;
1016         }
1017     }
1018
1019   free (hold_section_name);
1020
1021   return true;
1022 }
1023
1024 static void
1025 gld${EMULATION_NAME}_place_section (s)
1026      lang_statement_union_type *s;
1027 {
1028   lang_output_section_statement_type *os;
1029
1030   if (s->header.type != lang_output_section_statement_enum)
1031     return;
1032
1033   os = &s->output_section_statement;
1034
1035   if (strcmp (os->name, hold_section_name) == 0
1036       && os->bfd_section != NULL
1037       && ((hold_section->flags & (SEC_LOAD | SEC_ALLOC))
1038           == (os->bfd_section->flags & (SEC_LOAD | SEC_ALLOC))))
1039     hold_use = os;
1040
1041   if (strcmp (os->name, ".text") == 0)
1042     hold_text = os;
1043   else if (strcmp (os->name, ".rdata") == 0)
1044     hold_rdata = os;
1045   else if (strcmp (os->name, ".data") == 0)
1046     hold_data = os;
1047   else if (strcmp (os->name, ".bss") == 0)
1048     hold_bss = os;
1049 }
1050 \f
1051 static char *
1052 gld_${EMULATION_NAME}_get_script(isfile)
1053      int *isfile;
1054 EOF
1055 # Scripts compiled in.
1056 # sed commands to quote an ld script as a C string.
1057 sc="-f ${srcdir}/emultempl/stringify.sed"
1058
1059 cat >>e${EMULATION_NAME}.c <<EOF
1060 {                            
1061   *isfile = 0;
1062
1063   if (link_info.relocateable == true && config.build_constructors == true)
1064     return
1065 EOF
1066 sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
1067 echo '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1068 sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
1069 echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
1070 sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
1071 echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
1072 sed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
1073 echo '  ; else return'                                     >> e${EMULATION_NAME}.c
1074 sed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
1075 echo '; }'                                                 >> e${EMULATION_NAME}.c
1076
1077 cat >>e${EMULATION_NAME}.c <<EOF
1078
1079
1080 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 
1081 {
1082   gld_${EMULATION_NAME}_before_parse,
1083   syslib_default,
1084   hll_default,
1085   gld_${EMULATION_NAME}_after_parse,
1086   gld_${EMULATION_NAME}_after_open,
1087   after_allocation_default,
1088   set_output_arch_default,
1089   ldemul_default_target,
1090   gld_${EMULATION_NAME}_before_allocation,
1091   gld_${EMULATION_NAME}_get_script,
1092   "${EMULATION_NAME}",
1093   "${OUTPUT_FORMAT}",
1094   gld_${EMULATION_NAME}_finish, /* finish */
1095   NULL, /* create output section statements */
1096   NULL, /* open dynamic archive */
1097   gld_${EMULATION_NAME}_place_orphan,
1098   gld_${EMULATION_NAME}_set_symbols,
1099   gld_${EMULATION_NAME}_parse_args,
1100   gld_${EMULATION_NAME}_unrecognized_file,
1101   gld_${EMULATION_NAME}_list_options,
1102   gld_${EMULATION_NAME}_recognized_file
1103 };
1104 EOF
This page took 0.084748 seconds and 4 git commands to generate.