mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 22:57:44 +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:
parent
0c4f29f71f
commit
c10a5ac4ad
3 changed files with 94 additions and 4 deletions
|
@ -1,8 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/String.h>
|
||||
#include <Kernel/VM/VirtualAddress.h>
|
||||
#include <LibELF/exec_elf.h>
|
||||
|
||||
|
@ -92,6 +92,7 @@ public:
|
|||
u32 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;
|
||||
u32 flags() const { return m_section_header.sh_flags; }
|
||||
bool is_writable() const { return flags() & SHF_WRITE; }
|
||||
bool is_executable() const { return flags() & PF_X; }
|
||||
|
@ -103,6 +104,38 @@ public:
|
|||
unsigned m_section_index;
|
||||
};
|
||||
|
||||
class RelocationSection : public Section {
|
||||
public:
|
||||
RelocationSection(const Section& section)
|
||||
: Section(section.m_image, section.m_section_index)
|
||||
{
|
||||
}
|
||||
unsigned relocation_count() const { return entry_count(); }
|
||||
const Relocation relocation(unsigned index) const;
|
||||
template<typename F>
|
||||
void for_each_relocation(F) const;
|
||||
};
|
||||
|
||||
class Relocation {
|
||||
public:
|
||||
Relocation(const ELFImage& image, const Elf32_Rel& rel)
|
||||
: m_image(image)
|
||||
, m_rel(rel)
|
||||
{
|
||||
}
|
||||
|
||||
~Relocation() {}
|
||||
|
||||
unsigned offset() const { return m_rel.r_offset; }
|
||||
unsigned type() const { return ELF32_R_TYPE(m_rel.r_info); }
|
||||
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 symbol_count() const;
|
||||
unsigned section_count() const;
|
||||
unsigned program_header_count() const;
|
||||
|
@ -120,6 +153,10 @@ public:
|
|||
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 lookup_section(const char* name) const;
|
||||
|
||||
bool is_executable() const { return header().e_type == ET_EXEC; }
|
||||
bool is_relocatable() const { return header().e_type == ET_REL; }
|
||||
|
||||
|
@ -136,6 +173,7 @@ private:
|
|||
const char* section_index_to_string(unsigned index) const;
|
||||
|
||||
const u8* m_buffer { nullptr };
|
||||
HashMap<String, unsigned> m_sections;
|
||||
bool m_valid { false };
|
||||
unsigned m_symbol_table_section_index { 0 };
|
||||
unsigned m_string_table_section_index { 0 };
|
||||
|
@ -154,12 +192,21 @@ inline void ELFImage::for_each_section_of_type(unsigned type, F func) const
|
|||
for (unsigned i = 0; i < section_count(); ++i) {
|
||||
auto& section = this->section(i);
|
||||
if (section.type() == type) {
|
||||
if (!func(section))
|
||||
if (func(section) == IterationDecision::Break)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
inline void ELFImage::RelocationSection::for_each_relocation(F func) const
|
||||
{
|
||||
for (unsigned i = 0; i < relocation_count(); ++i) {
|
||||
if (func(relocation(i)) == IterationDecision::Break)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
inline void ELFImage::for_each_symbol(F func) const
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue