[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
|
bool
|
||||||
file::valid() const
|
file::valid(elf::filetype type) const
|
||||||
{
|
{
|
||||||
if (m_data.count < sizeof(file_header))
|
if (m_data.count < sizeof(file_header))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
const file_header *fheader = header();
|
const file_header *fheader = header();
|
||||||
|
|
||||||
return
|
bool abi_valid =
|
||||||
fheader->magic == expected_magic &&
|
fheader->magic == expected_magic &&
|
||||||
fheader->word_size == wordsize::bits64 &&
|
fheader->word_size == wordsize::bits64 &&
|
||||||
fheader->endianness == encoding::lsb &&
|
fheader->endianness == encoding::lsb &&
|
||||||
fheader->os_abi == osabi::sysV &&
|
fheader->os_abi == osabi::sysV &&
|
||||||
fheader->file_type == filetype::executable &&
|
|
||||||
fheader->machine_type == machine::x64 &&
|
fheader->machine_type == machine::x64 &&
|
||||||
fheader->ident_version == 1 &&
|
fheader->ident_version == 1 &&
|
||||||
fheader->version == 1;
|
fheader->version == 1;
|
||||||
|
|
||||||
|
if (!abi_valid)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (type != filetype::none && fheader->file_type != type)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
uintptr_t
|
uintptr_t
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <elf/headers.h>
|
||||||
#include <util/counted.h>
|
#include <util/counted.h>
|
||||||
#include <util/pointers.h>
|
#include <util/pointers.h>
|
||||||
|
|
||||||
@@ -42,8 +43,9 @@ public:
|
|||||||
file(util::const_buffer data);
|
file(util::const_buffer data);
|
||||||
|
|
||||||
/// Check the validity of the ELF data
|
/// Check the validity of the ELF data
|
||||||
|
/// \arg type Expected file type of the data
|
||||||
/// \returns true for valid ELF 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
|
/// Get the entrypoint address of the program image
|
||||||
/// \returns A pointer to the entrypoint of the program
|
/// \returns A pointer to the entrypoint of the program
|
||||||
@@ -60,10 +62,14 @@ public:
|
|||||||
/// Get the ELF section headers
|
/// Get the ELF section headers
|
||||||
inline const subheaders<section_header> & sections() const { return m_sections; }
|
inline const subheaders<section_header> & sections() const { return m_sections; }
|
||||||
|
|
||||||
|
/// Get the ELF file header
|
||||||
inline const file_header * header() const {
|
inline const file_header * header() const {
|
||||||
return reinterpret_cast<const file_header *>(m_data.pointer);
|
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:
|
private:
|
||||||
subheaders<segment_header> m_segments;
|
subheaders<segment_header> m_segments;
|
||||||
subheaders<section_header> m_sections;
|
subheaders<section_header> m_sections;
|
||||||
|
|||||||
Reference in New Issue
Block a user