Skip to content

Commit d23861c

Browse files
authored
upgrades bap to LLVM 11 (#1229)
There were a few breaking changes so we have stick with if/else macros. Mostly the the COFF loader is affected. Note, I was using llvm-11 from the http://apt.llvm.org/bionic/ source repository, which could be broken as it is missing libraries for static linking, so in order to use this pre-built binaries you have to use the dynamic linking mode, e.g., ``` ./configure-omake --with-llvm-version=11 \ --with-llvm-config=llvm-config-11 --disable-llvm-static ``` I hope this will go away with an official build of llvm.
1 parent 2faaa42 commit d23861c

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

lib/bap_llvm/llvm_coff_loader.hpp

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,26 @@ const coff_section* get_coff_section(const coff_obj &obj, const SectionRef &sec)
4646
error_or<int> section_number(const coff_obj &obj, const SymbolRef &sym);
4747
error_or<uint64_t> symbol_value(const coff_obj &obj, const SymbolRef &sym);
4848

49+
#if LLVM_VERSION_MAJOR >= 11
50+
const coff_section * get_coff_section(const coff_obj &obj, std::size_t index) {
51+
if (index != COFF::IMAGE_SYM_UNDEFINED) {
52+
auto sec = obj.getSection(index);
53+
return sec ? *sec : nullptr;
54+
} else {
55+
return nullptr;
56+
}
57+
}
58+
#else
4959
const coff_section * get_coff_section(const coff_obj &obj, std::size_t index) {
5060
const coff_section *sec = nullptr;
5161
bool fail = (index == COFF::IMAGE_SYM_UNDEFINED) || obj.getSection(index, sec);
5262
if (fail) return nullptr;
5363
else return sec;
5464
}
5565

66+
#endif
67+
68+
5669
void emit_base_address(const coff_obj &obj, ogre_doc &s) {
5770
s.entry("llvm:base-address") << obj.getImageBase();
5871
}
@@ -132,6 +145,17 @@ void emit_sections(const coff_obj &obj, ogre_doc &s) {
132145
emit_section(*get_coff_section(obj, sec), base, is_rel, s);
133146
}
134147

148+
#if LLVM_VERSION_MAJOR >= 11
149+
uint64_t get_symbol_value(const SymbolRef &sym) {
150+
auto value = sym.getValue();
151+
return value ? *value : 0;
152+
}
153+
#else
154+
uint64_t get_symbol_value(const SymbolRef &sym) {
155+
return sym.getValue();
156+
}
157+
#endif
158+
135159
void emit_symbols(const coff_obj &obj, ogre_doc &s) {
136160
for (auto sized_sym : prim::get_symbols_sizes(obj)) {
137161
auto sym = sized_sym.first;
@@ -144,7 +168,7 @@ void emit_symbols(const coff_obj &obj, ogre_doc &s) {
144168
<< *addr
145169
<< sized_sym.second
146170
<< *offs
147-
<< sym.getValue();
171+
<< get_symbol_value(sym);
148172
if (*type == SymbolRef::ST_Function)
149173
s.entry("llvm:code-entry") << *name << *offs << sized_sym.second;
150174
}
@@ -238,12 +262,26 @@ void emit_exported_symbols(const coff_obj &obj, exports &syms, ogre_doc &s) {
238262
}
239263
}
240264

265+
#if LLVM_VERSION_MAJOR >= 11
266+
const data_directory *get_export_table(const coff_obj &obj) {
267+
return obj.getDataDirectory(COFF::EXPORT_TABLE);
268+
}
269+
#else
270+
const data_directory *get_export_table(const coff_obj &obj) {
271+
const data_directory *data_entry;
272+
if (obj.getDataDirectory(COFF::EXPORT_TABLE, data_entry)) {
273+
return nullptr;
274+
} else {
275+
return data_entry;
276+
}
277+
}
278+
#endif // llvm >= 11
279+
241280
void emit_exported_symbols(const coff_obj &obj, ogre_doc &s) {
242281

243-
const data_directory *data_entry;
244282
uintptr_t ptr = 0;
245-
246-
if (obj.getDataDirectory(COFF::EXPORT_TABLE, data_entry)) return;
283+
const data_directory *data_entry = get_export_table(obj);
284+
if (!data_entry) return;
247285
uint32_t export_table_rva = data_entry->RelativeVirtualAddress;
248286
if (!export_table_rva) return;
249287
if (obj.getRvaPtr(export_table_rva, ptr)) return;
@@ -295,12 +333,13 @@ error_or<uint64_t> symbol_file_offset(const coff_obj &obj, const SymbolRef &sym)
295333
num == COFF::IMAGE_SYM_DEBUG)
296334
return success(coff_sym.getValue());
297335

298-
const coff_section *coff_sec;
299-
if (auto er = obj.getSection(num, coff_sec)) {
300-
return failure(er.message());
336+
const coff_section *coff_sec = get_coff_section(obj, num);
337+
if (coff_sec) {
338+
uint64_t off = coff_sec->PointerToRawData + coff_sym.getValue();
339+
return success(off);
340+
} else {
341+
return failure("failed to get the section");
301342
}
302-
uint64_t off = coff_sec->PointerToRawData + coff_sym.getValue();
303-
return success(off);
304343
}
305344
} // namespace coff_loader
306345

lib/bap_llvm/llvm_disasm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ class llvm_disassembler : public disassembler_interface {
506506

507507
void init_prefixes() {
508508
for (std::size_t i = 0; i < ins_info->getNumOpcodes(); i++) {
509-
if (ends_with(ins_info->getName(i), "_PREFIX")) {
509+
if (ends_with(std::string(ins_info->getName(i)), "_PREFIX")) {
510510
prefixes.push_back(i);
511511
}
512512
}

0 commit comments

Comments
 (0)