Commit 30c986ac authored by swift_gan's avatar swift_gan Committed by swift_gan

tweak code

parent d3ff754d
......@@ -23,6 +23,9 @@ add_library( # Sets the name of the library.
src/main/cpp/archs/arm64/register/register_a64.cpp
src/main/cpp/archs/arm64/register/register_list_a64.cpp
src/main/cpp/archs/arm64/decoder/decoder_arm64.cpp
src/main/cpp/relocate/code_relocate.cpp
src/main/cpp/elf/elf.cpp
src/main/cpp/assembler/assembler.cpp
)
......
......@@ -9,7 +9,7 @@
using namespace SandHook::Asm;
template<typename InstStruct>
U8 InstructionA64<InstStruct>::size() {
U32 InstructionA64<InstStruct>::size() {
return sizeof(InstA64);
}
......
......@@ -42,7 +42,7 @@ namespace SandHook {
return raw & *(this->get());
}
U8 size() override;
U32 size() override;
static inline U32 extend32(unsigned int bits, U32 value) {
return value << (32 - bits);
......
......@@ -29,10 +29,12 @@ namespace SandHook {
public:
Unit() {
raw = reinterpret_cast<Raw*>(malloc(size()));
if (unitType() != Void) {
raw = reinterpret_cast<Raw *>(malloc(size()));
memset(raw, 0, size());
auto_alloc = true;
}
}
Unit<Raw>(Raw *raw) : raw(raw) {}
......@@ -57,7 +59,11 @@ namespace SandHook {
memcpy(dest, getPC(), size());
}
virtual U8 size() {
virtual UnitType unitType() {
return UnitType::Unkown;
};
virtual U32 size() {
return sizeof(Raw);
}
......@@ -80,6 +86,10 @@ namespace SandHook {
Instruction(Inst *inst) : Unit<Inst>(inst) {}
UnitType unitType() override {
return UnitType::Inst;
};
virtual InstType instType() {
return unkownInst;
}
......@@ -101,19 +111,51 @@ namespace SandHook {
virtual void assembler() {}
};
class Data16 : public Unit<U16> {
template <typename DType>
class Data : public Unit<DType> {
public:
Data(DType raw) : Unit<DType>(raw) {}
inline UnitType unitType() override {
return UnitType::Data;
};
};
class Data16 : public Data<U16> {
public:
Data16(U16 raw) : Data(raw) {}
};
class Data32 : public Data<U32> {
public:
Data16(U16 raw) : Unit(raw) {}
Data32(U32 raw) : Data(raw) {}
};
class Data32 : public Unit<U32> {
class Data64 : public Data<U64> {
public:
Data32(U32 raw) : Unit(raw) {}
Data64(U64 raw) : Data(raw) {}
};
class Data64 : public Unit<U64> {
class Label : public Unit<None> {
public:
Data64(U64 raw) : Unit(raw) {}
Label() {}
inline UnitType unitType() override {
return UnitType::Label;
}
U32 size() override {
return 0;
}
};
class Void : public Unit<None> {
public:
Void(U32 size) : size_(size) {}
U32 size() override {
return size_;
}
private:
U32 size_;
};
}
......
//
// Created by swift on 2019/5/10.
//
//
// Created by swift on 2019/5/10.
//
#include "elf.h"
......@@ -35,6 +35,14 @@ enum Arch {
unkownArch
};
enum UnitType {
Inst,
Data,
Label,
Void,
Unkown
};
enum InstType {
A32,
thumb16,
......@@ -61,6 +69,9 @@ struct Unsigned<64> {
typedef U64 type;
};
class None {};
template <typename T>
T AlignDown(T pointer,
typename Unsigned<sizeof(T) * BITS_OF_BYTE>::type alignment) {
......
//
// Created by swift on 2019/5/10.
//
#ifndef SANDHOOK_NH_CODE_RELOCATE_H
#define SANDHOOK_NH_CODE_RELOCATE_H
#include "exception.h"
#include "../asm/instruction.h"
namespace SandHook {
namespace Asm {
template <typename Raw>
class CodeRelocateCallback {
public:
virtual bool result(Unit<Raw>* unit, bool end) throw(ErrorCodeException) = 0;
};
template <typename Raw>
class CodeRelocate {
virtual bool relocate(ADDR toPc, CodeRelocateCallback<Raw>& callback) throw(ErrorCodeException) = 0;
};
}
}
#endif //SANDHOOK_NH_CODE_RELOCATE_H
//
// Created by swift on 2019/5/10.
//
#ifndef SANDHOOK_NH_ELF_H
#define SANDHOOK_NH_ELF_H
#endif //SANDHOOK_NH_ELF_H
//
// Created by swift on 2019/5/10.
//
#include "exception"
#ifndef SANDHOOK_NH_EXCEPTION_H
#define SANDHOOK_NH_EXCEPTION_H
namespace SandHook {
namespace Asm {
class ErrorCodeException : public std::exception {
public:
ErrorCodeException(const char *what_) : what_(what_) {}
const char *what() const noexcept override {
return what_;
}
private:
const char *what_;
};
}
}
#endif //SANDHOOK_NH_EXCEPTION_H
//
// Created by swift on 2019/5/10.
//
#include "code_relocate.h"
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