* emultempl/pe.em (set_pe_subsystem): Don't call
[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
46 #define TARGET_IS_${EMULATION_NAME}
47
48 static void gld_${EMULATION_NAME}_set_symbols PARAMS ((void));
49 static void gld_${EMULATION_NAME}_after_open PARAMS ((void));
50 static void gld_${EMULATION_NAME}_before_parse PARAMS ((void));
51 static void gld_${EMULATION_NAME}_after_parse PARAMS ((void));
52 static void gld_${EMULATION_NAME}_before_allocation PARAMS ((void));
53 static boolean gld_${EMULATION_NAME}_place_orphan
54 PARAMS ((lang_input_statement_type *, asection *));
55 static char *gld_${EMULATION_NAME}_get_script PARAMS ((int *));
56 static int gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **));
57
58 #if 0 /* argument to qsort so don't prototype */
59 static int sort_by_file_name PARAMS ((void *, void *));
60 static int sort_by_section_name PARAMS ((void *, void *));
61 #endif
62 static lang_statement_union_type **sort_sections_1
63 PARAMS ((lang_statement_union_type **, lang_statement_union_type *, int,
64 int (*)()));
65 static void sort_sections PARAMS ((lang_statement_union_type *));
66
67 static struct internal_extra_pe_aouthdr pe;
68 static int dll;
69 static int support_old_code = 0;
70
71 extern const char *output_filename;
72
73 static void
74 gld_${EMULATION_NAME}_before_parse()
75 {
76 output_filename = "a.exe";
77 ldfile_output_architecture = bfd_arch_${ARCH};
78 }
79 \f
80 /* PE format extra command line options. */
81
82 /* Used for setting flags in the PE header. */
83 #define OPTION_BASE_FILE (300 + 1)
84 #define OPTION_DLL (OPTION_BASE_FILE + 1)
85 #define OPTION_FILE_ALIGNMENT (OPTION_DLL + 1)
86 #define OPTION_IMAGE_BASE (OPTION_FILE_ALIGNMENT + 1)
87 #define OPTION_MAJOR_IMAGE_VERSION (OPTION_IMAGE_BASE + 1)
88 #define OPTION_MAJOR_OS_VERSION (OPTION_MAJOR_IMAGE_VERSION + 1)
89 #define OPTION_MAJOR_SUBSYSTEM_VERSION (OPTION_MAJOR_OS_VERSION + 1)
90 #define OPTION_MINOR_IMAGE_VERSION (OPTION_MAJOR_SUBSYSTEM_VERSION + 1)
91 #define OPTION_MINOR_OS_VERSION (OPTION_MINOR_IMAGE_VERSION + 1)
92 #define OPTION_MINOR_SUBSYSTEM_VERSION (OPTION_MINOR_OS_VERSION + 1)
93 #define OPTION_SECTION_ALIGNMENT (OPTION_MINOR_SUBSYSTEM_VERSION + 1)
94 #define OPTION_STACK (OPTION_SECTION_ALIGNMENT + 1)
95 #define OPTION_SUBSYSTEM (OPTION_STACK + 1)
96 #define OPTION_HEAP (OPTION_SUBSYSTEM + 1)
97 #define OPTION_SUPPORT_OLD_CODE (OPTION_HEAP + 1)
98
99 static struct option longopts[] =
100 {
101 /* PE options */
102 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
103 {"dll", no_argument, NULL, OPTION_DLL},
104 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
105 {"heap", required_argument, NULL, OPTION_HEAP},
106 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
107 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
108 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
109 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
110 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
111 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
112 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
113 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
114 {"stack", required_argument, NULL, OPTION_STACK},
115 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
116 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
117 {NULL, no_argument, NULL, 0}
118 };
119
120
121 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
122 parameters which may be input from the command line */
123
124 typedef struct
125 {
126 void *ptr;
127 int size;
128 int value;
129 char *symbol;
130 int inited;
131 } definfo;
132
133 #define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol,0}
134
135 static definfo init[] =
136 {
137 /* imagebase must be first */
138 #define IMAGEBASEOFF 0
139 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
140 #define DLLOFF 1
141 {&dll, sizeof(dll), 0, "__dll__"},
142 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
143 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
144 D(MajorOperatingSystemVersion,"__major_os_version__", 4),
145 D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
146 D(MajorImageVersion,"__major_image_version__", 1),
147 D(MinorImageVersion,"__minor_image_version__", 0),
148 D(MajorSubsystemVersion,"__major_subsystem_version__", 4),
149 D(MinorSubsystemVersion,"__minor_subsystem_version__", 0),
150 D(Subsystem,"__subsystem__", 3),
151 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x2000000),
152 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
153 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
154 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
155 D(LoaderFlags,"__loader_flags__", 0x0),
156 { NULL, 0, 0, NULL, 0 }
157 };
158
159 static void
160 gld_${EMULATION_NAME}_list_options (file)
161 FILE * file;
162 {
163 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
164 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
165 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
166 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
167 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
168 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
169 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
170 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
171 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
172 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
173 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
174 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
175 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
176 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
177 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
178 }
179
180 static void
181 set_pe_name (name, val)
182 char *name;
183 long val;
184 {
185 int i;
186 /* Find the name and set it. */
187 for (i = 0; init[i].ptr; i++)
188 {
189 if (strcmp (name, init[i].symbol) == 0)
190 {
191 init[i].value = val;
192 init[i].inited = 1;
193 return;
194 }
195 }
196 abort();
197 }
198
199
200 static void
201 set_pe_subsystem ()
202 {
203 const char *sver;
204 int len;
205 int i;
206 static const struct
207 {
208 const char *name;
209 const int value;
210 const char *entry;
211 }
212 v[] =
213 {
214 { "native", 1, "_NtProcessStartup" },
215 { "windows", 2, "_WinMainCRTStartup" },
216 { "console", 3, "_mainCRTStartup" },
217 #if 0
218 /* The Microsoft linker does not recognize this. */
219 { "os2", 5, "" },
220 #endif
221 { "posix", 7, "___PosixProcessStartup"},
222 { 0, 0, 0 }
223 };
224
225 sver = strchr (optarg, ':');
226 if (sver == NULL)
227 len = strlen (optarg);
228 else
229 {
230 char *end;
231
232 len = sver - optarg;
233 set_pe_name ("__major_subsystem_version__",
234 strtoul (sver + 1, &end, 0));
235 if (*end == '.')
236 set_pe_name ("__minor_subsystem_version__",
237 strtoul (end + 1, &end, 0));
238 if (*end != '\0')
239 einfo (_("%P: warning: bad version number in -subsystem option\n"));
240 }
241
242 for (i = 0; v[i].name; i++)
243 {
244 if (strncmp (optarg, v[i].name, len) == 0
245 && v[i].name[len] == '\0')
246 {
247 set_pe_name ("__subsystem__", v[i].value);
248
249 lang_add_entry (v[i].entry, 1);
250
251 return;
252 }
253 }
254
255 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
256 }
257
258
259
260 static void
261 set_pe_value (name)
262 char *name;
263
264 {
265 char *end;
266
267 set_pe_name (name, strtoul (optarg, &end, 0));
268
269 if (end == optarg)
270 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
271
272 optarg = end;
273 }
274
275 static void
276 set_pe_stack_heap (resname, comname)
277 char *resname;
278 char *comname;
279 {
280 set_pe_value (resname);
281
282 if (*optarg == ',')
283 {
284 optarg++;
285 set_pe_value (comname);
286 }
287 else if (*optarg)
288 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
289 }
290
291
292
293 static int
294 gld_${EMULATION_NAME}_parse_args(argc, argv)
295 int argc;
296 char **argv;
297 {
298 int longind;
299 int optc;
300 int prevoptind = optind;
301 int prevopterr = opterr;
302 int wanterror;
303 static int lastoptind = -1;
304
305 if (lastoptind != optind)
306 opterr = 0;
307 wanterror = opterr;
308
309 lastoptind = optind;
310
311 optc = getopt_long_only (argc, argv, "-", longopts, &longind);
312 opterr = prevopterr;
313
314 switch (optc)
315 {
316 default:
317 if (wanterror)
318 xexit (1);
319 optind = prevoptind;
320 return 0;
321
322 case OPTION_BASE_FILE:
323 link_info.base_file = (PTR) fopen (optarg, FOPEN_WB);
324 if (link_info.base_file == NULL)
325 {
326 /* xgettext:c-format */
327 fprintf (stderr, _("%s: Can't open base file %s\n"),
328 program_name, optarg);
329 xexit (1);
330 }
331 break;
332
333 /* PE options */
334 case OPTION_HEAP:
335 set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
336 break;
337 case OPTION_STACK:
338 set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
339 break;
340 case OPTION_SUBSYSTEM:
341 set_pe_subsystem ();
342 break;
343 case OPTION_MAJOR_OS_VERSION:
344 set_pe_value ("__major_os_version__");
345 break;
346 case OPTION_MINOR_OS_VERSION:
347 set_pe_value ("__minor_os_version__");
348 break;
349 case OPTION_MAJOR_SUBSYSTEM_VERSION:
350 set_pe_value ("__major_subsystem_version__");
351 break;
352 case OPTION_MINOR_SUBSYSTEM_VERSION:
353 set_pe_value ("__minor_subsystem_version__");
354 break;
355 case OPTION_MAJOR_IMAGE_VERSION:
356 set_pe_value ("__major_image_version__");
357 break;
358 case OPTION_MINOR_IMAGE_VERSION:
359 set_pe_value ("__minor_image_version__");
360 break;
361 case OPTION_FILE_ALIGNMENT:
362 set_pe_value ("__file_alignment__");
363 break;
364 case OPTION_SECTION_ALIGNMENT:
365 set_pe_value ("__section_alignment__");
366 break;
367 case OPTION_DLL:
368 set_pe_name ("__dll__", 1);
369 break;
370 case OPTION_IMAGE_BASE:
371 set_pe_value ("__image_base__");
372 break;
373 case OPTION_SUPPORT_OLD_CODE:
374 support_old_code = 1;
375 break;
376 }
377 return 1;
378 }
379 \f
380 /* Assign values to the special symbols before the linker script is
381 read. */
382
383 static void
384 gld_${EMULATION_NAME}_set_symbols ()
385 {
386 /* Run through and invent symbols for all the
387 names and insert the defaults. */
388 int j;
389 lang_statement_list_type *save;
390
391 if (!init[IMAGEBASEOFF].inited)
392 {
393 if (link_info.relocateable)
394 init[IMAGEBASEOFF].value = 0;
395 else if (init[DLLOFF].value)
396 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
397 else
398 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
399 }
400
401 /* Don't do any symbol assignments if this is a relocateable link. */
402 if (link_info.relocateable)
403 return;
404
405 /* Glue the assignments into the abs section */
406 save = stat_ptr;
407
408 stat_ptr = &(abs_output_section->children);
409
410 for (j = 0; init[j].ptr; j++)
411 {
412 long val = init[j].value;
413 lang_add_assignment (exp_assop ('=' ,init[j].symbol, exp_intop (val)));
414 if (init[j].size == sizeof(short))
415 *(short *)init[j].ptr = val;
416 else if (init[j].size == sizeof(int))
417 *(int *)init[j].ptr = val;
418 else if (init[j].size == sizeof(long))
419 *(long *)init[j].ptr = val;
420 /* This might be a long long or other special type. */
421 else if (init[j].size == sizeof(bfd_vma))
422 *(bfd_vma *)init[j].ptr = val;
423 else abort();
424 }
425 /* Restore the pointer. */
426 stat_ptr = save;
427
428 if (pe.FileAlignment >
429 pe.SectionAlignment)
430 {
431 einfo (_("%P: warning, file alignment > section alignment.\n"));
432 }
433 }
434
435 /* This is called after the linker script and the command line options
436 have been read. */
437
438 static void
439 gld_${EMULATION_NAME}_after_parse ()
440 {
441 /* The Windows libraries are designed for the linker to treat the
442 entry point as an undefined symbol. Otherwise, the .obj that
443 defines mainCRTStartup is brought in because it is the first
444 encountered in libc.lib and it has other symbols in it which will
445 be pulled in by the link process. To avoid this, we act as
446 though the user specified -u with the entry point symbol.
447
448 This function is called after the linker script and command line
449 options have been read, so at this point we know the right entry
450 point. This function is called before the input files are
451 opened, so registering the symbol as undefined will make a
452 difference. */
453
454 ldlang_add_undef (entry_symbol);
455 }
456
457 static void
458 gld_${EMULATION_NAME}_after_open ()
459 {
460 /* Pass the wacky PE command line options into the output bfd.
461 FIXME: This should be done via a function, rather than by
462 including an internal BFD header. */
463
464 if (!coff_data (output_bfd)->pe)
465 einfo (_("%F%P: PE operations on non PE file.\n"));
466
467 pe_data (output_bfd)->pe_opthdr = pe;
468 pe_data (output_bfd)->dll = init[DLLOFF].value;
469
470 #ifdef TARGET_IS_armpe
471 {
472 /* Find a BFD that can hold the interworking stubs. */
473 LANG_FOR_EACH_INPUT_STATEMENT (is)
474 {
475 if (bfd_arm_get_bfd_for_interworking (is->the_bfd, & link_info))
476 break;
477 }
478 }
479 #endif
480 }
481 \f
482 /* Callback functions for qsort in sort_sections. */
483
484 static int
485 sort_by_file_name (a, b)
486 void *a;
487 void *b;
488 {
489 lang_statement_union_type **ra = a;
490 lang_statement_union_type **rb = b;
491 int i;
492
493 i = strcmp ((*ra)->input_section.ifile->the_bfd->my_archive->filename,
494 (*rb)->input_section.ifile->the_bfd->my_archive->filename);
495 if (i != 0)
496 return i;
497
498 return strcmp ((*ra)->input_section.ifile->filename,
499 (*rb)->input_section.ifile->filename);
500 }
501
502 static int
503 sort_by_section_name (a, b)
504 void *a;
505 void *b;
506 {
507 lang_statement_union_type **ra = a;
508 lang_statement_union_type **rb = b;
509 return strcmp ((*ra)->input_section.section->name,
510 (*rb)->input_section.section->name);
511 }
512
513 /* Subroutine of sort_sections to a contiguous subset of a list of sections.
514 NEXT_AFTER is the element after the last one to sort.
515 The result is a pointer to the last element's "next" pointer. */
516
517 static lang_statement_union_type **
518 sort_sections_1 (startptr, next_after, count, sort_func)
519 lang_statement_union_type **startptr,*next_after;
520 int count;
521 int (*sort_func) ();
522 {
523 lang_statement_union_type **vec;
524 lang_statement_union_type *p;
525 int i;
526 lang_statement_union_type **ret;
527
528 if (count == 0)
529 return startptr;
530
531 vec = ((lang_statement_union_type **)
532 xmalloc (count * sizeof (lang_statement_union_type *)));
533
534 for (p = *startptr, i = 0; i < count; i++, p = p->next)
535 vec[i] = p;
536
537 qsort (vec, count, sizeof (vec[0]), sort_func);
538
539 /* Fill in the next pointers again. */
540 *startptr = vec[0];
541 for (i = 0; i < count - 1; i++)
542 vec[i]->header.next = vec[i + 1];
543 vec[i]->header.next = next_after;
544 ret = &vec[i]->header.next;
545 free (vec);
546 return ret;
547 }
548
549 /* Sort the .idata\$foo input sections of archives into filename order.
550 The reason is so dlltool can arrange to have the pe dll import information
551 generated correctly - the head of the list goes into dh.o, the tail into
552 dt.o, and the guts into ds[nnnn].o. Note that this is only needed for the
553 .idata section.
554 FIXME: This may no longer be necessary with grouped sections. Instead of
555 sorting on dh.o, ds[nnnn].o, dt.o, one could, for example, have dh.o use
556 .idata\$4h, have ds[nnnn].o use .idata\$4s[nnnn], and have dt.o use .idata\$4t.
557 This would have to be elaborated upon to handle multiple dll's
558 [assuming such an eloboration is possible of course].
559
560 We also sort sections in '\$' wild statements. These are created by the
561 place_orphans routine to implement grouped sections. */
562
563 static void
564 sort_sections (s)
565 lang_statement_union_type *s;
566 {
567 for (; s ; s = s->next)
568 switch (s->header.type)
569 {
570 case lang_output_section_statement_enum:
571 sort_sections (s->output_section_statement.children.head);
572 break;
573 case lang_wild_statement_enum:
574 {
575 lang_statement_union_type **p = &s->wild_statement.children.head;
576
577 /* Is this the .idata section? */
578 if (s->wild_statement.section_name != NULL
579 && strncmp (s->wild_statement.section_name, ".idata", 6) == 0)
580 {
581 /* Sort the children. We want to sort any objects in
582 the same archive. In order to handle the case of
583 including a single archive multiple times, we sort
584 all the children by archive name and then by object
585 name. After sorting them, we re-thread the pointer
586 chain. */
587
588 while (*p)
589 {
590 lang_statement_union_type *start = *p;
591 if (start->header.type != lang_input_section_enum
592 || !start->input_section.ifile->the_bfd->my_archive)
593 p = &(start->header.next);
594 else
595 {
596 lang_statement_union_type *end;
597 int count;
598
599 for (end = start, count = 0;
600 end && end->header.type == lang_input_section_enum;
601 end = end->next)
602 count++;
603
604 p = sort_sections_1 (p, end, count, sort_by_file_name);
605 }
606 }
607 break;
608 }
609
610 /* If this is a collection of grouped sections, sort them.
611 The linker script must explicitly mention "*(.foo\$)" or
612 "*(.foo\$*)". Don't sort them if \$ is not the last
613 character (not sure if this is really useful, but it
614 allows explicitly mentioning some \$ sections and letting
615 the linker handle the rest). */
616 if (s->wild_statement.section_name != NULL)
617 {
618 char *q = strchr (s->wild_statement.section_name, '\$');
619
620 if (q != NULL
621 && (q[1] == '\0'
622 || (q[1] == '*' && q[2] == '\0')))
623 {
624 lang_statement_union_type *end;
625 int count;
626
627 for (end = *p, count = 0; end; end = end->next)
628 {
629 if (end->header.type != lang_input_section_enum)
630 abort ();
631 count++;
632 }
633 (void) sort_sections_1 (p, end, count, sort_by_section_name);
634 }
635 break;
636 }
637 }
638 break;
639 default:
640 break;
641 }
642 }
643
644 static void
645 gld_${EMULATION_NAME}_before_allocation()
646 {
647 extern lang_statement_list_type *stat_ptr;
648
649 #ifdef TARGET_IS_ppcpe
650 /* Here we rummage through the found bfds to collect toc information */
651 {
652 LANG_FOR_EACH_INPUT_STATEMENT (is)
653 {
654 if (!ppc_process_before_allocation (is->the_bfd, &link_info))
655 {
656 /* xgettext:c-format */
657 einfo (_("Errors encountered processing file %s\n"), is->filename);
658 }
659 }
660 }
661
662 /* We have seen it all. Allocate it, and carry on */
663 ppc_allocate_toc_section (&link_info);
664 #endif /* TARGET_IS_ppcpe */
665
666 #ifdef TARGET_IS_armpe
667 /* FIXME: we should be able to set the size of the interworking stub
668 section.
669
670 Here we rummage through the found bfds to collect glue
671 information. FIXME: should this be based on a command line
672 option? krk@cygnus.com */
673 {
674 LANG_FOR_EACH_INPUT_STATEMENT (is)
675 {
676 if (! bfd_arm_process_before_allocation
677 (is->the_bfd, & link_info, support_old_code))
678 {
679 /* xgettext:c-format */
680 einfo (_("Errors encountered processing file %s for interworking"),
681 is->filename);
682 }
683 }
684 }
685
686 /* We have seen it all. Allocate it, and carry on */
687 bfd_arm_allocate_interworking_sections (& link_info);
688 #endif /* TARGET_IS_armpe */
689
690 sort_sections (stat_ptr->head);
691 }
692 \f
693 /* Place an orphan section. We use this to put sections with a '\$' in them
694 into the right place. Any section with a '\$' in them (e.g. .text\$foo)
695 gets mapped to the output section with everything from the '\$' on stripped
696 (e.g. .text).
697 See the Microsoft Portable Executable and Common Object File Format
698 Specification 4.1, section 4.2, Grouped Sections.
699
700 FIXME: This is now handled by the linker script using wildcards,
701 but I'm leaving this here in case we want to enable it for sections
702 which are not mentioned in the linker script. */
703
704 /*ARGSUSED*/
705 static boolean
706 gld_${EMULATION_NAME}_place_orphan (file, s)
707 lang_input_statement_type *file;
708 asection *s;
709 {
710 const char *secname;
711 char *output_secname, *ps;
712 lang_output_section_statement_type *os;
713 lang_statement_union_type *l;
714
715 if ((s->flags & SEC_ALLOC) == 0)
716 return false;
717
718 /* Don't process grouped sections unless doing a final link.
719 If they're marked as COMDAT sections, we don't want .text\$foo to
720 end up in .text and then have .text disappear because it's marked
721 link-once-discard. */
722 if (link_info.relocateable)
723 return false;
724
725 secname = bfd_get_section_name (s->owner, s);
726
727 /* Everything from the '\$' on gets deleted so don't allow '\$' as the
728 first character. */
729 if (*secname == '\$')
730 einfo ("%P%F: section %s has '\$' as first character\n", secname);
731 if (strchr (secname + 1, '\$') == NULL)
732 return false;
733
734 /* Look up the output section. The Microsoft specs say sections names in
735 image files never contain a '\$'. Fortunately, lang_..._lookup creates
736 the section if it doesn't exist. */
737 output_secname = buystring (secname);
738 ps = strchr (output_secname + 1, '\$');
739 *ps = 0;
740 os = lang_output_section_statement_lookup (output_secname);
741
742 /* Find the '\$' wild statement for this section. We currently require the
743 linker script to explicitly mention "*(.foo\$)".
744 FIXME: ppcpe.sc has .CRT\$foo in the .rdata section. According to the
745 Microsoft docs this isn't correct so it's not (currently) handled. */
746
747 ps[0] = '\$';
748 ps[1] = 0;
749 for (l = os->children.head; l; l = l->next)
750 {
751 if (l->header.type == lang_wild_statement_enum
752 && strcmp (l->wild_statement.section_name, output_secname) == 0)
753 break;
754 }
755 ps[0] = 0;
756 if (l == NULL)
757 #if 1
758 einfo ("%P%F: *(%s\$) missing from linker script\n", output_secname);
759 #else /* FIXME: This block is untried. It exists to convey the intent,
760 should one decide to not require *(.foo\$) to appear in the linker
761 script. */
762 {
763 lang_wild_statement_type *new = new_stat (lang_wild_statement,
764 &os->children);
765 new->section_name = xmalloc (strlen (output_secname) + 2);
766 sprintf (new->section_name, "%s\$", output_secname);
767 new->filename = NULL;
768 lang_list_init (&new->children);
769 l = new;
770 }
771 #endif
772
773 /* Link the input section in and we're done for now.
774 The sections still have to be sorted, but that has to wait until
775 all such sections have been processed by us. The sorting is done by
776 sort_sections. */
777 wild_doit (&l->wild_statement.children, s, os, file);
778
779 return true;
780 }
781 \f
782 static char *
783 gld_${EMULATION_NAME}_get_script(isfile)
784 int *isfile;
785 EOF
786 # Scripts compiled in.
787 # sed commands to quote an ld script as a C string.
788 sc="-f ${srcdir}/emultempl/stringify.sed"
789
790 cat >>e${EMULATION_NAME}.c <<EOF
791 {
792 *isfile = 0;
793
794 if (link_info.relocateable == true && config.build_constructors == true)
795 return
796 EOF
797 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
798 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
799 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
800 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
801 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
802 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
803 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
804 echo ' ; else return' >> e${EMULATION_NAME}.c
805 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
806 echo '; }' >> e${EMULATION_NAME}.c
807
808 cat >>e${EMULATION_NAME}.c <<EOF
809
810
811 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
812 {
813 gld_${EMULATION_NAME}_before_parse,
814 syslib_default,
815 hll_default,
816 gld_${EMULATION_NAME}_after_parse,
817 gld_${EMULATION_NAME}_after_open,
818 after_allocation_default,
819 set_output_arch_default,
820 ldemul_default_target,
821 gld_${EMULATION_NAME}_before_allocation,
822 gld_${EMULATION_NAME}_get_script,
823 "${EMULATION_NAME}",
824 "${OUTPUT_FORMAT}",
825 NULL, /* finish */
826 NULL, /* create output section statements */
827 NULL, /* open dynamic archive */
828 gld_${EMULATION_NAME}_place_orphan,
829 gld_${EMULATION_NAME}_set_symbols,
830 gld_${EMULATION_NAME}_parse_args,
831 NULL, /* unrecognised file */
832 gld_${EMULATION_NAME}_list_options
833 };
834 EOF
This page took 0.053954 seconds and 4 git commands to generate.