1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:28:11 +00:00

LibELF: Restore the relocation code from git history

This is going to be very useful for implementing kernel modules.
We'll also need it for dynamic linking later on.
This commit is contained in:
Andreas Kling 2019-11-28 20:53:02 +01:00
parent 0c4f29f71f
commit c10a5ac4ad
3 changed files with 94 additions and 4 deletions

View file

@ -106,14 +106,21 @@ bool ELFImage::parse()
for (unsigned i = 0; i < section_count(); ++i) {
auto& sh = section_header(i);
if (sh.sh_type == SHT_SYMTAB) {
ASSERT(!m_symbol_table_section_index);
ASSERT(!m_symbol_table_section_index || m_symbol_table_section_index == i);
m_symbol_table_section_index = i;
}
if (sh.sh_type == SHT_STRTAB && i != header().e_shstrndx) {
ASSERT(!m_string_table_section_index);
ASSERT(!m_string_table_section_index || m_string_table_section_index == i);
m_string_table_section_index = i;
}
}
// 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));
}
return true;
}
@ -173,3 +180,36 @@ const ELFImage::ProgramHeader ELFImage::program_header(unsigned index) const
ASSERT(index < program_header_count());
return ProgramHeader(*this, index);
}
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
{
// FIXME: This is ugly.
char relocation_sectionName[128];
sprintf(relocation_sectionName, ".rel%s", name());
#ifdef ELFIMAGE_DEBUG
kprintf("looking for '%s'\n", relocation_sectionName);
#endif
auto relocation_section = m_image.lookup_section(relocation_sectionName);
if (relocation_section.type() != SHT_REL)
return static_cast<const RelocationSection>(m_image.section(0));
#ifdef ELFIMAGE_DEBUG
kprintf("Found relocations for %s in %s\n", name(), relocation_section.name());
#endif
return static_cast<const RelocationSection>(relocation_section);
}
const ELFImage::Section ELFImage::lookup_section(const char* name) const
{
if (auto it = m_sections.find(name); it != m_sections.end())
return section((*it).value);
return section(0);
}