Skip to content

Commit

Permalink
Fix error when rewriting an ELF with DT_RELR relocations
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Sep 14, 2024
1 parent cd81ed3 commit bf931df
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
2 changes: 2 additions & 0 deletions doc/sphinx/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 0 additions & 17 deletions src/ELF/Binary.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ void Binary::patch_relocations<ARCH::ARM>(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) {
Expand Down Expand Up @@ -82,10 +78,6 @@ void Binary::patch_relocations<ARCH::AARCH64>(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) {
Expand Down Expand Up @@ -153,11 +145,6 @@ void Binary::patch_relocations<ARCH::I386>(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) {
Expand Down Expand Up @@ -196,10 +183,6 @@ void Binary::patch_relocations<ARCH::X86_64>(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) {
Expand Down
8 changes: 8 additions & 0 deletions tests/elf/test_relr_relocations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bf931df

Please sign in to comment.