-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.h
83 lines (64 loc) · 1.42 KB
/
loader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef LOADER_H
#define LOADER_H
#include <stdint.h>
#include <string>
#include <vector>
class Binary;
class Section;
class Symbol;
class Symbol {
public:
enum SymbolTable {
SYM_TYPE_UKN = 0,
SYM_TYPE_FUNC = 1
};
Symbol() : type(SYM_TYPE_UKN), name(), addr(0) {}
SymbolTable type;
std::string name;
uint64_t addr;
};
class Section {
public:
enum SectionType {
SEC_TYPE_NONE = 0,
SEC_TYPE_CODE = 1,
SEC_TYPE_DATA = 2
};
Section() : binary(NULL), type(SEC_TYPE_NONE), vma(0), size(0), bytes(NULL) {}
bool contains(uint64_t addr) { return (addr >= vma) && (addr-vma < size); }
Binary *binary;
std::string name;
SectionType type;
uint64_t vma;
uint64_t size;
uint8_t *bytes;
};
// Root class which represents a binary file
class Binary {
public:
enum BinaryType {
BIN_TYPE_AUTO = 0,
BIN_TYPE_ELF = 1,
BIN_TYPE_PE = 2
};
enum BinaryArch {
ARCH_NONE = 0,
ARCH_X86 = 1
};
Binary() : type(BIN_TYPE_AUTO), arch(ARCH_NONE), bits(0), entry(0) {}
Section *get_text_section()
{for(auto &s : sections) if(s.name == ".text") return &s; return NULL;}
std::string filename;
BinaryType type;
std::string type_str;
BinaryArch arch;
std::string arch_str;
// 32 or 64 bit arch
unsigned bits;
uint64_t entry;
std::vector<Section> sections;
std::vector<Symbol> symbols;
};
int load_binary(std::string &fname, Binary *bin, Binary::BinaryType type);
void unload_binary(Binary *bin);
#endif /* LOADER_H */