some more work on linker and added some more needed (but not currently used) memory functions

This commit is contained in:
georgemoralis 2023-07-10 14:52:14 +03:00
parent 8f36540386
commit 42dc535638
3 changed files with 62 additions and 6 deletions

View File

@ -42,6 +42,17 @@ namespace Memory
return PAGE_NOACCESS;
}
}
static MemoryMode convertMemoryMode(u32 mode) {
switch (mode) {
case PAGE_NOACCESS: return MemoryMode::NoAccess;
case PAGE_READONLY: return MemoryMode::Read;
case PAGE_READWRITE: return MemoryMode::ReadWrite;
case PAGE_EXECUTE: return MemoryMode::Execute;
case PAGE_EXECUTE_READ: return MemoryMode::ExecuteRead;
case PAGE_EXECUTE_READWRITE: return MemoryMode::ExecuteReadWrite;
default: return MemoryMode::NoAccess;
}
}
u64 memory_alloc(u64 address, u64 size, MemoryMode mode)
{
@ -71,5 +82,46 @@ namespace Memory
#endif
return ptr;
}
bool memory_protect(u64 address, u64 size, MemoryMode mode, MemoryMode* old_mode) {
DWORD old_protect = 0;
if (VirtualProtect(reinterpret_cast<LPVOID>(static_cast<uintptr_t>(address)), size, convertMemoryMode(mode), &old_protect) == 0) {
auto err = static_cast<u32>(GetLastError());
LOG_ERROR_IF(true, "VirtualProtect() failed: 0x{:X}\n", err);
return false;
}
if (old_mode != nullptr) {
*old_mode = convertMemoryMode(old_protect);
}
return true;
}
bool memory_flush(u64 address, u64 size) {
if (::FlushInstructionCache(GetCurrentProcess(), reinterpret_cast<LPVOID>(static_cast<uintptr_t>(address)), size) == 0) {
auto err = static_cast<u32>(GetLastError());
LOG_ERROR_IF(true, "FlushInstructionCache() failed: 0x{:X}\n", err);
return false;
}
return true;
}
bool memory_patch(u64 vaddr, u64 value) {
MemoryMode old_mode{};
memory_protect(vaddr, 8, MemoryMode::ReadWrite, &old_mode);
auto* ptr = reinterpret_cast<uint64_t*>(vaddr);
bool ret = (*ptr != value);
*ptr = value;
memory_protect(vaddr, 8, old_mode, nullptr);
//if mode is executable flush it so insure that cpu finds it
if ((old_mode == MemoryMode::Execute || old_mode == MemoryMode::ExecuteRead || old_mode == MemoryMode::ExecuteWrite ||
old_mode == MemoryMode::ExecuteReadWrite)) {
memory_flush(vaddr, 8);
}
return ret;
}
}
}

View File

@ -20,5 +20,8 @@ namespace Memory
namespace VirtualMemory {
u64 memory_alloc(u64 address, u64 size, MemoryMode mode);
bool memory_protect(u64 address, u64 size, MemoryMode mode, MemoryMode* old_mode);
bool memory_flush(u64 address, u64 size);
bool memory_patch(u64 vaddr, u64 value);
}
}

View File

@ -511,6 +511,7 @@ static void relocate(u32 idx, elf_relocation* rel, Module* m, bool isJmpRel)
bool rel_isResolved = false;
u08 rel_sym_type = 0;
std::string rel_name;
u08 rel_bind_type = -1;//-1 means it didn't resolve
switch (type)
{
@ -544,12 +545,12 @@ static void relocate(u32 idx, elf_relocation* rel, Module* m, bool isJmpRel)
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);
}
rel_bind_type = STB_GLOBAL;
rel_name = namesTlb + sym.st_name;
if (type == R_X86_64_JUMP_SLOT) {
addend = 0;
}
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;
default:
LOG_INFO_IF(debug_loader, "UNK bind {}\n", sym_bind);