Telkin
Loading...
Searching...
No Matches
Hooks.h
Go to the documentation of this file.
1#pragma once
2
3#include <dynamic_libs/os_types.h>
4#include <type_traits>
5#include <telkin/Preprocessor.h>
6
7namespace tk {
8 enum class DataMagic : u32 {
9 BranchHook = 0x01C0FFEE,
10 PointerHook = 0x02C0FFEE,
11 PatchHook = 0x03C0FFEE,
12 NullHook = 0x04C0FFEE,
13 };
14
15 constexpr int cHookSize = 0x20;
16
17 enum class BranchType : u32 {
20 };
21
32
33 static_assert(sizeof(BranchHook) == cHookSize, "BranchHook size mismatch");
34 static_assert(std::is_trivially_constructible<BranchHook>::value, "BranchHook is not trivially constructible");
35 static_assert(std::is_trivially_destructible<BranchHook>::value, "BranchHook is not trivially destructible");
36
47
48 static_assert(sizeof(PointerHook) == cHookSize, "PointerHook size mismatch");
49 static_assert(std::is_trivially_constructible<PointerHook>::value, "PointerHook is not trivially constructible");
50 static_assert(std::is_trivially_destructible<PointerHook>::value, "PointerHook is not trivially destructible");
51
63
64 static_assert(sizeof(PatchHook) == cHookSize, "PatchHook size mismatch");
65 static_assert(std::is_trivially_constructible<PatchHook>::value, "PatchHook is not trivially constructible");
66 static_assert(std::is_trivially_destructible<PatchHook>::value, "PatchHook is not trivially destructible");
67
72
73 static_assert(sizeof(NullHook) == cHookSize, "NullHook size mismatch");
74 static_assert(std::is_trivially_constructible<NullHook>::value, "NullHook is not trivially constructible");
75 static_assert(std::is_trivially_destructible<NullHook>::value, "NullHook is not trivially destructible");
76}
77
78#ifdef __clangd__
79 #define tMangle(...) PP_STR(__VA_ARGS__)
80#else
81 #define tMangle(...) __builtin_mangle(__VA_ARGS__) // red hills compiler magic
82#endif
83
84// Normal func
85#define _tBranch3(addr, target, type)
86 tk::BranchHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::BranchHook(tk::DataMagic::BranchHook, reinterpret_cast<u32*>(addr), tMangle(target), type, 0, 0, 0, 0)
87
88// Overloaded func
89#define _tBranch4(addr, target, sig, type)
90 tk::BranchHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::BranchHook(tk::DataMagic::BranchHook, reinterpret_cast<u32*>(addr), tMangle(target, sig), type, 0, 0, 0, 0)
91
92#define tBranch(...)
93 PP_CONCAT_VAL(_tBranch, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
94
95// Explicit mangled string
96#define tBranchEx(addr, targetSym, type)
97 tk::BranchHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::BranchHook(tk::DataMagic::BranchHook, reinterpret_cast<u32*>(addr), targetSym, type, 0, 0, 0, 0)
98
99// Normal func
100#define _tPointerCode2(addr, target)
101 tk::PointerHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::PointerHook(tk::DataMagic::PointerHook, reinterpret_cast<u32*>(addr), tMangle(target), false, 0, 0, 0, 0)
102
103// Overloaded func
104#define _tPointerCode3(addr, target, sig)
105 tk::PointerHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::PointerHook(tk::DataMagic::PointerHook, reinterpret_cast<u32*>(addr), tMangle(target, sig), false, 0, 0, 0, 0)
106
107#define tPointerCode(...)
108 PP_CONCAT_VAL(_tPointerCode, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
109
110#define tPointerData(addr, target)
111 tk::PointerHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::PointerHook(tk::DataMagic::PointerHook, reinterpret_cast<u32*>(addr), tMangle(target), true, 0, 0, 0, 0)
112
113// Explicit mangled string
114#define tPointerEx(addr, targetSym, isdata)
115 tk::PointerHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::PointerHook(tk::DataMagic::PointerHook, reinterpret_cast<u32*>(addr), targetSym, isdata, 0, 0, 0, 0)
116
117#define _tPatch_u(addr, bits, ...)
118 const u##bits _tPatch_Data_ ## addr [] = { __VA_ARGS__ };
119 tk::PatchHook _tPatch_ ## addr __attribute__((section(".loaderdata"))) = tk::PatchHook(tk::DataMagic::PatchHook, reinterpret_cast<u32*>(addr), sizeof(_tPatch_Data_##addr) / sizeof(u##bits), bits, reinterpret_cast<const void*>(&_tPatch_Data_##addr), 0, 0, 0, 0)
120
121#define _tPatch_s(addr, bits, ...)
122 const s##bits _tPatch_Data_ ## addr [] = { __VA_ARGS__ };
123 tk::PatchHook _tPatch_ ## addr __attribute__((section(".loaderdata"))) = tk::PatchHook(tk::DataMagic::PatchHook, reinterpret_cast<u32*>(addr), sizeof(_tPatch_Data_##addr) / sizeof(u##bits), bits, reinterpret_cast<const void*>(&_tPatch_Data_##addr), 0, 0, 0, 0)
124
125#define _tPatch_f(addr, bits, ...)
126 const f##bits _tPatch_Data_ ## addr [] = { __VA_ARGS__ };
127 tk::PatchHook _tPatch_ ## addr __attribute__((section(".loaderdata"))) = tk::PatchHook(tk::DataMagic::PatchHook, reinterpret_cast<u32*>(addr), sizeof(_tPatch_Data_##addr) / sizeof(u##bits), bits, reinterpret_cast<const void*>(&_tPatch_Data_##addr), 0, 0, 0, 0)
128
129#define tPatch8u(addr, ...) _tPatch_u(addr, 8, __VA_ARGS__)
130#define tPatch16u(addr, ...) _tPatch_u(addr, 16, __VA_ARGS__)
131#define tPatch32u(addr, ...) _tPatch_u(addr, 32, __VA_ARGS__)
132#define tPatch64u(addr, ...) _tPatch_u(addr, 64, __VA_ARGS__)
133
134#define tPatch8s(addr, ...) _tPatch_s(addr, 8, __VA_ARGS__)
135#define tPatch16s(addr, ...) _tPatch_s(addr, 16, __VA_ARGS__)
136#define tPatch32s(addr, ...) _tPatch_s(addr, 32, __VA_ARGS__)
137#define tPatch64s(addr, ...) _tPatch_s(addr, 64, __VA_ARGS__)
138
139#define tPatch32f(addr, ...) _tPatch_f(addr, 32, __VA_ARGS__)
140#define tPatch64f(addr, ...) _tPatch_f(addr, 64, __VA_ARGS__)
141
142#define tPatchNop(addr) tPatch32u(addr, 0x60000000)
143#define tPatchBlr(addr) tPatch32u(addr, 0x4E800020)
#define tPatch32u(addr,...)
Definition Hooks.h:131
#define tMangle(...)
Definition Hooks.h:81
#define _tPatch_s(addr, bits,...)
Definition Hooks.h:121
#define _tPatch_f(addr, bits,...)
Definition Hooks.h:125
#define _tPatch_u(addr, bits,...)
Definition Hooks.h:117
DataMagic
Definition Hooks.h:8
@ PointerHook
Definition Hooks.h:10
@ NullHook
Definition Hooks.h:12
@ PatchHook
Definition Hooks.h:11
@ BranchHook
Definition Hooks.h:9
u64(*)() getTitleID_t
Definition Mod.h:16
constexpr int cHookSize
Definition Hooks.h:15
BranchType
Definition Hooks.h:17
@ b
Definition Hooks.h:18
@ bl
Definition Hooks.h:19
Definition Hooks.h:22
const char * target
Definition Hooks.h:25
u32 reserved3
Definition Hooks.h:29
BranchType type
Definition Hooks.h:26
u32 reserved2
Definition Hooks.h:28
u32 reserved1
Definition Hooks.h:27
DataMagic magic
Definition Hooks.h:23
u32 * source
Definition Hooks.h:24
u32 reserved4
Definition Hooks.h:30
Definition Hooks.h:68
DataMagic magic
Definition Hooks.h:69
u8 padding[cHookSize - sizeof(DataMagic)]
Definition Hooks.h:70
Definition Hooks.h:52
DataMagic magic
Definition Hooks.h:53
void * addr
Definition Hooks.h:54
u16 dataSize
Definition Hooks.h:56
u32 reserved2
Definition Hooks.h:59
u32 reserved1
Definition Hooks.h:58
u32 reserved3
Definition Hooks.h:60
u16 count
Definition Hooks.h:55
u32 reserved4
Definition Hooks.h:61
const void * data
Definition Hooks.h:57
Definition Hooks.h:37
DataMagic magic
Definition Hooks.h:38
u32 reserved4
Definition Hooks.h:45
u32 isData
Definition Hooks.h:41
const char * target
Definition Hooks.h:40
u32 reserved1
Definition Hooks.h:42
u32 * source
Definition Hooks.h:39
u32 reserved3
Definition Hooks.h:44
u32 reserved2
Definition Hooks.h:43