* ia64-ic.tbl (fr-readers): Add mem-writers-fp.
[binutils.git] / ld / emultempl / beos.em
blob3b84280f97111d66dbe8aa31c2bda38ce5a1f24a
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 if [ -z "$MACHINE" ]; then
4   OUTPUT_ARCH=${ARCH}
5 else
6   OUTPUT_ARCH=${ARCH}:${MACHINE}
7 fi
8 cat >e${EMULATION_NAME}.c <<EOF
9 /* This file is part of GLD, the Gnu Linker.
10    Copyright 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003
11    Free Software Foundation, Inc.
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
27 /* For WINDOWS_NT */
28 /* The original file generated returned different default scripts depending
29    on whether certain switches were set, but these switches pertain to the
30    Linux system and that particular version of coff.  In the NT case, we
31    only determine if the subsystem is console or windows in order to select
32    the correct entry point by default. */
34 #include "bfd.h"
35 #include "sysdep.h"
36 #include "bfdlink.h"
37 #include "getopt.h"
38 #include "libiberty.h"
39 #include "ld.h"
40 #include "ldmain.h"
41 #include "ldexp.h"
42 #include "ldlang.h"
43 #include "ldfile.h"
44 #include "ldemul.h"
45 #include <ldgram.h>
46 #include "ldlex.h"
47 #include "ldmisc.h"
48 #include "ldctor.h"
49 #include "coff/internal.h"
50 #include "../bfd/libcoff.h"
52 #define TARGET_IS_${EMULATION_NAME}
54 static void gld_${EMULATION_NAME}_set_symbols PARAMS ((void));
55 static void gld_${EMULATION_NAME}_after_open PARAMS ((void));
56 static void gld_${EMULATION_NAME}_before_parse PARAMS ((void));
57 static void gld_${EMULATION_NAME}_before_allocation PARAMS ((void));
58 static bfd_boolean gld${EMULATION_NAME}_place_orphan
59   PARAMS ((lang_input_statement_type *, asection *));
60 static char *gld_${EMULATION_NAME}_get_script PARAMS ((int *));
62 static int sort_by_file_name PARAMS ((const PTR, const PTR));
63 static int sort_by_section_name PARAMS ((const PTR, const PTR));
64 static lang_statement_union_type **sort_sections_1
65   PARAMS ((lang_statement_union_type **, lang_statement_union_type *, int,
66            int (*) PARAMS((const PTR, const PTR))));
67 static void sort_sections PARAMS ((lang_statement_union_type *));
69 static void set_pe_name PARAMS ((char *, long int));
70 static void set_pe_subsystem PARAMS ((void));
71 static void set_pe_value PARAMS ((char *));
72 static void set_pe_stack_heap PARAMS ((char *, char *));
74 static struct internal_extra_pe_aouthdr pe;
75 static int dll;
77 extern const char *output_filename;
79 static void
80 gld_${EMULATION_NAME}_before_parse()
82   const bfd_arch_info_type *arch = bfd_scan_arch ("${OUTPUT_ARCH}");
83   if (arch)
84     {
85       ldfile_output_architecture = arch->arch;
86       ldfile_output_machine = arch->mach;
87       ldfile_output_machine_name = arch->printable_name;
88     }
89   else
90     ldfile_output_architecture = bfd_arch_${ARCH};
91   output_filename = "a.exe";
94 /* PE format extra command line options.  */
96 /* Used for setting flags in the PE header. */
97 #define OPTION_BASE_FILE                (300  + 1)
98 #define OPTION_DLL                      (OPTION_BASE_FILE + 1)
99 #define OPTION_FILE_ALIGNMENT           (OPTION_DLL + 1)
100 #define OPTION_IMAGE_BASE               (OPTION_FILE_ALIGNMENT + 1)
101 #define OPTION_MAJOR_IMAGE_VERSION      (OPTION_IMAGE_BASE + 1)
102 #define OPTION_MAJOR_OS_VERSION         (OPTION_MAJOR_IMAGE_VERSION + 1)
103 #define OPTION_MAJOR_SUBSYSTEM_VERSION  (OPTION_MAJOR_OS_VERSION + 1)
104 #define OPTION_MINOR_IMAGE_VERSION      (OPTION_MAJOR_SUBSYSTEM_VERSION + 1)
105 #define OPTION_MINOR_OS_VERSION         (OPTION_MINOR_IMAGE_VERSION + 1)
106 #define OPTION_MINOR_SUBSYSTEM_VERSION  (OPTION_MINOR_OS_VERSION + 1)
107 #define OPTION_SECTION_ALIGNMENT        (OPTION_MINOR_SUBSYSTEM_VERSION + 1)
108 #define OPTION_STACK                    (OPTION_SECTION_ALIGNMENT + 1)
109 #define OPTION_SUBSYSTEM                (OPTION_STACK + 1)
110 #define OPTION_HEAP                     (OPTION_SUBSYSTEM + 1)
112 static void gld${EMULATION_NAME}_add_options
113   PARAMS ((int, char **, int, struct option **, int, struct option **));
115 static void
116 gld${EMULATION_NAME}_add_options (ns, shortopts, nl, longopts, nrl, really_longopts)
117      int ns ATTRIBUTE_UNUSED;
118      char **shortopts ATTRIBUTE_UNUSED;
119      int nl;
120      struct option **longopts;
121      int nrl ATTRIBUTE_UNUSED;
122      struct option **really_longopts ATTRIBUTE_UNUSED;
124   static const struct option xtra_long[] = {
125     /* PE options */
126     {"base-file", required_argument, NULL, OPTION_BASE_FILE},
127     {"dll", no_argument, NULL, OPTION_DLL},
128     {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
129     {"heap", required_argument, NULL, OPTION_HEAP},
130     {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
131     {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
132     {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
133     {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
134     {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
135     {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
136     {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
137     {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
138     {"stack", required_argument, NULL, OPTION_STACK},
139     {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
140     {NULL, no_argument, NULL, 0}
141   };
143   *longopts = (struct option *)
144     xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
145   memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
149 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
150    parameters which may be input from the command line */
152 typedef struct {
153   void *ptr;
154   int size;
155   int value;
156   char *symbol;
157   int inited;
158 } definfo;
160 #define D(field,symbol,def)  {&pe.field,sizeof(pe.field), def, symbol,0}
162 static definfo init[] =
164   /* imagebase must be first */
165 #define IMAGEBASEOFF 0
166   D(ImageBase,"__image_base__", BEOS_EXE_IMAGE_BASE),
167 #define DLLOFF 1
168   {&dll, sizeof(dll), 0, "__dll__", 0},
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 }
187 static void
188 set_pe_name (name, val)
189      char *name;
190      long val;
192   int i;
193   /* Find the name and set it. */
194   for (i = 0; init[i].ptr; i++)
195     {
196       if (strcmp (name, init[i].symbol) == 0)
197         {
198           init[i].value = val;
199           init[i].inited = 1;
200           return;
201         }
202     }
203   abort();
207 static void
208 set_pe_subsystem ()
210   const char *sver;
211   int len;
212   int i;
213   static const struct
214     {
215       const char *name;
216       const int value;
217       const char *entry;
218     }
219   v[] =
220     {
221       { "native", 1, "_NtProcessStartup" },
222       { "windows", 2, "_WinMainCRTStartup" },
223       { "wwindows", 2, "_wWinMainCRTStartup" },
224       { "console", 3, "_mainCRTStartup" },
225       { "wconsole", 3, "_wmainCRTStartup" },
226 #if 0
227       /* The Microsoft linker does not recognize this.  */
228       { "os2", 5, "" },
229 #endif
230       { "posix", 7, "___PosixProcessStartup"},
231       { 0, 0, 0 }
232     };
234   sver = strchr (optarg, ':');
235   if (sver == NULL)
236     len = strlen (optarg);
237   else
238     {
239       char *end;
241       len = sver - optarg;
242       set_pe_name ("__major_subsystem_version__",
243                    strtoul (sver + 1, &end, 0));
244       if (*end == '.')
245         set_pe_name ("__minor_subsystem_version__",
246                      strtoul (end + 1, &end, 0));
247       if (*end != '\0')
248         einfo ("%P: warning: bad version number in -subsystem option\n");
249     }
251   for (i = 0; v[i].name; i++)
252     {
253       if (strncmp (optarg, v[i].name, len) == 0
254           && v[i].name[len] == '\0')
255         {
256           set_pe_name ("__subsystem__", v[i].value);
258           /* If the subsystem is windows, we use a different entry
259              point.  We also register the entry point as an undefined
260              symbol. from lang_add_entry() The reason we do
261              this is so that the user
262              doesn't have to because they would have to use the -u
263              switch if they were specifying an entry point other than
264              _mainCRTStartup.  Specifically, if creating a windows
265              application, entry point _WinMainCRTStartup must be
266              specified.  What I have found for non console
267              applications (entry not _mainCRTStartup) is that the .obj
268              that contains mainCRTStartup is brought in since it is
269              the first encountered in libc.lib and it has other
270              symbols in it which will be pulled in by the link
271              process.  To avoid this, adding -u with the entry point
272              name specified forces the correct .obj to be used.  We
273              can avoid making the user do this by always adding the
274              entry point name as an undefined symbol.  */
275           lang_add_entry (v[i].entry, 1);
277           return;
278         }
279     }
280   einfo ("%P%F: invalid subsystem type %s\n", optarg);
285 static void
286 set_pe_value (name)
287      char *name;
290   char *end;
291   set_pe_name (name,  strtoul (optarg, &end, 0));
292   if (end == optarg)
293     {
294       einfo ("%P%F: invalid hex number for PE parameter '%s'\n", optarg);
295     }
297   optarg = end;
300 static void
301 set_pe_stack_heap (resname, comname)
302      char *resname;
303      char *comname;
305   set_pe_value (resname);
306   if (*optarg == ',')
307     {
308       optarg++;
309       set_pe_value (comname);
310     }
311   else if (*optarg)
312     {
313       einfo ("%P%F: strange hex info for PE parameter '%s'\n", optarg);
314     }
318 static bfd_boolean gld${EMULATION_NAME}_handle_option
319   PARAMS ((int));
321 static bfd_boolean
322 gld${EMULATION_NAME}_handle_option (optc)
323      int optc;
325   switch (optc)
326     {
327     default:
328       return FALSE;
330     case OPTION_BASE_FILE:
331       link_info.base_file = (PTR) fopen (optarg, FOPEN_WB);
332       if (link_info.base_file == NULL)
333         {
334           fprintf (stderr, "%s: Can't open base file %s\n",
335                    program_name, optarg);
336           xexit (1);
337         }
338       break;
340       /* PE options */
341     case OPTION_HEAP:
342       set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
343       break;
344     case OPTION_STACK:
345       set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
346       break;
347     case OPTION_SUBSYSTEM:
348       set_pe_subsystem ();
349       break;
350     case OPTION_MAJOR_OS_VERSION:
351       set_pe_value ("__major_os_version__");
352       break;
353     case OPTION_MINOR_OS_VERSION:
354       set_pe_value ("__minor_os_version__");
355       break;
356     case OPTION_MAJOR_SUBSYSTEM_VERSION:
357       set_pe_value ("__major_subsystem_version__");
358       break;
359     case OPTION_MINOR_SUBSYSTEM_VERSION:
360       set_pe_value ("__minor_subsystem_version__");
361       break;
362     case OPTION_MAJOR_IMAGE_VERSION:
363       set_pe_value ("__major_image_version__");
364       break;
365     case OPTION_MINOR_IMAGE_VERSION:
366       set_pe_value ("__minor_image_version__");
367       break;
368     case OPTION_FILE_ALIGNMENT:
369       set_pe_value ("__file_alignment__");
370       break;
371     case OPTION_SECTION_ALIGNMENT:
372       set_pe_value ("__section_alignment__");
373       break;
374     case OPTION_DLL:
375       set_pe_name ("__dll__", 1);
376       break;
377     case OPTION_IMAGE_BASE:
378       set_pe_value ("__image_base__");
379       break;
380     }
381   return TRUE;
384 /* Assign values to the special symbols before the linker script is
385    read.  */
387 static void
388 gld_${EMULATION_NAME}_set_symbols()
390   /* Run through and invent symbols for all the
391      names and insert the defaults. */
392   int j;
393   lang_statement_list_type *save;
395   if (!init[IMAGEBASEOFF].inited)
396     {
397       if (link_info.relocateable)
398         init[IMAGEBASEOFF].value = 0;
399       else if (init[DLLOFF].value)
400         init[IMAGEBASEOFF].value = BEOS_DLL_IMAGE_BASE;
401       else
402         init[IMAGEBASEOFF].value = BEOS_EXE_IMAGE_BASE;
403     }
405   /* Don't do any symbol assignments if this is a relocateable link.  */
406   if (link_info.relocateable)
407     return;
409   /* Glue the assignments into the abs section */
410   save = stat_ptr;
412   stat_ptr = &(abs_output_section->children);
414   for (j = 0; init[j].ptr; j++)
415     {
416       long val = init[j].value;
417       lang_add_assignment (exp_assop ('=', init[j].symbol, exp_intop (val)));
418       if (init[j].size == sizeof(short))
419         *(short *)init[j].ptr = val;
420       else if (init[j].size == sizeof(int))
421         *(int *)init[j].ptr = val;
422       else if (init[j].size == sizeof(long))
423         *(long *)init[j].ptr = val;
424       /* This might be a long long or other special type.  */
425       else if (init[j].size == sizeof(bfd_vma))
426         *(bfd_vma *)init[j].ptr = val;
427       else      abort();
428     }
429   /* Restore the pointer. */
430   stat_ptr = save;
432   if (pe.FileAlignment >
433       pe.SectionAlignment)
434     {
435       einfo ("%P: warning, file alignment > section alignment.\n");
436     }
439 static void
440 gld_${EMULATION_NAME}_after_open()
442   /* Pass the wacky PE command line options into the output bfd.
443      FIXME: This should be done via a function, rather than by
444      including an internal BFD header.  */
445   if (!coff_data(output_bfd)->pe)
446     {
447       einfo ("%F%P: PE operations on non PE file.\n");
448     }
450   pe_data(output_bfd)->pe_opthdr = pe;
451   pe_data(output_bfd)->dll = init[DLLOFF].value;
455 /* Callback functions for qsort in sort_sections. */
457 static int
458 sort_by_file_name (a, b)
459      const PTR a;
460      const PTR b;
462   const lang_statement_union_type *const *ra = a;
463   const lang_statement_union_type *const *rb = b;
464   int i, a_sec, b_sec;
466   i = strcmp ((*ra)->input_section.ifile->the_bfd->my_archive->filename,
467               (*rb)->input_section.ifile->the_bfd->my_archive->filename);
468   if (i != 0)
469     return i;
471   i = strcmp ((*ra)->input_section.ifile->filename,
472                  (*rb)->input_section.ifile->filename);
473   if (i != 0)
474     return i;
475   /* the tail idata4/5 are the only ones without relocs to an
476      idata$6 section unless we are importing by ordinal,
477      so sort them to last to terminate the IAT
478      and HNT properly. if no reloc this one is import by ordinal
479      so we have to sort by section contents */
481   if ( ((*ra)->input_section.section->reloc_count + (*rb)->input_section.section->reloc_count) )
482     {
483        i =  (((*ra)->input_section.section->reloc_count >
484                  (*rb)->input_section.section->reloc_count) ? -1 : 0);
485        if ( i != 0)
486          return i;
488         return  (((*ra)->input_section.section->reloc_count >
489                  (*rb)->input_section.section->reloc_count) ? 0 : 1);
490     }
491   else
492     {
493        if ( (strcmp( (*ra)->input_section.section->name, ".idata$6") == 0) )
494           return 0; /* don't sort .idata$6 or .idata$7 FIXME dlltool eliminate .idata$7 */
496        if (! bfd_get_section_contents ((*ra)->input_section.ifile->the_bfd,
497          (*ra)->input_section.section, &a_sec, (file_ptr) 0, (bfd_size_type)sizeof(a_sec)))
498             einfo ("%F%B: Can't read contents of section .idata: %E\n",
499                  (*ra)->input_section.ifile->the_bfd);
501        if (! bfd_get_section_contents ((*rb)->input_section.ifile->the_bfd,
502         (*rb)->input_section.section, &b_sec, (file_ptr) 0, (bfd_size_type)sizeof(b_sec) ))
503            einfo ("%F%B: Can't read contents of section .idata: %E\n",
504                 (*rb)->input_section.ifile->the_bfd);
506       i =  ((a_sec < b_sec) ? -1 : 0);
507       if ( i != 0)
508         return i;
509       return  ((a_sec < b_sec) ? 0 : 1);
510    }
511 return 0;
514 static int
515 sort_by_section_name (a, b)
516      const PTR a;
517      const PTR b;
519   const lang_statement_union_type *const *ra = a;
520   const lang_statement_union_type *const *rb = b;
521   int i;
522   i = strcmp ((*ra)->input_section.section->name,
523                  (*rb)->input_section.section->name);
524 /* this is a hack to make .stab and .stabstr last, so we don't have
525    to fix strip/objcopy for .reloc sections.
526    FIXME stripping images with a .rsrc section still needs to be fixed */
527   if ( i != 0)
528     {
529       if ((strncmp ((*ra)->input_section.section->name, ".stab", 5) == 0)
530            && (strncmp ((*rb)->input_section.section->name, ".stab", 5) != 0))
531          return 1;
532       return i;
533     }
534   return i;
537 /* Subroutine of sort_sections to a contiguous subset of a list of sections.
538    NEXT_AFTER is the element after the last one to sort.
539    The result is a pointer to the last element's "next" pointer.  */
541 static lang_statement_union_type **
542 sort_sections_1 (startptr, next_after, count, sort_func)
543      lang_statement_union_type **startptr,*next_after;
544      int count;
545      int (*sort_func) PARAMS ((const PTR, const PTR));
547   lang_statement_union_type **vec;
548   lang_statement_union_type *p;
549   int i;
550   lang_statement_union_type **ret;
552   if (count == 0)
553     return startptr;
555   vec = ((lang_statement_union_type **)
556          xmalloc (count * sizeof (lang_statement_union_type *)));
558   for (p = *startptr, i = 0; i < count; i++, p = p->header.next)
559     vec[i] = p;
561   qsort (vec, count, sizeof (vec[0]), sort_func);
563   /* Fill in the next pointers again. */
564   *startptr = vec[0];
565   for (i = 0; i < count - 1; i++)
566     vec[i]->header.next = vec[i + 1];
567   vec[i]->header.next = next_after;
568   ret = &vec[i]->header.next;
569   free (vec);
570   return ret;
573 /* Sort the .idata\$foo input sections of archives into filename order.
574    The reason is so dlltool can arrange to have the pe dll import information
575    generated correctly - the head of the list goes into dh.o, the tail into
576    dt.o, and the guts into ds[nnnn].o.  Note that this is only needed for the
577    .idata section.
578    FIXME: This may no longer be necessary with grouped sections.  Instead of
579    sorting on dh.o, ds[nnnn].o, dt.o, one could, for example, have dh.o use
580    .idata\$4h, have ds[nnnn].o use .idata\$4s[nnnn], and have dt.o use .idata\$4t.
581    This would have to be elaborated upon to handle multiple dll's
582    [assuming such an eloboration is possible of course].
584    We also sort sections in '\$' wild statements.  These are created by the
585    place_orphans routine to implement grouped sections.  */
587 static void
588 sort_sections (s)
589      lang_statement_union_type *s;
591   for (; s ; s = s->header.next)
592     switch (s->header.type)
593       {
594       case lang_output_section_statement_enum:
595         sort_sections (s->output_section_statement.children.head);
596         break;
597       case lang_wild_statement_enum:
598         {
599           lang_statement_union_type **p = &s->wild_statement.children.head;
600           struct wildcard_list *sec;
602           for (sec = s->wild_statement.section_list; sec; sec = sec->next)
603             {
604               /* Is this the .idata section?  */
605               if (sec->spec.name != NULL
606                   && strncmp (sec->spec.name, ".idata", 6) == 0)
607                 {
608                   /* Sort the children.  We want to sort any objects in
609                      the same archive.  In order to handle the case of
610                      including a single archive multiple times, we sort
611                      all the children by archive name and then by object
612                      name.  After sorting them, we re-thread the pointer
613                      chain.  */
615                   while (*p)
616                     {
617                       lang_statement_union_type *start = *p;
618                       if (start->header.type != lang_input_section_enum
619                           || !start->input_section.ifile->the_bfd->my_archive)
620                         p = &(start->header.next);
621                       else
622                         {
623                           lang_statement_union_type *end;
624                           int count;
626                           for (end = start, count = 0;
627                                end && (end->header.type
628                                        == lang_input_section_enum);
629                                end = end->header.next)
630                             count++;
632                           p = sort_sections_1 (p, end, count,
633                                                sort_by_file_name);
634                         }
635                     }
636                   break;
637                 }
639               /* If this is a collection of grouped sections, sort them.
640                  The linker script must explicitly mention "*(.foo\$)" or
641                  "*(.foo\$*)".  Don't sort them if \$ is not the last
642                  character (not sure if this is really useful, but it
643                  allows explicitly mentioning some \$ sections and letting
644                  the linker handle the rest).  */
645               if (sec->spec.name != NULL)
646                 {
647                   char *q = strchr (sec->spec.name, '\$');
649                   if (q != NULL
650                       && (q[1] == '\0'
651                           || (q[1] == '*' && q[2] == '\0')))
652                     {
653                       lang_statement_union_type *end;
654                       int count;
656                       for (end = *p, count = 0; end; end = end->header.next)
657                         {
658                           if (end->header.type != lang_input_section_enum)
659                             abort ();
660                           count++;
661                         }
662                       (void) sort_sections_1 (p, end, count,
663                                               sort_by_section_name);
664                     }
665                   break;
666                 }
667             }
668         }
669         break;
670       default:
671         break;
672       }
675 static void
676 gld_${EMULATION_NAME}_before_allocation()
678   extern lang_statement_list_type *stat_ptr;
680 #ifdef TARGET_IS_ppcpe
681   /* Here we rummage through the found bfds to collect toc information */
682   {
683     LANG_FOR_EACH_INPUT_STATEMENT (is)
684       {
685         if (!ppc_process_before_allocation(is->the_bfd, &link_info))
686           {
687             einfo("Errors encountered processing file %s\n", is->filename);
688           }
689       }
690   }
692   /* We have seen it all. Allocate it, and carry on */
693   ppc_allocate_toc_section (&link_info);
694 #else
695 #ifdef TARGET_IS_armpe
696   /* FIXME: we should be able to set the size of the interworking stub
697      section.
699      Here we rummage through the found bfds to collect glue
700      information.  FIXME: should this be based on a command line
701      option?  krk@cygnus.com */
702   {
703     LANG_FOR_EACH_INPUT_STATEMENT (is)
704       {
705         if (!arm_process_before_allocation (is->the_bfd, & link_info))
706           {
707             einfo ("Errors encountered processing file %s", is->filename);
708           }
709       }
710   }
712   /* We have seen it all. Allocate it, and carry on */
713   arm_allocate_interworking_sections (& link_info);
714 #endif /* TARGET_IS_armpe */
715 #endif /* TARGET_IS_ppcpe */
717   sort_sections (stat_ptr->head);
720 /* Place an orphan section.  We use this to put sections with a '\$' in them
721    into the right place.  Any section with a '\$' in them (e.g. .text\$foo)
722    gets mapped to the output section with everything from the '\$' on stripped
723    (e.g. .text).
724    See the Microsoft Portable Executable and Common Object File Format
725    Specification 4.1, section 4.2, Grouped Sections.
727    FIXME: This is now handled by the linker script using wildcards,
728    but I'm leaving this here in case we want to enable it for sections
729    which are not mentioned in the linker script.  */
731 /*ARGSUSED*/
732 static bfd_boolean
733 gld${EMULATION_NAME}_place_orphan (file, s)
734      lang_input_statement_type *file;
735      asection *s;
737   const char *secname;
738   char *output_secname, *ps;
739   lang_output_section_statement_type *os;
740   lang_statement_union_type *l;
742   if ((s->flags & SEC_ALLOC) == 0)
743     return FALSE;
745   /* Don't process grouped sections unless doing a final link.
746      If they're marked as COMDAT sections, we don't want .text\$foo to
747      end up in .text and then have .text disappear because it's marked
748      link-once-discard.  */
749   if (link_info.relocateable)
750     return FALSE;
752   secname = bfd_get_section_name (s->owner, s);
754   /* Everything from the '\$' on gets deleted so don't allow '\$' as the
755      first character.  */
756   if (*secname == '\$')
757     einfo ("%P%F: section %s has '\$' as first character\n", secname);
758   if (strchr (secname + 1, '\$') == NULL)
759     return FALSE;
761   /* Look up the output section.  The Microsoft specs say sections names in
762      image files never contain a '\$'.  Fortunately, lang_..._lookup creates
763      the section if it doesn't exist.  */
764   output_secname = xstrdup (secname);
765   ps = strchr (output_secname + 1, '\$');
766   *ps = 0;
767   os = lang_output_section_statement_lookup (output_secname);
769   /* Find the '\$' wild statement for this section.  We currently require the
770      linker script to explicitly mention "*(.foo\$)".
771      FIXME: ppcpe.sc has .CRT\$foo in the .rdata section.  According to the
772      Microsoft docs this isn't correct so it's not (currently) handled.  */
774   ps[0] = '\$';
775   ps[1] = 0;
776   for (l = os->children.head; l; l = l->header.next)
777     if (l->header.type == lang_wild_statement_enum)
778       {
779         struct wildcard_list *sec;
781         for (sec = l->wild_statement.section_list; sec; sec = sec->next)
782           if (sec->spec.name && strcmp (sec->spec.name, output_secname) == 0)
783             break;
784         if (sec)
785           break;
786       }
787   ps[0] = 0;
788   if (l == NULL)
789 #if 1
790     einfo ("%P%F: *(%s\$) missing from linker script\n", output_secname);
791 #else /* FIXME: This block is untried.  It exists to convey the intent,
792          should one decide to not require *(.foo\$) to appear in the linker
793          script.  */
794     {
795       lang_wild_statement_type *new;
796       struct wildcard_list *tmp;
798       tmp = (struct wildcard_list *) xmalloc (sizeof *tmp);
799       tmp->next = NULL;
800       tmp->spec.name = xmalloc (strlen (output_secname) + 2);
801       sprintf (tmp->spec.name, "%s\$", output_secname);
802       tmp->spec.exclude_name_list = NULL;
803       tmp->sorted = FALSE;
804       new = new_stat (lang_wild_statement, &os->children);
805       new->filename = NULL;
806       new->filenames_sorted = FALSE;
807       new->section_list = tmp;
808       new->keep_sections = FALSE;
809       lang_list_init (&new->children);
810       l = new;
811     }
812 #endif
814   /* Link the input section in and we're done for now.
815      The sections still have to be sorted, but that has to wait until
816      all such sections have been processed by us.  The sorting is done by
817      sort_sections.  */
818   lang_add_section (&l->wild_statement.children, s, os, file);
820   return TRUE;
823 static char *
824 gld_${EMULATION_NAME}_get_script(isfile)
825      int *isfile;
827 # Scripts compiled in.
828 # sed commands to quote an ld script as a C string.
829 sc="-f stringify.sed"
831 cat >>e${EMULATION_NAME}.c <<EOF
833   *isfile = 0;
835   if (link_info.relocateable && config.build_constructors)
836     return
838 sed $sc ldscripts/${EMULATION_NAME}.xu                 >> e${EMULATION_NAME}.c
839 echo '  ; else if (link_info.relocateable) return'     >> e${EMULATION_NAME}.c
840 sed $sc ldscripts/${EMULATION_NAME}.xr                 >> e${EMULATION_NAME}.c
841 echo '  ; else if (!config.text_read_only) return'     >> e${EMULATION_NAME}.c
842 sed $sc ldscripts/${EMULATION_NAME}.xbn                >> e${EMULATION_NAME}.c
843 echo '  ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
844 sed $sc ldscripts/${EMULATION_NAME}.xn                 >> e${EMULATION_NAME}.c
845 echo '  ; else return'                                 >> e${EMULATION_NAME}.c
846 sed $sc ldscripts/${EMULATION_NAME}.x                  >> e${EMULATION_NAME}.c
847 echo '; }'                                             >> e${EMULATION_NAME}.c
849 cat >>e${EMULATION_NAME}.c <<EOF
852 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
854   gld_${EMULATION_NAME}_before_parse,
855   syslib_default,
856   hll_default,
857   after_parse_default,
858   gld_${EMULATION_NAME}_after_open,
859   after_allocation_default,
860   set_output_arch_default,
861   ldemul_default_target,
862   gld_${EMULATION_NAME}_before_allocation,
863   gld_${EMULATION_NAME}_get_script,
864   "${EMULATION_NAME}",
865   "${OUTPUT_FORMAT}",
866   NULL, /* finish */
867   NULL, /* create output section statements */
868   NULL, /* open dynamic archive */
869   gld${EMULATION_NAME}_place_orphan,
870   gld_${EMULATION_NAME}_set_symbols,
871   NULL, /* parse_args */
872   gld${EMULATION_NAME}_add_options,
873   gld${EMULATION_NAME}_handle_option,
874   NULL, /* unrecognized file */
875   NULL, /* list options */
876   NULL, /* recognized file */
877   NULL, /* find_potential_libraries */
878   NULL  /* new_vers_pattern */