I'm a tabs guy. I like tabs, it's an elegant way to represent indentation instead of brute-forcing it. But I have to admit that the world seems to be going towards spaces, and tooling tends not to play nice with tabs. So here we go, changing the whole repo to spaces since I'm getting tired of all the inconsistent formatting.
46 lines
1.2 KiB
C++
46 lines
1.2 KiB
C++
#include "elf/file.h"
|
|
#include "elf/headers.h"
|
|
#include "pointer_manipulation.h"
|
|
|
|
static const uint32_t expected_magic = 0x464c457f; // "\x7f" "ELF"
|
|
|
|
namespace elf {
|
|
|
|
inline const file_header * fh(const void *data) { return reinterpret_cast<const file_header*>(data); }
|
|
|
|
file::file(const void *data, size_t size) :
|
|
m_programs(offset_ptr<program_header>(data, fh(data)->ph_offset), fh(data)->ph_entsize, fh(data)->ph_num),
|
|
m_sections(offset_ptr<section_header>(data, fh(data)->sh_offset), fh(data)->sh_entsize, fh(data)->sh_num),
|
|
m_data(data),
|
|
m_size(size)
|
|
{
|
|
}
|
|
|
|
bool
|
|
file::valid() const
|
|
{
|
|
if (m_size < sizeof(file_header))
|
|
return false;
|
|
|
|
const file_header *fheader = header();
|
|
|
|
return
|
|
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;
|
|
}
|
|
|
|
uintptr_t
|
|
file::entrypoint() const
|
|
{
|
|
return static_cast<uintptr_t>(header()->entrypoint);
|
|
}
|
|
|
|
|
|
} // namespace elf
|