[elf] Ressurect elf library

Resurrect the existing but unused ELF library in libraries/elf, and use
it instead of boot/elf.h for parsing ELF files in the bootloader.

Also adds a const version of offset_iterator called
const_offset_iterator.
This commit is contained in:
Justin C. Miller
2021-07-31 15:10:03 -07:00
parent 5e2cfab7ba
commit 363d30eadc
9 changed files with 189 additions and 250 deletions

View File

@@ -0,0 +1,45 @@
#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