Tidy up ELFLoader.

This commit is contained in:
Andreas Kling 2018-11-04 14:09:30 +01:00
parent 422b5403e5
commit b5c5286ee1
5 changed files with 117 additions and 120 deletions

View file

@ -4,7 +4,7 @@
ELFImage::ELFImage(ByteBuffer&& buffer)
: m_buffer(buffer)
{
m_isValid = parse();
m_valid = parse();
}
ELFImage::~ELFImage()
@ -23,7 +23,7 @@ static const char* objectFileTypeToString(Elf32_Half type)
}
}
const char* ELFImage::sectionIndexToString(unsigned index)
const char* ELFImage::section_index_to_string(unsigned index)
{
if (index == SHN_UNDEF)
return "Undefined";
@ -32,17 +32,17 @@ const char* ELFImage::sectionIndexToString(unsigned index)
return section(index).name();
}
unsigned ELFImage::symbolCount() const
unsigned ELFImage::symbol_count() const
{
return section(m_symbolTableSectionIndex).entryCount();
return section(m_symbol_table_section_index).entry_count();
}
void ELFImage::dump()
{
kprintf("ELFImage{%p} {\n", this);
kprintf(" isValid: %u\n", isValid());
kprintf(" isValid: %u\n", is_valid());
if (!isValid()) {
if (!is_valid()) {
kprintf("}\n");
return;
}
@ -65,12 +65,12 @@ void ELFImage::dump()
kprintf(" }\n");
}
kprintf("Symbol count: %u (table is %u)\n", symbolCount(), m_symbolTableSectionIndex);
for (unsigned i = 1; i < symbolCount(); ++i) {
kprintf("Symbol count: %u (table is %u)\n", symbol_count(), m_symbol_table_section_index);
for (unsigned i = 1; i < symbol_count(); ++i) {
auto& sym = symbol(i);
kprintf("Symbol @%u:\n", i);
kprintf(" Name: %s\n", sym.name());
kprintf(" In section: %s\n", sectionIndexToString(sym.sectionIndex()));
kprintf(" In section: %s\n", section_index_to_string(sym.section_index()));
kprintf(" Value: %x\n", sym.value());
kprintf(" Size: %u\n", sym.size());
}
@ -78,7 +78,7 @@ void ELFImage::dump()
kprintf("}\n");
}
unsigned ELFImage::sectionCount() const
unsigned ELFImage::section_count() const
{
return header().e_shnum;
}
@ -95,43 +95,43 @@ bool ELFImage::parse()
return false;
// First locate the string tables.
for (unsigned i = 0; i < sectionCount(); ++i) {
auto& sh = sectionHeader(i);
for (unsigned i = 0; i < section_count(); ++i) {
auto& sh = section_header(i);
if (sh.sh_type == SHT_SYMTAB) {
ASSERT(!m_symbolTableSectionIndex);
m_symbolTableSectionIndex = i;
ASSERT(!m_symbol_table_section_index);
m_symbol_table_section_index = i;
}
if (sh.sh_type == SHT_STRTAB && i != header().e_shstrndx) {
ASSERT(!m_stringTableSectionIndex);
m_stringTableSectionIndex = i;
ASSERT(!m_string_table_section_index);
m_string_table_section_index = i;
}
}
// Then create a name-to-index map.
for (unsigned i = 0; i < sectionCount(); ++i) {
for (unsigned i = 0; i < section_count(); ++i) {
auto& section = this->section(i);
m_sections.set(section.name(), move(i));
}
return true;
}
const char* ELFImage::sectionHeaderTableString(unsigned offset) const
const char* ELFImage::section_header_table_string(unsigned offset) const
{
auto& sh = sectionHeader(header().e_shstrndx);
auto& sh = section_header(header().e_shstrndx);
if (sh.sh_type != SHT_STRTAB)
return nullptr;
return rawData(sh.sh_offset + offset);
return raw_data(sh.sh_offset + offset);
}
const char* ELFImage::tableString(unsigned offset) const
const char* ELFImage::table_string(unsigned offset) const
{
auto& sh = sectionHeader(m_stringTableSectionIndex);
auto& sh = section_header(m_string_table_section_index);
if (sh.sh_type != SHT_STRTAB)
return nullptr;
return rawData(sh.sh_offset + offset);
return raw_data(sh.sh_offset + offset);
}
const char* ELFImage::rawData(unsigned offset) const
const char* ELFImage::raw_data(unsigned offset) const
{
#ifdef SERENITY
return reinterpret_cast<const char*>(m_buffer.pointer()) + offset;
@ -142,31 +142,31 @@ const char* ELFImage::rawData(unsigned offset) const
const Elf32_Ehdr& ELFImage::header() const
{
return *reinterpret_cast<const Elf32_Ehdr*>(rawData(0));
return *reinterpret_cast<const Elf32_Ehdr*>(raw_data(0));
}
const Elf32_Phdr& ELFImage::program_header_internal(unsigned index) const
{
ASSERT(index < header().e_phnum);
return *reinterpret_cast<const Elf32_Phdr*>(rawData(header().e_phoff + (index * sizeof(Elf32_Phdr))));
return *reinterpret_cast<const Elf32_Phdr*>(raw_data(header().e_phoff + (index * sizeof(Elf32_Phdr))));
}
const Elf32_Shdr& ELFImage::sectionHeader(unsigned index) const
const Elf32_Shdr& ELFImage::section_header(unsigned index) const
{
ASSERT(index < header().e_shnum);
return *reinterpret_cast<const Elf32_Shdr*>(rawData(header().e_shoff + (index * sizeof(Elf32_Shdr))));
return *reinterpret_cast<const Elf32_Shdr*>(raw_data(header().e_shoff + (index * sizeof(Elf32_Shdr))));
}
const ELFImage::Symbol ELFImage::symbol(unsigned index) const
{
ASSERT(index < symbolCount());
auto* rawSyms = reinterpret_cast<const Elf32_Sym*>(rawData(section(m_symbolTableSectionIndex).offset()));
ASSERT(index < symbol_count());
auto* rawSyms = reinterpret_cast<const Elf32_Sym*>(raw_data(section(m_symbol_table_section_index).offset()));
return Symbol(*this, index, rawSyms[index]);
}
const ELFImage::Section ELFImage::section(unsigned index) const
{
ASSERT(index < sectionCount());
ASSERT(index < section_count());
return Section(*this, index);
}
@ -178,8 +178,8 @@ const ELFImage::ProgramHeader ELFImage::program_header(unsigned index) const
const ELFImage::Relocation ELFImage::RelocationSection::relocation(unsigned index) const
{
ASSERT(index < relocationCount());
auto* rels = reinterpret_cast<const Elf32_Rel*>(m_image.rawData(offset()));
ASSERT(index < relocation_count());
auto* rels = reinterpret_cast<const Elf32_Rel*>(m_image.raw_data(offset()));
return Relocation(m_image, rels[index]);
}

View file

@ -11,7 +11,7 @@ public:
explicit ELFImage(ByteBuffer&&);
~ELFImage();
void dump();
bool isValid() const { return m_isValid; }
bool is_valid() const { return m_valid; }
bool parse();
class Section;
@ -30,13 +30,13 @@ public:
~Symbol() { }
const char* name() const { return m_image.tableString(m_sym.st_name); }
unsigned sectionIndex() const { return m_sym.st_shndx; }
const char* name() const { return m_image.table_string(m_sym.st_name); }
unsigned section_index() const { return m_sym.st_shndx; }
unsigned value() const { return m_sym.st_value; }
unsigned size() const { return m_sym.st_size; }
unsigned index() const { return m_index; }
unsigned type() const { return ELF32_ST_TYPE(m_sym.st_info); }
const Section section() const { return m_image.section(sectionIndex()); }
const Section section() const { return m_image.section(section_index()); }
private:
const ELFImage& m_image;
@ -73,39 +73,39 @@ public:
public:
Section(const ELFImage& image, unsigned sectionIndex)
: m_image(image)
, m_sectionHeader(image.sectionHeader(sectionIndex))
, m_sectionIndex(sectionIndex)
, m_section_header(image.section_header(sectionIndex))
, m_section_index(sectionIndex)
{
}
~Section() { }
const char* name() const { return m_image.sectionHeaderTableString(m_sectionHeader.sh_name); }
unsigned type() const { return m_sectionHeader.sh_type; }
unsigned offset() const { return m_sectionHeader.sh_offset; }
unsigned size() const { return m_sectionHeader.sh_size; }
unsigned entrySize() const { return m_sectionHeader.sh_entsize; }
unsigned entryCount() const { return size() / entrySize(); }
dword address() const { return m_sectionHeader.sh_addr; }
const char* rawData() const { return m_image.rawData(m_sectionHeader.sh_offset); }
bool isUndefined() const { return m_sectionIndex == SHN_UNDEF; }
const char* name() const { return m_image.section_header_table_string(m_section_header.sh_name); }
unsigned type() const { return m_section_header.sh_type; }
unsigned offset() const { return m_section_header.sh_offset; }
unsigned size() const { return m_section_header.sh_size; }
unsigned entry_size() const { return m_section_header.sh_entsize; }
unsigned entry_count() const { return size() / entry_size(); }
dword address() const { return m_section_header.sh_addr; }
const char* raw_data() const { return m_image.raw_data(m_section_header.sh_offset); }
bool is_undefined() const { return m_section_index == SHN_UNDEF; }
const RelocationSection relocations() const;
protected:
friend class RelocationSection;
const ELFImage& m_image;
const Elf32_Shdr& m_sectionHeader;
unsigned m_sectionIndex;
const Elf32_Shdr& m_section_header;
unsigned m_section_index;
};
class RelocationSection : public Section {
public:
RelocationSection(const Section& section)
: Section(section.m_image, section.m_sectionIndex)
: Section(section.m_image, section.m_section_index)
{
}
unsigned relocationCount() const { return entryCount(); }
unsigned relocation_count() const { return entry_count(); }
const Relocation relocation(unsigned index) const;
template<typename F> void forEachRelocation(F) const;
template<typename F> void for_each_relocation(F) const;
};
class Relocation {
@ -120,66 +120,62 @@ public:
unsigned offset() const { return m_rel.r_offset; }
unsigned type() const { return ELF32_R_TYPE(m_rel.r_info); }
unsigned symbolIndex() const { return ELF32_R_SYM(m_rel.r_info); }
const Symbol symbol() const { return m_image.symbol(symbolIndex()); }
unsigned symbol_index() const { return ELF32_R_SYM(m_rel.r_info); }
const Symbol symbol() const { return m_image.symbol(symbol_index()); }
private:
const ELFImage& m_image;
const Elf32_Rel& m_rel;
};
unsigned symbolCount() const;
unsigned sectionCount() const;
unsigned symbol_count() const;
unsigned section_count() const;
unsigned program_header_count() const;
const Symbol symbol(unsigned) const;
const Section section(unsigned) const;
const ProgramHeader program_header(unsigned const) const;
template<typename F> void forEachSection(F) const;
template<typename F> void forEachSectionOfType(unsigned, F) const;
template<typename F> void forEachSymbol(F) const;
template<typename F> void for_each_section(F) const;
template<typename F> void for_each_section_of_type(unsigned, F) const;
template<typename F> void for_each_symbol(F) const;
template<typename F> void for_each_program_header(F) const;
// NOTE: Returns section(0) if section with name is not found.
// FIXME: I don't love this API.
const Section lookupSection(const char* name) const;
bool isExecutable() const { return header().e_type == ET_EXEC; }
bool isRelocatable() const { return header().e_type == ET_REL; }
bool is_executable() const { return header().e_type == ET_EXEC; }
bool is_relocatable() const { return header().e_type == ET_REL; }
private:
bool parseHeader();
const char* rawData(unsigned offset) const;
const char* raw_data(unsigned offset) const;
const Elf32_Ehdr& header() const;
const Elf32_Shdr& sectionHeader(unsigned) const;
const Elf32_Shdr& section_header(unsigned) const;
const Elf32_Phdr& program_header_internal(unsigned) const;
const char* tableString(unsigned offset) const;
const char* sectionHeaderTableString(unsigned offset) const;
const char* sectionIndexToString(unsigned index);
const char* table_string(unsigned offset) const;
const char* section_header_table_string(unsigned offset) const;
const char* section_index_to_string(unsigned index);
#ifdef SERENITY
ByteBuffer m_buffer;
#else
MappedFile m_file;
#endif
HashMap<String, unsigned> m_sections;
bool m_isValid { false };
unsigned m_symbolTableSectionIndex { 0 };
unsigned m_stringTableSectionIndex { 0 };
bool m_valid { false };
unsigned m_symbol_table_section_index { 0 };
unsigned m_string_table_section_index { 0 };
};
template<typename F>
inline void ELFImage::forEachSection(F func) const
inline void ELFImage::for_each_section(F func) const
{
for (unsigned i = 0; i < sectionCount(); ++i)
for (unsigned i = 0; i < section_count(); ++i)
func(section(i));
}
template<typename F>
inline void ELFImage::forEachSectionOfType(unsigned type, F func) const
inline void ELFImage::for_each_section_of_type(unsigned type, F func) const
{
for (unsigned i = 0; i < sectionCount(); ++i) {
for (unsigned i = 0; i < section_count(); ++i) {
auto& section = this->section(i);
if (section.type() == type) {
if (!func(section))
@ -189,18 +185,18 @@ inline void ELFImage::forEachSectionOfType(unsigned type, F func) const
}
template<typename F>
inline void ELFImage::RelocationSection::forEachRelocation(F func) const
inline void ELFImage::RelocationSection::for_each_relocation(F func) const
{
for (unsigned i = 0; i < relocationCount(); ++i) {
for (unsigned i = 0; i < relocation_count(); ++i) {
if (!func(relocation(i)))
break;
}
}
template<typename F>
inline void ELFImage::forEachSymbol(F func) const
inline void ELFImage::for_each_symbol(F func) const
{
for (unsigned i = 0; i < symbolCount(); ++i) {
for (unsigned i = 0; i < symbol_count(); ++i) {
if (!func(symbol(i)))
break;
}
@ -209,7 +205,6 @@ inline void ELFImage::forEachSymbol(F func) const
template<typename F>
inline void ELFImage::for_each_program_header(F func) const
{
for (unsigned i = 0; i < program_header_count(); ++i) {
for (unsigned i = 0; i < program_header_count(); ++i)
func(program_header(i));
}
}

View file

@ -4,8 +4,8 @@
//#define ELFLOADER_DEBUG
ELFLoader::ELFLoader(ByteBuffer&& buffer)
: m_image(move(buffer))
{
m_image = make<ELFImage>(move(buffer));
}
ELFLoader::~ELFLoader()
@ -15,15 +15,15 @@ ELFLoader::~ELFLoader()
bool ELFLoader::load()
{
#ifdef ELFLOADER_DEBUG
m_image->dump();
m_image.dump();
#endif
if (!m_image->isValid())
if (!m_image.is_valid())
return false;
if (!layout())
return false;
exportSymbols();
if (!performRelocations())
export_symbols();
if (!perform_relocations())
return false;
return true;
@ -36,7 +36,7 @@ bool ELFLoader::layout()
#endif
bool failed = false;
m_image->for_each_program_header([&] (const ELFImage::ProgramHeader& program_header) {
m_image.for_each_program_header([&] (const ELFImage::ProgramHeader& program_header) {
if (program_header.type() != PT_LOAD)
return;
#ifdef ELFLOADER_DEBUG
@ -45,7 +45,7 @@ bool ELFLoader::layout()
allocate_section(program_header.laddr(), program_header.size_in_memory(), program_header.alignment(), program_header.is_readable(), program_header.is_writable());
});
m_image->forEachSectionOfType(SHT_PROGBITS, [this, &failed] (const ELFImage::Section& section) {
m_image.for_each_section_of_type(SHT_PROGBITS, [this, &failed] (const ELFImage::Section& section) {
#ifdef ELFLOADER_DEBUG
kprintf("ELFLoader: Copying progbits section: %s\n", section.name());
#endif
@ -58,11 +58,11 @@ bool ELFLoader::layout()
#endif
return true;
}
memcpy(ptr, section.rawData(), section.size());
memcpy(ptr, section.raw_data(), section.size());
m_sections.set(section.name(), move(ptr));
return true;
});
m_image->forEachSectionOfType(SHT_NOBITS, [this, &failed] (const ELFImage::Section& section) {
m_image.for_each_section_of_type(SHT_NOBITS, [this, &failed] (const ELFImage::Section& section) {
#ifdef ELFLOADER_DEBUG
kprintf("ELFLoader: Copying nobits section: %s\n", section.name());
#endif
@ -83,17 +83,17 @@ bool ELFLoader::layout()
void* ELFLoader::lookup(const ELFImage::Symbol& symbol)
{
if (symbol.section().isUndefined())
if (symbol.section().is_undefined())
return symbol_ptr(symbol.name());
return areaForSection(symbol.section()) + symbol.value();
return area_for_section(symbol.section()) + symbol.value();
}
char* ELFLoader::areaForSection(const ELFImage::Section& section)
char* ELFLoader::area_for_section(const ELFImage::Section& section)
{
return areaForSectionName(section.name());
return area_for_section_name(section.name());
}
char* ELFLoader::areaForSectionName(const char* name)
char* ELFLoader::area_for_section_name(const char* name)
{
if (auto it = m_sections.find(name); it != m_sections.end())
return (*it).value;
@ -101,7 +101,7 @@ char* ELFLoader::areaForSectionName(const char* name)
return nullptr;
}
bool ELFLoader::performRelocations()
bool ELFLoader::perform_relocations()
{
#ifdef ELFLOADER_DEBUG
kprintf("ELFLoader: Performing relocations\n");
@ -109,34 +109,34 @@ bool ELFLoader::performRelocations()
bool failed = false;
m_image->forEachSectionOfType(SHT_PROGBITS, [this, &failed] (const ELFImage::Section& section) -> bool {
m_image.for_each_section_of_type(SHT_PROGBITS, [this, &failed] (const ELFImage::Section& section) -> bool {
auto& relocations = section.relocations();
if (relocations.isUndefined())
if (relocations.is_undefined())
return true;
relocations.forEachRelocation([this, section, &failed] (const ELFImage::Relocation& relocation) {
relocations.for_each_relocation([this, section, &failed] (const ELFImage::Relocation& relocation) {
auto symbol = relocation.symbol();
auto& patchPtr = *reinterpret_cast<ptrdiff_t*>(areaForSection(section) + relocation.offset());
auto& patch_ptr = *reinterpret_cast<ptrdiff_t*>(area_for_section(section) + relocation.offset());
switch (relocation.type()) {
case R_386_PC32: {
char* targetPtr = (char*)lookup(symbol);
if (!targetPtr) {
char* target_ptr = (char*)lookup(symbol);
if (!target_ptr) {
kprintf("ELFLoader: unresolved symbol '%s'\n", symbol.name());
failed = true;
return false;
}
ptrdiff_t relativeOffset = (char*)targetPtr - ((char*)&patchPtr + 4);
ptrdiff_t relativeOffset = (char*)target_ptr - ((char*)&patch_ptr + 4);
#ifdef ELFLOADER_DEBUG
kprintf("ELFLoader: Relocate PC32: offset=%x, symbol=%u(%s) value=%x target=%p, offset=%d\n",
relocation.offset(),
symbol.index(),
symbol.name(),
symbol.value(),
targetPtr,
target_ptr,
relativeOffset
);
#endif
patchPtr = relativeOffset;
patch_ptr = relativeOffset;
break;
}
case R_386_32: {
@ -148,8 +148,8 @@ bool ELFLoader::performRelocations()
symbol.section().name()
);
#endif
char* targetPtr = areaForSection(symbol.section()) + symbol.value();
patchPtr += (ptrdiff_t)targetPtr;
char* target_ptr = area_for_section(symbol.section()) + symbol.value();
patch_ptr += (ptrdiff_t)target_ptr;
break;
}
default:
@ -163,18 +163,18 @@ bool ELFLoader::performRelocations()
return !failed;
}
void ELFLoader::exportSymbols()
void ELFLoader::export_symbols()
{
m_image->forEachSymbol([&] (const ELFImage::Symbol symbol) {
m_image.for_each_symbol([&] (const ELFImage::Symbol symbol) {
#ifdef ELFLOADER_DEBUG
kprintf("symbol: %u, type=%u, name=%s, section=%u\n", symbol.index(), symbol.type(), symbol.name(), symbol.sectionIndex());
#endif
if (symbol.type() == STT_FUNC) {
char* ptr;
if (m_image->isExecutable())
if (m_image.is_executable())
ptr = (char*)symbol.value();
else if (m_image->isRelocatable())
ptr = areaForSection(symbol.section()) + symbol.value();
else if (m_image.is_relocatable())
ptr = area_for_section(symbol.section()) + symbol.value();
else
ASSERT_NOT_REACHED();
add_symbol(symbol.name(), ptr, symbol.size());

View file

@ -19,11 +19,11 @@ public:
private:
bool layout();
bool performRelocations();
void exportSymbols();
bool perform_relocations();
void export_symbols();
void* lookup(const ELFImage::Symbol&);
char* areaForSection(const ELFImage::Section&);
char* areaForSectionName(const char*);
char* area_for_section(const ELFImage::Section&);
char* area_for_section_name(const char*);
struct PtrAndSize {
PtrAndSize() { }
@ -36,8 +36,9 @@ private:
char* ptr { nullptr };
unsigned size { 0 };
};
ELFImage m_image;
HashMap<String, PtrAndSize> m_symbols;
HashMap<String, char*> m_sections;
OwnPtr<ELFImage> m_image;
};

View file

@ -8,6 +8,7 @@ void __assertion_failed(const char* msg, const char* file, unsigned line, const
{
fprintf(stderr, "ASSERTION FAILED: %s\n%s:%u in %s\n", msg, file, line, func);
abort();
for (;;);
}
}