/
llvm_binary.hpp
372 lines (315 loc) · 10.8 KB
/
llvm_binary.hpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#ifndef LLVM_BINARY_HPP
#define LLVM_BINARY_HPP
#include <memory>
#include <numeric>
#include <vector>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <tuple>
#include <cstdlib>
#include <llvm/Object/ELFObjectFile.h>
#include <llvm/Object/COFF.h>
#include <llvm/Object/MachO.h>
#include <llvm/Object/Archive.h>
#include <llvm/Support/Compiler.h>
#include <llvm/Config/llvm-config.h>
#include "llvm_binary_34.hpp"
#include "llvm_binary_38_40.hpp"
#include "llvm_error_or.hpp"
#include "llvm_primitives.hpp"
extern "C" {
void bap_notify_error(const char*);
void bap_notify_warning(const char*);
}
void bap_notify_error(const std::string & message) {
bap_notify_error(message.c_str());
}
void bap_notify_warning(const std::string & message) {
bap_notify_warning(message.c_str());
}
namespace seg {
using namespace llvm;
using namespace llvm::object;
struct segment {
std::string name;
int offset;
uint64_t addr;
uint64_t size;
uint64_t vsize;
bool is_readable;
bool is_writable;
bool is_executable;
};
typedef std::vector<segment> segments;
template <typename S>
segment make_segment(const S &s) {
int off = static_cast<int>(s.fileoff);
return segment{s.segname, off, s.vmaddr, s.filesize, s.vmsize,
static_cast<bool>(s.initprot & MachO::VM_PROT_READ),
static_cast<bool>(s.initprot & MachO::VM_PROT_WRITE),
static_cast<bool>(s.initprot & MachO::VM_PROT_EXECUTE)};
}
segment make_segment(const coff_section &s, uint64_t image_base) {
return segment{s.Name,
static_cast<int>(s.PointerToRawData),
static_cast<uint64_t>(s.VirtualAddress + image_base),
static_cast<uint64_t>(s.SizeOfRawData),
static_cast<uint64_t>(s.VirtualSize),
static_cast<bool>((s.Characteristics) &
COFF::IMAGE_SCN_MEM_READ),
static_cast<bool>((s.Characteristics) &
COFF::IMAGE_SCN_MEM_WRITE),
static_cast<bool>((s.Characteristics) &
COFF::IMAGE_SCN_MEM_EXECUTE)};
}
template<typename T>
error_or<segments> read(const ELFObjectFile<T>& obj) {
auto hdrs = prim::elf_program_headers(*obj.getELFFile());
segments s;
auto it = hdrs.begin();
for (int pos = 0; it != hdrs.end(); ++it, ++pos) {
if (it -> p_type == ELF::PT_LOAD) {
std::ostringstream oss;
oss << std::setfill('0') << std::setw(2) << pos;
int off = static_cast<int>(it->p_offset);
s.push_back(segment{oss.str(),
off,
it->p_vaddr,
it->p_filesz,
it->p_memsz,
static_cast<bool>(it->p_flags & ELF::PF_R),
static_cast<bool>(it->p_flags & ELF::PF_W),
static_cast<bool>(it->p_flags & ELF::PF_X)});
}
}
return success(std::move(s));
}
error_or<segments> read(const MachOObjectFile& obj) {
auto cmds = load_commands(obj);
segments s;
for (auto it : cmds) {
if (it.C.cmd == MachO::LoadCommandType::LC_SEGMENT_64)
s.push_back(make_segment(obj.getSegment64LoadCommand(it)));
if (it.C.cmd == MachO::LoadCommandType::LC_SEGMENT)
s.push_back(make_segment(obj.getSegmentLoadCommand(it)));
}
return success(std::move(s));
}
error_or<segments> read(const COFFObjectFile& obj) {
auto image_base = getImageBase(obj);
auto obj_sections = sec::sections_range(obj);
if (auto er = image_base || obj_sections)
return failure(er.message());
segments segs;
for (auto it : *obj_sections) {
const coff_section *s = obj.getCOFFSection(it);
uint64_t c = static_cast<uint64_t>(s->Characteristics);
if ( c & COFF::IMAGE_SCN_CNT_CODE ||
c & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA ||
c & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA )
segs.push_back(make_segment(*s, *image_base));
}
return std::move(success(std::move(segs)) << obj_sections.warnings() << image_base.warnings());
}
} //namespace seg
namespace sym {
using namespace llvm;
using namespace llvm::object;
typedef SymbolRef::Type kind_type;
struct symbol {
std::string name;
bool is_fun;
bool is_debug;
uint64_t addr;
uint64_t size;
};
typedef std::vector<symbol> symbols;
error_or<symbol> make_symbol(const SymbolRef &sym, uint64_t size, error_or<uint64_t> addr) {
auto name = get_name(sym);
auto kind = get_kind(sym);
if (auto er = name || kind || addr) {
name << addr.warnings() << kind.warnings();
info & w = name.warning();
w << "skipping symbol ";
std::string str = " error while extracting ";
if (!name) w << str << "name: ";
if (!addr) w << *name << str << "address: ";
if (!kind) w << *name << " at " << std::hex << *addr << std::hex << str << "kind: ";
w << er.message();
return name;
}
bool is_fun = *kind == SymbolRef::ST_Function;
bool is_dbg = *kind == SymbolRef::ST_Debug;
return std::move(success(symbol{*name, is_fun, is_dbg, *addr, size}) << name.warnings());
}
template <typename T>
error_or<symbols> read(const T &obj) {
auto symbol_sizes = getSymbolSizes(obj);
if (!symbol_sizes) return symbol_sizes;
error_or<symbols> syms = success(symbols());
syms << symbol_sizes.warnings() ;
for (auto it : *symbol_sizes) {
auto sym = make_symbol(it.first, it.second, get_addr(it.first, obj));
if (sym) syms->push_back(*sym);
syms << sym.warnings();
}
return syms;
}
} //namespace sym
namespace sec {
using namespace llvm;
using namespace llvm::object;
struct section {
std::string name;
uint64_t addr;
uint64_t size;
};
typedef std::vector<section> sections;
error_or<section> make_section(const SectionRef &sec) {
auto descr = get_section(sec);
if (!descr) return descr;
std::string name;
uint64_t addr, size;
std::tie(name, addr, size) = *descr;
return std::move(success(section{name, addr, size}) << descr.warnings());
}
section make_section(const coff_section &s, const uint64_t image_base) {
std::string name;
uint64_t addr, size;
std::tie(name, addr, size) = get_section(s);
return section{name, addr + image_base, size};
}
error_or<sections> read(const ObjectFile &obj) {
auto obj_secs = sections_range(obj);
if (!obj_secs) return obj_secs;
error_or<sections> secs = success(sections());
secs << obj_secs.warnings();
for (section_iterator it : *obj_secs) {
auto sec = make_section(*it);
if (!sec) return sec;
secs->push_back(*sec);
secs << sec.warnings();
}
return secs;
}
error_or<sections> read(const COFFObjectFile &obj) {
auto image_base = getImageBase(obj);
auto obj_secs = sections_range(obj);
if (auto er = image_base || obj_secs)
return failure(er.message());
error_or<sections> secs = success(sections());
secs << image_base.warnings() << obj_secs.warnings();
for (section_iterator it : *obj_secs) {
const coff_section *s = getCOFFSection(obj, it);
secs->push_back(make_section(*s, *image_base));
}
return secs;
}
template <typename Sections, typename Segments>
void add_virtuals(Sections &secs, const Segments &segs) {
for (auto seg : segs) {
secs.push_back(section {seg.name, seg.addr, seg.vsize});
}
}
} //namespace sec
namespace img {
using std::move;
using namespace llvm;
using namespace llvm::object;
struct image {
std::string arch;
uint64_t entry;
seg::segments segments;
sym::symbols symbols;
sec::sections sections;
};
std::string image_arch(const ObjectFile& obj) {
return Triple::getArchTypeName(static_cast<Triple::ArchType>(obj.getArch()));
}
template <typename ELFT>
error_or<uint64_t> image_entry(const ELFObjectFile<ELFT>& obj) {
return error_or<uint64_t>(obj.getELFFile()->getHeader()->e_entry);
}
error_or<uint64_t> image_entry(const MachOObjectFile &obj) {
return image_entry_macho(obj);
}
error_or<uint64_t> image_entry(const COFFObjectFile &obj) {
return image_entry_coff(obj);
}
void print_warnings(const std::vector<std::string> &warns) {
if(const char* env_p = std::getenv("BAP_DEBUG")) {
if (std::string(env_p) == "1")
for (auto w : warns)
bap_notify_warning(w);
}
}
template <typename T>
image* create_image(error_or<object::Binary> &binary) {
if (auto ptr = llvm::dyn_cast<T>(binary.get())) {
auto arch = image_arch(*ptr);
auto entry = image_entry(*ptr);
auto segments = seg::read(*ptr);
auto symbols = sym::read(*ptr);
auto sections = sec::read(*ptr);
if (auto er = entry || segments || symbols || sections) {
bap_notify_error(er.message());
return nullptr;
}
binary << entry.warnings() << segments.warnings() << symbols.warnings() << sections.warnings();
print_warnings(binary.warnings());
auto img = (new image
{arch, *entry, move(*segments), move(*symbols), move(*sections)});
sec::add_virtuals(img->sections, img->segments);
return img;
}
bap_notify_error("Unrecognized object format");
return nullptr;
}
image* create_image_elf(error_or<object::Binary> &binary) {
if (isa<ELF32LEObjectFile>(*binary))
return create_image<ELF32LEObjectFile>(binary);
if (isa<ELF32BEObjectFile>(*binary))
return create_image<ELF32BEObjectFile>(binary);
if (isa<ELF64LEObjectFile>(*binary))
return create_image<ELF64LEObjectFile>(binary);
if (isa<ELF64BEObjectFile>(*binary))
return create_image<ELF64BEObjectFile>(binary);
bap_notify_error("Unrecognized ELF format");
return nullptr;
}
image* create_image_obj(error_or<object::Binary> &binary) {
if (binary->isCOFF())
return create_image<COFFObjectFile>(binary);
if (binary->isELF())
return create_image_elf(binary);
if (binary->isMachO())
return create_image<MachOObjectFile>(binary);
bap_notify_error("Unrecognized object format");
return nullptr;
}
image* create_image_arch(error_or<object::Binary> &binary) {
bap_notify_error("Archive loading unimplemented");
return nullptr;
}
image* create(error_or<object::Binary> &binary) {
if (!binary)
return nullptr;
if (isa<Archive>(*binary))
return create_image_arch(binary);
if (isa<ObjectFile>(*binary))
return create_image_obj(binary);
bap_notify_error("Unrecognized binary format");
return nullptr;
}
image* create(const char* data, std::size_t size) {
error_or<object::Binary> bin = get_binary(data, size);
if (!bin) {
bap_notify_error(bin.message());
return nullptr;
}
return create(bin);
}
} //namespace img
#endif //LLVM_BINARY_HPP