Commit c178fa97 authored by swift_gan's avatar swift_gan Committed by swift_gan

tweak code

parent 30c986ac
......@@ -23,8 +23,8 @@ template<typename Inst>
A64_INST_PC_REL<Inst>::A64_INST_PC_REL() {}
template<typename Inst>
ADDR A64_INST_PC_REL<Inst>::getImmPCOffsetTarget() {
return reinterpret_cast<ADDR>(this->getImmPCOffset() + (ADDR) this->getPC());
Addr A64_INST_PC_REL<Inst>::getImmPCOffsetTarget() {
return this->getImmPCOffset() + reinterpret_cast<Addr>(this->getPC());
}
//ADR ADRP
......@@ -35,26 +35,22 @@ A64_ADR_ADRP::A64_ADR_ADRP(STRUCT_A64(ADR_ADRP) *inst) : A64_INST_PC_REL(inst) {
decode(inst);
}
ADDR A64_ADR_ADRP::getImmPCOffset() {
Off A64_ADR_ADRP::getImmPCOffset() {
U32 hi = get()->immhi;
U32 lo = get()->immlo;
ADDR offset = signExtend64(12, COMBINE(hi, lo, IMM_LO_W));
Off offset = signExtend64(IMM_LO_W + IMM_HI_W, COMBINE(hi, lo, IMM_LO_W));
if (isADRP()) {
offset *= PAGE_SIZE;
}
return offset;
}
ADDR A64_ADR_ADRP::getImmPCOffsetTarget() {
Addr A64_ADR_ADRP::getImmPCOffsetTarget() {
void * base = AlignDown(getPC(), PAGE_SIZE);
return reinterpret_cast<ADDR>(getImmPCOffset() + (ADDR) base);
return getImmPCOffset() + reinterpret_cast<Addr>(base);
}
int A64_ADR_ADRP::getImm() {
return getImmPCOffset();
}
A64_ADR_ADRP::A64_ADR_ADRP(A64_ADR_ADRP::OP op, RegisterA64 *rd, int imme) : op(op), rd(rd),
A64_ADR_ADRP::A64_ADR_ADRP(A64_ADR_ADRP::OP op, RegisterA64 *rd, S64 imme) : op(op), rd(rd),
imme(imme) {
assembler();
}
......@@ -111,11 +107,11 @@ A64_B_BL::A64_B_BL(STRUCT_A64(B_BL) *inst) : A64_INST_PC_REL(inst) {
decode(inst);
}
A64_B_BL::A64_B_BL(A64_B_BL::OP op, ADDR offset) : op(op), offset(offset) {
A64_B_BL::A64_B_BL(A64_B_BL::OP op, Off offset) : op(op), offset(offset) {
assembler();
}
ADDR A64_B_BL::getImmPCOffset() {
Off A64_B_BL::getImmPCOffset() {
return signExtend64(26 + 2, COMBINE(get()->imm26, 0b00, 2));
}
......@@ -140,13 +136,13 @@ A64_CBZ_CBNZ::A64_CBZ_CBNZ(STRUCT_A64(CBZ_CBNZ) *inst) : A64_INST_PC_REL(inst) {
decode(inst);
}
A64_CBZ_CBNZ::A64_CBZ_CBNZ(A64_CBZ_CBNZ::OP op, ADDR offset, RegisterA64 *rt) : op(op),
A64_CBZ_CBNZ::A64_CBZ_CBNZ(A64_CBZ_CBNZ::OP op, Off offset, RegisterA64 *rt) : op(op),
offset(offset),
rt(rt) {
assembler();
}
ADDR A64_CBZ_CBNZ::getImmPCOffset() {
Off A64_CBZ_CBNZ::getImmPCOffset() {
return signExtend64(19 + 2, COMBINE(get()->imm19, 0b00, 2));
}
......@@ -177,11 +173,11 @@ A64_B_COND::A64_B_COND(STRUCT_A64(B_COND) *inst) : A64_INST_PC_REL(inst) {
decode(inst);
}
A64_B_COND::A64_B_COND(Condition condition, ADDR offset) : condition(condition), offset(offset) {
A64_B_COND::A64_B_COND(Condition condition, Off offset) : condition(condition), offset(offset) {
assembler();
}
ADDR A64_B_COND::getImmPCOffset() {
Off A64_B_COND::getImmPCOffset() {
return signExtend64(19 + 2, COMBINE(get()->imm19, 0b00, 2));
}
......@@ -205,14 +201,14 @@ A64_TBZ_TBNZ::A64_TBZ_TBNZ(STRUCT_A64(TBZ_TBNZ) *inst) : A64_INST_PC_REL(inst) {
decode(inst);
}
A64_TBZ_TBNZ::A64_TBZ_TBNZ(A64_TBZ_TBNZ::OP op, RegisterA64 *rt, U32 bit, ADDR offset) : op(op),
A64_TBZ_TBNZ::A64_TBZ_TBNZ(A64_TBZ_TBNZ::OP op, RegisterA64 *rt, U32 bit, Off offset) : op(op),
rt(rt),
bit(bit),
offset(offset) {
assembler();
}
ADDR A64_TBZ_TBNZ::getImmPCOffset() {
Off A64_TBZ_TBNZ::getImmPCOffset() {
return signExtend64(14 + 2, COMBINE(get()->imm14, 0b00, 2));
}
......@@ -247,12 +243,12 @@ A64_LDR_LIT::A64_LDR_LIT(STRUCT_A64(LDR_LIT) *inst) : A64_INST_PC_REL(inst) {
}
A64_LDR_LIT::A64_LDR_LIT(A64_LDR_LIT::OP op, RegisterA64 *rt, ADDR offset) : op(op), rt(rt),
A64_LDR_LIT::A64_LDR_LIT(A64_LDR_LIT::OP op, RegisterA64 *rt, Off offset) : op(op), rt(rt),
offset(offset) {
assembler();
}
ADDR A64_LDR_LIT::getImmPCOffset() {
Off A64_LDR_LIT::getImmPCOffset() {
return signExtend64(19 + 2, COMBINE(get()->imm19, 0b00, 2));
}
......@@ -282,7 +278,12 @@ A64_STR_IMM::A64_STR_IMM(STRUCT_A64(STR_IMM) *inst) : InstructionA64(inst) {
decode(inst);
}
A64_STR_IMM::A64_STR_IMM(RegisterA64 *rt, const MemOperand &operand) : rt(rt), operand(operand) {
A64_STR_IMM::A64_STR_IMM(RegisterA64 &rt, const MemOperand &operand) : rt(&rt), operand(operand) {
assembler();
}
A64_STR_IMM::A64_STR_IMM(Condition condition, RegisterA64 &rt, const MemOperand &operand)
: condition(condition), rt(&rt), operand(operand) {
assembler();
}
......@@ -294,6 +295,16 @@ AddrMode A64_STR_IMM::decodeAddrMode() {
} else if (get()->P == 1 && get()->W == 1) {
return PreIndex;
} else {
valid = false;
return NonAddrMode;
}
}
void A64_STR_IMM::decode(STRUCT_A64(STR_IMM) *inst) {
Instruction::decode(inst);
}
void A64_STR_IMM::assembler() {
INST_DCHECK(condition, Condition::nv)
}
......@@ -26,6 +26,18 @@ inst_test.raw = inst; \
return inst_test.inst.opcode == OPCODE_A64(X); \
}
#define DEFINE_IS_EXT(X, EXT_COND) \
inline static bool is(InstA64& inst) { \
union { \
InstA64 raw; \
STRUCT_A64(X) inst; \
} inst_test; \
inst_test.raw = inst; \
return inst_test.inst.opcode == OPCODE_A64(X) && EXT_COND; \
}
#define TEST_INST_FIELD(F,V) inst_test.inst.F == V
namespace SandHook {
namespace Asm {
......@@ -48,9 +60,12 @@ namespace SandHook {
return value << (32 - bits);
}
static inline U64 signExtend64(unsigned int bits, U64 value) {
U64 C = (U64) ((-1) << (bits - (U64) 1));
return static_cast<U64>((value + C) ^ C);
static inline S64 signExtend64(unsigned int bits, U64 value) {
return ExtractSignedBitfield64(bits - 1, 0, value);
}
static inline S32 signExtend32(unsigned int bits, U32 value) {
return ExtractSignedBitfield32(bits - 1, 0, value);
}
bool isPCRelAddressing() {
......@@ -151,9 +166,9 @@ namespace SandHook {
A64_INST_PC_REL(Inst *inst);
virtual ADDR getImmPCOffset() = 0;
virtual Off getImmPCOffset() = 0;
virtual ADDR getImmPCOffsetTarget();
virtual Addr getImmPCOffsetTarget();
};
......@@ -171,7 +186,7 @@ namespace SandHook {
A64_ADR_ADRP(STRUCT_A64(ADR_ADRP) *inst);
A64_ADR_ADRP(OP op, RegisterA64 *rd, int imme);
A64_ADR_ADRP(OP op, RegisterA64 *rd, S64 imme);
DEFINE_IS(ADR_ADRP)
......@@ -183,11 +198,9 @@ namespace SandHook {
return get()->op == OP::ADRP;
}
ADDR getImmPCOffset() override;
ADDR getImmPCOffsetTarget() override;
Off getImmPCOffset() override;
int getImm();
Addr getImmPCOffsetTarget() override;
void decode(STRUCT_A64(ADR_ADRP) *decode) override;
......@@ -196,7 +209,7 @@ namespace SandHook {
public:
OP op;
RegisterA64* rd;
int imme;
S64 imme;
};
......@@ -278,11 +291,11 @@ namespace SandHook {
A64_B_BL(STRUCT_A64(B_BL) *inst);
A64_B_BL(OP op, ADDR offset);
A64_B_BL(OP op, Off offset);
DEFINE_IS(B_BL)
inline ADDR getOffset() {
inline Off getOffset() {
return offset;
}
......@@ -294,7 +307,7 @@ namespace SandHook {
return op == B ? UnconditionalBranchOp::B : UnconditionalBranchOp::BL;
};
ADDR getImmPCOffset() override;
Off getImmPCOffset() override;
void decode(STRUCT_A64(B_BL) *decode) override;
......@@ -303,7 +316,7 @@ namespace SandHook {
public:
OP op;
ADDR offset;
Off offset;
};
......@@ -319,7 +332,7 @@ namespace SandHook {
A64_CBZ_CBNZ(STRUCT_A64(CBZ_CBNZ) *inst);
A64_CBZ_CBNZ(OP op, ADDR offset, RegisterA64 *rt);
A64_CBZ_CBNZ(OP op, Off offset, RegisterA64 *rt);
DEFINE_IS(CBZ_CBNZ)
......@@ -327,7 +340,7 @@ namespace SandHook {
return op == CBZ ? CompareBranchOp::CBZ : CompareBranchOp::CBNZ;
}
ADDR getImmPCOffset() override;
Off getImmPCOffset() override;
void decode(STRUCT_A64(CBZ_CBNZ) *inst) override;
......@@ -335,7 +348,7 @@ namespace SandHook {
public:
OP op;
ADDR offset;
Off offset;
RegisterA64* rt;
};
......@@ -346,7 +359,7 @@ namespace SandHook {
A64_B_COND(STRUCT_A64(B_COND) *inst);
A64_B_COND(Condition condition, ADDR offset);
A64_B_COND(Condition condition, Off offset);
DEFINE_IS(B_COND)
......@@ -354,7 +367,7 @@ namespace SandHook {
return B_cond;
}
ADDR getImmPCOffset() override;
Off getImmPCOffset() override;
void decode(STRUCT_A64(B_COND) *inst) override;
......@@ -362,7 +375,7 @@ namespace SandHook {
public:
Condition condition;
ADDR offset;
Off offset;
};
......@@ -378,7 +391,7 @@ namespace SandHook {
A64_TBZ_TBNZ(STRUCT_A64(TBZ_TBNZ) *inst);
A64_TBZ_TBNZ(OP op, RegisterA64 *rt, U32 bit, ADDR offset);
A64_TBZ_TBNZ(OP op, RegisterA64 *rt, U32 bit, Off offset);
DEFINE_IS(TBZ_TBNZ)
......@@ -386,7 +399,7 @@ namespace SandHook {
return op == TBZ ? TestBranchOp::TBZ : TestBranchOp::TBNZ;
};
ADDR getImmPCOffset() override;
Off getImmPCOffset() override;
void decode(STRUCT_A64(TBZ_TBNZ) *inst) override;
......@@ -396,7 +409,7 @@ namespace SandHook {
OP op;
RegisterA64* rt;
U32 bit;
ADDR offset;
Off offset;
};
......@@ -414,7 +427,7 @@ namespace SandHook {
A64_LDR_LIT(STRUCT_A64(LDR_LIT) *inst);
A64_LDR_LIT(OP op, RegisterA64 *rt, ADDR offset);
A64_LDR_LIT(OP op, RegisterA64 *rt, Off offset);
DEFINE_IS(LDR_LIT)
......@@ -431,7 +444,7 @@ namespace SandHook {
}
}
ADDR getImmPCOffset() override;
Off getImmPCOffset() override;
void decode(STRUCT_A64(LDR_LIT) *inst) override;
......@@ -440,7 +453,7 @@ namespace SandHook {
public:
OP op;
RegisterA64* rt;
ADDR offset;
Off offset;
};
......@@ -451,9 +464,15 @@ namespace SandHook {
A64_STR_IMM(STRUCT_A64(STR_IMM) *inst);
A64_STR_IMM(RegisterA64 *rt, const MemOperand &operand);
A64_STR_IMM(RegisterA64 &rt, const MemOperand &operand);
DEFINE_IS(STR_IMM)
A64_STR_IMM(Condition condition, RegisterA64 &rt, const MemOperand &operand);
DEFINE_IS_EXT(STR_IMM, TEST_INST_FIELD(unkown1_0,0) && TEST_INST_FIELD(unkown2_0,0))
void decode(STRUCT_A64(STR_IMM) *inst) override;
void assembler() override;
AddrMode getAddrMode() {
return operand.addr_mode;
......@@ -463,6 +482,7 @@ namespace SandHook {
AddrMode decodeAddrMode();
public:
Condition condition = Condition::al;
RegisterA64* rt;
MemOperand operand = MemOperand(nullptr);
private:
......
......@@ -156,14 +156,23 @@ struct STRUCT_A64(STR_IMM) {
InstA64 cond:4;
InstA64 opcode:3;
InstA64 P:1;
InstA64 unkown:1;
InstA64 U:1;
InstA64 unkown1_0:1;
InstA64 W:1;
InstA64 unkown2:1;
InstA64 unkown2_0:1;
InstA64 rn:4;
InstA64 rt:4;
InstA64 imm12:12;
};
DEFINE_OPCODE(BR, 0b00101)
struct STRUCT_A64(BR) {
InstA64 op:1;
InstA64 opcode:5;
InstA64 imm26:26;
};
#endif //SANDHOOK_NH_INST_AARCH64_H
......@@ -21,6 +21,13 @@ typedef U32 InstT32;
typedef U32 InstRaw;
#endif
#define INST_CHECK(X,V) \
CHECK(X,V, valid = false;)
#define INST_DCHECK(X,V) \
DCHECK(X,V, valid = false;)
namespace SandHook {
namespace Asm {
......@@ -43,8 +50,8 @@ namespace SandHook {
*this->raw = raw;
}
inline void* getPC() const {
return raw;
virtual void* getPC() {
return auto_alloc ? nullptr : raw;
}
inline Raw* get() const {
......@@ -106,9 +113,16 @@ namespace SandHook {
return false;
}
inline bool isValid() const {
return valid;
}
virtual void decode(Inst* inst) {}
virtual void assembler() {}
protected:
bool valid = true;
};
template <typename DType>
......
......@@ -19,7 +19,8 @@ typedef int16_t S16;
typedef int32_t S32;
typedef int64_t S64;
typedef size_t ADDR;
typedef size_t Addr;
typedef S64 Off;
const int PTR_BYTE = sizeof(void*);
......@@ -27,7 +28,7 @@ const int PTR_BYTE = sizeof(void*);
const int BITS_OF_BYTE = 8;
const ADDR PAGE_SIZE = 2 << PAGE_OFFSET;
const Addr PAGE_SIZE = 2 << PAGE_OFFSET;
enum Arch {
arm32,
......@@ -114,10 +115,10 @@ T AlignDown(T pointer,
#endif
//只保留低N位
inline uint64_t TruncateToUintN(unsigned n, uint64_t x) {
return static_cast<uint64_t>(x) & ((UINT64_C(1) << n) - 1);
}
VIXL_DEPRECATED("TruncateToUintN",
inline uint64_t truncate_to_intn(unsigned n, int64_t x)) {
return TruncateToUintN(n, x);
......@@ -151,6 +152,7 @@ V(57) V(58) V(59) V(60) V(61) V(62) V(63)
INT_1_TO_32_LIST(DECLARE_TRUNCATE_TO_UINT_32)
#undef DECLARE_TRUNCATE_TO_INT_N
//位提取
// Bit field extraction.
inline uint64_t ExtractUnsignedBitfield64(int msb, int lsb, uint64_t x) {
if ((msb == 63) && (lsb == 0)) return x;
......@@ -158,7 +160,7 @@ inline uint64_t ExtractUnsignedBitfield64(int msb, int lsb, uint64_t x) {
}
inline int64_t ExtractSignedBitfield64(int msb, int lsb, int64_t x) {
inline int64_t ExtractSignedBitfield64(int msb, int lsb, U64 x) {
uint64_t temp = ExtractUnsignedBitfield64(msb, lsb, x);
// If the highest extracted bit is set, sign extend.
if ((temp >> (msb - lsb)) == 1) {
......@@ -169,7 +171,7 @@ inline int64_t ExtractSignedBitfield64(int msb, int lsb, int64_t x) {
return result;
}
inline int32_t ExtractSignedBitfield32(int msb, int lsb, int32_t x) {
inline int32_t ExtractSignedBitfield32(int msb, int lsb, U32 x) {
uint32_t temp = TruncateToUint32(ExtractSignedBitfield64(msb, lsb, x));
int32_t result;
memcpy(&result, &temp, sizeof(result));
......@@ -207,7 +209,15 @@ inline int32_t BITS32H(int64_t value) {
#define SBITS(obj, st, fn) ((long)(BITS(obj, st, fn) | ((long)BIT(obj, fn) * ~SUB_MASK(fn - st))))
//big little edd
#define DCHECK(X,V, ACTION) \
if (X == V) { \
ACTION \
}
#define CHECK(X,V, ACTION) \
if (X != V) { \
ACTION \
}
#endif //SANDHOOK_BASE_H
......@@ -19,7 +19,7 @@ namespace SandHook {
template <typename Raw>
class CodeRelocate {
virtual bool relocate(ADDR toPc, CodeRelocateCallback<Raw>& callback) throw(ErrorCodeException) = 0;
virtual bool relocate(Addr toPc, CodeRelocateCallback<Raw>& callback) throw(ErrorCodeException) = 0;
};
}
......
......@@ -15,12 +15,12 @@ namespace SandHook {
class InstVisitor {
public:
virtual bool visit(Instruction<void>* inst, ADDR offset, ADDR length) = 0;
virtual bool visit(Instruction<void>* inst, Addr offset, Addr length) = 0;
};
class InstDecoder {
public:
virtual void decode(void* codeStart, ADDR codeLen, InstVisitor* visitor) = 0;
virtual void decode(void* codeStart, Addr codeLen, InstVisitor* visitor) = 0;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment