X-Git-Url: https://git.siccegge.de//index.cgi?p=frida%2Ffrida.git;a=blobdiff_plain;f=src%2Fdisassembler%2Fllvm%2FLLVMDisassembler.cxx;h=773a5189a27a33d35085da9f698864970b3ac9ec;hp=d2c68fdce4f4f48f803c90726a2707565007d8fc;hb=0daf9a157f3d41690cf4a0287db1adecc4ad0b71;hpb=d5084161ca261d7fc0bd284621569440b6503eac diff --git a/src/disassembler/llvm/LLVMDisassembler.cxx b/src/disassembler/llvm/LLVMDisassembler.cxx index d2c68fd..773a518 100644 --- a/src/disassembler/llvm/LLVMDisassembler.cxx +++ b/src/disassembler/llvm/LLVMDisassembler.cxx @@ -1,10 +1,11 @@ #include "disassembler/llvm/LLVMDisassembler.hxx" -#include "disassembler/llvm/LLVMBasicBlock.hxx" -#include "disassembler/llvm/LLVMFunction.hxx" #include "core/InformationManager.hxx" +#include "core/Function.hxx" +#include "core/BasicBlock.hxx" #include #include +#include using namespace llvm; using namespace llvm::object; @@ -161,19 +162,11 @@ void LLVMDisassembler::start() { } template -LLVMDisassembler::~LLVMDisassembler() { - // std::for_each(functions.begin(), functions.end(), - // [](std::pair it) { - // delete it.second; - // }); - // std::for_each(blocks.begin(), blocks.end(), - // [](std::pair it) { - // delete it.second; - // }); -} +LLVMDisassembler::~LLVMDisassembler() {} template Function* LLVMDisassembler::disassembleFunctionAt(uint64_t address, const std::string& name) { + Function * function; SectionRef text_section = sections[".text"]; uint64_t base_address, size; text_section.getAddress(base_address); @@ -184,28 +177,32 @@ Function* LLVMDisassembler::disassembleFunctionAt(uint64_t address, const return NULL; } - if (functions.find(address) != functions.end()) { - return functions[address]; - } + if (NULL == (function = manager->getFunction(address))) { - LLVMFunction * function; - if (name == "") { - std::stringstream s; - s << ""; - function = new LLVMFunction(s.str(), address); - } else { - function = new LLVMFunction(name, address); + if (name == "") { + std::stringstream s; + s << ""; + function = manager->newFunction(address); + function->setName(s.str()); + } else { + function = manager->newFunction(address); + function->setName(name); + } + disassembleFunction(function); + manager->finishFunction(function); } - functions.insert(std::make_pair(address, function)); - - disassembleFunction(function); return function; } template -void LLVMDisassembler::disassembleFunction(LLVMFunction* function) { - std::stack remaining_blocks; +void LLVMDisassembler::disassembleFunction(Function* function) { + std::stack 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 new_blocks; SectionRef text_section = sections[".text"]; StringRef bytes; text_section.getContents(bytes); @@ -213,16 +210,17 @@ void LLVMDisassembler::disassembleFunction(LLVMFunction* function) { LOG4CXX_DEBUG(logger, "Handling function " << function->getName()); - LLVMBasicBlock * block = new LLVMBasicBlock(function->getStartAddress(), this); + BasicBlock * block = manager->newBasicBlock(function->getStartAddress()); remaining_blocks.push(block); - blocks.insert(std::make_pair(block->getStartAddress(), block)); + new_blocks.insert(std::make_pair(block->getStartAddress(), block)); function->addBasicBlock(block); while (remaining_blocks.size()) { - LLVMBasicBlock * current_block = remaining_blocks.top(); + BasicBlock * current_block = remaining_blocks.top(); remaining_blocks.pop(); - LOG4CXX_DEBUG(logger, "Handling Block starting at " << std::hex << current_block->getStartAddress()); + LOG4CXX_DEBUG(logger, "Handling Block starting at " << std::hex + << current_block->getStartAddress()); uint64_t inst_size; uint64_t base_address; @@ -241,31 +239,34 @@ void LLVMDisassembler::disassembleFunction(LLVMFunction* function) { jmptarget += base_address; if (!MIA->isIndirectBranch(inst)) { if (MIA->isCall(inst)) { - if (functions.find(jmptarget) == functions.end()) { + if (NULL == manager->getFunction(jmptarget)) 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)); + 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(blocks.find(jmptarget)->second); + 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 (blocks.find(jmptarget) == blocks.end()) { - LLVMBasicBlock * block = new LLVMBasicBlock(jmptarget, this); - blocks.insert(std::make_pair(block->getStartAddress(), block)); + 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(blocks.find(jmptarget)->second); + LOG4CXX_DEBUG(logger, "Reusing Block starting at " << std::hex + << current_block->getStartAddress()); + function->addBasicBlock(new_blocks.find(jmptarget)->second); } } } @@ -293,7 +294,7 @@ void LLVMDisassembler::disassembleFunction(LLVMFunction* function) { template void LLVMDisassembler::disassemble() { SectionRef text_section = sections[".text"]; - std::vector remaining_functions; + std::vector remaining_functions; // Assume all function symbols actually start a real function for (auto x = symbols.begin(); x != symbols.end(); ++x) { @@ -310,15 +311,16 @@ void LLVMDisassembler::disassemble() { continue; if (!x->second.getAddress(result)) { - LLVMFunction * fun = new LLVMFunction(x->first, result); + Function * fun = manager->newFunction(result); + fun->setName(x->first); remaining_functions.push_back(fun); - functions.insert(std::make_pair(result, fun)); LOG4CXX_DEBUG(logger, "Disasembling " << x->first); } } - for (LLVMFunction* function : remaining_functions) { + for (Function* function : remaining_functions) { disassembleFunction(function); + manager->finishFunction(function); } if (binary->isELF()) { @@ -333,16 +335,14 @@ void LLVMDisassembler::disassemble() { disassembleFunctionAt(_entryAddress, s.str()); } - if (functions.empty()) { - uint64_t text_entry; - text_section.getAddress(text_entry); - LOG4CXX_INFO(logger, "No Symbols found, starting at the beginning of the text segment"); - disassembleFunctionAt(text_entry); - } + uint64_t text_entry; + text_section.getAddress(text_entry); + LOG4CXX_INFO(logger, "No Symbols found, starting at the beginning of the text segment"); + disassembleFunctionAt(text_entry); } template -void LLVMDisassembler::splitBlocks(LLVMFunction* function) { +void LLVMDisassembler::splitBlocks(Function* function) { SectionRef text_section = sections[".text"]; StringRef bytes; text_section.getContents(bytes); @@ -365,20 +365,22 @@ void LLVMDisassembler::splitBlocks(LLVMFunction* function) { if(llvm::MCDisassembler::Success == DisAsm->getInstruction(inst, inst_size, ref, current_address, nulls(), nulls())) { // See if some other block starts here - auto other = blocks.find(current_address + inst_size + base_address); + 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 != blocks.end()) { + 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->first); - function->addBasicBlock(other->second); + << other->getStartAddress()); + function->addBasicBlock(other); current_block->setEndAddress(endaddress); - current_block->setNextBlock(0, other->first); + current_block->setNextBlock(0, other->getStartAddress()); current_block->setNextBlock(1, 0); } } @@ -440,13 +442,13 @@ void LLVMDisassembler::readSections() { } -template -void LLVMDisassembler::forEachFunction(std::function callback) { - std::for_each(functions.begin(), functions.end(), - [&](std::pair x) { - callback(x.first, x.second); - }); -} +// template +// void LLVMDisassembler::forEachFunction(std::function callback) { +// // std::for_each(functions.begin(), functions.end(), +// // [&](std::pair x) { +// // callback(x.first, x.second); +// // }); +// } template void LLVMDisassembler::printEachInstruction(uint64_t start, uint64_t end,