Commit 2d5579fd authored by swift_gan's avatar swift_gan Committed by swift_gan

init code relocate

parent 8cabbbda
...@@ -8,11 +8,27 @@ SandHook::Assembler::AssemblerA64::AssemblerA64(CodeBuffer* codeBuffer) { ...@@ -8,11 +8,27 @@ SandHook::Assembler::AssemblerA64::AssemblerA64(CodeBuffer* codeBuffer) {
codeContainer.setCodeBuffer(codeBuffer); codeContainer.setCodeBuffer(codeBuffer);
} }
void *SandHook::Assembler::AssemblerA64::getPC() {
return reinterpret_cast<void *>(codeContainer.curPc);
}
void *SandHook::Assembler::AssemblerA64::getStartPC() {
return reinterpret_cast<void *>(codeContainer.startPc);
}
void SandHook::Assembler::AssemblerA64::allocBufferFirst(U32 size) {
codeContainer.allocBufferFirst(size);
}
void *SandHook::Assembler::AssemblerA64::finish() { void *SandHook::Assembler::AssemblerA64::finish() {
codeContainer.commit(); codeContainer.commit();
return reinterpret_cast<void *>(codeContainer.startPc); return reinterpret_cast<void *>(codeContainer.startPc);
} }
void SandHook::Assembler::AssemblerA64::Emit(Unit<Base> *unit) {
codeContainer.append(unit);
}
void void
SandHook::Assembler::AssemblerA64::MoveWide(RegisterA64 &rd, INST_A64(MOV_WIDE)::OP op, U64 imme, SandHook::Assembler::AssemblerA64::MoveWide(RegisterA64 &rd, INST_A64(MOV_WIDE)::OP op, U64 imme,
INST_A64(MOV_WIDE)::Shift shift) { INST_A64(MOV_WIDE)::Shift shift) {
...@@ -54,14 +70,3 @@ void SandHook::Assembler::AssemblerA64::Movn(RegisterA64 &rd, U64 imme, ...@@ -54,14 +70,3 @@ void SandHook::Assembler::AssemblerA64::Movn(RegisterA64 &rd, U64 imme,
MoveWide(rd, INST_A64(MOV_WIDE)::MOV_WideOp_N, imme, shift); MoveWide(rd, INST_A64(MOV_WIDE)::MOV_WideOp_N, imme, shift);
} }
void SandHook::Assembler::AssemblerA64::Emit(Unit<Base> &unit) {
codeContainer.append(&unit);
}
void *SandHook::Assembler::AssemblerA64::getPC() {
return reinterpret_cast<void *>(codeContainer.curPc);
}
void *SandHook::Assembler::AssemblerA64::getStartPC() {
return reinterpret_cast<void *>(codeContainer.startPc);
}
...@@ -16,12 +16,14 @@ namespace SandHook { ...@@ -16,12 +16,14 @@ namespace SandHook {
public: public:
AssemblerA64(CodeBuffer* codeBuffer); AssemblerA64(CodeBuffer* codeBuffer);
void allocBufferFirst(U32 size);
void* getStartPC(); void* getStartPC();
void* getPC(); void* getPC();
void* finish(); void* finish();
void Emit(Unit<Base>& unit); void Emit(Unit<Base>* unit);
void MoveWide(RegisterA64& rd, INST_A64(MOV_WIDE)::OP op, U64 imme, INST_A64(MOV_WIDE)::Shift shift); void MoveWide(RegisterA64& rd, INST_A64(MOV_WIDE)::OP op, U64 imme, INST_A64(MOV_WIDE)::Shift shift);
......
...@@ -6,15 +6,56 @@ ...@@ -6,15 +6,56 @@
#define __ assemblerA64-> #define __ assemblerA64->
#define IMPL_RELOCATE(X) void CodeRelocateA64::relocate_##X (INST_A64(X)* inst, void* toPc) throw(ErrorCodeException)
#define CASE(X) \
case InstCodeA64::X: \
relocate_##X(reinterpret_cast<INST_A64(X)*>(instruction), toPc); \
break;
CodeRelocateA64::CodeRelocateA64(AssemblerA64 &assembler) : CodeRelocate(assembler.codeContainer) { CodeRelocateA64::CodeRelocateA64(AssemblerA64 &assembler) : CodeRelocate(assembler.codeContainer) {
this->assemblerA64 = &assembler; this->assemblerA64 = &assembler;
} }
bool CodeRelocateA64::relocate(Instruction<Base> *instruction, void *toPc) throw(ErrorCodeException) { void CodeRelocateA64::relocate(void *startPc, void *toPc, Addr len) throw(ErrorCodeException) {
__ finish(); __ finish();
return false;
} }
bool CodeRelocateA64::relocate(void *startPc, void *toPc, Addr len) throw(ErrorCodeException) { void CodeRelocateA64::relocate(Instruction<Base> *instruction, void *toPc) throw(ErrorCodeException) {
return false; if (!instruction->pcRelate()) {
__ Emit(instruction);
return;
}
switch (instruction->instCode()) {
CASE(B_BL)
CASE(B_COND)
CASE(TBZ_TBNZ)
CASE(CBZ_CBNZ)
CASE(LDR_LIT)
CASE(ADR_ADRP)
}
}
IMPL_RELOCATE(B_BL) {
}
IMPL_RELOCATE(B_COND) {
}
IMPL_RELOCATE(TBZ_TBNZ) {
}
IMPL_RELOCATE(CBZ_CBNZ) {
}
IMPL_RELOCATE(LDR_LIT) {
}
IMPL_RELOCATE(ADR_ADRP) {
} }
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
using namespace SandHook::Assembler; using namespace SandHook::Assembler;
using namespace SandHook::Decoder; using namespace SandHook::Decoder;
#define DEFINE_RELOCATE(X) void relocate_##X (INST_A64(X)* inst, void* toPc) throw(ErrorCodeException);
namespace SandHook { namespace SandHook {
namespace Asm { namespace Asm {
...@@ -18,9 +20,22 @@ namespace SandHook { ...@@ -18,9 +20,22 @@ namespace SandHook {
public: public:
CodeRelocateA64(AssemblerA64 &assembler); CodeRelocateA64(AssemblerA64 &assembler);
bool relocate(Instruction<Base> *instruction, void *toPc) throw(ErrorCodeException) override; void relocate(Instruction<Base> *instruction, void *toPc) throw(ErrorCodeException) override;
void relocate(void *startPc, void *toPc, Addr len) throw(ErrorCodeException) override;
DEFINE_RELOCATE(B_BL)
DEFINE_RELOCATE(B_COND)
DEFINE_RELOCATE(TBZ_TBNZ)
DEFINE_RELOCATE(CBZ_CBNZ)
DEFINE_RELOCATE(LDR_LIT)
DEFINE_RELOCATE(ADR_ADRP)
bool relocate(void *startPc, void *toPc, Addr len) throw(ErrorCodeException) override;
private: private:
AssemblerA64* assemblerA64; AssemblerA64* assemblerA64;
......
...@@ -21,8 +21,8 @@ namespace SandHook { ...@@ -21,8 +21,8 @@ namespace SandHook {
CodeRelocate(CodeContainer &codeContainer) : codeContainer(&codeContainer) {} CodeRelocate(CodeContainer &codeContainer) : codeContainer(&codeContainer) {}
virtual bool relocate(Instruction<Base> *instruction, void* toPc) throw(ErrorCodeException) = 0; virtual void relocate(Instruction<Base> *instruction, void* toPc) throw(ErrorCodeException) = 0;
virtual bool relocate(void* startPc, void* toPc, Addr len) throw(ErrorCodeException) = 0; virtual void relocate(void* startPc, void* toPc, Addr len) throw(ErrorCodeException) = 0;
private: private:
CodeContainer* codeContainer; CodeContainer* codeContainer;
......
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