Skip to content

Commit

Permalink
Resolve 984
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Oct 28, 2023
1 parent 8bd5e58 commit 9961f66
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/ELF/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ bool Parser::should_swap() const {


ELF_CLASS determine_elf_class(BinaryStream& stream) {
ELF_CLASS from_ei_class = ELF_CLASS::ELFCLASSNONE;
ELF_CLASS from_e_machine = ELF_CLASS::ELFCLASSNONE;
auto from_ei_class = ELF_CLASS::ELFCLASSNONE;
auto from_e_machine = ELF_CLASS::ELFCLASSNONE;
auto file_type = E_TYPE::ET_NONE;

// First, check EI_CLASS
if (auto res = stream.peek<Header::identity_t>()) {
Expand All @@ -218,6 +219,11 @@ ELF_CLASS determine_elf_class(BinaryStream& stream) {
}
}

constexpr size_t e_type_off = offsetof(details::Elf32_Ehdr, e_type);
if (auto res = stream.peek<uint16_t>(e_type_off)) {
file_type = static_cast<E_TYPE>(*res);
}

// Try to determine the size based on Elf_Ehdr.e_machine
//
// typedef struct {
Expand Down Expand Up @@ -261,6 +267,10 @@ ELF_CLASS determine_elf_class(BinaryStream& stream) {
return from_ei_class;
}

if (file_type == E_TYPE::ET_REL) {
return from_ei_class;
}

LIEF_WARN("ELF class from machine type ('{}') does not match ELF class from "
"e_ident ('{}'). The binary has been likely modified.",
to_string(from_e_machine), to_string(from_ei_class));
Expand Down
4 changes: 4 additions & 0 deletions tests/elf/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,7 @@ class Wrong:

def test_path_like():
assert lief.ELF.parse(Path(get_sample('ELF/test_897.elf'))) is not None

def test_984():
elf = lief.ELF.parse(get_sample('ELF/issue_984_ilp32.o'))
assert len(elf.sections) > 0

0 comments on commit 9961f66

Please sign in to comment.