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