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