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