1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 cat >e${EMULATION_NAME}.c <<EOF
5 /* The original file generated returned different default scripts depending
6 on whether certain switches were set, but these switches pertain to the
7 Linux system and that particular version of coff. In the NT case, we
8 only determine if the subsystem is console or windows in order to select
9 the correct entry point by default. */
12 /* This file is part of GLD, the Gnu Linker.
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 2 of the License, or
17 (at your option) any later version.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
43 #include "coff/internal.h"
44 #include "../bfd/libcoff.h"
46 #define TARGET_IS_${EMULATION_NAME}
48 static void gld_${EMULATION_NAME}_set_symbols PARAMS ((void));
49 static void gld_${EMULATION_NAME}_after_open PARAMS ((void));
50 static void gld_${EMULATION_NAME}_before_parse PARAMS ((void));
51 static void gld_${EMULATION_NAME}_before_allocation PARAMS ((void));
52 static boolean gld${EMULATION_NAME}_place_orphan
53 PARAMS ((lang_input_statement_type *, asection *));
54 static char *gld_${EMULATION_NAME}_get_script PARAMS ((int *));
55 static int gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **));
57 #if 0 /* argument to qsort so don't prototype */
58 static int sort_by_file_name PARAMS ((void *, void *));
59 static int sort_by_section_name PARAMS ((void *, void *));
61 static lang_statement_union_type **sort_sections_1
62 PARAMS ((lang_statement_union_type **, lang_statement_union_type *, int,
64 static void sort_sections PARAMS ((lang_statement_union_type *));
66 static struct internal_extra_pe_aouthdr pe;
70 gld_${EMULATION_NAME}_before_parse()
72 ldfile_output_architecture = bfd_arch_${ARCH};
75 /* PE format extra command line options. */
77 /* Used for setting flags in the PE header. */
78 #define OPTION_BASE_FILE (300 + 1)
79 #define OPTION_DLL (OPTION_BASE_FILE + 1)
80 #define OPTION_FILE_ALIGNMENT (OPTION_DLL + 1)
81 #define OPTION_IMAGE_BASE (OPTION_FILE_ALIGNMENT + 1)
82 #define OPTION_MAJOR_IMAGE_VERSION (OPTION_IMAGE_BASE + 1)
83 #define OPTION_MAJOR_OS_VERSION (OPTION_MAJOR_IMAGE_VERSION + 1)
84 #define OPTION_MAJOR_SUBSYSTEM_VERSION (OPTION_MAJOR_OS_VERSION + 1)
85 #define OPTION_MINOR_IMAGE_VERSION (OPTION_MAJOR_SUBSYSTEM_VERSION + 1)
86 #define OPTION_MINOR_OS_VERSION (OPTION_MINOR_IMAGE_VERSION + 1)
87 #define OPTION_MINOR_SUBSYSTEM_VERSION (OPTION_MINOR_OS_VERSION + 1)
88 #define OPTION_SECTION_ALIGNMENT (OPTION_MINOR_SUBSYSTEM_VERSION + 1)
89 #define OPTION_STACK (OPTION_SECTION_ALIGNMENT + 1)
90 #define OPTION_SUBSYSTEM (OPTION_STACK + 1)
91 #define OPTION_HEAP (OPTION_SUBSYSTEM + 1)
93 static struct option longopts[] = {
95 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
96 {"dll", no_argument, NULL, OPTION_DLL},
97 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
98 {"heap", required_argument, NULL, OPTION_HEAP},
99 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
100 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
101 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
102 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
103 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
104 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
105 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
106 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
107 {"stack", required_argument, NULL, OPTION_STACK},
108 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
109 {NULL, no_argument, NULL, 0}
113 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
114 parameters which may be input from the command line */
124 #define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol,0}
126 static definfo init[] =
128 /* imagebase must be first */
129 #define IMAGEBASEOFF 0
130 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
132 {&dll, sizeof(dll), 0, "__dll__"},
133 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
134 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
135 D(MajorOperatingSystemVersion,"__major_os_version__", 4),
136 D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
137 D(MajorImageVersion,"__major_image_version__", 1),
138 D(MinorImageVersion,"__minor_image_version__", 0),
139 D(MajorSubsystemVersion,"__major_subsystem_version__", 4),
140 D(MinorSubsystemVersion,"__minor_subsystem_version__", 0),
141 D(Subsystem,"__subsystem__", 3),
142 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x2000000),
143 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
144 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
145 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
146 D(LoaderFlags,"__loader_flags__", 0x0),
152 set_pe_name (name, val)
157 /* Find the name and set it. */
158 for (i = 0; init[i].ptr; i++)
160 if (strcmp (name, init[i].symbol) == 0)
190 for (i = 0; v[i].name; i++)
192 if (!strcmp (optarg, v[i].name))
194 set_pe_name ("__subsystem__", v[i].value);
196 /* If the subsystem is windows, we use a different entry
197 point. We also register the entry point as an undefined
198 symbol. The reason we do this is so that the user
199 doesn't have to because they would have to use the -u
200 switch if they were specifying an entry point other than
201 _mainCRTStartup. Specifically, if creating a windows
202 application, entry point _WinMainCRTStartup must be
203 specified. What I have found for non console
204 applications (entry not _mainCRTStartup) is that the .obj
205 that contains mainCRTStartup is brought in since it is
206 the first encountered in libc.lib and it has other
207 symbols in it which will be pulled in by the link
208 process. To avoid this, adding -u with the entry point
209 name specified forces the correct .obj to be used. We
210 can avoid making the user do this by always adding the
211 entry point name as an undefined symbol. */
214 lang_add_entry ("_WinMainCRTStartup", 1);
215 ldlang_add_undef ("_WinMainCRTStartup");
221 einfo ("%P%F: invalid subsystem type %s\n", optarg);
232 set_pe_name (name, strtoul (optarg, &end, 0));
235 einfo ("%P%F: invalid hex number for PE parameter '%s'\n", optarg);
242 set_pe_stack_heap (resname, comname)
249 set_pe_value (resname);
253 set_pe_value (comname);
257 einfo ("%P%F: strange hex info for PE parameter '%s'\n", optarg);
264 gld_${EMULATION_NAME}_parse_args(argc, argv)
270 int prevoptind = optind;
271 int prevopterr = opterr;
273 static int lastoptind = -1;
275 if (lastoptind != optind)
281 optc = getopt_long_only (argc, argv, "-", longopts, &longind);
292 case OPTION_BASE_FILE:
293 link_info.base_file = (PTR) fopen (optarg, FOPEN_WB);
294 if (link_info.base_file == NULL)
296 fprintf (stderr, "%s: Can't open base file %s\n",
297 program_name, optarg);
304 set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
307 set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
309 case OPTION_SUBSYSTEM:
312 case OPTION_MAJOR_OS_VERSION:
313 set_pe_value ("__major_os_version__");
315 case OPTION_MINOR_OS_VERSION:
316 set_pe_value ("__minor_os_version__");
318 case OPTION_MAJOR_SUBSYSTEM_VERSION:
319 set_pe_value ("__major_subsystem_version__");
321 case OPTION_MINOR_SUBSYSTEM_VERSION:
322 set_pe_value ("__minor_subsystem_version__");
324 case OPTION_MAJOR_IMAGE_VERSION:
325 set_pe_value ("__major_image_version__");
327 case OPTION_MINOR_IMAGE_VERSION:
328 set_pe_value ("__minor_image_version__");
330 case OPTION_FILE_ALIGNMENT:
331 set_pe_value ("__file_alignment__");
333 case OPTION_SECTION_ALIGNMENT:
334 set_pe_value ("__section_alignment__");
337 set_pe_name ("__dll__", 1);
339 case OPTION_IMAGE_BASE:
340 set_pe_value ("__image_base__");
347 gld_${EMULATION_NAME}_set_symbols()
349 /* Run through and invent symbols for all the
350 names and insert the defaults. */
352 lang_statement_list_type *save;
354 if (!init[IMAGEBASEOFF].inited)
355 init[IMAGEBASEOFF].value = init[DLLOFF].value
356 ? NT_DLL_IMAGE_BASE : NT_EXE_IMAGE_BASE;
358 /* Glue the assignments into the abs section */
361 stat_ptr = &(abs_output_section->children);
363 for (j = 0; init[j].ptr; j++)
365 long val = init[j].value;
366 lang_add_assignment (exp_assop ('=' ,init[j].symbol, exp_intop (val)));
367 if (init[j].size == sizeof(short))
368 *(short *)init[j].ptr = val;
369 else if (init[j].size == sizeof(int))
370 *(int *)init[j].ptr = val;
371 else if (init[j].size == sizeof(long))
372 *(long *)init[j].ptr = val;
375 /* Restore the pointer. */
378 if (pe.FileAlignment >
381 einfo ("%P: warning, file alignment > section alignment.\n");
386 gld_${EMULATION_NAME}_after_open()
388 /* Pass the wacky PE command line options into the output bfd */
389 struct internal_extra_pe_aouthdr *i;
390 if (!coff_data(output_bfd)->pe)
392 einfo ("%F%P: PE operations on non PE file.\n");
395 pe_data(output_bfd)->pe_opthdr = pe;
396 pe_data(output_bfd)->dll = init[DLLOFF].value;
400 /* Callback functions for qsort in sort_sections. */
403 sort_by_file_name (a, b)
407 lang_statement_union_type **ra = a;
408 lang_statement_union_type **rb = b;
409 return strcmp ((*ra)->input_section.ifile->filename,
410 (*rb)->input_section.ifile->filename);
414 sort_by_section_name (a, b)
418 lang_statement_union_type **ra = a;
419 lang_statement_union_type **rb = b;
420 return strcmp ((*ra)->input_section.section->name,
421 (*rb)->input_section.section->name);
424 /* Subroutine of sort_sections to a contiguous subset of a list of sections.
425 NEXT_AFTER is the element after the last one to sort.
426 The result is a pointer to the last element's "next" pointer. */
428 static lang_statement_union_type **
429 sort_sections_1 (startptr, next_after, count, sort_func)
430 lang_statement_union_type **startptr,*next_after;
434 lang_statement_union_type **vec;
435 lang_statement_union_type *p;
441 vec = (lang_statement_union_type **)
442 alloca (count * sizeof (lang_statement_union_type *));
444 for (p = *startptr, i = 0; i < count; i++, p = p->next)
447 qsort (vec, count, sizeof (vec[0]), sort_func);
449 /* Fill in the next pointers again. */
451 for (i = 0; i < count - 1; i++)
452 vec[i]->header.next = vec[i + 1];
453 vec[i]->header.next = next_after;
454 return &(vec[i]->header.next);
457 /* Sort the .idata\$foo input sections of archives into filename order.
458 The reason is so dlltool can arrange to have the pe dll import information
459 generated correctly - the head of the list goes into dh.o, the tail into
460 dt.o, and the guts into ds[nnnn].o. Note that this is only needed for the
462 FIXME: This may no longer be necessary with grouped sections. Instead of
463 sorting on dh.o, ds[nnnn].o, dt.o, one could, for example, have dh.o use
464 .idata\$4h, have ds[nnnn].o use .idata\$4s[nnnn], and have dt.o use .idata\$4t.
465 This would have to be elaborated upon to handle multiple dll's
466 [assuming such an eloboration is possible of course].
468 We also sort sections in '\$' wild statements. These are created by the
469 place_orphans routine to implement grouped sections. */
473 lang_statement_union_type *s;
475 for (; s ; s = s->next)
476 switch (s->header.type)
478 case lang_output_section_statement_enum:
479 sort_sections (s->output_section_statement.children.head);
481 case lang_wild_statement_enum:
483 lang_statement_union_type **p = &s->wild_statement.children.head;
485 /* Is this the .idata section? */
486 if (s->wild_statement.section_name != NULL
487 && strncmp (s->wild_statement.section_name, ".idata", 6) == 0)
489 /* Sort any children in the same archive. Run through all
490 the children of this wild statement, when an
491 input_section in an archive is found, scan forward to
492 find all input_sections which are in the same archive.
493 Sort them by their filename and then re-thread the
498 lang_statement_union_type *start = *p;
499 if (start->header.type != lang_input_section_enum
500 || !start->input_section.ifile->the_bfd->my_archive)
501 p = &(start->header.next);
504 lang_statement_union_type *end;
507 for (end = start, count = 0;
508 end && end->header.type == lang_input_section_enum
509 && (end->input_section.ifile->the_bfd->my_archive
510 == start->input_section.ifile->the_bfd->my_archive);
514 p = sort_sections_1 (p, end, count, sort_by_file_name);
520 /* If this is a collection of grouped sections, sort them.
521 The linker script must explicitly mention "*(.foo\$)".
522 Don't sort them if \$ is not the last character (not sure if
523 this is really useful, but it allows explicitly mentioning
524 some \$ sections and letting the linker handle the rest). */
525 if (s->wild_statement.section_name != NULL)
527 char *q = strchr (s->wild_statement.section_name, '\$');
531 lang_statement_union_type *end;
534 for (end = *p, count = 0; end; end = end->next)
536 if (end->header.type != lang_input_section_enum)
540 (void) sort_sections_1 (p, end, count, sort_by_section_name);
552 gld_${EMULATION_NAME}_before_allocation()
554 extern lang_statement_list_type *stat_ptr;
556 #ifdef TARGET_IS_ppcpe
557 /* Here we rummage through the found bfds to collect toc information */
559 LANG_FOR_EACH_INPUT_STATEMENT (is)
561 if (!ppc_process_before_allocation(is->the_bfd, &link_info))
563 einfo("Errors encountered processing file %s", is->filename);
568 /* We have seen it all. Allocate it, and carry on */
569 ppc_allocate_toc_section (&link_info);
572 sort_sections (stat_ptr->head);
575 /* Place an orphan section. We use this to put sections with a '\$' in them
576 into the right place. Any section with a '\$' in them (e.g. .text\$foo)
577 gets mapped to the output section with everything from the '\$' on stripped
579 See the Microsoft Portable Executable and Common Object File Format
580 Specification 4.1, section 4.2, Grouped Sections. */
584 gld${EMULATION_NAME}_place_orphan (file, s)
585 lang_input_statement_type *file;
589 char *output_secname, *ps;
590 lang_output_section_statement_type *os;
591 lang_statement_list_type *ptr;
592 lang_statement_union_type *l;
594 if ((s->flags & SEC_ALLOC) == 0)
597 /* Don't process grouped sections unless doing a final link.
598 If they're marked as COMDAT sections, we don't want .text\$foo to
599 end up in .text and then have .text disappear because it's marked
600 link-once-discard. */
601 if (link_info.relocateable)
604 secname = bfd_get_section_name (s->owner, s);
606 /* Everything from the '\$' on gets deleted so don't allow '\$' as the
608 if (*secname == '\$')
609 einfo ("%P%F: section %s has '\$' as first character\n", secname);
610 if (strchr (secname + 1, '\$') == NULL)
613 /* Look up the output section. The Microsoft specs say sections names in
614 image files never contain a '\$'. Fortunately, lang_..._lookup creates
615 the section if it doesn't exist. */
616 output_secname = buystring (secname);
617 ps = strchr (output_secname + 1, '\$');
619 os = lang_output_section_statement_lookup (output_secname);
621 /* Find the '\$' wild statement for this section. We currently require the
622 linker script to explicitly mention "*(.foo\$)".
623 FIXME: ppcpe.sc has .CRT\$foo in the .rdata section. According to the
624 Microsoft docs this isn't correct so it's not (currently) handled. */
628 for (l = os->children.head; l; l = l->next)
630 if (l->header.type == lang_wild_statement_enum
631 && strcmp (l->wild_statement.section_name, output_secname) == 0)
637 einfo ("%P%F: *(%s\$) missing from linker script\n", output_secname);
638 #else /* FIXME: This block is untried. It exists to convey the intent,
639 should one decide to not require *(.foo\$) to appear in the linker
642 lang_wild_statement_type *new = new_stat (lang_wild_statement,
644 new->section_name = xmalloc (strlen (output_secname) + 2);
645 sprintf (new->section_name, "%s\$", output_secname);
646 new->filename = NULL;
647 lang_list_init (&new->children);
652 /* Link the input section in and we're done for now.
653 The sections still have to be sorted, but that has to wait until
654 all such sections have been processed by us. The sorting is done by
656 wild_doit (&l->wild_statement.children, s, os, file);
662 gld_${EMULATION_NAME}_get_script(isfile)
665 # Scripts compiled in.
666 # sed commands to quote an ld script as a C string.
667 sc="-f ${srcdir}/emultempl/stringify.sed"
669 cat >>e${EMULATION_NAME}.c <<EOF
673 if (link_info.relocateable == true && config.build_constructors == true)
676 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
677 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
678 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
679 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
680 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
681 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
682 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
683 echo ' ; else return' >> e${EMULATION_NAME}.c
684 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
685 echo '; }' >> e${EMULATION_NAME}.c
687 cat >>e${EMULATION_NAME}.c <<EOF
690 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
692 gld_${EMULATION_NAME}_before_parse,
696 gld_${EMULATION_NAME}_after_open,
697 after_allocation_default,
698 set_output_arch_default,
699 ldemul_default_target,
700 gld_${EMULATION_NAME}_before_allocation,
701 gld_${EMULATION_NAME}_get_script,
705 NULL, /* create output section statements */
706 NULL, /* open dynamic archive */
707 gld${EMULATION_NAME}_place_orphan,
708 gld_${EMULATION_NAME}_set_symbols,
709 gld_${EMULATION_NAME}_parse_args