diff options
author | Jan Beulich <jbeulich@suse.com> | 2022-03-23 08:43:13 +0100 |
---|---|---|
committer | Jan Beulich <jbeulich@suse.com> | 2022-03-23 08:43:13 +0100 |
commit | 36e2d65d26622e83fa4c3af8289f6728376b9e89 (patch) | |
tree | 06c5338662f8a1f490014ef13c8161aafe39048a /bfd/elfcode.h | |
parent | b3446f947bd16a0e2a211343d076c36e4de68a2c (diff) | |
download | binutils-36e2d65d26622e83fa4c3af8289f6728376b9e89.zip binutils-36e2d65d26622e83fa4c3af8289f6728376b9e89.tar.gz binutils-36e2d65d26622e83fa4c3af8289f6728376b9e89.tar.bz2 |
ELF32: don't silently truncate relocation addends
At least x86-64's x32 sub-mode and RISC-V's 32-bit mode calculate
addends as 64-bit values, but store them in signed 32-bit fields when
generating the file without encountering any earlier error. When the
relocated field is a 64-bit one, the value resulting after processing
the relocation record when linking (or the latest when loading) may
thus be wrong due to the truncation.
With the code change in place, one x32 testcase actually triggers the
new diagnostic. That one case of too large a (negative) addend is being
adjusted alongside the addition of a new testcase to actually trigger
the new error. (Note that due to internal BFD behavior the relocation in
.data doesn't get processed anymore after the errors in .text.)
Note that in principle it is possible to express 64-bit relocations in
ELF32, but this would require .rel relocations, i.e. with the addend
stored in the 64-bit field being relocated. But I guess it would be a
lot of effort for little gain to actually support this.
Diffstat (limited to 'bfd/elfcode.h')
-rw-r--r-- | bfd/elfcode.h | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/bfd/elfcode.h b/bfd/elfcode.h index c3ab053..4d4cb68 100644 --- a/bfd/elfcode.h +++ b/bfd/elfcode.h @@ -997,6 +997,19 @@ elf_write_relocs (bfd *abfd, asection *sec, void *data) return; } +#if defined(BFD64) && ARCH_SIZE == 32 + if (rela_hdr->sh_type == SHT_RELA + && ptr->howto->bitsize > 32 + && ptr->addend - INT32_MIN > UINT32_MAX) + { + _bfd_error_handler (_("%pB: %pA+%"BFD_VMA_FMT"x: " + "relocation addend %"BFD_VMA_FMT"x too large"), + abfd, sec, ptr->address, ptr->addend); + *failedp = true; + bfd_set_error (bfd_error_bad_value); + } +#endif + src_rela.r_offset = ptr->address + addr_offset; src_rela.r_info = ELF_R_INFO (n, ptr->howto->type); src_rela.r_addend = ptr->addend; |