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