- 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));
-
- 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));
- 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));
- 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;
- }
- }
- LOG4CXX_DEBUG(logger, "Finished function " << function->getName());
- manager->signal_new_function(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);