# This shell script emits a C file. -*- C -*- # It does some substitutions. cat >e${EMULATION_NAME}.c <children); for (j = 0; init[j].ptr; j++) { long val = init[j].value; lang_add_assignment (exp_assop ('=' ,init[j].symbol, exp_intop (val))); if (init[j].size == sizeof(short)) *(short *)init[j].ptr = val; else if (init[j].size == sizeof(int)) *(int *)init[j].ptr = val; else if (init[j].size == sizeof(long)) *(long *)init[j].ptr = val; else abort(); } /* Restore the pointer. */ stat_ptr = save; if (pe.FileAlignment > pe.SectionAlignment) { einfo ("%P: warning, file alignment > section alignment.\n"); } } static void gld_${EMULATION_NAME}_after_open() { /* Pass the wacky PE command line options into the output bfd */ struct internal_extra_pe_aouthdr *i; if (!coff_data(output_bfd)->pe) { einfo ("%F%P: PE operations on non PE file.\n"); } pe_data(output_bfd)->pe_opthdr = pe; pe_data(output_bfd)->dll = init[DLLOFF].value; } /* Callback functions for qsort in sort_sections. */ static int sort_by_file_name (a, b) void *a; void *b; { lang_statement_union_type **ra = a; lang_statement_union_type **rb = b; return strcmp ((*ra)->input_section.ifile->filename, (*rb)->input_section.ifile->filename); } static int sort_by_section_name (a, b) void *a; void *b; { lang_statement_union_type **ra = a; lang_statement_union_type **rb = b; return strcmp ((*ra)->input_section.section->name, (*rb)->input_section.section->name); } /* Subroutine of sort_sections to a contiguous subset of a list of sections. NEXT_AFTER is the element after the last one to sort. The result is a pointer to the last element's "next" pointer. */ static lang_statement_union_type ** sort_sections_1 (startptr, next_after, count, sort_func) lang_statement_union_type **startptr,*next_after; int count; int (*sort_func) (); { lang_statement_union_type **vec; lang_statement_union_type *p; int i; if (count == 0) return startptr; vec = (lang_statement_union_type **) alloca (count * sizeof (lang_statement_union_type *)); for (p = *startptr, i = 0; i < count; i++, p = p->next) vec[i] = p; qsort (vec, count, sizeof (vec[0]), sort_func); /* Fill in the next pointers again. */ *startptr = vec[0]; for (i = 0; i < count - 1; i++) vec[i]->header.next = vec[i + 1]; vec[i]->header.next = next_after; return &(vec[i]->header.next); } /* Sort the .idata\$foo input sections of archives into filename order. The reason is so dlltool can arrange to have the pe dll import information generated correctly - the head of the list goes into dh.o, the tail into dt.o, and the guts into ds[nnnn].o. Note that this is only needed for the .idata section. FIXME: This may no longer be necessary with grouped sections. Instead of sorting on dh.o, ds[nnnn].o, dt.o, one could, for example, have dh.o use .idata\$4h, have ds[nnnn].o use .idata\$4s[nnnn], and have dt.o use .idata\$4t. This would have to be elaborated upon to handle multiple dll's [assuming such an eloboration is possible of course]. We also sort sections in '\$' wild statements. These are created by the place_orphans routine to implement grouped sections. */ static void sort_sections (s) lang_statement_union_type *s; { for (; s ; s = s->next) switch (s->header.type) { case lang_output_section_statement_enum: sort_sections (s->output_section_statement.children.head); break; case lang_wild_statement_enum: { lang_statement_union_type **p = &s->wild_statement.children.head; /* Is this the .idata section? */ if (strncmp (s->wild_statement.section_name, ".idata", 6) == 0) { /* Sort any children in the same archive. Run through all the children of this wild statement, when an input_section in an archive is found, scan forward to find all input_sections which are in the same archive. Sort them by their filename and then re-thread the pointer chain. */ while (*p) { lang_statement_union_type *start = *p; if (start->header.type != lang_input_section_enum || !start->input_section.ifile->the_bfd->my_archive) p = &(start->header.next); else { lang_statement_union_type *end; int count; for (end = start, count = 0; end && end->header.type == lang_input_section_enum && (end->input_section.ifile->the_bfd->my_archive == start->input_section.ifile->the_bfd->my_archive); end = end->next) count++; p = sort_sections_1 (p, end, count, sort_by_file_name); } } break; } /* If this is a collection of grouped sections, sort them. The linker script must explicitly mention "*(.foo\$)". Don't sort them if \$ is not the last character (not sure if this is really useful, but it allows explicitly mentioning some \$ sections and letting the linker handle the rest). */ { char *q = strchr (s->wild_statement.section_name, '\$'); if (q && q[1] == 0) { lang_statement_union_type *end; int count; for (end = *p, count = 0; end; end = end->next) { if (end->header.type != lang_input_section_enum) abort (); count++; } (void) sort_sections_1 (p, end, count, sort_by_section_name); } break; } } break; default: break; } } static void gld_${EMULATION_NAME}_before_allocation() { extern lang_statement_list_type *stat_ptr; #ifdef TARGET_IS_ppcpe /* Here we rummage through the found bfds to collect toc information */ { LANG_FOR_EACH_INPUT_STATEMENT (is) { ppc_process_before_allocation(is->the_bfd, &link_info); } } /* We have seen it all. Allocate it, and carry on */ ppc_allocate_toc_section (&link_info); #endif sort_sections (stat_ptr->head); } /* Place an orphan section. We use this to put sections with a '\$' in them into the right place. Any section with a '\$' in them (e.g. .text\$foo) gets mapped to the output section with everything from the '\$' on stripped (e.g. .text). See the Microsoft Portable Executable and Common Object File Format Specification 4.1, section 4.2, Grouped Sections. */ /*ARGSUSED*/ static boolean gld${EMULATION_NAME}_place_orphan (file, s) lang_input_statement_type *file; asection *s; { const char *secname; char *output_secname, *ps; lang_output_section_statement_type *os; lang_statement_list_type *ptr; lang_statement_union_type *l; if ((s->flags & SEC_ALLOC) == 0) return false; /* Don't process grouped sections unless doing a final link. If they're marked as COMDAT sections, we don't want .text\$foo to end up in .text and then have .text disappear because it's marked link-once-discard. */ if (link_info.relocateable) return false; secname = bfd_get_section_name (s->owner, s); /* Everything from the '\$' on gets deleted so don't allow '\$' as the first character. */ if (*secname == '\$') einfo ("%P%F: section %s has '\$' as first character\n", secname); if (strchr (secname + 1, '\$') == NULL) return false; /* Look up the output section. The Microsoft specs say sections names in image files never contain a '\$'. Fortunately, lang_..._lookup creates the section if it doesn't exist. */ output_secname = buystring (secname); ps = strchr (output_secname + 1, '\$'); *ps = 0; os = lang_output_section_statement_lookup (output_secname); /* Find the '\$' wild statement for this section. We currently require the linker script to explicitly mention "*(.foo\$)". FIXME: ppcpe.sc has .CRT\$foo in the .rdata section. According to the Microsoft docs this isn't correct so it's not (currently) handled. */ ps[0] = '\$'; ps[1] = 0; for (l = os->children.head; l; l = l->next) { if (l->header.type == lang_wild_statement_enum && strcmp (l->wild_statement.section_name, output_secname) == 0) break; } ps[0] = 0; if (l == NULL) #if 1 einfo ("%P%F: *(%s\$) missing from linker script\n", output_secname); #else /* FIXME: This block is untried. It exists to convey the intent, should one decide to not require *(.foo\$) to appear in the linker script. */ { lang_wild_statement_type *new = new_stat (lang_wild_statement, &os->children); new->section_name = xmalloc (strlen (output_secname) + 2); sprintf (new->section_name, "%s\$", output_secname); new->filename = NULL; lang_list_init (&new->children); l = new; } #endif /* Link the input section in and we're done for now. The sections still have to be sorted, but that has to wait until all such sections have been processed by us. The sorting is done by sort_sections. */ wild_doit (&l->wild_statement.children, s, os, file); return true; } static char * gld_${EMULATION_NAME}_get_script(isfile) int *isfile; EOF # Scripts compiled in. # sed commands to quote an ld script as a C string. sc="-f ${srcdir}/emultempl/stringify.sed" cat >>e${EMULATION_NAME}.c <> e${EMULATION_NAME}.c echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c echo ' ; else return' >> e${EMULATION_NAME}.c sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c echo '; }' >> e${EMULATION_NAME}.c cat >>e${EMULATION_NAME}.c <