1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 03:37:34 +00:00

LibELF: Make SymbolLookupResult::address a VirtualAddress

Let's use a stronger type than void* for this since we're talking
specifically about a virtual address and not necessarily a pointer
to something actually in memory (yet).
This commit is contained in:
Andreas Kling 2021-02-20 23:59:17 +01:00
parent c5d93e55d0
commit a43910acc3
4 changed files with 28 additions and 32 deletions

View file

@ -158,20 +158,20 @@ static void initialize_libc(DynamicObject& libc)
// because it uses getenv() internally, so `environ` has to be initialized before we call `__libc_init`.
auto res = libc.lookup_symbol("environ");
ASSERT(res.has_value());
*((char***)res.value().address) = g_envp;
*((char***)res.value().address.as_ptr()) = g_envp;
res = libc.lookup_symbol("__environ_is_malloced");
ASSERT(res.has_value());
*((bool*)res.value().address) = false;
*((bool*)res.value().address.as_ptr()) = false;
res = libc.lookup_symbol("exit");
ASSERT(res.has_value());
g_libc_exit = (LibCExitFunction)res.value().address;
g_libc_exit = (LibCExitFunction)res.value().address.as_ptr();
res = libc.lookup_symbol("__libc_init");
ASSERT(res.has_value());
typedef void libc_init_func();
((libc_init_func*)res.value().address)();
((libc_init_func*)res.value().address.as_ptr())();
}
template<typename Callback>

View file

@ -420,18 +420,18 @@ DynamicLoader::RelocationResult DynamicLoader::do_relocation(size_t total_tls_si
dbgln("ERROR: symbol not found: {}.", symbol.name());
ASSERT_NOT_REACHED();
}
u32 symbol_address = res.value().address;
*patch_ptr += symbol_address;
auto symbol_address = res.value().address;
*patch_ptr += symbol_address.get();
dbgln_if(DYNAMIC_LOAD_DEBUG, " Symbol address: {:p}", *patch_ptr);
break;
}
case R_386_PC32: {
auto symbol = relocation.symbol();
dbgln_if(DYNAMIC_LOAD_DEBUG, "PC-relative relocation: '{}', value: {:p}", symbol.name(), symbol.value());
auto res = lookup_symbol(symbol);
ASSERT(res.has_value());
u32 relative_offset = (res.value().address - (FlatPtr)(m_dynamic_object->base_address().as_ptr() + relocation.offset()));
*patch_ptr += relative_offset;
auto result = lookup_symbol(symbol);
ASSERT(result.has_value());
auto relative_offset = result.value().address - m_dynamic_object->base_address().offset(relocation.offset());
*patch_ptr += relative_offset.get();
dbgln_if(DYNAMIC_LOAD_DEBUG, " Symbol address: {:p}", *patch_ptr);
break;
}
@ -455,9 +455,9 @@ DynamicLoader::RelocationResult DynamicLoader::do_relocation(size_t total_tls_si
dbgln_if(DYNAMIC_LOAD_DEBUG, "symbol found, location: {:#08x}", res.value().address);
dbgln_if(DYNAMIC_LOAD_DEBUG, "object: {}", m_filename);
u32 symbol_location = res.value().address;
ASSERT(symbol_location != (FlatPtr)m_dynamic_object->base_address().as_ptr());
*patch_ptr = symbol_location;
auto symbol_location = res.value().address;
ASSERT(symbol_location != m_dynamic_object->base_address());
*patch_ptr = symbol_location.get();
dbgln_if(DYNAMIC_LOAD_DEBUG, " Symbol address: {:p}", *patch_ptr);
break;
}
@ -500,7 +500,7 @@ DynamicLoader::RelocationResult DynamicLoader::do_relocation(size_t total_tls_si
// Eagerly BIND_NOW the PLT entries, doing all the symbol looking goodness
// The patch method returns the address for the LAZY fixup path, but we don't need it here
dbgln_if(DYNAMIC_LOAD_DEBUG, "patching plt reloaction: {:p}", relocation.offset_in_section());
[[maybe_unused]] auto rc = m_dynamic_object->patch_plt_entry(relocation.offset_in_section());
m_dynamic_object->patch_plt_entry(relocation.offset_in_section());
} else {
u8* relocation_address = relocation.address().as_ptr();
@ -536,10 +536,10 @@ void DynamicLoader::setup_plt_trampoline()
// Called from our ASM routine _plt_trampoline.
// Tell the compiler that it might be called from other places:
extern "C" Elf32_Addr _fixup_plt_entry(DynamicObject* object, u32 relocation_offset);
extern "C" Elf32_Addr _fixup_plt_entry(DynamicObject* object, u32 relocation_offset)
extern "C" FlatPtr _fixup_plt_entry(DynamicObject* object, u32 relocation_offset);
extern "C" FlatPtr _fixup_plt_entry(DynamicObject* object, u32 relocation_offset)
{
return object->patch_plt_entry(relocation_offset);
return object->patch_plt_entry(relocation_offset).get();
}
void DynamicLoader::call_object_init_functions()

View file

@ -466,7 +466,7 @@ Optional<DynamicObject::SymbolLookupResult> DynamicObject::lookup_symbol(const S
auto symbol = result.value();
if (symbol.is_undefined())
return {};
return SymbolLookupResult { symbol.value(), symbol.address().get(), symbol.bind(), this };
return SymbolLookupResult { symbol.value(), symbol.address(), symbol.bind(), this };
}
NonnullRefPtr<DynamicObject> DynamicObject::create(VirtualAddress base_address, VirtualAddress dynamic_section_address)
@ -475,27 +475,23 @@ NonnullRefPtr<DynamicObject> DynamicObject::create(VirtualAddress base_address,
}
// offset is in PLT relocation table
Elf32_Addr DynamicObject::patch_plt_entry(u32 relocation_offset)
VirtualAddress DynamicObject::patch_plt_entry(u32 relocation_offset)
{
auto relocation = plt_relocation_section().relocation_at_offset(relocation_offset);
ASSERT(relocation.type() == R_386_JMP_SLOT);
auto sym = relocation.symbol();
auto symbol = relocation.symbol();
u8* relocation_address = relocation.address().as_ptr();
auto result = lookup_symbol(sym);
auto result = lookup_symbol(symbol);
if (!result.has_value()) {
dbgln("did not find symbol: {}", sym.name());
dbgln("did not find symbol: {}", symbol.name());
ASSERT_NOT_REACHED();
}
u32 symbol_location = result.value().address;
auto symbol_location = result.value().address;
dbgln_if(DYNAMIC_LOAD_DEBUG, "DynamicLoader: Jump slot relocation: putting {} ({}) into PLT at {}", symbol.name(), symbol_location, (void*)relocation_address);
dbgln_if(DYNAMIC_LOAD_DEBUG, "DynamicLoader: Jump slot relocation: putting {} ({:p}) into PLT at {}", sym.name(), symbol_location, (void*)relocation_address);
*(u32*)relocation_address = symbol_location;
*(FlatPtr*)relocation_address = symbol_location.get();
return symbol_location;
}
@ -508,7 +504,7 @@ Optional<DynamicObject::SymbolLookupResult> DynamicObject::lookup_symbol(const E
if (!symbol.is_undefined()) {
dbgln_if(DYNAMIC_LOAD_DEBUG, "symbol is defined in its object");
return SymbolLookupResult { symbol.value(), (FlatPtr)symbol.address().as_ptr(), symbol.bind(), &symbol.object() };
return SymbolLookupResult { symbol.value(), symbol.address(), symbol.bind(), &symbol.object() };
}
return DynamicLinker::lookup_global_symbol(symbol.name());
}

View file

@ -255,14 +255,14 @@ public:
struct SymbolLookupResult {
FlatPtr value { 0 };
FlatPtr address { 0 };
VirtualAddress address;
unsigned bind { STB_LOCAL };
const ELF::DynamicObject* dynamic_object { nullptr }; // The object in which the symbol is defined
};
Optional<SymbolLookupResult> lookup_symbol(const StringView& name) const;
// Will be called from _fixup_plt_entry, as part of the PLT trampoline
Elf32_Addr patch_plt_entry(u32 relocation_offset);
VirtualAddress patch_plt_entry(u32 relocation_offset);
Optional<SymbolLookupResult> lookup_symbol(const ELF::DynamicObject::Symbol&) const;