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, nullptr, 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, nullptr, 0, 0, 0)
91
92#define tBranch(...)
93 PP_CONCAT_VAL(_tBranch, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
94
95// Normal func, titleid pinned
96#define _tBranchPin4(addr, target, type, titleid)
97 const u64 _tPin_ ## addr = titleid;
98 tk::BranchHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::BranchHook(tk::DataMagic::BranchHook, reinterpret_cast<u32*>(addr), tMangle(target), type, &_tPin_ ## addr, 0, 0, 0)
99
100// Overloaded func, titleid pinned
101#define _tBranchPin5(addr, target, sig, type, titleid)
102 const u64 _tPin_ ## addr = titleid;
103 tk::BranchHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::BranchHook(tk::DataMagic::BranchHook, reinterpret_cast<u32*>(addr), tMangle(target, sig), type, &_tPin_ ## addr, 0, 0, 0)
104
105#define tBranchPin(...)
106 PP_CONCAT_VAL(_tBranchPin, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
107
108// Explicit mangled string
109#define tBranchEx(addr, targetSym, type)
110 tk::BranchHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::BranchHook(tk::DataMagic::BranchHook, reinterpret_cast<u32*>(addr), targetSym, type, nullptr, 0, 0, 0)
111
112// Explicit mangled string, titleid pinned
113#define tBranchPinEx(addr, targetSym, type, titleid)
114 const u64 _tPin_ ## addr = titleid;
115 tk::BranchHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::BranchHook(tk::DataMagic::BranchHook, reinterpret_cast<u32*>(addr), targetSym, type, &_tPin_ ## addr, 0, 0, 0)
116
117// Normal func
118#define _tPointerCode2(addr, target)
119 tk::PointerHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::PointerHook(tk::DataMagic::PointerHook, reinterpret_cast<u32*>(addr), tMangle(target), false, nullptr, 0, 0, 0)
120
121// Overloaded func
122#define _tPointerCode3(addr, target, sig)
123 tk::PointerHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::PointerHook(tk::DataMagic::PointerHook, reinterpret_cast<u32*>(addr), tMangle(target, sig), false, nullptr, 0, 0, 0)
124
125#define tPointerCode(...)
126 PP_CONCAT_VAL(_tPointerCode, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
127
128// Normal func, titleid pinned
129#define _tPointerCodePin3(addr, target, titleid)
130 const u64 _tPin_ ## addr = titleid;
131 tk::PointerHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::PointerHook(tk::DataMagic::PointerHook, reinterpret_cast<u32*>(addr), tMangle(target), false, &_tPin_ ## addr, 0, 0, 0)
132
133// Overloaded func, titleid pinned
134#define _tPointerCodePin4(addr, target, sig)
135 const u64 _tPin_ ## addr = titleid;
136 tk::PointerHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::PointerHook(tk::DataMagic::PointerHook, reinterpret_cast<u32*>(addr), tMangle(target, sig), false, &_tPin_ ## addr, 0, 0, 0)
137
138#define tPointerCode(...)
139 PP_CONCAT_VAL(_tPointerCode, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
140
141#define tPointerCodePin(...)
142 PP_CONCAT_VAL(_tPointerCodePin, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
143
144#define tPointerData(addr, target)
145 tk::PointerHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::PointerHook(tk::DataMagic::PointerHook, reinterpret_cast<u32*>(addr), tMangle(target), true, nullptr, 0, 0, 0)
146
147// Explicit mangled string
148#define tPointerEx(addr, targetSym, isdata)
149 tk::PointerHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::PointerHook(tk::DataMagic::PointerHook, reinterpret_cast<u32*>(addr), targetSym, isdata, nullptr, 0, 0, 0)
150
151// Explicit mangled string, titleid pinned
152#define tPointerPinEx(addr, targetSym, isdata, titleid)
153 const u64 _tPin_ ## addr = titleid;
154 tk::PointerHook _tHook_ ## addr __attribute__((section(".loaderdata"))) = tk::PointerHook(tk::DataMagic::PointerHook, reinterpret_cast<u32*>(addr), targetSym, isdata, &_tPin_ ## addr, 0, 0, 0)
155
156 #define _tPatch_u(addr, bits, ...)
157 const u##bits _tPatch_Data_ ## addr [] = { __VA_ARGS__ };
158 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), nullptr, 0, 0, 0)
159
160#define _tPatch_s(addr, bits, ...)
161 const s##bits _tPatch_Data_ ## addr [] = { __VA_ARGS__ };
162 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), nullptr, 0, 0, 0)
163
164#define _tPatch_f(addr, bits, ...)
165 const f##bits _tPatch_Data_ ## addr [] = { __VA_ARGS__ };
166 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), nullptr, 0, 0, 0)
167
168#define _tPatchPin_u(addr, bits, titleid, ...)
169 const u##bits _tPatch_Data_ ## addr [] = { __VA_ARGS__ };
170 const u64 _tPin_ ## addr = titleid;
171 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), &_tPin_ ## addr, 0, 0, 0)
172
173#define _tPatchPin_s(addr, bits, titleid, ...)
174 const s##bits _tPatch_Data_ ## addr [] = { __VA_ARGS__ };
175 const u64 _tPin_ ## addr = titleid;
176 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), &_tPin_ ## addr, 0, 0, 0)
177
178#define _tPatchPin_f(addr, bits, titleid, ...)
179 const f##bits _tPatch_Data_ ## addr [] = { __VA_ARGS__ };
180 const u64 _tPin_ ## addr = titleid;
181 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), &_tPin_ ## addr, 0, 0, 0)
182
183#define tPatch8u(addr, ...) _tPatch_u(addr, 8, __VA_ARGS__)
184#define tPatch16u(addr, ...) _tPatch_u(addr, 16, __VA_ARGS__)
185#define tPatch32u(addr, ...) _tPatch_u(addr, 32, __VA_ARGS__)
186#define tPatch64u(addr, ...) _tPatch_u(addr, 64, __VA_ARGS__)
187
188#define tPatch8s(addr, ...) _tPatch_s(addr, 8, __VA_ARGS__)
189#define tPatch16s(addr, ...) _tPatch_s(addr, 16, __VA_ARGS__)
190#define tPatch32s(addr, ...) _tPatch_s(addr, 32, __VA_ARGS__)
191#define tPatch64s(addr, ...) _tPatch_s(addr, 64, __VA_ARGS__)
192
193#define tPatch32f(addr, ...) _tPatch_f(addr, 32, __VA_ARGS__)
194#define tPatch64f(addr, ...) _tPatch_f(addr, 64, __VA_ARGS__)
195
196#define tPatchNop(addr) tPatch32u(addr, 0x60000000)
197#define tPatchBlr(addr) tPatch32u(addr, 0x4E800020)
198
199#define tPatchPin8u(addr, titleid, ...) _tPatchPin_u(addr, 8, titleid, __VA_ARGS__)
200#define tPatchPin16u(addr, titleid, ...) _tPatchPin_u(addr, 16, titleid, __VA_ARGS__)
201#define tPatchPin32u(addr, titleid, ...) _tPatchPin_u(addr, 32, titleid, __VA_ARGS__)
202#define tPatchPin64u(addr, titleid, ...) _tPatchPin_u(addr, 64, titleid, __VA_ARGS__)
203
204#define tPatchPin8s(addr, titleid, ...) _tPatchPin_s(addr, 8, titleid, __VA_ARGS__)
205#define tPatchPin16s(addr, titleid, ...) _tPatchPin_s(addr, 16, titleid, __VA_ARGS__)
206#define tPatchPin32s(addr, titleid, ...) _tPatchPin_s(addr, 32, titleid, __VA_ARGS__)
207#define tPatchPin64s(addr, titleid, ...) _tPatchPin_s(addr, 64, titleid, __VA_ARGS__)
208
209#define tPatchPin32f(addr, titleid, ...) _tPatchPin_f(addr, 32, titleid, __VA_ARGS__)
210#define tPatchPin64f(addr, titleid, ...) _tPatchPin_f(addr, 64, titleid, __VA_ARGS__)
211
212#define tPatchPinNop(addr, titleid) tPatchPin32u(addr, titleid, 0x60000000)
213#define tPatchPinBlr(addr, titleid) tPatchPin32u(addr, titleid, 0x4E800020)
#define tPatchPin32u(addr, titleid,...)
Definition Hooks.h:201
#define tPatch32u(addr,...)
Definition Hooks.h:185
#define tMangle(...)
Definition Hooks.h:81
#define _tPatch_s(addr, bits,...)
Definition Hooks.h:160
#define _tPatchPin_f(addr, bits, titleid,...)
Definition Hooks.h:178
#define _tPatchPin_u(addr, bits, titleid,...)
Definition Hooks.h:168
#define _tPatchPin_s(addr, bits, titleid,...)
Definition Hooks.h:173
#define _tPatch_f(addr, bits,...)
Definition Hooks.h:164
#define _tPatch_u(addr, bits,...)
Definition Hooks.h:156
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
DataMagic magic
Definition Hooks.h:23
u32 * source
Definition Hooks.h:24
const u64 * titleID
Definition Hooks.h:27
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
const u64 * titleID
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 * source
Definition Hooks.h:39
u32 reserved3
Definition Hooks.h:44
const u64 * titleID
Definition Hooks.h:42
u32 reserved2
Definition Hooks.h:43