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