Add support for -fdata-sections
[deliverable/binutils-gdb.git] / ld / emultempl / pe.em
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 rm -f e${EMULATION_NAME}.c
4 (echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
5 cat >>e${EMULATION_NAME}.c <<EOF
6 /* This file is part of GLD, the Gnu Linker.
7 Copyright 1995, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
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 "ldfile.h"
41 #include "ldemul.h"
42 #include "ldlex.h"
43 #include "ldmisc.h"
44 #include "ldctor.h"
45 #include "coff/internal.h"
46
47 /* FIXME: This is a BFD internal header file, and we should not be
48 using it here. */
49 #include "../bfd/libcoff.h"
50
51 #include "deffile.h"
52 #include "pe-dll.h"
53
54 #define TARGET_IS_${EMULATION_NAME}
55
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
65 #if defined(TARGET_IS_i386pe)
66 #define DLL_SUPPORT
67 #endif
68 #if defined(TARGET_IS_shpe) || defined(TARGET_IS_mipspe) || defined(TARGET_IS_armpe)
69 #define DLL_SUPPORT
70 #endif
71
72 #if defined(TARGET_IS_i386pe) || ! defined(DLL_SUPPORT)
73 #define PE_DEF_SUBSYSTEM 3
74 #else
75 #undef NT_EXE_IMAGE_BASE
76 #undef PE_DEF_SECTION_ALIGNMENT
77 #undef PE_DEF_FILE_ALIGNMENT
78 #define NT_EXE_IMAGE_BASE 0x00010000
79 #ifdef TARGET_IS_armpe
80 #define PE_DEF_SECTION_ALIGNMENT 0x00001000
81 #define PE_DEF_SUBSYSTEM 9
82 #else
83 #define PE_DEF_SECTION_ALIGNMENT 0x00000400
84 #define PE_DEF_SUBSYSTEM 2
85 #endif
86 #define PE_DEF_FILE_ALIGNMENT 0x00000200
87 #endif
88
89 #ifdef TARGET_IS_arm_epoc_pe
90 #define bfd_arm_pe_allocate_interworking_sections \
91 bfd_arm_epoc_pe_allocate_interworking_sections
92 #define bfd_arm_pe_get_bfd_for_interworking \
93 bfd_arm_epoc_pe_get_bfd_for_interworking
94 #define bfd_arm_pe_process_before_allocation \
95 bfd_arm_epoc_pe_process_before_allocation
96 #endif
97
98 static void gld_${EMULATION_NAME}_set_symbols PARAMS ((void));
99 static void gld_${EMULATION_NAME}_after_open PARAMS ((void));
100 static void gld_${EMULATION_NAME}_before_parse PARAMS ((void));
101 static void gld_${EMULATION_NAME}_after_parse PARAMS ((void));
102 static void gld_${EMULATION_NAME}_before_allocation PARAMS ((void));
103 static boolean gld_${EMULATION_NAME}_place_orphan
104 PARAMS ((lang_input_statement_type *, asection *));
105 static char *gld_${EMULATION_NAME}_get_script PARAMS ((int *));
106 static int gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **));
107 static void gld_${EMULATION_NAME}_finish PARAMS ((void));
108 static boolean gld_${EMULATION_NAME}_open_dynamic_archive
109 PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
110
111 static struct internal_extra_pe_aouthdr pe;
112 static int dll;
113 static int support_old_code = 0;
114 static char * thumb_entry_symbol = NULL;
115 static lang_assignment_statement_type *image_base_statement = 0;
116
117 #ifdef DLL_SUPPORT
118 static int pe_enable_stdcall_fixup = -1; /* 0=disable 1=enable */
119 static char *pe_out_def_filename = NULL;
120 static char *pe_implib_filename = NULL;
121 static int pe_enable_auto_image_base = 0;
122 static char *pe_dll_search_prefix = NULL;
123 #endif
124
125 extern const char *output_filename;
126
127 static void
128 gld_${EMULATION_NAME}_before_parse()
129 {
130 output_filename = "${EXECUTABLE_NAME:-a.exe}";
131 ldfile_output_architecture = bfd_arch_${ARCH};
132 #ifdef DLL_SUPPORT
133 config.has_shared = 1;
134
135 #if (PE_DEF_SUBSYSTEM == 9) || (PE_DEF_SUBSYSTEM == 2)
136 #if defined TARGET_IS_mipspe || defined TARGET_IS_armpe
137 lang_add_entry ("WinMainCRTStartup", 1);
138 #else
139 lang_add_entry ("_WinMainCRTStartup", 1);
140 #endif
141 #endif
142 #endif
143 }
144 \f
145 /* PE format extra command line options. */
146
147 /* Used for setting flags in the PE header. */
148 #define OPTION_BASE_FILE (300 + 1)
149 #define OPTION_DLL (OPTION_BASE_FILE + 1)
150 #define OPTION_FILE_ALIGNMENT (OPTION_DLL + 1)
151 #define OPTION_IMAGE_BASE (OPTION_FILE_ALIGNMENT + 1)
152 #define OPTION_MAJOR_IMAGE_VERSION (OPTION_IMAGE_BASE + 1)
153 #define OPTION_MAJOR_OS_VERSION (OPTION_MAJOR_IMAGE_VERSION + 1)
154 #define OPTION_MAJOR_SUBSYSTEM_VERSION (OPTION_MAJOR_OS_VERSION + 1)
155 #define OPTION_MINOR_IMAGE_VERSION (OPTION_MAJOR_SUBSYSTEM_VERSION + 1)
156 #define OPTION_MINOR_OS_VERSION (OPTION_MINOR_IMAGE_VERSION + 1)
157 #define OPTION_MINOR_SUBSYSTEM_VERSION (OPTION_MINOR_OS_VERSION + 1)
158 #define OPTION_SECTION_ALIGNMENT (OPTION_MINOR_SUBSYSTEM_VERSION + 1)
159 #define OPTION_STACK (OPTION_SECTION_ALIGNMENT + 1)
160 #define OPTION_SUBSYSTEM (OPTION_STACK + 1)
161 #define OPTION_HEAP (OPTION_SUBSYSTEM + 1)
162 #define OPTION_SUPPORT_OLD_CODE (OPTION_HEAP + 1)
163 #define OPTION_OUT_DEF (OPTION_SUPPORT_OLD_CODE + 1)
164 #define OPTION_EXPORT_ALL (OPTION_OUT_DEF + 1)
165 #define OPTION_EXCLUDE_SYMBOLS (OPTION_EXPORT_ALL + 1)
166 #define OPTION_KILL_ATS (OPTION_EXCLUDE_SYMBOLS + 1)
167 #define OPTION_STDCALL_ALIASES (OPTION_KILL_ATS + 1)
168 #define OPTION_ENABLE_STDCALL_FIXUP (OPTION_STDCALL_ALIASES + 1)
169 #define OPTION_DISABLE_STDCALL_FIXUP (OPTION_ENABLE_STDCALL_FIXUP + 1)
170 #define OPTION_IMPLIB_FILENAME (OPTION_DISABLE_STDCALL_FIXUP + 1)
171 #define OPTION_THUMB_ENTRY (OPTION_IMPLIB_FILENAME + 1)
172 #define OPTION_WARN_DUPLICATE_EXPORTS (OPTION_THUMB_ENTRY + 1)
173 #define OPTION_IMP_COMPAT (OPTION_WARN_DUPLICATE_EXPORTS + 1)
174 #define OPTION_ENABLE_AUTO_IMAGE_BASE (OPTION_IMP_COMPAT + 1)
175 #define OPTION_DISABLE_AUTO_IMAGE_BASE (OPTION_ENABLE_AUTO_IMAGE_BASE + 1)
176 #define OPTION_DLL_SEARCH_PREFIX (OPTION_DISABLE_AUTO_IMAGE_BASE + 1)
177 #define OPTION_NO_DEFAULT_EXCLUDES (OPTION_DLL_SEARCH_PREFIX + 1)
178
179 static struct option longopts[] = {
180 /* PE options */
181 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
182 {"dll", no_argument, NULL, OPTION_DLL},
183 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
184 {"heap", required_argument, NULL, OPTION_HEAP},
185 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
186 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
187 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
188 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
189 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
190 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
191 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
192 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
193 {"stack", required_argument, NULL, OPTION_STACK},
194 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
195 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
196 {"thumb-entry", required_argument, NULL, OPTION_THUMB_ENTRY},
197 #ifdef DLL_SUPPORT
198 /* getopt allows abbreviations, so we do this to stop it from treating -o
199 as an abbreviation for this option */
200 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
201 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
202 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
203 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
204 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
205 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
206 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
207 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
208 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
209 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
210 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
211 {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
212 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
213 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
214 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
215 #endif
216 {NULL, no_argument, NULL, 0}
217 };
218
219
220 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
221 parameters which may be input from the command line */
222
223 typedef struct
224 {
225 void *ptr;
226 int size;
227 int value;
228 char *symbol;
229 int inited;
230 } definfo;
231
232 #define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol,0}
233
234 static definfo init[] =
235 {
236 /* imagebase must be first */
237 #define IMAGEBASEOFF 0
238 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
239 #define DLLOFF 1
240 {&dll, sizeof(dll), 0, "__dll__", 0},
241 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
242 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
243 D(MajorOperatingSystemVersion,"__major_os_version__", 4),
244 D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
245 D(MajorImageVersion,"__major_image_version__", 1),
246 D(MinorImageVersion,"__minor_image_version__", 0),
247 #ifdef TARGET_IS_armpe
248 D(MajorSubsystemVersion,"__major_subsystem_version__", 2),
249 #else
250 D(MajorSubsystemVersion,"__major_subsystem_version__", 4),
251 #endif
252 D(MinorSubsystemVersion,"__minor_subsystem_version__", 0),
253 D(Subsystem,"__subsystem__", ${SUBSYSTEM}),
254 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x2000000),
255 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
256 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
257 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
258 D(LoaderFlags,"__loader_flags__", 0x0),
259 { NULL, 0, 0, NULL, 0 }
260 };
261
262 static void
263 gld_${EMULATION_NAME}_list_options (file)
264 FILE * file;
265 {
266 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
267 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
268 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
269 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
270 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
271 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
272 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
273 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
274 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
275 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
276 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
277 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
278 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
279 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
280 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
281 fprintf (file, _(" --thumb-entry=<symbol> Set the entry point to be Thumb <symbol>\n"));
282 #ifdef DLL_SUPPORT
283 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
284 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
285 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
286 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
287 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
288 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
289 fprintf (file, _(" --out-implib <file> Generate import library\n"));
290 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
291 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
292 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n"));
293 fprintf (file, _(" create __imp_<SYMBOL> as well.\n"));
294 fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n"));
295 fprintf (file, _(" unless user specifies one\n"));
296 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
297 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll witout an\n"));
298 fprintf (file, _(" importlib, use <string><basename>.dll \n"));
299 fprintf (file, _(" in preference to lib<basename>.dll \n"));
300 #endif
301 }
302
303 static void
304 set_pe_name (name, val)
305 char *name;
306 long val;
307 {
308 int i;
309 /* Find the name and set it. */
310 for (i = 0; init[i].ptr; i++)
311 {
312 if (strcmp (name, init[i].symbol) == 0)
313 {
314 init[i].value = val;
315 init[i].inited = 1;
316 return;
317 }
318 }
319 abort();
320 }
321
322
323 static void
324 set_pe_subsystem ()
325 {
326 const char *sver;
327 int len;
328 int i;
329 static const struct
330 {
331 const char *name;
332 const int value;
333 const char *entry;
334 }
335 v[] =
336 {
337 { "native", 1, "NtProcessStartup" },
338 #if defined TARGET_IS_mipspe || defined TARGET_IS_armpe
339 { "windows", 2, "WinMainCRTStartup" },
340 #else
341 { "windows", 2, "WinMainCRTStartup" },
342 #endif
343 { "console", 3, "mainCRTStartup" },
344 #if 0
345 /* The Microsoft linker does not recognize this. */
346 { "os2", 5, "" },
347 #endif
348 { "posix", 7, "__PosixProcessStartup"},
349 { "wince", 9, "_WinMainCRTStartup" },
350 { 0, 0, 0 }
351 };
352
353 sver = strchr (optarg, ':');
354 if (sver == NULL)
355 len = strlen (optarg);
356 else
357 {
358 char *end;
359
360 len = sver - optarg;
361 set_pe_name ("__major_subsystem_version__",
362 strtoul (sver + 1, &end, 0));
363 if (*end == '.')
364 set_pe_name ("__minor_subsystem_version__",
365 strtoul (end + 1, &end, 0));
366 if (*end != '\0')
367 einfo (_("%P: warning: bad version number in -subsystem option\n"));
368 }
369
370 for (i = 0; v[i].name; i++)
371 {
372 if (strncmp (optarg, v[i].name, len) == 0
373 && v[i].name[len] == '\0')
374 {
375 const char *initial_symbol_char;
376 const char *entry;
377
378 set_pe_name ("__subsystem__", v[i].value);
379
380 initial_symbol_char = ${INITIAL_SYMBOL_CHAR};
381 if (*initial_symbol_char == '\0')
382 entry = v[i].entry;
383 else
384 {
385 char *alc_entry;
386
387 /* lang_add_entry expects its argument to be permanently
388 allocated, so we don't free this string. */
389 alc_entry = xmalloc (strlen (initial_symbol_char)
390 + strlen (v[i].entry)
391 + 1);
392 strcpy (alc_entry, initial_symbol_char);
393 strcat (alc_entry, v[i].entry);
394 entry = alc_entry;
395 }
396
397 lang_add_entry (entry, 1);
398
399 return;
400 }
401 }
402
403 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
404 }
405
406
407
408 static void
409 set_pe_value (name)
410 char *name;
411
412 {
413 char *end;
414
415 set_pe_name (name, strtoul (optarg, &end, 0));
416
417 if (end == optarg)
418 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
419
420 optarg = end;
421 }
422
423 static void
424 set_pe_stack_heap (resname, comname)
425 char *resname;
426 char *comname;
427 {
428 set_pe_value (resname);
429
430 if (*optarg == ',')
431 {
432 optarg++;
433 set_pe_value (comname);
434 }
435 else if (*optarg)
436 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
437 }
438
439
440
441 static int
442 gld_${EMULATION_NAME}_parse_args(argc, argv)
443 int argc;
444 char **argv;
445 {
446 int longind;
447 int optc;
448 int prevoptind = optind;
449 int prevopterr = opterr;
450 int wanterror;
451 static int lastoptind = -1;
452
453 if (lastoptind != optind)
454 opterr = 0;
455 wanterror = opterr;
456
457 lastoptind = optind;
458
459 optc = getopt_long_only (argc, argv, "-", longopts, &longind);
460 opterr = prevopterr;
461
462 switch (optc)
463 {
464 default:
465 if (wanterror)
466 xexit (1);
467 optind = prevoptind;
468 return 0;
469
470 case OPTION_BASE_FILE:
471 link_info.base_file = (PTR) fopen (optarg, FOPEN_WB);
472 if (link_info.base_file == NULL)
473 {
474 /* xgettext:c-format */
475 fprintf (stderr, _("%s: Can't open base file %s\n"),
476 program_name, optarg);
477 xexit (1);
478 }
479 break;
480
481 /* PE options */
482 case OPTION_HEAP:
483 set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
484 break;
485 case OPTION_STACK:
486 set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
487 break;
488 case OPTION_SUBSYSTEM:
489 set_pe_subsystem ();
490 break;
491 case OPTION_MAJOR_OS_VERSION:
492 set_pe_value ("__major_os_version__");
493 break;
494 case OPTION_MINOR_OS_VERSION:
495 set_pe_value ("__minor_os_version__");
496 break;
497 case OPTION_MAJOR_SUBSYSTEM_VERSION:
498 set_pe_value ("__major_subsystem_version__");
499 break;
500 case OPTION_MINOR_SUBSYSTEM_VERSION:
501 set_pe_value ("__minor_subsystem_version__");
502 break;
503 case OPTION_MAJOR_IMAGE_VERSION:
504 set_pe_value ("__major_image_version__");
505 break;
506 case OPTION_MINOR_IMAGE_VERSION:
507 set_pe_value ("__minor_image_version__");
508 break;
509 case OPTION_FILE_ALIGNMENT:
510 set_pe_value ("__file_alignment__");
511 break;
512 case OPTION_SECTION_ALIGNMENT:
513 set_pe_value ("__section_alignment__");
514 break;
515 case OPTION_DLL:
516 set_pe_name ("__dll__", 1);
517 break;
518 case OPTION_IMAGE_BASE:
519 set_pe_value ("__image_base__");
520 break;
521 case OPTION_SUPPORT_OLD_CODE:
522 support_old_code = 1;
523 break;
524 case OPTION_THUMB_ENTRY:
525 thumb_entry_symbol = optarg;
526 break;
527 #ifdef DLL_SUPPORT
528 case OPTION_OUT_DEF:
529 pe_out_def_filename = xstrdup (optarg);
530 break;
531 case OPTION_EXPORT_ALL:
532 pe_dll_export_everything = 1;
533 break;
534 case OPTION_EXCLUDE_SYMBOLS:
535 pe_dll_add_excludes (optarg);
536 break;
537 case OPTION_KILL_ATS:
538 pe_dll_kill_ats = 1;
539 break;
540 case OPTION_STDCALL_ALIASES:
541 pe_dll_stdcall_aliases = 1;
542 break;
543 case OPTION_ENABLE_STDCALL_FIXUP:
544 pe_enable_stdcall_fixup = 1;
545 break;
546 case OPTION_DISABLE_STDCALL_FIXUP:
547 pe_enable_stdcall_fixup = 0;
548 break;
549 case OPTION_IMPLIB_FILENAME:
550 pe_implib_filename = xstrdup (optarg);
551 break;
552 case OPTION_WARN_DUPLICATE_EXPORTS:
553 pe_dll_warn_dup_exports = 1;
554 break;
555 case OPTION_IMP_COMPAT:
556 pe_dll_compat_implib = 1;
557 break;
558 case OPTION_ENABLE_AUTO_IMAGE_BASE:
559 pe_enable_auto_image_base = 1;
560 break;
561 case OPTION_DISABLE_AUTO_IMAGE_BASE:
562 pe_enable_auto_image_base = 0;
563 break;
564 case OPTION_DLL_SEARCH_PREFIX:
565 pe_dll_search_prefix = xstrdup( optarg );
566 break;
567 case OPTION_NO_DEFAULT_EXCLUDES:
568 pe_dll_do_default_excludes = 0;
569 break;
570 #endif
571 }
572 return 1;
573 }
574 \f
575
576 #ifdef DLL_SUPPORT
577 static unsigned long
578 strhash (const char *str)
579 {
580 const unsigned char *s;
581 unsigned long hash;
582 unsigned int c;
583 unsigned int len;
584
585 hash = 0;
586 len = 0;
587 s = (const unsigned char *) str;
588 while ((c = *s++) != '\0')
589 {
590 hash += c + (c << 17);
591 hash ^= hash >> 2;
592 ++len;
593 }
594 hash += len + (len << 17);
595 hash ^= hash >> 2;
596
597 return hash;
598 }
599
600 /* Use the output file to create a image base for relocatable DLLs. */
601 static unsigned long
602 compute_dll_image_base (const char *ofile)
603 {
604 unsigned long hash = strhash (ofile);
605 return 0x60000000 | ((hash << 16) & 0x0FFC0000);
606 }
607 #endif
608
609 /* Assign values to the special symbols before the linker script is
610 read. */
611
612 static void
613 gld_${EMULATION_NAME}_set_symbols ()
614 {
615 /* Run through and invent symbols for all the
616 names and insert the defaults. */
617 int j;
618 lang_statement_list_type *save;
619
620 if (!init[IMAGEBASEOFF].inited)
621 {
622 if (link_info.relocateable)
623 init[IMAGEBASEOFF].value = 0;
624 else if (init[DLLOFF].value || link_info.shared)
625 #ifdef DLL_SUPPORT
626 init[IMAGEBASEOFF].value = (pe_enable_auto_image_base) ?
627 compute_dll_image_base (output_filename) : NT_DLL_IMAGE_BASE;
628 #else
629 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
630 #endif
631 else
632 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
633 }
634
635 /* Don't do any symbol assignments if this is a relocateable link. */
636 if (link_info.relocateable)
637 return;
638
639 /* Glue the assignments into the abs section */
640 save = stat_ptr;
641
642 stat_ptr = &(abs_output_section->children);
643
644 for (j = 0; init[j].ptr; j++)
645 {
646 long val = init[j].value;
647 lang_assignment_statement_type *rv;
648 rv = lang_add_assignment (exp_assop ('=' ,init[j].symbol, exp_intop (val)));
649 if (init[j].size == sizeof(short))
650 *(short *)init[j].ptr = val;
651 else if (init[j].size == sizeof(int))
652 *(int *)init[j].ptr = val;
653 else if (init[j].size == sizeof(long))
654 *(long *)init[j].ptr = val;
655 /* This might be a long long or other special type. */
656 else if (init[j].size == sizeof(bfd_vma))
657 *(bfd_vma *)init[j].ptr = val;
658 else abort();
659 if (j == IMAGEBASEOFF)
660 image_base_statement = rv;
661 }
662 /* Restore the pointer. */
663 stat_ptr = save;
664
665 if (pe.FileAlignment >
666 pe.SectionAlignment)
667 {
668 einfo (_("%P: warning, file alignment > section alignment.\n"));
669 }
670 }
671
672 /* This is called after the linker script and the command line options
673 have been read. */
674
675 static void
676 gld_${EMULATION_NAME}_after_parse ()
677 {
678 /* The Windows libraries are designed for the linker to treat the
679 entry point as an undefined symbol. Otherwise, the .obj that
680 defines mainCRTStartup is brought in because it is the first
681 encountered in libc.lib and it has other symbols in it which will
682 be pulled in by the link process. To avoid this, we act as
683 though the user specified -u with the entry point symbol.
684
685 This function is called after the linker script and command line
686 options have been read, so at this point we know the right entry
687 point. This function is called before the input files are
688 opened, so registering the symbol as undefined will make a
689 difference. */
690
691 if (! link_info.relocateable && entry_symbol != NULL)
692 ldlang_add_undef (entry_symbol);
693 }
694
695 #ifdef DLL_SUPPORT
696 static struct bfd_link_hash_entry *pe_undef_found_sym;
697
698 static boolean
699 pe_undef_cdecl_match (h, string)
700 struct bfd_link_hash_entry *h;
701 PTR string;
702 {
703 int sl = strlen (string);
704 if (h->type == bfd_link_hash_defined
705 && strncmp (h->root.string, string, sl) == 0
706 && h->root.string[sl] == '@')
707 {
708 pe_undef_found_sym = h;
709 return false;
710 }
711 return true;
712 }
713
714 static void
715 pe_fixup_stdcalls ()
716 {
717 static int gave_warning_message = 0;
718 struct bfd_link_hash_entry *undef, *sym;
719 char *at;
720 for (undef = link_info.hash->undefs; undef; undef=undef->next)
721 if (undef->type == bfd_link_hash_undefined)
722 {
723 at = strchr (undef->root.string, '@');
724 if (at)
725 {
726 /* The symbol is a stdcall symbol, so let's look for a cdecl
727 symbol with the same name and resolve to that */
728 char *cname = xstrdup (undef->root.string);
729 at = strchr (cname, '@');
730 *at = 0;
731 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
732 if (sym && sym->type == bfd_link_hash_defined)
733 {
734 undef->type = bfd_link_hash_defined;
735 undef->u.def.value = sym->u.def.value;
736 undef->u.def.section = sym->u.def.section;
737 if (pe_enable_stdcall_fixup == -1)
738 {
739 einfo (_("Warning: resolving %s by linking to %s\n"),
740 undef->root.string, cname);
741 if (! gave_warning_message)
742 {
743 gave_warning_message = 1;
744 einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
745 einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
746 }
747 }
748 }
749 }
750 else
751 {
752 /* The symbol is a cdecl symbol, so we look for stdcall
753 symbols - which means scanning the whole symbol table */
754 pe_undef_found_sym = 0;
755 bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
756 (PTR) undef->root.string);
757 sym = pe_undef_found_sym;
758 if (sym)
759 {
760 undef->type = bfd_link_hash_defined;
761 undef->u.def.value = sym->u.def.value;
762 undef->u.def.section = sym->u.def.section;
763 if (pe_enable_stdcall_fixup == -1)
764 {
765 einfo (_("Warning: resolving %s by linking to %s\n"),
766 undef->root.string, sym->root.string);
767 if (! gave_warning_message)
768 {
769 gave_warning_message = 1;
770 einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
771 einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
772 }
773 }
774 }
775 }
776 }
777 }
778 #endif /* DLL_SUPPORT */
779
780 static void
781 gld_${EMULATION_NAME}_after_open ()
782 {
783 /* Pass the wacky PE command line options into the output bfd.
784 FIXME: This should be done via a function, rather than by
785 including an internal BFD header. */
786
787 if (!coff_data (output_bfd)->pe)
788 einfo (_("%F%P: PE operations on non PE file.\n"));
789
790 pe_data (output_bfd)->pe_opthdr = pe;
791 pe_data (output_bfd)->dll = init[DLLOFF].value;
792
793 #ifdef DLL_SUPPORT
794 if (pe_enable_stdcall_fixup) /* -1=warn or 1=disable */
795 pe_fixup_stdcalls ();
796
797 pe_process_import_defs(output_bfd, &link_info);
798 if (link_info.shared)
799 pe_dll_build_sections (output_bfd, &link_info);
800
801 #ifndef TARGET_IS_i386pe
802 #ifndef TARGET_IS_armpe
803 else
804 pe_exe_build_sections (output_bfd, &link_info);
805 #endif
806 #endif
807 #endif
808
809 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
810 if (strstr (bfd_get_target (output_bfd), "arm") == NULL)
811 {
812 /* The arm backend needs special fields in the output hash structure.
813 These will only be created if the output format is an arm format,
814 hence we do not support linking and changing output formats at the
815 same time. Use a link followed by objcopy to change output formats. */
816 einfo ("%F%X%P: error: cannot change output format whilst linking ARM binaries\n");
817 return;
818 }
819 {
820 /* Find a BFD that can hold the interworking stubs. */
821 LANG_FOR_EACH_INPUT_STATEMENT (is)
822 {
823 if (bfd_arm_pe_get_bfd_for_interworking (is->the_bfd, & link_info))
824 break;
825 }
826 }
827 #endif
828
829 {
830 /* This next chunk of code tries to detect the case where you have
831 two import libraries for the same DLL (specifically,
832 symbolically linking libm.a and libc.a in cygwin to
833 libcygwin.a). In those cases, it's possible for function
834 thunks from the second implib to be used but without the
835 head/tail objects, causing an improper import table. We detect
836 those cases and rename the "other" import libraries to match
837 the one the head/tail come from, so that the linker will sort
838 things nicely and produce a valid import table. */
839
840 LANG_FOR_EACH_INPUT_STATEMENT (is)
841 {
842 if (is->the_bfd->my_archive)
843 {
844 int idata2 = 0, reloc_count=0, is_imp = 0;
845 asection *sec;
846
847 /* See if this is an import library thunk. */
848 for (sec = is->the_bfd->sections; sec; sec = sec->next)
849 {
850 if (strcmp (sec->name, ".idata\$2") == 0)
851 idata2 = 1;
852 if (strncmp (sec->name, ".idata\$", 7) == 0)
853 is_imp = 1;
854 reloc_count += sec->reloc_count;
855 }
856
857 if (is_imp && !idata2 && reloc_count)
858 {
859 /* It is, look for the reference to head and see if it's
860 from our own library. */
861 for (sec = is->the_bfd->sections; sec; sec = sec->next)
862 {
863 int i;
864 long symsize;
865 long relsize;
866 asymbol **symbols;
867 arelent **relocs;
868 int nrelocs;
869
870 symsize = bfd_get_symtab_upper_bound (is->the_bfd);
871 if (symsize < 1)
872 break;
873 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
874 if (relsize < 1)
875 break;
876
877 symbols = (asymbol **) xmalloc (symsize);
878 symsize = bfd_canonicalize_symtab (is->the_bfd, symbols);
879 if (symsize < 0)
880 {
881 einfo ("%X%P: unable to process symbols: %E");
882 return;
883 }
884
885 relocs = (arelent **) xmalloc ((size_t) relsize);
886 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
887 relocs, symbols);
888 if (nrelocs < 0)
889 {
890 free (relocs);
891 einfo ("%X%P: unable to process relocs: %E");
892 return;
893 }
894
895 for (i = 0; i < nrelocs; i++)
896 {
897 struct symbol_cache_entry *s;
898 struct bfd_link_hash_entry * blhe;
899 bfd *other_bfd;
900 char *n;
901
902 s = (relocs[i]->sym_ptr_ptr)[0];
903
904 if (s->flags & BSF_LOCAL)
905 continue;
906
907 /* Thunk section with reloc to another bfd. */
908 blhe = bfd_link_hash_lookup (link_info.hash,
909 s->name,
910 false, false, true);
911
912 if (blhe == NULL
913 || blhe->type != bfd_link_hash_defined)
914 continue;
915
916 other_bfd = blhe->u.def.section->owner;
917
918 if (strcmp (is->the_bfd->my_archive->filename,
919 other_bfd->my_archive->filename) == 0)
920 continue;
921
922 /* Rename this implib to match the other. */
923 n = (char *) xmalloc (strlen (other_bfd->my_archive->filename) + 1);
924
925 strcpy (n, other_bfd->my_archive->filename);
926
927 is->the_bfd->my_archive->filename = n;
928 }
929
930 free (relocs);
931 /* Note - we do not free the symbols,
932 they are now cached in the BFD. */
933 }
934 }
935 }
936 }
937 }
938
939 {
940 int is_ms_arch = 0;
941 bfd *cur_arch = 0;
942 lang_input_statement_type *is2;
943
944 /* Careful - this is a shell script. Watch those dollar signs! */
945 /* Microsoft import libraries have every member named the same,
946 and not in the right order for us to link them correctly. We
947 must detect these and rename the members so that they'll link
948 correctly. There are three types of objects: the head, the
949 thunks, and the sentinel(s). The head is easy; it's the one
950 with idata2. We assume that the sentinels won't have relocs,
951 and the thunks will. It's easier than checking the symbol
952 table for external references. */
953 LANG_FOR_EACH_INPUT_STATEMENT (is)
954 {
955 if (is->the_bfd->my_archive)
956 {
957 bfd *arch = is->the_bfd->my_archive;
958 if (cur_arch != arch)
959 {
960 cur_arch = arch;
961 is_ms_arch = 1;
962 for (is2 = is;
963 is2 && is2->the_bfd->my_archive == arch;
964 is2 = (lang_input_statement_type *)is2->next)
965 {
966 if (strcmp (is->the_bfd->filename, is2->the_bfd->filename))
967 is_ms_arch = 0;
968 }
969 }
970
971 if (is_ms_arch)
972 {
973 int idata2 = 0, reloc_count=0;
974 asection *sec;
975 char *new_name, seq;
976
977 for (sec = is->the_bfd->sections; sec; sec = sec->next)
978 {
979 if (strcmp (sec->name, ".idata\$2") == 0)
980 idata2 = 1;
981 reloc_count += sec->reloc_count;
982 }
983
984 if (idata2) /* .idata2 is the TOC */
985 seq = 'a';
986 else if (reloc_count > 0) /* thunks */
987 seq = 'b';
988 else /* sentinel */
989 seq = 'c';
990
991 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
992 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
993 is->the_bfd->filename = new_name;
994
995 new_name = xmalloc (strlen (is->filename) + 3);
996 sprintf (new_name, "%s.%c", is->filename, seq);
997 is->filename = new_name;
998 }
999 }
1000 }
1001 }
1002 }
1003 \f
1004 static void
1005 gld_${EMULATION_NAME}_before_allocation()
1006 {
1007 #ifdef TARGET_IS_ppcpe
1008 /* Here we rummage through the found bfds to collect toc information */
1009 {
1010 LANG_FOR_EACH_INPUT_STATEMENT (is)
1011 {
1012 if (!ppc_process_before_allocation (is->the_bfd, &link_info))
1013 {
1014 /* xgettext:c-format */
1015 einfo (_("Errors encountered processing file %s\n"), is->filename);
1016 }
1017 }
1018 }
1019
1020 /* We have seen it all. Allocate it, and carry on */
1021 ppc_allocate_toc_section (&link_info);
1022 #endif /* TARGET_IS_ppcpe */
1023
1024 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
1025 /* FIXME: we should be able to set the size of the interworking stub
1026 section.
1027
1028 Here we rummage through the found bfds to collect glue
1029 information. FIXME: should this be based on a command line
1030 option? krk@cygnus.com */
1031 {
1032 LANG_FOR_EACH_INPUT_STATEMENT (is)
1033 {
1034 if (! bfd_arm_pe_process_before_allocation
1035 (is->the_bfd, & link_info, support_old_code))
1036 {
1037 /* xgettext:c-format */
1038 einfo (_("Errors encountered processing file %s for interworking"),
1039 is->filename);
1040 }
1041 }
1042 }
1043
1044 /* We have seen it all. Allocate it, and carry on */
1045 bfd_arm_pe_allocate_interworking_sections (& link_info);
1046 #endif /* TARGET_IS_armpe */
1047 }
1048 \f
1049 #ifdef DLL_SUPPORT
1050 /* This is called when an input file isn't recognized as a BFD. We
1051 check here for .DEF files and pull them in automatically. */
1052
1053 static int
1054 saw_option(char *option)
1055 {
1056 int i;
1057 for (i=0; init[i].ptr; i++)
1058 if (strcmp (init[i].symbol, option) == 0)
1059 return init[i].inited;
1060 return 0;
1061 }
1062 #endif /* DLL_SUPPORT */
1063
1064 static boolean
1065 gld_${EMULATION_NAME}_unrecognized_file(entry)
1066 lang_input_statement_type *entry ATTRIBUTE_UNUSED;
1067 {
1068 #ifdef DLL_SUPPORT
1069 const char *ext = entry->filename + strlen (entry->filename) - 4;
1070
1071 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
1072 {
1073 if (pe_def_file == 0)
1074 pe_def_file = def_file_empty ();
1075 def_file_parse (entry->filename, pe_def_file);
1076 if (pe_def_file)
1077 {
1078 int i, buflen=0, len;
1079 char *buf;
1080 for (i=0; i<pe_def_file->num_exports; i++)
1081 {
1082 len = strlen(pe_def_file->exports[i].internal_name);
1083 if (buflen < len+2)
1084 buflen = len+2;
1085 }
1086 buf = (char *) xmalloc (buflen);
1087 for (i=0; i<pe_def_file->num_exports; i++)
1088 {
1089 struct bfd_link_hash_entry *h;
1090 sprintf(buf, "_%s", pe_def_file->exports[i].internal_name);
1091
1092 h = bfd_link_hash_lookup (link_info.hash, buf, true, true, true);
1093 if (h == (struct bfd_link_hash_entry *) NULL)
1094 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1095 if (h->type == bfd_link_hash_new)
1096 {
1097 h->type = bfd_link_hash_undefined;
1098 h->u.undef.abfd = NULL;
1099 bfd_link_add_undef (link_info.hash, h);
1100 }
1101 }
1102 free (buf);
1103
1104 /* def_file_print (stdout, pe_def_file); */
1105 if (pe_def_file->is_dll == 1)
1106 link_info.shared = 1;
1107
1108 if (pe_def_file->base_address != (bfd_vma)(-1))
1109 {
1110 pe.ImageBase =
1111 pe_data (output_bfd)->pe_opthdr.ImageBase =
1112 init[IMAGEBASEOFF].value = pe_def_file->base_address;
1113 init[IMAGEBASEOFF].inited = 1;
1114 if (image_base_statement)
1115 image_base_statement->exp =
1116 exp_assop ('=', "__image_base__", exp_intop (pe.ImageBase));
1117 }
1118
1119 #if 0
1120 /* Not sure if these *should* be set */
1121 if (pe_def_file->version_major != -1)
1122 {
1123 pe.MajorImageVersion = pe_def_file->version_major;
1124 pe.MinorImageVersion = pe_def_file->version_minor;
1125 }
1126 #endif
1127 if (pe_def_file->stack_reserve != -1
1128 && ! saw_option ("__size_of_stack_reserve__"))
1129 {
1130 pe.SizeOfStackReserve = pe_def_file->stack_reserve;
1131 if (pe_def_file->stack_commit != -1)
1132 pe.SizeOfStackCommit = pe_def_file->stack_commit;
1133 }
1134 if (pe_def_file->heap_reserve != -1
1135 && ! saw_option ("__size_of_heap_reserve__"))
1136 {
1137 pe.SizeOfHeapReserve = pe_def_file->heap_reserve;
1138 if (pe_def_file->heap_commit != -1)
1139 pe.SizeOfHeapCommit = pe_def_file->heap_commit;
1140 }
1141 return true;
1142 }
1143 }
1144 #endif
1145 return false;
1146
1147 }
1148
1149 static boolean
1150 gld_${EMULATION_NAME}_recognized_file(entry)
1151 lang_input_statement_type *entry ATTRIBUTE_UNUSED;
1152 {
1153 #ifdef DLL_SUPPORT
1154 #ifdef TARGET_IS_i386pe
1155 pe_dll_id_target ("pei-i386");
1156 #endif
1157 #ifdef TARGET_IS_shpe
1158 pe_dll_id_target ("pei-shl");
1159 #endif
1160 #ifdef TARGET_IS_mipspe
1161 pe_dll_id_target ("pei-mips");
1162 #endif
1163 #ifdef TARGET_IS_armpe
1164 pe_dll_id_target ("pei-arm-little");
1165 #endif
1166 if (bfd_get_format (entry->the_bfd) == bfd_object)
1167 {
1168 const char *ext = entry->filename + strlen (entry->filename) - 4;
1169 if (strcmp (ext, ".dll") == 0 || strcmp (ext, ".DLL") == 0)
1170 return pe_implied_import_dll (entry->filename);
1171 }
1172 #endif
1173 return false;
1174 }
1175
1176 static void
1177 gld_${EMULATION_NAME}_finish ()
1178 {
1179 #if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
1180 struct bfd_link_hash_entry * h;
1181
1182 if (thumb_entry_symbol != NULL)
1183 {
1184 h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol, false, false, true);
1185
1186 if (h != (struct bfd_link_hash_entry *) NULL
1187 && (h->type == bfd_link_hash_defined
1188 || h->type == bfd_link_hash_defweak)
1189 && h->u.def.section->output_section != NULL)
1190 {
1191 static char buffer[32];
1192 bfd_vma val;
1193
1194 /* Special procesing is required for a Thumb entry symbol. The
1195 bottom bit of its address must be set. */
1196 val = (h->u.def.value
1197 + bfd_get_section_vma (output_bfd,
1198 h->u.def.section->output_section)
1199 + h->u.def.section->output_offset);
1200
1201 val |= 1;
1202
1203 /* Now convert this value into a string and store it in entry_symbol
1204 where the lang_finish() function will pick it up. */
1205 buffer[0] = '0';
1206 buffer[1] = 'x';
1207
1208 sprintf_vma (buffer + 2, val);
1209
1210 if (entry_symbol != NULL && entry_from_cmdline)
1211 einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
1212 thumb_entry_symbol, entry_symbol);
1213 entry_symbol = buffer;
1214 }
1215 else
1216 einfo (_("%P: warning: connot find thumb start symbol %s\n"), thumb_entry_symbol);
1217 }
1218 #endif /* defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe) */
1219
1220 #ifdef DLL_SUPPORT
1221 if (link_info.shared)
1222 {
1223 pe_dll_fill_sections (output_bfd, &link_info);
1224 if (pe_implib_filename)
1225 pe_dll_generate_implib (pe_def_file, pe_implib_filename);
1226 }
1227 #if defined(TARGET_IS_shpe) || defined(TARGET_IS_mipspe)
1228 /* ARM doesn't need relocs. */
1229 else
1230 {
1231 pe_exe_fill_sections (output_bfd, &link_info);
1232 }
1233 #endif
1234
1235 if (pe_out_def_filename)
1236 pe_dll_generate_def_file (pe_out_def_filename);
1237 #endif /* DLL_SUPPORT */
1238 }
1239
1240 \f
1241 /* Place an orphan section.
1242
1243 We use this to put sections in a reasonable place in the file, and
1244 to ensure that they are aligned as required.
1245
1246 We handle grouped sections here as well. A section named .foo$nn
1247 goes into the output section .foo. All grouped sections are sorted
1248 by name.
1249
1250 Grouped sections for the default sections are handled by the
1251 default linker script using wildcards, and are sorted by
1252 sort_sections. */
1253
1254 struct orphan_save
1255 {
1256 lang_output_section_statement_type *os;
1257 asection **section;
1258 lang_statement_union_type **stmt;
1259 };
1260
1261 /*ARGSUSED*/
1262 static boolean
1263 gld_${EMULATION_NAME}_place_orphan (file, s)
1264 lang_input_statement_type *file;
1265 asection *s;
1266 {
1267 const char *secname;
1268 char *hold_section_name;
1269 char *dollar = NULL;
1270 const char *ps = NULL;
1271 lang_output_section_statement_type *os;
1272 lang_statement_list_type add_child;
1273
1274 secname = bfd_get_section_name (s->owner, s);
1275
1276 /* Look through the script to see where to place this section. */
1277
1278 hold_section_name = xstrdup (secname);
1279 if (!link_info.relocateable)
1280 {
1281 dollar = strchr (hold_section_name, '$');
1282 if (dollar != NULL)
1283 *dollar = '\0';
1284 }
1285
1286 os = lang_output_section_find (hold_section_name);
1287
1288 lang_list_init (&add_child);
1289
1290 if (os != NULL
1291 && os->bfd_section != NULL
1292 && ((s->flags ^ os->bfd_section->flags) & (SEC_LOAD | SEC_ALLOC)) == 0)
1293 {
1294 wild_doit (&add_child, s, os, file);
1295 }
1296 else
1297 {
1298 struct orphan_save *place;
1299 static struct orphan_save hold_text;
1300 static struct orphan_save hold_rdata;
1301 static struct orphan_save hold_data;
1302 static struct orphan_save hold_bss;
1303 char *outsecname;
1304 lang_statement_list_type *old;
1305 lang_statement_list_type add;
1306 etree_type *address;
1307
1308 /* Try to put the new output section in a reasonable place based
1309 on the section name and section flags. */
1310 #define HAVE_SECTION(hold, name) \
1311 (hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
1312
1313 place = NULL;
1314 if ((s->flags & SEC_ALLOC) == 0)
1315 ;
1316 else if ((s->flags & SEC_HAS_CONTENTS) == 0
1317 && HAVE_SECTION (hold_bss, ".bss"))
1318 place = &hold_bss;
1319 else if ((s->flags & SEC_READONLY) == 0
1320 && HAVE_SECTION (hold_data, ".data"))
1321 place = &hold_data;
1322 else if ((s->flags & SEC_CODE) == 0
1323 && (s->flags & SEC_READONLY) != 0
1324 && HAVE_SECTION (hold_rdata, ".rdata"))
1325 place = &hold_rdata;
1326 else if ((s->flags & SEC_READONLY) != 0
1327 && HAVE_SECTION (hold_text, ".text"))
1328 place = &hold_text;
1329
1330 #undef HAVE_SECTION
1331
1332 /* Choose a unique name for the section. This will be needed if
1333 the same section name appears in the input file with
1334 different loadable or allocateable characteristics. */
1335 outsecname = xstrdup (hold_section_name);
1336 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
1337 {
1338 unsigned int len;
1339 char *newname;
1340 unsigned int i;
1341
1342 len = strlen (outsecname);
1343 newname = xmalloc (len + 5);
1344 strcpy (newname, outsecname);
1345 i = 0;
1346 do
1347 {
1348 sprintf (newname + len, "%d", i);
1349 ++i;
1350 }
1351 while (bfd_get_section_by_name (output_bfd, newname) != NULL);
1352
1353 free (outsecname);
1354 outsecname = newname;
1355 }
1356
1357 /* Start building a list of statements for this section. */
1358 old = stat_ptr;
1359 stat_ptr = &add;
1360 lang_list_init (stat_ptr);
1361
1362 if (config.build_constructors)
1363 {
1364 /* If the name of the section is representable in C, then create
1365 symbols to mark the start and the end of the section. */
1366 for (ps = outsecname; *ps != '\0'; ps++)
1367 if (! isalnum ((unsigned char) *ps) && *ps != '_')
1368 break;
1369 if (*ps == '\0')
1370 {
1371 char *symname;
1372 etree_type *e_align;
1373
1374 symname = (char *) xmalloc (ps - outsecname + sizeof "___start_");
1375 sprintf (symname, "___start_%s", outsecname);
1376 e_align = exp_unop (ALIGN_K,
1377 exp_intop ((bfd_vma) 1 << s->alignment_power));
1378 lang_add_assignment (exp_assop ('=', symname, e_align));
1379 }
1380 }
1381
1382 if (link_info.relocateable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1383 address = exp_intop ((bfd_vma) 0);
1384 else
1385 {
1386 /* All sections in an executable must be aligned to a page
1387 boundary. */
1388 address = exp_unop (ALIGN_K,
1389 exp_nameop (NAME, "__section_alignment__"));
1390 }
1391
1392 os = lang_enter_output_section_statement (outsecname, address, 0,
1393 (bfd_vma) 0,
1394 (etree_type *) NULL,
1395 (etree_type *) NULL,
1396 (etree_type *) NULL);
1397
1398 wild_doit (&add_child, s, os, file);
1399
1400 lang_leave_output_section_statement
1401 ((bfd_vma) 0, "*default*",
1402 (struct lang_output_section_phdr_list *) NULL, "*default*");
1403
1404 if (config.build_constructors && *ps == '\0')
1405 {
1406 char *symname;
1407
1408 /* lang_leave_ouput_section_statement resets stat_ptr. Put
1409 stat_ptr back where we want it. */
1410 if (place != NULL)
1411 stat_ptr = &add;
1412
1413 symname = (char *) xmalloc (ps - outsecname + sizeof "___stop_");
1414 sprintf (symname, "___stop_%s", outsecname);
1415 lang_add_assignment (exp_assop ('=', symname,
1416 exp_nameop (NAME, ".")));
1417 }
1418
1419 stat_ptr = old;
1420
1421 if (place != NULL)
1422 {
1423 asection *snew, **pps;
1424
1425 snew = os->bfd_section;
1426 if (place->os->bfd_section != NULL || place->section != NULL)
1427 {
1428 /* Shuffle the section to make the output file look neater. */
1429 if (place->section == NULL)
1430 {
1431 #if 0
1432 /* Finding the end of the list is a little tricky. We
1433 make a wild stab at it by comparing section flags. */
1434 flagword first_flags = place->os->bfd_section->flags;
1435 for (pps = &place->os->bfd_section->next;
1436 *pps != NULL && (*pps)->flags == first_flags;
1437 pps = &(*pps)->next)
1438 ;
1439 place->section = pps;
1440 #else
1441 /* Put orphans after the first section on the list. */
1442 place->section = &place->os->bfd_section->next;
1443 #endif
1444 }
1445
1446 /* Unlink the section. */
1447 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1448 ;
1449 *pps = snew->next;
1450
1451 /* Now tack it on to the "place->os" section list. */
1452 snew->next = *place->section;
1453 *place->section = snew;
1454 }
1455 place->section = &snew->next; /* Save the end of this list. */
1456
1457 if (place->stmt == NULL)
1458 {
1459 /* Put the new statement list right at the head. */
1460 *add.tail = place->os->header.next;
1461 place->os->header.next = add.head;
1462 }
1463 else
1464 {
1465 /* Put it after the last orphan statement we added. */
1466 *add.tail = *place->stmt;
1467 *place->stmt = add.head;
1468 }
1469 place->stmt = add.tail; /* Save the end of this list. */
1470 }
1471 }
1472
1473 {
1474 lang_statement_union_type **pl = &os->children.head;
1475
1476 if (dollar != NULL)
1477 {
1478 boolean found_dollar;
1479
1480 /* The section name has a '$'. Sort it with the other '$'
1481 sections. */
1482
1483 found_dollar = false;
1484 for ( ; *pl != NULL; pl = &(*pl)->next)
1485 {
1486 lang_input_section_type *ls;
1487 const char *lname;
1488
1489 if ((*pl)->header.type != lang_input_section_enum)
1490 continue;
1491
1492 ls = &(*pl)->input_section;
1493
1494 lname = bfd_get_section_name (ls->ifile->the_bfd, ls->section);
1495 if (strchr (lname, '$') == NULL)
1496 {
1497 if (found_dollar)
1498 break;
1499 }
1500 else
1501 {
1502 found_dollar = true;
1503 if (strcmp (secname, lname) < 0)
1504 break;
1505 }
1506 }
1507 }
1508
1509 if (add_child.head != NULL)
1510 {
1511 add_child.head->next = *pl;
1512 *pl = add_child.head;
1513 }
1514 }
1515
1516 free (hold_section_name);
1517
1518 return true;
1519 }
1520
1521 static boolean
1522 gld_${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
1523 const char * arch ATTRIBUTE_UNUSED;
1524 search_dirs_type * search;
1525 lang_input_statement_type * entry;
1526 {
1527 const char * filename;
1528 char * string;
1529
1530 if (! entry->is_archive)
1531 return false;
1532
1533 filename = entry->filename;
1534
1535 string = (char *) xmalloc (strlen (search->name)
1536 + strlen (filename)
1537 + sizeof "/lib.a.dll"
1538 #ifdef DLL_SUPPORT
1539 + (pe_dll_search_prefix ? strlen (pe_dll_search_prefix) : 0)
1540 #endif
1541 + 1);
1542
1543 /* Try "libfoo.dll.a" first (preferred explicit import library for dll's */
1544 sprintf (string, "%s/lib%s.dll.a", search->name, filename);
1545
1546 if (! ldfile_try_open_bfd (string, entry))
1547 {
1548 /* Try "foo.dll.a" next (alternate explicit import library for dll's */
1549 sprintf (string, "%s/%s.dll.a", search->name, filename);
1550 if (! ldfile_try_open_bfd (string, entry))
1551 {
1552 /*
1553 Try libfoo.a next. Normally, this would be interpreted as a static
1554 library, but it *could* be an import library. For backwards compatibility,
1555 libfoo.a needs to ==precede== libfoo.dll and foo.dll in the search,
1556 or sometimes errors occur when building legacy packages.
1557
1558 Putting libfoo.a here means that in a failure case (i.e. the library
1559 -lfoo is not found) we will search for libfoo.a twice before
1560 giving up -- once here, and once when searching for a "static" lib.
1561 for a "static" lib.
1562 */
1563 /* Try "libfoo.a" (import lib, or static lib, but must
1564 take precedence over dll's) */
1565 sprintf (string, "%s/lib%s.a", search->name, filename);
1566 if (! ldfile_try_open_bfd (string, entry))
1567 {
1568 #ifdef DLL_SUPPORT
1569 if (pe_dll_search_prefix)
1570 {
1571 /* Try "<prefix>foo.dll" (preferred dll name, if specified) */
1572 sprintf (string, "%s/%s%s.dll", search->name, pe_dll_search_prefix, filename);
1573 if (! ldfile_try_open_bfd (string, entry))
1574 {
1575 /* Try "libfoo.dll" (default preferred dll name) */
1576 sprintf (string, "%s/lib%s.dll", search->name, filename);
1577 if (! ldfile_try_open_bfd (string, entry))
1578 {
1579 /* Finally, try "foo.dll" (alternate dll name) */
1580 sprintf (string, "%s/%s.dll", search->name, filename);
1581 if (! ldfile_try_open_bfd (string, entry))
1582 {
1583 free (string);
1584 return false;
1585 }
1586 }
1587 }
1588 }
1589 else /* pe_dll_search_prefix not specified */
1590 #endif
1591 {
1592 /* Try "libfoo.dll" (preferred dll name) */
1593 sprintf (string, "%s/lib%s.dll", search->name, filename);
1594 if (! ldfile_try_open_bfd (string, entry))
1595 {
1596 /* Finally, try "foo.dll" (alternate dll name) */
1597 sprintf (string, "%s/%s.dll", search->name, filename);
1598 if (! ldfile_try_open_bfd (string, entry))
1599 {
1600 free (string);
1601 return false;
1602 }
1603 }
1604 }
1605 }
1606 }
1607 }
1608
1609 entry->filename = string;
1610
1611 return true;
1612 }
1613
1614 static int
1615 gld_${EMULATION_NAME}_find_potential_libraries (name, entry)
1616 char * name;
1617 lang_input_statement_type * entry;
1618 {
1619 return ldfile_open_file_search (name, entry, "", ".lib");
1620 }
1621 \f
1622 static char *
1623 gld_${EMULATION_NAME}_get_script(isfile)
1624 int *isfile;
1625 EOF
1626 # Scripts compiled in.
1627 # sed commands to quote an ld script as a C string.
1628 sc="-f stringify.sed"
1629
1630 cat >>e${EMULATION_NAME}.c <<EOF
1631 {
1632 *isfile = 0;
1633
1634 if (link_info.relocateable == true && config.build_constructors == true)
1635 return
1636 EOF
1637 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1638 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1639 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1640 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1641 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1642 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1643 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1644 echo ' ; else return' >> e${EMULATION_NAME}.c
1645 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1646 echo '; }' >> e${EMULATION_NAME}.c
1647
1648 cat >>e${EMULATION_NAME}.c <<EOF
1649
1650
1651 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1652 {
1653 gld_${EMULATION_NAME}_before_parse,
1654 syslib_default,
1655 hll_default,
1656 gld_${EMULATION_NAME}_after_parse,
1657 gld_${EMULATION_NAME}_after_open,
1658 after_allocation_default,
1659 set_output_arch_default,
1660 ldemul_default_target,
1661 gld_${EMULATION_NAME}_before_allocation,
1662 gld_${EMULATION_NAME}_get_script,
1663 "${EMULATION_NAME}",
1664 "${OUTPUT_FORMAT}",
1665 gld_${EMULATION_NAME}_finish, /* finish */
1666 NULL, /* create output section statements */
1667 gld_${EMULATION_NAME}_open_dynamic_archive,
1668 gld_${EMULATION_NAME}_place_orphan,
1669 gld_${EMULATION_NAME}_set_symbols,
1670 gld_${EMULATION_NAME}_parse_args,
1671 gld_${EMULATION_NAME}_unrecognized_file,
1672 gld_${EMULATION_NAME}_list_options,
1673 gld_${EMULATION_NAME}_recognized_file,
1674 gld_${EMULATION_NAME}_find_potential_libraries
1675 };
1676 EOF
This page took 0.064157 seconds and 4 git commands to generate.