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