]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/emultempl/pe.em
* cgen/opcodes fix
[thirdparty/binutils-gdb.git] / ld / emultempl / pe.em
CommitLineData
252b5132
RH
1# This shell script emits a C file. -*- C -*-
2# It does some substitutions.
361d94e7
ILT
3rm -f e${EMULATION_NAME}.c
4(echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
c6c37250 5cat >>e${EMULATION_NAME}.c <<EOF
252b5132 6/* This file is part of GLD, the Gnu Linker.
870df5dc 7 Copyright 1995, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
252b5132
RH
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
23/* For WINDOWS_NT */
24/* The original file generated returned different default scripts depending
25 on whether certain switches were set, but these switches pertain to the
26 Linux system and that particular version of coff. In the NT case, we
27 only determine if the subsystem is console or windows in order to select
28 the correct entry point by default. */
29
30#include "bfd.h"
31#include "sysdep.h"
32#include "bfdlink.h"
33#include "getopt.h"
34#include "libiberty.h"
35#include "ld.h"
36#include "ldmain.h"
37#include "ldgram.h"
38#include "ldexp.h"
39#include "ldlang.h"
40#include "ldemul.h"
41#include "ldlex.h"
42#include "ldmisc.h"
43#include "ldctor.h"
44#include "ldfile.h"
45#include "coff/internal.h"
71add731
ILT
46
47/* FIXME: This is a BFD internal header file, and we should not be
48 using it here. */
252b5132 49#include "../bfd/libcoff.h"
71add731 50
252b5132 51#include "deffile.h"
1069dd8d 52#include "pe-dll.h"
252b5132
RH
53
54#define TARGET_IS_${EMULATION_NAME}
55
2be9b2c7
ILT
56/* Permit the emulation parameters to override the default section
57 alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes
58 it seem that include/coff/internal.h should not define
59 PE_DEF_SECTION_ALIGNMENT. */
60#if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
61#undef PE_DEF_SECTION_ALIGNMENT
62#define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
63#endif
64
c6c37250
DD
65#if defined(TARGET_IS_i386pe)
66#define DLL_SUPPORT
67#endif
344a211f
NC
68#if defined(TARGET_IS_shpe) || defined(TARGET_IS_mipspe) || defined(TARGET_IS_armpe)
69#define DLL_SUPPORT
70#endif
c6c37250 71
344a211f 72#if defined(TARGET_IS_i386pe) || ! defined(DLL_SUPPORT)
c6c37250 73#define PE_DEF_SUBSYSTEM 3
344a211f
NC
74#else
75#undef NT_EXE_IMAGE_BASE
76#undef PE_DEF_SECTION_ALIGNMENT
77#undef PE_DEF_FILE_ALIGNMENT
78#define NT_EXE_IMAGE_BASE 0x00010000
79#ifdef TARGET_IS_armpe
80#define PE_DEF_SECTION_ALIGNMENT 0x00001000
81#define PE_DEF_SUBSYSTEM 9
82#else
83#define PE_DEF_SECTION_ALIGNMENT 0x00000400
84#define PE_DEF_SUBSYSTEM 2
85#endif
86#define PE_DEF_FILE_ALIGNMENT 0x00000200
87#endif
c6c37250 88
434d1125
NC
89#ifdef TARGET_IS_arm_epoc_pe
90#define bfd_arm_pe_allocate_interworking_sections \
91 bfd_arm_epoc_pe_allocate_interworking_sections
92#define bfd_arm_pe_get_bfd_for_interworking \
93 bfd_arm_epoc_pe_get_bfd_for_interworking
94#define bfd_arm_pe_process_before_allocation \
95 bfd_arm_epoc_pe_process_before_allocation
96#endif
97
252b5132
RH
98static void gld_${EMULATION_NAME}_set_symbols PARAMS ((void));
99static void gld_${EMULATION_NAME}_after_open PARAMS ((void));
100static void gld_${EMULATION_NAME}_before_parse PARAMS ((void));
101static void gld_${EMULATION_NAME}_after_parse PARAMS ((void));
102static void gld_${EMULATION_NAME}_before_allocation PARAMS ((void));
103static boolean gld_${EMULATION_NAME}_place_orphan
104 PARAMS ((lang_input_statement_type *, asection *));
252b5132
RH
105static char *gld_${EMULATION_NAME}_get_script PARAMS ((int *));
106static int gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **));
6f798e5c 107static void gld_${EMULATION_NAME}_finish PARAMS ((void));
252b5132
RH
108
109static struct internal_extra_pe_aouthdr pe;
110static int dll;
111static int support_old_code = 0;
6f798e5c 112static char * thumb_entry_symbol = NULL;
252b5132
RH
113static lang_assignment_statement_type *image_base_statement = 0;
114
252b5132 115static int pe_enable_stdcall_fixup = -1; /* 0=disable 1=enable */
f0c87f88
NC
116#ifdef DLL_SUPPORT
117static char *pe_out_def_filename = 0;
252b5132 118static char *pe_implib_filename = 0;
f0c87f88 119#endif
252b5132
RH
120
121extern const char *output_filename;
122
123static void
124gld_${EMULATION_NAME}_before_parse()
125{
2be9b2c7 126 output_filename = "${EXECUTABLE_NAME:-a.exe}";
252b5132 127 ldfile_output_architecture = bfd_arch_${ARCH};
c6c37250 128#ifdef DLL_SUPPORT
252b5132 129 config.has_shared = 1;
344a211f
NC
130
131#if (PE_DEF_SUBSYSTEM == 9) || (PE_DEF_SUBSYSTEM == 2)
132#if defined TARGET_IS_mipspe || defined TARGET_IS_armpe
133 lang_add_entry ("WinMainCRTStartup", 1);
134#else
135 lang_add_entry ("_WinMainCRTStartup", 1);
136#endif
137#endif
252b5132
RH
138#endif
139}
140\f
141/* PE format extra command line options. */
142
143/* Used for setting flags in the PE header. */
144#define OPTION_BASE_FILE (300 + 1)
145#define OPTION_DLL (OPTION_BASE_FILE + 1)
146#define OPTION_FILE_ALIGNMENT (OPTION_DLL + 1)
147#define OPTION_IMAGE_BASE (OPTION_FILE_ALIGNMENT + 1)
148#define OPTION_MAJOR_IMAGE_VERSION (OPTION_IMAGE_BASE + 1)
149#define OPTION_MAJOR_OS_VERSION (OPTION_MAJOR_IMAGE_VERSION + 1)
150#define OPTION_MAJOR_SUBSYSTEM_VERSION (OPTION_MAJOR_OS_VERSION + 1)
151#define OPTION_MINOR_IMAGE_VERSION (OPTION_MAJOR_SUBSYSTEM_VERSION + 1)
152#define OPTION_MINOR_OS_VERSION (OPTION_MINOR_IMAGE_VERSION + 1)
153#define OPTION_MINOR_SUBSYSTEM_VERSION (OPTION_MINOR_OS_VERSION + 1)
154#define OPTION_SECTION_ALIGNMENT (OPTION_MINOR_SUBSYSTEM_VERSION + 1)
155#define OPTION_STACK (OPTION_SECTION_ALIGNMENT + 1)
156#define OPTION_SUBSYSTEM (OPTION_STACK + 1)
157#define OPTION_HEAP (OPTION_SUBSYSTEM + 1)
158#define OPTION_SUPPORT_OLD_CODE (OPTION_HEAP + 1)
159#define OPTION_OUT_DEF (OPTION_SUPPORT_OLD_CODE + 1)
160#define OPTION_EXPORT_ALL (OPTION_OUT_DEF + 1)
161#define OPTION_EXCLUDE_SYMBOLS (OPTION_EXPORT_ALL + 1)
162#define OPTION_KILL_ATS (OPTION_EXCLUDE_SYMBOLS + 1)
163#define OPTION_STDCALL_ALIASES (OPTION_KILL_ATS + 1)
164#define OPTION_ENABLE_STDCALL_FIXUP (OPTION_STDCALL_ALIASES + 1)
165#define OPTION_DISABLE_STDCALL_FIXUP (OPTION_ENABLE_STDCALL_FIXUP + 1)
166#define OPTION_IMPLIB_FILENAME (OPTION_DISABLE_STDCALL_FIXUP + 1)
6f798e5c 167#define OPTION_THUMB_ENTRY (OPTION_IMPLIB_FILENAME + 1)
870df5dc
NC
168#define OPTION_WARN_DUPLICATE_EXPORTS (OPTION_THUMB_ENTRY + 1)
169#define OPTION_IMP_COMPAT (OPTION_WARN_DUPLICATE_EXPORTS + 1)
252b5132
RH
170
171static struct option longopts[] =
172{
173 /* PE options */
174 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
175 {"dll", no_argument, NULL, OPTION_DLL},
176 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
177 {"heap", required_argument, NULL, OPTION_HEAP},
178 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
179 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
180 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
181 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
182 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
183 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
184 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
185 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
186 {"stack", required_argument, NULL, OPTION_STACK},
187 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
188 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
6f798e5c 189 {"thumb-entry", required_argument, NULL, OPTION_THUMB_ENTRY},
c6c37250 190#ifdef DLL_SUPPORT
252b5132
RH
191 /* getopt allows abbreviations, so we do this to stop it from treating -o
192 as an abbreviation for this option */
193 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
194 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
195 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
196 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
197 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
198 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
199 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
200 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
201 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
870df5dc
NC
202 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
203 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
252b5132
RH
204#endif
205 {NULL, no_argument, NULL, 0}
206};
207
208
209/* PE/WIN32; added routines to get the subsystem type, heap and/or stack
210 parameters which may be input from the command line */
211
212typedef struct
213{
214 void *ptr;
215 int size;
216 int value;
217 char *symbol;
218 int inited;
219} definfo;
220
221#define D(field,symbol,def) {&pe.field,sizeof(pe.field), def, symbol,0}
222
223static definfo init[] =
224{
225 /* imagebase must be first */
226#define IMAGEBASEOFF 0
227 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
228#define DLLOFF 1
1069dd8d 229 {&dll, sizeof(dll), 0, "__dll__", 0},
252b5132
RH
230 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
231 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
232 D(MajorOperatingSystemVersion,"__major_os_version__", 4),
233 D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
234 D(MajorImageVersion,"__major_image_version__", 1),
235 D(MinorImageVersion,"__minor_image_version__", 0),
344a211f
NC
236#ifdef TARGET_IS_armpe
237 D(MajorSubsystemVersion,"__major_subsystem_version__", 2),
238#else
252b5132 239 D(MajorSubsystemVersion,"__major_subsystem_version__", 4),
344a211f 240#endif
252b5132 241 D(MinorSubsystemVersion,"__minor_subsystem_version__", 0),
2be9b2c7 242 D(Subsystem,"__subsystem__", ${SUBSYSTEM}),
252b5132
RH
243 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x2000000),
244 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
245 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
246 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
247 D(LoaderFlags,"__loader_flags__", 0x0),
248 { NULL, 0, 0, NULL, 0 }
249};
250
251static void
252gld_${EMULATION_NAME}_list_options (file)
253 FILE * file;
254{
255 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
256 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
257 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
258 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
259 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
260 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
261 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
262 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
263 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
264 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
265 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
266 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
267 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
268 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
269 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
6f798e5c 270 fprintf (file, _(" --thumb-entry=<symbol> Set the entry point to be Thumb <symbol>\n"));
c6c37250 271#ifdef DLL_SUPPORT
252b5132
RH
272 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
273 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
274 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
275 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
276 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
277 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
278 fprintf (file, _(" --out-implib <file> Generate import library\n"));
279 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
0752a404
NC
280 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
281 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n"));
282 fprintf (file, _(" create __imp_<SYMBOL> as well.\n"));
252b5132
RH
283#endif
284}
285
286static void
287set_pe_name (name, val)
288 char *name;
289 long val;
290{
291 int i;
292 /* Find the name and set it. */
293 for (i = 0; init[i].ptr; i++)
294 {
295 if (strcmp (name, init[i].symbol) == 0)
296 {
297 init[i].value = val;
298 init[i].inited = 1;
299 return;
300 }
301 }
302 abort();
303}
304
305
306static void
307set_pe_subsystem ()
308{
309 const char *sver;
310 int len;
311 int i;
312 static const struct
313 {
314 const char *name;
315 const int value;
316 const char *entry;
317 }
318 v[] =
319 {
2be9b2c7 320 { "native", 1, "NtProcessStartup" },
344a211f 321#if defined TARGET_IS_mipspe || defined TARGET_IS_armpe
2be9b2c7 322 { "windows", 2, "WinMainCRTStartup" },
344a211f
NC
323#else
324 { "windows", 2, "WinMainCRTStartup" },
325#endif
2be9b2c7 326 { "console", 3, "mainCRTStartup" },
252b5132
RH
327#if 0
328 /* The Microsoft linker does not recognize this. */
329 { "os2", 5, "" },
330#endif
2be9b2c7 331 { "posix", 7, "__PosixProcessStartup"},
344a211f 332 { "wince", 9, "_WinMainCRTStartup" },
252b5132
RH
333 { 0, 0, 0 }
334 };
335
336 sver = strchr (optarg, ':');
337 if (sver == NULL)
338 len = strlen (optarg);
339 else
340 {
341 char *end;
342
343 len = sver - optarg;
344 set_pe_name ("__major_subsystem_version__",
345 strtoul (sver + 1, &end, 0));
346 if (*end == '.')
347 set_pe_name ("__minor_subsystem_version__",
348 strtoul (end + 1, &end, 0));
349 if (*end != '\0')
350 einfo (_("%P: warning: bad version number in -subsystem option\n"));
351 }
352
353 for (i = 0; v[i].name; i++)
354 {
355 if (strncmp (optarg, v[i].name, len) == 0
356 && v[i].name[len] == '\0')
357 {
2be9b2c7
ILT
358 const char *initial_symbol_char;
359 const char *entry;
360
252b5132
RH
361 set_pe_name ("__subsystem__", v[i].value);
362
2be9b2c7
ILT
363 initial_symbol_char = ${INITIAL_SYMBOL_CHAR};
364 if (*initial_symbol_char == '\0')
365 entry = v[i].entry;
366 else
367 {
368 char *alc_entry;
369
370 /* lang_add_entry expects its argument to be permanently
371 allocated, so we don't free this string. */
372 alc_entry = xmalloc (strlen (initial_symbol_char)
373 + strlen (v[i].entry)
374 + 1);
375 strcpy (alc_entry, initial_symbol_char);
376 strcat (alc_entry, v[i].entry);
377 entry = alc_entry;
378 }
379
380 lang_add_entry (entry, 1);
252b5132
RH
381
382 return;
383 }
384 }
385
386 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
387}
388
389
390
391static void
392set_pe_value (name)
393 char *name;
394
395{
396 char *end;
397
398 set_pe_name (name, strtoul (optarg, &end, 0));
399
400 if (end == optarg)
401 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
402
403 optarg = end;
404}
405
406static void
407set_pe_stack_heap (resname, comname)
408 char *resname;
409 char *comname;
410{
411 set_pe_value (resname);
412
413 if (*optarg == ',')
414 {
415 optarg++;
416 set_pe_value (comname);
417 }
418 else if (*optarg)
419 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
420}
421
422
423
424static int
425gld_${EMULATION_NAME}_parse_args(argc, argv)
426 int argc;
427 char **argv;
428{
429 int longind;
430 int optc;
431 int prevoptind = optind;
432 int prevopterr = opterr;
433 int wanterror;
434 static int lastoptind = -1;
435
436 if (lastoptind != optind)
437 opterr = 0;
438 wanterror = opterr;
439
440 lastoptind = optind;
441
442 optc = getopt_long_only (argc, argv, "-", longopts, &longind);
443 opterr = prevopterr;
444
445 switch (optc)
446 {
447 default:
448 if (wanterror)
449 xexit (1);
450 optind = prevoptind;
451 return 0;
452
453 case OPTION_BASE_FILE:
454 link_info.base_file = (PTR) fopen (optarg, FOPEN_WB);
455 if (link_info.base_file == NULL)
456 {
457 /* xgettext:c-format */
458 fprintf (stderr, _("%s: Can't open base file %s\n"),
459 program_name, optarg);
460 xexit (1);
461 }
462 break;
463
464 /* PE options */
465 case OPTION_HEAP:
466 set_pe_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
467 break;
468 case OPTION_STACK:
469 set_pe_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
470 break;
471 case OPTION_SUBSYSTEM:
472 set_pe_subsystem ();
473 break;
474 case OPTION_MAJOR_OS_VERSION:
475 set_pe_value ("__major_os_version__");
476 break;
477 case OPTION_MINOR_OS_VERSION:
478 set_pe_value ("__minor_os_version__");
479 break;
480 case OPTION_MAJOR_SUBSYSTEM_VERSION:
481 set_pe_value ("__major_subsystem_version__");
482 break;
483 case OPTION_MINOR_SUBSYSTEM_VERSION:
484 set_pe_value ("__minor_subsystem_version__");
485 break;
486 case OPTION_MAJOR_IMAGE_VERSION:
487 set_pe_value ("__major_image_version__");
488 break;
489 case OPTION_MINOR_IMAGE_VERSION:
490 set_pe_value ("__minor_image_version__");
491 break;
492 case OPTION_FILE_ALIGNMENT:
493 set_pe_value ("__file_alignment__");
494 break;
495 case OPTION_SECTION_ALIGNMENT:
496 set_pe_value ("__section_alignment__");
497 break;
498 case OPTION_DLL:
499 set_pe_name ("__dll__", 1);
500 break;
501 case OPTION_IMAGE_BASE:
502 set_pe_value ("__image_base__");
503 break;
504 case OPTION_SUPPORT_OLD_CODE:
505 support_old_code = 1;
506 break;
6f798e5c
NC
507 case OPTION_THUMB_ENTRY:
508 thumb_entry_symbol = optarg;
509 break;
c6c37250 510#ifdef DLL_SUPPORT
252b5132
RH
511 case OPTION_OUT_DEF:
512 pe_out_def_filename = xstrdup (optarg);
513 break;
514 case OPTION_EXPORT_ALL:
515 pe_dll_export_everything = 1;
516 break;
517 case OPTION_EXCLUDE_SYMBOLS:
252b5132 518 pe_dll_add_excludes (optarg);
252b5132
RH
519 break;
520 case OPTION_KILL_ATS:
521 pe_dll_kill_ats = 1;
522 break;
523 case OPTION_STDCALL_ALIASES:
524 pe_dll_stdcall_aliases = 1;
525 break;
526 case OPTION_ENABLE_STDCALL_FIXUP:
527 pe_enable_stdcall_fixup = 1;
528 break;
529 case OPTION_DISABLE_STDCALL_FIXUP:
530 pe_enable_stdcall_fixup = 0;
531 break;
532 case OPTION_IMPLIB_FILENAME:
533 pe_implib_filename = xstrdup (optarg);
534 break;
870df5dc
NC
535 case OPTION_WARN_DUPLICATE_EXPORTS:
536 pe_dll_warn_dup_exports = 1;
537 break;
538 case OPTION_IMP_COMPAT:
539 pe_dll_compat_implib = 1;
540 break;
c6c37250 541#endif
252b5132
RH
542 }
543 return 1;
544}
545\f
546/* Assign values to the special symbols before the linker script is
547 read. */
548
549static void
550gld_${EMULATION_NAME}_set_symbols ()
551{
552 /* Run through and invent symbols for all the
553 names and insert the defaults. */
554 int j;
555 lang_statement_list_type *save;
556
557 if (!init[IMAGEBASEOFF].inited)
558 {
559 if (link_info.relocateable)
560 init[IMAGEBASEOFF].value = 0;
561 else if (init[DLLOFF].value || link_info.shared)
562 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
563 else
564 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
565 }
566
567 /* Don't do any symbol assignments if this is a relocateable link. */
568 if (link_info.relocateable)
569 return;
570
571 /* Glue the assignments into the abs section */
572 save = stat_ptr;
573
574 stat_ptr = &(abs_output_section->children);
575
576 for (j = 0; init[j].ptr; j++)
577 {
578 long val = init[j].value;
579 lang_assignment_statement_type *rv;
580 rv = lang_add_assignment (exp_assop ('=' ,init[j].symbol, exp_intop (val)));
581 if (init[j].size == sizeof(short))
582 *(short *)init[j].ptr = val;
583 else if (init[j].size == sizeof(int))
584 *(int *)init[j].ptr = val;
585 else if (init[j].size == sizeof(long))
586 *(long *)init[j].ptr = val;
587 /* This might be a long long or other special type. */
588 else if (init[j].size == sizeof(bfd_vma))
589 *(bfd_vma *)init[j].ptr = val;
590 else abort();
591 if (j == IMAGEBASEOFF)
592 image_base_statement = rv;
593 }
594 /* Restore the pointer. */
595 stat_ptr = save;
596
597 if (pe.FileAlignment >
598 pe.SectionAlignment)
599 {
600 einfo (_("%P: warning, file alignment > section alignment.\n"));
601 }
602}
603
604/* This is called after the linker script and the command line options
605 have been read. */
606
607static void
608gld_${EMULATION_NAME}_after_parse ()
609{
610 /* The Windows libraries are designed for the linker to treat the
611 entry point as an undefined symbol. Otherwise, the .obj that
612 defines mainCRTStartup is brought in because it is the first
613 encountered in libc.lib and it has other symbols in it which will
614 be pulled in by the link process. To avoid this, we act as
615 though the user specified -u with the entry point symbol.
616
617 This function is called after the linker script and command line
618 options have been read, so at this point we know the right entry
619 point. This function is called before the input files are
620 opened, so registering the symbol as undefined will make a
621 difference. */
622
2a275620 623 if (! link_info.relocateable && entry_symbol != NULL)
252b5132
RH
624 ldlang_add_undef (entry_symbol);
625}
626
627static struct bfd_link_hash_entry *pe_undef_found_sym;
628
629static boolean
630pe_undef_cdecl_match (h, string)
631 struct bfd_link_hash_entry *h;
632 PTR string;
633{
634 int sl = strlen (string);
635 if (h->type == bfd_link_hash_defined
636 && strncmp (h->root.string, string, sl) == 0
637 && h->root.string[sl] == '@')
638 {
639 pe_undef_found_sym = h;
640 return false;
641 }
642 return true;
643}
644
f0c87f88 645#ifdef DLL_SUPPORT
252b5132
RH
646static void
647pe_fixup_stdcalls ()
648{
649 static int gave_warning_message = 0;
650 struct bfd_link_hash_entry *undef, *sym;
651 char *at;
652 for (undef = link_info.hash->undefs; undef; undef=undef->next)
653 if (undef->type == bfd_link_hash_undefined)
654 {
655 at = strchr (undef->root.string, '@');
656 if (at)
657 {
658 /* The symbol is a stdcall symbol, so let's look for a cdecl
659 symbol with the same name and resolve to that */
660 char *cname = xstrdup (undef->root.string);
661 at = strchr (cname, '@');
662 *at = 0;
663 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
664 if (sym && sym->type == bfd_link_hash_defined)
665 {
666 undef->type = bfd_link_hash_defined;
667 undef->u.def.value = sym->u.def.value;
668 undef->u.def.section = sym->u.def.section;
669 if (pe_enable_stdcall_fixup == -1)
670 {
671 einfo (_("Warning: resolving %s by linking to %s\n"),
672 undef->root.string, cname);
673 if (! gave_warning_message)
674 {
675 gave_warning_message = 1;
676 einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
677 einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
678 }
679 }
680 }
681 }
682 else
683 {
684 /* The symbol is a cdecl symbol, so we look for stdcall
685 symbols - which means scanning the whole symbol table */
686 pe_undef_found_sym = 0;
687 bfd_link_hash_traverse (link_info.hash, pe_undef_cdecl_match,
688 (PTR) undef->root.string);
689 sym = pe_undef_found_sym;
690 if (sym)
691 {
692 undef->type = bfd_link_hash_defined;
693 undef->u.def.value = sym->u.def.value;
694 undef->u.def.section = sym->u.def.section;
695 if (pe_enable_stdcall_fixup == -1)
696 {
697 einfo (_("Warning: resolving %s by linking to %s\n"),
698 undef->root.string, sym->root.string);
699 if (! gave_warning_message)
700 {
701 gave_warning_message = 1;
702 einfo(_("Use --enable-stdcall-fixup to disable these warnings\n"));
703 einfo(_("Use --disable-stdcall-fixup to disable these fixups\n"));
704 }
705 }
706 }
707 }
708 }
709}
f0c87f88 710#endif /* DLL_SUPPORT */
252b5132
RH
711
712static void
713gld_${EMULATION_NAME}_after_open ()
714{
715 /* Pass the wacky PE command line options into the output bfd.
716 FIXME: This should be done via a function, rather than by
717 including an internal BFD header. */
718
719 if (!coff_data (output_bfd)->pe)
720 einfo (_("%F%P: PE operations on non PE file.\n"));
721
722 pe_data (output_bfd)->pe_opthdr = pe;
723 pe_data (output_bfd)->dll = init[DLLOFF].value;
724
c6c37250 725#ifdef DLL_SUPPORT
252b5132
RH
726 if (pe_enable_stdcall_fixup) /* -1=warn or 1=disable */
727 pe_fixup_stdcalls ();
728
729 pe_process_import_defs(output_bfd, &link_info);
730 if (link_info.shared)
731 pe_dll_build_sections (output_bfd, &link_info);
344a211f
NC
732
733#ifndef TARGET_IS_i386pe
734#ifndef TARGET_IS_armpe
735 else
736 pe_exe_build_sections (output_bfd, &link_info);
737#endif
738#endif
252b5132
RH
739#endif
740
626e0105 741#if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
f11523b0
NC
742 if (strstr (bfd_get_target (output_bfd), "arm") == NULL)
743 {
744 /* The arm backend needs special fields in the output hash structure.
745 These will only be created if the output format is an arm format,
746 hence we do not support linking and changing output formats at the
747 same time. Use a link followed by objcopy to change output formats. */
748 einfo ("%F%X%P: error: cannot change output format whilst linking ARM binaries\n");
749 return;
750 }
252b5132
RH
751 {
752 /* Find a BFD that can hold the interworking stubs. */
753 LANG_FOR_EACH_INPUT_STATEMENT (is)
754 {
46d23b7c 755 if (bfd_arm_pe_get_bfd_for_interworking (is->the_bfd, & link_info))
252b5132
RH
756 break;
757 }
758 }
759#endif
c6c37250
DD
760
761 {
f0c87f88 762 int is_ms_arch = 0;
1069dd8d 763 bfd *cur_arch = 0;
c6c37250 764 lang_input_statement_type *is2;
1069dd8d 765
c6c37250
DD
766 /* Careful - this is a shell script. Watch those dollar signs! */
767 /* Microsoft import libraries have every member named the same,
768 and not in the right order for us to link them correctly. We
769 must detect these and rename the members so that they'll link
770 correctly. There are three types of objects: the head, the
771 thunks, and the sentinel(s). The head is easy; it's the one
772 with idata2. We assume that the sentinels won't have relocs,
773 and the thunks will. It's easier than checking the symbol
1069dd8d 774 table for external references. */
c6c37250
DD
775 LANG_FOR_EACH_INPUT_STATEMENT (is)
776 {
777 if (is->the_bfd->my_archive)
778 {
779 bfd *arch = is->the_bfd->my_archive;
780 if (cur_arch != arch)
781 {
782 cur_arch = arch;
783 is_ms_arch = 1;
784 for (is2 = is;
785 is2 && is2->the_bfd->my_archive == arch;
786 is2 = (lang_input_statement_type *)is2->next)
787 {
788 if (strcmp (is->the_bfd->filename, is2->the_bfd->filename))
789 is_ms_arch = 0;
790 }
791 }
792
793 if (is_ms_arch)
794 {
1069dd8d 795 int idata2 = 0, reloc_count=0;
c6c37250
DD
796 asection *sec;
797 char *new_name, seq;
1069dd8d 798
c6c37250
DD
799 for (sec = is->the_bfd->sections; sec; sec = sec->next)
800 {
801 if (strcmp (sec->name, ".idata\$2") == 0)
802 idata2 = 1;
803 reloc_count += sec->reloc_count;
804 }
805
806 if (idata2) /* .idata2 is the TOC */
807 seq = 'a';
808 else if (reloc_count > 0) /* thunks */
809 seq = 'b';
810 else /* sentinel */
811 seq = 'c';
812
e4e24acb 813 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
c6c37250
DD
814 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
815 is->the_bfd->filename = new_name;
816
e4e24acb 817 new_name = xmalloc (strlen(is->filename) + 3);
c6c37250
DD
818 sprintf (new_name, "%s.%c", is->filename, seq);
819 is->filename = new_name;
820 }
821 }
822 }
823 }
252b5132
RH
824}
825\f
826static void
827gld_${EMULATION_NAME}_before_allocation()
828{
829#ifdef TARGET_IS_ppcpe
830 /* Here we rummage through the found bfds to collect toc information */
831 {
832 LANG_FOR_EACH_INPUT_STATEMENT (is)
833 {
834 if (!ppc_process_before_allocation (is->the_bfd, &link_info))
835 {
836 /* xgettext:c-format */
837 einfo (_("Errors encountered processing file %s\n"), is->filename);
838 }
839 }
840 }
841
842 /* We have seen it all. Allocate it, and carry on */
843 ppc_allocate_toc_section (&link_info);
844#endif /* TARGET_IS_ppcpe */
845
626e0105 846#if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
252b5132
RH
847 /* FIXME: we should be able to set the size of the interworking stub
848 section.
849
850 Here we rummage through the found bfds to collect glue
851 information. FIXME: should this be based on a command line
852 option? krk@cygnus.com */
853 {
854 LANG_FOR_EACH_INPUT_STATEMENT (is)
855 {
46d23b7c 856 if (! bfd_arm_pe_process_before_allocation
252b5132
RH
857 (is->the_bfd, & link_info, support_old_code))
858 {
859 /* xgettext:c-format */
860 einfo (_("Errors encountered processing file %s for interworking"),
861 is->filename);
862 }
863 }
864 }
865
866 /* We have seen it all. Allocate it, and carry on */
46d23b7c 867 bfd_arm_pe_allocate_interworking_sections (& link_info);
252b5132
RH
868#endif /* TARGET_IS_armpe */
869}
870\f
871
872/* This is called when an input file isn't recognized as a BFD. We
873 check here for .DEF files and pull them in automatically. */
f0c87f88 874#ifdef DLL_SUPPORT
252b5132
RH
875static int
876saw_option(char *option)
877{
878 int i;
879 for (i=0; init[i].ptr; i++)
880 if (strcmp (init[i].symbol, option) == 0)
881 return init[i].inited;
882 return 0;
883}
f0c87f88 884#endif
252b5132
RH
885
886static boolean
887gld_${EMULATION_NAME}_unrecognized_file(entry)
f0c87f88 888 lang_input_statement_type *entry ATTRIBUTE_UNUSED;
252b5132 889{
c6c37250 890#ifdef DLL_SUPPORT
252b5132
RH
891 const char *ext = entry->filename + strlen (entry->filename) - 4;
892
893 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
894 {
895 if (pe_def_file == 0)
896 pe_def_file = def_file_empty ();
897 def_file_parse (entry->filename, pe_def_file);
898 if (pe_def_file)
899 {
900 int i, buflen=0, len;
901 char *buf;
902 for (i=0; i<pe_def_file->num_exports; i++)
903 {
904 len = strlen(pe_def_file->exports[i].internal_name);
905 if (buflen < len+2)
906 buflen = len+2;
907 }
908 buf = (char *) xmalloc (buflen);
909 for (i=0; i<pe_def_file->num_exports; i++)
910 {
911 struct bfd_link_hash_entry *h;
912 sprintf(buf, "_%s", pe_def_file->exports[i].internal_name);
913
914 h = bfd_link_hash_lookup (link_info.hash, buf, true, true, true);
915 if (h == (struct bfd_link_hash_entry *) NULL)
916 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
917 if (h->type == bfd_link_hash_new)
918 {
919 h->type = bfd_link_hash_undefined;
920 h->u.undef.abfd = NULL;
921 bfd_link_add_undef (link_info.hash, h);
922 }
923 }
924 free (buf);
925
926 /* def_file_print (stdout, pe_def_file); */
927 if (pe_def_file->is_dll == 1)
928 link_info.shared = 1;
929
930 if (pe_def_file->base_address != (bfd_vma)(-1))
931 {
932 pe.ImageBase =
933 pe_data (output_bfd)->pe_opthdr.ImageBase =
934 init[IMAGEBASEOFF].value = pe_def_file->base_address;
935 init[IMAGEBASEOFF].inited = 1;
936 if (image_base_statement)
937 image_base_statement->exp =
938 exp_assop ('=', "__image_base__", exp_intop (pe.ImageBase));
939 }
940
941#if 0
942 /* Not sure if these *should* be set */
943 if (pe_def_file->version_major != -1)
944 {
945 pe.MajorImageVersion = pe_def_file->version_major;
946 pe.MinorImageVersion = pe_def_file->version_minor;
947 }
948#endif
949 if (pe_def_file->stack_reserve != -1
950 && ! saw_option ("__size_of_stack_reserve__"))
951 {
952 pe.SizeOfStackReserve = pe_def_file->stack_reserve;
953 if (pe_def_file->stack_commit != -1)
954 pe.SizeOfStackCommit = pe_def_file->stack_commit;
955 }
956 if (pe_def_file->heap_reserve != -1
957 && ! saw_option ("__size_of_heap_reserve__"))
958 {
959 pe.SizeOfHeapReserve = pe_def_file->heap_reserve;
960 if (pe_def_file->heap_commit != -1)
961 pe.SizeOfHeapCommit = pe_def_file->heap_commit;
962 }
963 return true;
964 }
965 }
966#endif
967 return false;
968
969}
970
971static boolean
972gld_${EMULATION_NAME}_recognized_file(entry)
f0c87f88 973 lang_input_statement_type *entry ATTRIBUTE_UNUSED;
252b5132 974{
c6c37250 975#ifdef DLL_SUPPORT
252b5132 976#ifdef TARGET_IS_i386pe
c6c37250 977 pe_dll_id_target ("pei-i386");
344a211f
NC
978#endif
979#ifdef TARGET_IS_shpe
980 pe_dll_id_target ("pei-shl");
981#endif
982#ifdef TARGET_IS_mipspe
983 pe_dll_id_target ("pei-mips");
984#endif
985#ifdef TARGET_IS_armpe
986 pe_dll_id_target ("pei-arm-little");
c6c37250 987#endif
252b5132
RH
988 if (bfd_get_format (entry->the_bfd) == bfd_object)
989 {
990 const char *ext = entry->filename + strlen (entry->filename) - 4;
991 if (strcmp (ext, ".dll") == 0 || strcmp (ext, ".DLL") == 0)
992 return pe_implied_import_dll (entry->filename);
993 }
994#endif
995 return false;
996}
997
998static void
999gld_${EMULATION_NAME}_finish ()
1000{
6f798e5c
NC
1001#if defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe)
1002 struct bfd_link_hash_entry * h;
1003
1004 if (thumb_entry_symbol != NULL)
1005 {
1006 h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol, false, false, true);
1007
1008 if (h != (struct bfd_link_hash_entry *) NULL
1009 && (h->type == bfd_link_hash_defined
1010 || h->type == bfd_link_hash_defweak)
1011 && h->u.def.section->output_section != NULL)
1012 {
1013 static char buffer[32];
1014 bfd_vma val;
1015
1016 /* Special procesing is required for a Thumb entry symbol. The
1017 bottom bit of its address must be set. */
1018 val = (h->u.def.value
1019 + bfd_get_section_vma (output_bfd,
1020 h->u.def.section->output_section)
1021 + h->u.def.section->output_offset);
1022
1023 val |= 1;
1024
1025 /* Now convert this value into a string and store it in entry_symbol
1026 where the lang_finish() function will pick it up. */
1027 buffer[0] = '0';
1028 buffer[1] = 'x';
1029
1030 sprintf_vma (buffer + 2, val);
1031
1032 if (entry_symbol != NULL && entry_from_cmdline)
1033 einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
1034 thumb_entry_symbol, entry_symbol);
1035 entry_symbol = buffer;
1036 }
1037 else
1038 einfo (_("%P: warning: connot find thumb start symbol %s\n"), thumb_entry_symbol);
1039 }
1040#endif /* defined(TARGET_IS_armpe) || defined(TARGET_IS_arm_epoc_pe) */
1041
c6c37250 1042#ifdef DLL_SUPPORT
252b5132
RH
1043 if (link_info.shared)
1044 {
1045 pe_dll_fill_sections (output_bfd, &link_info);
1046 if (pe_implib_filename)
1047 pe_dll_generate_implib (pe_def_file, pe_implib_filename);
1048 }
344a211f
NC
1049#if defined(TARGET_IS_shpe) || defined(TARGET_IS_mipspe)
1050 else
1051 {
1052 pe_exe_fill_sections (output_bfd, &link_info);
1053 }
1054#endif
1055
252b5132
RH
1056 if (pe_out_def_filename)
1057 pe_dll_generate_def_file (pe_out_def_filename);
1058#endif
1059}
1060
1061\f
1062/* Place an orphan section.
1063
1064 We use this to put sections in a reasonable place in the file, and
1065 to ensure that they are aligned as required.
1066
1067 We handle grouped sections here as well. A section named .foo$nn
1068 goes into the output section .foo. All grouped sections are sorted
1069 by name.
1070
1071 Grouped sections for the default sections are handled by the
1072 default linker script using wildcards, and are sorted by
1073 sort_sections. */
1074
6a345e87
AM
1075struct orphan_save
1076{
1077 lang_output_section_statement_type *os;
5ba47421 1078 asection **section;
6a345e87
AM
1079 lang_statement_union_type **stmt;
1080};
252b5132
RH
1081
1082/*ARGSUSED*/
1083static boolean
1084gld_${EMULATION_NAME}_place_orphan (file, s)
1085 lang_input_statement_type *file;
1086 asection *s;
1087{
1088 const char *secname;
aea4bd9d 1089 char *hold_section_name;
f0c87f88 1090 char *dollar = NULL;
aea4bd9d 1091 lang_output_section_statement_type *os;
5ba47421 1092 lang_statement_list_type add_child;
252b5132 1093
252b5132
RH
1094 secname = bfd_get_section_name (s->owner, s);
1095
1096 /* Look through the script to see where to place this section. */
1097
252b5132 1098 hold_section_name = xstrdup (secname);
6eec49fc
DD
1099 if (!link_info.relocateable)
1100 {
1101 dollar = strchr (hold_section_name, '$');
1102 if (dollar != NULL)
1103 *dollar = '\0';
1104 }
252b5132 1105
aea4bd9d 1106 os = lang_output_section_find (hold_section_name);
252b5132 1107
5ba47421
AM
1108 lang_list_init (&add_child);
1109
aea4bd9d
AM
1110 if (os != NULL
1111 && os->bfd_section != NULL
1112 && ((s->flags ^ os->bfd_section->flags) & (SEC_LOAD | SEC_ALLOC)) == 0)
5ba47421 1113 {
aea4bd9d 1114 wild_doit (&add_child, s, os, file);
5ba47421
AM
1115 }
1116 else
252b5132 1117 {
6a345e87 1118 struct orphan_save *place;
aea4bd9d
AM
1119 static struct orphan_save hold_text;
1120 static struct orphan_save hold_rdata;
1121 static struct orphan_save hold_data;
1122 static struct orphan_save hold_bss;
252b5132 1123 char *outsecname;
252b5132
RH
1124 lang_statement_list_type *old;
1125 lang_statement_list_type add;
1126 etree_type *address;
1127
1128 /* Try to put the new output section in a reasonable place based
1129 on the section name and section flags. */
aea4bd9d
AM
1130#define HAVE_SECTION(hold, name) \
1131(hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
1132
252b5132 1133 place = NULL;
5ba47421
AM
1134 if ((s->flags & SEC_ALLOC) == 0)
1135 ;
1136 else if ((s->flags & SEC_HAS_CONTENTS) == 0
aea4bd9d 1137 && HAVE_SECTION (hold_bss, ".bss"))
6a345e87 1138 place = &hold_bss;
252b5132 1139 else if ((s->flags & SEC_READONLY) == 0
aea4bd9d 1140 && HAVE_SECTION (hold_data, ".data"))
6a345e87 1141 place = &hold_data;
252b5132
RH
1142 else if ((s->flags & SEC_CODE) == 0
1143 && (s->flags & SEC_READONLY) != 0
aea4bd9d 1144 && HAVE_SECTION (hold_rdata, ".rdata"))
6a345e87 1145 place = &hold_rdata;
252b5132 1146 else if ((s->flags & SEC_READONLY) != 0
aea4bd9d 1147 && HAVE_SECTION (hold_text, ".text"))
6a345e87 1148 place = &hold_text;
252b5132 1149
aea4bd9d
AM
1150#undef HAVE_SECTION
1151
252b5132
RH
1152 /* Choose a unique name for the section. This will be needed if
1153 the same section name appears in the input file with
1154 different loadable or allocateable characteristics. */
1155 outsecname = xstrdup (hold_section_name);
1156 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
1157 {
1158 unsigned int len;
1159 char *newname;
1160 unsigned int i;
1161
1162 len = strlen (outsecname);
1163 newname = xmalloc (len + 5);
1164 strcpy (newname, outsecname);
1165 i = 0;
1166 do
1167 {
1168 sprintf (newname + len, "%d", i);
1169 ++i;
1170 }
1171 while (bfd_get_section_by_name (output_bfd, newname) != NULL);
1172
1173 free (outsecname);
1174 outsecname = newname;
1175 }
1176
252b5132
RH
1177 /* Start building a list of statements for this section. */
1178 old = stat_ptr;
1179 stat_ptr = &add;
1180 lang_list_init (stat_ptr);
1181
01cc8ff8
AM
1182 if (link_info.relocateable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1183 address = exp_intop ((bfd_vma) 0);
252b5132
RH
1184 else
1185 {
1186 /* All sections in an executable must be aligned to a page
1187 boundary. */
1188 address = exp_unop (ALIGN_K,
1189 exp_nameop (NAME, "__section_alignment__"));
1190 }
1191
aea4bd9d
AM
1192 os = lang_enter_output_section_statement (outsecname, address, 0,
1193 (bfd_vma) 0,
1194 (etree_type *) NULL,
1195 (etree_type *) NULL,
1196 (etree_type *) NULL);
252b5132 1197
aea4bd9d 1198 wild_doit (&add_child, s, os, file);
252b5132
RH
1199
1200 lang_leave_output_section_statement
1201 ((bfd_vma) 0, "*default*",
aea4bd9d 1202 (struct lang_output_section_phdr_list *) NULL, "*default*");
252b5132 1203
5ba47421
AM
1204 stat_ptr = old;
1205
252b5132
RH
1206 if (place != NULL)
1207 {
5ba47421
AM
1208 asection *snew, **pps;
1209
aea4bd9d 1210 snew = os->bfd_section;
5ba47421
AM
1211 if (place->os->bfd_section != NULL || place->section != NULL)
1212 {
1213 /* Shuffle the section to make the output file look neater. */
1214 if (place->section == NULL)
1215 {
1216#if 0
1217 /* Finding the end of the list is a little tricky. We
1218 make a wild stab at it by comparing section flags. */
1219 flagword first_flags = place->os->bfd_section->flags;
1220 for (pps = &place->os->bfd_section->next;
1221 *pps != NULL && (*pps)->flags == first_flags;
1222 pps = &(*pps)->next)
1223 ;
1224 place->section = pps;
1225#else
1226 /* Put orphans after the first section on the list. */
1227 place->section = &place->os->bfd_section->next;
1228#endif
1229 }
1230
1231 /* Unlink the section. */
1232 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1233 ;
1234 *pps = snew->next;
1235
1236 /* Now tack it on to the "place->os" section list. */
1237 snew->next = *place->section;
1238 *place->section = snew;
1239 }
1240 place->section = &snew->next; /* Save the end of this list. */
1241
1242 if (place->stmt == NULL)
6a345e87
AM
1243 {
1244 /* Put the new statement list right at the head. */
1245 *add.tail = place->os->header.next;
1246 place->os->header.next = add.head;
1247 }
1248 else
1249 {
1250 /* Put it after the last orphan statement we added. */
1251 *add.tail = *place->stmt;
1252 *place->stmt = add.head;
1253 }
1254 place->stmt = add.tail; /* Save the end of this list. */
252b5132 1255 }
252b5132
RH
1256 }
1257
5ba47421 1258 {
aea4bd9d 1259 lang_statement_union_type **pl = &os->children.head;
252b5132 1260
5ba47421
AM
1261 if (dollar != NULL)
1262 {
1263 boolean found_dollar;
252b5132 1264
5ba47421
AM
1265 /* The section name has a '$'. Sort it with the other '$'
1266 sections. */
252b5132 1267
5ba47421
AM
1268 found_dollar = false;
1269 for ( ; *pl != NULL; pl = &(*pl)->next)
1270 {
1271 lang_input_section_type *ls;
1272 const char *lname;
252b5132 1273
5ba47421
AM
1274 if ((*pl)->header.type != lang_input_section_enum)
1275 continue;
252b5132 1276
5ba47421 1277 ls = &(*pl)->input_section;
252b5132 1278
5ba47421
AM
1279 lname = bfd_get_section_name (ls->ifile->the_bfd, ls->section);
1280 if (strchr (lname, '$') == NULL)
1281 {
1282 if (found_dollar)
1283 break;
1284 }
1285 else
1286 {
1287 found_dollar = true;
1288 if (strcmp (secname, lname) < 0)
1289 break;
1290 }
1291 }
1292 }
1293
1294 if (add_child.head != NULL)
1295 {
1296 add_child.head->next = *pl;
1297 *pl = add_child.head;
1298 }
1299 }
252b5132
RH
1300
1301 free (hold_section_name);
1302
1303 return true;
1304}
1305
344a211f
NC
1306static int
1307gld_${EMULATION_NAME}_find_potential_libraries (name, entry)
1308 char * name;
1309 lang_input_statement_type * entry;
1310{
1311 return ldfile_open_file_search (name, entry, "", ".lib");
1312}
252b5132
RH
1313\f
1314static char *
1315gld_${EMULATION_NAME}_get_script(isfile)
1316 int *isfile;
1317EOF
1318# Scripts compiled in.
1319# sed commands to quote an ld script as a C string.
597e2591 1320sc="-f stringify.sed"
252b5132
RH
1321
1322cat >>e${EMULATION_NAME}.c <<EOF
1323{
1324 *isfile = 0;
1325
1326 if (link_info.relocateable == true && config.build_constructors == true)
1327 return
1328EOF
1329sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1330echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1331sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1332echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1333sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1334echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1335sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1336echo ' ; else return' >> e${EMULATION_NAME}.c
1337sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1338echo '; }' >> e${EMULATION_NAME}.c
1339
1340cat >>e${EMULATION_NAME}.c <<EOF
1341
1342
1343struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1344{
1345 gld_${EMULATION_NAME}_before_parse,
1346 syslib_default,
1347 hll_default,
1348 gld_${EMULATION_NAME}_after_parse,
1349 gld_${EMULATION_NAME}_after_open,
1350 after_allocation_default,
1351 set_output_arch_default,
1352 ldemul_default_target,
1353 gld_${EMULATION_NAME}_before_allocation,
1354 gld_${EMULATION_NAME}_get_script,
1355 "${EMULATION_NAME}",
1356 "${OUTPUT_FORMAT}",
1357 gld_${EMULATION_NAME}_finish, /* finish */
1358 NULL, /* create output section statements */
1359 NULL, /* open dynamic archive */
1360 gld_${EMULATION_NAME}_place_orphan,
1361 gld_${EMULATION_NAME}_set_symbols,
1362 gld_${EMULATION_NAME}_parse_args,
1363 gld_${EMULATION_NAME}_unrecognized_file,
1364 gld_${EMULATION_NAME}_list_options,
344a211f
NC
1365 gld_${EMULATION_NAME}_recognized_file,
1366 gld_${EMULATION_NAME}_find_potential_libraries
252b5132
RH
1367};
1368EOF