summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaurice <maurice.henke@protonmail.com>2021-04-25 23:39:00 +0200
committerMaurice <maurice.henke@protonmail.com>2021-04-25 23:39:00 +0200
commit09319a8b90032bd7cad04d45ae7885b4565b48e1 (patch)
treecc898db9e4a931fce49078c7aafcdd30b6fb89d3
parent1b78e18270c35849a624a77426d2920438eeab4b (diff)
parent2735bb35773600bb18647484d3640e3067c59ab6 (diff)
Merge branch 'master' of https://github.com/zsrtp/libtp_rel
-rw-r--r--include/patch.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/include/patch.h b/include/patch.h
index 4dff3d3..e67447d 100644
--- a/include/patch.h
+++ b/include/patch.h
@@ -36,5 +36,40 @@ namespace libtp::patch
return reinterpret_cast<Func>( trampoline );
}
+
+ template<typename Func>
+ Func unhookFunction( Func trampoline )
+ {
+ uint32_t* instructions = reinterpret_cast<uint32_t*>( trampoline );
+
+ // Restore the original instruction
+ uint32_t instructionAddress = reinterpret_cast<uint32_t>( &instructions[1] );
+ uint32_t branchInstruction = instructions[1];
+ uint32_t branchLength;
+ uint32_t* address;
+
+ if ( branchInstruction >= 0x4A000000 )
+ {
+ // Branch is negative
+ branchLength = 0x4C000000 - branchInstruction;
+ address = reinterpret_cast<uint32_t*>( instructionAddress - branchLength - 0x4 );
+ }
+ else
+ {
+ // Branch is positive
+ branchLength = branchInstruction - 0x48000000;
+ address = reinterpret_cast<uint32_t*>( instructionAddress + branchLength - 0x4 );
+ }
+
+ *address = instructions[0];
+
+ // Clear the cache for both the address and where the instructions were stored
+ memory::clear_DC_IC_Cache( address, sizeof( uint32_t ) );
+ memory::clear_DC_IC_Cache( instructions, sizeof( uint32_t ) );
+
+ // Free the memory used by the trampoline
+ delete[] instructions;
+ return nullptr;
+ }
} // namespace libtp::patch
#endif \ No newline at end of file