[elf] Allow checking for different file types

Previously `elf::file::valid()` only returned true for ELF files of type
`ET_EXEC`, now allow passing in of an expected file type.
This commit is contained in:
Justin C. Miller
2023-08-05 17:37:45 -07:00
parent bbe27c6b53
commit c86c0f2ae6
2 changed files with 18 additions and 5 deletions

View File

@@ -20,22 +20,29 @@ file::file(util::const_buffer data) :
}
bool
file::valid() const
file::valid(elf::filetype type) const
{
if (m_data.count < sizeof(file_header))
return false;
const file_header *fheader = header();
return
bool abi_valid =
fheader->magic == expected_magic &&
fheader->word_size == wordsize::bits64 &&
fheader->endianness == encoding::lsb &&
fheader->os_abi == osabi::sysV &&
fheader->file_type == filetype::executable &&
fheader->machine_type == machine::x64 &&
fheader->ident_version == 1 &&
fheader->version == 1;
if (!abi_valid)
return false;
if (type != filetype::none && fheader->file_type != type)
return false;
return true;
}
uintptr_t

View File

@@ -2,6 +2,7 @@
#include <stddef.h>
#include <stdint.h>
#include <elf/headers.h>
#include <util/counted.h>
#include <util/pointers.h>
@@ -42,8 +43,9 @@ public:
file(util::const_buffer data);
/// Check the validity of the ELF data
/// \arg type Expected file type of the data
/// \returns true for valid ELF data
bool valid() const;
bool valid(elf::filetype type = elf::filetype::executable) const;
/// Get the entrypoint address of the program image
/// \returns A pointer to the entrypoint of the program
@@ -60,10 +62,14 @@ public:
/// Get the ELF section headers
inline const subheaders<section_header> & sections() const { return m_sections; }
/// Get the ELF file header
inline const file_header * header() const {
return reinterpret_cast<const file_header *>(m_data.pointer);
}
/// Get the ELF file type from the header
inline filetype type() const { return header()->file_type; }
private:
subheaders<segment_header> m_segments;
subheaders<section_header> m_sections;