mirror of
https://github.com/justinian/jsix.git
synced 2025-12-10 08:24:32 -08:00
[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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
/// \returns true for valid ELF data
|
||||
bool valid() const;
|
||||
/// \arg type Expected file type of the data
|
||||
/// \returns true for valid ELF data
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user