1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 if [ -z "$MACHINE" ]; then
6 OUTPUT_ARCH=${ARCH}:${MACHINE}
8 cat >e${EMULATION_NAME}.c <<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 Free Software Foundation, Inc.
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
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. */
38 #include "libiberty.h"
49 #include "coff/internal.h"
50 #include "../bfd/libcoff.h"
52 #define TARGET_IS_${EMULATION_NAME}
54 static struct internal_extra_pe_aouthdr pe;
57 extern const char *output_filename;
60 gld_${EMULATION_NAME}_before_parse (void)
62 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
63 output_filename = "a.exe";
66 /* PE format extra command line options. */
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)
85 gld${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)
90 static const struct option xtra_long[] = {
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}
109 *longopts = (struct option *)
110 xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
111 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
115 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
116 parameters which may be input from the command line */
126 #define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol,0}
128 static definfo init[] =
130 /* imagebase must be first */
131 #define IMAGEBASEOFF 0
132 D(ImageBase,"__image_base__", BEOS_EXE_IMAGE_BASE),
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 }
154 set_pe_name (char *name, long val)
157 /* Find the name and set it. */
158 for (i = 0; init[i].ptr; i++)
160 if (strcmp (name, init[i].symbol) == 0)
172 set_pe_subsystem (void)
185 { "native", 1, "_NtProcessStartup" },
186 { "windows", 2, "_WinMainCRTStartup" },
187 { "wwindows", 2, "_wWinMainCRTStartup" },
188 { "console", 3, "_mainCRTStartup" },
189 { "wconsole", 3, "_wmainCRTStartup" },
190 { "posix", 7, "___PosixProcessStartup"},
194 sver = strchr (optarg, ':');
196 len = strlen (optarg);
202 set_pe_name ("__major_subsystem_version__",
203 strtoul (sver + 1, &end, 0));
205 set_pe_name ("__minor_subsystem_version__",
206 strtoul (end + 1, &end, 0));
208 einfo ("%P: warning: bad version number in -subsystem option\n");
211 for (i = 0; v[i].name; i++)
213 if (strncmp (optarg, v[i].name, len) == 0
214 && v[i].name[len] == '\0')
216 set_pe_name ("__subsystem__", v[i].value);
218 /* If the subsystem is windows, we use a different entry
220 lang_default_entry (v[i].entry);
225 einfo ("%P%F: invalid subsystem type %s\n", optarg);
230 set_pe_value (char *name)
233 set_pe_name (name, strtoul (optarg, &end, 0));
236 einfo ("%P%F: invalid hex number for PE parameter '%s'\n", optarg);
243 set_pe_stack_heap (char *resname, char *comname)
245 set_pe_value (resname);
249 set_pe_value (comname);
253 einfo ("%P%F: strange hex info for PE parameter '%s'\n", optarg);
259 gld${EMULATION_NAME}_handle_option (int optc)
266 case OPTION_BASE_FILE:
267 link_info.base_file = fopen (optarg, FOPEN_WB);
268 if (link_info.base_file == NULL)
270 fprintf (stderr, "%s: Can't open base file %s\n",
271 program_name, optarg);
278 set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
281 set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
283 case OPTION_SUBSYSTEM:
286 case OPTION_MAJOR_OS_VERSION:
287 set_pe_value ("__major_os_version__");
289 case OPTION_MINOR_OS_VERSION:
290 set_pe_value ("__minor_os_version__");
292 case OPTION_MAJOR_SUBSYSTEM_VERSION:
293 set_pe_value ("__major_subsystem_version__");
295 case OPTION_MINOR_SUBSYSTEM_VERSION:
296 set_pe_value ("__minor_subsystem_version__");
298 case OPTION_MAJOR_IMAGE_VERSION:
299 set_pe_value ("__major_image_version__");
301 case OPTION_MINOR_IMAGE_VERSION:
302 set_pe_value ("__minor_image_version__");
304 case OPTION_FILE_ALIGNMENT:
305 set_pe_value ("__file_alignment__");
307 case OPTION_SECTION_ALIGNMENT:
308 set_pe_value ("__section_alignment__");
311 set_pe_name ("__dll__", 1);
313 case OPTION_IMAGE_BASE:
314 set_pe_value ("__image_base__");
320 /* Assign values to the special symbols before the linker script is
324 gld_${EMULATION_NAME}_set_symbols (void)
326 /* Run through and invent symbols for all the
327 names and insert the defaults. */
329 lang_statement_list_type *save;
331 if (!init[IMAGEBASEOFF].inited)
333 if (link_info.relocatable)
334 init[IMAGEBASEOFF].value = 0;
335 else if (init[DLLOFF].value)
336 init[IMAGEBASEOFF].value = BEOS_DLL_IMAGE_BASE;
338 init[IMAGEBASEOFF].value = BEOS_EXE_IMAGE_BASE;
341 /* Don't do any symbol assignments if this is a relocatable link. */
342 if (link_info.relocatable)
345 /* Glue the assignments into the abs section */
348 stat_ptr = &(abs_output_section->children);
350 for (j = 0; init[j].ptr; j++)
352 long val = init[j].value;
353 lang_add_assignment (exp_assop ('=', init[j].symbol, exp_intop (val)));
354 if (init[j].size == sizeof(short))
355 *(short *)init[j].ptr = val;
356 else if (init[j].size == sizeof(int))
357 *(int *)init[j].ptr = val;
358 else if (init[j].size == sizeof(long))
359 *(long *)init[j].ptr = val;
360 /* This might be a long long or other special type. */
361 else if (init[j].size == sizeof(bfd_vma))
362 *(bfd_vma *)init[j].ptr = val;
365 /* Restore the pointer. */
368 if (pe.FileAlignment >
371 einfo ("%P: warning, file alignment > section alignment.\n");
376 gld_${EMULATION_NAME}_after_open (void)
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(output_bfd)->pe)
383 einfo ("%F%P: PE operations on non PE file.\n");
386 pe_data(output_bfd)->pe_opthdr = pe;
387 pe_data(output_bfd)->dll = init[DLLOFF].value;
391 /* Callback functions for qsort in sort_sections. */
394 sort_by_file_name (const void *a, const void *b)
396 const lang_statement_union_type *const *ra = a;
397 const lang_statement_union_type *const *rb = b;
400 i = strcmp ((*ra)->input_section.section->owner->my_archive->filename,
401 (*rb)->input_section.section->owner->my_archive->filename);
405 i = strcmp ((*ra)->input_section.section->owner->filename,
406 (*rb)->input_section.section->owner->filename);
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 */
415 if ( ((*ra)->input_section.section->reloc_count + (*rb)->input_section.section->reloc_count) )
417 i = (((*ra)->input_section.section->reloc_count >
418 (*rb)->input_section.section->reloc_count) ? -1 : 0);
422 return (((*ra)->input_section.section->reloc_count >
423 (*rb)->input_section.section->reloc_count) ? 0 : 1);
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 */
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);
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);
440 i = ((a_sec < b_sec) ? -1 : 0);
443 return ((a_sec < b_sec) ? 0 : 1);
449 sort_by_section_name (const void *a, const void *b)
451 const lang_statement_union_type *const *ra = a;
452 const lang_statement_union_type *const *rb = b;
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 */
461 if ((strncmp ((*ra)->input_section.section->name, ".stab", 5) == 0)
462 && (strncmp ((*rb)->input_section.section->name, ".stab", 5) != 0))
469 /* Subroutine of sort_sections to a contiguous subset of a list of sections.
470 NEXT_AFTER is the element after the last one to sort.
471 The result is a pointer to the last element's "next" pointer. */
473 static lang_statement_union_type **
474 sort_sections_1 (lang_statement_union_type **startptr,
475 lang_statement_union_type *next_after,
477 int (*sort_func) (const void *, const void *))
479 lang_statement_union_type **vec;
480 lang_statement_union_type *p;
482 lang_statement_union_type **ret;
487 vec = ((lang_statement_union_type **)
488 xmalloc (count * sizeof (lang_statement_union_type *)));
490 for (p = *startptr, i = 0; i < count; i++, p = p->header.next)
493 qsort (vec, count, sizeof (vec[0]), sort_func);
495 /* Fill in the next pointers again. */
497 for (i = 0; i < count - 1; i++)
498 vec[i]->header.next = vec[i + 1];
499 vec[i]->header.next = next_after;
500 ret = &vec[i]->header.next;
505 /* Sort the .idata\$foo input sections of archives into filename order.
506 The reason is so dlltool can arrange to have the pe dll import information
507 generated correctly - the head of the list goes into dh.o, the tail into
508 dt.o, and the guts into ds[nnnn].o. Note that this is only needed for the
510 FIXME: This may no longer be necessary with grouped sections. Instead of
511 sorting on dh.o, ds[nnnn].o, dt.o, one could, for example, have dh.o use
512 .idata\$4h, have ds[nnnn].o use .idata\$4s[nnnn], and have dt.o use .idata\$4t.
513 This would have to be elaborated upon to handle multiple dll's
514 [assuming such an eloboration is possible of course].
516 We also sort sections in '\$' wild statements. These are created by the
517 place_orphans routine to implement grouped sections. */
520 sort_sections (lang_statement_union_type *s)
522 for (; s ; s = s->header.next)
523 switch (s->header.type)
525 case lang_output_section_statement_enum:
526 sort_sections (s->output_section_statement.children.head);
528 case lang_wild_statement_enum:
530 lang_statement_union_type **p = &s->wild_statement.children.head;
531 struct wildcard_list *sec;
533 for (sec = s->wild_statement.section_list; sec; sec = sec->next)
535 /* Is this the .idata section? */
536 if (sec->spec.name != NULL
537 && strncmp (sec->spec.name, ".idata", 6) == 0)
539 /* Sort the children. We want to sort any objects in
540 the same archive. In order to handle the case of
541 including a single archive multiple times, we sort
542 all the children by archive name and then by object
543 name. After sorting them, we re-thread the pointer
548 lang_statement_union_type *start = *p;
549 if (start->header.type != lang_input_section_enum
550 || !start->input_section.section->owner->my_archive)
551 p = &(start->header.next);
554 lang_statement_union_type *end;
557 for (end = start, count = 0;
558 end && (end->header.type
559 == lang_input_section_enum);
560 end = end->header.next)
563 p = sort_sections_1 (p, end, count,
570 /* If this is a collection of grouped sections, sort them.
571 The linker script must explicitly mention "*(.foo\$)" or
572 "*(.foo\$*)". Don't sort them if \$ is not the last
573 character (not sure if this is really useful, but it
574 allows explicitly mentioning some \$ sections and letting
575 the linker handle the rest). */
576 if (sec->spec.name != NULL)
578 char *q = strchr (sec->spec.name, '\$');
582 || (q[1] == '*' && q[2] == '\0')))
584 lang_statement_union_type *end;
587 for (end = *p, count = 0; end; end = end->header.next)
589 if (end->header.type != lang_input_section_enum)
593 (void) sort_sections_1 (p, end, count,
594 sort_by_section_name);
607 gld_${EMULATION_NAME}_before_allocation (void)
609 extern lang_statement_list_type *stat_ptr;
611 #ifdef TARGET_IS_ppcpe
612 /* Here we rummage through the found bfds to collect toc information */
614 LANG_FOR_EACH_INPUT_STATEMENT (is)
616 if (!ppc_process_before_allocation(is->the_bfd, &link_info))
618 einfo("Errors encountered processing file %s\n", is->filename);
623 /* We have seen it all. Allocate it, and carry on */
624 ppc_allocate_toc_section (&link_info);
626 #ifdef TARGET_IS_armpe
627 /* FIXME: we should be able to set the size of the interworking stub
630 Here we rummage through the found bfds to collect glue
631 information. FIXME: should this be based on a command line
632 option? krk@cygnus.com */
634 LANG_FOR_EACH_INPUT_STATEMENT (is)
636 if (!arm_process_before_allocation (is->the_bfd, & link_info))
638 einfo ("Errors encountered processing file %s", is->filename);
643 /* We have seen it all. Allocate it, and carry on */
644 arm_allocate_interworking_sections (& link_info);
645 #endif /* TARGET_IS_armpe */
646 #endif /* TARGET_IS_ppcpe */
648 sort_sections (stat_ptr->head);
650 before_allocation_default ();
653 /* Place an orphan section. We use this to put sections with a '\$' in them
654 into the right place. Any section with a '\$' in them (e.g. .text\$foo)
655 gets mapped to the output section with everything from the '\$' on stripped
657 See the Microsoft Portable Executable and Common Object File Format
658 Specification 4.1, section 4.2, Grouped Sections.
660 FIXME: This is now handled by the linker script using wildcards,
661 but I'm leaving this here in case we want to enable it for sections
662 which are not mentioned in the linker script. */
665 gld${EMULATION_NAME}_place_orphan (asection *s)
668 char *output_secname, *ps;
669 lang_output_section_statement_type *os;
670 lang_statement_union_type *l;
672 if ((s->flags & SEC_ALLOC) == 0)
675 /* Don't process grouped sections unless doing a final link.
676 If they're marked as COMDAT sections, we don't want .text\$foo to
677 end up in .text and then have .text disappear because it's marked
678 link-once-discard. */
679 if (link_info.relocatable)
682 secname = bfd_get_section_name (s->owner, s);
684 /* Everything from the '\$' on gets deleted so don't allow '\$' as the
686 if (*secname == '\$')
687 einfo ("%P%F: section %s has '\$' as first character\n", secname);
688 if (strchr (secname + 1, '\$') == NULL)
691 /* Look up the output section. The Microsoft specs say sections names in
692 image files never contain a '\$'. Fortunately, lang_..._lookup creates
693 the section if it doesn't exist. */
694 output_secname = xstrdup (secname);
695 ps = strchr (output_secname + 1, '\$');
697 os = lang_output_section_statement_lookup (output_secname);
699 /* Find the '\$' wild statement for this section. We currently require the
700 linker script to explicitly mention "*(.foo\$)".
701 FIXME: ppcpe.sc has .CRT\$foo in the .rdata section. According to the
702 Microsoft docs this isn't correct so it's not (currently) handled. */
706 for (l = os->children.head; l; l = l->header.next)
707 if (l->header.type == lang_wild_statement_enum)
709 struct wildcard_list *sec;
711 for (sec = l->wild_statement.section_list; sec; sec = sec->next)
712 if (sec->spec.name && strcmp (sec->spec.name, output_secname) == 0)
719 einfo ("%P%F: *(%s\$) missing from linker script\n", output_secname);
721 /* Link the input section in and we're done for now.
722 The sections still have to be sorted, but that has to wait until
723 all such sections have been processed by us. The sorting is done by
725 lang_add_section (&l->wild_statement.children, s, os);
731 gld_${EMULATION_NAME}_get_script (int *isfile)
733 # Scripts compiled in.
734 # sed commands to quote an ld script as a C string.
735 sc="-f stringify.sed"
737 cat >>e${EMULATION_NAME}.c <<EOF
741 if (link_info.relocatable && config.build_constructors)
744 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
745 echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c
746 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
747 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
748 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
749 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
750 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
751 echo ' ; else return' >> e${EMULATION_NAME}.c
752 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
753 echo '; }' >> e${EMULATION_NAME}.c
755 cat >>e${EMULATION_NAME}.c <<EOF
758 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
760 gld_${EMULATION_NAME}_before_parse,
764 gld_${EMULATION_NAME}_after_open,
765 after_allocation_default,
766 set_output_arch_default,
767 ldemul_default_target,
768 gld_${EMULATION_NAME}_before_allocation,
769 gld_${EMULATION_NAME}_get_script,
773 NULL, /* create output section statements */
774 NULL, /* open dynamic archive */
775 gld${EMULATION_NAME}_place_orphan,
776 gld_${EMULATION_NAME}_set_symbols,
777 NULL, /* parse_args */
778 gld${EMULATION_NAME}_add_options,
779 gld${EMULATION_NAME}_handle_option,
780 NULL, /* unrecognized file */
781 NULL, /* list options */
782 NULL, /* recognized file */
783 NULL, /* find_potential_libraries */
784 NULL /* new_vers_pattern */