+ Mod.reset(OD->buildModule(/* withCFG */ false));
+
+ symbols = readSymbols(*o);
+ sections = readSections(*o);
+
+ // for_each(sections.begin(), sections.end(), [](const std::pair<std::string, SectionRef>& i){
+ // std::cout << "Section: " << i.first << std::endl;
+ // });
+}
+
+
+
+std::vector<std::string>
+::Binary::getSymbols() {
+ error_code ec;
+ SectionRef r = sections[".text"];
+ std::vector<std::string> result;
+ for_each(symbols.begin(), symbols.end(), [&](const std::pair<std::string, SymbolRef>& i) {
+ bool contains;
+ SymbolRef::Type t;
+ if (!error(r.containsSymbol(i.second, contains)) && contains) {
+ i.second.getType(t);
+ if (SymbolRef::ST_Function == t)
+ result.push_back(i.first);
+ }
+ });
+ return result;
+}
+
+void ::Binary::for_each_instruction(const std::string& function,
+ std::function<void (long, std::string, std::string)> callback) {
+ StringRef bytes;
+ uint64_t base_address, address, ssize, size(0), index, end;
+ StringRefMemoryObject memoryObject("");
+
+ if (symbols.end() != symbols.find(function)) {
+ SymbolRef ref;
+ section_iterator sec(o->section_begin());
+
+ ref = symbols.at(function);
+ if (error(ref.getSection(sec))) return;
+ if (error(ref.getAddress(address))) return;
+ if (address == UnknownAddressOrSize) return;
+ if (error(ref.getSize(ssize))) return;
+ if (error(sec->getAddress(base_address))) return;
+ if (error(sec->getContents(bytes))) return;
+ memoryObject = bytes;
+
+ }
+ else if (sections.end() != sections.find(function)) {
+ SectionRef sref = sections.at(function);
+ if (error(sref.getAddress(address))) return;
+ if (address == UnknownAddressOrSize) return;
+ if (error(sref.getSize(ssize))) return;
+ if (error(sref.getContents(bytes))) return;
+ base_address = address;
+ memoryObject = bytes;
+ }
+
+
+ // outs() << "Start for_each_instruction " << function << "\n";
+
+
+ for (end = address + ssize - base_address, index = address - base_address; index < end; index += size) {
+ MCInst Inst;
+
+ if (DisAsm->getInstruction(Inst, size, memoryObject, index,
+ nulls(), nulls())) {
+ std::string buf;
+ llvm::raw_string_ostream s(buf);
+ IP->printInst(&Inst, s, "");
+
+ if (index + size < bytes.str().length())
+ callback(base_address + index, bytes.str().substr(index, size), s.str());
+
+ } else {
+ errs() << "warning: invalid instruction encoding\n";
+ if (size == 0)
+ size = 1; // skip illegible bytes
+ }
+ }
+ // outs() << "End for_each_instruction\n";
+