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