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