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