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