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