/* * inlinehook.c - inlinehook for functions * * Copyright (C) 2016 Baidu, Inc. All Rights Reserved. * * You should have received a copy of license along with this program; * if not, ask for it from Baidu, Inc. * */ #include #include #include #include #include #include #include #include #include "inlinehook.h" #include "hook_insn.h" #include "util.h" #if defined(__aarch64__) /* any insn with bit[28:27] = 00 kprobes use 0x07F001F8U */ #define OASES_ARM_UNDEF_INSN 0x07F02016U /* * make_jump_insn() - generate a B instruction to jump from @addr to @dst * * Return: 0 for success, also set insn, otherwise return -1 when failed */ int oases_make_jump_insn(u32 *addr, u32 *dst, u32 *insn) { u32 jump_insn; unsigned long offset = 0; unsigned long forward_max = 0; unsigned long backward_min = 0; offset = (unsigned long) dst - (unsigned long) addr; /* * Forward offset: 0 - 0x7FFFFFCUL * Backward offset: 0xFFFFFFFFF8000000UL - 0xFFFFFFFFFFFFFFFCUL */ forward_max = 0x7FFFFFCUL; backward_min = 0xFFFFFFFFF8000000UL; if (offset > forward_max && offset < backward_min) { return -1; } /* offset = SignExtend(imm26:'00', 64) */ offset = (offset & 0xFFFFFFFUL) >> 2; jump_insn = (u32) 0x14000000UL + (u32) offset; *insn = jump_insn; return 0; } /* location dependent instructions */ /* ADR ,