Skip to content

Commit

Permalink
LibELF: Implement support for RELA relocations
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarbeutner authored and awesomekling committed Jul 1, 2021
1 parent 1f93ffc commit f9a8c6f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
10 changes: 8 additions & 2 deletions Userland/Libraries/LibELF/DynamicLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,10 @@ DynamicLoader::RelocationResult DynamicLoader::do_relocation(const ELF::DynamicO
return RelocationResult::Failed;
}
auto symbol_address = res.value().address;
*patch_ptr += symbol_address.get();
if (relocation.addend_used())
*patch_ptr = symbol_address.get() + relocation.addend();
else
*patch_ptr += symbol_address.get();
break;
}
#ifndef __LP64__
Expand Down Expand Up @@ -466,7 +469,10 @@ DynamicLoader::RelocationResult DynamicLoader::do_relocation(const ELF::DynamicO
// FIXME: According to the spec, R_386_relative ones must be done first.
// We could explicitly do them first using m_number_of_relocations from DT_RELCOUNT
// However, our compiler is nice enough to put them at the front of the relocations for us :)
*patch_ptr += (FlatPtr)m_dynamic_object->base_address().as_ptr(); // + addend for RelA (addend for Rel is stored at addr)
if (relocation.addend_used())
*patch_ptr = (FlatPtr)m_dynamic_object->base_address().as_ptr() + relocation.addend();
else
*patch_ptr += (FlatPtr)m_dynamic_object->base_address().as_ptr();
break;
}
#ifndef __LP64__
Expand Down
14 changes: 8 additions & 6 deletions Userland/Libraries/LibELF/DynamicObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ void DynamicObject::parse()
m_plt_relocation_offset_location = entry.ptr() - (FlatPtr)m_elf_base_address.as_ptr();
break;
case DT_RELA:
m_addend_used = true;
[[fallthrough]];
case DT_REL:
m_relocation_table_offset = entry.ptr() - (FlatPtr)m_elf_base_address.as_ptr();
break;
Expand Down Expand Up @@ -189,15 +191,15 @@ DynamicObject::Relocation DynamicObject::RelocationSection::relocation(unsigned
{
VERIFY(index < entry_count());
unsigned offset_in_section = index * entry_size();
auto relocation_address = (ElfW(Rel)*)address().offset(offset_in_section).as_ptr();
return Relocation(m_dynamic, *relocation_address, offset_in_section);
auto relocation_address = (ElfW(Rela)*)address().offset(offset_in_section).as_ptr();
return Relocation(m_dynamic, *relocation_address, offset_in_section, m_addend_used);
}

DynamicObject::Relocation DynamicObject::RelocationSection::relocation_at_offset(unsigned offset) const
{
VERIFY(offset <= (m_section_size_bytes - m_entry_size));
auto relocation_address = (ElfW(Rel)*)address().offset(offset).as_ptr();
return Relocation(m_dynamic, *relocation_address, offset);
auto relocation_address = (ElfW(Rela)*)address().offset(offset).as_ptr();
return Relocation(m_dynamic, *relocation_address, offset, m_addend_used);
}

DynamicObject::Symbol DynamicObject::symbol(unsigned index) const
Expand Down Expand Up @@ -229,12 +231,12 @@ DynamicObject::Section DynamicObject::fini_array_section() const

DynamicObject::RelocationSection DynamicObject::relocation_section() const
{
return RelocationSection(Section(*this, m_relocation_table_offset, m_size_of_relocation_table, m_size_of_relocation_entry, "DT_REL"sv));
return RelocationSection(Section(*this, m_relocation_table_offset, m_size_of_relocation_table, m_size_of_relocation_entry, "DT_REL"sv), m_addend_used);
}

DynamicObject::RelocationSection DynamicObject::plt_relocation_section() const
{
return RelocationSection(Section(*this, m_plt_relocation_offset_location, m_size_of_plt_relocation_entry_list, m_size_of_relocation_entry, "DT_JMPREL"sv));
return RelocationSection(Section(*this, m_plt_relocation_offset_location, m_size_of_plt_relocation_entry_list, m_size_of_relocation_entry, "DT_JMPREL"sv), false);
}

ElfW(Half) DynamicObject::program_header_count() const
Expand Down
20 changes: 17 additions & 3 deletions Userland/Libraries/LibELF/DynamicObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ class DynamicObject : public RefCounted<DynamicObject> {

class RelocationSection : public Section {
public:
explicit RelocationSection(const Section& section)
explicit RelocationSection(const Section& section, bool addend_used)
: Section(section.m_dynamic, section.m_section_offset, section.m_section_size_bytes, section.m_entry_size, section.m_name)
, m_addend_used(addend_used)
{
}
unsigned relocation_count() const { return entry_count(); }
Expand All @@ -145,14 +146,18 @@ class DynamicObject : public RefCounted<DynamicObject> {
void for_each_relocation(F) const;
template<VoidFunction<DynamicObject::Relocation&> F>
void for_each_relocation(F func) const;

private:
const bool m_addend_used;
};

class Relocation {
public:
Relocation(const DynamicObject& dynamic, const ElfW(Rel) & rel, unsigned offset_in_section)
Relocation(const DynamicObject& dynamic, const ElfW(Rela) & rel, unsigned offset_in_section, bool addend_used)
: m_dynamic(dynamic)
, m_rel(rel)
, m_offset_in_section(offset_in_section)
, m_addend_used(addend_used)
{
}

Expand All @@ -173,6 +178,13 @@ class DynamicObject : public RefCounted<DynamicObject> {
}
unsigned symbol_index() const { return ELF64_R_SYM(m_rel.r_info); }
#endif
unsigned addend() const
{
VERIFY(m_addend_used);
return m_rel.r_addend;
}
bool addend_used() const { return m_addend_used; }

Symbol symbol() const
{
return m_dynamic.symbol(symbol_index());
Expand All @@ -186,8 +198,9 @@ class DynamicObject : public RefCounted<DynamicObject> {

private:
const DynamicObject& m_dynamic;
const ElfW(Rel) & m_rel;
const ElfW(Rela) & m_rel;
const unsigned m_offset_in_section;
const bool m_addend_used;
};

enum class HashType {
Expand Down Expand Up @@ -357,6 +370,7 @@ class DynamicObject : public RefCounted<DynamicObject> {
size_t m_number_of_relocations { 0 };
size_t m_size_of_relocation_entry { 0 };
size_t m_size_of_relocation_table { 0 };
bool m_addend_used { false };
FlatPtr m_relocation_table_offset { 0 };
bool m_is_elf_dynamic { false };

Expand Down

0 comments on commit f9a8c6f

Please sign in to comment.