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