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