From 8c0546e928b557f10cb5aba2a91f3ecee660029d Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Fri, 5 Mar 2021 18:24:56 -0800 Subject: elf/x86-64: Subtract __ImageBase for R_AMD64_IMAGEBASE When linking Windows x86-64 relocatable object files to generate x86-64 ELF executable, we need to subtract __ImageBase, aka __executable_start, for R_AMD64_IMAGEBASE relocation: 1. Add link_info to struct output_elf_obj_tdata to store linker info and _bfd_get_link_info() to retrieve it. 2. Add ldelf_set_output_arch to set up link_info. 3. Add pex64_link_add_symbols to create an indirect reference to __executable_start for __ImageBase to support R_AMD64_IMAGEBASE relocation when adding symbols from Windows x86-64 relocatable object files to generate x86-64 ELF executable. 4. Also subtract __ImageBase for R_AMD64_IMAGEBASE when generating x86-64 ELF executable. bfd/ PR ld/27425 PR ld/27432 * bfd.c (_bfd_get_link_info): New function. * elf-bfd.h (output_elf_obj_tdata): Add link_info. (elf_link_info): New. * libbfd-in.h (_bfd_get_link_info): New prototype. * coff-x86_64.c (coff_amd64_reloc): Also subtract __ImageBase for R_AMD64_IMAGEBASE when generating x86-64 ELF executable. * pe-x86_64.c: Include "coff/internal.h" and "libcoff.h". (pex64_link_add_symbols): New function. (coff_bfd_link_add_symbols): New macro. * libbfd.h: Regenerated. ld/ PR ld/27425 PR ld/27432 * ldelf.c (ldelf_set_output_arch): New function. * ldelf.h (ldelf_set_output_arch): New prototype. * emultempl/elf.em (LDEMUL_SET_OUTPUT_ARCH): Default to ldelf_set_output_arch. * ld-x86-64/pe-x86-64-1.od: Expect __executable_start. * testsuite/ld-x86-64/pe-x86-64-2.od: Likewise. * testsuite/ld-x86-64/pe-x86-64-3.od: Likewise. * testsuite/ld-x86-64/pe-x86-64-4.od: Likewise. * testsuite/ld-x86-64/pe-x86-64-5.od: Likewise. * testsuite/ld-x86-64/pe-x86-64-5.rd: Likewise. * testsuite/ld-x86-64/pe-x86-64-6.obj.bz2: New file. * testsuite/ld-x86-64/pe-x86-64-6.od: Likewise. * testsuite/ld-x86-64/pe-x86-64.exp: Run ld/27425 test. --- ld/emultempl/elf.em | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ld/emultempl') diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em index cea89e5..9e7c3d8 100644 --- a/ld/emultempl/elf.em +++ b/ld/emultempl/elf.em @@ -919,7 +919,7 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = ${LDEMUL_AFTER_CHECK_RELOCS-after_check_relocs_default}, ${LDEMUL_BEFORE_PLACE_ORPHANS-ldelf_before_place_orphans}, ${LDEMUL_AFTER_ALLOCATION-gld${EMULATION_NAME}_after_allocation}, - ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default}, + ${LDEMUL_SET_OUTPUT_ARCH-ldelf_set_output_arch}, ${LDEMUL_CHOOSE_TARGET-ldemul_default_target}, ${LDEMUL_BEFORE_ALLOCATION-gld${EMULATION_NAME}_before_allocation}, ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script}, -- cgit v1.1