progress in relocations

This commit is contained in:
georgemoralis 2023-07-06 21:55:41 +03:00
parent 6717482662
commit 0edc265dc2
3 changed files with 69 additions and 11 deletions

View File

@ -33,7 +33,6 @@ namespace Memory
u64 memory_alloc(u64 address, u64 size, MemoryMode mode) u64 memory_alloc(u64 address, u64 size, MemoryMode mode)
{ {
//TODO it supports only execute_read_write mode
#ifdef _WIN64 #ifdef _WIN64
auto ptr = reinterpret_cast<uintptr_t>(VirtualAlloc(reinterpret_cast<LPVOID>(static_cast<uintptr_t>(address)), auto ptr = reinterpret_cast<uintptr_t>(VirtualAlloc(reinterpret_cast<LPVOID>(static_cast<uintptr_t>(address)),
size, size,

View File

@ -497,24 +497,80 @@ void Linker::LoadSymbols(Module* m)
} }
} }
} }
static void relocate(u32 idx, elf_relocation* rel, Module* m, bool isJmpRel)
{
auto type = rel->GetType();
auto symbol = rel->GetSymbol();
auto addend = rel->rel_addend;
auto* symbolsTlb = m->dynamic_info->symbol_table;
auto* namesTlb = m->dynamic_info->str_table;
u64 rel_value = 0;
u64 rel_base_virtual_addr = m->base_virtual_addr;
u64 rel_virtual_addr = m->base_virtual_addr + rel->rel_offset;
bool rel_isResolved = false;
u08 rel_sym_type = 0;
std::string rel_name;
switch (type)
{
case R_X86_64_RELATIVE:
if (symbol != 0)//should be always zero
{
LOG_INFO_IF(debug_loader, "R_X86_64_RELATIVE symbol not zero = {:#010x}\n", type, symbol);
}
rel_value = rel_base_virtual_addr + addend;
rel_isResolved = true;
break;
case R_X86_64_64:
case R_X86_64_JUMP_SLOT://similar but addend is not take into account
{
auto sym = symbolsTlb[symbol];
auto sym_bind = sym.GetBind();
auto sym_type = sym.GetType();
auto sym_visibility = sym.GetVisibility();
u64 symbol_vitrual_addr = 0;
switch (sym_type)
{
case STT_FUN: rel_sym_type = 2; break;
case STT_OBJECT: rel_sym_type = 1; break;
default:
LOG_INFO_IF(debug_loader, "unknown symbol type {}\n",sym_type);
}
if (sym_visibility != 0)//should be zero log if else
{
LOG_INFO_IF(debug_loader, "symbol visilibity !=0");
}
switch (sym_bind)
{
case STB_GLOBAL:
if (type == R_X86_64_64) {
LOG_INFO_IF(debug_loader, "R_X86_64_64 sym_type {} bind STB_GLOBAL symbol : {:#010x}\n", sym_type,symbol);
}
if (type == R_X86_64_JUMP_SLOT) {
LOG_INFO_IF(debug_loader, "R_X86_64_JUMP_SLOT sym_type {} bind STB_GLOBAL symbol : {:#010x}\n", sym_type,symbol);
}
break;
default:
LOG_INFO_IF(debug_loader, "UNK bind {}\n", sym_bind);
}
}
break;
default:
LOG_INFO_IF(debug_loader, "UNK type {:#010x} rel symbol : {:#010x}\n", type, symbol);
}
}
void Linker::Relocate(Module* m) void Linker::Relocate(Module* m)
{ {
u32 idx = 0; u32 idx = 0;
for (auto* rel = m->dynamic_info->relocation_table; reinterpret_cast<u08*>(rel) < reinterpret_cast<u08*>(m->dynamic_info->relocation_table) + m->dynamic_info->relocation_table_size; rel++, idx++) for (auto* rel = m->dynamic_info->relocation_table; reinterpret_cast<u08*>(rel) < reinterpret_cast<u08*>(m->dynamic_info->relocation_table) + m->dynamic_info->relocation_table_size; rel++, idx++)
{ {
auto type = rel->GetType(); relocate(idx, rel, m, false);
auto symbol = rel->GetSymbol();
auto addend = rel->rel_addend;
LOG_INFO_IF(debug_loader, "rel type {:#010x} rel symbol : {:#010x}\n", type, symbol);
} }
idx = 0; idx = 0;
for (auto* rel = m->dynamic_info->jmp_relocation_table; reinterpret_cast<u08*>(rel) < reinterpret_cast<u08*>(m->dynamic_info->jmp_relocation_table) + m->dynamic_info->jmp_relocation_table_size; rel++, idx++) for (auto* rel = m->dynamic_info->jmp_relocation_table; reinterpret_cast<u08*>(rel) < reinterpret_cast<u08*>(m->dynamic_info->jmp_relocation_table) + m->dynamic_info->jmp_relocation_table_size; rel++, idx++)
{ {
auto type = rel->GetType(); relocate(idx, rel, m, true);
auto symbol = rel->GetSymbol();
auto addend = rel->rel_addend;
LOG_INFO_IF(debug_loader, "jmprel type {:#010x} rel symbol : {:#010x}\n", type, symbol);
} }
} }

View File

@ -442,6 +442,9 @@ struct elf_relocation
u64 rel_info; u64 rel_info;
s64 rel_addend; s64 rel_addend;
}; };
constexpr u32 R_X86_64_64 = 1; // Direct 64 bit
constexpr u32 R_X86_64_JUMP_SLOT = 7; // Create PLT entry
constexpr u32 R_X86_64_RELATIVE = 8; // Adjust by program base
class Elf class Elf
{ {