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