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