Skip to content

Commit

Permalink
Add CPU architecture detection and gate current mod function hooking …
Browse files Browse the repository at this point in the history
…behind x86_64 define
  • Loading branch information
Mr-Wiseguy committed Sep 10, 2024
1 parent 26a0543 commit f36cdf9
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions librecomp/src/mods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@
#include "librecomp/game.hpp"
#include "n64recomp.h"

// Architecture detection.

// MSVC x86_64
#if defined (_M_AMD64) && (_M_AMD64 == 100) && !defined (_M_ARM64EC)
# define IS_X86_64
// GCC/Clang x86_64
#elif defined(__x86_64__)
# define IS_X86_64
// MSVC/GCC/Clang ARM64
#elif defined(__ARM_ARCH_ISA_A64)
# define IS_ARM64
#else
# error "Unsupported architecture!"
#endif


#if defined(_WIN32)
#define PATHFMT "%ls"
#else
Expand Down Expand Up @@ -355,8 +371,6 @@ void recomp::mods::NativeCodeHandle::set_imported_function(size_t import_index,
}

void patch_func(recomp_func_t* target_func, recomp::mods::GenericFunction replacement_func) {
static const uint8_t movabs_rax[] = {0x48, 0xB8};
static const uint8_t jmp_rax[] = {0xFF, 0xE0};
uint8_t* target_func_u8 = reinterpret_cast<uint8_t*>(target_func);
size_t offset = 0;

Expand All @@ -368,13 +382,21 @@ void patch_func(recomp_func_t* target_func, recomp::mods::GenericFunction replac
uint64_t old_flags;
unprotect(target_func_u8, &old_flags);

#ifdef IS_X86_64
static const uint8_t movabs_rax[] = {0x48, 0xB8};
static const uint8_t jmp_rax[] = {0xFF, 0xE0};
std::visit(overloaded {
[&write_bytes](recomp_func_t* native_func) {
write_bytes(movabs_rax, sizeof(movabs_rax));
write_bytes(&native_func, sizeof(&native_func));
write_bytes(jmp_rax, sizeof(jmp_rax));
}
}, replacement_func);
#elif IS_ARM64
ultramodern::error_handling::message_box("Mod loading not currently implemented on ARM CPUs!\n");
#else
# error "Unsupported architecture"
#endif

protect(target_func_u8, old_flags);
}
Expand Down

0 comments on commit f36cdf9

Please sign in to comment.