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