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

done b bl

parent 04e206a0
...@@ -22,12 +22,6 @@ A64_INST_PC_REL<Inst>::A64_INST_PC_REL(Inst *inst):InstructionA64<Inst>(inst) { ...@@ -22,12 +22,6 @@ A64_INST_PC_REL<Inst>::A64_INST_PC_REL(Inst *inst):InstructionA64<Inst>(inst) {
template<typename Inst> template<typename Inst>
A64_INST_PC_REL<Inst>::A64_INST_PC_REL() {} A64_INST_PC_REL<Inst>::A64_INST_PC_REL() {}
template<typename Inst>
ADDR A64_INST_PC_REL<Inst>::getImmPCOffset() {
return static_cast<ADDR>(this->getImmBranch() * this->size());
}
template<typename Inst> template<typename Inst>
ADDR A64_INST_PC_REL<Inst>::getImmPCOffsetTarget() { ADDR A64_INST_PC_REL<Inst>::getImmPCOffsetTarget() {
return reinterpret_cast<ADDR>(this->getImmPCOffset() + (ADDR) this->getPC()); return reinterpret_cast<ADDR>(this->getImmPCOffset() + (ADDR) this->getPC());
...@@ -41,16 +35,10 @@ A64_ADR_ADRP::A64_ADR_ADRP(aarch64_adr_adrp *inst) : A64_INST_PC_REL(inst) { ...@@ -41,16 +35,10 @@ A64_ADR_ADRP::A64_ADR_ADRP(aarch64_adr_adrp *inst) : A64_INST_PC_REL(inst) {
decode(inst); decode(inst);
} }
int A64_ADR_ADRP::getImmPCRel() {
U32 hi = static_cast<U32>(get()->immhi);
U32 lo = get()->immlo;
U32 offset = (hi << IMM_LO_W) | lo;
int width = IMM_HI_W + IMM_LO_W;
return ExtractSignedBitfield32(width - 1, 0, offset);
}
ADDR A64_ADR_ADRP::getImmPCOffset() { ADDR A64_ADR_ADRP::getImmPCOffset() {
ADDR offset = static_cast<ADDR>(getImmPCRel()); U32 hi = get()->immhi;
U32 lo = get()->immlo;
ADDR offset = signExtend64(12, COMBINE(hi, lo, IMM_LO_W));
if (isADRP()) { if (isADRP()) {
offset *= PAGE_SIZE; offset *= PAGE_SIZE;
} }
...@@ -58,12 +46,12 @@ ADDR A64_ADR_ADRP::getImmPCOffset() { ...@@ -58,12 +46,12 @@ ADDR A64_ADR_ADRP::getImmPCOffset() {
} }
ADDR A64_ADR_ADRP::getImmPCOffsetTarget() { ADDR A64_ADR_ADRP::getImmPCOffsetTarget() {
aarch64_adr_adrp* base = AlignDown(get(), PAGE_SIZE); void * base = AlignDown(getPC(), PAGE_SIZE);
return reinterpret_cast<ADDR>(getImmPCOffset() + (ADDR) base); return reinterpret_cast<ADDR>(getImmPCOffset() + (ADDR) base);
} }
int A64_ADR_ADRP::getImm() { int A64_ADR_ADRP::getImm() {
return getImmPCRel(); 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, int imme) : op(op), rd(rd),
...@@ -123,21 +111,21 @@ A64_B_BL::A64_B_BL(aarch64_b_bl *inst) : A64_INST_PC_REL(inst) { ...@@ -123,21 +111,21 @@ A64_B_BL::A64_B_BL(aarch64_b_bl *inst) : A64_INST_PC_REL(inst) {
decode(inst); decode(inst);
} }
A64_B_BL::A64_B_BL(A64_B_BL::OP op, U32 imme) : op(op), imme(imme) { A64_B_BL::A64_B_BL(A64_B_BL::OP op, ADDR offset) : op(op), offset(offset) {
assembler(); assembler();
} }
ADDR A64_B_BL::getImmPCOffset() {
return signExtend64(26 + 2, COMBINE(get()->imm26, 0b00, 2));
}
void A64_B_BL::decode(aarch64_b_bl *inst) { void A64_B_BL::decode(aarch64_b_bl *inst) {
op = static_cast<OP>(inst->op); op = static_cast<OP>(inst->op);
imme = inst->imm26; offset = getImmPCOffset();
} }
void A64_B_BL::assembler() { void A64_B_BL::assembler() {
get()->opcode = B_BL_OPCODE; get()->opcode = B_BL_OPCODE;
get()->op = op; get()->op = op;
get()->imm26 = imme; get()->imm26 = TruncateToUint32(offset);
}
int A64_B_BL::getImmBranch() {
} }
...@@ -29,12 +29,12 @@ namespace SandHook { ...@@ -29,12 +29,12 @@ namespace SandHook {
U8 size() override; U8 size() override;
static inline U32 SignExtend64(unsigned int bits, U64 value) { static inline U32 signExtend64(unsigned int bits, U64 value) {
U32 C = (U32) ((-1) << (bits - (U32) 1)); // NOLINT U32 C = (U32) ((-1) << (bits - (U32) 1));
return (value + C) ^ C; return static_cast<U32>((value + C) ^ C);
} }
bool IsPCRelAddressing() { bool isPCRelAddressing() {
return mask(PCRelAddressingFMask) == PCRelAddressingFixed; return mask(PCRelAddressingFMask) == PCRelAddressingFixed;
} }
...@@ -46,11 +46,6 @@ namespace SandHook { ...@@ -46,11 +46,6 @@ namespace SandHook {
return arm64; return arm64;
} }
virtual int getImmBranch() {
//TODO
return 0;
}
}; };
...@@ -62,7 +57,7 @@ namespace SandHook { ...@@ -62,7 +57,7 @@ namespace SandHook {
A64_INST_PC_REL(Inst *inst); A64_INST_PC_REL(Inst *inst);
virtual ADDR getImmPCOffset(); virtual ADDR getImmPCOffset() = 0;
virtual ADDR getImmPCOffsetTarget(); virtual ADDR getImmPCOffsetTarget();
...@@ -92,8 +87,6 @@ namespace SandHook { ...@@ -92,8 +87,6 @@ namespace SandHook {
return get()->op == OP::ADRP; return get()->op == OP::ADRP;
} }
int getImmPCRel();
ADDR getImmPCOffset() override; ADDR getImmPCOffset() override;
ADDR getImmPCOffsetTarget() override; ADDR getImmPCOffsetTarget() override;
...@@ -187,13 +180,21 @@ namespace SandHook { ...@@ -187,13 +180,21 @@ namespace SandHook {
A64_B_BL(aarch64_b_bl *inst); A64_B_BL(aarch64_b_bl *inst);
A64_B_BL(OP op, U32 imme); A64_B_BL(OP op, ADDR offset);
inline ADDR getOffset() {
return offset;
}
inline OP getOP() {
return op;
}
inline U32 instCode() override { inline U32 instCode() override {
return op == B ? UnconditionalBranchOp::B : UnconditionalBranchOp ::BL; return op == B ? UnconditionalBranchOp::B : UnconditionalBranchOp ::BL;
}; };
int getImmBranch() override; ADDR getImmPCOffset() override;
void decode(aarch64_b_bl *decode) override; void decode(aarch64_b_bl *decode) override;
...@@ -202,7 +203,7 @@ namespace SandHook { ...@@ -202,7 +203,7 @@ namespace SandHook {
private: private:
OP op; OP op;
U32 imme; ADDR offset;
}; };
} }
......
...@@ -181,6 +181,16 @@ inline int32_t BITS32H(int64_t value) { ...@@ -181,6 +181,16 @@ inline int32_t BITS32H(int64_t value) {
#define LFT(a, b, c) ((a & ((1 << b) - 1)) << c) #define LFT(a, b, c) ((a & ((1 << b) - 1)) << c)
#define RHT(a, b, c) ((a >> c) & ((1 << b) - 1)) #define RHT(a, b, c) ((a >> c) & ((1 << b) - 1))
#define COMBINE(hi, lo, lowide) (hi << lowide) | lo
/* borrow from gdb, refer: binutils-gdb/gdb/arch/arm.h */
#define SUB_MASK(x) ((1L << ((x) + 1)) - 1)
#define BITS(obj, st, fn) (((obj) >> (st)) & SUB_MASK((fn) - (st)))
#define BIT(obj, st) (((obj) >> (st)) & 1)
#define SBITS(obj, st, fn) ((long)(BITS(obj, st, fn) | ((long)BIT(obj, fn) * ~SUB_MASK(fn - st))))
//big little edd //big little edd
......
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