Commit 1e554746 authored by swift_gan's avatar swift_gan Committed by swift_gan

tweak label

parent 84178e6e
//
// Created by swift on 2019/5/10.
//
#ifndef SANDHOOK_NH_DATA_H
#define SANDHOOK_NH_DATA_H
#include "unit.h"
namespace SandHook {
namespace Asm {
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:
Data32(U32 raw) : Data(raw) {}
};
class Data64 : public Data<U64> {
public:
Data64(U64 raw) : Data(raw) {}
};
}
}
#endif //SANDHOOK_NH_DATA_H
...@@ -5,8 +5,7 @@ ...@@ -5,8 +5,7 @@
#ifndef SANDHOOK_INSTRUCTION_H #ifndef SANDHOOK_INSTRUCTION_H
#define SANDHOOK_INSTRUCTION_H #define SANDHOOK_INSTRUCTION_H
#include <malloc.h> #include "unit.h"
#include "../includes/base.h"
//aarch64 //aarch64
typedef U32 InstA64; typedef U32 InstA64;
...@@ -36,60 +35,6 @@ if (COND) { \ ...@@ -36,60 +35,6 @@ if (COND) { \
namespace SandHook { namespace SandHook {
namespace Asm { namespace Asm {
template <typename Raw>
class Unit {
public:
Unit() {
if (unitType() != Void) {
raw = reinterpret_cast<Raw *>(malloc(size()));
memset(raw, 0, size());
auto_alloc = true;
}
}
Unit<Raw>(Raw *raw) : raw(raw) {}
Unit<Raw>(Raw raw) {
Unit();
*this->raw = raw;
}
virtual void* getPC() {
return auto_alloc ? nullptr : raw;
}
inline Raw* get() const {
return raw;
}
inline void set(Raw raw) const {
*this->raw = raw;
}
inline void copy(void* dest) {
memcpy(dest, getPC(), size());
}
virtual UnitType unitType() {
return UnitType::Unkown;
};
virtual U32 size() {
return sizeof(Raw);
}
virtual ~Unit() {
if (auto_alloc) {
free(raw);
}
}
private:
Raw* raw;
bool auto_alloc = false;
};
template <typename Inst> template <typename Inst>
class Instruction : public Unit<Inst> { class Instruction : public Unit<Inst> {
public: public:
...@@ -130,41 +75,6 @@ namespace SandHook { ...@@ -130,41 +75,6 @@ namespace SandHook {
bool valid = true; bool valid = true;
}; };
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:
Data32(U32 raw) : Data(raw) {}
};
class Data64 : public Data<U64> {
public:
Data64(U64 raw) : Data(raw) {}
};
class Label : public Unit<None> {
public:
Label() {}
inline UnitType unitType() override {
return UnitType::Label;
}
U32 size() override {
return 0;
}
};
class Void : public Unit<None> { class Void : public Unit<None> {
public: public:
Void(U32 size) : size_(size) {} Void(U32 size) : size_(size) {}
......
//
// Created by swift on 2019/5/10.
//
#ifndef SANDHOOK_NH_LABEL_H
#define SANDHOOK_NH_LABEL_H
#include "unit.h"
#include <list>
namespace SandHook {
namespace Asm {
virtual class LabelBinder {
public:
virtual void bindLabel(void* pc) = 0;
};
class Label : public Unit<None> {
public:
Label() {}
Label(void *pc) : pc(pc) {}
inline UnitType unitType() override {
return UnitType::Label;
}
inline U32 size() override {
return 0;
}
inline void setPC(void* pc) {
this->pc = pc;
}
inline void *getPC() override {
return pc;
}
inline void addBinder(LabelBinder* binder) {
binders.push_back(binder);
}
inline void removeBinder(LabelBinder* binder) {
binders.push_back(binder);
}
inline void bindLabel(void* pc) {
setPC(pc);
std::list<LabelBinder*>::iterator binder;
for(binder = binders.begin();binder != binders.end(); ++binder) {
(*binder)->bindLabel(pc);
}
}
private:
void* pc;
std::list<LabelBinder*> binders = std::list();
};
}
}
#endif //SANDHOOK_NH_LABEL_H
//
// Created by swift on 2019/5/10.
//
#ifndef SANDHOOK_NH_UNIT_H
#define SANDHOOK_NH_UNIT_H
#include <malloc.h>
#include "../includes/base.h"
namespace SandHook {
namespace Asm {
template <typename Raw>
class Unit {
public:
Unit() {
if (unitType() != Void) {
raw = reinterpret_cast<Raw *>(malloc(size()));
memset(raw, 0, size());
auto_alloc = true;
}
}
Unit<Raw>(Raw *raw) : raw(raw) {}
Unit<Raw>(Raw raw) {
Unit();
*this->raw = raw;
}
virtual void* getPC() {
return auto_alloc ? nullptr : raw;
}
inline Raw* get() const {
return raw;
}
inline void set(Raw raw) const {
*this->raw = raw;
}
inline void copy(void* dest) {
memcpy(dest, getPC(), size());
}
virtual UnitType unitType() {
return UnitType::Unkown;
};
virtual U32 size() {
return sizeof(Raw);
}
virtual ~Unit() {
if (auto_alloc) {
free(raw);
}
}
private:
Raw* raw;
bool auto_alloc = false;
};
}
}
#endif //SANDHOOK_NH_UNIT_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