LibELF: ELF now has methods for accessing more attributes

You can now access the program headers and the memory itself
This commit is contained in:
Bananymous 2023-04-22 15:31:05 +03:00
parent c15f031c3f
commit 10c884bba4
2 changed files with 30 additions and 2 deletions

View File

@ -68,6 +68,20 @@ namespace LibELF
return true; return true;
} }
bool ELF::parse_elf64_program_header(const Elf64ProgramHeader& header)
{
dprintln("program header");
dprintln(" type {H}", header.p_type);
dprintln(" flags {H}", header.p_flags);
dprintln(" offset {H}", header.p_offset);
dprintln(" vaddr {H}", header.p_vaddr);
dprintln(" paddr {H}", header.p_paddr);
dprintln(" filesz {}", header.p_filesz);
dprintln(" memsz {}", header.p_memsz);
dprintln(" align {}", header.p_align);
return true;
}
bool ELF::parse_elf64_section_header(const Elf64SectionHeader& header) bool ELF::parse_elf64_section_header(const Elf64SectionHeader& header)
{ {
if (auto* name = lookup_section_name(header.sh_name)) if (auto* name = lookup_section_name(header.sh_name))
@ -159,6 +173,13 @@ namespace LibELF
if (!parse_elf64_file_header(header)) if (!parse_elf64_file_header(header))
return BAN::Error::from_errno(EINVAL); return BAN::Error::from_errno(EINVAL);
for (size_t i = 0; i < header.e_phnum; i++)
{
auto& program_header = program_header64(i);
if (!parse_elf64_program_header(program_header))
return BAN::Error::from_errno(EINVAL);
}
for (size_t i = 1; i < header.e_shnum; i++) for (size_t i = 1; i < header.e_shnum; i++)
{ {
auto& section_header = section_header64(i); auto& section_header = section_header64(i);
@ -177,12 +198,16 @@ namespace LibELF
const Elf64ProgramHeader& ELF::program_header64(size_t index) const const Elf64ProgramHeader& ELF::program_header64(size_t index) const
{ {
return ((const Elf64ProgramHeader*)(m_data.data() + file_header64().e_phoff))[index]; const auto& file_header = file_header64();
ASSERT(index < file_header.e_phnum);
return *(const Elf64ProgramHeader*)(m_data.data() + file_header.e_phoff + file_header.e_phentsize * index);
} }
const Elf64SectionHeader& ELF::section_header64(size_t index) const const Elf64SectionHeader& ELF::section_header64(size_t index) const
{ {
return ((const Elf64SectionHeader*)(m_data.data() + file_header64().e_shoff))[index]; const auto& file_header = file_header64();
ASSERT(index < file_header.e_shnum);
return *(const Elf64SectionHeader*)(m_data.data() + file_header.e_shoff + file_header.e_shentsize * index);
} }

View File

@ -19,6 +19,8 @@ namespace LibELF
const char* lookup_section_name(uint32_t) const; const char* lookup_section_name(uint32_t) const;
const char* lookup_string(size_t, uint32_t) const; const char* lookup_string(size_t, uint32_t) const;
const uint8_t* data() const { return m_data.data(); }
private: private:
ELF(BAN::Vector<uint8_t>&& data) ELF(BAN::Vector<uint8_t>&& data)
: m_data(BAN::move(data)) : m_data(BAN::move(data))
@ -26,6 +28,7 @@ namespace LibELF
BAN::ErrorOr<void> load(); BAN::ErrorOr<void> load();
bool parse_elf64_file_header(const Elf64FileHeader&); bool parse_elf64_file_header(const Elf64FileHeader&);
bool parse_elf64_program_header(const Elf64ProgramHeader&);
bool parse_elf64_section_header(const Elf64SectionHeader&); bool parse_elf64_section_header(const Elf64SectionHeader&);
private: private: