]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/emultempl/pep.em
aarch64-pe support for LD, GAS and BFD
[thirdparty/binutils-gdb.git] / ld / emultempl / pep.em
CommitLineData
99ad8390
NC
1# This shell script emits a C file. -*- C -*-
2# It does some substitutions.
99ad8390
NC
3if [ -z "$MACHINE" ]; then
4 OUTPUT_ARCH=${ARCH}
5else
6 OUTPUT_ARCH=${ARCH}:${MACHINE}
7fi
a07ee50e
CV
8
9case ${target} in
10 *-*-cygwin*)
11 move_default_addr_high=1
ef6379e1 12 cygwin_behavior=1
a07ee50e
CV
13 ;;
14 *)
15 move_default_addr_high=0;
ef6379e1 16 cygwin_behavior=0;
a07ee50e
CV
17 ;;
18esac
19
99ad8390
NC
20rm -f e${EMULATION_NAME}.c
21(echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-)
92b93329 22fragment <<EOF
a2c58332 23/* Copyright (C) 2006-2022 Free Software Foundation, Inc.
f96b4a7b
NC
24 Written by Kai Tietz, OneVision Software GmbH&CoKg.
25
26 This file is part of the GNU Binutils.
99ad8390
NC
27
28 This program is free software; you can redistribute it and/or modify
29 it under the terms of the GNU General Public License as published by
f96b4a7b 30 the Free Software Foundation; either version 3 of the License, or
99ad8390
NC
31 (at your option) any later version.
32
33 This program is distributed in the hope that it will be useful,
34 but WITHOUT ANY WARRANTY; without even the implied warranty of
35 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 GNU General Public License for more details.
37
38 You should have received a copy of the GNU General Public License
39 along with this program; if not, write to the Free Software
f96b4a7b
NC
40 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
41 MA 02110-1301, USA. */
42
99ad8390
NC
43
44/* For WINDOWS_XP64 and higher */
45/* Based on pe.em, but modified for 64 bit support. */
46
47#define TARGET_IS_${EMULATION_NAME}
48
49#define COFF_IMAGE_WITH_PE
50#define COFF_WITH_PE
c60b3806
JT
51#ifdef TARGET_IS_aarch64pe
52#define COFF_WITH_peAArch64
53#elif defined (TARGET_IS_i386pep)
99ad8390 54#define COFF_WITH_pex64
c60b3806 55#endif
99ad8390 56
99ad8390 57#include "sysdep.h"
3db64b00 58#include "bfd.h"
99ad8390 59#include "bfdlink.h"
1ff6de03 60#include "ctf-api.h"
99ad8390
NC
61#include "getopt.h"
62#include "libiberty.h"
42627821 63#include "filenames.h"
99ad8390
NC
64#include "ld.h"
65#include "ldmain.h"
66#include "ldexp.h"
67#include "ldlang.h"
68#include "ldfile.h"
69#include "ldemul.h"
70#include <ldgram.h>
71#include "ldlex.h"
72#include "ldmisc.h"
73#include "ldctor.h"
61e2488c 74#include "ldbuildid.h"
99ad8390
NC
75#include "coff/internal.h"
76
77/* FIXME: See bfd/peXXigen.c for why we include an architecture specific
78 header in generic PE code. */
c60b3806
JT
79#ifdef TARGET_IS_i386pep
80# include "coff/x86_64.h"
81#elif defined TARGET_IS_aarch64pe
82# include "coff/aarch64.h"
83#endif
99ad8390
NC
84#include "coff/pe.h"
85
61e2488c 86/* FIXME: These are BFD internal header files, and we should not be
99ad8390
NC
87 using it here. */
88#include "../bfd/libcoff.h"
61e2488c 89#include "../bfd/libpei.h"
99ad8390
NC
90
91#undef AOUTSZ
92#define AOUTSZ PEPAOUTSZ
93#define PEAOUTHDR PEPAOUTHDR
94
95#include "deffile.h"
96#include "pep-dll.h"
97#include "safe-ctype.h"
98
99/* Permit the emulation parameters to override the default section
100 alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes
101 it seem that include/coff/internal.h should not define
102 PE_DEF_SECTION_ALIGNMENT. */
103#if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT}
104#undef PE_DEF_SECTION_ALIGNMENT
105#define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT}
106#endif
107
108#ifdef TARGET_IS_i386pep
109#define DLL_SUPPORT
110#endif
111
ef6379e1
JD
112#define DEFAULT_DLL_CHARACTERISTICS (${cygwin_behavior} ? 0 : \
113 IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE \
514b4e19 114 | IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA \
ef6379e1 115 | IMAGE_DLL_CHARACTERISTICS_NX_COMPAT)
514b4e19 116
99ad8390
NC
117#if defined(TARGET_IS_i386pep) || ! defined(DLL_SUPPORT)
118#define PE_DEF_SUBSYSTEM 3
a07ee50e
CV
119#undef NT_EXE_IMAGE_BASE
120#define NT_EXE_IMAGE_BASE \
121 ((bfd_vma) (${move_default_addr_high} ? 0x100400000LL \
514b4e19 122 : 0x140000000LL))
a07ee50e
CV
123#undef NT_DLL_IMAGE_BASE
124#define NT_DLL_IMAGE_BASE \
125 ((bfd_vma) (${move_default_addr_high} ? 0x400000000LL \
514b4e19 126 : 0x180000000LL))
a07ee50e
CV
127#undef NT_DLL_AUTO_IMAGE_BASE
128#define NT_DLL_AUTO_IMAGE_BASE \
129 ((bfd_vma) (${move_default_addr_high} ? 0x400000000LL \
514b4e19 130 : 0x1C0000000LL))
a07ee50e
CV
131#undef NT_DLL_AUTO_IMAGE_MASK
132#define NT_DLL_AUTO_IMAGE_MASK \
133 ((bfd_vma) (${move_default_addr_high} ? 0x1ffff0000LL \
514b4e19 134 : 0x1ffff0000LL))
99ad8390
NC
135#else
136#undef NT_EXE_IMAGE_BASE
a07ee50e
CV
137#define NT_EXE_IMAGE_BASE \
138 ((bfd_vma) (${move_default_addr_high} ? 0x100010000LL \
139 : 0x10000LL))
140#undef NT_DLL_IMAGE_BASE
141#define NT_DLL_IMAGE_BASE \
142 ((bfd_vma) (${move_default_addr_high} ? 0x110000000LL \
143 : 0x10000000LL))
144#undef NT_DLL_AUTO_IMAGE_BASE
145#define NT_DLL_AUTO_IMAGE_BASE \
146 ((bfd_vma) (${move_default_addr_high} ? 0x120000000LL \
147 : 0x61300000LL))
148#undef NT_DLL_AUTO_IMAGE_MASK
149#define NT_DLL_AUTO_IMAGE_MASK \
150 ((bfd_vma) (${move_default_addr_high} ? 0x0ffff0000LL \
151 : 0x0ffc0000LL))
99ad8390
NC
152#undef PE_DEF_SECTION_ALIGNMENT
153#define PE_DEF_SUBSYSTEM 2
154#undef PE_DEF_FILE_ALIGNMENT
155#define PE_DEF_FILE_ALIGNMENT 0x00000200
156#define PE_DEF_SECTION_ALIGNMENT 0x00000400
157#endif
158
99ad8390
NC
159static struct internal_extra_pe_aouthdr pep;
160static int dll;
decfa41e 161static int pep_subsystem = ${SUBSYSTEM};
99ad8390
NC
162static flagword real_flags = IMAGE_FILE_LARGE_ADDRESS_AWARE;
163static int support_old_code = 0;
164static lang_assignment_statement_type *image_base_statement = 0;
514b4e19 165static unsigned short pe_dll_characteristics = DEFAULT_DLL_CHARACTERISTICS;
f38a2680 166static bool insert_timestamp = true;
61e2488c 167static const char *emit_build_id;
99ad8390
NC
168
169#ifdef DLL_SUPPORT
880383ca 170static int pep_enable_stdcall_fixup = 1; /* 0=disable 1=enable (default). */
99ad8390 171static char * pep_out_def_filename = NULL;
99ad8390
NC
172static int pep_enable_auto_image_base = 0;
173static char * pep_dll_search_prefix = NULL;
174#endif
175
176extern const char *output_filename;
177
45692d91
MS
178static int
179is_underscoring (void)
99ad8390 180{
d74720d2
KT
181 int u = 0;
182 if (pep_leading_underscore != -1)
183 return pep_leading_underscore;
184 if (!bfd_get_target_info ("${OUTPUT_FORMAT}", NULL, NULL, &u, NULL))
b804e4af
KT
185 bfd_get_target_info ("${RELOCATEABLE_OUTPUT_FORMAT}", NULL, NULL, &u, NULL);
186
187 if (u == -1)
188 abort ();
d74720d2
KT
189 pep_leading_underscore = (u != 0 ? 1 : 0);
190 return pep_leading_underscore;
191}
b804e4af 192
b53b12c8
MS
193/* A case insensitive comparison, regardless of the host platform, used for
194 comparing file extensions. */
195static int
196fileext_cmp (const char *s1, const char *s2)
197{
198 for (;;)
199 {
200 int c1 = TOLOWER (*s1++);
201 int c2 = *s2++; /* Assumed to be lower case from the caller. */
202
203 if (c1 != c2)
204 return (c1 - c2);
205
206 if (c1 == '\0')
207 return 0;
208 }
209}
d74720d2
KT
210
211static void
a9261391 212gld${EMULATION_NAME}_before_parse (void)
d74720d2
KT
213{
214 is_underscoring ();
99ad8390
NC
215 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
216 output_filename = "${EXECUTABLE_NAME:-a.exe}";
217#ifdef DLL_SUPPORT
f38a2680 218 input_flags.dynamic = true;
99ad8390 219 config.has_shared = 1;
6ec6c79c 220 link_info.pei386_auto_import = 1;
6cb442d3 221 link_info.pei386_runtime_pseudo_reloc = 2; /* Use by default version 2. */
99ad8390
NC
222#endif
223}
224\f
225/* PE format extra command line options. */
226
227/* Used for setting flags in the PE header. */
228enum options
229{
230 OPTION_BASE_FILE = 300 + 1,
231 OPTION_DLL,
232 OPTION_FILE_ALIGNMENT,
233 OPTION_IMAGE_BASE,
234 OPTION_MAJOR_IMAGE_VERSION,
235 OPTION_MAJOR_OS_VERSION,
236 OPTION_MAJOR_SUBSYSTEM_VERSION,
237 OPTION_MINOR_IMAGE_VERSION,
238 OPTION_MINOR_OS_VERSION,
239 OPTION_MINOR_SUBSYSTEM_VERSION,
240 OPTION_SECTION_ALIGNMENT,
241 OPTION_STACK,
242 OPTION_SUBSYSTEM,
243 OPTION_HEAP,
244 OPTION_SUPPORT_OLD_CODE,
245 OPTION_OUT_DEF,
246 OPTION_EXPORT_ALL,
247 OPTION_EXCLUDE_SYMBOLS,
2927aaca 248 OPTION_EXCLUDE_ALL_SYMBOLS,
99ad8390
NC
249 OPTION_KILL_ATS,
250 OPTION_STDCALL_ALIASES,
251 OPTION_ENABLE_STDCALL_FIXUP,
252 OPTION_DISABLE_STDCALL_FIXUP,
99ad8390
NC
253 OPTION_WARN_DUPLICATE_EXPORTS,
254 OPTION_IMP_COMPAT,
255 OPTION_ENABLE_AUTO_IMAGE_BASE,
256 OPTION_DISABLE_AUTO_IMAGE_BASE,
257 OPTION_DLL_SEARCH_PREFIX,
258 OPTION_NO_DEFAULT_EXCLUDES,
259 OPTION_DLL_ENABLE_AUTO_IMPORT,
260 OPTION_DLL_DISABLE_AUTO_IMPORT,
261 OPTION_ENABLE_EXTRA_PE_DEBUG,
262 OPTION_EXCLUDE_LIBS,
263 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC,
6cb442d3 264 OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC,
e1c37eb5 265 OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2,
ce11ba6c 266 OPTION_EXCLUDE_MODULES_FOR_IMPLIB,
88183869 267 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES,
522f09cd
KT
268 OPTION_NO_LEADING_UNDERSCORE,
269 OPTION_LEADING_UNDERSCORE,
88183869 270 OPTION_ENABLE_LONG_SECTION_NAMES,
2f563b51 271 OPTION_DISABLE_LONG_SECTION_NAMES,
2d5c3743 272 OPTION_HIGH_ENTROPY_VA,
2f563b51
DK
273 OPTION_DYNAMIC_BASE,
274 OPTION_FORCE_INTEGRITY,
275 OPTION_NX_COMPAT,
276 OPTION_NO_ISOLATION,
277 OPTION_NO_SEH,
278 OPTION_NO_BIND,
279 OPTION_WDM_DRIVER,
0cb112f7 280 OPTION_INSERT_TIMESTAMP,
eeb14e5a 281 OPTION_NO_INSERT_TIMESTAMP,
61e2488c 282 OPTION_TERMINAL_SERVER_AWARE,
dc9bd8c9 283 OPTION_BUILD_ID,
514b4e19
JD
284 OPTION_ENABLE_RELOC_SECTION,
285 OPTION_DISABLE_RELOC_SECTION,
286 OPTION_DISABLE_HIGH_ENTROPY_VA,
287 OPTION_DISABLE_DYNAMIC_BASE,
288 OPTION_DISABLE_FORCE_INTEGRITY,
289 OPTION_DISABLE_NX_COMPAT,
290 OPTION_DISABLE_NO_ISOLATION,
291 OPTION_DISABLE_NO_SEH,
292 OPTION_DISABLE_NO_BIND,
293 OPTION_DISABLE_WDM_DRIVER,
294 OPTION_DISABLE_TERMINAL_SERVER_AWARE
99ad8390
NC
295};
296
297static void
298gld${EMULATION_NAME}_add_options
299 (int ns ATTRIBUTE_UNUSED,
300 char **shortopts ATTRIBUTE_UNUSED,
301 int nl,
302 struct option **longopts,
303 int nrl ATTRIBUTE_UNUSED,
304 struct option **really_longopts ATTRIBUTE_UNUSED)
305{
306 static const struct option xtra_long[] =
307 {
308 /* PE options */
309 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
310 {"dll", no_argument, NULL, OPTION_DLL},
311 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
312 {"heap", required_argument, NULL, OPTION_HEAP},
313 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
314 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
315 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
316 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
317 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
318 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
319 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
320 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
321 {"stack", required_argument, NULL, OPTION_STACK},
322 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
323 {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
ce11ba6c
KT
324 {"use-nul-prefixed-import-tables", no_argument, NULL,
325 OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
522f09cd
KT
326 {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
327 {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE},
99ad8390
NC
328#ifdef DLL_SUPPORT
329 /* getopt allows abbreviations, so we do this to stop it
330 from treating -o as an abbreviation for this option. */
331 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
332 {"output-def", required_argument, NULL, OPTION_OUT_DEF},
333 {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL},
334 {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS},
2927aaca 335 {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS},
99ad8390 336 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
e1c37eb5 337 {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB},
99ad8390
NC
338 {"kill-at", no_argument, NULL, OPTION_KILL_ATS},
339 {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES},
340 {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
341 {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
99ad8390
NC
342 {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS},
343 /* getopt() allows abbreviations, so we do this to stop it from
344 treating -c as an abbreviation for these --compat-implib. */
345 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
346 {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT},
347 {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
348 {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
349 {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX},
350 {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
351 {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT},
352 {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT},
353 {"enable-extra-pep-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG},
354 {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC},
355 {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC},
6cb442d3 356 {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2},
99ad8390 357#endif
88183869
DK
358 {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES},
359 {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES},
2d5c3743 360 {"high-entropy-va", no_argument, NULL, OPTION_HIGH_ENTROPY_VA},
2f563b51
DK
361 {"dynamicbase",no_argument, NULL, OPTION_DYNAMIC_BASE},
362 {"forceinteg", no_argument, NULL, OPTION_FORCE_INTEGRITY},
363 {"nxcompat", no_argument, NULL, OPTION_NX_COMPAT},
364 {"no-isolation", no_argument, NULL, OPTION_NO_ISOLATION},
365 {"no-seh", no_argument, NULL, OPTION_NO_SEH},
366 {"no-bind", no_argument, NULL, OPTION_NO_BIND},
367 {"wdmdriver", no_argument, NULL, OPTION_WDM_DRIVER},
d127ecce 368 {"tsaware", no_argument, NULL, OPTION_TERMINAL_SERVER_AWARE},
0cb112f7 369 {"insert-timestamp", no_argument, NULL, OPTION_INSERT_TIMESTAMP},
eeb14e5a 370 {"no-insert-timestamp", no_argument, NULL, OPTION_NO_INSERT_TIMESTAMP},
61e2488c 371 {"build-id", optional_argument, NULL, OPTION_BUILD_ID},
dc9bd8c9 372 {"enable-reloc-section", no_argument, NULL, OPTION_ENABLE_RELOC_SECTION},
514b4e19
JD
373 {"disable-reloc-section", no_argument, NULL, OPTION_DISABLE_RELOC_SECTION},
374 {"disable-high-entropy-va", no_argument, NULL, OPTION_DISABLE_HIGH_ENTROPY_VA},
375 {"disable-dynamicbase",no_argument, NULL, OPTION_DISABLE_DYNAMIC_BASE},
376 {"disable-forceinteg", no_argument, NULL, OPTION_DISABLE_FORCE_INTEGRITY},
377 {"disable-nxcompat", no_argument, NULL, OPTION_DISABLE_NX_COMPAT},
378 {"disable-no-isolation", no_argument, NULL, OPTION_DISABLE_NO_ISOLATION},
379 {"disable-no-seh", no_argument, NULL, OPTION_DISABLE_NO_SEH},
380 {"disable-no-bind", no_argument, NULL, OPTION_DISABLE_NO_BIND},
381 {"disable-wdmdriver", no_argument, NULL, OPTION_DISABLE_WDM_DRIVER},
382 {"disable-tsaware", no_argument, NULL, OPTION_DISABLE_TERMINAL_SERVER_AWARE},
d127ecce 383 {NULL, no_argument, NULL, 0}
99ad8390
NC
384 };
385
d127ecce
AM
386 *longopts
387 = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
99ad8390
NC
388 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
389}
390
391/* PE/WIN32; added routines to get the subsystem type, heap and/or stack
392 parameters which may be input from the command line. */
393
394typedef struct
395{
396 void *ptr;
397 int size;
0ead4f8d 398 bfd_vma value;
99ad8390
NC
399 char *symbol;
400 int inited;
522f09cd
KT
401 /* FALSE for an assembly level symbol and TRUE for a C visible symbol.
402 C visible symbols can be prefixed by underscore dependent on target's
403 settings. */
f38a2680 404 bool is_c_symbol;
99ad8390
NC
405} definfo;
406
522f09cd
KT
407#define GET_INIT_SYMBOL_NAME(IDX) \
408 (init[(IDX)].symbol \
535b785f 409 + ((!init[(IDX)].is_c_symbol || is_underscoring () == 1) ? 0 : 1))
522f09cd
KT
410
411/* Decorates the C visible symbol by underscore, if target requires. */
412#define U(CSTR) \
d74720d2 413 ((is_underscoring () == 0) ? CSTR : "_" CSTR)
522f09cd 414
61e2488c 415#define D(field,symbol,def,usc) {&pep.field, sizeof (pep.field), def, symbol, 0, usc}
99ad8390
NC
416
417static definfo init[] =
418{
419 /* imagebase must be first */
420#define IMAGEBASEOFF 0
f38a2680 421 D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE, false),
99ad8390 422#define DLLOFF 1
f38a2680 423 {&dll, sizeof(dll), 0, "__dll__", 0, false},
99ad8390 424#define MSIMAGEBASEOFF 2
f38a2680
AM
425 D(ImageBase, "___ImageBase", NT_EXE_IMAGE_BASE, true),
426 D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT, false),
427 D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT, false),
428 D(MajorOperatingSystemVersion,"__major_os_version__", 4, false),
429 D(MinorOperatingSystemVersion,"__minor_os_version__", 0, false),
430 D(MajorImageVersion,"__major_image_version__", 0, false),
431 D(MinorImageVersion,"__minor_image_version__", 0, false),
432 D(MajorSubsystemVersion,"__major_subsystem_version__", 5, false),
433 D(MinorSubsystemVersion,"__minor_subsystem_version__", 2, false),
434 D(Subsystem,"__subsystem__", ${SUBSYSTEM}, false),
435 D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000, false),
436 D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000, false),
437 D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000, false),
438 D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000, false),
439 D(LoaderFlags,"__loader_flags__", 0x0, false),
440 D(DllCharacteristics, "__dll_characteristics__", DEFAULT_DLL_CHARACTERISTICS, false),
441 { NULL, 0, 0, NULL, 0, false}
99ad8390
NC
442};
443
444
445static void
a9261391 446gld${EMULATION_NAME}_list_options (FILE *file)
99ad8390
NC
447{
448 fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n"));
449 fprintf (file, _(" --dll Set image base to the default for DLLs\n"));
450 fprintf (file, _(" --file-alignment <size> Set file alignment\n"));
451 fprintf (file, _(" --heap <size> Set initial size of the heap\n"));
452 fprintf (file, _(" --image-base <address> Set start address of the executable\n"));
453 fprintf (file, _(" --major-image-version <number> Set version number of the executable\n"));
454 fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n"));
455 fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n"));
456 fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n"));
457 fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n"));
458 fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n"));
459 fprintf (file, _(" --section-alignment <size> Set section alignment\n"));
460 fprintf (file, _(" --stack <size> Set size of the initial stack\n"));
461 fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n"));
462 fprintf (file, _(" --support-old-code Support interworking with old code\n"));
522f09cd 463 fprintf (file, _(" --[no-]leading-underscore Set explicit symbol underscore prefix mode\n"));
df5f2391 464 fprintf (file, _(" --[no-]insert-timestamp Use a real timestamp rather than zero (default)\n"));
0cb112f7 465 fprintf (file, _(" This makes binaries non-deterministic\n"));
99ad8390
NC
466#ifdef DLL_SUPPORT
467 fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n"));
468 fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n"));
469 fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n"));
470 fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n"));
2927aaca 471 fprintf (file, _(" --exclude-all-symbols Exclude all symbols from automatic export\n"));
99ad8390 472 fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n"));
e1c37eb5
DK
473 fprintf (file, _(" --exclude-modules-for-implib mod,mod,...\n"));
474 fprintf (file, _(" Exclude objects, archive members from auto\n"));
df5f2391 475 fprintf (file, _(" export, place into import library instead\n"));
99ad8390
NC
476 fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n"));
477 fprintf (file, _(" --kill-at Remove @nn from exported symbols\n"));
99ad8390 478 fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n"));
df5f2391 479 fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports\n"));
99ad8390 480 fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\
df5f2391 481 create __imp_<SYMBOL> as well\n"));
99ad8390
NC
482 fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n\
483 unless user specifies one\n"));
df5f2391 484 fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base (default)\n"));
99ad8390
NC
485 fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\
486 an importlib, use <string><basename>.dll\n\
487 in preference to lib<basename>.dll \n"));
c85f715a 488 fprintf (file, _(" --enable-auto-import Do sophisticated linking of _sym to\n\
99ad8390
NC
489 __imp_sym for DATA references\n"));
490 fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n"));
491 fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\
492 adding pseudo-relocations resolved at\n\
df5f2391 493 runtime\n"));
99ad8390 494 fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\
df5f2391 495 auto-imported DATA\n"));
99ad8390
NC
496 fprintf (file, _(" --enable-extra-pep-debug Enable verbose debug output when building\n\
497 or linking to DLLs (esp. auto-import)\n"));
88183869
DK
498 fprintf (file, _(" --enable-long-section-names Use long COFF section names even in\n\
499 executable image files\n"));
500 fprintf (file, _(" --disable-long-section-names Never use long COFF section names, even\n\
501 in object files\n"));
514b4e19 502 fprintf (file, _(" --[disable-]high-entropy-va Image is compatible with 64-bit address space\n\
2d5c3743 503 layout randomization (ASLR)\n"));
514b4e19 504 fprintf (file, _(" --[disable-]dynamicbase Image base address may be relocated using\n\
df5f2391 505 address space layout randomization (ASLR)\n"));
dc9bd8c9 506 fprintf (file, _(" --enable-reloc-section Create the base relocation table\n"));
514b4e19
JD
507 fprintf (file, _(" --disable-reloc-section Do not create the base relocation table\n"));
508 fprintf (file, _(" --[disable-]forceinteg Code integrity checks are enforced\n"));
509 fprintf (file, _(" --[disable-]nxcompat Image is compatible with data execution\n\
510 prevention\n"));
511 fprintf (file, _(" --[disable-]no-isolation Image understands isolation but do not\n\
512 isolate the image\n"));
513 fprintf (file, _(" --[disable-]no-seh Image does not use SEH; no SE handler may\n\
df5f2391 514 be called in this image\n"));
514b4e19
JD
515 fprintf (file, _(" --[disable-]no-bind Do not bind this image\n"));
516 fprintf (file, _(" --[disable-]wdmdriver Driver uses the WDM model\n"));
517 fprintf (file, _(" --[disable-]tsaware Image is Terminal Server aware\n"));
518 fprintf (file, _(" --build-id[=STYLE] Generate build ID\n"));
99ad8390
NC
519#endif
520}
521
522
523static void
0ead4f8d 524set_pep_name (char *name, bfd_vma val)
99ad8390 525{
d74720d2
KT
526 int i;
527 is_underscoring ();
99ad8390
NC
528 /* Find the name and set it. */
529 for (i = 0; init[i].ptr; i++)
530 {
522f09cd 531 if (strcmp (name, GET_INIT_SYMBOL_NAME (i)) == 0)
99ad8390
NC
532 {
533 init[i].value = val;
534 init[i].inited = 1;
535 if (strcmp (name,"__image_base__") == 0)
522f09cd 536 set_pep_name (U ("__ImageBase"), val);
99ad8390
NC
537 return;
538 }
539 }
540 abort ();
541}
542
decfa41e
KT
543static void
544set_entry_point (void)
545{
546 const char *entry;
547 const char *initial_symbol_char;
d74720d2 548 int i;
decfa41e
KT
549
550 static const struct
6c19b93b
AM
551 {
552 const int value;
553 const char *entry;
554 }
decfa41e
KT
555 v[] =
556 {
557 { 1, "NtProcessStartup" },
558 { 2, "WinMainCRTStartup" },
559 { 3, "mainCRTStartup" },
560 { 7, "__PosixProcessStartup" },
561 { 9, "WinMainCRTStartup" },
562 {14, "mainCRTStartup" },
563 { 0, NULL }
564 };
565
566 /* Entry point name for arbitrary subsystem numbers. */
567 static const char default_entry[] = "mainCRTStartup";
568
307f2595 569 if (bfd_link_dll (&link_info) || dll)
decfa41e
KT
570 {
571 entry = "DllMainCRTStartup";
572 }
573 else
574 {
575 for (i = 0; v[i].entry; i++)
6c19b93b
AM
576 if (v[i].value == pep_subsystem)
577 break;
decfa41e
KT
578
579 /* If no match, use the default. */
580 if (v[i].entry != NULL)
6c19b93b 581 entry = v[i].entry;
decfa41e 582 else
6c19b93b 583 entry = default_entry;
decfa41e
KT
584 }
585
522f09cd 586 /* Now we check target's default for getting proper symbol_char. */
d74720d2 587 initial_symbol_char = (is_underscoring () != 0 ? "_" : "");
522f09cd 588
decfa41e
KT
589 if (*initial_symbol_char != '\0')
590 {
591 char *alc_entry;
592
593 /* lang_default_entry expects its argument to be permanently
594 allocated, so we don't free this string. */
595 alc_entry = xmalloc (strlen (initial_symbol_char)
596 + strlen (entry)
597 + 1);
598 strcpy (alc_entry, initial_symbol_char);
599 strcat (alc_entry, entry);
600 entry = alc_entry;
601 }
602
603 lang_default_entry (entry);
604}
99ad8390
NC
605
606static void
607set_pep_subsystem (void)
608{
609 const char *sver;
99ad8390
NC
610 char *end;
611 int len;
612 int i;
99ad8390
NC
613 unsigned long temp_subsystem;
614 static const struct
615 {
616 const char *name;
617 const int value;
99ad8390
NC
618 }
619 v[] =
620 {
decfa41e
KT
621 { "native", 1 },
622 { "windows", 2 },
623 { "console", 3 },
624 { "posix", 7 },
625 { "wince", 9 },
626 { "xbox", 14 },
627 { NULL, 0 }
99ad8390 628 };
99ad8390
NC
629
630 /* Check for the presence of a version number. */
631 sver = strchr (optarg, ':');
632 if (sver == NULL)
633 len = strlen (optarg);
634 else
635 {
636 len = sver - optarg;
637 set_pep_name ("__major_subsystem_version__",
638 strtoul (sver + 1, &end, 0));
639 if (*end == '.')
640 set_pep_name ("__minor_subsystem_version__",
641 strtoul (end + 1, &end, 0));
642 if (*end != '\0')
643 einfo (_("%P: warning: bad version number in -subsystem option\n"));
644 }
645
646 /* Check for numeric subsystem. */
647 temp_subsystem = strtoul (optarg, & end, 0);
648 if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536))
649 {
650 /* Search list for a numeric match to use its entry point. */
651 for (i = 0; v[i].name; i++)
652 if (v[i].value == (int) temp_subsystem)
653 break;
654
99ad8390 655 /* Use this subsystem. */
decfa41e 656 pep_subsystem = (int) temp_subsystem;
99ad8390
NC
657 }
658 else
659 {
660 /* Search for subsystem by name. */
661 for (i = 0; v[i].name; i++)
662 if (strncmp (optarg, v[i].name, len) == 0
663 && v[i].name[len] == '\0')
664 break;
665
666 if (v[i].name == NULL)
667 {
df5f2391 668 einfo (_("%F%P: invalid subsystem type %s\n"), optarg);
99ad8390
NC
669 return;
670 }
671
decfa41e 672 pep_subsystem = v[i].value;
99ad8390
NC
673 }
674
decfa41e 675 set_pep_name ("__subsystem__", pep_subsystem);
99ad8390
NC
676
677 return;
678}
679
680
681static void
682set_pep_value (char *name)
683{
684 char *end;
685
0ead4f8d 686 set_pep_name (name, (bfd_vma) strtoull (optarg, &end, 0));
99ad8390
NC
687
688 if (end == optarg)
df5f2391 689 einfo (_("%F%P: invalid hex number for PE parameter '%s'\n"), optarg);
99ad8390
NC
690
691 optarg = end;
692}
693
694
695static void
696set_pep_stack_heap (char *resname, char *comname)
697{
698 set_pep_value (resname);
699
700 if (*optarg == ',')
701 {
702 optarg++;
703 set_pep_value (comname);
704 }
705 else if (*optarg)
df5f2391 706 einfo (_("%F%P: strange hex info for PE parameter '%s'\n"), optarg);
99ad8390
NC
707}
708
61e2488c 709#define DEFAULT_BUILD_ID_STYLE "md5"
99ad8390 710
f38a2680 711static bool
99ad8390
NC
712gld${EMULATION_NAME}_handle_option (int optc)
713{
d74720d2 714 is_underscoring ();
99ad8390
NC
715 switch (optc)
716 {
717 default:
f38a2680 718 return false;
99ad8390
NC
719
720 case OPTION_BASE_FILE:
721 link_info.base_file = fopen (optarg, FOPEN_WB);
722 if (link_info.base_file == NULL)
e922bcab 723 einfo (_("%F%P: cannot open base file %s\n"), optarg);
99ad8390
NC
724 break;
725
726 /* PE options. */
727 case OPTION_HEAP:
728 set_pep_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__");
729 break;
730 case OPTION_STACK:
731 set_pep_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__");
732 break;
733 case OPTION_SUBSYSTEM:
734 set_pep_subsystem ();
735 break;
736 case OPTION_MAJOR_OS_VERSION:
737 set_pep_value ("__major_os_version__");
738 break;
739 case OPTION_MINOR_OS_VERSION:
740 set_pep_value ("__minor_os_version__");
741 break;
742 case OPTION_MAJOR_SUBSYSTEM_VERSION:
743 set_pep_value ("__major_subsystem_version__");
744 break;
745 case OPTION_MINOR_SUBSYSTEM_VERSION:
746 set_pep_value ("__minor_subsystem_version__");
747 break;
748 case OPTION_MAJOR_IMAGE_VERSION:
749 set_pep_value ("__major_image_version__");
750 break;
751 case OPTION_MINOR_IMAGE_VERSION:
752 set_pep_value ("__minor_image_version__");
753 break;
754 case OPTION_FILE_ALIGNMENT:
755 set_pep_value ("__file_alignment__");
756 break;
757 case OPTION_SECTION_ALIGNMENT:
758 set_pep_value ("__section_alignment__");
759 break;
760 case OPTION_DLL:
761 set_pep_name ("__dll__", 1);
762 break;
763 case OPTION_IMAGE_BASE:
764 set_pep_value ("__image_base__");
765 break;
766 case OPTION_SUPPORT_OLD_CODE:
767 support_old_code = 1;
768 break;
ce11ba6c 769 case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
f38a2680 770 pep_use_nul_prefixed_import_tables = true;
ce11ba6c 771 break;
522f09cd
KT
772 case OPTION_NO_LEADING_UNDERSCORE:
773 pep_leading_underscore = 0;
774 break;
775 case OPTION_LEADING_UNDERSCORE:
776 pep_leading_underscore = 1;
777 break;
0cb112f7 778 case OPTION_INSERT_TIMESTAMP:
f38a2680 779 insert_timestamp = true;
0cb112f7 780 break;
eeb14e5a 781 case OPTION_NO_INSERT_TIMESTAMP:
f38a2680 782 insert_timestamp = false;
eeb14e5a 783 break;
99ad8390
NC
784#ifdef DLL_SUPPORT
785 case OPTION_OUT_DEF:
786 pep_out_def_filename = xstrdup (optarg);
787 break;
788 case OPTION_EXPORT_ALL:
789 pep_dll_export_everything = 1;
790 break;
791 case OPTION_EXCLUDE_SYMBOLS:
e1c37eb5 792 pep_dll_add_excludes (optarg, EXCLUDESYMS);
99ad8390 793 break;
2927aaca
NC
794 case OPTION_EXCLUDE_ALL_SYMBOLS:
795 pep_dll_exclude_all_symbols = 1;
796 break;
99ad8390 797 case OPTION_EXCLUDE_LIBS:
e1c37eb5
DK
798 pep_dll_add_excludes (optarg, EXCLUDELIBS);
799 break;
800 case OPTION_EXCLUDE_MODULES_FOR_IMPLIB:
801 pep_dll_add_excludes (optarg, EXCLUDEFORIMPLIB);
99ad8390
NC
802 break;
803 case OPTION_KILL_ATS:
804 pep_dll_kill_ats = 1;
805 break;
806 case OPTION_STDCALL_ALIASES:
807 pep_dll_stdcall_aliases = 1;
808 break;
809 case OPTION_ENABLE_STDCALL_FIXUP:
810 pep_enable_stdcall_fixup = 1;
811 break;
812 case OPTION_DISABLE_STDCALL_FIXUP:
813 pep_enable_stdcall_fixup = 0;
814 break;
99ad8390
NC
815 case OPTION_WARN_DUPLICATE_EXPORTS:
816 pep_dll_warn_dup_exports = 1;
817 break;
818 case OPTION_IMP_COMPAT:
819 pep_dll_compat_implib = 1;
820 break;
821 case OPTION_ENABLE_AUTO_IMAGE_BASE:
822 pep_enable_auto_image_base = 1;
823 break;
824 case OPTION_DISABLE_AUTO_IMAGE_BASE:
825 pep_enable_auto_image_base = 0;
826 break;
827 case OPTION_DLL_SEARCH_PREFIX:
828 pep_dll_search_prefix = xstrdup (optarg);
829 break;
830 case OPTION_NO_DEFAULT_EXCLUDES:
831 pep_dll_do_default_excludes = 0;
832 break;
833 case OPTION_DLL_ENABLE_AUTO_IMPORT:
834 link_info.pei386_auto_import = 1;
835 break;
836 case OPTION_DLL_DISABLE_AUTO_IMPORT:
837 link_info.pei386_auto_import = 0;
838 break;
839 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC:
6cb442d3 840 link_info.pei386_runtime_pseudo_reloc = 2;
99ad8390
NC
841 break;
842 case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC:
843 link_info.pei386_runtime_pseudo_reloc = 0;
844 break;
6cb442d3
KT
845 case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2:
846 link_info.pei386_runtime_pseudo_reloc = 2;
847 break;
99ad8390
NC
848 case OPTION_ENABLE_EXTRA_PE_DEBUG:
849 pep_dll_extra_pe_debug = 1;
850 break;
851#endif
88183869
DK
852 case OPTION_ENABLE_LONG_SECTION_NAMES:
853 pep_use_coff_long_section_names = 1;
854 break;
855 case OPTION_DISABLE_LONG_SECTION_NAMES:
856 pep_use_coff_long_section_names = 0;
857 break;
2f563b51 858 /* Get DLLCharacteristics bits */
2d5c3743
NC
859 case OPTION_HIGH_ENTROPY_VA:
860 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA;
dc9bd8c9 861 /* fall through */
2f563b51
DK
862 case OPTION_DYNAMIC_BASE:
863 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
dc9bd8c9
HD
864 /* fall through */
865 case OPTION_ENABLE_RELOC_SECTION:
866 pep_dll_enable_reloc_section = 1;
2f563b51 867 break;
514b4e19
JD
868 case OPTION_DISABLE_RELOC_SECTION:
869 pep_dll_enable_reloc_section = 0;
870 /* fall through */
871 case OPTION_DISABLE_DYNAMIC_BASE:
872 pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
873 /* fall through */
874 case OPTION_DISABLE_HIGH_ENTROPY_VA:
875 pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA;
876 break;
2f563b51
DK
877 case OPTION_FORCE_INTEGRITY:
878 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
879 break;
514b4e19
JD
880 case OPTION_DISABLE_FORCE_INTEGRITY:
881 pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
882 break;
2f563b51
DK
883 case OPTION_NX_COMPAT:
884 pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
885 break;
514b4e19
JD
886 case OPTION_DISABLE_NX_COMPAT:
887 pe_dll_characteristics &= ~ IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
888 break;
2f563b51
DK
889 case OPTION_NO_ISOLATION:
890 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
891 break;
514b4e19
JD
892 case OPTION_DISABLE_NO_ISOLATION:
893 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
894 break;
2f563b51
DK
895 case OPTION_NO_SEH:
896 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_SEH;
897 break;
514b4e19
JD
898 case OPTION_DISABLE_NO_SEH:
899 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_SEH;
900 break;
2f563b51
DK
901 case OPTION_NO_BIND:
902 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_BIND;
903 break;
514b4e19
JD
904 case OPTION_DISABLE_NO_BIND:
905 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_NO_BIND;
906 break;
2f563b51
DK
907 case OPTION_WDM_DRIVER:
908 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
909 break;
514b4e19
JD
910 case OPTION_DISABLE_WDM_DRIVER:
911 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_WDM_DRIVER;
912 break;
2f563b51
DK
913 case OPTION_TERMINAL_SERVER_AWARE:
914 pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
915 break;
514b4e19
JD
916 case OPTION_DISABLE_TERMINAL_SERVER_AWARE:
917 pe_dll_characteristics &= ~ IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
918 break;
61e2488c 919 case OPTION_BUILD_ID:
5e2ab612
AM
920 free ((char *) emit_build_id);
921 emit_build_id = NULL;
61e2488c
JT
922 if (optarg == NULL)
923 optarg = DEFAULT_BUILD_ID_STYLE;
924 if (strcmp (optarg, "none"))
925 emit_build_id = xstrdup (optarg);
926 break;
99ad8390 927 }
2f563b51
DK
928
929 /* Set DLLCharacteristics bits */
930 set_pep_name ("__dll_characteristics__", pe_dll_characteristics);
e4492aa0 931
f38a2680 932 return true;
99ad8390
NC
933}
934\f
935
936#ifdef DLL_SUPPORT
937static unsigned long
938strhash (const char *str)
939{
940 const unsigned char *s;
941 unsigned long hash;
942 unsigned int c;
943 unsigned int len;
944
945 hash = 0;
946 len = 0;
947 s = (const unsigned char *) str;
948 while ((c = *s++) != '\0')
949 {
950 hash += c + (c << 17);
951 hash ^= hash >> 2;
952 ++len;
953 }
954 hash += len + (len << 17);
955 hash ^= hash >> 2;
956
957 return hash;
958}
959
960/* Use the output file to create a image base for relocatable DLLs. */
961
0ead4f8d 962static bfd_vma
99ad8390
NC
963compute_dll_image_base (const char *ofile)
964{
0ead4f8d 965 bfd_vma hash = (bfd_vma) strhash (ofile);
a07ee50e 966 return NT_DLL_AUTO_IMAGE_BASE + ((hash << 16) & NT_DLL_AUTO_IMAGE_MASK);
99ad8390
NC
967}
968#endif
969
970/* Assign values to the special symbols before the linker script is
971 read. */
972
973static void
a9261391 974gld${EMULATION_NAME}_set_symbols (void)
99ad8390
NC
975{
976 /* Run through and invent symbols for all the
977 names and insert the defaults. */
d74720d2 978 int j;
b804e4af 979
d74720d2 980 is_underscoring ();
99ad8390
NC
981
982 if (!init[IMAGEBASEOFF].inited)
983 {
0e1862bb 984 if (bfd_link_relocatable (&link_info))
99ad8390 985 init[IMAGEBASEOFF].value = 0;
0e1862bb 986 else if (init[DLLOFF].value || bfd_link_dll (&link_info))
d127ecce 987 {
99ad8390 988#ifdef DLL_SUPPORT
d127ecce
AM
989 init[IMAGEBASEOFF].value = (pep_enable_auto_image_base
990 ? compute_dll_image_base (output_filename)
991 : NT_DLL_IMAGE_BASE);
99ad8390 992#else
d127ecce 993 init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
99ad8390 994#endif
d127ecce 995 }
99ad8390
NC
996 else
997 init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
d127ecce 998 init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value;
99ad8390
NC
999 }
1000
1001 /* Don't do any symbol assignments if this is a relocatable link. */
0e1862bb 1002 if (bfd_link_relocatable (&link_info))
99ad8390
NC
1003 return;
1004
1005 /* Glue the assignments into the abs section. */
bde18da4 1006 push_stat_ptr (&abs_output_section->children);
99ad8390
NC
1007
1008 for (j = 0; init[j].ptr; j++)
1009 {
0ead4f8d 1010 bfd_vma val = init[j].value;
99ad8390 1011 lang_assignment_statement_type *rv;
522f09cd 1012
2e57b2af 1013 rv = lang_add_assignment (exp_assign (GET_INIT_SYMBOL_NAME (j),
f38a2680 1014 exp_intop (val), false));
99ad8390 1015 if (init[j].size == sizeof (short))
0ead4f8d 1016 *(short *) init[j].ptr = (short) val;
99ad8390 1017 else if (init[j].size == sizeof (int))
0ead4f8d 1018 *(int *) init[j].ptr = (int) val;
99ad8390 1019 else if (init[j].size == sizeof (long))
0ead4f8d 1020 *(long *) init[j].ptr = (long) val;
99ad8390
NC
1021 /* This might be a long long or other special type. */
1022 else if (init[j].size == sizeof (bfd_vma))
1023 *(bfd_vma *) init[j].ptr = val;
1024 else abort ();
1025 if (j == IMAGEBASEOFF)
1026 image_base_statement = rv;
1027 }
1028 /* Restore the pointer. */
bde18da4 1029 pop_stat_ptr ();
99ad8390
NC
1030
1031 if (pep.FileAlignment > pep.SectionAlignment)
1032 {
df5f2391 1033 einfo (_("%P: warning, file alignment > section alignment\n"));
99ad8390
NC
1034 }
1035}
1036
1037/* This is called after the linker script and the command line options
1038 have been read. */
1039
1040static void
a9261391 1041gld${EMULATION_NAME}_after_parse (void)
99ad8390 1042{
8b747e1a
DK
1043 /* PR ld/6744: Warn the user if they have used an ELF-only
1044 option hoping it will work on PE+. */
1045 if (link_info.export_dynamic)
1046 einfo (_("%P: warning: --export-dynamic is not supported for PE+ "
1047 "targets, did you mean --export-all-symbols?\n"));
97b11f40 1048
decfa41e 1049 set_entry_point ();
e4492aa0 1050
97b11f40 1051 after_parse_default ();
99ad8390
NC
1052}
1053
99ad8390
NC
1054#ifdef DLL_SUPPORT
1055static struct bfd_link_hash_entry *pep_undef_found_sym;
1056
f38a2680 1057static bool
99ad8390
NC
1058pep_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf)
1059{
1060 int sl;
1061 char *string = inf;
8a9eab9b 1062 const char *hs = h->root.string;
99ad8390
NC
1063
1064 sl = strlen (string);
1065 if (h->type == bfd_link_hash_defined
8a9eab9b
KT
1066 && ((*hs == '@' && *string == '_'
1067 && strncmp (hs + 1, string + 1, sl - 1) == 0)
1068 || strncmp (hs, string, sl) == 0)
99ad8390
NC
1069 && h->root.string[sl] == '@')
1070 {
1071 pep_undef_found_sym = h;
f38a2680 1072 return false;
99ad8390 1073 }
f38a2680 1074 return true;
99ad8390
NC
1075}
1076
1077static void
1078pep_fixup_stdcalls (void)
1079{
1080 static int gave_warning_message = 0;
1081 struct bfd_link_hash_entry *undef, *sym;
1082
1083 if (pep_dll_extra_pe_debug)
1084 printf ("%s\n", __FUNCTION__);
1085
1086 for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next)
1087 if (undef->type == bfd_link_hash_undefined)
1088 {
1089 char* at = strchr (undef->root.string, '@');
1090 int lead_at = (*undef->root.string == '@');
8a9eab9b
KT
1091 if (lead_at)
1092 at = strchr (undef->root.string + 1, '@');
1093 if (at || lead_at)
99ad8390
NC
1094 {
1095 /* The symbol is a stdcall symbol, so let's look for a
1096 cdecl symbol with the same name and resolve to that. */
8a9eab9b
KT
1097 char *cname = xstrdup (undef->root.string);
1098
1099 if (lead_at)
1100 *cname = '_';
99ad8390 1101 at = strchr (cname, '@');
8a9eab9b
KT
1102 if (at)
1103 *at = 0;
99ad8390
NC
1104 sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1);
1105
1106 if (sym && sym->type == bfd_link_hash_defined)
1107 {
1108 undef->type = bfd_link_hash_defined;
1109 undef->u.def.value = sym->u.def.value;
1110 undef->u.def.section = sym->u.def.section;
1111
1112 if (pep_enable_stdcall_fixup == -1)
1113 {
df5f2391 1114 einfo (_("warning: resolving %s by linking to %s\n"),
99ad8390
NC
1115 undef->root.string, cname);
1116 if (! gave_warning_message)
1117 {
1118 gave_warning_message = 1;
1119 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
1120 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
1121 }
1122 }
1123 }
1124 }
1125 else
1126 {
1127 /* The symbol is a cdecl symbol, so we look for stdcall
1128 symbols - which means scanning the whole symbol table. */
1129 pep_undef_found_sym = 0;
1130 bfd_link_hash_traverse (link_info.hash, pep_undef_cdecl_match,
1131 (char *) undef->root.string);
1132 sym = pep_undef_found_sym;
1133 if (sym)
1134 {
1135 undef->type = bfd_link_hash_defined;
1136 undef->u.def.value = sym->u.def.value;
1137 undef->u.def.section = sym->u.def.section;
1138
1139 if (pep_enable_stdcall_fixup == -1)
1140 {
df5f2391 1141 einfo (_("warning: resolving %s by linking to %s\n"),
99ad8390
NC
1142 undef->root.string, sym->root.string);
1143 if (! gave_warning_message)
1144 {
1145 gave_warning_message = 1;
1146 einfo (_("Use --enable-stdcall-fixup to disable these warnings\n"));
1147 einfo (_("Use --disable-stdcall-fixup to disable these fixups\n"));
1148 }
1149 }
1150 }
1151 }
1152 }
1153}
1154
317ff008
EB
1155static void
1156make_import_fixup (arelent *rel, asection *s, char *name, const char *symname)
99ad8390
NC
1157{
1158 struct bfd_symbol *sym = *rel->sym_ptr_ptr;
6cb442d3
KT
1159 char addend[8];
1160 bfd_vma _addend = 0;
1161 int suc = 0;
99ad8390
NC
1162
1163 if (pep_dll_extra_pe_debug)
1164 printf ("arelent: %s@%#lx: add=%li\n", sym->name,
0af1713e 1165 (unsigned long) rel->address, (long) rel->addend);
99ad8390 1166
6cb442d3
KT
1167 memset (addend, 0, sizeof (addend));
1168 switch ((rel->howto->bitsize))
1169 {
fbea1508
AM
1170 case 8:
1171 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 1);
1172 if (suc && rel->howto->pc_relative)
1173 _addend = bfd_get_signed_8 (s->owner, addend);
1174 else if (suc)
1175 _addend = bfd_get_8 (s->owner, addend);
1176 break;
1177 case 16:
1178 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 2);
1179 if (suc && rel->howto->pc_relative)
1180 _addend = bfd_get_signed_16 (s->owner, addend);
1181 else if (suc)
1182 _addend = bfd_get_16 (s->owner, addend);
1183 break;
1184 case 32:
1185 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 4);
1186 if (suc && rel->howto->pc_relative)
1187 _addend = bfd_get_signed_32 (s->owner, addend);
1188 else if (suc)
1189 _addend = bfd_get_32 (s->owner, addend);
1190 break;
1191 case 64:
1192 suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 8);
1193 if (suc)
1194 _addend = bfd_get_64 (s->owner, addend);
1195 break;
6cb442d3
KT
1196 }
1197 if (! suc)
df5f2391 1198 einfo (_("%P: %C: cannot get section contents - auto-import exception\n"),
99ad8390
NC
1199 s->owner, s, rel->address);
1200
6cb442d3
KT
1201 if (pep_dll_extra_pe_debug)
1202 {
fbea1508
AM
1203 printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx",
1204 (long) _addend, (long) rel->addend, (long) rel->address);
1205 if (rel->howto->pc_relative)
1206 printf (" pcrel");
1207 printf (" %d bit rel.\n", (int) rel->howto->bitsize);
1208 }
99ad8390 1209
317ff008 1210 pep_create_import_fixup (rel, s, _addend, name, symname);
99ad8390
NC
1211}
1212
f38a2680 1213static bool
99ad8390
NC
1214pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
1215{
80cc85a9 1216 printf ("+%s\n", h->string);
99ad8390 1217
f38a2680 1218 return true;
99ad8390
NC
1219}
1220#endif /* DLL_SUPPORT */
1221
e4492aa0 1222static void
3efd345c
DK
1223debug_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
1224{
1225 int *found = (int *) obj;
61e2488c 1226
3efd345c
DK
1227 if (strncmp (".debug_", sect->name, sizeof (".debug_") - 1) == 0)
1228 *found = 1;
1229}
99ad8390 1230
f38a2680 1231static bool
61e2488c
JT
1232pecoff_checksum_contents (bfd *abfd,
1233 void (*process) (const void *, size_t, void *),
1234 void *arg)
1235{
1236 file_ptr filepos = (file_ptr) 0;
1237
1238 while (1)
1239 {
1240 unsigned char b;
1241 int status;
1242
1243 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
1244 return 0;
1245
1246 status = bfd_bread (&b, (bfd_size_type) 1, abfd);
1247 if (status < 1)
6c19b93b
AM
1248 {
1249 break;
1250 }
61e2488c
JT
1251
1252 (*process) (&b, 1, arg);
1253 filepos += 1;
1254 }
1255
f38a2680 1256 return true;
61e2488c
JT
1257}
1258
f38a2680 1259static bool
61e2488c
JT
1260write_build_id (bfd *abfd)
1261{
1262 struct pe_tdata *t = pe_data (abfd);
1263 asection *asec;
1264 struct bfd_link_order *link_order = NULL;
1265 unsigned char *contents;
1266 bfd_size_type size;
1267 bfd_size_type build_id_size;
1268 unsigned char *build_id;
1269
74fffc39 1270 /* Find the section the .buildid output section has been merged info. */
61e2488c
JT
1271 for (asec = abfd->sections; asec != NULL; asec = asec->next)
1272 {
1273 struct bfd_link_order *l = NULL;
1274 for (l = asec->map_head.link_order; l != NULL; l = l->next)
6c19b93b
AM
1275 {
1276 if (l->type == bfd_indirect_link_order)
1277 {
1278 if (l->u.indirect.section == t->build_id.sec)
1279 {
1280 link_order = l;
1281 break;
1282 }
1283 }
1284 }
61e2488c
JT
1285
1286 if (link_order)
6c19b93b 1287 break;
61e2488c
JT
1288 }
1289
1290 if (!link_order)
1291 {
74fffc39 1292 einfo (_("%P: warning: .buildid section discarded,"
df5f2391 1293 " --build-id ignored\n"));
f38a2680 1294 return true;
61e2488c
JT
1295 }
1296
1297 if (t->build_id.sec->contents == NULL)
1298 t->build_id.sec->contents = (unsigned char *) xmalloc (t->build_id.sec->size);
1299 contents = t->build_id.sec->contents;
1300 size = t->build_id.sec->size;
1301
1302 build_id_size = compute_build_id_size (t->build_id.style);
1303 build_id = xmalloc (build_id_size);
1304 generate_build_id (abfd, t->build_id.style, pecoff_checksum_contents, build_id, build_id_size);
1305
1306 bfd_vma ib = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase;
1307
1308 /* Construct a debug directory entry which points to an immediately following CodeView record. */
1309 struct internal_IMAGE_DEBUG_DIRECTORY idd;
1310 idd.Characteristics = 0;
1311 idd.TimeDateStamp = 0;
1312 idd.MajorVersion = 0;
1313 idd.MinorVersion = 0;
1314 idd.Type = PE_IMAGE_DEBUG_TYPE_CODEVIEW;
1315 idd.SizeOfData = sizeof (CV_INFO_PDB70) + 1;
1316 idd.AddressOfRawData = asec->vma - ib + link_order->offset
1317 + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1318 idd.PointerToRawData = asec->filepos + link_order->offset
1319 + sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1320
1321 struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *)contents;
1322 _bfd_XXi_swap_debugdir_out (abfd, &idd, ext);
1323
1324 /* Write the debug directory enttry */
1325 if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0)
1326 return 0;
1327
b6518b38 1328 if (bfd_bwrite (contents, size, abfd) != size)
61e2488c
JT
1329 return 0;
1330
1331 /* Construct the CodeView record. */
1332 CODEVIEW_INFO cvinfo;
1333 cvinfo.CVSignature = CVINFO_PDB70_CVSIGNATURE;
1334 cvinfo.Age = 1;
1335
1336 /* Zero pad or truncate the generated build_id to fit in the CodeView record. */
1337 memset (&(cvinfo.Signature), 0, CV_INFO_SIGNATURE_LENGTH);
1338 memcpy (&(cvinfo.Signature), build_id, (build_id_size > CV_INFO_SIGNATURE_LENGTH)
1339 ? CV_INFO_SIGNATURE_LENGTH : build_id_size);
1340
1341 free (build_id);
1342
1343 /* Write the codeview record. */
1344 if (_bfd_XXi_write_codeview_record (abfd, idd.PointerToRawData, &cvinfo) == 0)
1345 return 0;
1346
1347 /* Record the location of the debug directory in the data directory. */
1348 pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress
1349 = asec->vma - ib + link_order->offset;
1350 pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
1351 = sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
1352
f38a2680 1353 return true;
61e2488c
JT
1354}
1355
74fffc39 1356/* Make .buildid section, and set up coff_tdata->build_id. */
f38a2680 1357static bool
61e2488c
JT
1358setup_build_id (bfd *ibfd)
1359{
1360 asection *s;
1361 flagword flags;
1362
1363 if (!validate_build_id_style (emit_build_id))
1364 {
df5f2391 1365 einfo (_("%P: warning: unrecognized --build-id style ignored\n"));
f38a2680 1366 return false;
61e2488c
JT
1367 }
1368
1369 flags = (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY
1370 | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA);
74fffc39 1371 s = bfd_make_section_anyway_with_flags (ibfd, ".buildid", flags);
61e2488c
JT
1372 if (s != NULL)
1373 {
1374 struct pe_tdata *t = pe_data (link_info.output_bfd);
1375 t->build_id.after_write_object_contents = &write_build_id;
1376 t->build_id.style = emit_build_id;
1377 t->build_id.sec = s;
1378
1379 /* Section is a fixed size:
1380 One IMAGE_DEBUG_DIRECTORY entry, of type IMAGE_DEBUG_TYPE_CODEVIEW,
1381 pointing at a CV_INFO_PDB70 record containing the build-id, with a
1382 null byte for PdbFileName. */
1383 s->size = sizeof (struct external_IMAGE_DEBUG_DIRECTORY)
1384 + sizeof (CV_INFO_PDB70) + 1;
1385
f38a2680 1386 return true;
61e2488c
JT
1387 }
1388
df5f2391
AM
1389 einfo (_("%P: warning: cannot create .buildid section,"
1390 " --build-id ignored\n"));
f38a2680 1391 return false;
61e2488c
JT
1392}
1393
99ad8390 1394static void
a9261391 1395gld${EMULATION_NAME}_after_open (void)
99ad8390 1396{
5c3049d2
AM
1397 after_open_default ();
1398
d74720d2 1399 is_underscoring ();
99ad8390
NC
1400#ifdef DLL_SUPPORT
1401 if (pep_dll_extra_pe_debug)
1402 {
1403 bfd *a;
1404 struct bfd_link_hash_entry *sym;
1405
1406 printf ("%s()\n", __FUNCTION__);
1407
1408 for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next)
1409 printf ("-%s\n", sym->root.string);
1410 bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL);
1411
c72f2fb2 1412 for (a = link_info.input_bfds; a; a = a->link.next)
607b4833 1413 printf ("*%s\n", bfd_get_filename (a));
99ad8390
NC
1414 }
1415#endif
1416
61e2488c
JT
1417 if (emit_build_id != NULL)
1418 {
1419 bfd *abfd;
1420
1421 /* Find a COFF input. */
1422 for (abfd = link_info.input_bfds;
c72f2fb2 1423 abfd != (bfd *) NULL; abfd = abfd->link.next)
61e2488c
JT
1424 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
1425 break;
1426
1427 /* If there are no COFF input files do not try to
1428 add a build-id section. */
1429 if (abfd == NULL
1430 || !setup_build_id (abfd))
1431 {
1432 free ((char *) emit_build_id);
1433 emit_build_id = NULL;
1434 }
1435 }
1436
99ad8390
NC
1437 /* Pass the wacky PE command line options into the output bfd.
1438 FIXME: This should be done via a function, rather than by
1439 including an internal BFD header. */
1440
eabf307f
AM
1441 if (bfd_get_flavour (link_info.output_bfd) != bfd_target_coff_flavour
1442 || coff_data (link_info.output_bfd) == NULL
f13a99db 1443 || coff_data (link_info.output_bfd)->pe == 0)
df5f2391 1444 einfo (_("%F%P: cannot perform PE operations on non PE output file '%pB'\n"),
f13a99db 1445 link_info.output_bfd);
99ad8390 1446
f13a99db
AM
1447 pe_data (link_info.output_bfd)->pe_opthdr = pep;
1448 pe_data (link_info.output_bfd)->dll = init[DLLOFF].value;
1449 pe_data (link_info.output_bfd)->real_flags |= real_flags;
00386881
NC
1450 if (insert_timestamp)
1451 pe_data (link_info.output_bfd)->timestamp = -1;
1452 else
1453 pe_data (link_info.output_bfd)->timestamp = 0;
3efd345c
DK
1454
1455 /* At this point we must decide whether to use long section names
1456 in the output or not. If the user hasn't explicitly specified
1457 on the command line, we leave it to the default for the format
1458 (object files yes, image files no), except if there is debug
1459 information present; GDB relies on the long section names to
1460 find it, so enable it in that case. */
1461 if (pep_use_coff_long_section_names < 0 && link_info.strip == strip_none)
1462 {
0e1862bb 1463 if (bfd_link_relocatable (&link_info))
61e2488c
JT
1464 pep_use_coff_long_section_names = 1;
1465 else
3efd345c 1466 {
61e2488c
JT
1467 /* Iterate over all sections of all input BFDs, checking
1468 for any that begin 'debug_' and are long names. */
1469 LANG_FOR_EACH_INPUT_STATEMENT (is)
1470 {
1471 int found_debug = 0;
1472
1473 bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug);
1474 if (found_debug)
1475 {
1476 pep_use_coff_long_section_names = 1;
1477 break;
1478 }
1479 }
3efd345c
DK
1480 }
1481 }
1482
88183869 1483 pep_output_file_set_long_section_names (link_info.output_bfd);
99ad8390
NC
1484
1485#ifdef DLL_SUPPORT
f13a99db 1486 pep_process_import_defs (link_info.output_bfd, &link_info);
99ad8390 1487
317ff008
EB
1488 if (link_info.pei386_auto_import) /* -1=warn or 1=enable */
1489 pep_find_data_imports (U ("_head_"), make_import_fixup);
99ad8390 1490
317ff008
EB
1491 /* The implementation of the feature is rather dumb and would cause the
1492 compilation time to go through the roof if there are many undefined
1493 symbols in the link, so it needs to be run after auto-import. */
1494 if (pep_enable_stdcall_fixup) /* -1=warn or 1=enable */
8a9eab9b
KT
1495 pep_fixup_stdcalls ();
1496
99ad8390 1497#ifndef TARGET_IS_i386pep
0e1862bb 1498 if (bfd_link_pic (&link_info))
99ad8390 1499#else
0e1862bb 1500 if (!bfd_link_relocatable (&link_info))
99ad8390 1501#endif
f13a99db 1502 pep_dll_build_sections (link_info.output_bfd, &link_info);
99ad8390
NC
1503
1504#ifndef TARGET_IS_i386pep
1505 else
f13a99db 1506 pep_exe_build_sections (link_info.output_bfd, &link_info);
99ad8390
NC
1507#endif
1508#endif /* DLL_SUPPORT */
1509
1510 {
1511 /* This next chunk of code tries to detect the case where you have
1512 two import libraries for the same DLL (specifically,
1513 symbolically linking libm.a and libc.a in cygwin to
1514 libcygwin.a). In those cases, it's possible for function
1515 thunks from the second implib to be used but without the
1516 head/tail objects, causing an improper import table. We detect
1517 those cases and rename the "other" import libraries to match
1518 the one the head/tail come from, so that the linker will sort
1519 things nicely and produce a valid import table. */
1520
1521 LANG_FOR_EACH_INPUT_STATEMENT (is)
1522 {
1523 if (is->the_bfd->my_archive)
1524 {
1525 int idata2 = 0, reloc_count=0, is_imp = 0;
1526 asection *sec;
1527
1528 /* See if this is an import library thunk. */
1529 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1530 {
1531 if (strcmp (sec->name, ".idata\$2") == 0)
1532 idata2 = 1;
08dedd66 1533 if (startswith (sec->name, ".idata\$"))
99ad8390
NC
1534 is_imp = 1;
1535 reloc_count += sec->reloc_count;
1536 }
1537
1538 if (is_imp && !idata2 && reloc_count)
1539 {
1540 /* It is, look for the reference to head and see if it's
1541 from our own library. */
1542 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1543 {
1544 int i;
99ad8390
NC
1545 long relsize;
1546 asymbol **symbols;
1547 arelent **relocs;
1548 int nrelocs;
1549
99ad8390
NC
1550 relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec);
1551 if (relsize < 1)
1552 break;
1553
5c1d2f5f 1554 if (!bfd_generic_link_read_symbols (is->the_bfd))
99ad8390 1555 {
df5f2391 1556 einfo (_("%F%P: %pB: could not read symbols: %E\n"),
5c1d2f5f 1557 is->the_bfd);
99ad8390
NC
1558 return;
1559 }
5c1d2f5f 1560 symbols = bfd_get_outsymbols (is->the_bfd);
99ad8390
NC
1561
1562 relocs = xmalloc ((size_t) relsize);
1563 nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec,
1564 relocs, symbols);
1565 if (nrelocs < 0)
1566 {
1567 free (relocs);
d003af55 1568 einfo (_("%X%P: unable to process relocs: %E\n"));
99ad8390
NC
1569 return;
1570 }
1571
1572 for (i = 0; i < nrelocs; i++)
1573 {
1574 struct bfd_symbol *s;
1575 struct bfd_link_hash_entry * blhe;
a6ad7914 1576 bfd *other_bfd;
b16c44de 1577 const char *other_bfd_filename;
99ad8390
NC
1578
1579 s = (relocs[i]->sym_ptr_ptr)[0];
1580
1581 if (s->flags & BSF_LOCAL)
1582 continue;
1583
1584 /* Thunk section with reloc to another bfd. */
1585 blhe = bfd_link_hash_lookup (link_info.hash,
1586 s->name,
f38a2680 1587 false, false, true);
99ad8390
NC
1588
1589 if (blhe == NULL
1590 || blhe->type != bfd_link_hash_defined)
1591 continue;
1592
a6ad7914
AM
1593 other_bfd = blhe->u.def.section->owner;
1594 if (other_bfd->my_archive == is->the_bfd->my_archive)
1595 continue;
1596
99ad8390 1597 other_bfd_filename
a6ad7914
AM
1598 = (other_bfd->my_archive
1599 ? bfd_get_filename (other_bfd->my_archive)
1600 : bfd_get_filename (other_bfd));
99ad8390 1601
42627821
KT
1602 if (filename_cmp (bfd_get_filename
1603 (is->the_bfd->my_archive),
1604 other_bfd_filename) == 0)
99ad8390
NC
1605 continue;
1606
a6ad7914
AM
1607 /* Sort this implib to match the other one. */
1608 lang_input_statement_type *arch_is
1609 = bfd_usrdata (is->the_bfd->my_archive);
1610 arch_is->sort_key = other_bfd_filename;
1611 break;
99ad8390
NC
1612 }
1613
1614 free (relocs);
1615 /* Note - we do not free the symbols,
1616 they are now cached in the BFD. */
1617 }
1618 }
1619 }
1620 }
1621 }
1622
1623 {
99ad8390
NC
1624
1625 /* Careful - this is a shell script. Watch those dollar signs! */
1626 /* Microsoft import libraries have every member named the same,
1627 and not in the right order for us to link them correctly. We
1628 must detect these and rename the members so that they'll link
1629 correctly. There are three types of objects: the head, the
1630 thunks, and the sentinel(s). The head is easy; it's the one
1631 with idata2. We assume that the sentinels won't have relocs,
1632 and the thunks will. It's easier than checking the symbol
1633 table for external references. */
1634 LANG_FOR_EACH_INPUT_STATEMENT (is)
1635 {
1636 if (is->the_bfd->my_archive)
1637 {
1638 char *pnt;
99ad8390 1639
46bbc1c0
MS
1640 /* Microsoft import libraries may contain archive members for
1641 one or more DLLs, together with static object files.
1642 Inspect all members that are named *.dll - check whether
1643 they contain .idata sections. Do the renaming of all
1644 archive members that seem to be Microsoft style import
1645 objects. */
607b4833 1646 pnt = strrchr (bfd_get_filename (is->the_bfd), '.');
99ad8390 1647
46bbc1c0 1648 if (pnt != NULL && (fileext_cmp (pnt + 1, "dll") == 0))
99ad8390 1649 {
46bbc1c0 1650 int idata2 = 0, reloc_count = 0, idata = 0;
99ad8390
NC
1651 asection *sec;
1652 char *new_name, seq;
1653
1654 for (sec = is->the_bfd->sections; sec; sec = sec->next)
1655 {
1656 if (strcmp (sec->name, ".idata\$2") == 0)
1657 idata2 = 1;
46bbc1c0
MS
1658 if (strncmp (sec->name, ".idata\$", 6) == 0)
1659 idata = 1;
99ad8390
NC
1660 reloc_count += sec->reloc_count;
1661 }
1662
46bbc1c0
MS
1663 /* An archive member named .dll, but not having any .idata
1664 sections - apparently not a Microsoft import object
1665 after all: Skip renaming it. */
1666 if (!idata)
1667 continue;
1668
99ad8390
NC
1669 if (idata2) /* .idata2 is the TOC */
1670 seq = 'a';
1671 else if (reloc_count > 0) /* thunks */
1672 seq = 'b';
1673 else /* sentinel */
1674 seq = 'c';
1675
7b958a48
AM
1676 new_name
1677 = xmalloc (strlen (bfd_get_filename (is->the_bfd)) + 3);
1678 sprintf (new_name, "%s.%c",
1679 bfd_get_filename (is->the_bfd), seq);
a6ad7914 1680 is->sort_key = new_name;
99ad8390
NC
1681 }
1682 }
1683 }
1684 }
1685}
1686\f
1687static void
a9261391 1688gld${EMULATION_NAME}_before_allocation (void)
99ad8390 1689{
d74720d2 1690 is_underscoring ();
99ad8390
NC
1691 before_allocation_default ();
1692}
1693\f
1694#ifdef DLL_SUPPORT
1695/* This is called when an input file isn't recognized as a BFD. We
1696 check here for .DEF files and pull them in automatically. */
1697
1698static int
1699saw_option (char *option)
1700{
1701 int i;
1702
d74720d2
KT
1703 is_underscoring ();
1704
99ad8390 1705 for (i = 0; init[i].ptr; i++)
522f09cd 1706 if (strcmp (GET_INIT_SYMBOL_NAME (i), option) == 0)
99ad8390
NC
1707 return init[i].inited;
1708 return 0;
1709}
1710#endif /* DLL_SUPPORT */
1711
f38a2680 1712static bool
a9261391 1713gld${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
99ad8390
NC
1714{
1715#ifdef DLL_SUPPORT
b53b12c8 1716 const char *ext = strrchr (entry->filename, '.');
99ad8390 1717
b53b12c8 1718 if (ext != NULL && fileext_cmp (ext + 1, "def") == 0)
99ad8390
NC
1719 {
1720 pep_def_file = def_file_parse (entry->filename, pep_def_file);
1721
1722 if (pep_def_file)
1723 {
1724 int i, buflen=0, len;
1725 char *buf;
1726
1727 for (i = 0; i < pep_def_file->num_exports; i++)
1728 {
1729 len = strlen (pep_def_file->exports[i].internal_name);
1730 if (buflen < len + 2)
1731 buflen = len + 2;
1732 }
1733
1734 buf = xmalloc (buflen);
1735
1736 for (i = 0; i < pep_def_file->num_exports; i++)
1737 {
1738 struct bfd_link_hash_entry *h;
1739
522f09cd
KT
1740 sprintf (buf, "%s%s", U (""),
1741 pep_def_file->exports[i].internal_name);
99ad8390 1742
f38a2680 1743 h = bfd_link_hash_lookup (link_info.hash, buf, true, true, true);
99ad8390 1744 if (h == (struct bfd_link_hash_entry *) NULL)
df5f2391 1745 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
99ad8390
NC
1746 if (h->type == bfd_link_hash_new)
1747 {
1748 h->type = bfd_link_hash_undefined;
1749 h->u.undef.abfd = NULL;
1750 bfd_link_add_undef (link_info.hash, h);
1751 }
1752 }
1753 free (buf);
1754
1755 /* def_file_print (stdout, pep_def_file); */
1756 if (pep_def_file->is_dll == 1)
64d94ba0 1757 link_info.type = type_dll;
99ad8390
NC
1758
1759 if (pep_def_file->base_address != (bfd_vma)(-1))
1760 {
f13a99db
AM
1761 pep.ImageBase
1762 = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase
1763 = init[IMAGEBASEOFF].value
1764 = pep_def_file->base_address;
99ad8390
NC
1765 init[IMAGEBASEOFF].inited = 1;
1766 if (image_base_statement)
eb8476a6
MR
1767 image_base_statement->exp
1768 = exp_assign ("__image_base__", exp_intop (pep.ImageBase),
f38a2680 1769 false);
99ad8390
NC
1770 }
1771
1772 if (pep_def_file->stack_reserve != -1
1773 && ! saw_option ("__size_of_stack_reserve__"))
1774 {
1775 pep.SizeOfStackReserve = pep_def_file->stack_reserve;
1776 if (pep_def_file->stack_commit != -1)
1777 pep.SizeOfStackCommit = pep_def_file->stack_commit;
1778 }
1779 if (pep_def_file->heap_reserve != -1
1780 && ! saw_option ("__size_of_heap_reserve__"))
1781 {
1782 pep.SizeOfHeapReserve = pep_def_file->heap_reserve;
1783 if (pep_def_file->heap_commit != -1)
1784 pep.SizeOfHeapCommit = pep_def_file->heap_commit;
1785 }
f38a2680 1786 return true;
99ad8390
NC
1787 }
1788 }
1789#endif
f38a2680 1790 return false;
99ad8390
NC
1791}
1792
f38a2680 1793static bool
a9261391 1794gld${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED)
99ad8390 1795{
d74720d2 1796 is_underscoring ();
99ad8390
NC
1797#ifdef DLL_SUPPORT
1798#ifdef TARGET_IS_i386pep
1799 pep_dll_id_target ("pei-x86-64");
1800#endif
880383ca
NC
1801 if (pep_bfd_is_dll (entry->the_bfd))
1802 return pep_implied_import_dll (entry->filename);
99ad8390 1803#endif
f38a2680 1804 return false;
99ad8390
NC
1805}
1806
1807static void
a9261391 1808gld${EMULATION_NAME}_finish (void)
99ad8390 1809{
d74720d2 1810 is_underscoring ();
99ad8390
NC
1811 finish_default ();
1812
1813#ifdef DLL_SUPPORT
0e1862bb 1814 if (bfd_link_pic (&link_info)
dc9bd8c9 1815 || pep_dll_enable_reloc_section
0e1862bb
L
1816 || (!bfd_link_relocatable (&link_info)
1817 && pep_def_file->num_exports != 0))
99ad8390 1818 {
f13a99db 1819 pep_dll_fill_sections (link_info.output_bfd, &link_info);
9cdc5bac 1820 if (command_line.out_implib_filename
51dee44b
JD
1821 && (pep_def_file->num_exports != 0
1822 || bfd_link_pic (&link_info)))
76359541
TP
1823 pep_dll_generate_implib (pep_def_file,
1824 command_line.out_implib_filename, &link_info);
99ad8390
NC
1825 }
1826
1827 if (pep_out_def_filename)
1828 pep_dll_generate_def_file (pep_out_def_filename);
1829#endif /* DLL_SUPPORT */
1830
1831 /* I don't know where .idata gets set as code, but it shouldn't be. */
1832 {
f13a99db 1833 asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata");
99ad8390
NC
1834
1835 if (asec)
1836 {
1837 asec->flags &= ~SEC_CODE;
1838 asec->flags |= SEC_DATA;
1839 }
1840 }
1841}
1842
1843\f
1844/* Place an orphan section.
1845
1846 We use this to put sections in a reasonable place in the file, and
1847 to ensure that they are aligned as required.
1848
d34c1c51 1849 We handle grouped sections here as well. A section named .foo\$nn
99ad8390
NC
1850 goes into the output section .foo. All grouped sections are sorted
1851 by name.
1852
1853 Grouped sections for the default sections are handled by the
1854 default linker script using wildcards, and are sorted by
1855 sort_sections. */
1856
c2edb4b8 1857static lang_output_section_statement_type *
a9261391 1858gld${EMULATION_NAME}_place_orphan (asection *s,
8a99a385
AM
1859 const char *secname,
1860 int constraint)
99ad8390 1861{
bcacc0f5 1862 const char *orig_secname = secname;
99ad8390
NC
1863 char *dollar = NULL;
1864 lang_output_section_statement_type *os;
1865 lang_statement_list_type add_child;
b5f14a6d 1866 lang_output_section_statement_type *match_by_name = NULL;
011aa75f 1867 lang_statement_union_type **pl;
99ad8390 1868
99ad8390 1869 /* Look through the script to see where to place this section. */
0e1862bb 1870 if (!bfd_link_relocatable (&link_info)
d34c1c51 1871 && (dollar = strchr (secname, '\$')) != NULL)
99ad8390 1872 {
bcacc0f5 1873 size_t len = dollar - secname;
99ad8390 1874 char *newname = xmalloc (len + 1);
bcacc0f5 1875 memcpy (newname, secname, len);
99ad8390
NC
1876 newname[len] = '\0';
1877 secname = newname;
1878 }
1879
99ad8390
NC
1880 lang_list_init (&add_child);
1881
d127ecce
AM
1882 os = NULL;
1883 if (constraint == 0)
1884 for (os = lang_output_section_find (secname);
1885 os != NULL;
1886 os = next_matching_output_section_statement (os, 0))
1887 {
1888 /* If we don't match an existing output section, tell
1889 lang_insert_orphan to create a new output section. */
1890 constraint = SPECIAL;
1891
1892 if (os->bfd_section != NULL
1893 && (os->bfd_section->flags == 0
1894 || ((s->flags ^ os->bfd_section->flags)
1895 & (SEC_LOAD | SEC_ALLOC)) == 0))
1896 {
1897 /* We already have an output section statement with this
1898 name, and its bfd section has compatible flags.
1899 If the section already exists but does not have any flags set,
1900 then it has been created by the linker, probably as a result of
1901 a --section-start command line switch. */
b209b5a6 1902 lang_add_section (&add_child, s, NULL, NULL, os);
d127ecce
AM
1903 break;
1904 }
b5f14a6d
DD
1905
1906 /* Save unused output sections in case we can match them
1907 against orphans later. */
1908 if (os->bfd_section == NULL)
1909 match_by_name = os;
d127ecce
AM
1910 }
1911
b5f14a6d
DD
1912 /* If we didn't match an active output section, see if we matched an
1913 unused one and use that. */
1914 if (os == NULL && match_by_name)
1915 {
b209b5a6 1916 lang_add_section (&match_by_name->children, s, NULL, NULL, match_by_name);
b5f14a6d
DD
1917 return match_by_name;
1918 }
1919
d127ecce 1920 if (os == NULL)
99ad8390
NC
1921 {
1922 static struct orphan_save hold[] =
1923 {
1924 { ".text",
1925 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1926 0, 0, 0, 0 },
d4874973
KT
1927 { ".idata",
1928 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1929 0, 0, 0, 0 },
99ad8390
NC
1930 { ".rdata",
1931 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1932 0, 0, 0, 0 },
1933 { ".data",
1934 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1935 0, 0, 0, 0 },
1936 { ".bss",
1937 SEC_ALLOC,
1938 0, 0, 0, 0 }
1939 };
1940 enum orphan_save_index
1941 {
1942 orphan_text = 0,
d4874973 1943 orphan_idata,
99ad8390
NC
1944 orphan_rodata,
1945 orphan_data,
1946 orphan_bss
1947 };
1948 static int orphan_init_done = 0;
1949 struct orphan_save *place;
1950 lang_output_section_statement_type *after;
1951 etree_type *address;
93638471
AM
1952 flagword flags;
1953 asection *nexts;
99ad8390
NC
1954
1955 if (!orphan_init_done)
1956 {
1957 struct orphan_save *ho;
1958 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1959 if (ho->name != NULL)
1960 {
1961 ho->os = lang_output_section_find (ho->name);
1962 if (ho->os != NULL && ho->os->flags == 0)
1963 ho->os->flags = ho->flags;
1964 }
1965 orphan_init_done = 1;
1966 }
1967
523f4c92
L
1968 flags = s->flags;
1969 if (!bfd_link_relocatable (&link_info))
1970 {
1971 nexts = s;
1972 while ((nexts = bfd_get_next_section_by_name (nexts->owner,
1973 nexts)))
1974 if (nexts->output_section == NULL
1975 && (nexts->flags & SEC_EXCLUDE) == 0
1976 && ((nexts->flags ^ flags) & (SEC_LOAD | SEC_ALLOC)) == 0
1977 && (nexts->owner->flags & DYNAMIC) == 0
00f93c44 1978 && !bfd_input_just_syms (nexts->owner))
523f4c92
L
1979 flags = (((flags ^ SEC_READONLY)
1980 | (nexts->flags ^ SEC_READONLY))
1981 ^ SEC_READONLY);
1982 }
1983
99ad8390
NC
1984 /* Try to put the new output section in a reasonable place based
1985 on the section name and section flags. */
1986
1987 place = NULL;
93638471 1988 if ((flags & SEC_ALLOC) == 0)
99ad8390 1989 ;
93638471 1990 else if ((flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
99ad8390 1991 place = &hold[orphan_bss];
93638471 1992 else if ((flags & SEC_READONLY) == 0)
99ad8390 1993 place = &hold[orphan_data];
93638471 1994 else if ((flags & SEC_CODE) == 0)
d4874973
KT
1995 {
1996 place = (!strncmp (secname, ".idata\$", 7) ? &hold[orphan_idata]
1997 : &hold[orphan_rodata]);
1998 }
99ad8390
NC
1999 else
2000 place = &hold[orphan_text];
2001
2002 after = NULL;
2003 if (place != NULL)
2004 {
2005 if (place->os == NULL)
2006 place->os = lang_output_section_find (place->name);
2007 after = place->os;
2008 if (after == NULL)
93638471
AM
2009 after = lang_output_section_find_by_flags (s, flags, &place->os,
2010 NULL);
99ad8390
NC
2011 if (after == NULL)
2012 /* *ABS* is always the first output section statement. */
8ce18f9c 2013 after = (void *) lang_os_list.head;
99ad8390
NC
2014 }
2015
4bf0cafb
DK
2016 /* All sections in an executable must be aligned to a page boundary.
2017 In a relocatable link, just preserve the incoming alignment; the
2018 address is discarded by lang_insert_orphan in that case, anyway. */
99ad8390 2019 address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__"));
8a99a385
AM
2020 os = lang_insert_orphan (s, secname, constraint, after, place, address,
2021 &add_child);
0e1862bb 2022 if (bfd_link_relocatable (&link_info))
4bf0cafb 2023 {
3d9c8f6b 2024 os->section_alignment = exp_intop (1U << s->alignment_power);
4bf0cafb
DK
2025 os->bfd_section->alignment_power = s->alignment_power;
2026 }
99ad8390
NC
2027 }
2028
011aa75f
AM
2029 /* If the section name has a '\$', sort it with the other '\$'
2030 sections. */
2031 for (pl = &os->children.head; *pl != NULL; pl = &(*pl)->header.next)
2032 {
2033 lang_input_section_type *ls;
2034 const char *lname;
99ad8390 2035
011aa75f
AM
2036 if ((*pl)->header.type != lang_input_section_enum)
2037 continue;
99ad8390 2038
011aa75f 2039 ls = &(*pl)->input_section;
99ad8390 2040
fd361982 2041 lname = bfd_section_name (ls->section);
011aa75f
AM
2042 if (strchr (lname, '\$') != NULL
2043 && (dollar == NULL || strcmp (orig_secname, lname) < 0))
2044 break;
2045 }
99ad8390 2046
011aa75f
AM
2047 if (add_child.head != NULL)
2048 {
2049 *add_child.tail = *pl;
2050 *pl = add_child.head;
2051 }
99ad8390 2052
c2edb4b8 2053 return os;
99ad8390
NC
2054}
2055
f38a2680 2056static bool
a9261391 2057gld${EMULATION_NAME}_open_dynamic_archive
99ad8390
NC
2058 (const char *arch ATTRIBUTE_UNUSED,
2059 search_dirs_type *search,
2060 lang_input_statement_type *entry)
2061{
2062 static const struct
2063 {
2064 const char * format;
f38a2680 2065 bool use_prefix;
99ad8390
NC
2066 }
2067 libname_fmt [] =
2068 {
2069 /* Preferred explicit import library for dll's. */
f38a2680 2070 { "lib%s.dll.a", false },
99ad8390 2071 /* Alternate explicit import library for dll's. */
f38a2680 2072 { "%s.dll.a", false },
99ad8390 2073 /* "libfoo.a" could be either an import lib or a static lib.
6c19b93b
AM
2074 For backwards compatibility, libfoo.a needs to precede
2075 libfoo.dll and foo.dll in the search. */
f38a2680 2076 { "lib%s.a", false },
92b93329 2077 /* The 'native' spelling of an import lib name is "foo.lib". */
f38a2680 2078 { "%s.lib", false },
f6c4d4b1 2079 /* PR 22948 - Check for an import library. */
f38a2680 2080 { "lib%s.lib", false },
99ad8390
NC
2081#ifdef DLL_SUPPORT
2082 /* Try "<prefix>foo.dll" (preferred dll name, if specified). */
f38a2680 2083 { "%s%s.dll", true },
99ad8390
NC
2084#endif
2085 /* Try "libfoo.dll" (default preferred dll name). */
f38a2680 2086 { "lib%s.dll", false },
99ad8390 2087 /* Finally try 'native' dll name "foo.dll". */
f38a2680 2088 { "%s.dll", false },
99ad8390
NC
2089 /* Note: If adding more formats to this table, make sure to check to
2090 see if their length is longer than libname_fmt[0].format, and if
2091 so, update the call to xmalloc() below. */
f38a2680 2092 { NULL, false }
99ad8390
NC
2093 };
2094 static unsigned int format_max_len = 0;
2095 const char * filename;
2096 char * full_string;
2097 char * base_string;
2098 unsigned int i;
2099
2100
d4ae5fb0 2101 if (! entry->flags.maybe_archive || entry->flags.full_name_provided)
f38a2680 2102 return false;
99ad8390
NC
2103
2104 filename = entry->filename;
2105
2106 if (format_max_len == 0)
2107 /* We need to allow space in the memory that we are going to allocate
2108 for the characters in the format string. Since the format array is
2109 static we only need to calculate this information once. In theory
2110 this value could also be computed statically, but this introduces
2111 the possibility for a discrepancy and hence a possible memory
2112 corruption. The lengths we compute here will be too long because
2113 they will include any formating characters (%s) in the strings, but
2114 this will not matter. */
2115 for (i = 0; libname_fmt[i].format; i++)
2116 if (format_max_len < strlen (libname_fmt[i].format))
2117 format_max_len = strlen (libname_fmt[i].format);
2118
2119 full_string = xmalloc (strlen (search->name)
2120 + strlen (filename)
2121 + format_max_len
2122#ifdef DLL_SUPPORT
2123 + (pep_dll_search_prefix
2124 ? strlen (pep_dll_search_prefix) : 0)
2125#endif
2126 /* Allow for the terminating NUL and for the path
2127 separator character that is inserted between
2128 search->name and the start of the format string. */
2129 + 2);
2130
2131 sprintf (full_string, "%s/", search->name);
2132 base_string = full_string + strlen (full_string);
2133
2134 for (i = 0; libname_fmt[i].format; i++)
2135 {
92b93329 2136#ifdef DLL_SUPPORT
99ad8390
NC
2137 if (libname_fmt[i].use_prefix)
2138 {
2139 if (!pep_dll_search_prefix)
2140 continue;
2141 sprintf (base_string, libname_fmt[i].format, pep_dll_search_prefix, filename);
2142 }
2143 else
2144#endif
2145 sprintf (base_string, libname_fmt[i].format, filename);
2146
2147 if (ldfile_try_open_bfd (full_string, entry))
2148 break;
2149 }
2150
2151 if (!libname_fmt[i].format)
2152 {
2153 free (full_string);
f38a2680 2154 return false;
99ad8390
NC
2155 }
2156
2157 entry->filename = full_string;
2158
f38a2680 2159 return true;
99ad8390
NC
2160}
2161
2162static int
a9261391 2163gld${EMULATION_NAME}_find_potential_libraries
99ad8390
NC
2164 (char *name, lang_input_statement_type *entry)
2165{
2166 return ldfile_open_file_search (name, entry, "", ".lib");
2167}
2168\f
2169static char *
a9261391 2170gld${EMULATION_NAME}_get_script (int *isfile)
99ad8390 2171EOF
1c9c9b9b
AM
2172
2173if test x"$COMPILE_IN" = xyes
2174then
99ad8390 2175# Scripts compiled in.
1c9c9b9b 2176
99ad8390
NC
2177# sed commands to quote an ld script as a C string.
2178sc="-f stringify.sed"
2179
92b93329 2180fragment <<EOF
99ad8390
NC
2181{
2182 *isfile = 0;
2183
0e1862bb 2184 if (bfd_link_relocatable (&link_info) && config.build_constructors)
99ad8390
NC
2185 return
2186EOF
2187sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
0e1862bb 2188echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
99ad8390
NC
2189sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
2190echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
2191sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
2192echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
2193sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
e2a83dd0 2194if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
6ec6c79c 2195echo ' ; else if (link_info.pei386_auto_import == 1 && link_info.pei386_runtime_pseudo_reloc != 2) return' >> e${EMULATION_NAME}.c
e2a83dd0
NC
2196sed $sc ldscripts/${EMULATION_NAME}.xa >> e${EMULATION_NAME}.c
2197fi
99ad8390
NC
2198echo ' ; else return' >> e${EMULATION_NAME}.c
2199sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
2200echo '; }' >> e${EMULATION_NAME}.c
2201
1c9c9b9b
AM
2202else
2203# Scripts read from the filesystem.
2204
2205fragment <<EOF
2206{
2207 *isfile = 1;
2208
2209 if (bfd_link_relocatable (&link_info) && config.build_constructors)
2210 return "ldscripts/${EMULATION_NAME}.xu";
2211 else if (bfd_link_relocatable (&link_info))
2212 return "ldscripts/${EMULATION_NAME}.xr";
2213 else if (!config.text_read_only)
2214 return "ldscripts/${EMULATION_NAME}.xbn";
2215 else if (!config.magic_demand_paged)
2216 return "ldscripts/${EMULATION_NAME}.xn";
2217EOF
2218if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then
2219fragment <<EOF
2220 else if (link_info.pei386_auto_import == 1
2221 && link_info.pei386_runtime_pseudo_reloc != 2)
2222 return "ldscripts/${EMULATION_NAME}.xa";
2223EOF
2224fi
2225fragment <<EOF
2226 else
2227 return "ldscripts/${EMULATION_NAME}.x";
2228}
2229EOF
2230fi
2231
a9261391
L
2232LDEMUL_AFTER_PARSE=gld${EMULATION_NAME}_after_parse
2233LDEMUL_AFTER_OPEN=gld${EMULATION_NAME}_after_open
2234LDEMUL_BEFORE_ALLOCATION=gld${EMULATION_NAME}_before_allocation
2235LDEMUL_FINISH=gld${EMULATION_NAME}_finish
2236LDEMUL_OPEN_DYNAMIC_ARCHIVE=gld${EMULATION_NAME}_open_dynamic_archive
2237LDEMUL_PLACE_ORPHAN=gld${EMULATION_NAME}_place_orphan
2238LDEMUL_SET_SYMBOLS=gld${EMULATION_NAME}_set_symbols
2239LDEMUL_ADD_OPTIONS=gld${EMULATION_NAME}_add_options
2240LDEMUL_HANDLE_OPTION=gld${EMULATION_NAME}_handle_option
2241LDEMUL_UNRECOGNIZED_FILE=gld${EMULATION_NAME}_unrecognized_file
2242LDEMUL_LIST_OPTIONS=gld${EMULATION_NAME}_list_options
2243LDEMUL_RECOGNIZED_FILE=gld${EMULATION_NAME}_recognized_file
2244LDEMUL_FIND_POTENTIAL_LIBRARIES=gld${EMULATION_NAME}_find_potential_libraries
2245
2246source_em ${srcdir}/emultempl/emulation.em