bfd/ChangeLog
[deliverable/binutils-gdb.git] / ld / emultempl / pep.em
CommitLineData
99ad8390
NC
1# This shell script emits a C file. -*- C -*-
2# It does some substitutions.
3test -z "${ENTRY}" && ENTRY="_mainCRTStartup"
4if [ -z "$MACHINE" ]; then
5 OUTPUT_ARCH=${ARCH}
6else
7 OUTPUT_ARCH=${ARCH}:${MACHINE}
8fi
9rm -f e${EMULATION_NAME}.c
10(echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
92b93329 11fragment <<EOF
bde18da4 12/* Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
f96b4a7b
NC
13 Written by Kai Tietz, OneVision Software GmbH&CoKg.
14
15 This file is part of the GNU Binutils.
99ad8390
NC
16
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
f96b4a7b 19 the Free Software Foundation; either version 3 of the License, or
99ad8390
NC
20 (at your option) any later version.
21
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
f96b4a7b
NC
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
30 MA 02110-1301, USA. */
31
99ad8390
NC
32
33/* For WINDOWS_XP64 and higher */
34/* Based on pe.em, but modified for 64 bit support. */
35
36#define TARGET_IS_${EMULATION_NAME}
37
38#define COFF_IMAGE_WITH_PE
39#define COFF_WITH_PE
40#define COFF_WITH_pex64
41
99ad8390 42#include "sysdep.h"
3db64b00 43#include "bfd.h"
99ad8390
NC
44#include "bfdlink.h"
45#include "getopt.h"
46#include "libiberty.h"
47#include "ld.h"
48#include "ldmain.h"
49#include "ldexp.h"
50#include "ldlang.h"
51#include "ldfile.h"
52#include "ldemul.h"
53#include <ldgram.h>
54#include "ldlex.h"
55#include "ldmisc.h"
56#include "ldctor.h"
57#include "coff/internal.h"
58
59/* FIXME: See bfd/peXXigen.c for why we include an architecture specific
60 header in generic PE code. */
61#include "coff/x86_64.h"
62#include "coff/pe.h"
63
64/* FIXME: This is a BFD internal header file, and we should not be
65 using it here. */
66#include "../bfd/libcoff.h"
67
68#undef AOUTSZ
69#define AOUTSZ PEPAOUTSZ
70#define PEAOUTHDR PEPAOUTHDR
71
72#include "deffile.h"
73#include "pep-dll.h"
74#include "safe-ctype.h"
75
76/* Permit the emulation parameters to override the default section
77 alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes
78 it seem that include/coff/internal.h should not define
79 PE_DEF_SECTION_ALIGNMENT. */
80#if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
81#undef PE_DEF_SECTION_ALIGNMENT
82#define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
83#endif
84
85#ifdef TARGET_IS_i386pep
86#define DLL_SUPPORT
87#endif
88
89#if defined(TARGET_IS_i386pep) || ! defined(DLL_SUPPORT)
90#define PE_DEF_SUBSYSTEM 3
91#else
92#undef NT_EXE_IMAGE_BASE
93#define NT_EXE_IMAGE_BASE 0x00010000
94#undef PE_DEF_SECTION_ALIGNMENT
95#define PE_DEF_SUBSYSTEM 2
96#undef PE_DEF_FILE_ALIGNMENT
97#define PE_DEF_FILE_ALIGNMENT 0x00000200
98#define PE_DEF_SECTION_ALIGNMENT 0x00000400
99#endif
100
101
102static struct internal_extra_pe_aouthdr pep;
103static int dll;
104static flagword real_flags = IMAGE_FILE_LARGE_ADDRESS_AWARE;
105static int support_old_code = 0;
106static lang_assignment_statement_type *image_base_statement = 0;
107
108#ifdef DLL_SUPPORT
880383ca 109static int pep_enable_stdcall_fixup = 1; /* 0=disable 1=enable (default). */
99ad8390
NC
110static char * pep_out_def_filename = NULL;
111static char * pep_implib_filename = NULL;
112static int pep_enable_auto_image_base = 0;
113static char * pep_dll_search_prefix = NULL;
114#endif
115
116extern const char *output_filename;
117
118static void
119gld_${EMULATION_NAME}_before_parse (void)
120{
121 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
122 output_filename = "${EXECUTABLE_NAME:-a.exe}";
123#ifdef DLL_SUPPORT
124 config.dynamic_link = TRUE;
125 config.has_shared = 1;
126 link_info.pei386_auto_import = -1;
6cb442d3 127 link_info.pei386_runtime_pseudo_reloc = 2; /* Use by default version 2. */
99ad8390
NC
128
129#if (PE_DEF_SUBSYSTEM == 9) || (PE_DEF_SUBSYSTEM == 2)
130 lang_default_entry ("_WinMainCRTStartup");
131#else
132 lang_default_entry ("${ENTRY}");
133#endif
134#endif
135}
136\f
137/* PE format extra command line options. */
138
139/* Used for setting flags in the PE header. */
140enum options
141{
142 OPTION_BASE_FILE = 300 + 1,
143 OPTION_DLL,
144 OPTION_FILE_ALIGNMENT,
145 OPTION_IMAGE_BASE,
146 OPTION_MAJOR_IMAGE_VERSION,
147 OPTION_MAJOR_OS_VERSION,
148 OPTION_MAJOR_SUBSYSTEM_VERSION,
149 OPTION_MINOR_IMAGE_VERSION,
150 OPTION_MINOR_OS_VERSION,
151 OPTION_MINOR_SUBSYSTEM_VERSION,
152 OPTION_SECTION_ALIGNMENT,
153 OPTION_STACK,
154 OPTION_SUBSYSTEM,
155 OPTION_HEAP,
156 OPTION_SUPPORT_OLD_CODE,
157 OPTION_OUT_DEF,
158 OPTION_EXPORT_ALL,
159 OPTION_EXCLUDE_SYMBOLS,
160 OPTION_KILL_ATS,
161 OPTION_STDCALL_ALIASES,
162 OPTION_ENABLE_STDCALL_FIXUP,
163 OPTION_DISABLE_STDCALL_FIXUP,
164 OPTION_IMPLIB_FILENAME,
165 OPTION_WARN_DUPLICATE_EXPORTS,
166 OPTION_IMP_COMPAT,
167 OPTION_ENABLE_AUTO_IMAGE_BASE,
168 OPTION_DISABLE_AUTO_IMAGE_BASE,
169 OPTION_DLL_SEARCH_PREFIX,
170 OPTION_NO_DEFAULT_EXCLUDES,
171 OPTION_DLL_ENABLE_AUTO_IMPORT,
172 OPTION_DLL_DISABLE_AUTO_IMPORT,
173 OPTION_ENABLE_EXTRA_PE_DEBUG,
174 OPTION_EXCLUDE_LIBS,
175 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC,
6cb442d3
KT
176 OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC,
177 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1,
e1c37eb5 178 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2,
ce11ba6c 179 OPTION_EXCLUDE_MODULES_FOR_IMPLIB,
88183869
DK
180 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES,
181 OPTION_ENABLE_LONG_SECTION_NAMES,
182 OPTION_DISABLE_LONG_SECTION_NAMES
99ad8390
NC
183};
184
185static void
186gld${EMULATION_NAME}_add_options
187 (int ns ATTRIBUTE_UNUSED,
188 char **shortopts ATTRIBUTE_UNUSED,
189 int nl,
190 struct option **longopts,
191 int nrl ATTRIBUTE_UNUSED,
192 struct option **really_longopts ATTRIBUTE_UNUSED)
193{
194 static const struct option xtra_long[] =
195 {
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},
ce11ba6c
KT
212 {"use-nul-prefixed-import-tables", no_argument, NULL,
213 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
99ad8390
NC
214#ifdef DLL_SUPPORT
215 /* getopt allows abbreviations, so we do this to stop it
216 from treating -o as an abbreviation for this option. */
217 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
218 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
219 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
220 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
221 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
e1c37eb5 222 {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
99ad8390
NC
223 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
224 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
225 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
226 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
227 {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
228 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
229 /* getopt() allows abbreviations, so we do this to stop it from
230 treating -c as an abbreviation for these --compat-implib. */
231 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
232 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
233 {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
234 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
235 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
236 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
237 {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
238 {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
239 {"enable-extra-pep-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
240 {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
241 {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
6cb442d3
KT
242 {"enable-runtime-pseudo-reloc-v1", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1},
243 {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2},
99ad8390 244#endif
88183869
DK
245 {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
246 {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
99ad8390
NC
247 {NULL, no_argument, NULL, 0}
248 };
249
250 *longopts = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
251 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
252}
253
254/* PE/WIN32; added routines to get the subsystem type, heap and/or stack
255 parameters which may be input from the command line. */
256
257typedef struct
258{
259 void *ptr;
260 int size;
0ead4f8d 261 bfd_vma value;
99ad8390
NC
262 char *symbol;
263 int inited;
264} definfo;
265
266#define D(field,symbol,def) {&pep.field,sizeof(pep.field), def, symbol,0}
267
268static definfo init[] =
269{
270 /* imagebase must be first */
271#define IMAGEBASEOFF 0
272 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE),
273#define DLLOFF 1
274 {&dll, sizeof(dll), 0, "__dll__", 0},
275#define MSIMAGEBASEOFF 2
dffc6a64 276 D(ImageBase,"___ImageBase", NT_EXE_IMAGE_BASE),
99ad8390
NC
277 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT),
278 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT),
279 D(MajorOperatingSystemVersion,"__major_os_version__", 4),
280 D(MinorOperatingSystemVersion,"__minor_os_version__", 0),
281 D(MajorImageVersion,"__major_image_version__", 0),
282 D(MinorImageVersion,"__minor_image_version__", 0),
283 D(MajorSubsystemVersion,"__major_subsystem_version__", 5),
284 D(MinorSubsystemVersion,"__minor_subsystem_version__", 2),
285 D(Subsystem,"__subsystem__", ${SUBSYSTEM}),
286 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000),
287 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000),
288 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000),
289 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000),
290 D(LoaderFlags,"__loader_flags__", 0x0),
291 { NULL, 0, 0, NULL, 0 }
292};
293
294
295static void
296gld_${EMULATION_NAME}_list_options (FILE *file)
297{
298 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
299 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
300 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
301 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
302 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
303 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
304 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
305 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
306 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
307 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
308 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
309 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
310 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
311 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
312 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
313#ifdef DLL_SUPPORT
314 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
315 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
316 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
317 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
318 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
e1c37eb5
DK
319 fprintf (file, _(" --exclude-modules-for-implib mod,mod,...\n"));
320 fprintf (file, _(" Exclude objects, archive members from auto\n"));
321 fprintf (file, _(" export, place into import library instead.\n"));
99ad8390
NC
322 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
323 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
324 fprintf (file, _(" --out-implib <file> Generate import library\n"));
325 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
326 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports.\n"));
327 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
328 create __imp_<SYMBOL> as well.\n"));
329 fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n\
330 unless user specifies one\n"));
331 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base. (default)\n"));
332 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
333 an importlib, use <string><basename>.dll\n\
334 in preference to lib<basename>.dll \n"));
335 fprintf (file, _(" --enable-auto-import Do sophistcated linking of _sym to\n\
336 __imp_sym for DATA references\n"));
337 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
338 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
339 adding pseudo-relocations resolved at\n\
340 runtime.\n"));
341 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
342 auto-imported DATA.\n"));
343 fprintf (file, _(" --enable-extra-pep-debug Enable verbose debug output when building\n\
344 or linking to DLLs (esp. auto-import)\n"));
88183869
DK
345 fprintf (file, _(" --enable-long-section-names Use long COFF section names even in\n\
346 executable image files\n"));
347 fprintf (file, _(" --disable-long-section-names Never use long COFF section names, even\n\
348 in object files\n"));
99ad8390
NC
349#endif
350}
351
352
353static void
0ead4f8d 354set_pep_name (char *name, bfd_vma val)
99ad8390
NC
355{
356 int i;
357
358 /* Find the name and set it. */
359 for (i = 0; init[i].ptr; i++)
360 {
361 if (strcmp (name, init[i].symbol) == 0)
362 {
363 init[i].value = val;
364 init[i].inited = 1;
365 if (strcmp (name,"__image_base__") == 0)
dffc6a64 366 set_pep_name ("___ImageBase", val);
99ad8390
NC
367 return;
368 }
369 }
370 abort ();
371}
372
373
374static void
375set_pep_subsystem (void)
376{
377 const char *sver;
378 const char *entry;
379 const char *initial_symbol_char;
380 char *end;
381 int len;
382 int i;
383 int subsystem;
384 unsigned long temp_subsystem;
385 static const struct
386 {
387 const char *name;
388 const int value;
389 const char *entry;
390 }
391 v[] =
392 {
393 { "native", 1, "NtProcessStartup" },
394 { "windows", 2, "WinMainCRTStartup" },
395 { "console", 3, "mainCRTStartup" },
396 { "posix", 7, "__PosixProcessStartup"},
397 { "wince", 9, "_WinMainCRTStartup" },
398 { "xbox", 14, "mainCRTStartup" },
399 { NULL, 0, NULL }
400 };
401 /* Entry point name for arbitrary subsystem numbers. */
402 static const char default_entry[] = "mainCRTStartup";
403
404 /* Check for the presence of a version number. */
405 sver = strchr (optarg, ':');
406 if (sver == NULL)
407 len = strlen (optarg);
408 else
409 {
410 len = sver - optarg;
411 set_pep_name ("__major_subsystem_version__",
412 strtoul (sver + 1, &end, 0));
413 if (*end == '.')
414 set_pep_name ("__minor_subsystem_version__",
415 strtoul (end + 1, &end, 0));
416 if (*end != '\0')
417 einfo (_("%P: warning: bad version number in -subsystem option\n"));
418 }
419
420 /* Check for numeric subsystem. */
421 temp_subsystem = strtoul (optarg, & end, 0);
422 if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
423 {
424 /* Search list for a numeric match to use its entry point. */
425 for (i = 0; v[i].name; i++)
426 if (v[i].value == (int) temp_subsystem)
427 break;
428
429 /* If no match, use the default. */
430 if (v[i].name != NULL)
431 entry = v[i].entry;
432 else
433 entry = default_entry;
434
435 /* Use this subsystem. */
436 subsystem = (int) temp_subsystem;
437 }
438 else
439 {
440 /* Search for subsystem by name. */
441 for (i = 0; v[i].name; i++)
442 if (strncmp (optarg, v[i].name, len) == 0
443 && v[i].name[len] == '\0')
444 break;
445
446 if (v[i].name == NULL)
447 {
448 einfo (_("%P%F: invalid subsystem type %s\n"), optarg);
449 return;
450 }
451
452 entry = v[i].entry;
453 subsystem = v[i].value;
454 }
455
456 set_pep_name ("__subsystem__", subsystem);
457
458 initial_symbol_char = ${INITIAL_SYMBOL_CHAR};
459 if (*initial_symbol_char != '\0')
460 {
461 char *alc_entry;
462
463 /* lang_default_entry expects its argument to be permanently
464 allocated, so we don't free this string. */
465 alc_entry = xmalloc (strlen (initial_symbol_char)
466 + strlen (entry)
467 + 1);
468 strcpy (alc_entry, initial_symbol_char);
469 strcat (alc_entry, entry);
470 entry = alc_entry;
471 }
472
473 lang_default_entry (entry);
474
475 return;
476}
477
478
479static void
480set_pep_value (char *name)
481{
482 char *end;
483
0ead4f8d 484 set_pep_name (name, (bfd_vma) strtoull (optarg, &end, 0));
99ad8390
NC
485
486 if (end == optarg)
487 einfo (_("%P%F: invalid hex number for PE parameter '%s'\n"), optarg);
488
489 optarg = end;
490}
491
492
493static void
494set_pep_stack_heap (char *resname, char *comname)
495{
496 set_pep_value (resname);
497
498 if (*optarg == ',')
499 {
500 optarg++;
501 set_pep_value (comname);
502 }
503 else if (*optarg)
504 einfo (_("%P%F: strange hex info for PE parameter '%s'\n"), optarg);
505}
506
507
508static bfd_boolean
509gld${EMULATION_NAME}_handle_option (int optc)
510{
511 switch (optc)
512 {
513 default:
514 return FALSE;
515
516 case OPTION_BASE_FILE:
517 link_info.base_file = fopen (optarg, FOPEN_WB);
518 if (link_info.base_file == NULL)
519 {
520 /* xgettext:c-format */
521 fprintf (stderr, _("%s: Can't open base file %s\n"),
522 program_name, optarg);
523 xexit (1);
524 }
525 break;
526
527 /* PE options. */
528 case OPTION_HEAP:
529 set_pep_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
530 break;
531 case OPTION_STACK:
532 set_pep_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
533 break;
534 case OPTION_SUBSYSTEM:
535 set_pep_subsystem ();
536 break;
537 case OPTION_MAJOR_OS_VERSION:
538 set_pep_value ("__major_os_version__");
539 break;
540 case OPTION_MINOR_OS_VERSION:
541 set_pep_value ("__minor_os_version__");
542 break;
543 case OPTION_MAJOR_SUBSYSTEM_VERSION:
544 set_pep_value ("__major_subsystem_version__");
545 break;
546 case OPTION_MINOR_SUBSYSTEM_VERSION:
547 set_pep_value ("__minor_subsystem_version__");
548 break;
549 case OPTION_MAJOR_IMAGE_VERSION:
550 set_pep_value ("__major_image_version__");
551 break;
552 case OPTION_MINOR_IMAGE_VERSION:
553 set_pep_value ("__minor_image_version__");
554 break;
555 case OPTION_FILE_ALIGNMENT:
556 set_pep_value ("__file_alignment__");
557 break;
558 case OPTION_SECTION_ALIGNMENT:
559 set_pep_value ("__section_alignment__");
560 break;
561 case OPTION_DLL:
562 set_pep_name ("__dll__", 1);
563 break;
564 case OPTION_IMAGE_BASE:
565 set_pep_value ("__image_base__");
566 break;
567 case OPTION_SUPPORT_OLD_CODE:
568 support_old_code = 1;
569 break;
ce11ba6c
KT
570 case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
571 pep_use_nul_prefixed_import_tables = TRUE;
572 break;
99ad8390
NC
573#ifdef DLL_SUPPORT
574 case OPTION_OUT_DEF:
575 pep_out_def_filename = xstrdup (optarg);
576 break;
577 case OPTION_EXPORT_ALL:
578 pep_dll_export_everything = 1;
579 break;
580 case OPTION_EXCLUDE_SYMBOLS:
e1c37eb5 581 pep_dll_add_excludes (optarg, EXCLUDESYMS);
99ad8390
NC
582 break;
583 case OPTION_EXCLUDE_LIBS:
e1c37eb5
DK
584 pep_dll_add_excludes (optarg, EXCLUDELIBS);
585 break;
586 case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
587 pep_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
99ad8390
NC
588 break;
589 case OPTION_KILL_ATS:
590 pep_dll_kill_ats = 1;
591 break;
592 case OPTION_STDCALL_ALIASES:
593 pep_dll_stdcall_aliases = 1;
594 break;
595 case OPTION_ENABLE_STDCALL_FIXUP:
596 pep_enable_stdcall_fixup = 1;
597 break;
598 case OPTION_DISABLE_STDCALL_FIXUP:
599 pep_enable_stdcall_fixup = 0;
600 break;
601 case OPTION_IMPLIB_FILENAME:
602 pep_implib_filename = xstrdup (optarg);
603 break;
604 case OPTION_WARN_DUPLICATE_EXPORTS:
605 pep_dll_warn_dup_exports = 1;
606 break;
607 case OPTION_IMP_COMPAT:
608 pep_dll_compat_implib = 1;
609 break;
610 case OPTION_ENABLE_AUTO_IMAGE_BASE:
611 pep_enable_auto_image_base = 1;
612 break;
613 case OPTION_DISABLE_AUTO_IMAGE_BASE:
614 pep_enable_auto_image_base = 0;
615 break;
616 case OPTION_DLL_SEARCH_PREFIX:
617 pep_dll_search_prefix = xstrdup (optarg);
618 break;
619 case OPTION_NO_DEFAULT_EXCLUDES:
620 pep_dll_do_default_excludes = 0;
621 break;
622 case OPTION_DLL_ENABLE_AUTO_IMPORT:
623 link_info.pei386_auto_import = 1;
624 break;
625 case OPTION_DLL_DISABLE_AUTO_IMPORT:
626 link_info.pei386_auto_import = 0;
627 break;
628 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
6cb442d3 629 link_info.pei386_runtime_pseudo_reloc = 2;
99ad8390
NC
630 break;
631 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
632 link_info.pei386_runtime_pseudo_reloc = 0;
633 break;
6cb442d3
KT
634 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V1:
635 link_info.pei386_runtime_pseudo_reloc = 1;
636 break;
637 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2:
638 link_info.pei386_runtime_pseudo_reloc = 2;
639 break;
99ad8390
NC
640 case OPTION_ENABLE_EXTRA_PE_DEBUG:
641 pep_dll_extra_pe_debug = 1;
642 break;
643#endif
88183869
DK
644 case OPTION_ENABLE_LONG_SECTION_NAMES:
645 pep_use_coff_long_section_names = 1;
646 break;
647 case OPTION_DISABLE_LONG_SECTION_NAMES:
648 pep_use_coff_long_section_names = 0;
649 break;
99ad8390
NC
650 }
651 return TRUE;
652}
653\f
654
655#ifdef DLL_SUPPORT
656static unsigned long
657strhash (const char *str)
658{
659 const unsigned char *s;
660 unsigned long hash;
661 unsigned int c;
662 unsigned int len;
663
664 hash = 0;
665 len = 0;
666 s = (const unsigned char *) str;
667 while ((c = *s++) != '\0')
668 {
669 hash += c + (c << 17);
670 hash ^= hash >> 2;
671 ++len;
672 }
673 hash += len + (len << 17);
674 hash ^= hash >> 2;
675
676 return hash;
677}
678
679/* Use the output file to create a image base for relocatable DLLs. */
680
0ead4f8d 681static bfd_vma
99ad8390
NC
682compute_dll_image_base (const char *ofile)
683{
0ead4f8d 684 bfd_vma hash = (bfd_vma) strhash (ofile);
99ad8390
NC
685 return 0x61300000 + ((hash << 16) & 0x0FFC0000);
686}
687#endif
688
689/* Assign values to the special symbols before the linker script is
690 read. */
691
692static void
693gld_${EMULATION_NAME}_set_symbols (void)
694{
695 /* Run through and invent symbols for all the
696 names and insert the defaults. */
697 int j;
99ad8390
NC
698
699 if (!init[IMAGEBASEOFF].inited)
700 {
701 if (link_info.relocatable)
702 init[IMAGEBASEOFF].value = 0;
703 else if (init[DLLOFF].value || (link_info.shared && !link_info.pie))
704#ifdef DLL_SUPPORT
705 init[IMAGEBASEOFF].value = (pep_enable_auto_image_base) ?
706 compute_dll_image_base (output_filename) : NT_DLL_IMAGE_BASE;
707#else
708 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
709#endif
710 else
711 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
712 init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
713 }
714
715 /* Don't do any symbol assignments if this is a relocatable link. */
716 if (link_info.relocatable)
717 return;
718
719 /* Glue the assignments into the abs section. */
bde18da4 720 push_stat_ptr (&abs_output_section->children);
99ad8390
NC
721
722 for (j = 0; init[j].ptr; j++)
723 {
0ead4f8d 724 bfd_vma val = init[j].value;
99ad8390
NC
725 lang_assignment_statement_type *rv;
726 rv = lang_add_assignment (exp_assop ('=', init[j].symbol,
727 exp_intop (val)));
728 if (init[j].size == sizeof (short))
0ead4f8d 729 *(short *) init[j].ptr = (short) val;
99ad8390 730 else if (init[j].size == sizeof (int))
0ead4f8d 731 *(int *) init[j].ptr = (int) val;
99ad8390 732 else if (init[j].size == sizeof (long))
0ead4f8d 733 *(long *) init[j].ptr = (long) val;
99ad8390
NC
734 /* This might be a long long or other special type. */
735 else if (init[j].size == sizeof (bfd_vma))
736 *(bfd_vma *) init[j].ptr = val;
737 else abort ();
738 if (j == IMAGEBASEOFF)
739 image_base_statement = rv;
740 }
741 /* Restore the pointer. */
bde18da4 742 pop_stat_ptr ();
99ad8390
NC
743
744 if (pep.FileAlignment > pep.SectionAlignment)
745 {
746 einfo (_("%P: warning, file alignment > section alignment.\n"));
747 }
748}
749
750/* This is called after the linker script and the command line options
751 have been read. */
752
753static void
754gld_${EMULATION_NAME}_after_parse (void)
755{
756 /* The Windows libraries are designed for the linker to treat the
757 entry point as an undefined symbol. Otherwise, the .obj that
758 defines mainCRTStartup is brought in because it is the first
759 encountered in libc.lib and it has other symbols in it which will
760 be pulled in by the link process. To avoid this, we act as
761 though the user specified -u with the entry point symbol.
762
763 This function is called after the linker script and command line
764 options have been read, so at this point we know the right entry
765 point. This function is called before the input files are
766 opened, so registering the symbol as undefined will make a
767 difference. */
768
769 if (! link_info.relocatable && entry_symbol.name != NULL)
770 ldlang_add_undef (entry_symbol.name);
771}
772
773/* pep-dll.c directly accesses pep_data_import_dll,
774 so it must be defined outside of #ifdef DLL_SUPPORT.
775 Note - this variable is deliberately not initialised.
776 This allows it to be treated as a common varaible, and only
777 exist in one incarnation in a multiple target enabled linker. */
778char * pep_data_import_dll;
779
780#ifdef DLL_SUPPORT
781static struct bfd_link_hash_entry *pep_undef_found_sym;
782
783static bfd_boolean
784pep_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
785{
786 int sl;
787 char *string = inf;
788
789 sl = strlen (string);
790 if (h->type == bfd_link_hash_defined
791 && strncmp (h->root.string, string, sl) == 0
792 && h->root.string[sl] == '@')
793 {
794 pep_undef_found_sym = h;
795 return FALSE;
796 }
797 return TRUE;
798}
799
800static void
801pep_fixup_stdcalls (void)
802{
803 static int gave_warning_message = 0;
804 struct bfd_link_hash_entry *undef, *sym;
805
806 if (pep_dll_extra_pe_debug)
807 printf ("%s\n", __FUNCTION__);
808
809 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
810 if (undef->type == bfd_link_hash_undefined)
811 {
812 char* at = strchr (undef->root.string, '@');
813 int lead_at = (*undef->root.string == '@');
814 /* For now, don't try to fixup fastcall symbols. */
815
816 if (at && !lead_at)
817 {
818 /* The symbol is a stdcall symbol, so let's look for a
819 cdecl symbol with the same name and resolve to that. */
820 char *cname = xstrdup (undef->root.string /* + lead_at */);
821 at = strchr (cname, '@');
822 *at = 0;
823 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
824
825 if (sym && sym->type == bfd_link_hash_defined)
826 {
827 undef->type = bfd_link_hash_defined;
828 undef->u.def.value = sym->u.def.value;
829 undef->u.def.section = sym->u.def.section;
830
831 if (pep_enable_stdcall_fixup == -1)
832 {
833 einfo (_("Warning: resolving %s by linking to %s\n"),
834 undef->root.string, cname);
835 if (! gave_warning_message)
836 {
837 gave_warning_message = 1;
838 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
839 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
840 }
841 }
842 }
843 }
844 else
845 {
846 /* The symbol is a cdecl symbol, so we look for stdcall
847 symbols - which means scanning the whole symbol table. */
848 pep_undef_found_sym = 0;
849 bfd_link_hash_traverse (link_info.hash, pep_undef_cdecl_match,
850 (char *) undef->root.string);
851 sym = pep_undef_found_sym;
852 if (sym)
853 {
854 undef->type = bfd_link_hash_defined;
855 undef->u.def.value = sym->u.def.value;
856 undef->u.def.section = sym->u.def.section;
857
858 if (pep_enable_stdcall_fixup == -1)
859 {
860 einfo (_("Warning: resolving %s by linking to %s\n"),
861 undef->root.string, sym->root.string);
862 if (! gave_warning_message)
863 {
864 gave_warning_message = 1;
865 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
866 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
867 }
868 }
869 }
870 }
871 }
872}
873
874static int
875make_import_fixup (arelent *rel, asection *s)
876{
877 struct bfd_symbol *sym = *rel->sym_ptr_ptr;
6cb442d3
KT
878 char addend[8];
879 bfd_vma _addend = 0;
880 int suc = 0;
99ad8390
NC
881
882 if (pep_dll_extra_pe_debug)
883 printf ("arelent: %s@%#lx: add=%li\n", sym->name,
0af1713e 884 (unsigned long) rel->address, (long) rel->addend);
99ad8390 885
6cb442d3
KT
886 memset (addend, 0, sizeof (addend));
887 switch ((rel->howto->bitsize))
888 {
889 case 8:
890 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 1);
891 if (suc && rel->howto->pc_relative)
892 _addend = (bfd_vma) ((bfd_signed_vma) ((char) bfd_get_8 (s->owner, addend)));
893 else if (suc)
894 _addend = ((bfd_vma) bfd_get_8 (s->owner, addend)) & 0xff;
895 break;
896 case 16:
897 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 2);
898 if (suc && rel->howto->pc_relative)
899 _addend = (bfd_vma) ((bfd_signed_vma) ((short) bfd_get_16 (s->owner, addend)));
900 else if (suc)
901 _addend = ((bfd_vma) bfd_get_16 (s->owner, addend)) & 0xffff;
902 break;
903 case 32:
904 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 4);
905 if (suc && rel->howto->pc_relative)
906 _addend = (bfd_vma) ((bfd_signed_vma) ((int) bfd_get_32 (s->owner, addend)));
907 else if (suc)
908 _addend = ((bfd_vma) bfd_get_32 (s->owner, addend)) & 0xffffffff;
909 break;
910 case 64:
911 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 8);
912 if (suc)
913 _addend = ((bfd_vma) bfd_get_64 (s->owner, addend));
914 break;
915 }
916 if (! suc)
99ad8390
NC
917 einfo (_("%C: Cannot get section contents - auto-import exception\n"),
918 s->owner, s, rel->address);
919
6cb442d3
KT
920 if (pep_dll_extra_pe_debug)
921 {
922 printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx", (long) _addend, (long) rel->addend, (long) rel->address);
923 if (rel->howto->pc_relative) printf (" pcrel");
924 printf (" %d bit rel.\n",(int) rel->howto->bitsize);
925 }
926 pep_create_import_fixup (rel, s, _addend);
99ad8390
NC
927
928 return 1;
929}
930
931static void
932pep_find_data_imports (void)
933{
934 struct bfd_link_hash_entry *undef, *sym;
935
936 if (link_info.pei386_auto_import == 0)
937 return;
938
939 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
940 {
941 if (undef->type == bfd_link_hash_undefined)
942 {
943 /* C++ symbols are *long*. */
944 char buf[4096];
945
946 if (pep_dll_extra_pe_debug)
947 printf ("%s:%s\n", __FUNCTION__, undef->root.string);
948
949 sprintf (buf, "__imp_%s", undef->root.string);
950
951 sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
952
953 if (sym && sym->type == bfd_link_hash_defined)
954 {
955 bfd *b = sym->u.def.section->owner;
956 asymbol **symbols;
5c1d2f5f 957 int nsyms, i;
99ad8390
NC
958
959 if (link_info.pei386_auto_import == -1)
8524166d
NC
960 {
961 static bfd_boolean warned = FALSE;
962
963 info_msg (_("Info: resolving %s by linking to %s (auto-import)\n"),
964 undef->root.string, buf);
965
966 /* PR linker/4844. */
967 if (! warned)
968 {
969 warned = TRUE;
970 einfo (_("%P: warning: auto-importing has been activated without --enable-auto-import specified on the command line.\n\
44e36d8a 971This should work unless it involves constant data structures referencing symbols from auto-imported DLLs.\n"));
8524166d
NC
972 }
973 }
99ad8390 974
5c1d2f5f
AM
975 if (!bfd_generic_link_read_symbols (b))
976 {
977 einfo (_("%B%F: could not read symbols: %E\n"), b);
978 return;
979 }
980
981 symbols = bfd_get_outsymbols (b);
982 nsyms = bfd_get_symcount (b);
99ad8390
NC
983
984 for (i = 0; i < nsyms; i++)
985 {
986 if (! CONST_STRNEQ (symbols[i]->name, "__head_"))
987 continue;
988
989 if (pep_dll_extra_pe_debug)
990 printf ("->%s\n", symbols[i]->name);
991
992 pep_data_import_dll = (char*) (symbols[i]->name +
993 sizeof ("__head_") - 1);
994 break;
995 }
996
997 pep_walk_relocs_of_symbol (&link_info, undef->root.string,
998 make_import_fixup);
999
1000 /* Let's differentiate it somehow from defined. */
1001 undef->type = bfd_link_hash_defweak;
1002 /* We replace original name with __imp_ prefixed, this
1003 1) may trash memory 2) leads to duplicate symbol generation.
1004 Still, IMHO it's better than having name poluted. */
1005 undef->root.string = sym->root.string;
1006 undef->u.def.value = sym->u.def.value;
1007 undef->u.def.section = sym->u.def.section;
1008 }
1009 }
1010 }
1011}
1012
1013static bfd_boolean
1014pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
1015{
1016 if (pep_dll_extra_pe_debug)
1017 printf ("+%s\n", h->string);
1018
1019 return TRUE;
1020}
1021#endif /* DLL_SUPPORT */
1022
1023
1024static void
1025gld_${EMULATION_NAME}_after_open (void)
1026{
1027#ifdef DLL_SUPPORT
1028 if (pep_dll_extra_pe_debug)
1029 {
1030 bfd *a;
1031 struct bfd_link_hash_entry *sym;
1032
1033 printf ("%s()\n", __FUNCTION__);
1034
1035 for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1036 printf ("-%s\n", sym->root.string);
1037 bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
1038
1039 for (a = link_info.input_bfds; a; a = a->link_next)
1040 printf ("*%s\n",a->filename);
1041 }
1042#endif
1043
1044 /* Pass the wacky PE command line options into the output bfd.
1045 FIXME: This should be done via a function, rather than by
1046 including an internal BFD header. */
1047
f13a99db
AM
1048 if (coff_data (link_info.output_bfd) == NULL
1049 || coff_data (link_info.output_bfd)->pe == 0)
1050 einfo (_("%F%P: cannot perform PE operations on non PE output file '%B'.\n"),
1051 link_info.output_bfd);
99ad8390 1052
f13a99db
AM
1053 pe_data (link_info.output_bfd)->pe_opthdr = pep;
1054 pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
1055 pe_data (link_info.output_bfd)->real_flags |= real_flags;
88183869 1056 pep_output_file_set_long_section_names (link_info.output_bfd);
99ad8390
NC
1057
1058#ifdef DLL_SUPPORT
1059 if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
1060 pep_fixup_stdcalls ();
1061
f13a99db 1062 pep_process_import_defs (link_info.output_bfd, &link_info);
99ad8390
NC
1063
1064 pep_find_data_imports ();
1065
1066#ifndef TARGET_IS_i386pep
1067 if (link_info.shared)
1068#else
1069 if (!link_info.relocatable)
1070#endif
f13a99db 1071 pep_dll_build_sections (link_info.output_bfd, &link_info);
99ad8390
NC
1072
1073#ifndef TARGET_IS_i386pep
1074 else
f13a99db 1075 pep_exe_build_sections (link_info.output_bfd, &link_info);
99ad8390
NC
1076#endif
1077#endif /* DLL_SUPPORT */
1078
1079 {
1080 /* This next chunk of code tries to detect the case where you have
1081 two import libraries for the same DLL (specifically,
1082 symbolically linking libm.a and libc.a in cygwin to
1083 libcygwin.a). In those cases, it's possible for function
1084 thunks from the second implib to be used but without the
1085 head/tail objects, causing an improper import table. We detect
1086 those cases and rename the "other" import libraries to match
1087 the one the head/tail come from, so that the linker will sort
1088 things nicely and produce a valid import table. */
1089
1090 LANG_FOR_EACH_INPUT_STATEMENT (is)
1091 {
1092 if (is->the_bfd->my_archive)
1093 {
1094 int idata2 = 0, reloc_count=0, is_imp = 0;
1095 asection *sec;
1096
1097 /* See if this is an import library thunk. */
1098 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1099 {
1100 if (strcmp (sec->name, ".idata\$2") == 0)
1101 idata2 = 1;
1102 if (CONST_STRNEQ (sec->name, ".idata\$"))
1103 is_imp = 1;
1104 reloc_count += sec->reloc_count;
1105 }
1106
1107 if (is_imp && !idata2 && reloc_count)
1108 {
1109 /* It is, look for the reference to head and see if it's
1110 from our own library. */
1111 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1112 {
1113 int i;
99ad8390
NC
1114 long relsize;
1115 asymbol **symbols;
1116 arelent **relocs;
1117 int nrelocs;
1118
99ad8390
NC
1119 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1120 if (relsize < 1)
1121 break;
1122
5c1d2f5f 1123 if (!bfd_generic_link_read_symbols (is->the_bfd))
99ad8390 1124 {
5c1d2f5f
AM
1125 einfo (_("%B%F: could not read symbols: %E\n"),
1126 is->the_bfd);
99ad8390
NC
1127 return;
1128 }
5c1d2f5f 1129 symbols = bfd_get_outsymbols (is->the_bfd);
99ad8390
NC
1130
1131 relocs = xmalloc ((size_t) relsize);
1132 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1133 relocs, symbols);
1134 if (nrelocs < 0)
1135 {
1136 free (relocs);
44e36d8a 1137 einfo ("%X%P: unable to process relocs: %E\n");
99ad8390
NC
1138 return;
1139 }
1140
1141 for (i = 0; i < nrelocs; i++)
1142 {
1143 struct bfd_symbol *s;
1144 struct bfd_link_hash_entry * blhe;
1145 char *other_bfd_filename;
1146 char *n;
1147
1148 s = (relocs[i]->sym_ptr_ptr)[0];
1149
1150 if (s->flags & BSF_LOCAL)
1151 continue;
1152
1153 /* Thunk section with reloc to another bfd. */
1154 blhe = bfd_link_hash_lookup (link_info.hash,
1155 s->name,
1156 FALSE, FALSE, TRUE);
1157
1158 if (blhe == NULL
1159 || blhe->type != bfd_link_hash_defined)
1160 continue;
1161
1162 other_bfd_filename
1163 = blhe->u.def.section->owner->my_archive
1164 ? bfd_get_filename (blhe->u.def.section->owner->my_archive)
1165 : bfd_get_filename (blhe->u.def.section->owner);
1166
1167 if (strcmp (bfd_get_filename (is->the_bfd->my_archive),
1168 other_bfd_filename) == 0)
1169 continue;
1170
1171 /* Rename this implib to match the other one. */
1172 n = xmalloc (strlen (other_bfd_filename) + 1);
1173 strcpy (n, other_bfd_filename);
1174 is->the_bfd->my_archive->filename = n;
1175 }
1176
1177 free (relocs);
1178 /* Note - we do not free the symbols,
1179 they are now cached in the BFD. */
1180 }
1181 }
1182 }
1183 }
1184 }
1185
1186 {
1187 int is_ms_arch = 0;
1188 bfd *cur_arch = 0;
1189 lang_input_statement_type *is2;
1190 lang_input_statement_type *is3;
1191
1192 /* Careful - this is a shell script. Watch those dollar signs! */
1193 /* Microsoft import libraries have every member named the same,
1194 and not in the right order for us to link them correctly. We
1195 must detect these and rename the members so that they'll link
1196 correctly. There are three types of objects: the head, the
1197 thunks, and the sentinel(s). The head is easy; it's the one
1198 with idata2. We assume that the sentinels won't have relocs,
1199 and the thunks will. It's easier than checking the symbol
1200 table for external references. */
1201 LANG_FOR_EACH_INPUT_STATEMENT (is)
1202 {
1203 if (is->the_bfd->my_archive)
1204 {
1205 char *pnt;
1206 bfd *arch = is->the_bfd->my_archive;
1207
1208 if (cur_arch != arch)
1209 {
1210 cur_arch = arch;
1211 is_ms_arch = 1;
1212
1213 for (is3 = is;
1214 is3 && is3->the_bfd->my_archive == arch;
1215 is3 = (lang_input_statement_type *) is3->next)
1216 {
1217 /* A MS dynamic import library can also contain static
1218 members, so look for the first element with a .dll
1219 extension, and use that for the remainder of the
1220 comparisons. */
1221 pnt = strrchr (is3->the_bfd->filename, '.');
1222 if (pnt != NULL && strcmp (pnt, ".dll") == 0)
1223 break;
1224 }
1225
1226 if (is3 == NULL)
1227 is_ms_arch = 0;
1228 else
1229 {
1230 /* OK, found one. Now look to see if the remaining
1231 (dynamic import) members use the same name. */
1232 for (is2 = is;
1233 is2 && is2->the_bfd->my_archive == arch;
1234 is2 = (lang_input_statement_type *) is2->next)
1235 {
1236 /* Skip static members, ie anything with a .obj
1237 extension. */
1238 pnt = strrchr (is2->the_bfd->filename, '.');
1239 if (pnt != NULL && strcmp (pnt, ".obj") == 0)
1240 continue;
1241
1242 if (strcmp (is3->the_bfd->filename,
1243 is2->the_bfd->filename))
1244 {
1245 is_ms_arch = 0;
1246 break;
1247 }
1248 }
1249 }
1250 }
1251
1252 /* This fragment might have come from an .obj file in a Microsoft
1253 import, and not an actual import record. If this is the case,
1254 then leave the filename alone. */
1255 pnt = strrchr (is->the_bfd->filename, '.');
1256
1257 if (is_ms_arch && (strcmp (pnt, ".dll") == 0))
1258 {
1259 int idata2 = 0, reloc_count=0;
1260 asection *sec;
1261 char *new_name, seq;
1262
1263 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1264 {
1265 if (strcmp (sec->name, ".idata\$2") == 0)
1266 idata2 = 1;
1267 reloc_count += sec->reloc_count;
1268 }
1269
1270 if (idata2) /* .idata2 is the TOC */
1271 seq = 'a';
1272 else if (reloc_count > 0) /* thunks */
1273 seq = 'b';
1274 else /* sentinel */
1275 seq = 'c';
1276
1277 new_name = xmalloc (strlen (is->the_bfd->filename) + 3);
1278 sprintf (new_name, "%s.%c", is->the_bfd->filename, seq);
1279 is->the_bfd->filename = new_name;
1280
1281 new_name = xmalloc (strlen (is->filename) + 3);
1282 sprintf (new_name, "%s.%c", is->filename, seq);
1283 is->filename = new_name;
1284 }
1285 }
1286 }
1287 }
1288}
1289\f
1290static void
1291gld_${EMULATION_NAME}_before_allocation (void)
1292{
1293 before_allocation_default ();
1294}
1295\f
1296#ifdef DLL_SUPPORT
1297/* This is called when an input file isn't recognized as a BFD. We
1298 check here for .DEF files and pull them in automatically. */
1299
1300static int
1301saw_option (char *option)
1302{
1303 int i;
1304
1305 for (i = 0; init[i].ptr; i++)
1306 if (strcmp (init[i].symbol, option) == 0)
1307 return init[i].inited;
1308 return 0;
1309}
1310#endif /* DLL_SUPPORT */
1311
1312static bfd_boolean
1313gld_${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1314{
1315#ifdef DLL_SUPPORT
1316 const char *ext = entry->filename + strlen (entry->filename) - 4;
1317
1318 if (strcmp (ext, ".def") == 0 || strcmp (ext, ".DEF") == 0)
1319 {
1320 pep_def_file = def_file_parse (entry->filename, pep_def_file);
1321
1322 if (pep_def_file)
1323 {
1324 int i, buflen=0, len;
1325 char *buf;
1326
1327 for (i = 0; i < pep_def_file->num_exports; i++)
1328 {
1329 len = strlen (pep_def_file->exports[i].internal_name);
1330 if (buflen < len + 2)
1331 buflen = len + 2;
1332 }
1333
1334 buf = xmalloc (buflen);
1335
1336 for (i = 0; i < pep_def_file->num_exports; i++)
1337 {
1338 struct bfd_link_hash_entry *h;
1339
1340 sprintf (buf, "_%s", pep_def_file->exports[i].internal_name);
1341
1342 h = bfd_link_hash_lookup (link_info.hash, buf, TRUE, TRUE, TRUE);
1343 if (h == (struct bfd_link_hash_entry *) NULL)
1344 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1345 if (h->type == bfd_link_hash_new)
1346 {
1347 h->type = bfd_link_hash_undefined;
1348 h->u.undef.abfd = NULL;
1349 bfd_link_add_undef (link_info.hash, h);
1350 }
1351 }
1352 free (buf);
1353
1354 /* def_file_print (stdout, pep_def_file); */
1355 if (pep_def_file->is_dll == 1)
1356 link_info.shared = 1;
1357
1358 if (pep_def_file->base_address != (bfd_vma)(-1))
1359 {
f13a99db
AM
1360 pep.ImageBase
1361 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1362 = init[IMAGEBASEOFF].value
1363 = pep_def_file->base_address;
99ad8390
NC
1364 init[IMAGEBASEOFF].inited = 1;
1365 if (image_base_statement)
f13a99db
AM
1366 image_base_statement->exp = exp_assop ('=', "__image_base__",
1367 exp_intop (pep.ImageBase));
99ad8390
NC
1368 }
1369
1370 if (pep_def_file->stack_reserve != -1
1371 && ! saw_option ("__size_of_stack_reserve__"))
1372 {
1373 pep.SizeOfStackReserve = pep_def_file->stack_reserve;
1374 if (pep_def_file->stack_commit != -1)
1375 pep.SizeOfStackCommit = pep_def_file->stack_commit;
1376 }
1377 if (pep_def_file->heap_reserve != -1
1378 && ! saw_option ("__size_of_heap_reserve__"))
1379 {
1380 pep.SizeOfHeapReserve = pep_def_file->heap_reserve;
1381 if (pep_def_file->heap_commit != -1)
1382 pep.SizeOfHeapCommit = pep_def_file->heap_commit;
1383 }
1384 return TRUE;
1385 }
1386 }
1387#endif
1388 return FALSE;
1389}
1390
1391static bfd_boolean
1392gld_${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
1393{
1394#ifdef DLL_SUPPORT
1395#ifdef TARGET_IS_i386pep
1396 pep_dll_id_target ("pei-x86-64");
1397#endif
880383ca
NC
1398 if (pep_bfd_is_dll (entry->the_bfd))
1399 return pep_implied_import_dll (entry->filename);
99ad8390
NC
1400#endif
1401 return FALSE;
1402}
1403
1404static void
1405gld_${EMULATION_NAME}_finish (void)
1406{
1407 finish_default ();
1408
1409#ifdef DLL_SUPPORT
1410 if (link_info.shared
1411 || (!link_info.relocatable && pep_def_file->num_exports != 0))
1412 {
f13a99db 1413 pep_dll_fill_sections (link_info.output_bfd, &link_info);
99ad8390 1414 if (pep_implib_filename)
e1c37eb5 1415 pep_dll_generate_implib (pep_def_file, pep_implib_filename, &link_info);
99ad8390
NC
1416 }
1417
1418 if (pep_out_def_filename)
1419 pep_dll_generate_def_file (pep_out_def_filename);
1420#endif /* DLL_SUPPORT */
1421
1422 /* I don't know where .idata gets set as code, but it shouldn't be. */
1423 {
f13a99db 1424 asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
99ad8390
NC
1425
1426 if (asec)
1427 {
1428 asec->flags &= ~SEC_CODE;
1429 asec->flags |= SEC_DATA;
1430 }
1431 }
1432}
1433
1434\f
1435/* Place an orphan section.
1436
1437 We use this to put sections in a reasonable place in the file, and
1438 to ensure that they are aligned as required.
1439
d34c1c51 1440 We handle grouped sections here as well. A section named .foo\$nn
99ad8390
NC
1441 goes into the output section .foo. All grouped sections are sorted
1442 by name.
1443
1444 Grouped sections for the default sections are handled by the
1445 default linker script using wildcards, and are sorted by
1446 sort_sections. */
1447
c2edb4b8 1448static lang_output_section_statement_type *
8a99a385
AM
1449gld_${EMULATION_NAME}_place_orphan (asection *s,
1450 const char *secname,
1451 int constraint)
99ad8390 1452{
bcacc0f5 1453 const char *orig_secname = secname;
99ad8390
NC
1454 char *dollar = NULL;
1455 lang_output_section_statement_type *os;
1456 lang_statement_list_type add_child;
1457
99ad8390 1458 /* Look through the script to see where to place this section. */
99ad8390 1459 if (!link_info.relocatable
d34c1c51 1460 && (dollar = strchr (secname, '\$')) != NULL)
99ad8390 1461 {
bcacc0f5 1462 size_t len = dollar - secname;
99ad8390 1463 char *newname = xmalloc (len + 1);
bcacc0f5 1464 memcpy (newname, secname, len);
99ad8390
NC
1465 newname[len] = '\0';
1466 secname = newname;
1467 }
1468
99ad8390
NC
1469 lang_list_init (&add_child);
1470
8a99a385
AM
1471 if (constraint == 0
1472 && (os = lang_output_section_find (secname)) != NULL
bcacc0f5
AM
1473 && os->bfd_section != NULL
1474 && (os->bfd_section->flags == 0
99ad8390
NC
1475 || ((s->flags ^ os->bfd_section->flags)
1476 & (SEC_LOAD | SEC_ALLOC)) == 0))
1477 {
1478 /* We already have an output section statement with this
bcacc0f5 1479 name, and its bfd section has compatible flags.
99ad8390
NC
1480 If the section already exists but does not have any flags set,
1481 then it has been created by the linker, probably as a result of
1482 a --section-start command line switch. */
1483 lang_add_section (&add_child, s, os);
1484 }
1485 else
1486 {
1487 static struct orphan_save hold[] =
1488 {
1489 { ".text",
1490 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1491 0, 0, 0, 0 },
1492 { ".rdata",
1493 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1494 0, 0, 0, 0 },
1495 { ".data",
1496 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1497 0, 0, 0, 0 },
1498 { ".bss",
1499 SEC_ALLOC,
1500 0, 0, 0, 0 }
1501 };
1502 enum orphan_save_index
1503 {
1504 orphan_text = 0,
1505 orphan_rodata,
1506 orphan_data,
1507 orphan_bss
1508 };
1509 static int orphan_init_done = 0;
1510 struct orphan_save *place;
1511 lang_output_section_statement_type *after;
1512 etree_type *address;
1513
1514 if (!orphan_init_done)
1515 {
1516 struct orphan_save *ho;
1517 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1518 if (ho->name != NULL)
1519 {
1520 ho->os = lang_output_section_find (ho->name);
1521 if (ho->os != NULL && ho->os->flags == 0)
1522 ho->os->flags = ho->flags;
1523 }
1524 orphan_init_done = 1;
1525 }
1526
1527 /* Try to put the new output section in a reasonable place based
1528 on the section name and section flags. */
1529
1530 place = NULL;
1531 if ((s->flags & SEC_ALLOC) == 0)
1532 ;
1533 else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
1534 place = &hold[orphan_bss];
1535 else if ((s->flags & SEC_READONLY) == 0)
1536 place = &hold[orphan_data];
1537 else if ((s->flags & SEC_CODE) == 0)
1538 place = &hold[orphan_rodata];
1539 else
1540 place = &hold[orphan_text];
1541
1542 after = NULL;
1543 if (place != NULL)
1544 {
1545 if (place->os == NULL)
1546 place->os = lang_output_section_find (place->name);
1547 after = place->os;
1548 if (after == NULL)
1549 after = lang_output_section_find_by_flags (s, &place->os, NULL);
1550 if (after == NULL)
1551 /* *ABS* is always the first output section statement. */
1552 after = (&lang_output_section_statement.head
1553 ->output_section_statement);
1554 }
1555
99ad8390
NC
1556 /* All sections in an executable must be aligned to a page boundary. */
1557 address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
8a99a385
AM
1558 os = lang_insert_orphan (s, secname, constraint, after, place, address,
1559 &add_child);
99ad8390
NC
1560 }
1561
1562 {
1563 lang_statement_union_type **pl = &os->children.head;
1564
1565 if (dollar != NULL)
1566 {
1567 bfd_boolean found_dollar;
1568
d34c1c51 1569 /* The section name has a '\$'. Sort it with the other '\$'
99ad8390
NC
1570 sections. */
1571 found_dollar = FALSE;
1572 for ( ; *pl != NULL; pl = &(*pl)->header.next)
1573 {
1574 lang_input_section_type *ls;
1575 const char *lname;
1576
1577 if ((*pl)->header.type != lang_input_section_enum)
1578 continue;
1579
1580 ls = &(*pl)->input_section;
1581
1582 lname = bfd_get_section_name (ls->section->owner, ls->section);
d34c1c51 1583 if (strchr (lname, '\$') == NULL)
99ad8390
NC
1584 {
1585 if (found_dollar)
1586 break;
1587 }
1588 else
1589 {
1590 found_dollar = TRUE;
1591 if (strcmp (orig_secname, lname) < 0)
1592 break;
1593 }
1594 }
1595 }
1596
1597 if (add_child.head != NULL)
1598 {
1599 add_child.head->header.next = *pl;
1600 *pl = add_child.head;
1601 }
1602 }
1603
c2edb4b8 1604 return os;
99ad8390
NC
1605}
1606
1607static bfd_boolean
1608gld_${EMULATION_NAME}_open_dynamic_archive
1609 (const char *arch ATTRIBUTE_UNUSED,
1610 search_dirs_type *search,
1611 lang_input_statement_type *entry)
1612{
1613 static const struct
1614 {
1615 const char * format;
1616 bfd_boolean use_prefix;
1617 }
1618 libname_fmt [] =
1619 {
1620 /* Preferred explicit import library for dll's. */
1621 { "lib%s.dll.a", FALSE },
1622 /* Alternate explicit import library for dll's. */
1623 { "%s.dll.a", FALSE },
1624 /* "libfoo.a" could be either an import lib or a static lib.
1625 For backwards compatibility, libfoo.a needs to precede
1626 libfoo.dll and foo.dll in the search. */
1627 { "lib%s.a", FALSE },
92b93329 1628 /* The 'native' spelling of an import lib name is "foo.lib". */
99ad8390
NC
1629 { "%s.lib", FALSE },
1630#ifdef DLL_SUPPORT
1631 /* Try "<prefix>foo.dll" (preferred dll name, if specified). */
1632 { "%s%s.dll", TRUE },
1633#endif
1634 /* Try "libfoo.dll" (default preferred dll name). */
1635 { "lib%s.dll", FALSE },
1636 /* Finally try 'native' dll name "foo.dll". */
1637 { "%s.dll", FALSE },
1638 /* Note: If adding more formats to this table, make sure to check to
1639 see if their length is longer than libname_fmt[0].format, and if
1640 so, update the call to xmalloc() below. */
1641 { NULL, FALSE }
1642 };
1643 static unsigned int format_max_len = 0;
1644 const char * filename;
1645 char * full_string;
1646 char * base_string;
1647 unsigned int i;
1648
1649
1650 if (! entry->is_archive)
1651 return FALSE;
1652
1653 filename = entry->filename;
1654
1655 if (format_max_len == 0)
1656 /* We need to allow space in the memory that we are going to allocate
1657 for the characters in the format string. Since the format array is
1658 static we only need to calculate this information once. In theory
1659 this value could also be computed statically, but this introduces
1660 the possibility for a discrepancy and hence a possible memory
1661 corruption. The lengths we compute here will be too long because
1662 they will include any formating characters (%s) in the strings, but
1663 this will not matter. */
1664 for (i = 0; libname_fmt[i].format; i++)
1665 if (format_max_len < strlen (libname_fmt[i].format))
1666 format_max_len = strlen (libname_fmt[i].format);
1667
1668 full_string = xmalloc (strlen (search->name)
1669 + strlen (filename)
1670 + format_max_len
1671#ifdef DLL_SUPPORT
1672 + (pep_dll_search_prefix
1673 ? strlen (pep_dll_search_prefix) : 0)
1674#endif
1675 /* Allow for the terminating NUL and for the path
1676 separator character that is inserted between
1677 search->name and the start of the format string. */
1678 + 2);
1679
1680 sprintf (full_string, "%s/", search->name);
1681 base_string = full_string + strlen (full_string);
1682
1683 for (i = 0; libname_fmt[i].format; i++)
1684 {
92b93329 1685#ifdef DLL_SUPPORT
99ad8390
NC
1686 if (libname_fmt[i].use_prefix)
1687 {
1688 if (!pep_dll_search_prefix)
1689 continue;
1690 sprintf (base_string, libname_fmt[i].format, pep_dll_search_prefix, filename);
1691 }
1692 else
1693#endif
1694 sprintf (base_string, libname_fmt[i].format, filename);
1695
1696 if (ldfile_try_open_bfd (full_string, entry))
1697 break;
1698 }
1699
1700 if (!libname_fmt[i].format)
1701 {
1702 free (full_string);
1703 return FALSE;
1704 }
1705
1706 entry->filename = full_string;
1707
1708 return TRUE;
1709}
1710
1711static int
1712gld_${EMULATION_NAME}_find_potential_libraries
1713 (char *name, lang_input_statement_type *entry)
1714{
1715 return ldfile_open_file_search (name, entry, "", ".lib");
1716}
1717\f
1718static char *
1719gld_${EMULATION_NAME}_get_script (int *isfile)
1720EOF
1721# Scripts compiled in.
1722# sed commands to quote an ld script as a C string.
1723sc="-f stringify.sed"
1724
92b93329 1725fragment <<EOF
99ad8390
NC
1726{
1727 *isfile = 0;
1728
1729 if (link_info.relocatable && config.build_constructors)
1730 return
1731EOF
1732sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1733echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c
1734sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1735echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1736sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1737echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1738sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
e2a83dd0
NC
1739if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
1740echo ' ; else if (link_info.pei386_auto_import == 1) return' >> e${EMULATION_NAME}.c
1741sed $sc ldscripts/${EMULATION_NAME}.xa >> e${EMULATION_NAME}.c
1742fi
99ad8390
NC
1743echo ' ; else return' >> e${EMULATION_NAME}.c
1744sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1745echo '; }' >> e${EMULATION_NAME}.c
1746
92b93329 1747fragment <<EOF
99ad8390
NC
1748
1749
1750struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1751{
1752 gld_${EMULATION_NAME}_before_parse,
1753 syslib_default,
1754 hll_default,
1755 gld_${EMULATION_NAME}_after_parse,
1756 gld_${EMULATION_NAME}_after_open,
1757 after_allocation_default,
1758 set_output_arch_default,
1759 ldemul_default_target,
1760 gld_${EMULATION_NAME}_before_allocation,
1761 gld_${EMULATION_NAME}_get_script,
1762 "${EMULATION_NAME}",
1763 "${OUTPUT_FORMAT}",
1764 gld_${EMULATION_NAME}_finish,
1765 NULL, /* Create output section statements. */
1766 gld_${EMULATION_NAME}_open_dynamic_archive,
1767 gld_${EMULATION_NAME}_place_orphan,
1768 gld_${EMULATION_NAME}_set_symbols,
1769 NULL, /* parse_args */
1770 gld${EMULATION_NAME}_add_options,
1771 gld${EMULATION_NAME}_handle_option,
1772 gld_${EMULATION_NAME}_unrecognized_file,
1773 gld_${EMULATION_NAME}_list_options,
1774 gld_${EMULATION_NAME}_recognized_file,
1775 gld_${EMULATION_NAME}_find_potential_libraries,
1776 NULL /* new_vers_pattern. */
1777};
1778EOF
This page took 0.177974 seconds and 4 git commands to generate.