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