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