+ std::for_each(functions.begin(), functions.end(),
+ [](std::pair<uint64_t,LLVMFunction*> it) {
+ delete it.second;
+ });
+ std::for_each(blocks.begin(), blocks.end(),
+ [](std::pair<uint64_t, LLVMBasicBlock*> it) {
+ delete it.second;
+ });
+}
+
+Function* LLVMDisassembler::disassembleFunctionAt(uint64_t address, const std::string& name) {
+ SectionRef text_section = sections[".text"];
+ uint64_t base_address, size;
+ text_section.getAddress(base_address);
+ text_section.getSize(size);
+
+ if (address < base_address ||
+ address >= base_address + size) {
+ return NULL;
+ }
+
+ if (functions.find(address) != functions.end()) {
+ return functions[address];
+ }
+
+ LLVMFunction * function;
+ if (name == "") {
+ std::stringstream s;
+ s << "<Unnamed 0x" << std::hex << address << ">";
+ function = new LLVMFunction(s.str(), address);
+ } else {
+ function = new LLVMFunction(name, address);
+ }
+ functions.insert(std::make_pair(address, function));
+
+ disassembleFunction(function);
+
+ return function;
+}
+
+void LLVMDisassembler::disassembleFunction(LLVMFunction* function) {
+ std::stack<LLVMBasicBlock*> remaining_blocks;
+ SectionRef text_section = sections[".text"];
+ StringRef bytes;
+ text_section.getContents(bytes);
+ StringRefMemoryObject ref(bytes);
+
+ LOG4CXX_DEBUG(logger, "Handling function " << function->getName());
+
+ LLVMBasicBlock * block = new LLVMBasicBlock(function->getStartAddress(), this);
+ remaining_blocks.push(block);
+ blocks.insert(std::make_pair(block->getStartAddress(), block));
+ function->addBasicBlock(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())) {
+ 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()) {
+ disassembleFunctionAt(jmptarget);
+ }
+ } else {
+ current_block->setNextBlock(0, jmptarget);
+ if (blocks.find(jmptarget) == blocks.end()) {
+ LLVMBasicBlock * block = new LLVMBasicBlock(jmptarget, this);
+ blocks.insert(std::make_pair(block->getStartAddress(), block));
+ function->addBasicBlock(block);
+ remaining_blocks.push(block);
+ }
+ if (MIA->isConditionalBranch(inst)) {
+ jmptarget = base_address + current_address + inst_size;
+ current_block->setNextBlock(1, jmptarget);
+ if (blocks.find(jmptarget) == blocks.end()) {
+ LLVMBasicBlock * block = new LLVMBasicBlock(jmptarget, this);
+ blocks.insert(std::make_pair(block->getStartAddress(), block));
+ function->addBasicBlock(block);
+ remaining_blocks.push(block);
+ }
+ }
+ }
+ }
+ }
+ } 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->signal_new_function(function);