From bf931df30385ee064a6d5cd98b2d4049e4b75efc Mon Sep 17 00:00:00 2001 From: Romain Thomas Date: Sat, 14 Sep 2024 06:31:20 +0200 Subject: [PATCH] Fix error when rewriting an ELF with `DT_RELR` relocations --- doc/sphinx/changelog.rst | 2 ++ src/ELF/Binary.tcc | 17 ----------------- tests/elf/test_relr_relocations.py | 8 ++++++++ 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/doc/sphinx/changelog.rst b/doc/sphinx/changelog.rst index dc036a38a9..44dd417ba9 100644 --- a/doc/sphinx/changelog.rst +++ b/doc/sphinx/changelog.rst @@ -56,6 +56,8 @@ Changelog :ELF: + * Fix a critical error when rewriting ELF file with ``DT_RELR`` relocations. + This error leads to a crash of the modified binary. * Fix error while (re)generating ELF's RELR relocations (:issue:`1097`) * Add support for RISC-V architecture * Fix bug when trying to remove a dynamic symbol that is associated with diff --git a/src/ELF/Binary.tcc b/src/ELF/Binary.tcc index 81f7930939..bf31e38ee9 100644 --- a/src/ELF/Binary.tcc +++ b/src/ELF/Binary.tcc @@ -44,10 +44,6 @@ void Binary::patch_relocations(uint64_t from, uint64_t shift) { relocation.address(relocation.address() + shift); } - if (relocation.encoding() == Relocation::ENCODING::RELR) { - continue; - } - const Relocation::TYPE type = relocation.type(); switch (type) { @@ -82,10 +78,6 @@ void Binary::patch_relocations(uint64_t from, uint64_t shift) { relocation.address(relocation.address() + shift); } - if (relocation.encoding() == Relocation::ENCODING::RELR) { - continue; - } - const Relocation::TYPE type = relocation.type(); switch (type) { @@ -153,11 +145,6 @@ void Binary::patch_relocations(uint64_t from, uint64_t shift) { //shift_code(relocation.address(), shift, relocation.size() / 8); relocation.address(relocation.address() + shift); } - - if (relocation.encoding() == Relocation::ENCODING::RELR) { - continue; - } - const Relocation::TYPE type = relocation.type(); switch (type) { @@ -196,10 +183,6 @@ void Binary::patch_relocations(uint64_t from, uint64_t shift) { relocation.address(relocation.address() + shift); } - if (relocation.encoding() == Relocation::ENCODING::RELR) { - continue; - } - const Relocation::TYPE type = relocation.type(); switch (type) { diff --git a/tests/elf/test_relr_relocations.py b/tests/elf/test_relr_relocations.py index 5c7ebe2911..f423e6b041 100644 --- a/tests/elf/test_relr_relocations.py +++ b/tests/elf/test_relr_relocations.py @@ -41,3 +41,11 @@ def test_relr_relocations(tmp_path: Path): lib = ctypes.cdll.LoadLibrary(out.as_posix()) assert lib.cos is not None +def test_relr_addend(tmp_path: Path): + elf = lief.ELF.parse(get_sample("ELF/ls-glibc2.40-relr.elf")) + elf.relocate_phdr_table() + out = tmp_path / "out.elf" + elf.write(out.as_posix()) + + new_elf = lief.ELF.parse(out) + assert new_elf.get_int_from_virtual_address(0x21f40, 8) == 0xa680