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