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