- StringRef bytes;
- text_section.getContents(bytes);
- StringRefMemoryObject ref(bytes);
-
- while (remaining_functions.size()) {
- LLVMFunction * current_function = remaining_functions.top();
- remaining_functions.pop();
-
- LOG4CXX_DEBUG(logger, "Handling function " << current_function->getName());
-
- // if ("_start" != current_function->getName())
- // continue;
-
- LLVMBasicBlock * block = new LLVMBasicBlock(current_function->getStartAddress());
- remaining_blocks.push(block);
- blocks.insert(std::make_pair(block->getStartAddress(), block));
-
- while (remaining_blocks.size()) {
- LLVMBasicBlock * current_block = remaining_blocks.top();
- remaining_blocks.pop();
-
- LOG4CXX_DEBUG(logger, "Handling Block starting at " << std::hex << current_block->getStartAddress());
-
- uint64_t inst_size;
- uint64_t base_address;
- text_section.getAddress(base_address);
- uint64_t current_address = current_block->getStartAddress() - base_address;
- while(true) {
- MCInst inst;
- std::string buf;
- llvm::raw_string_ostream s(buf);
-
- if(llvm::MCDisassembler::Success ==
- DisAsm->getInstruction(inst, inst_size, ref, current_address, nulls(), nulls())) {
-
- uint8_t bytes[inst_size+2];
- ref.readBytes(current_address, inst_size, bytes);
- s << '\t';
- for(uint8_t* cur = bytes; cur < bytes + inst_size; ++cur) {
- s.write_hex(*cur);
- s << ' ';
- }
- s << '\t';
-
- IP->printInst(&inst, s, "");
-
- LOG4CXX_DEBUG(logger, std::hex << current_address + base_address << s.str());
-
- uint64_t jmptarget;
- if (MIA->evaluateBranch(inst, current_address, inst_size, jmptarget)) {
- jmptarget += base_address;
- if (!MIA->isIndirectBranch(inst)) {
- if (MIA->isCall(inst)) {
- if (functions.find(jmptarget) == functions.end()) {
- std::stringstream s;
- s << "<Unnamed 0x" << std::hex << jmptarget << ">";
- LLVMFunction * fun = new LLVMFunction(s.str(), jmptarget);
- functions.insert(std::make_pair(jmptarget, fun));
- remaining_functions.push(fun);
- }
- } else {
- if (blocks.find(jmptarget) == blocks.end()) {
- LLVMBasicBlock * block = new LLVMBasicBlock(jmptarget);
- blocks.insert(std::make_pair(block->getStartAddress(), block));
- current_block->setNextBlock(0, block->getStartAddress());
- remaining_blocks.push(block);
- }
- if (MIA->isConditionalBranch(inst)) {
- jmptarget = base_address + current_address + inst_size;
- if (blocks.find(jmptarget) == blocks.end()) {
- LLVMBasicBlock * block = new LLVMBasicBlock(jmptarget);
- blocks.insert(std::make_pair(block->getStartAddress(), block));
- current_block->setNextBlock(1, block->getStartAddress());
- remaining_blocks.push(new LLVMBasicBlock(jmptarget));
- }
- }
- }
- }
- }
- } else {
- inst_size = 0;
- }
-
-
- if (inst_size == 0 || MIA->isTerminator(inst) || MIA->isBranch(inst)) {
- current_block->setEndAddress(current_address + base_address);
- LOG4CXX_DEBUG(logger, "Finished Block at " << std::hex <<
- current_block->getEndAddress());
- break;
- }
- current_address += inst_size;
- }
- }
- LOG4CXX_DEBUG(logger, "Finished function " << current_function->getName());
- }
+ for (Function* function : remaining_functions) {
+ disassembleFunction(function);
+ manager->finishFunction(function);
+ }
+
+ if (binary->isELF()) {
+ uint64_t _entryAddress = entryAddress();
+ LOG4CXX_DEBUG(logger, "Adding entryAddress at: " << std::hex << _entryAddress);
+ std::stringstream s;
+ s << "<_start 0x" << std::hex << _entryAddress << ">";
+
+ disassembleFunctionAt(_entryAddress, s.str());
+ }
+
+ if (!manager->hasFunctions()) {
+ uint64_t text_entry;
+#if defined(LLVM_35)
+ text_section.getAddress(text_entry);
+#elif defined(LLVM_36)
+ text_entry = text_section.getAddress();
+#endif
+ LOG4CXX_INFO(logger, "No Symbols found, starting at the beginning of the text segment");
+ disassembleFunctionAt(text_entry);
+ }
+}
+
+template <>
+uint64_t LLVMDisassembler<COFFT>::entryAddress() {
+ const auto coffobject = dyn_cast<COFFObjectFile>(o);
+ const struct pe32_header* pe32_header;
+ const struct pe32plus_header* pe32plus_header;
+
+ coffobject->getPE32PlusHeader(pe32plus_header);
+
+ if (pe32plus_header) {
+ return pe32plus_header->AddressOfEntryPoint;
+ } else {
+ coffobject->getPE32Header(pe32_header);
+ return pe32_header->AddressOfEntryPoint;
+ }
+}
+
+template<>
+uint64_t LLVMDisassembler<MACHOT>::entryAddress() {
+ // TODO
+ return 0;
+}
+
+template <typename ELFT>
+uint64_t LLVMDisassembler<ELFT>::entryAddress() {
+ const auto elffile = dyn_cast<ELFObjectFile<ELFT>>(o)->getELFFile();
+ const auto * header = elffile->getHeader();
+
+ return header->e_entry;
+}
+
+template <typename ELFT>
+void LLVMDisassembler<ELFT>::splitBlocks(Function* function) {
+ SectionRef text_section = getTextSection();
+ StringRef bytes;
+ text_section.getContents(bytes);
+#if defined(LLVM_35)
+ StringRefMemoryObject ref(bytes);
+#elif defined(LLVM_36)
+ ArrayRef<uint8_t> bytearray(reinterpret_cast<const uint8_t *>(bytes.data()),
+ bytes.size());
+#endif
+
+
+ LOG4CXX_DEBUG(logger, "Splitting Blocks in Function " << function->getName());
+ // Split blocks where jumps are going inside the block
+ for (auto it = function->blocks().begin();
+ it != function->blocks().end();
+ ++it) {
+ BasicBlock * current_block = it->second;
+ if (current_block->getEndAddress() == 0) {
+ LOG4CXX_ERROR(logger, "UNFINISHED BLOCK " << std::hex << current_block->getStartAddress());
+ break;
+ }
+ uint64_t inst_size;
+ uint64_t base_address;
+#if defined(LLVM_35)
+ text_section.getAddress(base_address);
+#elif defined(LLVM_36)
+ base_address = text_section.getAddress();
+#endif
+ uint64_t current_address = current_block->getStartAddress() - base_address;
+ while(current_block->getEndAddress() - base_address > current_address) {
+ MCInst inst;
+ std::string buf;
+ llvm::raw_string_ostream s(buf);
+
+ if(llvm::MCDisassembler::Success ==
+#if defined(LLVM_35)
+ DisAsm->getInstruction(inst, inst_size, ref, current_address, nulls(), nulls())) {
+#elif defined(LLVM_36)
+ DisAsm->getInstruction(inst, inst_size,
+ bytearray.slice(current_address),
+ base_address + current_address,
+ nulls(), nulls())) {
+#endif
+
+ // See if some other block starts here
+ BasicBlock* other = manager->getBasicBlock(current_address
+ + inst_size
+ + base_address);
+
+ // Special case, other block starts here but we are at the end anyway
+ if (other != NULL) {
+ uint64_t endaddress = current_address + inst_size + base_address;
+ if (endaddress != current_block->getEndAddress()) {
+ LOG4CXX_DEBUG(logger, "Shortening block starting at "
+ << std::hex
+ << current_block->getStartAddress()
+ << " now ending at "
+ << other->getStartAddress());
+ function->addBasicBlock(other);
+ current_block->setEndAddress(endaddress);
+ current_block->setNextBlock(0, other->getStartAddress());
+ current_block->setNextBlock(1, 0);
+ }
+ }
+ } else {
+ inst_size = 1;
+ }
+ current_address += inst_size;
+ }
+ }
+}
+
+template<>
+void LLVMDisassembler<COFFT>::readDynamicSymbols() {
+ //TODO
+}
+
+template<>
+void LLVMDisassembler<MACHOT>::readDynamicSymbols() {
+ //TODO
+}
+
+template <typename ELFT>
+void LLVMDisassembler<ELFT>::readDynamicSymbols() {
+ const auto elffile = dyn_cast<ELFObjectFile<ELFT>>(o)->getELFFile();
+ for (auto it = elffile->begin_dynamic_symbols(),
+ end = elffile->end_dynamic_symbols();
+ it != end;
+ ++it) {
+ if (it->getType() == 2) { // Function
+ bool is_default;
+ // TODO: Error handling
+ std::string symbolname = *(elffile->getSymbolName(it));
+ std::string symbolversion = *(elffile->getSymbolVersion(nullptr, &*it, is_default));
+ // TODO: actually get the symbol address from relocations
+ Function* f = manager->newDynamicFunction(0);
+ f->setName(symbolname + (is_default? "@@" : "@") + symbolversion);
+ manager->finishFunction(f);
+
+ LOG4CXX_DEBUG(logger, "Adding dynamic Symbol " << symbolname << (is_default? "@@" : "@") << symbolversion);
+ }
+ }
+}
+
+template <typename ELFT>
+void LLVMDisassembler<ELFT>::readSymbols() {
+ error_code ec;
+ symbol_iterator si(o->symbol_begin()), se(o->symbol_end());
+ for (; si != se; ++si) {
+ StringRef name;
+ if ((ec = si->getName(name))) {
+ LOG4CXX_ERROR(logger, ec.message());
+ break;
+ }
+ LOG4CXX_DEBUG(logger, "Added symbol " << name.str());
+ symbols.insert(make_pair(name.str(), *si));
+ }
+}
+
+template <typename ELFT>
+void LLVMDisassembler<ELFT>::readSections() {
+ error_code ec;
+ section_iterator i(o->section_begin()), e(o->section_end());
+ for (; i != e; ++i) {
+ StringRef name;
+ if ((ec = i->getName(name))) {
+ LOG4CXX_ERROR(logger, ec.message());
+ break;
+ }
+ LOG4CXX_DEBUG(logger, "Added section " << name.str());
+ sections.insert(make_pair(name.str(), *i));
+ }
+