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