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