-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpatch_hook_area.cpp
More file actions
66 lines (52 loc) · 2.3 KB
/
Copy pathpatch_hook_area.cpp
File metadata and controls
66 lines (52 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "patch_hook_area.h"
#include "analyze/base_func.h"
#include "3rdparty/aarch64_asm_helper.h"
using namespace asmjit;
using namespace asmjit::a64;
using namespace asmjit::a64::Predicate;
PatchHookArea::PatchHookArea(const PatchBase66& patch_base)
: PatchBase66(patch_base) {}
PatchHookArea::~PatchHookArea() {}
size_t PatchHookArea::reserve_hook_trampoline_area(
const SymbolRegion& hook_func_start_region,
size_t& out_area_start,
size_t& out_area_size,
std::vector<patch_bytes_data>& vec_out_patch_bytes_data)
{
size_t hook_func_start_addr = hook_func_start_region.offset;
if (hook_func_start_addr == 0) { return 0; }
std::cout << "Reserving hook trampoline area at: 0x" << std::hex << hook_func_start_addr << std::endl << std::endl;
// 计算可用空间
size_t available_size = hook_func_start_region.size;
size_t reserve_size = std::min(available_size, HOOK_TRAMPOLINE_SIZE);
if (reserve_size < HOOK_AREA_HEADER_SIZE + 256) {
std::cout << "[发生错误] Not enough space for hook trampoline area" << std::endl;
return 0;
}
// 输出跳板区信息
out_area_start = hook_func_start_addr + HOOK_AREA_HEADER_SIZE;
out_area_size = reserve_size - HOOK_AREA_HEADER_SIZE;
// 初始化跳板区头部
// 布局:
// [0]: base_addr (用于验证)
// [8]: used_offset (初始为0)
struct HookAreaHeader {
uint64_t base_addr;
uint64_t used_offset;
} header;
header.base_addr = out_area_start;
header.used_offset = 0;
std::string str_bytes = bytes2hex((const unsigned char*)&header, sizeof(header));
// 填充剩余空间为NOP (便于调试)
// NOP指令: 0xD503201F
uint32_t nop_insn = 0xD503201F;
for (size_t i = HOOK_AREA_HEADER_SIZE; i < reserve_size; i += 4) {
str_bytes += bytes2hex((const unsigned char*)&nop_insn, sizeof(nop_insn));
}
vec_out_patch_bytes_data.push_back({ str_bytes, hook_func_start_addr });
std::cout << "Hook trampoline area reserved:" << std::endl;
std::cout << " Header address: 0x" << std::hex << hook_func_start_addr << std::endl;
std::cout << " Handler area start: 0x" << std::hex << out_area_start << std::endl;
std::cout << " Available size: " << std::dec << out_area_size << " bytes" << std::endl;
return reserve_size;
}