improved memory_alloc function

This commit is contained in:
georgemoralis 2023-07-04 12:29:28 +03:00
parent 9103a545a2
commit bc2facaee4
3 changed files with 36 additions and 4 deletions

View File

@ -1,4 +1,5 @@
#include "../Core/PS4/Loader/Elf.h" #include "../Core/PS4/Loader/Elf.h"
#include "Memory.h"
#ifdef _WIN64 #ifdef _WIN64
#include <windows.h> #include <windows.h>
@ -11,15 +12,33 @@
namespace Memory namespace Memory
{ {
namespace VirtualMemory { namespace VirtualMemory {
static DWORD convertMemoryMode(MemoryMode mode)
{
switch (mode)
{
case MemoryMode::Read: return PAGE_READONLY;
case MemoryMode::Write:
case MemoryMode::ReadWrite: return PAGE_READWRITE;
u64 memory_alloc(u64 address, u64 size) case MemoryMode::Execute: return PAGE_EXECUTE;
case MemoryMode::ExecuteRead: return PAGE_EXECUTE_READ;
case MemoryMode::ExecuteWrite:
case MemoryMode::ExecuteReadWrite: return PAGE_EXECUTE_READWRITE;
case MemoryMode::NoAccess: return PAGE_NOACCESS;
default:
return PAGE_NOACCESS;
}
}
u64 memory_alloc(u64 address, u64 size, MemoryMode mode)
{ {
//TODO it supports only execute_read_write 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,
static_cast<DWORD>(MEM_COMMIT) | static_cast<DWORD>(MEM_RESERVE), static_cast<DWORD>(MEM_COMMIT) | static_cast<DWORD>(MEM_RESERVE),
PAGE_EXECUTE_READWRITE)); convertMemoryMode(mode)));
if (ptr == 0) if (ptr == 0)
{ {

View File

@ -1,11 +1,24 @@
#pragma once #pragma once
#include "../types.h"
constexpr u64 SYSTEM_RESERVED = 0x800000000u; constexpr u64 SYSTEM_RESERVED = 0x800000000u;
constexpr u64 CODE_BASE_OFFSET = 0x100000000u; constexpr u64 CODE_BASE_OFFSET = 0x100000000u;
namespace Memory namespace Memory
{ {
enum class MemoryMode : u32
{
NoAccess = 0,
Read = 1,
Write = 2,
ReadWrite =3,
Execute = 4,
ExecuteRead = 5,
ExecuteWrite = 6,
ExecuteReadWrite = 7,
};
namespace VirtualMemory { namespace VirtualMemory {
u64 memory_alloc(u64 address, u64 size); u64 memory_alloc(u64 address, u64 size, MemoryMode mode);
} }
} }

View File

@ -109,7 +109,7 @@ void Linker::LoadModuleToMemory(Module* m)
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 = Memory::VirtualMemory::memory_alloc(g_load_addr, m->aligned_base_size); m->base_virtual_addr = Memory::VirtualMemory::memory_alloc(g_load_addr, m->aligned_base_size, Memory::MemoryMode::ExecuteReadWrite);
LOG_INFO_IF(debug_loader, "====Load Module to Memory ========\n"); LOG_INFO_IF(debug_loader, "====Load Module to Memory ========\n");
LOG_INFO_IF(debug_loader, "base_virtual_addr ......: {:#018x}\n", m->base_virtual_addr); LOG_INFO_IF(debug_loader, "base_virtual_addr ......: {:#018x}\n", m->base_virtual_addr);