Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | # This shell script emits a C file. -*- C -*- |
2 | # It does some substitutions. | |
9b5799b8 | 3 | if [ -z "$MACHINE" ]; then |
86af25fe L |
4 | OUTPUT_ARCH=${ARCH} |
5 | else | |
6 | OUTPUT_ARCH=${ARCH}:${MACHINE} | |
7 | fi | |
252b5132 RH |
8 | cat >e${EMULATION_NAME}.c <<EOF |
9 | /* This file is part of GLD, the Gnu Linker. | |
2c382fb6 | 10 | Copyright 1995, 1996, 1997, 1998, 2000, 2001, 2002 |
39dcfe18 | 11 | Free Software Foundation, Inc. |
252b5132 RH |
12 | |
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. | |
17 | ||
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. | |
22 | ||
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
26 | ||
27 | /* For WINDOWS_NT */ | |
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 | |
9b5799b8 AJ |
32 | the correct entry point by default. */ |
33 | ||
252b5132 RH |
34 | #include "bfd.h" |
35 | #include "sysdep.h" | |
36 | #include "bfdlink.h" | |
37 | #include "getopt.h" | |
38 | #include "libiberty.h" | |
39 | #include "ld.h" | |
40 | #include "ldmain.h" | |
252b5132 RH |
41 | #include "ldexp.h" |
42 | #include "ldlang.h" | |
b71e2778 | 43 | #include "ldfile.h" |
252b5132 | 44 | #include "ldemul.h" |
2c382fb6 | 45 | #include "ldgram.h" |
252b5132 RH |
46 | #include "ldlex.h" |
47 | #include "ldmisc.h" | |
48 | #include "ldctor.h" | |
252b5132 RH |
49 | #include "coff/internal.h" |
50 | #include "../bfd/libcoff.h" | |
51 | ||
52 | #define TARGET_IS_${EMULATION_NAME} | |
53 | ||
54 | static void gld_${EMULATION_NAME}_set_symbols PARAMS ((void)); | |
55 | static void gld_${EMULATION_NAME}_after_open PARAMS ((void)); | |
56 | static void gld_${EMULATION_NAME}_before_parse PARAMS ((void)); | |
57 | static void gld_${EMULATION_NAME}_before_allocation PARAMS ((void)); | |
58 | static boolean gld${EMULATION_NAME}_place_orphan | |
59 | PARAMS ((lang_input_statement_type *, asection *)); | |
60 | static char *gld_${EMULATION_NAME}_get_script PARAMS ((int *)); | |
61 | static int gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **)); | |
62 | ||
9b5799b8 AJ |
63 | static int sort_by_file_name PARAMS ((const PTR, const PTR)); |
64 | static int sort_by_section_name PARAMS ((const PTR, const PTR)); | |
252b5132 RH |
65 | static lang_statement_union_type **sort_sections_1 |
66 | PARAMS ((lang_statement_union_type **, lang_statement_union_type *, int, | |
9b5799b8 | 67 | int (*) PARAMS((const PTR, const PTR)))); |
252b5132 RH |
68 | static void sort_sections PARAMS ((lang_statement_union_type *)); |
69 | ||
48f6162b AJ |
70 | static void set_pe_name PARAMS ((char *, long int)); |
71 | static void set_pe_subsystem PARAMS ((void)); | |
72 | static void set_pe_value PARAMS ((char *)); | |
73 | static void set_pe_stack_heap PARAMS ((char *, char *)); | |
74 | ||
252b5132 RH |
75 | static struct internal_extra_pe_aouthdr pe; |
76 | static int dll; | |
77 | ||
78 | extern const char *output_filename; | |
79 | ||
80 | static void | |
81 | gld_${EMULATION_NAME}_before_parse() | |
82 | { | |
86af25fe L |
83 | const bfd_arch_info_type *arch = bfd_scan_arch ("${OUTPUT_ARCH}"); |
84 | if (arch) | |
85 | { | |
86 | ldfile_output_architecture = arch->arch; | |
87 | ldfile_output_machine = arch->mach; | |
88 | ldfile_output_machine_name = arch->printable_name; | |
89 | } | |
90 | else | |
91 | ldfile_output_architecture = bfd_arch_${ARCH}; | |
db8d4f23 | 92 | output_filename = "a.exe"; |
252b5132 RH |
93 | } |
94 | \f | |
95 | /* PE format extra command line options. */ | |
96 | ||
97 | /* Used for setting flags in the PE header. */ | |
98 | #define OPTION_BASE_FILE (300 + 1) | |
99 | #define OPTION_DLL (OPTION_BASE_FILE + 1) | |
100 | #define OPTION_FILE_ALIGNMENT (OPTION_DLL + 1) | |
101 | #define OPTION_IMAGE_BASE (OPTION_FILE_ALIGNMENT + 1) | |
102 | #define OPTION_MAJOR_IMAGE_VERSION (OPTION_IMAGE_BASE + 1) | |
103 | #define OPTION_MAJOR_OS_VERSION (OPTION_MAJOR_IMAGE_VERSION + 1) | |
104 | #define OPTION_MAJOR_SUBSYSTEM_VERSION (OPTION_MAJOR_OS_VERSION + 1) | |
105 | #define OPTION_MINOR_IMAGE_VERSION (OPTION_MAJOR_SUBSYSTEM_VERSION + 1) | |
106 | #define OPTION_MINOR_OS_VERSION (OPTION_MINOR_IMAGE_VERSION + 1) | |
107 | #define OPTION_MINOR_SUBSYSTEM_VERSION (OPTION_MINOR_OS_VERSION + 1) | |
108 | #define OPTION_SECTION_ALIGNMENT (OPTION_MINOR_SUBSYSTEM_VERSION + 1) | |
109 | #define OPTION_STACK (OPTION_SECTION_ALIGNMENT + 1) | |
110 | #define OPTION_SUBSYSTEM (OPTION_STACK + 1) | |
111 | #define OPTION_HEAP (OPTION_SUBSYSTEM + 1) | |
112 | ||
113 | static struct option longopts[] = { | |
114 | /* PE options */ | |
115 | {"base-file", required_argument, NULL, OPTION_BASE_FILE}, | |
116 | {"dll", no_argument, NULL, OPTION_DLL}, | |
117 | {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT}, | |
9b5799b8 AJ |
118 | {"heap", required_argument, NULL, OPTION_HEAP}, |
119 | {"image-base", required_argument, NULL, OPTION_IMAGE_BASE}, | |
252b5132 RH |
120 | {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION}, |
121 | {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION}, | |
122 | {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION}, | |
123 | {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION}, | |
124 | {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION}, | |
125 | {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION}, | |
126 | {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT}, | |
127 | {"stack", required_argument, NULL, OPTION_STACK}, | |
128 | {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM}, | |
129 | {NULL, no_argument, NULL, 0} | |
130 | }; | |
131 | ||
132 | ||
133 | /* PE/WIN32; added routines to get the subsystem type, heap and/or stack | |
134 | parameters which may be input from the command line */ | |
135 | ||
136 | typedef struct { | |
137 | void *ptr; | |
138 | int size; | |
139 | int value; | |
140 | char *symbol; | |
141 | int inited; | |
142 | } definfo; | |
143 | ||
144 | #define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol,0} | |
145 | ||
146 | static definfo init[] = | |
147 | { | |
148 | /* imagebase must be first */ | |
149 | #define IMAGEBASEOFF 0 | |
150 | D(ImageBase,"__image_base__", BEOS_EXE_IMAGE_BASE), | |
151 | #define DLLOFF 1 | |
ff7e6f88 | 152 | {&dll, sizeof(dll), 0, "__dll__", 0}, |
252b5132 RH |
153 | D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT), |
154 | D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT), | |
155 | D(MajorOperatingSystemVersion,"__major_os_version__", 4), | |
156 | D(MinorOperatingSystemVersion,"__minor_os_version__", 0), | |
157 | D(MajorImageVersion,"__major_image_version__", 1), | |
158 | D(MinorImageVersion,"__minor_image_version__", 0), | |
159 | D(MajorSubsystemVersion,"__major_subsystem_version__", 4), | |
160 | D(MinorSubsystemVersion,"__minor_subsystem_version__", 0), | |
161 | D(Subsystem,"__subsystem__", 3), | |
162 | D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x2000000), | |
163 | D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000), | |
164 | D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000), | |
165 | D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000), | |
166 | D(LoaderFlags,"__loader_flags__", 0x0), | |
167 | { NULL, 0, 0, NULL, 0 } | |
168 | }; | |
169 | ||
170 | ||
171 | static void | |
172 | set_pe_name (name, val) | |
173 | char *name; | |
174 | long val; | |
175 | { | |
176 | int i; | |
177 | /* Find the name and set it. */ | |
178 | for (i = 0; init[i].ptr; i++) | |
179 | { | |
180 | if (strcmp (name, init[i].symbol) == 0) | |
181 | { | |
182 | init[i].value = val; | |
183 | init[i].inited = 1; | |
184 | return; | |
185 | } | |
186 | } | |
187 | abort(); | |
188 | } | |
189 | ||
190 | ||
191 | static void | |
192 | set_pe_subsystem () | |
193 | { | |
194 | const char *sver; | |
195 | int len; | |
196 | int i; | |
9b5799b8 | 197 | static const struct |
252b5132 RH |
198 | { |
199 | const char *name; | |
200 | const int value; | |
201 | const char *entry; | |
202 | } | |
203 | v[] = | |
204 | { | |
205 | { "native", 1, "_NtProcessStartup" }, | |
206 | { "windows", 2, "_WinMainCRTStartup" }, | |
207 | { "wwindows", 2, "_wWinMainCRTStartup" }, | |
208 | { "console", 3, "_mainCRTStartup" }, | |
209 | { "wconsole", 3, "_wmainCRTStartup" }, | |
210 | #if 0 | |
211 | /* The Microsoft linker does not recognize this. */ | |
212 | { "os2", 5, "" }, | |
213 | #endif | |
214 | { "posix", 7, "___PosixProcessStartup"}, | |
215 | { 0, 0, 0 } | |
216 | }; | |
217 | ||
218 | sver = strchr (optarg, ':'); | |
219 | if (sver == NULL) | |
220 | len = strlen (optarg); | |
221 | else | |
222 | { | |
223 | char *end; | |
224 | ||
225 | len = sver - optarg; | |
226 | set_pe_name ("__major_subsystem_version__", | |
227 | strtoul (sver + 1, &end, 0)); | |
228 | if (*end == '.') | |
229 | set_pe_name ("__minor_subsystem_version__", | |
230 | strtoul (end + 1, &end, 0)); | |
231 | if (*end != '\0') | |
232 | einfo ("%P: warning: bad version number in -subsystem option\n"); | |
233 | } | |
234 | ||
235 | for (i = 0; v[i].name; i++) | |
236 | { | |
237 | if (strncmp (optarg, v[i].name, len) == 0 | |
238 | && v[i].name[len] == '\0') | |
239 | { | |
240 | set_pe_name ("__subsystem__", v[i].value); | |
241 | ||
242 | /* If the subsystem is windows, we use a different entry | |
243 | point. We also register the entry point as an undefined | |
244 | symbol. from lang_add_entry() The reason we do | |
245 | this is so that the user | |
246 | doesn't have to because they would have to use the -u | |
247 | switch if they were specifying an entry point other than | |
248 | _mainCRTStartup. Specifically, if creating a windows | |
249 | application, entry point _WinMainCRTStartup must be | |
250 | specified. What I have found for non console | |
251 | applications (entry not _mainCRTStartup) is that the .obj | |
252 | that contains mainCRTStartup is brought in since it is | |
253 | the first encountered in libc.lib and it has other | |
254 | symbols in it which will be pulled in by the link | |
255 | process. To avoid this, adding -u with the entry point | |
256 | name specified forces the correct .obj to be used. We | |
257 | can avoid making the user do this by always adding the | |
258 | entry point name as an undefined symbol. */ | |
259 | lang_add_entry (v[i].entry, 1); | |
260 | ||
261 | return; | |
262 | } | |
263 | } | |
264 | einfo ("%P%F: invalid subsystem type %s\n", optarg); | |
265 | } | |
266 | ||
267 | ||
268 | ||
269 | static void | |
270 | set_pe_value (name) | |
271 | char *name; | |
9b5799b8 | 272 | |
252b5132 RH |
273 | { |
274 | char *end; | |
275 | set_pe_name (name, strtoul (optarg, &end, 0)); | |
276 | if (end == optarg) | |
277 | { | |
278 | einfo ("%P%F: invalid hex number for PE parameter '%s'\n", optarg); | |
279 | } | |
280 | ||
281 | optarg = end; | |
282 | } | |
283 | ||
284 | static void | |
285 | set_pe_stack_heap (resname, comname) | |
286 | char *resname; | |
287 | char *comname; | |
288 | { | |
289 | set_pe_value (resname); | |
290 | if (*optarg == ',') | |
291 | { | |
292 | optarg++; | |
293 | set_pe_value (comname); | |
294 | } | |
295 | else if (*optarg) | |
296 | { | |
297 | einfo ("%P%F: strange hex info for PE parameter '%s'\n", optarg); | |
298 | } | |
299 | } | |
300 | ||
301 | ||
302 | ||
303 | static int | |
304 | gld_${EMULATION_NAME}_parse_args(argc, argv) | |
305 | int argc; | |
306 | char **argv; | |
307 | { | |
308 | int longind; | |
309 | int optc; | |
310 | int prevoptind = optind; | |
311 | int prevopterr = opterr; | |
312 | int wanterror; | |
313 | static int lastoptind = -1; | |
314 | ||
315 | if (lastoptind != optind) | |
316 | opterr = 0; | |
317 | wanterror = opterr; | |
318 | ||
319 | lastoptind = optind; | |
320 | ||
321 | optc = getopt_long_only (argc, argv, "-", longopts, &longind); | |
322 | opterr = prevopterr; | |
323 | ||
324 | switch (optc) | |
325 | { | |
326 | default: | |
327 | if (wanterror) | |
328 | xexit (1); | |
329 | optind = prevoptind; | |
330 | return 0; | |
331 | ||
332 | case OPTION_BASE_FILE: | |
333 | link_info.base_file = (PTR) fopen (optarg, FOPEN_WB); | |
334 | if (link_info.base_file == NULL) | |
335 | { | |
336 | fprintf (stderr, "%s: Can't open base file %s\n", | |
337 | program_name, optarg); | |
338 | xexit (1); | |
339 | } | |
340 | break; | |
341 | ||
342 | /* PE options */ | |
9b5799b8 | 343 | case OPTION_HEAP: |
252b5132 RH |
344 | set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__"); |
345 | break; | |
9b5799b8 | 346 | case OPTION_STACK: |
252b5132 RH |
347 | set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__"); |
348 | break; | |
349 | case OPTION_SUBSYSTEM: | |
350 | set_pe_subsystem (); | |
351 | break; | |
352 | case OPTION_MAJOR_OS_VERSION: | |
353 | set_pe_value ("__major_os_version__"); | |
354 | break; | |
355 | case OPTION_MINOR_OS_VERSION: | |
356 | set_pe_value ("__minor_os_version__"); | |
357 | break; | |
358 | case OPTION_MAJOR_SUBSYSTEM_VERSION: | |
359 | set_pe_value ("__major_subsystem_version__"); | |
360 | break; | |
361 | case OPTION_MINOR_SUBSYSTEM_VERSION: | |
362 | set_pe_value ("__minor_subsystem_version__"); | |
363 | break; | |
364 | case OPTION_MAJOR_IMAGE_VERSION: | |
365 | set_pe_value ("__major_image_version__"); | |
366 | break; | |
367 | case OPTION_MINOR_IMAGE_VERSION: | |
368 | set_pe_value ("__minor_image_version__"); | |
369 | break; | |
370 | case OPTION_FILE_ALIGNMENT: | |
371 | set_pe_value ("__file_alignment__"); | |
372 | break; | |
373 | case OPTION_SECTION_ALIGNMENT: | |
374 | set_pe_value ("__section_alignment__"); | |
375 | break; | |
376 | case OPTION_DLL: | |
377 | set_pe_name ("__dll__", 1); | |
378 | break; | |
379 | case OPTION_IMAGE_BASE: | |
380 | set_pe_value ("__image_base__"); | |
381 | break; | |
382 | } | |
383 | return 1; | |
384 | } | |
385 | \f | |
386 | /* Assign values to the special symbols before the linker script is | |
387 | read. */ | |
388 | ||
389 | static void | |
390 | gld_${EMULATION_NAME}_set_symbols() | |
391 | { | |
392 | /* Run through and invent symbols for all the | |
393 | names and insert the defaults. */ | |
394 | int j; | |
395 | lang_statement_list_type *save; | |
396 | ||
397 | if (!init[IMAGEBASEOFF].inited) | |
398 | { | |
399 | if (link_info.relocateable) | |
400 | init[IMAGEBASEOFF].value = 0; | |
401 | else if (init[DLLOFF].value) | |
402 | init[IMAGEBASEOFF].value = BEOS_DLL_IMAGE_BASE; | |
403 | else | |
404 | init[IMAGEBASEOFF].value = BEOS_EXE_IMAGE_BASE; | |
405 | } | |
406 | ||
407 | /* Don't do any symbol assignments if this is a relocateable link. */ | |
408 | if (link_info.relocateable) | |
409 | return; | |
410 | ||
411 | /* Glue the assignments into the abs section */ | |
412 | save = stat_ptr; | |
413 | ||
414 | stat_ptr = &(abs_output_section->children); | |
415 | ||
416 | for (j = 0; init[j].ptr; j++) | |
417 | { | |
418 | long val = init[j].value; | |
2c382fb6 | 419 | lang_add_assignment (exp_assop ('=', init[j].symbol, exp_intop (val))); |
252b5132 RH |
420 | if (init[j].size == sizeof(short)) |
421 | *(short *)init[j].ptr = val; | |
422 | else if (init[j].size == sizeof(int)) | |
423 | *(int *)init[j].ptr = val; | |
424 | else if (init[j].size == sizeof(long)) | |
425 | *(long *)init[j].ptr = val; | |
426 | /* This might be a long long or other special type. */ | |
427 | else if (init[j].size == sizeof(bfd_vma)) | |
428 | *(bfd_vma *)init[j].ptr = val; | |
429 | else abort(); | |
430 | } | |
431 | /* Restore the pointer. */ | |
432 | stat_ptr = save; | |
9b5799b8 | 433 | |
252b5132 RH |
434 | if (pe.FileAlignment > |
435 | pe.SectionAlignment) | |
436 | { | |
437 | einfo ("%P: warning, file alignment > section alignment.\n"); | |
438 | } | |
439 | } | |
440 | ||
441 | static void | |
442 | gld_${EMULATION_NAME}_after_open() | |
443 | { | |
444 | /* Pass the wacky PE command line options into the output bfd. | |
445 | FIXME: This should be done via a function, rather than by | |
446 | including an internal BFD header. */ | |
447 | if (!coff_data(output_bfd)->pe) | |
448 | { | |
449 | einfo ("%F%P: PE operations on non PE file.\n"); | |
450 | } | |
451 | ||
452 | pe_data(output_bfd)->pe_opthdr = pe; | |
453 | pe_data(output_bfd)->dll = init[DLLOFF].value; | |
454 | ||
455 | } | |
456 | \f | |
457 | /* Callback functions for qsort in sort_sections. */ | |
458 | ||
459 | static int | |
460 | sort_by_file_name (a, b) | |
9b5799b8 AJ |
461 | const PTR a; |
462 | const PTR b; | |
252b5132 | 463 | { |
db09f25b AM |
464 | const lang_statement_union_type *const *ra = a; |
465 | const lang_statement_union_type *const *rb = b; | |
252b5132 RH |
466 | int i, a_sec, b_sec; |
467 | ||
468 | i = strcmp ((*ra)->input_section.ifile->the_bfd->my_archive->filename, | |
469 | (*rb)->input_section.ifile->the_bfd->my_archive->filename); | |
470 | if (i != 0) | |
471 | return i; | |
472 | ||
473 | i = strcmp ((*ra)->input_section.ifile->filename, | |
474 | (*rb)->input_section.ifile->filename); | |
475 | if (i != 0) | |
476 | return i; | |
477 | /* the tail idata4/5 are the only ones without relocs to an | |
9b5799b8 | 478 | idata$6 section unless we are importing by ordinal, |
252b5132 RH |
479 | so sort them to last to terminate the IAT |
480 | and HNT properly. if no reloc this one is import by ordinal | |
481 | so we have to sort by section contents */ | |
482 | ||
483 | if ( ((*ra)->input_section.section->reloc_count + (*rb)->input_section.section->reloc_count) ) | |
484 | { | |
9b5799b8 | 485 | i = (((*ra)->input_section.section->reloc_count > |
252b5132 RH |
486 | (*rb)->input_section.section->reloc_count) ? -1 : 0); |
487 | if ( i != 0) | |
488 | return i; | |
489 | ||
9b5799b8 | 490 | return (((*ra)->input_section.section->reloc_count > |
252b5132 RH |
491 | (*rb)->input_section.section->reloc_count) ? 0 : 1); |
492 | } | |
493 | else | |
494 | { | |
495 | if ( (strcmp( (*ra)->input_section.section->name, ".idata$6") == 0) ) | |
496 | return 0; /* don't sort .idata$6 or .idata$7 FIXME dlltool eliminate .idata$7 */ | |
497 | ||
9b5799b8 | 498 | if (! bfd_get_section_contents ((*ra)->input_section.ifile->the_bfd, |
252b5132 RH |
499 | (*ra)->input_section.section, &a_sec, (file_ptr) 0, (bfd_size_type)sizeof(a_sec))) |
500 | einfo ("%F%B: Can't read contents of section .idata: %E\n", | |
501 | (*ra)->input_section.ifile->the_bfd); | |
502 | ||
9b5799b8 | 503 | if (! bfd_get_section_contents ((*rb)->input_section.ifile->the_bfd, |
252b5132 RH |
504 | (*rb)->input_section.section, &b_sec, (file_ptr) 0, (bfd_size_type)sizeof(b_sec) )) |
505 | einfo ("%F%B: Can't read contents of section .idata: %E\n", | |
506 | (*rb)->input_section.ifile->the_bfd); | |
507 | ||
508 | i = ((a_sec < b_sec) ? -1 : 0); | |
509 | if ( i != 0) | |
510 | return i; | |
511 | return ((a_sec < b_sec) ? 0 : 1); | |
512 | } | |
513 | return 0; | |
514 | } | |
515 | ||
516 | static int | |
517 | sort_by_section_name (a, b) | |
9b5799b8 AJ |
518 | const PTR a; |
519 | const PTR b; | |
252b5132 | 520 | { |
db09f25b AM |
521 | const lang_statement_union_type *const *ra = a; |
522 | const lang_statement_union_type *const *rb = b; | |
252b5132 RH |
523 | int i; |
524 | i = strcmp ((*ra)->input_section.section->name, | |
525 | (*rb)->input_section.section->name); | |
526 | /* this is a hack to make .stab and .stabstr last, so we don't have | |
527 | to fix strip/objcopy for .reloc sections. | |
528 | FIXME stripping images with a .rsrc section still needs to be fixed */ | |
529 | if ( i != 0) | |
530 | { | |
531 | if ((strncmp ((*ra)->input_section.section->name, ".stab", 5) == 0) | |
532 | && (strncmp ((*rb)->input_section.section->name, ".stab", 5) != 0)) | |
533 | return 1; | |
534 | return i; | |
535 | } | |
536 | return i; | |
537 | } | |
538 | ||
539 | /* Subroutine of sort_sections to a contiguous subset of a list of sections. | |
540 | NEXT_AFTER is the element after the last one to sort. | |
541 | The result is a pointer to the last element's "next" pointer. */ | |
542 | ||
543 | static lang_statement_union_type ** | |
544 | sort_sections_1 (startptr, next_after, count, sort_func) | |
545 | lang_statement_union_type **startptr,*next_after; | |
546 | int count; | |
9b5799b8 | 547 | int (*sort_func) PARAMS ((const PTR, const PTR)); |
252b5132 RH |
548 | { |
549 | lang_statement_union_type **vec; | |
550 | lang_statement_union_type *p; | |
551 | int i; | |
552 | lang_statement_union_type **ret; | |
553 | ||
554 | if (count == 0) | |
555 | return startptr; | |
556 | ||
557 | vec = ((lang_statement_union_type **) | |
558 | xmalloc (count * sizeof (lang_statement_union_type *))); | |
559 | ||
bba1a0c0 | 560 | for (p = *startptr, i = 0; i < count; i++, p = p->header.next) |
252b5132 RH |
561 | vec[i] = p; |
562 | ||
563 | qsort (vec, count, sizeof (vec[0]), sort_func); | |
564 | ||
565 | /* Fill in the next pointers again. */ | |
566 | *startptr = vec[0]; | |
567 | for (i = 0; i < count - 1; i++) | |
568 | vec[i]->header.next = vec[i + 1]; | |
569 | vec[i]->header.next = next_after; | |
570 | ret = &vec[i]->header.next; | |
571 | free (vec); | |
572 | return ret; | |
573 | } | |
574 | ||
575 | /* Sort the .idata\$foo input sections of archives into filename order. | |
576 | The reason is so dlltool can arrange to have the pe dll import information | |
577 | generated correctly - the head of the list goes into dh.o, the tail into | |
578 | dt.o, and the guts into ds[nnnn].o. Note that this is only needed for the | |
579 | .idata section. | |
580 | FIXME: This may no longer be necessary with grouped sections. Instead of | |
581 | sorting on dh.o, ds[nnnn].o, dt.o, one could, for example, have dh.o use | |
582 | .idata\$4h, have ds[nnnn].o use .idata\$4s[nnnn], and have dt.o use .idata\$4t. | |
583 | This would have to be elaborated upon to handle multiple dll's | |
584 | [assuming such an eloboration is possible of course]. | |
585 | ||
586 | We also sort sections in '\$' wild statements. These are created by the | |
587 | place_orphans routine to implement grouped sections. */ | |
588 | ||
589 | static void | |
590 | sort_sections (s) | |
591 | lang_statement_union_type *s; | |
592 | { | |
bba1a0c0 | 593 | for (; s ; s = s->header.next) |
252b5132 RH |
594 | switch (s->header.type) |
595 | { | |
596 | case lang_output_section_statement_enum: | |
597 | sort_sections (s->output_section_statement.children.head); | |
598 | break; | |
599 | case lang_wild_statement_enum: | |
600 | { | |
601 | lang_statement_union_type **p = &s->wild_statement.children.head; | |
3fd36710 | 602 | struct wildcard_list *sec; |
252b5132 | 603 | |
3fd36710 | 604 | for (sec = s->wild_statement.section_list; sec; sec = sec->next) |
252b5132 | 605 | { |
3fd36710 AM |
606 | /* Is this the .idata section? */ |
607 | if (sec->spec.name != NULL | |
608 | && strncmp (sec->spec.name, ".idata", 6) == 0) | |
252b5132 | 609 | { |
3fd36710 AM |
610 | /* Sort the children. We want to sort any objects in |
611 | the same archive. In order to handle the case of | |
612 | including a single archive multiple times, we sort | |
613 | all the children by archive name and then by object | |
614 | name. After sorting them, we re-thread the pointer | |
615 | chain. */ | |
616 | ||
617 | while (*p) | |
252b5132 | 618 | { |
3fd36710 AM |
619 | lang_statement_union_type *start = *p; |
620 | if (start->header.type != lang_input_section_enum | |
621 | || !start->input_section.ifile->the_bfd->my_archive) | |
622 | p = &(start->header.next); | |
623 | else | |
624 | { | |
625 | lang_statement_union_type *end; | |
626 | int count; | |
627 | ||
628 | for (end = start, count = 0; | |
629 | end && (end->header.type | |
630 | == lang_input_section_enum); | |
bba1a0c0 | 631 | end = end->header.next) |
3fd36710 AM |
632 | count++; |
633 | ||
634 | p = sort_sections_1 (p, end, count, | |
635 | sort_by_file_name); | |
636 | } | |
252b5132 | 637 | } |
3fd36710 | 638 | break; |
252b5132 | 639 | } |
252b5132 | 640 | |
3fd36710 AM |
641 | /* If this is a collection of grouped sections, sort them. |
642 | The linker script must explicitly mention "*(.foo\$)" or | |
643 | "*(.foo\$*)". Don't sort them if \$ is not the last | |
644 | character (not sure if this is really useful, but it | |
645 | allows explicitly mentioning some \$ sections and letting | |
646 | the linker handle the rest). */ | |
647 | if (sec->spec.name != NULL) | |
252b5132 | 648 | { |
3fd36710 | 649 | char *q = strchr (sec->spec.name, '\$'); |
252b5132 | 650 | |
3fd36710 AM |
651 | if (q != NULL |
652 | && (q[1] == '\0' | |
653 | || (q[1] == '*' && q[2] == '\0'))) | |
252b5132 | 654 | { |
3fd36710 AM |
655 | lang_statement_union_type *end; |
656 | int count; | |
657 | ||
bba1a0c0 | 658 | for (end = *p, count = 0; end; end = end->header.next) |
3fd36710 AM |
659 | { |
660 | if (end->header.type != lang_input_section_enum) | |
661 | abort (); | |
662 | count++; | |
663 | } | |
664 | (void) sort_sections_1 (p, end, count, | |
665 | sort_by_section_name); | |
252b5132 | 666 | } |
3fd36710 | 667 | break; |
252b5132 | 668 | } |
252b5132 RH |
669 | } |
670 | } | |
671 | break; | |
672 | default: | |
673 | break; | |
674 | } | |
675 | } | |
676 | ||
9b5799b8 | 677 | static void |
252b5132 RH |
678 | gld_${EMULATION_NAME}_before_allocation() |
679 | { | |
680 | extern lang_statement_list_type *stat_ptr; | |
681 | ||
682 | #ifdef TARGET_IS_ppcpe | |
683 | /* Here we rummage through the found bfds to collect toc information */ | |
684 | { | |
685 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
686 | { | |
687 | if (!ppc_process_before_allocation(is->the_bfd, &link_info)) | |
688 | { | |
689 | einfo("Errors encountered processing file %s\n", is->filename); | |
690 | } | |
691 | } | |
692 | } | |
693 | ||
694 | /* We have seen it all. Allocate it, and carry on */ | |
695 | ppc_allocate_toc_section (&link_info); | |
696 | #else | |
697 | #ifdef TARGET_IS_armpe | |
698 | /* FIXME: we should be able to set the size of the interworking stub | |
699 | section. | |
700 | ||
701 | Here we rummage through the found bfds to collect glue | |
702 | information. FIXME: should this be based on a command line | |
703 | option? krk@cygnus.com */ | |
704 | { | |
705 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
706 | { | |
707 | if (!arm_process_before_allocation (is->the_bfd, & link_info)) | |
708 | { | |
709 | einfo ("Errors encountered processing file %s", is->filename); | |
710 | } | |
711 | } | |
712 | } | |
713 | ||
714 | /* We have seen it all. Allocate it, and carry on */ | |
715 | arm_allocate_interworking_sections (& link_info); | |
716 | #endif /* TARGET_IS_armpe */ | |
717 | #endif /* TARGET_IS_ppcpe */ | |
718 | ||
719 | sort_sections (stat_ptr->head); | |
720 | } | |
721 | \f | |
722 | /* Place an orphan section. We use this to put sections with a '\$' in them | |
723 | into the right place. Any section with a '\$' in them (e.g. .text\$foo) | |
724 | gets mapped to the output section with everything from the '\$' on stripped | |
725 | (e.g. .text). | |
726 | See the Microsoft Portable Executable and Common Object File Format | |
727 | Specification 4.1, section 4.2, Grouped Sections. | |
728 | ||
729 | FIXME: This is now handled by the linker script using wildcards, | |
730 | but I'm leaving this here in case we want to enable it for sections | |
731 | which are not mentioned in the linker script. */ | |
732 | ||
733 | /*ARGSUSED*/ | |
734 | static boolean | |
735 | gld${EMULATION_NAME}_place_orphan (file, s) | |
736 | lang_input_statement_type *file; | |
737 | asection *s; | |
738 | { | |
739 | const char *secname; | |
740 | char *output_secname, *ps; | |
741 | lang_output_section_statement_type *os; | |
742 | lang_statement_union_type *l; | |
743 | ||
744 | if ((s->flags & SEC_ALLOC) == 0) | |
745 | return false; | |
746 | ||
747 | /* Don't process grouped sections unless doing a final link. | |
748 | If they're marked as COMDAT sections, we don't want .text\$foo to | |
749 | end up in .text and then have .text disappear because it's marked | |
750 | link-once-discard. */ | |
751 | if (link_info.relocateable) | |
752 | return false; | |
753 | ||
754 | secname = bfd_get_section_name (s->owner, s); | |
755 | ||
756 | /* Everything from the '\$' on gets deleted so don't allow '\$' as the | |
757 | first character. */ | |
758 | if (*secname == '\$') | |
759 | einfo ("%P%F: section %s has '\$' as first character\n", secname); | |
760 | if (strchr (secname + 1, '\$') == NULL) | |
761 | return false; | |
762 | ||
763 | /* Look up the output section. The Microsoft specs say sections names in | |
764 | image files never contain a '\$'. Fortunately, lang_..._lookup creates | |
765 | the section if it doesn't exist. */ | |
c7e40348 | 766 | output_secname = xstrdup (secname); |
252b5132 RH |
767 | ps = strchr (output_secname + 1, '\$'); |
768 | *ps = 0; | |
769 | os = lang_output_section_statement_lookup (output_secname); | |
770 | ||
771 | /* Find the '\$' wild statement for this section. We currently require the | |
772 | linker script to explicitly mention "*(.foo\$)". | |
773 | FIXME: ppcpe.sc has .CRT\$foo in the .rdata section. According to the | |
774 | Microsoft docs this isn't correct so it's not (currently) handled. */ | |
775 | ||
776 | ps[0] = '\$'; | |
777 | ps[1] = 0; | |
bba1a0c0 | 778 | for (l = os->children.head; l; l = l->header.next) |
3fd36710 AM |
779 | if (l->header.type == lang_wild_statement_enum) |
780 | { | |
781 | struct wildcard_list *sec; | |
782 | ||
783 | for (sec = l->wild_statement.section_list; sec; sec = sec->next) | |
784 | if (sec->spec.name && strcmp (sec->spec.name, output_secname) == 0) | |
785 | break; | |
786 | if (sec) | |
787 | break; | |
788 | } | |
252b5132 RH |
789 | ps[0] = 0; |
790 | if (l == NULL) | |
791 | #if 1 | |
792 | einfo ("%P%F: *(%s\$) missing from linker script\n", output_secname); | |
793 | #else /* FIXME: This block is untried. It exists to convey the intent, | |
794 | should one decide to not require *(.foo\$) to appear in the linker | |
795 | script. */ | |
796 | { | |
3fd36710 AM |
797 | lang_wild_statement_type *new; |
798 | struct wildcard_list *tmp; | |
799 | ||
800 | tmp = (struct wildcard_list *) xmalloc (sizeof *tmp); | |
801 | tmp->next = NULL; | |
802 | tmp->spec.name = xmalloc (strlen (output_secname) + 2); | |
803 | sprintf (tmp->spec.name, "%s\$", output_secname); | |
804 | tmp->spec.exclude_name_list = NULL; | |
805 | tmp->sorted = false; | |
806 | new = new_stat (lang_wild_statement, &os->children); | |
252b5132 | 807 | new->filename = NULL; |
3fd36710 AM |
808 | new->filenames_sorted = false; |
809 | new->section_list = tmp; | |
810 | new->keep_sections = false; | |
252b5132 RH |
811 | lang_list_init (&new->children); |
812 | l = new; | |
813 | } | |
814 | #endif | |
815 | ||
816 | /* Link the input section in and we're done for now. | |
817 | The sections still have to be sorted, but that has to wait until | |
818 | all such sections have been processed by us. The sorting is done by | |
819 | sort_sections. */ | |
39dcfe18 | 820 | lang_add_section (&l->wild_statement.children, s, os, file); |
252b5132 RH |
821 | |
822 | return true; | |
823 | } | |
824 | \f | |
825 | static char * | |
826 | gld_${EMULATION_NAME}_get_script(isfile) | |
827 | int *isfile; | |
828 | EOF | |
829 | # Scripts compiled in. | |
830 | # sed commands to quote an ld script as a C string. | |
597e2591 | 831 | sc="-f stringify.sed" |
252b5132 RH |
832 | |
833 | cat >>e${EMULATION_NAME}.c <<EOF | |
9b5799b8 | 834 | { |
252b5132 RH |
835 | *isfile = 0; |
836 | ||
837 | if (link_info.relocateable == true && config.build_constructors == true) | |
838 | return | |
839 | EOF | |
840 | sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c | |
841 | echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c | |
842 | sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c | |
843 | echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c | |
844 | sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c | |
845 | echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c | |
846 | sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c | |
847 | echo ' ; else return' >> e${EMULATION_NAME}.c | |
848 | sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c | |
849 | echo '; }' >> e${EMULATION_NAME}.c | |
850 | ||
851 | cat >>e${EMULATION_NAME}.c <<EOF | |
852 | ||
853 | ||
9b5799b8 | 854 | struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = |
252b5132 RH |
855 | { |
856 | gld_${EMULATION_NAME}_before_parse, | |
857 | syslib_default, | |
858 | hll_default, | |
859 | after_parse_default, | |
860 | gld_${EMULATION_NAME}_after_open, | |
861 | after_allocation_default, | |
862 | set_output_arch_default, | |
863 | ldemul_default_target, | |
864 | gld_${EMULATION_NAME}_before_allocation, | |
865 | gld_${EMULATION_NAME}_get_script, | |
866 | "${EMULATION_NAME}", | |
867 | "${OUTPUT_FORMAT}", | |
868 | NULL, /* finish */ | |
869 | NULL, /* create output section statements */ | |
870 | NULL, /* open dynamic archive */ | |
871 | gld${EMULATION_NAME}_place_orphan, | |
872 | gld_${EMULATION_NAME}_set_symbols, | |
e1c47aa4 AM |
873 | gld_${EMULATION_NAME}_parse_args, |
874 | NULL, /* unrecognized file */ | |
875 | NULL, /* list options */ | |
40d109bf | 876 | NULL, /* recognized file */ |
9b5799b8 | 877 | NULL /* find_potential_libraries */ |
252b5132 RH |
878 | }; |
879 | EOF |