diff options
author | H.J. Lu <hjl.tools@gmail.com> | 2021-03-05 18:24:56 -0800 |
---|---|---|
committer | H.J. Lu <hjl.tools@gmail.com> | 2021-03-05 18:25:06 -0800 |
commit | 8c0546e928b557f10cb5aba2a91f3ecee660029d (patch) | |
tree | fa6de685644e0fc9b754cd1d81a28c1a7f6c3ee8 /bfd | |
parent | b0a8c2ff9c1b5f7d2e1ef1e0d65dcff0e9f089cc (diff) | |
download | binutils-8c0546e928b557f10cb5aba2a91f3ecee660029d.zip binutils-8c0546e928b557f10cb5aba2a91f3ecee660029d.tar.gz binutils-8c0546e928b557f10cb5aba2a91f3ecee660029d.tar.bz2 |
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.
Diffstat (limited to 'bfd')
-rw-r--r-- | bfd/ChangeLog | 15 | ||||
-rw-r--r-- | bfd/bfd.c | 11 | ||||
-rw-r--r-- | bfd/coff-x86_64.c | 35 | ||||
-rw-r--r-- | bfd/elf-bfd.h | 4 | ||||
-rw-r--r-- | bfd/libbfd-in.h | 2 | ||||
-rw-r--r-- | bfd/libbfd.h | 2 | ||||
-rw-r--r-- | bfd/pe-x86_64.c | 32 |
7 files changed, 96 insertions, 5 deletions
diff --git a/bfd/ChangeLog b/bfd/ChangeLog index dac602b..a43a3c2 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,18 @@ +2021-03-05 H.J. Lu <hongjiu.lu@intel.com> + + 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. + 2021-03-05 Craig Blackmore <craig.blackmore@embecosm.com> Andrew Burgess <andrew.burgess@embecosm.com> @@ -2808,3 +2808,14 @@ bfd_convert_section_contents (bfd *ibfd, sec_ptr isec, bfd *obfd, *ptr_size = size; return TRUE; } + +/* Get the linker information. */ + +struct bfd_link_info * +_bfd_get_link_info (bfd *abfd) +{ + if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) + return NULL; + + return elf_link_info (abfd); +} diff --git a/bfd/coff-x86_64.c b/bfd/coff-x86_64.c index 5b09023..870df2b 100644 --- a/bfd/coff-x86_64.c +++ b/bfd/coff-x86_64.c @@ -131,11 +131,38 @@ coff_amd64_reloc (bfd *abfd, diff -= reloc_entry->howto->type - R_AMD64_PCRLONG; } - /* FIXME: How should this case be handled? */ if (reloc_entry->howto->type == R_AMD64_IMAGEBASE - && output_bfd != NULL - && bfd_get_flavour (output_bfd) == bfd_target_coff_flavour) - diff -= pe_data (output_bfd)->pe_opthdr.ImageBase; + && output_bfd == NULL) + { + bfd *obfd = input_section->output_section->owner; + struct bfd_link_info *link_info; + struct bfd_link_hash_entry *h; + switch (bfd_get_flavour (obfd)) + { + case bfd_target_coff_flavour: + diff -= pe_data (obfd)->pe_opthdr.ImageBase; + break; + case bfd_target_elf_flavour: + /* Subtract __ImageBase. */ + link_info = _bfd_get_link_info (obfd); + if (link_info == NULL) + return bfd_reloc_dangerous; + h = bfd_link_hash_lookup (link_info->hash, "__ImageBase", + FALSE, FALSE, FALSE); + if (h == NULL) + return bfd_reloc_dangerous; + while (h->type == bfd_link_hash_indirect) + h = h->u.i.link; + /* ELF symbols in relocatable files are section relative, + but in nonrelocatable files they are virtual addresses. */ + diff -= (h->u.def.value + + h->u.def.section->output_offset + + h->u.def.section->output_section->vma); + break; + default: + break; + } + } #endif #define DOIT(x) \ diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index c40030b..04785a8 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -1887,6 +1887,9 @@ struct output_elf_obj_tdata /* Used when laying out sections. */ file_ptr next_file_pos; + /* Linker information. */ + struct bfd_link_info *link_info; + int num_section_syms; unsigned int shstrtab_section, strtab_section; @@ -2064,6 +2067,7 @@ struct elf_obj_tdata #define elf_elfsections(bfd) (elf_tdata(bfd) -> elf_sect_ptr) #define elf_numsections(bfd) (elf_tdata(bfd) -> num_elf_sections) #define elf_seg_map(bfd) (elf_tdata(bfd) -> o->seg_map) +#define elf_link_info(bfd) (elf_tdata(bfd) -> o->link_info) #define elf_next_file_pos(bfd) (elf_tdata(bfd) -> o->next_file_pos) #define elf_eh_frame_hdr(bfd) (elf_tdata(bfd) -> o->eh_frame_hdr) #define elf_stack_flags(bfd) (elf_tdata(bfd) -> o->stack_flags) diff --git a/bfd/libbfd-in.h b/bfd/libbfd-in.h index 2dc20ec..62b1cee 100644 --- a/bfd/libbfd-in.h +++ b/bfd/libbfd-in.h @@ -899,6 +899,8 @@ extern bfd_vma _bfd_safe_read_leb128 extern bfd_byte * _bfd_write_unsigned_leb128 (bfd_byte *, bfd_byte *, bfd_vma) ATTRIBUTE_HIDDEN; +extern struct bfd_link_info *_bfd_get_link_info (bfd *); + #if GCC_VERSION >= 7000 #define _bfd_mul_overflow(a, b, res) __builtin_mul_overflow (a, b, res) #else diff --git a/bfd/libbfd.h b/bfd/libbfd.h index 7271a2a..3a481ea 100644 --- a/bfd/libbfd.h +++ b/bfd/libbfd.h @@ -904,6 +904,8 @@ extern bfd_vma _bfd_safe_read_leb128 extern bfd_byte * _bfd_write_unsigned_leb128 (bfd_byte *, bfd_byte *, bfd_vma) ATTRIBUTE_HIDDEN; +extern struct bfd_link_info *_bfd_get_link_info (bfd *); + #if GCC_VERSION >= 7000 #define _bfd_mul_overflow(a, b, res) __builtin_mul_overflow (a, b, res) #else diff --git a/bfd/pe-x86_64.c b/bfd/pe-x86_64.c index 5b73444..771651a 100644 --- a/bfd/pe-x86_64.c +++ b/bfd/pe-x86_64.c @@ -22,6 +22,8 @@ #include "sysdep.h" #include "bfd.h" +#include "coff/internal.h" +#include "libcoff.h" #define TARGET_SYM x86_64_pe_vec #define TARGET_NAME "pe-x86-64" @@ -66,5 +68,33 @@ extern bfd_boolean pex64_bfd_print_pdata (bfd *, void *); #define bfd_pe_print_pdata pex64_bfd_print_pdata -#include "coff-x86_64.c" +static bfd_boolean +pex64_link_add_symbols (bfd *abfd, struct bfd_link_info *info) +{ + if (bfd_link_pde (info) + && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour) + { + /* NB: When linking Windows x86-64 relocatable object files to + generate ELF executable, create an indirect reference to + __executable_start for __ImageBase to support R_AMD64_IMAGEBASE + relocation which is relative to __ImageBase. */ + struct bfd_link_hash_entry *h, *hi; + hi = bfd_link_hash_lookup (info->hash, "__ImageBase", TRUE, FALSE, + FALSE); + if (hi->type == bfd_link_hash_new + || hi->type == bfd_link_hash_undefined + || hi->type == bfd_link_hash_undefweak) + { + h = bfd_link_hash_lookup (info->hash, "__executable_start", + TRUE, FALSE, TRUE); + hi->type = bfd_link_hash_indirect; + hi->u.i.link = h; + } + } + + return _bfd_coff_link_add_symbols (abfd, info); +} +#define coff_bfd_link_add_symbols pex64_link_add_symbols + +#include "coff-x86_64.c" |