Commit | Line | Data |
---|---|---|
5f8ac7e7 SC |
1 | # This shell script emits a C file. -*- C -*- |
2 | # It does some substitutions. | |
3 | cat >e${EMULATION_NAME}.c <<EOF | |
4 | /* For WINDOWS_NT */ | |
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. */ | |
10 | ||
11 | ||
12 | /* This file is part of GLD, the Gnu Linker. | |
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
27 | ||
5f8ac7e7 SC |
28 | #include "bfd.h" |
29 | #include "sysdep.h" | |
30 | #include "bfdlink.h" | |
31 | #include "getopt.h" | |
32 | #include "ld.h" | |
5f8ac7e7 SC |
33 | #include "ld.h" |
34 | #include "ldmain.h" | |
35 | #include "ldgram.h" | |
36 | #include "ldexp.h" | |
37 | #include "ldlang.h" | |
38 | #include "ldemul.h" | |
39 | #include "ldlex.h" | |
40 | #include "ldmisc.h" | |
41 | #include "ldctor.h" | |
42 | #include "ldfile.h" | |
43 | #include "coff/internal.h" | |
44 | #include "../bfd/libcoff.h" | |
45 | ||
d4279937 SC |
46 | #define TARGET_IS_${EMULATION_NAME} |
47 | ||
fe6e2957 DE |
48 | static void gld_${EMULATION_NAME}_set_symbols PARAMS ((void)); |
49 | static void gld_${EMULATION_NAME}_after_open PARAMS ((void)); | |
5f8ac7e7 | 50 | static void gld_${EMULATION_NAME}_before_parse PARAMS ((void)); |
fe6e2957 | 51 | static void gld_${EMULATION_NAME}_before_allocation PARAMS ((void)); |
a1613b5f DE |
52 | static boolean gld${EMULATION_NAME}_place_orphan |
53 | PARAMS ((lang_input_statement_type *, asection *)); | |
fe6e2957 DE |
54 | static char *gld_${EMULATION_NAME}_get_script PARAMS ((int *)); |
55 | static int gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **)); | |
56 | ||
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 *)); | |
60 | #endif | |
61 | static lang_statement_union_type **sort_sections_1 | |
62 | PARAMS ((lang_statement_union_type **, lang_statement_union_type *, int, | |
63 | int (*)())); | |
64 | static void sort_sections PARAMS ((lang_statement_union_type *)); | |
5f8ac7e7 SC |
65 | |
66 | static struct internal_extra_pe_aouthdr pe; | |
67 | static int dll; | |
68 | ||
69 | static void | |
70 | gld_${EMULATION_NAME}_before_parse() | |
71 | { | |
72 | ldfile_output_architecture = bfd_arch_${ARCH}; | |
73 | } | |
fe6e2957 DE |
74 | \f |
75 | /* PE format extra command line options. */ | |
5f8ac7e7 SC |
76 | |
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) | |
92 | ||
a1613b5f | 93 | static struct option longopts[] = { |
5f8ac7e7 SC |
94 | /* PE options */ |
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} | |
110 | }; | |
111 | ||
112 | ||
113 | /* PE/WIN32; added routines to get the subsystem type, heap and/or stack | |
114 | parameters which may be input from the command line */ | |
115 | ||
5f8ac7e7 SC |
116 | typedef struct { |
117 | void *ptr; | |
118 | int size; | |
119 | int value; | |
120 | char *symbol; | |
121 | int inited; | |
122 | } definfo; | |
123 | ||
124 | #define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol,0} | |
125 | ||
126 | static definfo init[] = | |
127 | { | |
128 | /* imagebase must be first */ | |
129 | #define IMAGEBASEOFF 0 | |
130 | D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE), | |
131 | #define DLLOFF 1 | |
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), | |
417fe276 | 142 | D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x2000000), |
5f8ac7e7 SC |
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), | |
147 | 0 | |
148 | }; | |
149 | ||
150 | ||
151 | static void | |
152 | set_pe_name (name, val) | |
153 | char *name; | |
154 | 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 | ||
171 | static void | |
172 | set_pe_subsystem () | |
173 | { | |
174 | int i; | |
175 | static struct | |
176 | { | |
177 | char *name ; | |
178 | int value; | |
179 | } | |
180 | v[] = | |
181 | { | |
2543860d SC |
182 | {"native", 1}, |
183 | {"windows",2}, | |
184 | {"console",3}, | |
185 | {"os2",5}, | |
186 | {"posix", 7}, | |
5f8ac7e7 SC |
187 | {0,0} |
188 | }; | |
189 | ||
190 | for (i = 0; v[i].name; i++) | |
191 | { | |
192 | if (!strcmp (optarg, v[i].name)) | |
193 | { | |
194 | set_pe_name ("__subsystem__", v[i].value); | |
d0d63887 ILT |
195 | |
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. */ | |
212 | if (v[i].value == 2) | |
213 | { | |
214 | lang_add_entry ("_WinMainCRTStartup", 1); | |
215 | ldlang_add_undef ("_WinMainCRTStartup"); | |
216 | } | |
217 | ||
5f8ac7e7 SC |
218 | return; |
219 | } | |
220 | } | |
221 | einfo ("%P%F: invalid subsystem type %s\n", optarg); | |
222 | } | |
223 | ||
224 | ||
225 | ||
226 | static void | |
227 | set_pe_value (name) | |
228 | char *name; | |
229 | ||
230 | { | |
231 | char *end; | |
fe6e2957 | 232 | set_pe_name (name, strtoul (optarg, &end, 0)); |
5f8ac7e7 SC |
233 | if (end == optarg) |
234 | { | |
235 | einfo ("%P%F: invalid hex number for PE parameter '%s'\n", optarg); | |
236 | } | |
237 | ||
238 | optarg = end; | |
239 | } | |
240 | ||
241 | static void | |
242 | set_pe_stack_heap (resname, comname) | |
243 | char *resname; | |
244 | char *comname; | |
245 | { | |
246 | char *begin_commit; | |
247 | char *end; | |
248 | ||
249 | set_pe_value (resname); | |
250 | if (*optarg == ',') | |
251 | { | |
252 | optarg++; | |
253 | set_pe_value (comname); | |
254 | } | |
255 | else if (*optarg) | |
256 | { | |
257 | einfo ("%P%F: strange hex info for PE parameter '%s'\n", optarg); | |
258 | } | |
259 | } | |
260 | ||
261 | ||
262 | ||
263 | static int | |
264 | gld_${EMULATION_NAME}_parse_args(argc, argv) | |
265 | int argc; | |
266 | char **argv; | |
267 | { | |
268 | int longind; | |
269 | int optc; | |
270 | int prevoptind = optind; | |
271 | int prevopterr = opterr; | |
2543860d SC |
272 | int wanterror; |
273 | static int lastoptind = -1; | |
274 | ||
275 | if (lastoptind != optind) | |
276 | opterr = 0; | |
277 | wanterror = opterr; | |
278 | ||
279 | lastoptind = optind; | |
280 | ||
5f8ac7e7 SC |
281 | optc = getopt_long_only (argc, argv, "-", longopts, &longind); |
282 | opterr = prevopterr; | |
2543860d | 283 | |
5f8ac7e7 SC |
284 | switch (optc) |
285 | { | |
286 | default: | |
2543860d SC |
287 | if (wanterror) |
288 | xexit (1); | |
289 | optind = prevoptind; | |
5f8ac7e7 SC |
290 | return 0; |
291 | ||
292 | case OPTION_BASE_FILE: | |
d4279937 | 293 | link_info.base_file = (PTR) fopen (optarg, FOPEN_WB); |
5f8ac7e7 SC |
294 | if (link_info.base_file == NULL) |
295 | { | |
296 | fprintf (stderr, "%s: Can't open base file %s\n", | |
297 | program_name, optarg); | |
298 | xexit (1); | |
299 | } | |
300 | break; | |
301 | ||
302 | /* PE options */ | |
303 | case OPTION_HEAP: | |
a1613b5f | 304 | set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__"); |
5f8ac7e7 SC |
305 | break; |
306 | case OPTION_STACK: | |
a1613b5f | 307 | set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__"); |
5f8ac7e7 SC |
308 | break; |
309 | case OPTION_SUBSYSTEM: | |
310 | set_pe_subsystem (); | |
311 | break; | |
312 | case OPTION_MAJOR_OS_VERSION: | |
313 | set_pe_value ("__major_os_version__"); | |
314 | break; | |
315 | case OPTION_MINOR_OS_VERSION: | |
316 | set_pe_value ("__minor_os_version__"); | |
317 | break; | |
318 | case OPTION_MAJOR_SUBSYSTEM_VERSION: | |
319 | set_pe_value ("__major_subsystem_version__"); | |
320 | break; | |
321 | case OPTION_MINOR_SUBSYSTEM_VERSION: | |
2543860d | 322 | set_pe_value ("__minor_subsystem_version__"); |
5f8ac7e7 SC |
323 | break; |
324 | case OPTION_MAJOR_IMAGE_VERSION: | |
325 | set_pe_value ("__major_image_version__"); | |
326 | break; | |
327 | case OPTION_MINOR_IMAGE_VERSION: | |
328 | set_pe_value ("__minor_image_version__"); | |
329 | break; | |
330 | case OPTION_FILE_ALIGNMENT: | |
331 | set_pe_value ("__file_alignment__"); | |
332 | break; | |
333 | case OPTION_SECTION_ALIGNMENT: | |
334 | set_pe_value ("__section_alignment__"); | |
335 | break; | |
336 | case OPTION_DLL: | |
337 | set_pe_name ("__dll__", 1); | |
338 | break; | |
339 | case OPTION_IMAGE_BASE: | |
340 | set_pe_value ("__image_base__"); | |
341 | break; | |
342 | } | |
343 | return 1; | |
344 | } | |
fe6e2957 | 345 | \f |
5f8ac7e7 | 346 | static void |
fe6e2957 | 347 | gld_${EMULATION_NAME}_set_symbols() |
5f8ac7e7 | 348 | { |
5f8ac7e7 SC |
349 | /* Run through and invent symbols for all the |
350 | names and insert the defaults. */ | |
351 | int j; | |
2543860d | 352 | lang_statement_list_type *save; |
5f8ac7e7 SC |
353 | |
354 | if (!init[IMAGEBASEOFF].inited) | |
355 | init[IMAGEBASEOFF].value = init[DLLOFF].value | |
356 | ? NT_DLL_IMAGE_BASE : NT_EXE_IMAGE_BASE; | |
357 | ||
2543860d | 358 | /* Glue the assignments into the abs section */ |
d4279937 | 359 | save = stat_ptr; |
2543860d SC |
360 | |
361 | stat_ptr = &(abs_output_section->children); | |
d4279937 | 362 | |
5f8ac7e7 SC |
363 | for (j = 0; init[j].ptr; j++) |
364 | { | |
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; | |
373 | else abort(); | |
374 | } | |
2543860d SC |
375 | /* Restore the pointer. */ |
376 | stat_ptr = save; | |
377 | ||
5f8ac7e7 SC |
378 | if (pe.FileAlignment > |
379 | pe.SectionAlignment) | |
380 | { | |
381 | einfo ("%P: warning, file alignment > section alignment.\n"); | |
382 | } | |
383 | } | |
384 | ||
385 | static void | |
386 | gld_${EMULATION_NAME}_after_open() | |
387 | { | |
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) | |
391 | { | |
392 | einfo ("%F%P: PE operations on non PE file.\n"); | |
393 | } | |
394 | ||
395 | pe_data(output_bfd)->pe_opthdr = pe; | |
396 | pe_data(output_bfd)->dll = init[DLLOFF].value; | |
397 | ||
398 | } | |
a1613b5f | 399 | \f |
fe6e2957 | 400 | /* Callback functions for qsort in sort_sections. */ |
5f8ac7e7 | 401 | |
fe6e2957 DE |
402 | static int |
403 | sort_by_file_name (a, b) | |
404 | void *a; | |
405 | void *b; | |
5f8ac7e7 SC |
406 | { |
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); | |
411 | } | |
412 | ||
fe6e2957 DE |
413 | static int |
414 | sort_by_section_name (a, b) | |
415 | void *a; | |
416 | void *b; | |
417 | { | |
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); | |
422 | } | |
423 | ||
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. */ | |
427 | ||
428 | static lang_statement_union_type ** | |
429 | sort_sections_1 (startptr, next_after, count, sort_func) | |
430 | lang_statement_union_type **startptr,*next_after; | |
431 | int count; | |
432 | int (*sort_func) (); | |
433 | { | |
434 | lang_statement_union_type **vec; | |
435 | lang_statement_union_type *p; | |
436 | int i; | |
437 | ||
438 | if (count == 0) | |
439 | return startptr; | |
440 | ||
441 | vec = (lang_statement_union_type **) | |
442 | alloca (count * sizeof (lang_statement_union_type *)); | |
443 | ||
444 | for (p = *startptr, i = 0; i < count; i++, p = p->next) | |
445 | vec[i] = p; | |
446 | ||
447 | qsort (vec, count, sizeof (vec[0]), sort_func); | |
448 | ||
449 | /* Fill in the next pointers again. */ | |
450 | *startptr = vec[0]; | |
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); | |
455 | } | |
456 | ||
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 | |
461 | .idata section. | |
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]. | |
467 | ||
468 | We also sort sections in '\$' wild statements. These are created by the | |
469 | place_orphans routine to implement grouped sections. */ | |
a1613b5f | 470 | |
5f8ac7e7 SC |
471 | static void |
472 | sort_sections (s) | |
473 | lang_statement_union_type *s; | |
474 | { | |
475 | for (; s ; s = s->next) | |
476 | switch (s->header.type) | |
477 | { | |
478 | case lang_output_section_statement_enum: | |
479 | sort_sections (s->output_section_statement.children.head); | |
480 | break; | |
481 | case lang_wild_statement_enum: | |
482 | { | |
483 | lang_statement_union_type **p = &s->wild_statement.children.head; | |
484 | ||
fe6e2957 | 485 | /* Is this the .idata section? */ |
417fe276 GN |
486 | if (s->wild_statement.section_name != NULL |
487 | && strncmp (s->wild_statement.section_name, ".idata", 6) == 0) | |
5f8ac7e7 | 488 | { |
fe6e2957 DE |
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 | |
494 | pointer chain. */ | |
495 | ||
496 | while (*p) | |
5f8ac7e7 | 497 | { |
fe6e2957 DE |
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); | |
502 | else | |
503 | { | |
504 | lang_statement_union_type *end; | |
505 | int count; | |
506 | ||
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); | |
511 | end = end->next) | |
512 | count++; | |
513 | ||
514 | p = sort_sections_1 (p, end, count, sort_by_file_name); | |
515 | } | |
5f8ac7e7 | 516 | } |
fe6e2957 | 517 | break; |
5f8ac7e7 | 518 | } |
fe6e2957 DE |
519 | |
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). */ | |
417fe276 GN |
525 | if (s->wild_statement.section_name != NULL) |
526 | { | |
527 | char *q = strchr (s->wild_statement.section_name, '\$'); | |
528 | ||
529 | if (q && q[1] == 0) | |
530 | { | |
531 | lang_statement_union_type *end; | |
532 | int count; | |
533 | ||
534 | for (end = *p, count = 0; end; end = end->next) | |
535 | { | |
536 | if (end->header.type != lang_input_section_enum) | |
537 | abort (); | |
538 | count++; | |
539 | } | |
540 | (void) sort_sections_1 (p, end, count, sort_by_section_name); | |
541 | } | |
542 | break; | |
543 | } | |
5f8ac7e7 SC |
544 | } |
545 | break; | |
546 | default: | |
547 | break; | |
548 | } | |
549 | } | |
550 | ||
551 | static void | |
552 | gld_${EMULATION_NAME}_before_allocation() | |
553 | { | |
554 | extern lang_statement_list_type *stat_ptr; | |
d4279937 SC |
555 | |
556 | #ifdef TARGET_IS_ppcpe | |
557 | /* Here we rummage through the found bfds to collect toc information */ | |
558 | { | |
559 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
560 | { | |
417fe276 GN |
561 | if (!ppc_process_before_allocation(is->the_bfd, &link_info)) |
562 | { | |
563 | einfo("Errors encountered processing file %s", is->filename); | |
564 | } | |
d4279937 SC |
565 | } |
566 | } | |
567 | ||
568 | /* We have seen it all. Allocate it, and carry on */ | |
569 | ppc_allocate_toc_section (&link_info); | |
570 | #endif | |
571 | ||
fe6e2957 | 572 | sort_sections (stat_ptr->head); |
5f8ac7e7 | 573 | } |
a1613b5f | 574 | \f |
fe6e2957 DE |
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 | |
578 | (e.g. .text). | |
579 | See the Microsoft Portable Executable and Common Object File Format | |
580 | Specification 4.1, section 4.2, Grouped Sections. */ | |
a1613b5f DE |
581 | |
582 | /*ARGSUSED*/ | |
583 | static boolean | |
584 | gld${EMULATION_NAME}_place_orphan (file, s) | |
585 | lang_input_statement_type *file; | |
586 | asection *s; | |
587 | { | |
fe6e2957 DE |
588 | const char *secname; |
589 | char *output_secname, *ps; | |
a1613b5f | 590 | lang_output_section_statement_type *os; |
fe6e2957 DE |
591 | lang_statement_list_type *ptr; |
592 | lang_statement_union_type *l; | |
a1613b5f DE |
593 | |
594 | if ((s->flags & SEC_ALLOC) == 0) | |
595 | return false; | |
596 | ||
fe6e2957 DE |
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) | |
602 | return false; | |
a1613b5f DE |
603 | |
604 | secname = bfd_get_section_name (s->owner, s); | |
605 | ||
fe6e2957 DE |
606 | /* Everything from the '\$' on gets deleted so don't allow '\$' as the |
607 | first character. */ | |
608 | if (*secname == '\$') | |
609 | einfo ("%P%F: section %s has '\$' as first character\n", secname); | |
610 | if (strchr (secname + 1, '\$') == NULL) | |
a1613b5f DE |
611 | return false; |
612 | ||
fe6e2957 DE |
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, '\$'); | |
618 | *ps = 0; | |
619 | os = lang_output_section_statement_lookup (output_secname); | |
620 | ||
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. */ | |
625 | ||
626 | ps[0] = '\$'; | |
627 | ps[1] = 0; | |
628 | for (l = os->children.head; l; l = l->next) | |
a1613b5f | 629 | { |
fe6e2957 DE |
630 | if (l->header.type == lang_wild_statement_enum |
631 | && strcmp (l->wild_statement.section_name, output_secname) == 0) | |
632 | break; | |
a1613b5f | 633 | } |
fe6e2957 DE |
634 | ps[0] = 0; |
635 | if (l == NULL) | |
636 | #if 1 | |
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 | |
640 | script. */ | |
a1613b5f | 641 | { |
fe6e2957 DE |
642 | lang_wild_statement_type *new = new_stat (lang_wild_statement, |
643 | &os->children); | |
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); | |
648 | l = new; | |
a1613b5f DE |
649 | } |
650 | #endif | |
651 | ||
fe6e2957 DE |
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 | |
655 | sort_sections. */ | |
656 | wild_doit (&l->wild_statement.children, s, os, file); | |
a1613b5f DE |
657 | |
658 | return true; | |
659 | } | |
a1613b5f | 660 | \f |
5f8ac7e7 SC |
661 | static char * |
662 | gld_${EMULATION_NAME}_get_script(isfile) | |
663 | int *isfile; | |
664 | EOF | |
665 | # Scripts compiled in. | |
666 | # sed commands to quote an ld script as a C string. | |
667 | sc="-f ${srcdir}/emultempl/stringify.sed" | |
668 | ||
669 | cat >>e${EMULATION_NAME}.c <<EOF | |
670 | { | |
671 | *isfile = 0; | |
672 | ||
673 | if (link_info.relocateable == true && config.build_constructors == true) | |
674 | return | |
675 | EOF | |
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 | |
686 | ||
687 | cat >>e${EMULATION_NAME}.c <<EOF | |
688 | ||
689 | ||
5f8ac7e7 SC |
690 | struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = |
691 | { | |
692 | gld_${EMULATION_NAME}_before_parse, | |
693 | syslib_default, | |
694 | hll_default, | |
695 | after_parse_default, | |
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, | |
702 | "${EMULATION_NAME}", | |
703 | "${OUTPUT_FORMAT}", | |
704 | NULL, /* finish */ | |
705 | NULL, /* create output section statements */ | |
706 | NULL, /* open dynamic archive */ | |
a1613b5f | 707 | gld${EMULATION_NAME}_place_orphan, |
5f8ac7e7 SC |
708 | gld_${EMULATION_NAME}_set_symbols, |
709 | gld_${EMULATION_NAME}_parse_args | |
710 | }; | |
711 | EOF |