2020-01-05 10:37:05 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2018-10-18 16:03:10 +03:00
|
|
|
#include <AK/kstdio.h>
|
2020-01-05 10:37:05 +03:00
|
|
|
#include <LibELF/ELFImage.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
ELFImage::ELFImage(const u8* buffer)
|
2018-10-18 16:38:04 +03:00
|
|
|
: m_buffer(buffer)
|
2018-10-18 16:03:10 +03:00
|
|
|
{
|
2018-11-04 16:09:30 +03:00
|
|
|
m_valid = parse();
|
2018-10-18 16:03:10 +03:00
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
ELFImage::~ELFImage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-31 19:31:23 +03:00
|
|
|
static const char* object_file_type_to_string(Elf32_Half type)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
switch (type) {
|
2019-06-07 18:12:30 +03:00
|
|
|
case ET_NONE:
|
|
|
|
return "None";
|
|
|
|
case ET_REL:
|
|
|
|
return "Relocatable";
|
|
|
|
case ET_EXEC:
|
|
|
|
return "Executable";
|
|
|
|
case ET_DYN:
|
|
|
|
return "Shared object";
|
|
|
|
case ET_CORE:
|
|
|
|
return "Core";
|
|
|
|
default:
|
|
|
|
return "(?)";
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 13:13:57 +03:00
|
|
|
const char* ELFImage::section_index_to_string(unsigned index) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
if (index == SHN_UNDEF)
|
|
|
|
return "Undefined";
|
|
|
|
if (index >= SHN_LORESERVE)
|
|
|
|
return "Reserved";
|
|
|
|
return section(index).name();
|
|
|
|
}
|
|
|
|
|
2018-11-04 16:09:30 +03:00
|
|
|
unsigned ELFImage::symbol_count() const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2018-11-04 16:09:30 +03:00
|
|
|
return section(m_symbol_table_section_index).entry_count();
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-07-18 13:13:57 +03:00
|
|
|
void ELFImage::dump() const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2019-12-29 09:20:38 +03:00
|
|
|
dbgprintf("ELFImage{%p} {\n", this);
|
|
|
|
dbgprintf(" is_valid: %u\n", is_valid());
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-04 16:09:30 +03:00
|
|
|
if (!is_valid()) {
|
2019-12-29 09:20:38 +03:00
|
|
|
dbgprintf("}\n");
|
2018-10-10 12:53:07 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-29 09:20:38 +03:00
|
|
|
dbgprintf(" type: %s\n", object_file_type_to_string(header().e_type));
|
|
|
|
dbgprintf(" machine: %u\n", header().e_machine);
|
|
|
|
dbgprintf(" entry: %x\n", header().e_entry);
|
|
|
|
dbgprintf(" shoff: %u\n", header().e_shoff);
|
|
|
|
dbgprintf(" shnum: %u\n", header().e_shnum);
|
|
|
|
dbgprintf(" shstrndx: %u\n", header().e_shstrndx);
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < header().e_shnum; ++i) {
|
|
|
|
auto& section = this->section(i);
|
2019-12-29 09:20:38 +03:00
|
|
|
dbgprintf(" Section %u: {\n", i);
|
|
|
|
dbgprintf(" name: %s\n", section.name());
|
|
|
|
dbgprintf(" type: %x\n", section.type());
|
|
|
|
dbgprintf(" offset: %x\n", section.offset());
|
|
|
|
dbgprintf(" size: %u\n", section.size());
|
|
|
|
dbgprintf(" \n");
|
|
|
|
dbgprintf(" }\n");
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-12-29 09:20:38 +03:00
|
|
|
dbgprintf("Symbol count: %u (table is %u)\n", symbol_count(), m_symbol_table_section_index);
|
2018-11-04 16:09:30 +03:00
|
|
|
for (unsigned i = 1; i < symbol_count(); ++i) {
|
2018-10-10 12:53:07 +03:00
|
|
|
auto& sym = symbol(i);
|
2019-12-29 09:20:38 +03:00
|
|
|
dbgprintf("Symbol @%u:\n", i);
|
|
|
|
dbgprintf(" Name: %s\n", sym.name());
|
|
|
|
dbgprintf(" In section: %s\n", section_index_to_string(sym.section_index()));
|
|
|
|
dbgprintf(" Value: %x\n", sym.value());
|
|
|
|
dbgprintf(" Size: %u\n", sym.size());
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-12-29 09:20:38 +03:00
|
|
|
dbgprintf("}\n");
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2018-11-04 16:09:30 +03:00
|
|
|
unsigned ELFImage::section_count() const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
return header().e_shnum;
|
|
|
|
}
|
|
|
|
|
2018-11-03 12:11:56 +03:00
|
|
|
unsigned ELFImage::program_header_count() const
|
|
|
|
{
|
|
|
|
return header().e_phnum;
|
|
|
|
}
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
bool ELFImage::parse()
|
|
|
|
{
|
|
|
|
// We only support i386.
|
2019-03-27 03:29:49 +03:00
|
|
|
if (header().e_machine != 3) {
|
2019-12-29 09:20:38 +03:00
|
|
|
dbgprintf("ELFImage::parse(): e_machine=%u not supported!\n", header().e_machine);
|
2018-10-10 12:53:07 +03:00
|
|
|
return false;
|
2019-03-27 03:29:49 +03:00
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
// First locate the string tables.
|
2018-11-04 16:09:30 +03:00
|
|
|
for (unsigned i = 0; i < section_count(); ++i) {
|
|
|
|
auto& sh = section_header(i);
|
2018-10-10 12:53:07 +03:00
|
|
|
if (sh.sh_type == SHT_SYMTAB) {
|
2019-11-28 22:53:02 +03:00
|
|
|
ASSERT(!m_symbol_table_section_index || m_symbol_table_section_index == i);
|
2018-11-04 16:09:30 +03:00
|
|
|
m_symbol_table_section_index = i;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
if (sh.sh_type == SHT_STRTAB && i != header().e_shstrndx) {
|
2020-01-01 00:45:50 +03:00
|
|
|
if (StringView(".strtab") == section_header_table_string(sh.sh_name))
|
|
|
|
m_string_table_section_index = i;
|
|
|
|
}
|
|
|
|
if (sh.sh_type == SHT_DYNAMIC) {
|
|
|
|
ASSERT(!m_dynamic_section_index || m_dynamic_section_index == i);
|
|
|
|
m_dynamic_section_index = i;
|
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
2019-11-28 22:53:02 +03:00
|
|
|
|
|
|
|
// Then create a name-to-index map.
|
|
|
|
for (unsigned i = 0; i < section_count(); ++i) {
|
|
|
|
auto& section = this->section(i);
|
|
|
|
m_sections.set(section.name(), move(i));
|
|
|
|
}
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-04 16:09:30 +03:00
|
|
|
const char* ELFImage::section_header_table_string(unsigned offset) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2018-11-04 16:09:30 +03:00
|
|
|
auto& sh = section_header(header().e_shstrndx);
|
2018-10-10 12:53:07 +03:00
|
|
|
if (sh.sh_type != SHT_STRTAB)
|
|
|
|
return nullptr;
|
2018-11-04 16:09:30 +03:00
|
|
|
return raw_data(sh.sh_offset + offset);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2018-11-04 16:09:30 +03:00
|
|
|
const char* ELFImage::table_string(unsigned offset) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2018-11-04 16:09:30 +03:00
|
|
|
auto& sh = section_header(m_string_table_section_index);
|
2018-10-10 12:53:07 +03:00
|
|
|
if (sh.sh_type != SHT_STRTAB)
|
|
|
|
return nullptr;
|
2018-11-04 16:09:30 +03:00
|
|
|
return raw_data(sh.sh_offset + offset);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2018-11-04 16:09:30 +03:00
|
|
|
const char* ELFImage::raw_data(unsigned offset) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2018-11-08 23:20:09 +03:00
|
|
|
return reinterpret_cast<const char*>(m_buffer) + offset;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const Elf32_Ehdr& ELFImage::header() const
|
|
|
|
{
|
2018-11-04 16:09:30 +03:00
|
|
|
return *reinterpret_cast<const Elf32_Ehdr*>(raw_data(0));
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2018-11-03 12:11:56 +03:00
|
|
|
const Elf32_Phdr& ELFImage::program_header_internal(unsigned index) const
|
|
|
|
{
|
|
|
|
ASSERT(index < header().e_phnum);
|
2018-11-04 16:09:30 +03:00
|
|
|
return *reinterpret_cast<const Elf32_Phdr*>(raw_data(header().e_phoff + (index * sizeof(Elf32_Phdr))));
|
2018-11-03 12:11:56 +03:00
|
|
|
}
|
|
|
|
|
2018-11-04 16:09:30 +03:00
|
|
|
const Elf32_Shdr& ELFImage::section_header(unsigned index) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
ASSERT(index < header().e_shnum);
|
2020-01-01 00:45:50 +03:00
|
|
|
return *reinterpret_cast<const Elf32_Shdr*>(raw_data(header().e_shoff + (index * header().e_shentsize)));
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const ELFImage::Symbol ELFImage::symbol(unsigned index) const
|
|
|
|
{
|
2018-11-04 16:09:30 +03:00
|
|
|
ASSERT(index < symbol_count());
|
2019-01-31 19:31:23 +03:00
|
|
|
auto* raw_syms = reinterpret_cast<const Elf32_Sym*>(raw_data(section(m_symbol_table_section_index).offset()));
|
|
|
|
return Symbol(*this, index, raw_syms[index]);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const ELFImage::Section ELFImage::section(unsigned index) const
|
|
|
|
{
|
2018-11-04 16:09:30 +03:00
|
|
|
ASSERT(index < section_count());
|
2018-10-10 12:53:07 +03:00
|
|
|
return Section(*this, index);
|
|
|
|
}
|
|
|
|
|
2018-11-03 12:11:56 +03:00
|
|
|
const ELFImage::ProgramHeader ELFImage::program_header(unsigned index) const
|
|
|
|
{
|
|
|
|
ASSERT(index < program_header_count());
|
|
|
|
return ProgramHeader(*this, index);
|
|
|
|
}
|
2019-11-28 22:53:02 +03:00
|
|
|
|
|
|
|
const ELFImage::Relocation ELFImage::RelocationSection::relocation(unsigned index) const
|
|
|
|
{
|
|
|
|
ASSERT(index < relocation_count());
|
|
|
|
auto* rels = reinterpret_cast<const Elf32_Rel*>(m_image.raw_data(offset()));
|
|
|
|
return Relocation(m_image, rels[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ELFImage::RelocationSection ELFImage::Section::relocations() const
|
|
|
|
{
|
2020-01-05 10:37:05 +03:00
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(".rel");
|
|
|
|
builder.append(name());
|
2019-11-28 22:53:02 +03:00
|
|
|
|
2020-01-05 10:37:05 +03:00
|
|
|
auto relocation_section = m_image.lookup_section(builder.to_string());
|
2019-11-28 22:53:02 +03:00
|
|
|
if (relocation_section.type() != SHT_REL)
|
|
|
|
return static_cast<const RelocationSection>(m_image.section(0));
|
|
|
|
|
|
|
|
#ifdef ELFIMAGE_DEBUG
|
2019-12-29 09:20:38 +03:00
|
|
|
dbgprintf("Found relocations for %s in %s\n", name(), relocation_section.name());
|
2019-11-28 22:53:02 +03:00
|
|
|
#endif
|
|
|
|
return static_cast<const RelocationSection>(relocation_section);
|
|
|
|
}
|
|
|
|
|
2020-01-05 10:37:05 +03:00
|
|
|
const ELFImage::Section ELFImage::lookup_section(const String& name) const
|
2019-11-28 22:53:02 +03:00
|
|
|
{
|
|
|
|
if (auto it = m_sections.find(name); it != m_sections.end())
|
|
|
|
return section((*it).value);
|
|
|
|
return section(0);
|
|
|
|
}
|
2020-01-01 00:45:50 +03:00
|
|
|
|
|
|
|
const ELFImage::DynamicSection ELFImage::dynamic_section() const
|
|
|
|
{
|
|
|
|
ASSERT(is_dynamic());
|
|
|
|
return section(m_dynamic_section_index);
|
|
|
|
}
|