more work on relocations for libc loading

This commit is contained in:
georgemoralis 2023-12-31 19:47:38 +02:00
parent 7dfdf4c259
commit 62b2e57b6b
3 changed files with 359 additions and 437 deletions

View File

@ -1,11 +1,13 @@
#include <fmt/core.h> #include "core/linker.h"
#include <Zydis/Zydis.h> #include <Zydis/Zydis.h>
#include <fmt/core.h>
#include "common/log.h" #include "common/log.h"
#include "common/string_util.h" #include "common/string_util.h"
#include "core/aerolib/aerolib.h" #include "core/aerolib/aerolib.h"
#include "core/aerolib/stubs.h" #include "core/aerolib/stubs.h"
#include "core/hle/libraries/libkernel/thread_management.h" #include "core/hle/libraries/libkernel/thread_management.h"
#include "core/linker.h"
#include "core/virtual_memory.h" #include "core/virtual_memory.h"
namespace Core { namespace Core {
@ -14,21 +16,16 @@ constexpr bool debug_loader = true;
static u64 g_load_addr = SYSTEM_RESERVED + CODE_BASE_OFFSET; static u64 g_load_addr = SYSTEM_RESERVED + CODE_BASE_OFFSET;
static u64 get_aligned_size(const elf_program_header& phdr) static u64 get_aligned_size(const elf_program_header& phdr) {
{
return (phdr.p_align != 0 ? (phdr.p_memsz + (phdr.p_align - 1)) & ~(phdr.p_align - 1) : phdr.p_memsz); return (phdr.p_align != 0 ? (phdr.p_memsz + (phdr.p_align - 1)) & ~(phdr.p_align - 1) : phdr.p_memsz);
} }
static u64 calculate_base_size(const elf_header& ehdr, std::span<const elf_program_header> phdr) static u64 calculate_base_size(const elf_header& ehdr, std::span<const elf_program_header> phdr) {
{
u64 base_size = 0; u64 base_size = 0;
for (u16 i = 0; i < ehdr.e_phnum; i++) for (u16 i = 0; i < ehdr.e_phnum; i++) {
{ if (phdr[i].p_memsz != 0 && (phdr[i].p_type == PT_LOAD || phdr[i].p_type == PT_SCE_RELRO)) {
if (phdr[i].p_memsz != 0 && (phdr[i].p_type == PT_LOAD || phdr[i].p_type == PT_SCE_RELRO))
{
u64 last_addr = phdr[i].p_vaddr + get_aligned_size(phdr[i]); u64 last_addr = phdr[i].p_vaddr + get_aligned_size(phdr[i]);
if (last_addr > base_size) if (last_addr > base_size) {
{
base_size = last_addr; base_size = last_addr;
} }
} }
@ -36,23 +33,16 @@ static u64 calculate_base_size(const elf_header& ehdr, std::span<const elf_progr
return base_size; return base_size;
} }
static std::string encodeId(u64 nVal) static std::string encodeId(u64 nVal) {
{
std::string enc; std::string enc;
const char pCodes[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-"; const char pCodes[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
if (nVal < 0x40u) if (nVal < 0x40u) {
{
enc += pCodes[nVal]; enc += pCodes[nVal];
} } else {
else if (nVal < 0x1000u) {
{
if (nVal < 0x1000u)
{
enc += pCodes[static_cast<u16>(nVal >> 6u) & 0x3fu]; enc += pCodes[static_cast<u16>(nVal >> 6u) & 0x3fu];
enc += pCodes[nVal & 0x3fu]; enc += pCodes[nVal & 0x3fu];
} } else {
else
{
enc += pCodes[static_cast<u16>(nVal >> 12u) & 0x3fu]; enc += pCodes[static_cast<u16>(nVal >> 12u) & 0x3fu];
enc += pCodes[static_cast<u16>(nVal >> 6u) & 0x3fu]; enc += pCodes[static_cast<u16>(nVal >> 6u) & 0x3fu];
enc += pCodes[nVal & 0x3fu]; enc += pCodes[nVal & 0x3fu];
@ -65,8 +55,7 @@ Linker::Linker() = default;
Linker::~Linker() = default; Linker::~Linker() = default;
Module* Linker::LoadModule(const std::string& elf_name) Module* Linker::LoadModule(const std::string& elf_name) {
{
std::scoped_lock lock{m_mutex}; std::scoped_lock lock{m_mutex};
auto& m = m_modules.emplace_back(); auto& m = m_modules.emplace_back();
@ -86,23 +75,14 @@ Module* Linker::LoadModule(const std::string& elf_name)
return &m; return &m;
} }
Module* Linker::FindModule(/*u32 id*/) void Linker::LoadModuleToMemory(Module* m) {
{
// TODO atm we only have 1 module so we don't need to iterate on vector
if (m_modules.empty()) [[unlikely]] {
return nullptr;
}
return &m_modules[0];
}
void Linker::LoadModuleToMemory(Module* m)
{
// get elf header, program header // get elf header, program header
const auto elf_header = m->elf.GetElfHeader(); const auto elf_header = m->elf.GetElfHeader();
const auto elf_pheader = m->elf.GetProgramHeader(); const auto elf_pheader = m->elf.GetProgramHeader();
u64 base_size = calculate_base_size(elf_header, elf_pheader); u64 base_size = calculate_base_size(elf_header, elf_pheader);
m->aligned_base_size = (base_size & ~(static_cast<u64>(0x1000) - 1)) + 0x1000;//align base size to 0x1000 block size (TODO is that the default block size or it can be changed? m->aligned_base_size = (base_size & ~(static_cast<u64>(0x1000) - 1)) +
0x1000; // align base size to 0x1000 block size (TODO is that the default block size or it can be changed?
m->base_virtual_addr = VirtualMemory::memory_alloc(g_load_addr, m->aligned_base_size, VirtualMemory::MemoryMode::ExecuteReadWrite); m->base_virtual_addr = VirtualMemory::memory_alloc(g_load_addr, m->aligned_base_size, VirtualMemory::MemoryMode::ExecuteReadWrite);
@ -111,14 +91,11 @@ void Linker::LoadModuleToMemory(Module* m)
LOG_INFO_IF(debug_loader, "base_size ..............: {:#018x}\n", base_size); LOG_INFO_IF(debug_loader, "base_size ..............: {:#018x}\n", base_size);
LOG_INFO_IF(debug_loader, "aligned_base_size ......: {:#018x}\n", m->aligned_base_size); LOG_INFO_IF(debug_loader, "aligned_base_size ......: {:#018x}\n", m->aligned_base_size);
for (u16 i = 0; i < elf_header.e_phnum; i++) for (u16 i = 0; i < elf_header.e_phnum; i++) {
{ switch (elf_pheader[i].p_type) {
switch (elf_pheader[i].p_type)
{
case PT_LOAD: case PT_LOAD:
case PT_SCE_RELRO: case PT_SCE_RELRO:
if (elf_pheader[i].p_memsz != 0) if (elf_pheader[i].p_memsz != 0) {
{
u64 segment_addr = elf_pheader[i].p_vaddr + m->base_virtual_addr; u64 segment_addr = elf_pheader[i].p_vaddr + m->base_virtual_addr;
u64 segment_file_size = elf_pheader[i].p_filesz; u64 segment_file_size = elf_pheader[i].p_filesz;
u64 segment_memory_size = get_aligned_size(elf_pheader[i]); u64 segment_memory_size = get_aligned_size(elf_pheader[i]);
@ -130,36 +107,27 @@ void Linker::LoadModuleToMemory(Module* m)
LOG_INFO_IF(debug_loader, "segment_mode ..........: {}\n", segment_mode); LOG_INFO_IF(debug_loader, "segment_mode ..........: {}\n", segment_mode);
m->elf.LoadSegment(segment_addr, elf_pheader[i].p_offset, segment_file_size); m->elf.LoadSegment(segment_addr, elf_pheader[i].p_offset, segment_file_size);
} } else {
else
{
LOG_ERROR_IF(debug_loader, "p_memsz==0 in type {}\n", m->elf.ElfPheaderTypeStr(elf_pheader[i].p_type)); LOG_ERROR_IF(debug_loader, "p_memsz==0 in type {}\n", m->elf.ElfPheaderTypeStr(elf_pheader[i].p_type));
} }
break; break;
case PT_DYNAMIC: case PT_DYNAMIC:
if (elf_pheader[i].p_filesz != 0) if (elf_pheader[i].p_filesz != 0) {
{
m->m_dynamic.resize(elf_pheader[i].p_filesz); m->m_dynamic.resize(elf_pheader[i].p_filesz);
m->elf.LoadSegment(reinterpret_cast<u64>(m->m_dynamic.data()), elf_pheader[i].p_offset, elf_pheader[i].p_filesz); m->elf.LoadSegment(reinterpret_cast<u64>(m->m_dynamic.data()), elf_pheader[i].p_offset, elf_pheader[i].p_filesz);
} } else {
else
{
LOG_ERROR_IF(debug_loader, "p_filesz==0 in type {}\n", m->elf.ElfPheaderTypeStr(elf_pheader[i].p_type)); LOG_ERROR_IF(debug_loader, "p_filesz==0 in type {}\n", m->elf.ElfPheaderTypeStr(elf_pheader[i].p_type));
} }
break; break;
case PT_SCE_DYNLIBDATA: case PT_SCE_DYNLIBDATA:
if (elf_pheader[i].p_filesz != 0) if (elf_pheader[i].p_filesz != 0) {
{
m->m_dynamic_data.resize(elf_pheader[i].p_filesz); m->m_dynamic_data.resize(elf_pheader[i].p_filesz);
m->elf.LoadSegment(reinterpret_cast<u64>(m->m_dynamic_data.data()), elf_pheader[i].p_offset, elf_pheader[i].p_filesz); m->elf.LoadSegment(reinterpret_cast<u64>(m->m_dynamic_data.data()), elf_pheader[i].p_offset, elf_pheader[i].p_filesz);
} } else {
else
{
LOG_ERROR_IF(debug_loader, "p_filesz==0 in type {}\n", m->elf.ElfPheaderTypeStr(elf_pheader[i].p_type)); LOG_ERROR_IF(debug_loader, "p_filesz==0 in type {}\n", m->elf.ElfPheaderTypeStr(elf_pheader[i].p_type));
} }
break; break;
default: default: LOG_ERROR_IF(debug_loader, "Unimplemented type {}\n", m->elf.ElfPheaderTypeStr(elf_pheader[i].p_type));
LOG_ERROR_IF(debug_loader, "Unimplemented type {}\n", m->elf.ElfPheaderTypeStr(elf_pheader[i].p_type));
} }
} }
LOG_INFO_IF(debug_loader, "program entry addr ..........: {:#018x}\n", m->elf.GetElfEntry() + m->base_virtual_addr); LOG_INFO_IF(debug_loader, "program entry addr ..........: {:#018x}\n", m->elf.GetElfEntry() + m->base_virtual_addr);
@ -175,20 +143,16 @@ void Linker::LoadModuleToMemory(Module* m)
/* runtime_address: */ runtime_address, /* runtime_address: */ runtime_address,
/* buffer: */ rt1 + offset, /* buffer: */ rt1 + offset,
/* length: */ sizeof(rt1) - offset, /* length: */ sizeof(rt1) - offset,
/* instruction: */ &instruction /* instruction: */ &instruction))) {
))) {
fmt::print("{:#x}" PRIX64 " {}\n", runtime_address, instruction.text); fmt::print("{:#x}" PRIX64 " {}\n", runtime_address, instruction.text);
offset += instruction.info.length; offset += instruction.info.length;
runtime_address += instruction.info.length; runtime_address += instruction.info.length;
} }
} }
void Linker::LoadDynamicInfo(Module* m) void Linker::LoadDynamicInfo(Module* m) {
{ for (const auto* dyn = reinterpret_cast<elf_dynamic*>(m->m_dynamic.data()); dyn->d_tag != DT_NULL; dyn++) {
for (const auto* dyn = reinterpret_cast<elf_dynamic*>(m->m_dynamic.data()); dyn->d_tag != DT_NULL; dyn++) switch (dyn->d_tag) {
{
switch (dyn->d_tag)
{
case DT_SCE_HASH: // Offset of the hash table. case DT_SCE_HASH: // Offset of the hash table.
m->dynamic_info.hash_table = reinterpret_cast<void*>(m->m_dynamic_data.data() + dyn->d_un.d_ptr); m->dynamic_info.hash_table = reinterpret_cast<void*>(m->m_dynamic_data.data() + dyn->d_un.d_ptr);
break; break;
@ -207,12 +171,8 @@ void Linker::LoadDynamicInfo(Module* m)
case DT_SCE_SYMTABSZ: // Size of the symbol table. case DT_SCE_SYMTABSZ: // Size of the symbol table.
m->dynamic_info.symbol_table_total_size = dyn->d_un.d_val; m->dynamic_info.symbol_table_total_size = dyn->d_un.d_val;
break; break;
case DT_INIT: case DT_INIT: m->dynamic_info.init_virtual_addr = dyn->d_un.d_ptr; break;
m->dynamic_info.init_virtual_addr = dyn->d_un.d_ptr; case DT_FINI: m->dynamic_info.fini_virtual_addr = dyn->d_un.d_ptr; break;
break;
case DT_FINI:
m->dynamic_info.fini_virtual_addr = dyn->d_un.d_ptr;
break;
case DT_SCE_PLTGOT: // Offset of the global offset table. case DT_SCE_PLTGOT: // Offset of the global offset table.
m->dynamic_info.pltgot_virtual_addr = dyn->d_un.d_ptr; m->dynamic_info.pltgot_virtual_addr = dyn->d_un.d_ptr;
break; break;
@ -224,8 +184,7 @@ void Linker::LoadDynamicInfo(Module* m)
break; break;
case DT_SCE_PLTREL: // The type of relocations in the relocation table. Should be DT_RELA case DT_SCE_PLTREL: // The type of relocations in the relocation table. Should be DT_RELA
m->dynamic_info.jmp_relocation_type = dyn->d_un.d_val; m->dynamic_info.jmp_relocation_type = dyn->d_un.d_val;
if (m->dynamic_info.jmp_relocation_type != DT_RELA) if (m->dynamic_info.jmp_relocation_type != DT_RELA) {
{
LOG_WARN_IF(debug_loader, "DT_SCE_PLTREL is NOT DT_RELA should check!"); LOG_WARN_IF(debug_loader, "DT_SCE_PLTREL is NOT DT_RELA should check!");
} }
break; break;
@ -267,47 +226,37 @@ void Linker::LoadDynamicInfo(Module* m)
LOG_WARN_IF(debug_loader, "DT_SCE_SYMENT is NOT 0x18 should check!"); LOG_WARN_IF(debug_loader, "DT_SCE_SYMENT is NOT 0x18 should check!");
} }
break; break;
case DT_DEBUG: case DT_DEBUG: m->dynamic_info.debug = dyn->d_un.d_val; break;
m->dynamic_info.debug = dyn->d_un.d_val; case DT_TEXTREL: m->dynamic_info.textrel = dyn->d_un.d_val; break;
break;
case DT_TEXTREL:
m->dynamic_info.textrel = dyn->d_un.d_val;
break;
case DT_FLAGS: case DT_FLAGS:
m->dynamic_info.flags = dyn->d_un.d_val; m->dynamic_info.flags = dyn->d_un.d_val;
if (m->dynamic_info.flags != 0x04) // this value should always be DF_TEXTREL (0x04) if (m->dynamic_info.flags != 0x04) // this value should always be DF_TEXTREL (0x04)
{ {
LOG_WARN_IF(debug_loader, "DT_FLAGS is NOT 0x04 should check!"); LOG_WARN_IF(debug_loader, "DT_FLAGS is NOT 0x04 should check!\n");
} }
break; break;
case DT_NEEDED: // Offset of the library string in the string table to be linked in. case DT_NEEDED: // Offset of the library string in the string table to be linked in.
if (m->dynamic_info.str_table != nullptr) // in theory this should already be filled from about just make a test case if (m->dynamic_info.str_table != nullptr) // in theory this should already be filled from about just make a test case
{ {
m->dynamic_info.needed.push_back(m->dynamic_info.str_table + dyn->d_un.d_val); m->dynamic_info.needed.push_back(m->dynamic_info.str_table + dyn->d_un.d_val);
} } else {
else
{
LOG_ERROR_IF(debug_loader, "DT_NEEDED str table is not loaded should check!"); LOG_ERROR_IF(debug_loader, "DT_NEEDED str table is not loaded should check!");
} }
break; break;
case DT_SCE_NEEDED_MODULE: case DT_SCE_NEEDED_MODULE: {
{
ModuleInfo info{}; ModuleInfo info{};
info.value = dyn->d_un.d_val; info.value = dyn->d_un.d_val;
info.name = m->dynamic_info.str_table + info.name_offset; info.name = m->dynamic_info.str_table + info.name_offset;
info.enc_id = encodeId(info.id); info.enc_id = encodeId(info.id);
m->dynamic_info.import_modules.push_back(info); m->dynamic_info.import_modules.push_back(info);
} } break;
break; case DT_SCE_IMPORT_LIB: {
case DT_SCE_IMPORT_LIB:
{
LibraryInfo info{}; LibraryInfo info{};
info.value = dyn->d_un.d_val; info.value = dyn->d_un.d_val;
info.name = m->dynamic_info.str_table + info.name_offset; info.name = m->dynamic_info.str_table + info.name_offset;
info.enc_id = encodeId(info.id); info.enc_id = encodeId(info.id);
m->dynamic_info.import_libs.push_back(info); m->dynamic_info.import_libs.push_back(info);
} } break;
break;
case DT_SCE_FINGERPRINT: case DT_SCE_FINGERPRINT:
// The fingerprint is a 24 byte (0x18) size buffer that contains a unique identifier for the given app. // The fingerprint is a 24 byte (0x18) size buffer that contains a unique identifier for the given app.
// How exactly this is generated isn't known, however it is not necessary to have a valid fingerprint. // How exactly this is generated isn't known, however it is not necessary to have a valid fingerprint.
@ -318,9 +267,7 @@ void Linker::LoadDynamicInfo(Module* m)
// The upper 32-bits should contain the module index multiplied by 0x10000. The lower 32-bits should be a constant 0x9. // The upper 32-bits should contain the module index multiplied by 0x10000. The lower 32-bits should be a constant 0x9.
LOG_INFO_IF(debug_loader, "unsupported DT_SCE_IMPORT_LIB_ATTR value = ..........: {:#018x}\n", dyn->d_un.d_val); LOG_INFO_IF(debug_loader, "unsupported DT_SCE_IMPORT_LIB_ATTR value = ..........: {:#018x}\n", dyn->d_un.d_val);
break; break;
case DT_SCE_ORIGINAL_FILENAME: case DT_SCE_ORIGINAL_FILENAME: m->dynamic_info.filename = m->dynamic_info.str_table + dyn->d_un.d_val; break;
m->dynamic_info.filename = m->dynamic_info.str_table + dyn->d_un.d_val;
break;
case DT_SCE_MODULE_INFO: // probably only useable in shared modules case DT_SCE_MODULE_INFO: // probably only useable in shared modules
{ {
ModuleInfo info{}; ModuleInfo info{};
@ -328,46 +275,36 @@ void Linker::LoadDynamicInfo(Module* m)
info.name = m->dynamic_info.str_table + info.name_offset; info.name = m->dynamic_info.str_table + info.name_offset;
info.enc_id = encodeId(info.id); info.enc_id = encodeId(info.id);
m->dynamic_info.export_modules.push_back(info); m->dynamic_info.export_modules.push_back(info);
} } break;
break;
case DT_SCE_MODULE_ATTR: case DT_SCE_MODULE_ATTR:
// TODO? // TODO?
LOG_INFO_IF(debug_loader, "unsupported DT_SCE_MODULE_ATTR value = ..........: {:#018x}\n", dyn->d_un.d_val); LOG_INFO_IF(debug_loader, "unsupported DT_SCE_MODULE_ATTR value = ..........: {:#018x}\n", dyn->d_un.d_val);
break; break;
case DT_SCE_EXPORT_LIB: case DT_SCE_EXPORT_LIB: {
{
LibraryInfo info{}; LibraryInfo info{};
info.value = dyn->d_un.d_val; info.value = dyn->d_un.d_val;
info.name = m->dynamic_info.str_table + info.name_offset; info.name = m->dynamic_info.str_table + info.name_offset;
info.enc_id = encodeId(info.id); info.enc_id = encodeId(info.id);
m->dynamic_info.export_libs.push_back(info); m->dynamic_info.export_libs.push_back(info);
} break;
default: LOG_INFO_IF(debug_loader, "unsupported dynamic tag ..........: {:#018x}\n", dyn->d_tag);
} }
break;
default:
LOG_INFO_IF(debug_loader, "unsupported dynamic tag ..........: {:#018x}\n", dyn->d_tag);
}
} }
} }
const ModuleInfo* Linker::FindModule(const Module& m, const std::string& id) const ModuleInfo* Linker::FindModule(const Module& m, const std::string& id) {
{
const auto& import_modules = m.dynamic_info.import_modules; const auto& import_modules = m.dynamic_info.import_modules;
int index = 0; int index = 0;
for (const auto& mod : import_modules) for (const auto& mod : import_modules) {
{ if (mod.enc_id.compare(id) == 0) {
if (mod.enc_id.compare(id) == 0)
{
return &import_modules.at(index); return &import_modules.at(index);
} }
index++; index++;
} }
const auto& export_modules = m.dynamic_info.export_modules; const auto& export_modules = m.dynamic_info.export_modules;
index = 0; index = 0;
for (const auto& mod : export_modules) for (const auto& mod : export_modules) {
{ if (mod.enc_id.compare(id) == 0) {
if (mod.enc_id.compare(id) == 0)
{
return &export_modules.at(index); return &export_modules.at(index);
} }
index++; index++;
@ -375,24 +312,19 @@ const ModuleInfo* Linker::FindModule(const Module& m, const std::string& id)
return nullptr; return nullptr;
} }
const LibraryInfo* Linker::FindLibrary(const Module& m, const std::string& id) const LibraryInfo* Linker::FindLibrary(const Module& m, const std::string& id) {
{
const auto& import_libs = m.dynamic_info.import_libs; const auto& import_libs = m.dynamic_info.import_libs;
int index = 0; int index = 0;
for (const auto& lib : import_libs) for (const auto& lib : import_libs) {
{ if (lib.enc_id.compare(id) == 0) {
if (lib.enc_id.compare(id) == 0)
{
return &import_libs.at(index); return &import_libs.at(index);
} }
index++; index++;
} }
const auto& export_libs = m.dynamic_info.export_libs; const auto& export_libs = m.dynamic_info.export_libs;
index = 0; index = 0;
for (const auto& lib : export_libs) for (const auto& lib : export_libs) {
{ if (lib.enc_id.compare(id) == 0) {
if (lib.enc_id.compare(id) == 0)
{
return &export_libs.at(index); return &export_libs.at(index);
} }
index++; index++;
@ -400,18 +332,15 @@ const LibraryInfo* Linker::FindLibrary(const Module& m, const std::string& id)
return nullptr; return nullptr;
} }
void Linker::LoadSymbols(Module* m) void Linker::LoadSymbols(Module* m) {
{ if (m->dynamic_info.symbol_table == nullptr || m->dynamic_info.str_table == nullptr || m->dynamic_info.symbol_table_total_size == 0) {
if (m->dynamic_info.symbol_table == nullptr || m->dynamic_info.str_table == nullptr || m->dynamic_info.symbol_table_total_size==0)
{
LOG_INFO_IF(debug_loader, "Symbol table not found!\n"); LOG_INFO_IF(debug_loader, "Symbol table not found!\n");
return; return;
} }
for (auto* sym = m->dynamic_info.symbol_table; for (auto* sym = m->dynamic_info.symbol_table;
reinterpret_cast<uint8_t*>(sym) < reinterpret_cast<uint8_t*>(m->dynamic_info.symbol_table) + m->dynamic_info.symbol_table_total_size; reinterpret_cast<uint8_t*>(sym) < reinterpret_cast<uint8_t*>(m->dynamic_info.symbol_table) + m->dynamic_info.symbol_table_total_size;
sym++) sym++) {
{
std::string id = std::string(m->dynamic_info.str_table + sym->st_name); std::string id = std::string(m->dynamic_info.str_table + sym->st_name);
const auto ids = Common::SplitString(id, '#'); const auto ids = Common::SplitString(id, '#');
if (ids.size() == 3) // symbols are 3 parts name , library , module if (ids.size() == 3) // symbols are 3 parts name , library , module
@ -421,33 +350,20 @@ void Linker::LoadSymbols(Module* m)
auto bind = sym->GetBind(); auto bind = sym->GetBind();
auto type = sym->GetType(); auto type = sym->GetType();
auto visibility = sym->GetVisibility(); auto visibility = sym->GetVisibility();
if (library != nullptr || module != nullptr) if (library != nullptr || module != nullptr) {
{ switch (bind) {
switch (bind)
{
case STB_GLOBAL: case STB_GLOBAL:
case STB_WEAK: case STB_WEAK: break;
break; default: LOG_INFO_IF(debug_loader, "Unsupported bind {} for name symbol {} \n", bind, ids.at(0)); break;
default:
LOG_INFO_IF(debug_loader, "Unsupported bind {} for name symbol {} \n", bind,ids.at(0));
continue;
} }
switch (type) switch (type) {
{
case STT_OBJECT: case STT_OBJECT:
case STT_FUN: case STT_FUN: break;
break; default: LOG_INFO_IF(debug_loader, "Unsupported type {} for name symbol {} \n", type, ids.at(0)); break;
default:
LOG_INFO_IF(debug_loader, "Unsupported type {} for name symbol {} \n", type, ids.at(0));
continue;
} }
switch (visibility) switch (visibility) {
{ case STV_DEFAULT: break;
case STV_DEFAULT: default: LOG_INFO_IF(debug_loader, "Unsupported visibility {} for name symbol {} \n", visibility, ids.at(0)); break;
break;
default:
LOG_INFO_IF(debug_loader, "Unsupported visibility {} for name symbol {} \n", visibility, ids.at(0));
continue;
} }
// if st_value!=0 then it's export symbol // if st_value!=0 then it's export symbol
bool is_sym_export = sym->st_value != 0; bool is_sym_export = sym->st_value != 0;
@ -455,12 +371,9 @@ void Linker::LoadSymbols(Module* m)
auto aeronid = AeroLib::FindByNid(ids.at(0).c_str()); auto aeronid = AeroLib::FindByNid(ids.at(0).c_str());
if (aeronid != nullptr) if (aeronid != nullptr) {
{
nidName = aeronid->name; nidName = aeronid->name;
} } else {
else
{
nidName = "UNK"; nidName = "UNK";
} }
@ -474,17 +387,14 @@ void Linker::LoadSymbols(Module* m)
sym_r.module_version_minor = module->version_minor; sym_r.module_version_minor = module->version_minor;
sym_r.type = type; sym_r.type = type;
if (is_sym_export) if (is_sym_export) {
{
m->export_sym.AddSymbol(sym_r, sym->st_value + m->base_virtual_addr); m->export_sym.AddSymbol(sym_r, sym->st_value + m->base_virtual_addr);
} } else {
else
{
m->import_sym.AddSymbol(sym_r, 0); m->import_sym.AddSymbol(sym_r, 0);
} }
LOG_INFO_IF(debug_loader, "name {} function {} library {} module {} bind {} type {} visibility {}\n", ids.at(0), nidName,
LOG_INFO_IF(debug_loader, "name {} function {} library {} module {} bind {} type {} visibility {}\n", ids.at(0),nidName,library->name, module->name, bind, type, visibility); library->name, module->name, bind, type, visibility);
} }
} }
} }
@ -501,6 +411,7 @@ static void relocate(u32 idx, elf_relocation* rel, Module* m, bool isJmpRel) {
u64 rel_virtual_addr = m->base_virtual_addr + rel->rel_offset; u64 rel_virtual_addr = m->base_virtual_addr + rel->rel_offset;
bool rel_isResolved = false; bool rel_isResolved = false;
u08 rel_sym_type = 0; u08 rel_sym_type = 0;
u08 rel_sym_bind = 0;
std::string rel_name; std::string rel_name;
switch (type) { switch (type) {
@ -546,32 +457,51 @@ static void relocate(u32 idx, elf_relocation* rel, Module* m, bool isJmpRel) {
LOG_INFO_IF(debug_loader, "R_X86_64_64-R_X86_64_JUMP_SLOT sym_type {} bind STB_GLOBAL symbol : {:#010x}\n", sym_type, symbol); LOG_INFO_IF(debug_loader, "R_X86_64_64-R_X86_64_JUMP_SLOT sym_type {} bind STB_GLOBAL symbol : {:#010x}\n", sym_type, symbol);
} }
break; break;
case STB_WEAK:
rel_name = namesTlb + sym.st_name;
m->linker->Resolve(rel_name, rel_sym_type, m, &symrec);
symbol_vitrual_addr = symrec.virtual_address;
rel_isResolved = (symbol_vitrual_addr != 0);
rel_name = symrec.name;
if (type == R_X86_64_JUMP_SLOT) {
addend = 0;
}
rel_value = (rel_isResolved ? symbol_vitrual_addr + addend : 0);
if (!rel_isResolved) {
LOG_INFO_IF(debug_loader, "R_X86_64_64-R_X86_64_JUMP_SLOT sym_type {} bind STB_WEAK symbol : {:#010x}\n", sym_type, symbol);
}
break;
default: LOG_INFO_IF(debug_loader, "UNK bind {}\n", sym_bind); default: LOG_INFO_IF(debug_loader, "UNK bind {}\n", sym_bind);
} }
} break; } break;
case R_X86_64_DTPMOD64:
rel_value = reinterpret_cast<u64>(m);
rel_isResolved = true;
rel_sym_type = STT_TLS;
rel_sym_bind = STB_LOCAL;
break;
default: LOG_INFO_IF(debug_loader, "UNK type {:#010x} rel symbol : {:#010x}\n", type, symbol); default: LOG_INFO_IF(debug_loader, "UNK type {:#010x} rel symbol : {:#010x}\n", type, symbol);
} }
if (rel_isResolved) { if (rel_isResolved) {
VirtualMemory::memory_patch(rel_virtual_addr, rel_value); VirtualMemory::memory_patch(rel_virtual_addr, rel_value);
} } else {
else
{
LOG_INFO_IF(debug_loader, "function not patched! {}\n", rel_name); LOG_INFO_IF(debug_loader, "function not patched! {}\n", rel_name);
} }
} }
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++) {
relocate(idx, rel, m, false); relocate(idx, rel, m, false);
} }
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++) {
relocate(idx, rel, m, true); relocate(idx, rel, m, true);
} }
} }
@ -620,7 +550,6 @@ void Linker::Resolve(const std::string& name, int Symtype, Module* m, Loader::Sy
if (rec == nullptr) { if (rec == nullptr) {
if (auto* p = FindModuleExp(*module, *library); p != nullptr && p->export_sym.GetSize() > 0) { if (auto* p = FindModuleExp(*module, *library); p != nullptr && p->export_sym.GetSize() > 0) {
rec = p->export_sym.FindSymbol(sr); rec = p->export_sym.FindSymbol(sr);
} }
} }
if (rec != nullptr) { if (rec != nullptr) {
@ -634,27 +563,21 @@ void Linker::Resolve(const std::string& name, int Symtype, Module* m, Loader::Sy
return_info->virtual_address = AeroLib::GetStub(sr.name.c_str()); return_info->virtual_address = AeroLib::GetStub(sr.name.c_str());
return_info->name = "Unknown !!!"; return_info->name = "Unknown !!!";
} }
LOG_ERROR_IF(debug_loader, "Linker: Stub resolved {} as {} (lib: {}, mod: {}) \n", sr.name, return_info->name, library->name, module->name); LOG_ERROR_IF(debug_loader, "Linker: Stub resolved {} as {} (lib: {}, mod: {}) \n", sr.name, return_info->name, library->name,
module->name);
} }
} } else {
else
{
__debugbreak(); // den tha prepei na ftasoume edo __debugbreak(); // den tha prepei na ftasoume edo
} }
} } else {
else
{
__debugbreak(); // oute edo mallon __debugbreak(); // oute edo mallon
} }
} }
using exit_func_t = PS4_SYSV_ABI void (*)(); using exit_func_t = PS4_SYSV_ABI void (*)();
using entry_func_t = PS4_SYSV_ABI void (*)(EntryParams* params, exit_func_t atexit_func); using entry_func_t = PS4_SYSV_ABI void (*)(EntryParams* params, exit_func_t atexit_func);
static PS4_SYSV_ABI void ProgramExitFunc() { static PS4_SYSV_ABI void ProgramExitFunc() { fmt::print("exit function called\n"); }
fmt::print("exit function called\n");
}
static void run_main_entry(u64 addr, EntryParams* params, exit_func_t exit_func) { static void run_main_entry(u64 addr, EntryParams* params, exit_func_t exit_func) {
// reinterpret_cast<entry_func_t>(addr)(params, exit_func); // can't be used, stack has to have a specific layout // reinterpret_cast<entry_func_t>(addr)(params, exit_func); // can't be used, stack has to have a specific layout
@ -676,8 +599,7 @@ static void run_main_entry(u64 addr, EntryParams* params, exit_func_t exit_func)
// there's no coming back // there's no coming back
: :
: "r"(addr), "r"(params), "r"(exit_func) : "r"(addr), "r"(params), "r"(exit_func)
: "rax", "rsi", "rdi", "rsp", "rbp" : "rax", "rsi", "rdi", "rsp", "rbp");
);
} }
void Linker::Execute() { void Linker::Execute() {

View File

@ -115,7 +115,6 @@ public:
virtual ~Linker(); virtual ~Linker();
Module* LoadModule(const std::string& elf_name); Module* LoadModule(const std::string& elf_name);
Module* FindModule(/*u32 id*/);
void LoadModuleToMemory(Module* m); void LoadModuleToMemory(Module* m);
void LoadDynamicInfo(Module* m); void LoadDynamicInfo(Module* m);
void LoadSymbols(Module* m); void LoadSymbols(Module* m);

View File

@ -449,6 +449,7 @@ struct elf_relocation
constexpr u32 R_X86_64_64 = 1; // Direct 64 bit 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_JUMP_SLOT = 7; // Create PLT entry
constexpr u32 R_X86_64_RELATIVE = 8; // Adjust by program base constexpr u32 R_X86_64_RELATIVE = 8; // Adjust by program base
constexpr u32 R_X86_64_DTPMOD64 = 16;
namespace Core::Loader { namespace Core::Loader {