-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
47 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @file Arch.h | ||
* @brief Arch utilities. | ||
*/ | ||
#ifndef _CTRL_ARCH_H | ||
#define _CTRL_ARCH_H | ||
|
||
#include "CTRL/Types.h" | ||
|
||
#if defined(__cplusplus) | ||
extern "C" { | ||
#endif // __cplusplus | ||
|
||
/** | ||
* @brief Check if an address references thumb code. | ||
* @param[in] addr Address. | ||
* @result True if the address references thumb code, false otherwise. | ||
*/ | ||
CTRL_INLINE bool ctrlIsThumb(u32 addr) { return addr & 1; } | ||
|
||
/** | ||
* @brief Set thumb mode bit in address. | ||
* @param[in] addr Address. | ||
* @result Address with thumb mode bit set. | ||
*/ | ||
CTRL_INLINE u32 ctrlSetThumb(u32 addr) { return addr | 1; } | ||
|
||
/** | ||
* @brief Clear thumb mode bit in address. | ||
* @param[in] addr Address. | ||
* @result Address with thumb mode bit clear. | ||
*/ | ||
CTRL_INLINE u32 ctrlClearThumb(u32 addr) { return addr & ~(1u); } | ||
|
||
#if defined(__cplusplus) | ||
} | ||
#endif // __cplusplus | ||
|
||
#endif /* _CTRL_ARCH_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
#include "CTRL/Hook.h" | ||
#include "CTRL/Patch.h" | ||
#include "CTRL/Memory.h" | ||
#include "CTRL/Arch.h" | ||
|
||
#include <string.h> | ||
|
||
#define IS_THUMB(addr) ((addr) & 1) | ||
#define GET_REAL_ADDR(addr) ((addr) & ~(1u)) | ||
|
||
const u8 ARM_OP[4] = { 0x04, 0xF0, 0x1F, 0xE5 }; // LDR PC, [PC, #-4] | ||
const u8 THUMB_OP[4] = { 0x00, 0x4F, 0x38, 0x47 }; // LDR R7, [PC, #0]; BX R7 | ||
|
||
Result ctrlPlaceHook(CTRLHook* hook) { | ||
u8 buffer[CTRL_HOOK_SIZE]; | ||
memcpy(buffer, IS_THUMB(hook->addr) ? THUMB_OP : ARM_OP, 4); | ||
const u32 realAddr = ctrlClearThumb(hook->addr); | ||
memcpy(buffer, ctrlIsThumb(hook->addr) ? THUMB_OP : ARM_OP, 4); | ||
memcpy(&buffer[4], &hook->callback, sizeof(u32)); | ||
memcpy(hook->ogBytes, (void*)GET_REAL_ADDR(hook->addr), CTRL_HOOK_SIZE); | ||
return ctrlPatchMemory(GET_REAL_ADDR(hook->addr), buffer, CTRL_HOOK_SIZE); | ||
memcpy(hook->ogBytes, (void*)realAddr, CTRL_HOOK_SIZE); | ||
return ctrlPatchMemory(realAddr, buffer, CTRL_HOOK_SIZE); | ||
} | ||
|
||
Result ctrlRemoveHook(CTRLHook* hook) { | ||
return ctrlPatchMemory(GET_REAL_ADDR(hook->addr), hook->ogBytes, CTRL_HOOK_SIZE); | ||
return ctrlPatchMemory(ctrlClearThumb(hook->addr), hook->ogBytes, CTRL_HOOK_SIZE); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters