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