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