Advertisement
Sc2ad

Orig-copy transpiler

Dec 10th, 2020
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. /*
  2. Orig copy approach. Uses the fact that the installation already protects a (sufficiently large enough) region of memory for any inserted instructions. Not always guaranteed, but will almost always work.
  3. This is if you don't want to do one of the other options, or if the heap is always mapped as -X (which it might be).
  4. */
  5.  
  6. MAKE_HOOK_OFFSETLESS(LateUpdate, void, void *self) {
  7.     // We actually modified orig, so we just call it here.
  8.     // You could do something similar to the first approach (allocate a temporary array) and call that instead if you prefer
  9.     LateUpdate(self);
  10. }
  11.  
  12. extern "C" void load() {
  13.     auto lateUpdateInfo = il2cpp_utils::FindMethodUnsafe("", "BeatmapObjectCallbackController", "LateUpdate", 0);
  14.     auto nextMethodInfo = il2cpp_utils::FindMethodUnsafe("", "BeatmapObjectCallbackController", "AddBeatmapObjectCallback", 2);
  15.     uint32_t *startAddr = (uint32_t *) lateUpdateInfo->methodPointer;
  16.     uint32_t *endAddr = (uint32_t *) nextMethodInfo->methodPointer;
  17.  
  18.     INSTALL_HOOK_OFFSETLESS(LateUpdate, il2cpp_utils::FindMethodUnsafe("", "BeatmapObjectCallbackController", "LateUpdate", 0));
  19.     // Copy instructions from trampoline
  20.     std::vector<uint32_t> instructions(reinterpret_cast<uint32_t*>(LateUpdate), reinterpret_cast<uint32_t*>(
  21.         reinterpret_cast<std::size_t>(LateUpdate) + (endAddr - startAddr)
  22.     ));
  23.  
  24.     int dstIdx = -1;
  25.     for (int i = 0; i < instructions.size(); i++) {
  26.         uint32_t ins = instructions[i];
  27.         if (ins == 0x1e293901) { // 0x0139291e fsub s1,s8,s9
  28.             dstIdx = i;
  29.             // Insert instructions, can insert at index i, since insts is copied
  30.             // This will replace the fsub instruction (can skip if you add 1 to dstIdx first)
  31.             reinterpret_cast<uint32_t*>(LateUpdate)[dstIdx] = 0x0;
  32.             ++dstIdx;
  33.         }
  34.         if (dstIdx >= 0) {
  35.             // Only overwrite orig when necessary
  36.             // (Can save some code if you use a local)
  37.             reinterpret_cast<uint32_t*>(LateUpdate)[dstIdx] = ins;
  38.             ++dstIdx;
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement