mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 13:00:29 +03:00
Kernel: Remove relocation-related code since it's not used by the kernel.
If/when we need this in the future, we can bring it back. Right now it's just sitting there making the ELF code look huge when it's not.
This commit is contained in:
parent
75866438b5
commit
853597200e
Notes:
sideshowbarker
2024-07-19 14:01:46 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/853597200e3
@ -108,14 +108,6 @@ bool ELFImage::parse()
|
||||
m_string_table_section_index = i;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef SUPPORT_RELOCATIONS
|
||||
// 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));
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -175,39 +167,3 @@ const ELFImage::ProgramHeader ELFImage::program_header(unsigned index) const
|
||||
ASSERT(index < program_header_count());
|
||||
return ProgramHeader(*this, index);
|
||||
}
|
||||
|
||||
#ifdef SUPPORT_RELOCATIONS
|
||||
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];
|
||||
ksprintf(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);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -90,9 +90,6 @@ public:
|
||||
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; }
|
||||
#ifdef SUPPORT_RELOCATIONS
|
||||
const RelocationSection relocations() const;
|
||||
#endif
|
||||
dword flags() const { return m_section_header.sh_flags; }
|
||||
bool is_writable() const { return flags() & SHF_WRITE; }
|
||||
bool is_executable() const { return flags() & PF_X; }
|
||||
@ -104,39 +101,6 @@ public:
|
||||
unsigned m_section_index;
|
||||
};
|
||||
|
||||
#ifdef SUPPORT_RELOCATIONS
|
||||
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;
|
||||
};
|
||||
#endif
|
||||
|
||||
unsigned symbol_count() const;
|
||||
unsigned section_count() const;
|
||||
unsigned program_header_count() const;
|
||||
@ -150,10 +114,6 @@ public:
|
||||
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 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; }
|
||||
|
||||
@ -170,9 +130,6 @@ private:
|
||||
const char* section_index_to_string(unsigned index);
|
||||
|
||||
const byte* m_buffer { nullptr };
|
||||
#ifdef SUPPORT_RELOCATIONS
|
||||
HashMap<String, unsigned> m_sections;
|
||||
#endif
|
||||
bool m_valid { false };
|
||||
unsigned m_symbol_table_section_index { 0 };
|
||||
unsigned m_string_table_section_index { 0 };
|
||||
@ -197,17 +154,6 @@ inline void ELFImage::for_each_section_of_type(unsigned type, F func) const
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef SUPPORT_RELOCATIONS
|
||||
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)))
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
template<typename F>
|
||||
inline void ELFImage::for_each_symbol(F func) const
|
||||
{
|
||||
|
@ -3,7 +3,6 @@
|
||||
#include <AK/QuickSort.h>
|
||||
|
||||
//#define ELFLOADER_DEBUG
|
||||
//#define SUPPORT_RELOCATIONS
|
||||
|
||||
ELFLoader::ELFLoader(const byte* buffer)
|
||||
: m_image(buffer)
|
||||
@ -24,20 +23,12 @@ bool ELFLoader::load()
|
||||
|
||||
if (!layout())
|
||||
return false;
|
||||
#ifdef SUPPORT_RELOCATIONS
|
||||
if (!perform_relocations())
|
||||
return false;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ELFLoader::layout()
|
||||
{
|
||||
#ifdef ELFLOADER_DEBUG
|
||||
kprintf("ELFLoader: Layout\n");
|
||||
#endif
|
||||
|
||||
bool failed = false;
|
||||
m_image.for_each_program_header([&] (const ELFImage::ProgramHeader& program_header) {
|
||||
if (program_header.type() != PT_LOAD)
|
||||
@ -70,94 +61,6 @@ bool ELFLoader::layout()
|
||||
return !failed;
|
||||
}
|
||||
|
||||
#ifdef SUPPORT_RELOCATIONS
|
||||
void* ELFLoader::lookup(const ELFImage::Symbol& symbol)
|
||||
{
|
||||
if (symbol.section().is_undefined())
|
||||
return symbol_ptr(symbol.name());
|
||||
return area_for_section(symbol.section()) + symbol.value();
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SUPPORT_RELOCATIONS
|
||||
char* ELFLoader::area_for_section(const ELFImage::Section& section)
|
||||
{
|
||||
return area_for_section_name(section.name());
|
||||
}
|
||||
|
||||
char* ELFLoader::area_for_section_name(const char* name)
|
||||
{
|
||||
if (auto it = m_sections.find(name); it != m_sections.end())
|
||||
return (*it).value;
|
||||
ASSERT_NOT_REACHED();
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SUPPORT_RELOCATIONS
|
||||
bool ELFLoader::perform_relocations()
|
||||
{
|
||||
#ifdef ELFLOADER_DEBUG
|
||||
kprintf("ELFLoader: Performing relocations\n");
|
||||
#endif
|
||||
|
||||
bool failed = false;
|
||||
|
||||
m_image.for_each_section_of_type(SHT_PROGBITS, [this, &failed] (const ELFImage::Section& section) -> bool {
|
||||
auto& relocations = section.relocations();
|
||||
if (relocations.is_undefined())
|
||||
return true;
|
||||
relocations.for_each_relocation([this, section, &failed] (const ELFImage::Relocation& relocation) {
|
||||
auto symbol = relocation.symbol();
|
||||
auto& patch_ptr = *reinterpret_cast<ptrdiff_t*>(area_for_section(section) + relocation.offset());
|
||||
|
||||
switch (relocation.type()) {
|
||||
case R_386_PC32: {
|
||||
char* target_ptr = (char*)lookup(symbol);
|
||||
if (!target_ptr) {
|
||||
kprintf("ELFLoader: unresolved symbol '%s'\n", symbol.name());
|
||||
failed = true;
|
||||
return false;
|
||||
}
|
||||
ptrdiff_t relative_offset = (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(),
|
||||
target_ptr,
|
||||
relative_offset
|
||||
);
|
||||
#endif
|
||||
patch_ptr = relative_offset;
|
||||
break;
|
||||
}
|
||||
case R_386_32: {
|
||||
#ifdef ELFLOADER_DEBUG
|
||||
kprintf("ELFLoader: Relocate Abs32: symbol=%u(%s), value=%x, section=%s\n",
|
||||
symbol.index(),
|
||||
symbol.name(),
|
||||
symbol.value(),
|
||||
symbol.section().name()
|
||||
);
|
||||
#endif
|
||||
char* target_ptr = area_for_section(symbol.section()) + symbol.value();
|
||||
patch_ptr += (ptrdiff_t)target_ptr;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
ASSERT_NOT_REACHED();
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return !failed;
|
||||
});
|
||||
return !failed;
|
||||
}
|
||||
#endif
|
||||
|
||||
char* ELFLoader::symbol_ptr(const char* name)
|
||||
{
|
||||
char* found_ptr = nullptr;
|
||||
@ -168,10 +71,6 @@ char* ELFLoader::symbol_ptr(const char* name)
|
||||
return IterationDecision::Continue;
|
||||
if (m_image.is_executable())
|
||||
found_ptr = (char*)symbol.value();
|
||||
#ifdef SUPPORT_RELOCATIONS
|
||||
else if (m_image.is_relocatable())
|
||||
found_ptr = area_for_section(symbol.section()) + symbol.value();
|
||||
#endif
|
||||
else
|
||||
ASSERT_NOT_REACHED();
|
||||
return IterationDecision::Abort;
|
||||
|
Loading…
Reference in New Issue
Block a user