+template <typename ELFT>
+LLVMDisassembler<ELFT>::~LLVMDisassembler() {}
+
+template <typename ELFT>
+Function* LLVMDisassembler<ELFT>::disassembleFunctionAt(uint64_t address, const std::string& name) {
+ Function * function;
+ SectionRef text_section = getTextSection();
+ uint64_t base_address, size;
+#if defined(LLVM_35)
+ text_section.getAddress(base_address);
+ text_section.getSize(size);
+#elif defined(LLVM_36)
+ base_address = text_section.getAddress();
+ size = text_section.getSize();
+#endif
+ if (address < base_address ||
+ address >= base_address + size) {
+ return NULL;
+ }
+
+ if (NULL == (function = manager->getFunction(address))) {
+
+ if (name == "") {
+ std::stringstream s;
+ s << "<Unnamed 0x" << std::hex << address << ">";
+ function = manager->newFunction(address);
+ function->setName(s.str());
+ } else {
+ function = manager->newFunction(address);
+ function->setName(name);
+ }
+ disassembleFunction(function);
+ }
+
+ return function;
+}
+
+template <typename ELFT>
+void LLVMDisassembler<ELFT>::disassembleFunction(Function* function) {
+ std::vector<uint64_t> called_functions;
+ std::stack<BasicBlock*> remaining_blocks;
+ /* TODO:
+ * Do all blocks get added properly? We should take care to remove
+ * the other ones at the end of the function!
+ */
+ std::map<uint64_t, BasicBlock*> new_blocks;
+ SectionRef text_section = getTextSection();
+ StringRef bytes;
+ uint64_t base_address, size;
+ text_section.getContents(bytes);
+#if defined(LLVM_35)
+ StringRefMemoryObject ref(bytes);
+ text_section.getAddress(base_address);
+ text_section.getSize(size);
+#elif defined(LLVM_36)
+ ArrayRef<uint8_t> bytearray(reinterpret_cast<const uint8_t *>(bytes.data()),
+ bytes.size());
+ base_address = text_section.getAddress();
+ size = text_section.getSize();
+#else
+#error LLVM != 3.5 | 3.6 not supported
+#endif
+
+ LOG4CXX_DEBUG(logger, "Handling function " << function->getName());
+
+ if(function->getStartAddress() < base_address || function->getStartAddress() > base_address + size) {
+ LOG4CXX_INFO(logger, "Trying to disassemble function " << function->getName() << " but start address " << std::hex << function->getStartAddress() << " is located outside the text segment");
+ return;
+ }
+
+ BasicBlock * block = manager->newBasicBlock(function->getStartAddress());
+ remaining_blocks.push(block);
+ new_blocks.insert(std::make_pair(block->getStartAddress(), block));
+ function->addBasicBlock(block);
+
+ LOG4CXX_DEBUG(logger, "Text section at " << std::hex << base_address << " with size " << size);
+
+ while (remaining_blocks.size()) {
+ BasicBlock * 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 current_address = current_block->getStartAddress() - base_address;
+ while(true) {
+ 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
+ uint64_t jmptarget;
+
+ if (MIA->evaluateBranch(inst, current_address, inst_size, jmptarget)) {
+ jmptarget += base_address;
+ if (!MIA->isIndirectBranch(inst)) {
+ if (MIA->isCall(inst)) {
+ if (NULL == manager->getFunction(jmptarget))
+ called_functions.push_back(jmptarget);
+ } else {
+ if(jmptarget < base_address || jmptarget > base_address + size) {
+ if (MIA->isConditionalBranch(inst)) {
+ LOG4CXX_WARN(logger, "Conditional jump out of the text segment. This should never happen!");
+ } else {
+ LOG4CXX_INFO(logger, "Unconditional jump to PLT. Assuming Tail-Call to some library");
+ current_address += inst_size;
+ continue;
+ }
+ }
+ current_block->setNextBlock(0, jmptarget);
+ if (new_blocks.find(jmptarget) == new_blocks.end()) {
+ BasicBlock * block = manager->newBasicBlock(jmptarget);
+ assert(block);
+ new_blocks.insert(std::make_pair(block->getStartAddress(), block));
+ function->addBasicBlock(block);
+ remaining_blocks.push(block);
+ } else {
+ LOG4CXX_DEBUG(logger, "Reusing Block starting at " << std::hex
+ << current_block->getStartAddress());
+ function->addBasicBlock(new_blocks.find(jmptarget)->second);
+ }
+ if (MIA->isConditionalBranch(inst)) {
+ jmptarget = base_address + current_address + inst_size;
+ current_block->setNextBlock(1, jmptarget);
+ if (new_blocks.find(jmptarget) == new_blocks.end()) {
+ BasicBlock * block = manager->newBasicBlock(jmptarget);
+ assert(block);
+ new_blocks.insert(std::make_pair(block->getStartAddress(), block));
+ function->addBasicBlock(block);
+ remaining_blocks.push(block);
+ } else {
+ LOG4CXX_DEBUG(logger, "Reusing Block starting at " << std::hex
+ << current_block->getStartAddress());
+ function->addBasicBlock(new_blocks.find(jmptarget)->second);
+ }
+ }
+ }
+ }
+ }
+ } else {
+ inst_size = 0;
+ }
+
+
+ if (inst_size == 0 || MIA->isTerminator(inst) || MIA->isBranch(inst)) {
+ current_block->setEndAddress(current_address + base_address + inst_size);
+ LOG4CXX_DEBUG(logger, "Finished Block at " << std::hex <<
+ current_block->getEndAddress());
+ break;
+ }
+ current_address += inst_size;
+ }
+ }
+ splitBlocks(function);
+ LOG4CXX_DEBUG(logger, "Finished function " << function->getName());
+ manager->finishFunction(function);
+ for (uint64_t address : called_functions)
+ disassembleFunctionAt(address);
+}
+
+template <typename ELFT>
+void LLVMDisassembler<ELFT>::disassemble() {
+ SectionRef text_section = getTextSection();
+ std::vector<Function*> remaining_functions;