-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
63 lines (51 loc) · 1.56 KB
/
main.cc
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
#include <stdio.h>
#include <assert.h>
#include <capstone/capstone.h>
#include "elfio/elfio.hpp"
#include "quark.hh"
#include "builder.hh"
using namespace ELFIO;
int main(int argc, char** argv) {
if (argc != 3) {
fprintf(stderr, "usage: %s <input> <output>\n", argv[0]);
return 1;
}
elfio reader;
if (!reader.load(argv[1])) {
fprintf(stderr, "unable to load file %s\n", argv[1]);
return 1;
}
csh handle;
if (cs_open(CS_ARCH_AARCH64, CS_MODE_ARM, &handle) != CS_ERR_OK) {
fprintf(stderr, "could not open arm64 disassembler\n");
return 1;
}
cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
assert(reader.get_class() == ELFIO::ELFCLASS64);
assert(reader.get_encoding() == ELFIO::ELFDATA2LSB);
assert(reader.get_type() == ELFIO::ET_REL);
struct qk_elf elf = qk_readelf(reader, handle);
const uint64_t INST_RET = 0xd65f03c0;
for (auto& sec : elf.codes) {
struct qk_builder* b = new_builder(&elf, sec);
struct qk_inst* inst = sec->inst_front;
while (inst) {
if (inst->data == INST_RET && inst != sec->inst_front) {
b->locate(inst);
b->insert_before(new_inst((struct qk_inst_dat){
.data = 0xd503201f,
.size = 4,
}));
}
inst = inst->next;
}
delete b;
}
elf.encode(argv[2]);
cs_close(&handle);
return 0;
}
#ifdef __cplusplus
extern "C"
#endif
const char* __asan_default_options() { return "detect_leaks=0"; }