Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | # This shell script emits a C file. -*- C -*- |
2 | # It does some substitutions. | |
361d94e7 ILT |
3 | rm -f e${EMULATION_NAME}.c |
4 | (echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-) | |
c6c37250 | 5 | cat >>e${EMULATION_NAME}.c <<EOF |
252b5132 | 6 | /* This file is part of GLD, the Gnu Linker. |
2a275620 | 7 | Copyright 1995, 96, 97, 98, 1999 Free Software Foundation, Inc. |
252b5132 RH |
8 | |
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
22 | ||
23 | /* For WINDOWS_NT */ | |
24 | /* The original file generated returned different default scripts depending | |
25 | on whether certain switches were set, but these switches pertain to the | |
26 | Linux system and that particular version of coff. In the NT case, we | |
27 | only determine if the subsystem is console or windows in order to select | |
28 | the correct entry point by default. */ | |
29 | ||
30 | #include "bfd.h" | |
31 | #include "sysdep.h" | |
32 | #include "bfdlink.h" | |
33 | #include "getopt.h" | |
34 | #include "libiberty.h" | |
35 | #include "ld.h" | |
36 | #include "ldmain.h" | |
37 | #include "ldgram.h" | |
38 | #include "ldexp.h" | |
39 | #include "ldlang.h" | |
40 | #include "ldemul.h" | |
41 | #include "ldlex.h" | |
42 | #include "ldmisc.h" | |
43 | #include "ldctor.h" | |
44 | #include "ldfile.h" | |
45 | #include "coff/internal.h" | |
71add731 ILT |
46 | |
47 | /* FIXME: This is a BFD internal header file, and we should not be | |
48 | using it here. */ | |
252b5132 | 49 | #include "../bfd/libcoff.h" |
71add731 | 50 | |
252b5132 | 51 | #include "deffile.h" |
1069dd8d | 52 | #include "pe-dll.h" |
252b5132 RH |
53 | |
54 | #define TARGET_IS_${EMULATION_NAME} | |
55 | ||
2be9b2c7 ILT |
56 | /* Permit the emulation parameters to override the default section |
57 | alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes | |
58 | it seem that include/coff/internal.h should not define | |
59 | PE_DEF_SECTION_ALIGNMENT. */ | |
60 | #if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT} | |
61 | #undef PE_DEF_SECTION_ALIGNMENT | |
62 | #define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT} | |
63 | #endif | |
64 | ||
c6c37250 DD |
65 | #if defined(TARGET_IS_i386pe) |
66 | #define DLL_SUPPORT | |
67 | #endif | |
68 | ||
69 | #define PE_DEF_SUBSYSTEM 3 | |
70 | ||
434d1125 NC |
71 | #ifdef TARGET_IS_arm_epoc_pe |
72 | #define bfd_arm_pe_allocate_interworking_sections \ | |
73 | bfd_arm_epoc_pe_allocate_interworking_sections | |
74 | #define bfd_arm_pe_get_bfd_for_interworking \ | |
75 | bfd_arm_epoc_pe_get_bfd_for_interworking | |
76 | #define bfd_arm_pe_process_before_allocation \ | |
77 | bfd_arm_epoc_pe_process_before_allocation | |
78 | #endif | |
79 | ||
252b5132 RH |
80 | static void gld_${EMULATION_NAME}_set_symbols PARAMS ((void)); |
81 | static void gld_${EMULATION_NAME}_after_open PARAMS ((void)); | |
82 | static void gld_${EMULATION_NAME}_before_parse PARAMS ((void)); | |
83 | static void gld_${EMULATION_NAME}_after_parse PARAMS ((void)); | |
84 | static void gld_${EMULATION_NAME}_before_allocation PARAMS ((void)); | |
85 | static boolean gld_${EMULATION_NAME}_place_orphan | |
86 | PARAMS ((lang_input_statement_type *, asection *)); | |
87 | static void gld${EMULATION_NAME}_place_section | |
88 | PARAMS ((lang_statement_union_type *)); | |
89 | static char *gld_${EMULATION_NAME}_get_script PARAMS ((int *)); | |
90 | static int gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **)); | |
6f798e5c | 91 | static void gld_${EMULATION_NAME}_finish PARAMS ((void)); |
252b5132 RH |
92 | |
93 | static struct internal_extra_pe_aouthdr pe; | |
94 | static int dll; | |
95 | static int support_old_code = 0; | |
6f798e5c | 96 | static char * thumb_entry_symbol = NULL; |
252b5132 RH |
97 | static lang_assignment_statement_type *image_base_statement = 0; |
98 | ||
99 | static char *pe_out_def_filename = 0; | |
252b5132 RH |
100 | static int pe_enable_stdcall_fixup = -1; /* 0=disable 1=enable */ |
101 | static char *pe_implib_filename = 0; | |
102 | ||
103 | extern const char *output_filename; | |
104 | ||
105 | static void | |
106 | gld_${EMULATION_NAME}_before_parse() | |
107 | { | |
2be9b2c7 | 108 | output_filename = "${EXECUTABLE_NAME:-a.exe}"; |
252b5132 | 109 | ldfile_output_architecture = bfd_arch_${ARCH}; |
c6c37250 | 110 | #ifdef DLL_SUPPORT |
252b5132 RH |
111 | config.has_shared = 1; |
112 | #endif | |
113 | } | |
114 | \f | |
115 | /* PE format extra command line options. */ | |
116 | ||
117 | /* Used for setting flags in the PE header. */ | |
118 | #define OPTION_BASE_FILE (300 + 1) | |
119 | #define OPTION_DLL (OPTION_BASE_FILE + 1) | |
120 | #define OPTION_FILE_ALIGNMENT (OPTION_DLL + 1) | |
121 | #define OPTION_IMAGE_BASE (OPTION_FILE_ALIGNMENT + 1) | |
122 | #define OPTION_MAJOR_IMAGE_VERSION (OPTION_IMAGE_BASE + 1) | |
123 | #define OPTION_MAJOR_OS_VERSION (OPTION_MAJOR_IMAGE_VERSION + 1) | |
124 | #define OPTION_MAJOR_SUBSYSTEM_VERSION (OPTION_MAJOR_OS_VERSION + 1) | |
125 | #define OPTION_MINOR_IMAGE_VERSION (OPTION_MAJOR_SUBSYSTEM_VERSION + 1) | |
126 | #define OPTION_MINOR_OS_VERSION (OPTION_MINOR_IMAGE_VERSION + 1) | |
127 | #define OPTION_MINOR_SUBSYSTEM_VERSION (OPTION_MINOR_OS_VERSION + 1) | |
128 | #define OPTION_SECTION_ALIGNMENT (OPTION_MINOR_SUBSYSTEM_VERSION + 1) | |
129 | #define OPTION_STACK (OPTION_SECTION_ALIGNMENT + 1) | |
130 | #define OPTION_SUBSYSTEM (OPTION_STACK + 1) | |
131 | #define OPTION_HEAP (OPTION_SUBSYSTEM + 1) | |
132 | #define OPTION_SUPPORT_OLD_CODE (OPTION_HEAP + 1) | |
133 | #define OPTION_OUT_DEF (OPTION_SUPPORT_OLD_CODE + 1) | |
134 | #define OPTION_EXPORT_ALL (OPTION_OUT_DEF + 1) | |
135 | #define OPTION_EXCLUDE_SYMBOLS (OPTION_EXPORT_ALL + 1) | |
136 | #define OPTION_KILL_ATS (OPTION_EXCLUDE_SYMBOLS + 1) | |
137 | #define OPTION_STDCALL_ALIASES (OPTION_KILL_ATS + 1) | |
138 | #define OPTION_ENABLE_STDCALL_FIXUP (OPTION_STDCALL_ALIASES + 1) | |
139 | #define OPTION_DISABLE_STDCALL_FIXUP (OPTION_ENABLE_STDCALL_FIXUP + 1) | |
140 | #define OPTION_IMPLIB_FILENAME (OPTION_DISABLE_STDCALL_FIXUP + 1) | |
6f798e5c | 141 | #define OPTION_THUMB_ENTRY (OPTION_IMPLIB_FILENAME + 1) |
252b5132 RH |
142 | |
143 | static struct option longopts[] = | |
144 | { | |
145 | /* PE options */ | |
146 | {"base-file", required_argument, NULL, OPTION_BASE_FILE}, | |
147 | {"dll", no_argument, NULL, OPTION_DLL}, | |
148 | {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT}, | |
149 | {"heap", required_argument, NULL, OPTION_HEAP}, | |
150 | {"image-base", required_argument, NULL, OPTION_IMAGE_BASE}, | |
151 | {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION}, | |
152 | {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION}, | |
153 | {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION}, | |
154 | {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION}, | |
155 | {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION}, | |
156 | {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION}, | |
157 | {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT}, | |
158 | {"stack", required_argument, NULL, OPTION_STACK}, | |
159 | {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM}, | |
160 | {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE}, | |
6f798e5c | 161 | {"thumb-entry", required_argument, NULL, OPTION_THUMB_ENTRY}, |
c6c37250 | 162 | #ifdef DLL_SUPPORT |
252b5132 RH |
163 | /* getopt allows abbreviations, so we do this to stop it from treating -o |
164 | as an abbreviation for this option */ | |
165 | {"output-def", required_argument, NULL, OPTION_OUT_DEF}, | |
166 | {"output-def", required_argument, NULL, OPTION_OUT_DEF}, | |
167 | {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL}, | |
168 | {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS}, | |
169 | {"kill-at", no_argument, NULL, OPTION_KILL_ATS}, | |
170 | {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES}, | |
171 | {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP}, | |
172 | {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP}, | |
173 | {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME}, | |
174 | #endif | |
175 | {NULL, no_argument, NULL, 0} | |
176 | }; | |
177 | ||
178 | ||
179 | /* PE/WIN32; added routines to get the subsystem type, heap and/or stack | |
180 | parameters which may be input from the command line */ | |
181 | ||
182 | typedef struct | |
183 | { | |
184 | void *ptr; | |
185 | int size; | |
186 | int value; | |
187 | char *symbol; | |
188 | int inited; | |
189 | } definfo; | |
190 | ||
191 | #define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol,0} | |
192 | ||
193 | static definfo init[] = | |
194 | { | |
195 | /* imagebase must be first */ | |
196 | #define IMAGEBASEOFF 0 | |
197 | D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE), | |
198 | #define DLLOFF 1 | |
1069dd8d | 199 | {&dll, sizeof(dll), 0, "__dll__", 0}, |
252b5132 RH |
200 | D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT), |
201 | D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT), | |
202 | D(MajorOperatingSystemVersion,"__major_os_version__", 4), | |
203 | D(MinorOperatingSystemVersion,"__minor_os_version__", 0), | |
204 | D(MajorImageVersion,"__major_image_version__", 1), | |
205 | D(MinorImageVersion,"__minor_image_version__", 0), | |
206 | D(MajorSubsystemVersion,"__major_subsystem_version__", 4), | |
207 | D(MinorSubsystemVersion,"__minor_subsystem_version__", 0), | |
2be9b2c7 | 208 | D(Subsystem,"__subsystem__", ${SUBSYSTEM}), |
252b5132 RH |
209 | D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x2000000), |
210 | D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000), | |
211 | D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000), | |
212 | D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000), | |
213 | D(LoaderFlags,"__loader_flags__", 0x0), | |
214 | { NULL, 0, 0, NULL, 0 } | |
215 | }; | |
216 | ||
217 | static void | |
218 | gld_${EMULATION_NAME}_list_options (file) | |
219 | FILE * file; | |
220 | { | |
221 | fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n")); | |
222 | fprintf (file, _(" --dll Set image base to the default for DLLs\n")); | |
223 | fprintf (file, _(" --file-alignment <size> Set file alignment\n")); | |
224 | fprintf (file, _(" --heap <size> Set initial size of the heap\n")); | |
225 | fprintf (file, _(" --image-base <address> Set start address of the executable\n")); | |
226 | fprintf (file, _(" --major-image-version <number> Set version number of the executable\n")); | |
227 | fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n")); | |
228 | fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n")); | |
229 | fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n")); | |
230 | fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n")); | |
231 | fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n")); | |
232 | fprintf (file, _(" --section-alignment <size> Set section alignment\n")); | |
233 | fprintf (file, _(" --stack <size> Set size of the initial stack\n")); | |
234 | fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n")); | |
235 | fprintf (file, _(" --support-old-code Support interworking with old code\n")); | |
6f798e5c | 236 | fprintf (file, _(" --thumb-entry=<symbol> Set the entry point to be Thumb <symbol>\n")); |
c6c37250 | 237 | #ifdef DLL_SUPPORT |
252b5132 RH |
238 | fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n")); |
239 | fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n")); | |
240 | fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n")); | |
241 | fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n")); | |
242 | fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n")); | |
243 | fprintf (file, _(" --kill-at Remove @nn from exported symbols\n")); | |
244 | fprintf (file, _(" --out-implib <file> Generate import library\n")); | |
245 | fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n")); | |
246 | #endif | |
247 | } | |
248 | ||
249 | static void | |
250 | set_pe_name (name, val) | |
251 | char *name; | |
252 | long val; | |
253 | { | |
254 | int i; | |
255 | /* Find the name and set it. */ | |
256 | for (i = 0; init[i].ptr; i++) | |
257 | { | |
258 | if (strcmp (name, init[i].symbol) == 0) | |
259 | { | |
260 | init[i].value = val; | |
261 | init[i].inited = 1; | |
262 | return; | |
263 | } | |
264 | } | |
265 | abort(); | |
266 | } | |
267 | ||
268 | ||
269 | static void | |
270 | set_pe_subsystem () | |
271 | { | |
272 | const char *sver; | |
273 | int len; | |
274 | int i; | |
275 | static const struct | |
276 | { | |
277 | const char *name; | |
278 | const int value; | |
279 | const char *entry; | |
280 | } | |
281 | v[] = | |
282 | { | |
2be9b2c7 ILT |
283 | { "native", 1, "NtProcessStartup" }, |
284 | { "windows", 2, "WinMainCRTStartup" }, | |
285 | { "console", 3, "mainCRTStartup" }, | |
252b5132 RH |
286 | #if 0 |
287 | /* The Microsoft linker does not recognize this. */ | |
288 | { "os2", 5, "" }, | |
289 | #endif | |
2be9b2c7 | 290 | { "posix", 7, "__PosixProcessStartup"}, |
252b5132 RH |
291 | { 0, 0, 0 } |
292 | }; | |
293 | ||
294 | sver = strchr (optarg, ':'); | |
295 | if (sver == NULL) | |
296 | len = strlen (optarg); | |
297 | else | |
298 | { | |
299 | char *end; | |
300 | ||
301 | len = sver - optarg; | |
302 | set_pe_name ("__major_subsystem_version__", | |
303 | strtoul (sver + 1, &end, 0)); | |
304 | if (*end == '.') | |
305 | set_pe_name ("__minor_subsystem_version__", | |
306 | strtoul (end + 1, &end, 0)); | |
307 | if (*end != '\0') | |
308 | einfo (_("%P: warning: bad version number in -subsystem option\n")); | |
309 | } | |
310 | ||
311 | for (i = 0; v[i].name; i++) | |
312 | { | |
313 | if (strncmp (optarg, v[i].name, len) == 0 | |
314 | && v[i].name[len] == '\0') | |
315 | { | |
2be9b2c7 ILT |
316 | const char *initial_symbol_char; |
317 | const char *entry; | |
318 | ||
252b5132 RH |
319 | set_pe_name ("__subsystem__", v[i].value); |
320 | ||
2be9b2c7 ILT |
321 | initial_symbol_char = ${INITIAL_SYMBOL_CHAR}; |
322 | if (*initial_symbol_char == '\0') | |
323 | entry = v[i].entry; | |
324 | else | |
325 | { | |
326 | char *alc_entry; | |
327 | ||
328 | /* lang_add_entry expects its argument to be permanently | |
329 | allocated, so we don't free this string. */ | |
330 | alc_entry = xmalloc (strlen (initial_symbol_char) | |
331 | + strlen (v[i].entry) | |
332 | + 1); | |
333 | strcpy (alc_entry, initial_symbol_char); | |
334 | strcat (alc_entry, v[i].entry); | |
335 | entry = alc_entry; | |
336 | } | |
337 | ||
338 | lang_add_entry (entry, 1); | |
252b5132 RH |
339 | |
340 | return; | |
341 | } | |
342 | } | |
343 | ||
344 | einfo (_("%P%F: invalid subsystem type %s\n"), optarg); | |
345 | } | |
346 | ||
347 | ||
348 | ||
349 | static void | |
350 | set_pe_value (name) | |
351 | char *name; | |
352 | ||
353 | { | |
354 | char *end; | |
355 | ||
356 | set_pe_name (name, strtoul (optarg, &end, 0)); | |
357 | ||
358 | if (end == optarg) | |
359 | einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg); | |
360 | ||
361 | optarg = end; | |
362 | } | |
363 | ||
364 | static void | |
365 | set_pe_stack_heap (resname, comname) | |
366 | char *resname; | |
367 | char *comname; | |
368 | { | |
369 | set_pe_value (resname); | |
370 | ||
371 | if (*optarg == ',') | |
372 | { | |
373 | optarg++; | |
374 | set_pe_value (comname); | |
375 | } | |
376 | else if (*optarg) | |
377 | einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg); | |
378 | } | |
379 | ||
380 | ||
381 | ||
382 | static int | |
383 | gld_${EMULATION_NAME}_parse_args(argc, argv) | |
384 | int argc; | |
385 | char **argv; | |
386 | { | |
387 | int longind; | |
388 | int optc; | |
389 | int prevoptind = optind; | |
390 | int prevopterr = opterr; | |
391 | int wanterror; | |
392 | static int lastoptind = -1; | |
393 | ||
394 | if (lastoptind != optind) | |
395 | opterr = 0; | |
396 | wanterror = opterr; | |
397 | ||
398 | lastoptind = optind; | |
399 | ||
400 | optc = getopt_long_only (argc, argv, "-", longopts, &longind); | |
401 | opterr = prevopterr; | |
402 | ||
403 | switch (optc) | |
404 | { | |
405 | default: | |
406 | if (wanterror) | |
407 | xexit (1); | |
408 | optind = prevoptind; | |
409 | return 0; | |
410 | ||
411 | case OPTION_BASE_FILE: | |
412 | link_info.base_file = (PTR) fopen (optarg, FOPEN_WB); | |
413 | if (link_info.base_file == NULL) | |
414 | { | |
415 | /* xgettext:c-format */ | |
416 | fprintf (stderr, _("%s: Can't open base file %s\n"), | |
417 | program_name, optarg); | |
418 | xexit (1); | |
419 | } | |
420 | break; | |
421 | ||
422 | /* PE options */ | |
423 | case OPTION_HEAP: | |
424 | set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__"); | |
425 | break; | |
426 | case OPTION_STACK: | |
427 | set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__"); | |
428 | break; | |
429 | case OPTION_SUBSYSTEM: | |
430 | set_pe_subsystem (); | |
431 | break; | |
432 | case OPTION_MAJOR_OS_VERSION: | |
433 | set_pe_value ("__major_os_version__"); | |
434 | break; | |
435 | case OPTION_MINOR_OS_VERSION: | |
436 | set_pe_value ("__minor_os_version__"); | |
437 | break; | |
438 | case OPTION_MAJOR_SUBSYSTEM_VERSION: | |
439 | set_pe_value ("__major_subsystem_version__"); | |
440 | break; | |
441 | case OPTION_MINOR_SUBSYSTEM_VERSION: | |
442 | set_pe_value ("__minor_subsystem_version__"); | |
443 | break; | |
444 | case OPTION_MAJOR_IMAGE_VERSION: | |
445 | set_pe_value ("__major_image_version__"); | |
446 | break; | |
447 | case OPTION_MINOR_IMAGE_VERSION: | |
448 | set_pe_value ("__minor_image_version__"); | |
449 | break; | |
450 | case OPTION_FILE_ALIGNMENT: | |
451 | set_pe_value ("__file_alignment__"); | |
452 | break; | |
453 | case OPTION_SECTION_ALIGNMENT: | |
454 | set_pe_value ("__section_alignment__"); | |
455 | break; | |
456 | case OPTION_DLL: | |
457 | set_pe_name ("__dll__", 1); | |
458 | break; | |
459 | case OPTION_IMAGE_BASE: | |
460 | set_pe_value ("__image_base__"); | |
461 | break; | |
462 | case OPTION_SUPPORT_OLD_CODE: | |
463 | support_old_code = 1; | |
464 | break; | |
6f798e5c NC |
465 | case OPTION_THUMB_ENTRY: |
466 | thumb_entry_symbol = optarg; | |
467 | break; | |
c6c37250 | 468 | #ifdef DLL_SUPPORT |
252b5132 RH |
469 | case OPTION_OUT_DEF: |
470 | pe_out_def_filename = xstrdup (optarg); | |
471 | break; | |
472 | case OPTION_EXPORT_ALL: | |
473 | pe_dll_export_everything = 1; | |
474 | break; | |
475 | case OPTION_EXCLUDE_SYMBOLS: | |
252b5132 | 476 | pe_dll_add_excludes (optarg); |
252b5132 RH |
477 | break; |
478 | case OPTION_KILL_ATS: | |
479 | pe_dll_kill_ats = 1; | |
480 | break; | |
481 | case OPTION_STDCALL_ALIASES: | |
482 | pe_dll_stdcall_aliases = 1; | |
483 | break; | |
484 | case OPTION_ENABLE_STDCALL_FIXUP: | |
485 | pe_enable_stdcall_fixup = 1; | |
486 | break; | |
487 | case OPTION_DISABLE_STDCALL_FIXUP: | |
488 | pe_enable_stdcall_fixup = 0; | |
489 | break; | |
490 | case OPTION_IMPLIB_FILENAME: | |
491 | pe_implib_filename = xstrdup (optarg); | |
492 | break; | |
c6c37250 | 493 | #endif |
252b5132 RH |
494 | } |
495 | return 1; | |
496 | } | |
497 | \f | |
498 | /* Assign values to the special symbols before the linker script is | |
499 | read. */ | |
500 | ||
501 | static void | |
502 | gld_${EMULATION_NAME}_set_symbols () | |
503 | { | |
504 | /* Run through and invent symbols for all the | |
505 | names and insert the defaults. */ | |
506 | int j; | |
507 | lang_statement_list_type *save; | |
508 | ||
509 | if (!init[IMAGEBASEOFF].inited) | |
510 | { | |
511 | if (link_info.relocateable) | |
512 | init[IMAGEBASEOFF].value = 0; | |
513 | else if (init[DLLOFF].value || link_info.shared) | |
514 | init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE; | |
515 | else | |
516 | init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE; | |
517 | } | |
518 | ||
519 | /* Don't do any symbol assignments if this is a relocateable link. */ | |
520 | if (link_info.relocateable) | |
521 | return; | |
522 | ||
523 | /* Glue the assignments into the abs section */ | |
524 | save = stat_ptr; | |
525 | ||
526 | stat_ptr = &(abs_output_section->children); | |
527 | ||
528 | for (j = 0; init[j].ptr; j++) | |
529 | { | |
530 | long val = init[j].value; | |
531 | lang_assignment_statement_type *rv; | |
532 | rv = lang_add_assignment (exp_assop ('=' ,init[j].symbol, exp_intop (val))); | |
533 | if (init[j].size == sizeof(short)) | |
534 | *(short *)init[j].ptr = val; | |
535 | else if (init[j].size == sizeof(int)) | |
536 | *(int *)init[j].ptr = val; | |
537 | else if (init[j].size == sizeof(long)) | |
538 | *(long *)init[j].ptr = val; | |
539 | /* This might be a long long or other special type. */ | |
540 | else if (init[j].size == sizeof(bfd_vma)) | |
541 | *(bfd_vma *)init[j].ptr = val; | |
542 | else abort(); | |
543 | if (j == IMAGEBASEOFF) | |
544 | image_base_statement = rv; | |
545 | } | |
546 | /* Restore the pointer. */ | |
547 | stat_ptr = save; | |
548 | ||
549 | if (pe.FileAlignment > | |
550 | pe.SectionAlignment) | |
551 | { | |
552 | einfo (_("%P: warning, file alignment > section alignment.\n")); | |
553 | } | |
554 | } | |
555 | ||
556 | /* This is called after the linker script and the command line options | |
557 | have been read. */ | |
558 | ||
559 | static void | |
560 | gld_${EMULATION_NAME}_after_parse () | |
561 | { | |
562 | /* The Windows libraries are designed for the linker to treat the | |
563 | entry point as an undefined symbol. Otherwise, the .obj that | |
564 | defines mainCRTStartup is brought in because it is the first | |
565 | encountered in libc.lib and it has other symbols in it which will | |
566 | be pulled in by the link process. To avoid this, we act as | |
567 | though the user specified -u with the entry point symbol. | |
568 | ||
569 | This function is called after the linker script and command line | |
570 | options have been read, so at this point we know the right entry | |
571 | point. This function is called before the input files are | |
572 | opened, so registering the symbol as undefined will make a | |
573 | difference. */ | |
574 | ||
2a275620 | 575 | if (! link_info.relocateable && entry_symbol != NULL) |
252b5132 RH |
576 | ldlang_add_undef (entry_symbol); |
577 | } | |
578 | ||
579 | static struct bfd_link_hash_entry *pe_undef_found_sym; | |
580 | ||
581 | static boolean | |
582 | pe_undef_cdecl_match (h, string) | |
583 | struct bfd_link_hash_entry *h; | |
584 | PTR string; | |
585 | { | |
586 | int sl = strlen (string); | |
587 | if (h->type == bfd_link_hash_defined | |
588 | && strncmp (h->root.string, string, sl) == 0 | |
589 | && h->root.string[sl] == '@') | |
590 | { | |
591 | pe_undef_found_sym = h; | |
592 | return false; | |
593 | } | |
594 | return true; | |
595 | } | |
596 | ||
597 | static void | |
598 | pe_fixup_stdcalls () | |
599 | { | |
600 | static int gave_warning_message = 0; | |
601 | struct bfd_link_hash_entry *undef, *sym; | |
602 | char *at; | |
603 | for (undef = link_info.hash->undefs; undef; undef=undef->next) | |
604 | if (undef->type == bfd_link_hash_undefined) | |
605 | { | |
606 | at = strchr (undef->root.string, '@'); | |
607 | if (at) | |
608 | { | |
609 | /* The symbol is a stdcall symbol, so let's look for a cdecl | |
610 | symbol with the same name and resolve to that */ | |
611 | char *cname = xstrdup (undef->root.string); | |
612 | at = strchr (cname, '@'); | |
613 | *at = 0; | |
614 | sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1); | |
615 | if (sym && sym->type == bfd_link_hash_defined) | |
616 | { | |
617 | undef->type = bfd_link_hash_defined; | |
618 | undef->u.def.value = sym->u.def.value; | |
619 | undef->u.def.section = sym->u.def.section; | |
620 | if (pe_enable_stdcall_fixup == -1) | |
621 | { | |
622 | einfo (_("Warning: resolving %s by linking to %s\n"), | |
623 | undef->root.string, cname); | |
624 | if (! gave_warning_message) | |
625 | { | |
626 | gave_warning_message = 1; | |
627 | einfo(_("Use --enable-stdcall-fixup to disable these warnings\n")); | |
628 | einfo(_("Use --disable-stdcall-fixup to disable these fixups\n")); | |
629 | } | |
630 | } | |
631 | } | |
632 | } | |
633 | else | |
634 | { | |
635 | /* The symbol is a cdecl symbol, so we look for stdcall | |
636 | symbols - which means scanning the whole symbol table */ | |
637 | pe_undef_found_sym = 0; | |
638 | bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match, | |
639 | (PTR) undef->root.string); | |
640 | sym = pe_undef_found_sym; | |
641 | if (sym) | |
642 | { | |
643 | undef->type = bfd_link_hash_defined; | |
644 | undef->u.def.value = sym->u.def.value; | |
645 | undef->u.def.section = sym->u.def.section; | |
646 | if (pe_enable_stdcall_fixup == -1) | |
647 | { | |
648 | einfo (_("Warning: resolving %s by linking to %s\n"), | |
649 | undef->root.string, sym->root.string); | |
650 | if (! gave_warning_message) | |
651 | { | |
652 | gave_warning_message = 1; | |
653 | einfo(_("Use --enable-stdcall-fixup to disable these warnings\n")); | |
654 | einfo(_("Use --disable-stdcall-fixup to disable these fixups\n")); | |
655 | } | |
656 | } | |
657 | } | |
658 | } | |
659 | } | |
660 | } | |
661 | ||
662 | static void | |
663 | gld_${EMULATION_NAME}_after_open () | |
664 | { | |
665 | /* Pass the wacky PE command line options into the output bfd. | |
666 | FIXME: This should be done via a function, rather than by | |
667 | including an internal BFD header. */ | |
668 | ||
669 | if (!coff_data (output_bfd)->pe) | |
670 | einfo (_("%F%P: PE operations on non PE file.\n")); | |
671 | ||
672 | pe_data (output_bfd)->pe_opthdr = pe; | |
673 | pe_data (output_bfd)->dll = init[DLLOFF].value; | |
674 | ||
c6c37250 | 675 | #ifdef DLL_SUPPORT |
252b5132 RH |
676 | if (pe_enable_stdcall_fixup) /* -1=warn or 1=disable */ |
677 | pe_fixup_stdcalls (); | |
678 | ||
679 | pe_process_import_defs(output_bfd, &link_info); | |
680 | if (link_info.shared) | |
681 | pe_dll_build_sections (output_bfd, &link_info); | |
682 | #endif | |
683 | ||
626e0105 | 684 | #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe) |
f11523b0 NC |
685 | if (strstr (bfd_get_target (output_bfd), "arm") == NULL) |
686 | { | |
687 | /* The arm backend needs special fields in the output hash structure. | |
688 | These will only be created if the output format is an arm format, | |
689 | hence we do not support linking and changing output formats at the | |
690 | same time. Use a link followed by objcopy to change output formats. */ | |
691 | einfo ("%F%X%P: error: cannot change output format whilst linking ARM binaries\n"); | |
692 | return; | |
693 | } | |
252b5132 RH |
694 | { |
695 | /* Find a BFD that can hold the interworking stubs. */ | |
696 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
697 | { | |
46d23b7c | 698 | if (bfd_arm_pe_get_bfd_for_interworking (is->the_bfd, & link_info)) |
252b5132 RH |
699 | break; |
700 | } | |
701 | } | |
702 | #endif | |
c6c37250 DD |
703 | |
704 | { | |
c6c37250 | 705 | int is_ms_arch; |
1069dd8d | 706 | bfd *cur_arch = 0; |
c6c37250 | 707 | lang_input_statement_type *is2; |
1069dd8d | 708 | |
c6c37250 DD |
709 | /* Careful - this is a shell script. Watch those dollar signs! */ |
710 | /* Microsoft import libraries have every member named the same, | |
711 | and not in the right order for us to link them correctly. We | |
712 | must detect these and rename the members so that they'll link | |
713 | correctly. There are three types of objects: the head, the | |
714 | thunks, and the sentinel(s). The head is easy; it's the one | |
715 | with idata2. We assume that the sentinels won't have relocs, | |
716 | and the thunks will. It's easier than checking the symbol | |
1069dd8d | 717 | table for external references. */ |
c6c37250 DD |
718 | LANG_FOR_EACH_INPUT_STATEMENT (is) |
719 | { | |
720 | if (is->the_bfd->my_archive) | |
721 | { | |
722 | bfd *arch = is->the_bfd->my_archive; | |
723 | if (cur_arch != arch) | |
724 | { | |
725 | cur_arch = arch; | |
726 | is_ms_arch = 1; | |
727 | for (is2 = is; | |
728 | is2 && is2->the_bfd->my_archive == arch; | |
729 | is2 = (lang_input_statement_type *)is2->next) | |
730 | { | |
731 | if (strcmp (is->the_bfd->filename, is2->the_bfd->filename)) | |
732 | is_ms_arch = 0; | |
733 | } | |
734 | } | |
735 | ||
736 | if (is_ms_arch) | |
737 | { | |
1069dd8d | 738 | int idata2 = 0, reloc_count=0; |
c6c37250 DD |
739 | asection *sec; |
740 | char *new_name, seq; | |
1069dd8d | 741 | |
c6c37250 DD |
742 | for (sec = is->the_bfd->sections; sec; sec = sec->next) |
743 | { | |
744 | if (strcmp (sec->name, ".idata\$2") == 0) | |
745 | idata2 = 1; | |
746 | reloc_count += sec->reloc_count; | |
747 | } | |
748 | ||
749 | if (idata2) /* .idata2 is the TOC */ | |
750 | seq = 'a'; | |
751 | else if (reloc_count > 0) /* thunks */ | |
752 | seq = 'b'; | |
753 | else /* sentinel */ | |
754 | seq = 'c'; | |
755 | ||
e4e24acb | 756 | new_name = xmalloc (strlen (is->the_bfd->filename) + 3); |
c6c37250 DD |
757 | sprintf (new_name, "%s.%c", is->the_bfd->filename, seq); |
758 | is->the_bfd->filename = new_name; | |
759 | ||
e4e24acb | 760 | new_name = xmalloc (strlen(is->filename) + 3); |
c6c37250 DD |
761 | sprintf (new_name, "%s.%c", is->filename, seq); |
762 | is->filename = new_name; | |
763 | } | |
764 | } | |
765 | } | |
766 | } | |
252b5132 RH |
767 | } |
768 | \f | |
769 | static void | |
770 | gld_${EMULATION_NAME}_before_allocation() | |
771 | { | |
772 | #ifdef TARGET_IS_ppcpe | |
773 | /* Here we rummage through the found bfds to collect toc information */ | |
774 | { | |
775 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
776 | { | |
777 | if (!ppc_process_before_allocation (is->the_bfd, &link_info)) | |
778 | { | |
779 | /* xgettext:c-format */ | |
780 | einfo (_("Errors encountered processing file %s\n"), is->filename); | |
781 | } | |
782 | } | |
783 | } | |
784 | ||
785 | /* We have seen it all. Allocate it, and carry on */ | |
786 | ppc_allocate_toc_section (&link_info); | |
787 | #endif /* TARGET_IS_ppcpe */ | |
788 | ||
626e0105 | 789 | #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe) |
252b5132 RH |
790 | /* FIXME: we should be able to set the size of the interworking stub |
791 | section. | |
792 | ||
793 | Here we rummage through the found bfds to collect glue | |
794 | information. FIXME: should this be based on a command line | |
795 | option? krk@cygnus.com */ | |
796 | { | |
797 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
798 | { | |
46d23b7c | 799 | if (! bfd_arm_pe_process_before_allocation |
252b5132 RH |
800 | (is->the_bfd, & link_info, support_old_code)) |
801 | { | |
802 | /* xgettext:c-format */ | |
803 | einfo (_("Errors encountered processing file %s for interworking"), | |
804 | is->filename); | |
805 | } | |
806 | } | |
807 | } | |
808 | ||
809 | /* We have seen it all. Allocate it, and carry on */ | |
46d23b7c | 810 | bfd_arm_pe_allocate_interworking_sections (& link_info); |
252b5132 RH |
811 | #endif /* TARGET_IS_armpe */ |
812 | } | |
813 | \f | |
814 | ||
815 | /* This is called when an input file isn't recognized as a BFD. We | |
816 | check here for .DEF files and pull them in automatically. */ | |
817 | ||
818 | static int | |
819 | saw_option(char *option) | |
820 | { | |
821 | int i; | |
822 | for (i=0; init[i].ptr; i++) | |
823 | if (strcmp (init[i].symbol, option) == 0) | |
824 | return init[i].inited; | |
825 | return 0; | |
826 | } | |
827 | ||
828 | static boolean | |
829 | gld_${EMULATION_NAME}_unrecognized_file(entry) | |
830 | lang_input_statement_type *entry; | |
831 | { | |
c6c37250 | 832 | #ifdef DLL_SUPPORT |
252b5132 RH |
833 | const char *ext = entry->filename + strlen (entry->filename) - 4; |
834 | ||
835 | if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0) | |
836 | { | |
837 | if (pe_def_file == 0) | |
838 | pe_def_file = def_file_empty (); | |
839 | def_file_parse (entry->filename, pe_def_file); | |
840 | if (pe_def_file) | |
841 | { | |
842 | int i, buflen=0, len; | |
843 | char *buf; | |
844 | for (i=0; i<pe_def_file->num_exports; i++) | |
845 | { | |
846 | len = strlen(pe_def_file->exports[i].internal_name); | |
847 | if (buflen < len+2) | |
848 | buflen = len+2; | |
849 | } | |
850 | buf = (char *) xmalloc (buflen); | |
851 | for (i=0; i<pe_def_file->num_exports; i++) | |
852 | { | |
853 | struct bfd_link_hash_entry *h; | |
854 | sprintf(buf, "_%s", pe_def_file->exports[i].internal_name); | |
855 | ||
856 | h = bfd_link_hash_lookup (link_info.hash, buf, true, true, true); | |
857 | if (h == (struct bfd_link_hash_entry *) NULL) | |
858 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); | |
859 | if (h->type == bfd_link_hash_new) | |
860 | { | |
861 | h->type = bfd_link_hash_undefined; | |
862 | h->u.undef.abfd = NULL; | |
863 | bfd_link_add_undef (link_info.hash, h); | |
864 | } | |
865 | } | |
866 | free (buf); | |
867 | ||
868 | /* def_file_print (stdout, pe_def_file); */ | |
869 | if (pe_def_file->is_dll == 1) | |
870 | link_info.shared = 1; | |
871 | ||
872 | if (pe_def_file->base_address != (bfd_vma)(-1)) | |
873 | { | |
874 | pe.ImageBase = | |
875 | pe_data (output_bfd)->pe_opthdr.ImageBase = | |
876 | init[IMAGEBASEOFF].value = pe_def_file->base_address; | |
877 | init[IMAGEBASEOFF].inited = 1; | |
878 | if (image_base_statement) | |
879 | image_base_statement->exp = | |
880 | exp_assop ('=', "__image_base__", exp_intop (pe.ImageBase)); | |
881 | } | |
882 | ||
883 | #if 0 | |
884 | /* Not sure if these *should* be set */ | |
885 | if (pe_def_file->version_major != -1) | |
886 | { | |
887 | pe.MajorImageVersion = pe_def_file->version_major; | |
888 | pe.MinorImageVersion = pe_def_file->version_minor; | |
889 | } | |
890 | #endif | |
891 | if (pe_def_file->stack_reserve != -1 | |
892 | && ! saw_option ("__size_of_stack_reserve__")) | |
893 | { | |
894 | pe.SizeOfStackReserve = pe_def_file->stack_reserve; | |
895 | if (pe_def_file->stack_commit != -1) | |
896 | pe.SizeOfStackCommit = pe_def_file->stack_commit; | |
897 | } | |
898 | if (pe_def_file->heap_reserve != -1 | |
899 | && ! saw_option ("__size_of_heap_reserve__")) | |
900 | { | |
901 | pe.SizeOfHeapReserve = pe_def_file->heap_reserve; | |
902 | if (pe_def_file->heap_commit != -1) | |
903 | pe.SizeOfHeapCommit = pe_def_file->heap_commit; | |
904 | } | |
905 | return true; | |
906 | } | |
907 | } | |
908 | #endif | |
909 | return false; | |
910 | ||
911 | } | |
912 | ||
913 | static boolean | |
914 | gld_${EMULATION_NAME}_recognized_file(entry) | |
915 | lang_input_statement_type *entry; | |
916 | { | |
c6c37250 | 917 | #ifdef DLL_SUPPORT |
252b5132 | 918 | #ifdef TARGET_IS_i386pe |
c6c37250 DD |
919 | pe_dll_id_target ("pei-i386"); |
920 | #endif | |
252b5132 RH |
921 | if (bfd_get_format (entry->the_bfd) == bfd_object) |
922 | { | |
923 | const char *ext = entry->filename + strlen (entry->filename) - 4; | |
924 | if (strcmp (ext, ".dll") == 0 || strcmp (ext, ".DLL") == 0) | |
925 | return pe_implied_import_dll (entry->filename); | |
926 | } | |
927 | #endif | |
928 | return false; | |
929 | } | |
930 | ||
931 | static void | |
932 | gld_${EMULATION_NAME}_finish () | |
933 | { | |
6f798e5c NC |
934 | #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe) |
935 | struct bfd_link_hash_entry * h; | |
936 | ||
937 | if (thumb_entry_symbol != NULL) | |
938 | { | |
939 | h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol, false, false, true); | |
940 | ||
941 | if (h != (struct bfd_link_hash_entry *) NULL | |
942 | && (h->type == bfd_link_hash_defined | |
943 | || h->type == bfd_link_hash_defweak) | |
944 | && h->u.def.section->output_section != NULL) | |
945 | { | |
946 | static char buffer[32]; | |
947 | bfd_vma val; | |
948 | ||
949 | /* Special procesing is required for a Thumb entry symbol. The | |
950 | bottom bit of its address must be set. */ | |
951 | val = (h->u.def.value | |
952 | + bfd_get_section_vma (output_bfd, | |
953 | h->u.def.section->output_section) | |
954 | + h->u.def.section->output_offset); | |
955 | ||
956 | val |= 1; | |
957 | ||
958 | /* Now convert this value into a string and store it in entry_symbol | |
959 | where the lang_finish() function will pick it up. */ | |
960 | buffer[0] = '0'; | |
961 | buffer[1] = 'x'; | |
962 | ||
963 | sprintf_vma (buffer + 2, val); | |
964 | ||
965 | if (entry_symbol != NULL && entry_from_cmdline) | |
966 | einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"), | |
967 | thumb_entry_symbol, entry_symbol); | |
968 | entry_symbol = buffer; | |
969 | } | |
970 | else | |
971 | einfo (_("%P: warning: connot find thumb start symbol %s\n"), thumb_entry_symbol); | |
972 | } | |
973 | #endif /* defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe) */ | |
974 | ||
c6c37250 | 975 | #ifdef DLL_SUPPORT |
252b5132 RH |
976 | if (link_info.shared) |
977 | { | |
978 | pe_dll_fill_sections (output_bfd, &link_info); | |
979 | if (pe_implib_filename) | |
980 | pe_dll_generate_implib (pe_def_file, pe_implib_filename); | |
981 | } | |
982 | if (pe_out_def_filename) | |
983 | pe_dll_generate_def_file (pe_out_def_filename); | |
984 | #endif | |
985 | } | |
986 | ||
987 | \f | |
988 | /* Place an orphan section. | |
989 | ||
990 | We use this to put sections in a reasonable place in the file, and | |
991 | to ensure that they are aligned as required. | |
992 | ||
993 | We handle grouped sections here as well. A section named .foo$nn | |
994 | goes into the output section .foo. All grouped sections are sorted | |
995 | by name. | |
996 | ||
997 | Grouped sections for the default sections are handled by the | |
998 | default linker script using wildcards, and are sorted by | |
999 | sort_sections. */ | |
1000 | ||
1001 | static asection *hold_section; | |
1002 | static char *hold_section_name; | |
1003 | static lang_output_section_statement_type *hold_use; | |
1004 | static lang_output_section_statement_type *hold_text; | |
1005 | static lang_output_section_statement_type *hold_rdata; | |
1006 | static lang_output_section_statement_type *hold_data; | |
1007 | static lang_output_section_statement_type *hold_bss; | |
1008 | ||
1009 | /* Place an orphan section. We use this to put random SHF_ALLOC | |
1010 | sections in the right segment. */ | |
1011 | ||
1012 | /*ARGSUSED*/ | |
1013 | static boolean | |
1014 | gld_${EMULATION_NAME}_place_orphan (file, s) | |
1015 | lang_input_statement_type *file; | |
1016 | asection *s; | |
1017 | { | |
1018 | const char *secname; | |
1019 | char *dollar; | |
1020 | ||
1021 | if ((s->flags & SEC_ALLOC) == 0) | |
1022 | return false; | |
1023 | ||
1024 | secname = bfd_get_section_name (s->owner, s); | |
1025 | ||
1026 | /* Look through the script to see where to place this section. */ | |
1027 | ||
1028 | hold_section = s; | |
1029 | ||
1030 | hold_section_name = xstrdup (secname); | |
1031 | dollar = strchr (hold_section_name, '$'); | |
1032 | if (dollar != NULL) | |
1033 | *dollar = '\0'; | |
1034 | ||
1035 | hold_use = NULL; | |
1036 | lang_for_each_statement (gld${EMULATION_NAME}_place_section); | |
1037 | ||
1038 | if (hold_use == NULL) | |
1039 | { | |
1040 | lang_output_section_statement_type *place; | |
1041 | char *outsecname; | |
1042 | asection *snew, **pps; | |
1043 | lang_statement_list_type *old; | |
1044 | lang_statement_list_type add; | |
1045 | etree_type *address; | |
1046 | ||
1047 | /* Try to put the new output section in a reasonable place based | |
1048 | on the section name and section flags. */ | |
1049 | place = NULL; | |
1050 | if ((s->flags & SEC_HAS_CONTENTS) == 0 | |
1051 | && hold_bss != NULL) | |
1052 | place = hold_bss; | |
1053 | else if ((s->flags & SEC_READONLY) == 0 | |
1054 | && hold_data != NULL) | |
1055 | place = hold_data; | |
1056 | else if ((s->flags & SEC_CODE) == 0 | |
1057 | && (s->flags & SEC_READONLY) != 0 | |
1058 | && hold_rdata != NULL) | |
1059 | place = hold_rdata; | |
1060 | else if ((s->flags & SEC_READONLY) != 0 | |
1061 | && hold_text != NULL) | |
1062 | place = hold_text; | |
1063 | ||
1064 | /* Choose a unique name for the section. This will be needed if | |
1065 | the same section name appears in the input file with | |
1066 | different loadable or allocateable characteristics. */ | |
1067 | outsecname = xstrdup (hold_section_name); | |
1068 | if (bfd_get_section_by_name (output_bfd, outsecname) != NULL) | |
1069 | { | |
1070 | unsigned int len; | |
1071 | char *newname; | |
1072 | unsigned int i; | |
1073 | ||
1074 | len = strlen (outsecname); | |
1075 | newname = xmalloc (len + 5); | |
1076 | strcpy (newname, outsecname); | |
1077 | i = 0; | |
1078 | do | |
1079 | { | |
1080 | sprintf (newname + len, "%d", i); | |
1081 | ++i; | |
1082 | } | |
1083 | while (bfd_get_section_by_name (output_bfd, newname) != NULL); | |
1084 | ||
1085 | free (outsecname); | |
1086 | outsecname = newname; | |
1087 | } | |
1088 | ||
1089 | /* We don't want to free OUTSECNAME, as it may get attached to | |
1090 | the output section statement. */ | |
1091 | ||
1092 | /* Create the section in the output file, and put it in the | |
1093 | right place. This shuffling is to make the output file look | |
1094 | neater. */ | |
1095 | snew = bfd_make_section (output_bfd, outsecname); | |
1096 | if (snew == NULL) | |
1097 | einfo ("%P%F: output format %s cannot represent section called %s\n", | |
1098 | output_bfd->xvec->name, outsecname); | |
1099 | if (place != NULL && place->bfd_section != NULL) | |
1100 | { | |
1101 | for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next) | |
1102 | ; | |
1103 | *pps = snew->next; | |
1104 | snew->next = place->bfd_section->next; | |
1105 | place->bfd_section->next = snew; | |
1106 | } | |
1107 | ||
1108 | /* Start building a list of statements for this section. */ | |
1109 | old = stat_ptr; | |
1110 | stat_ptr = &add; | |
1111 | lang_list_init (stat_ptr); | |
1112 | ||
1113 | if (link_info.relocateable) | |
1114 | address = NULL; | |
1115 | else | |
1116 | { | |
1117 | /* All sections in an executable must be aligned to a page | |
1118 | boundary. */ | |
1119 | address = exp_unop (ALIGN_K, | |
1120 | exp_nameop (NAME, "__section_alignment__")); | |
1121 | } | |
1122 | ||
1123 | lang_enter_output_section_statement (outsecname, address, 0, | |
1124 | (bfd_vma) 0, | |
1125 | (etree_type *) NULL, | |
1126 | (etree_type *) NULL, | |
1127 | (etree_type *) NULL); | |
1128 | ||
1129 | hold_use = lang_output_section_statement_lookup (outsecname); | |
1130 | ||
1131 | lang_leave_output_section_statement | |
1132 | ((bfd_vma) 0, "*default*", | |
1133 | (struct lang_output_section_phdr_list *) NULL); | |
1134 | ||
1135 | /* Now stick the new statement list right after PLACE. */ | |
1136 | if (place != NULL) | |
1137 | { | |
1138 | *add.tail = place->header.next; | |
1139 | place->header.next = add.head; | |
1140 | } | |
1141 | ||
1142 | stat_ptr = old; | |
1143 | } | |
1144 | ||
1145 | if (dollar == NULL) | |
1146 | wild_doit (&hold_use->children, s, hold_use, file); | |
1147 | else | |
1148 | { | |
1149 | lang_statement_union_type **pl; | |
1150 | boolean found_dollar; | |
1151 | lang_statement_list_type list; | |
1152 | ||
1153 | /* The section name has a '$'. Sort it with the other '$' | |
1154 | sections. */ | |
1155 | ||
1156 | found_dollar = false; | |
1157 | for (pl = &hold_use->children.head; *pl != NULL; pl = &(*pl)->next) | |
1158 | { | |
1159 | lang_input_section_type *ls; | |
1160 | const char *lname; | |
1161 | ||
1162 | if ((*pl)->header.type != lang_input_section_enum) | |
1163 | continue; | |
1164 | ||
1165 | ls = &(*pl)->input_section; | |
1166 | ||
1167 | lname = bfd_get_section_name (ls->ifile->the_bfd, ls->section); | |
1168 | if (strchr (lname, '$') == NULL) | |
1169 | { | |
1170 | if (found_dollar) | |
1171 | break; | |
1172 | } | |
1173 | else | |
1174 | { | |
1175 | found_dollar = true; | |
1176 | if (strcmp (secname, lname) < 0) | |
1177 | break; | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | lang_list_init (&list); | |
1182 | wild_doit (&list, s, hold_use, file); | |
1183 | if (list.head != NULL) | |
1184 | { | |
1185 | ASSERT (list.head->next == NULL); | |
1186 | list.head->next = *pl; | |
1187 | *pl = list.head; | |
1188 | } | |
1189 | } | |
1190 | ||
1191 | free (hold_section_name); | |
1192 | ||
1193 | return true; | |
1194 | } | |
1195 | ||
1196 | static void | |
1197 | gld${EMULATION_NAME}_place_section (s) | |
1198 | lang_statement_union_type *s; | |
1199 | { | |
1200 | lang_output_section_statement_type *os; | |
1201 | ||
1202 | if (s->header.type != lang_output_section_statement_enum) | |
1203 | return; | |
1204 | ||
1205 | os = &s->output_section_statement; | |
1206 | ||
1207 | if (strcmp (os->name, hold_section_name) == 0 | |
1208 | && os->bfd_section != NULL | |
1209 | && ((hold_section->flags & (SEC_LOAD | SEC_ALLOC)) | |
1210 | == (os->bfd_section->flags & (SEC_LOAD | SEC_ALLOC)))) | |
1211 | hold_use = os; | |
1212 | ||
1213 | if (strcmp (os->name, ".text") == 0) | |
1214 | hold_text = os; | |
1215 | else if (strcmp (os->name, ".rdata") == 0) | |
1216 | hold_rdata = os; | |
1217 | else if (strcmp (os->name, ".data") == 0) | |
1218 | hold_data = os; | |
1219 | else if (strcmp (os->name, ".bss") == 0) | |
1220 | hold_bss = os; | |
1221 | } | |
1222 | \f | |
1223 | static char * | |
1224 | gld_${EMULATION_NAME}_get_script(isfile) | |
1225 | int *isfile; | |
1226 | EOF | |
1227 | # Scripts compiled in. | |
1228 | # sed commands to quote an ld script as a C string. | |
597e2591 | 1229 | sc="-f stringify.sed" |
252b5132 RH |
1230 | |
1231 | cat >>e${EMULATION_NAME}.c <<EOF | |
1232 | { | |
1233 | *isfile = 0; | |
1234 | ||
1235 | if (link_info.relocateable == true && config.build_constructors == true) | |
1236 | return | |
1237 | EOF | |
1238 | sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c | |
1239 | echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c | |
1240 | sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c | |
1241 | echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c | |
1242 | sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c | |
1243 | echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c | |
1244 | sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c | |
1245 | echo ' ; else return' >> e${EMULATION_NAME}.c | |
1246 | sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c | |
1247 | echo '; }' >> e${EMULATION_NAME}.c | |
1248 | ||
1249 | cat >>e${EMULATION_NAME}.c <<EOF | |
1250 | ||
1251 | ||
1252 | struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = | |
1253 | { | |
1254 | gld_${EMULATION_NAME}_before_parse, | |
1255 | syslib_default, | |
1256 | hll_default, | |
1257 | gld_${EMULATION_NAME}_after_parse, | |
1258 | gld_${EMULATION_NAME}_after_open, | |
1259 | after_allocation_default, | |
1260 | set_output_arch_default, | |
1261 | ldemul_default_target, | |
1262 | gld_${EMULATION_NAME}_before_allocation, | |
1263 | gld_${EMULATION_NAME}_get_script, | |
1264 | "${EMULATION_NAME}", | |
1265 | "${OUTPUT_FORMAT}", | |
1266 | gld_${EMULATION_NAME}_finish, /* finish */ | |
1267 | NULL, /* create output section statements */ | |
1268 | NULL, /* open dynamic archive */ | |
1269 | gld_${EMULATION_NAME}_place_orphan, | |
1270 | gld_${EMULATION_NAME}_set_symbols, | |
1271 | gld_${EMULATION_NAME}_parse_args, | |
1272 | gld_${EMULATION_NAME}_unrecognized_file, | |
1273 | gld_${EMULATION_NAME}_list_options, | |
1274 | gld_${EMULATION_NAME}_recognized_file | |
1275 | }; | |
1276 | EOF |