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