add Linux support

This commit is contained in:
InoriRus
2022-10-03 15:33:23 +10:00
parent e427645422
commit 37b020e9ba
69 changed files with 2716 additions and 971 deletions
@@ -12,6 +12,10 @@
#ifdef KYTY_EMU_ENABLED
namespace Kyty::Core::VirtualMemory {
class ExceptionHandler;
} // namespace Kyty::Core::VirtualMemory
namespace Kyty::Loader {
class Elf64;
@@ -19,10 +23,6 @@ struct Elf64_Sym;
struct Elf64_Rela;
class RuntimeLinker;
namespace VirtualMemory {
class ExceptionHandler;
} // namespace VirtualMemory
using module_func_t = int (*)(size_t args, const void* argp);
struct ModuleId
@@ -103,23 +103,23 @@ struct DynamicInfo
struct Program
{
int32_t unique_id = -1;
RuntimeLinker* rt = nullptr;
String file_name;
Elf64* elf = nullptr;
VirtualMemory::ExceptionHandler* exception_handler = nullptr;
DynamicInfo* dynamic_info = nullptr;
uint64_t base_vaddr = 0;
uint64_t base_size = 0;
uint64_t base_size_aligned = 0;
SymbolDatabase* export_symbols = nullptr;
SymbolDatabase* import_symbols = nullptr;
ThreadLocalStorage tls;
bool fail_if_global_not_resolved = true;
bool dbg_print_reloc = false;
uint64_t proc_param_vaddr = 0;
uint64_t custom_call_plt_vaddr = 0;
uint32_t custom_call_plt_num = 0;
int32_t unique_id = -1;
RuntimeLinker* rt = nullptr;
String file_name;
Elf64* elf = nullptr;
Core::VirtualMemory::ExceptionHandler* exception_handler = nullptr;
DynamicInfo* dynamic_info = nullptr;
uint64_t base_vaddr = 0;
uint64_t base_size = 0;
uint64_t base_size_aligned = 0;
SymbolDatabase* export_symbols = nullptr;
SymbolDatabase* import_symbols = nullptr;
ThreadLocalStorage tls;
bool fail_if_global_not_resolved = true;
bool dbg_print_reloc = false;
uint64_t proc_param_vaddr = 0;
uint64_t custom_call_plt_vaddr = 0;
uint32_t custom_call_plt_num = 0;
};
class RuntimeLinker
@@ -1,117 +0,0 @@
#ifndef EMULATOR_INCLUDE_EMULATOR_LOADER_VIRTUALMEMORY_H_
#define EMULATOR_INCLUDE_EMULATOR_LOADER_VIRTUALMEMORY_H_
#include "Kyty/Core/Common.h"
#include "Kyty/Core/String.h"
#include "Emulator/Common.h"
#ifdef KYTY_EMU_ENABLED
namespace Kyty::Loader {
enum class ProcessorArchitecture
{
Unknown,
Amd64, // x64 (AMD or Intel)
};
struct SystemInfo
{
uint32_t PageSize;
uint64_t MinimumApplicationAddress;
uint64_t MaximumApplicationAddress;
uint32_t ActiveProcessorMask;
uint32_t NumberOfProcessors;
ProcessorArchitecture ProcessorArchitecture;
uint32_t AllocationGranularity;
uint16_t ProcessorLevel;
uint16_t ProcessorRevision;
String ProcessorName;
};
SystemInfo GetSystemInfo();
namespace VirtualMemory {
class ExceptionHandlerPrivate;
class ExceptionHandler
{
public:
enum class ExceptionType
{
Unknown,
AccessViolation
};
enum class AccessViolationType
{
Unknown,
Read,
Write,
Execute
};
struct ExceptionInfo
{
ExceptionType type = ExceptionType::Unknown;
AccessViolationType access_violation_type = AccessViolationType::Unknown;
uint64_t access_violation_vaddr = 0;
uint64_t exception_address = 0;
uint64_t rbp = 0;
uint32_t exception_win_code = 0;
};
using handler_func_t = void (*)(const ExceptionInfo*);
ExceptionHandler();
virtual ~ExceptionHandler();
KYTY_CLASS_NO_COPY(ExceptionHandler);
static uint64_t GetSize();
bool Install(uint64_t base_address, uint64_t handler_addr, uint64_t image_size, handler_func_t func);
bool Uninstall();
static bool InstallVectored(handler_func_t func);
private:
ExceptionHandlerPrivate* m_p = nullptr;
};
enum class Mode : uint32_t
{
NoAccess = 0,
Read = 1,
Write = 2,
ReadWrite = Read | Write,
Execute = 4,
ExecuteRead = Execute | Read,
ExecuteWrite = Execute | Write,
ExecuteReadWrite = Execute | Read | Write,
};
inline bool IsExecute(Mode mode)
{
return (mode == Mode::Execute || mode == Mode::ExecuteRead || mode == Mode::ExecuteWrite || mode == Mode::ExecuteReadWrite);
}
void Init();
uint64_t Alloc(uint64_t address, uint64_t size, Mode mode);
uint64_t AllocAligned(uint64_t address, uint64_t size, Mode mode, uint64_t alignment);
bool AllocFixed(uint64_t address, uint64_t size, Mode mode);
bool Free(uint64_t address);
bool Protect(uint64_t address, uint64_t size, Mode mode, Mode* old_mode = nullptr);
bool FlushInstructionCache(uint64_t address, uint64_t size);
bool PatchReplace(uint64_t vaddr, uint64_t value);
} // namespace VirtualMemory
} // namespace Kyty::Loader
#endif // KYTY_EMU_ENABLED
#endif /* EMULATOR_INCLUDE_EMULATOR_LOADER_VIRTUALMEMORY_H_ */