mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:57:43 +00:00
LibELF: Convert dbgprintf() in DynamicLoader class to dbgln()
This commit is contained in:
parent
9a0762b203
commit
e9fd81b964
1 changed files with 31 additions and 45 deletions
|
@ -37,14 +37,6 @@
|
|||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#ifdef DYNAMIC_LOAD_VERBOSE
|
||||
# define VERBOSE(fmt, ...) dbgprintf(fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
# define VERBOSE(fmt, ...) \
|
||||
do { \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
#ifndef __serenity__
|
||||
static void* mmap_with_name(void* addr, size_t length, int prot, int flags, int fd, off_t offset, const char*)
|
||||
{
|
||||
|
@ -144,14 +136,10 @@ RefPtr<DynamicObject> DynamicLoader::load_from_image(unsigned flags, size_t tota
|
|||
m_valid = m_elf_image.is_valid();
|
||||
|
||||
if (!m_valid) {
|
||||
dbgprintf("DynamicLoader::load_from_image failed: image is invalid\n");
|
||||
dbgln("DynamicLoader::load_from_image failed: image is invalid");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
#ifdef DYNAMIC_LOAD_VERBOSE
|
||||
// m_image->dump();
|
||||
#endif
|
||||
|
||||
load_program_headers();
|
||||
|
||||
m_dynamic_object = DynamicObject::construct(m_text_segment_load_address, m_dynamic_section_address);
|
||||
|
@ -160,7 +148,7 @@ RefPtr<DynamicObject> DynamicLoader::load_from_image(unsigned flags, size_t tota
|
|||
|
||||
auto rc = load_stage_2(flags, total_tls_size);
|
||||
if (!rc) {
|
||||
dbgprintf("DynamicLoader::load_from_image failed at load_stage_2\n");
|
||||
dbgln("DynamicLoader::load_from_image failed at load_stage_2");
|
||||
return nullptr;
|
||||
}
|
||||
return m_dynamic_object;
|
||||
|
@ -232,7 +220,7 @@ RefPtr<DynamicObject> DynamicLoader::load_stage_3(unsigned flags, size_t total_t
|
|||
|
||||
call_object_init_functions();
|
||||
|
||||
VERBOSE("Loaded %s\n", m_filename.characters());
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("Loaded {}", m_filename);
|
||||
return m_dynamic_object;
|
||||
}
|
||||
|
||||
|
@ -325,26 +313,25 @@ void DynamicLoader::load_program_headers()
|
|||
|
||||
DynamicLoader::RelocationResult DynamicLoader::do_relocation(size_t total_tls_size, ELF::DynamicObject::Relocation relocation)
|
||||
{
|
||||
VERBOSE("Relocation symbol: %s, type: %d\n", relocation.symbol().name(), relocation.type());
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("Relocation symbol: {}, type: {}", relocation.symbol().name(), relocation.type());
|
||||
FlatPtr* patch_ptr = nullptr;
|
||||
if (is_dynamic())
|
||||
patch_ptr = (FlatPtr*)(m_dynamic_object->base_address().as_ptr() + relocation.offset());
|
||||
else
|
||||
patch_ptr = (FlatPtr*)(FlatPtr)relocation.offset();
|
||||
|
||||
// VERBOSE("dynamic object name: %s\n", dynamic_object.object_name());
|
||||
VERBOSE("dynamic object base address: %p\n", m_dynamic_object->base_address());
|
||||
VERBOSE("relocation offset: 0x%x\n", relocation.offset());
|
||||
VERBOSE("patch_ptr: %p\n", patch_ptr);
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("dynamic object base address: {:p}", m_dynamic_object->base_address().as_ptr());
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("relocation offset: {:#08x}", relocation.offset());
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("patch_ptr: {:p}", patch_ptr);
|
||||
switch (relocation.type()) {
|
||||
case R_386_NONE:
|
||||
// Apparently most loaders will just skip these?
|
||||
// Seems if the 'link editor' generates one something is funky with your code
|
||||
VERBOSE("None relocation. No symbol, no nothing.\n");
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("None relocation. No symbol, no nothing.");
|
||||
break;
|
||||
case R_386_32: {
|
||||
auto symbol = relocation.symbol();
|
||||
VERBOSE("Absolute relocation: name: '%s', value: %p\n", symbol.name(), symbol.value());
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("Absolute relocation: name: '{}', value: {}", symbol.name(), symbol.value());
|
||||
auto res = lookup_symbol(symbol);
|
||||
if (!res.found) {
|
||||
if (symbol.bind() == STB_WEAK) {
|
||||
|
@ -355,22 +342,22 @@ DynamicLoader::RelocationResult DynamicLoader::do_relocation(size_t total_tls_si
|
|||
}
|
||||
u32 symbol_address = res.address;
|
||||
*patch_ptr += symbol_address;
|
||||
VERBOSE(" Symbol address: %p\n", *patch_ptr);
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>(" Symbol address: {:p}", *patch_ptr);
|
||||
break;
|
||||
}
|
||||
case R_386_PC32: {
|
||||
auto symbol = relocation.symbol();
|
||||
VERBOSE("PC-relative relocation: '%s', value: %p\n", symbol.name(), symbol.value());
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("PC-relative relocation: '{}', value: {:p}", symbol.name(), symbol.value());
|
||||
auto res = lookup_symbol(symbol);
|
||||
ASSERT(res.found);
|
||||
u32 relative_offset = (res.address - (FlatPtr)(m_dynamic_object->base_address().as_ptr() + relocation.offset()));
|
||||
*patch_ptr += relative_offset;
|
||||
VERBOSE(" Symbol address: %p\n", *patch_ptr);
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>(" Symbol address: {:p}", *patch_ptr);
|
||||
break;
|
||||
}
|
||||
case R_386_GLOB_DAT: {
|
||||
auto symbol = relocation.symbol();
|
||||
VERBOSE("Global data relocation: '%s', value: %p\n", symbol.name(), symbol.value());
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("Global data relocation: '{}', value: {:p}", symbol.name(), symbol.value());
|
||||
auto res = lookup_symbol(symbol);
|
||||
if (!res.found) {
|
||||
// We do not support these
|
||||
|
@ -388,48 +375,47 @@ DynamicLoader::RelocationResult DynamicLoader::do_relocation(size_t total_tls_si
|
|||
// Symbol not found
|
||||
return RelocationResult::Failed;
|
||||
}
|
||||
VERBOSE("was symbol found? %d, address: 0x%x\n", res.found, res.address);
|
||||
VERBOSE("object: %s\n", m_filename.characters());
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("was symbol found? {}, address: {:#08x}", res.found, res.address);
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("object: {}", m_filename);
|
||||
|
||||
u32 symbol_location = res.address;
|
||||
ASSERT(symbol_location != (FlatPtr)m_dynamic_object->base_address().as_ptr());
|
||||
*patch_ptr = symbol_location;
|
||||
VERBOSE(" Symbol address: %p\n", *patch_ptr);
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>(" Symbol address: {:p}", *patch_ptr);
|
||||
break;
|
||||
}
|
||||
case R_386_RELATIVE: {
|
||||
// FIXME: According to the spec, R_386_relative ones must be done first.
|
||||
// We could explicitly do them first using m_number_of_relocatoins from DT_RELCOUNT
|
||||
// However, our compiler is nice enough to put them at the front of the relocations for us :)
|
||||
VERBOSE("Load address relocation at offset %X\n", relocation.offset());
|
||||
VERBOSE(" patch ptr == %p, adding load base address (%p) to it and storing %p\n", *patch_ptr, m_dynamic_object->base_address().as_ptr(), *patch_ptr + m_dynamic_object->base_address().as_ptr());
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("Load address relocation at offset {:#08x}", relocation.offset());
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>(" patch ptr == %p, adding load base address ({:p}) to it and storing {:p}", *patch_ptr, m_dynamic_object->base_address().as_ptr(), *patch_ptr + m_dynamic_object->base_address().as_ptr());
|
||||
*patch_ptr += (FlatPtr)m_dynamic_object->base_address().as_ptr(); // + addend for RelA (addend for Rel is stored at addr)
|
||||
break;
|
||||
}
|
||||
case R_386_TLS_TPOFF32:
|
||||
case R_386_TLS_TPOFF: {
|
||||
VERBOSE("Relocation type: R_386_TLS_TPOFF at offset %X\n", relocation.offset());
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("Relocation type: R_386_TLS_TPOFF at offset {:#08x}", relocation.offset());
|
||||
auto symbol = relocation.symbol();
|
||||
// For some reason, LibC has a R_386_TLS_TPOFF that refers to the undefined symbol.. huh
|
||||
if (relocation.symbol_index() == 0)
|
||||
break;
|
||||
VERBOSE("Symbol index: %d\n", symbol.index());
|
||||
VERBOSE("Symbol is_undefined?: %d\n", symbol.is_undefined());
|
||||
VERBOSE("TLS relocation: '%s', value: %p\n", symbol.name(), symbol.value());
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("Symbol index: {}", symbol.index());
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("Symbol is_undefined?: {}", symbol.is_undefined());
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("TLS relocation: '{}', value: {:p}", symbol.name(), symbol.value());
|
||||
auto res = lookup_symbol(symbol);
|
||||
if (!res.found)
|
||||
break;
|
||||
ASSERT(res.found);
|
||||
u32 symbol_value = res.value;
|
||||
VERBOSE("symbol value: %d\n", symbol_value);
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("symbol value: {}", symbol_value);
|
||||
const auto dynamic_object_of_symbol = res.dynamic_object;
|
||||
ASSERT(dynamic_object_of_symbol);
|
||||
size_t offset_of_tls_end = dynamic_object_of_symbol->tls_offset().value() + dynamic_object_of_symbol->tls_size().value();
|
||||
// size_t offset_of_tls_end = tls_offset() + tls_size();
|
||||
VERBOSE("patch ptr: 0x%x\n", patch_ptr);
|
||||
VERBOSE("tls end offset: %d, total tls size: %d\n", offset_of_tls_end, total_tls_size);
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("patch ptr: {:p}", patch_ptr);
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("tls end offset: {}, total tls size: {}", offset_of_tls_end, total_tls_size);
|
||||
*patch_ptr = (offset_of_tls_end - total_tls_size - symbol_value - sizeof(Elf32_Addr));
|
||||
VERBOSE("*patch ptr: %d\n", (i32)*patch_ptr);
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("*patch ptr: {}", (i32)*patch_ptr);
|
||||
break;
|
||||
}
|
||||
case R_386_JMP_SLOT: {
|
||||
|
@ -437,7 +423,7 @@ DynamicLoader::RelocationResult DynamicLoader::do_relocation(size_t total_tls_si
|
|||
if (m_dynamic_object->must_bind_now() || s_always_bind_now) {
|
||||
// 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
|
||||
VERBOSE("patching plt reloaction: 0x%x\n", relocation.offset_in_section());
|
||||
dbgln<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());
|
||||
} else {
|
||||
u8* relocation_address = relocation.address().as_ptr();
|
||||
|
@ -449,7 +435,7 @@ DynamicLoader::RelocationResult DynamicLoader::do_relocation(size_t total_tls_si
|
|||
}
|
||||
default:
|
||||
// Raise the alarm! Someone needs to implement this relocation type
|
||||
VERBOSE("Found a new exciting relocation type %d\n", relocation.type());
|
||||
dbgln("Found a new exciting relocation type {}", relocation.type());
|
||||
// printf("DynamicLoader: Found unknown relocation type %d\n", relocation.type());
|
||||
ASSERT_NOT_REACHED();
|
||||
break;
|
||||
|
@ -469,7 +455,7 @@ void DynamicLoader::setup_plt_trampoline()
|
|||
got_ptr[1] = (FlatPtr)m_dynamic_object.ptr();
|
||||
got_ptr[2] = (FlatPtr)&_plt_trampoline;
|
||||
|
||||
VERBOSE("Set GOT PLT entries at %p: [0] = %p [1] = %p, [2] = %p\n", got_ptr, (void*)got_ptr[0], (void*)got_ptr[1], (void*)got_ptr[2]);
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("Set GOT PLT entries at {:p}: [0] = {:p} [1] = {:p}, [2] = {:p}", got_ptr, (void*)got_ptr[0], (void*)got_ptr[1], (void*)got_ptr[2]);
|
||||
}
|
||||
|
||||
// Called from our ASM routine _plt_trampoline.
|
||||
|
@ -487,7 +473,7 @@ void DynamicLoader::call_object_init_functions()
|
|||
if (m_dynamic_object->has_init_section()) {
|
||||
auto init_function = (InitFunc)(m_dynamic_object->init_section().address().as_ptr());
|
||||
|
||||
VERBOSE("Calling DT_INIT at %p\n", init_function);
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("Calling DT_INIT at {:p}", init_function);
|
||||
(init_function)();
|
||||
}
|
||||
|
||||
|
@ -501,7 +487,7 @@ void DynamicLoader::call_object_init_functions()
|
|||
// 0 definitely shows up. Apparently 0/-1 are valid? Confusing.
|
||||
if (!*init_begin || ((FlatPtr)*init_begin == (FlatPtr)-1))
|
||||
continue;
|
||||
VERBOSE("Calling DT_INITARRAY entry at %p\n", *init_begin);
|
||||
dbgln<DYNAMIC_LOAD_DEBUG>("Calling DT_INITARRAY entry at {:p}", *init_begin);
|
||||
(*init_begin)();
|
||||
++init_begin;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue