Skip to content

Commit e9fce1a

Browse files
committed
added handle for b.cond
1 parent 2b1b4b1 commit e9fce1a

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/tinyhook.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static int calc_far_jump(uint8_t *output, void *src, void *dst, bool link) {
5656
return jump_size;
5757
}
5858

59-
static int calc_jump(uint8_t *output, void *src, void *dst, bool link) {
59+
static int calc_jump(void *output, void *src, void *dst, bool link) {
6060
if (need_far_jump(src, dst))
6161
return calc_far_jump(output, src, dst, link);
6262
else
@@ -103,7 +103,16 @@ static inline void save_header(void **src_p, void **dst_p, int min_len) {
103103
bool link = insn >> 31;
104104
int32_t imm26 = sign_extend(insn, 26);
105105
void *addr = (void *)src + (imm26 << 2);
106-
dst += calc_jump((uint8_t *)dst, dst, addr, link) / 4;
106+
dst += calc_jump(dst, dst, addr, link) / 4;
107+
}
108+
else if (((insn ^ 0x54000000) & 0xff000000) == 0) {
109+
// b.cond or bc.cond
110+
int32_t imm19 = sign_extend(insn >> 5, 19);
111+
void *jmp_dst = (void *)src + (imm19 << 2);
112+
int jmp_size = calc_jump(dst + 1, dst + 1, jmp_dst, false);
113+
// clean imm, set new imm, invert cond
114+
*dst++ = ((insn & 0xff00001f) | ((jmp_size + 4) << 3)) ^ 1;
115+
dst += jmp_size / 4;
107116
}
108117
else {
109118
*dst++ = insn;
@@ -132,7 +141,7 @@ static inline void save_header(void **src_p, void **dst_p, int min_len) {
132141
}
133142
else if ((insn.opcode & 0xf0) == 0x70) {
134143
// jcc (short)
135-
// revert the condition and insert a jump (len: 2 -> 2+14)
144+
// invert the condition and insert a jump (len: 2 -> 2+14)
136145
void *jmp_dst = src + insn.len + insn.imm8;
137146
int jmp_len = calc_jump(dst + 2, dst + 2, jmp_dst, false);
138147
*(uint8_t *)dst = insn.opcode ^ 1; // invert the condition

0 commit comments

Comments
 (0)