X-Git-Url: https://git.siccegge.de//index.cgi?p=frida%2Ffrida.git;a=blobdiff_plain;f=src%2Fdisassembler%2Fllvm%2FLLVMDisassembler.cxx;h=21766bb344cb708a473188f577659d03723bd1c5;hp=1f1cdff1d17670d6b87b900523ef33b7f85bbc6a;hb=099f1e8222109bf7397eff6da0c511a07906c9cd;hpb=f9ae4391568cfcbf4e6de0475bddcdd68e9b31d2 diff --git a/src/disassembler/llvm/LLVMDisassembler.cxx b/src/disassembler/llvm/LLVMDisassembler.cxx index 1f1cdff..21766bb 100644 --- a/src/disassembler/llvm/LLVMDisassembler.cxx +++ b/src/disassembler/llvm/LLVMDisassembler.cxx @@ -15,6 +15,10 @@ namespace { class COFFT { }; + + class MACHOT { + + }; } /* @@ -44,6 +48,9 @@ Disassembler * createLLVMDisassembler(const std::string& filename, InformationMa if (COFFObjectFile * object = dyn_cast(op)) { return new LLVMDisassembler(filename, manager, object); } + if (MachOObjectFile * object = dyn_cast(op)) { + return new LLVMDisassembler(filename, manager, object); + } return NULL; } @@ -58,7 +65,7 @@ LLVMDisassembler::LLVMDisassembler(const std::string& filename, InformationManager* manager, ObjectFile* file) : Disassembler() - , logger(log4cxx::Logger::getLogger("LLVMDisassembler")) + , logger(log4cxx::Logger::getLogger("disassembler.LLVMDisassembler")) , triple("unknown-unknown-unknown") , manager(manager) { @@ -177,7 +184,7 @@ LLVMDisassembler::~LLVMDisassembler() {} template Function* LLVMDisassembler::disassembleFunctionAt(uint64_t address, const std::string& name) { Function * function; - SectionRef text_section = sections[".text"]; + SectionRef text_section = getTextSection(); uint64_t base_address, size; text_section.getAddress(base_address); text_section.getSize(size); @@ -213,7 +220,7 @@ void LLVMDisassembler::disassembleFunction(Function* function) { * the other ones at the end of the function! */ std::map new_blocks; - SectionRef text_section = sections[".text"]; + SectionRef text_section = getTextSection(); StringRef bytes; text_section.getContents(bytes); StringRefMemoryObject ref(bytes); @@ -225,6 +232,11 @@ void LLVMDisassembler::disassembleFunction(Function* function) { new_blocks.insert(std::make_pair(block->getStartAddress(), block)); function->addBasicBlock(block); + uint64_t base_address, size; + text_section.getAddress(base_address); + text_section.getSize(size); + 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(); @@ -233,8 +245,6 @@ void LLVMDisassembler::disassembleFunction(Function* function) { << 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; @@ -305,7 +315,7 @@ void LLVMDisassembler::disassembleFunction(Function* function) { template void LLVMDisassembler::disassemble() { - SectionRef text_section = sections[".text"]; + SectionRef text_section = getTextSection(); std::vector remaining_functions; // Assume all function symbols actually start a real function @@ -324,9 +334,14 @@ void LLVMDisassembler::disassemble() { if (!x->second.getAddress(result)) { Function * fun = manager->newFunction(result); - fun->setName(x->first); - remaining_functions.push_back(fun); - LOG4CXX_DEBUG(logger, "Disasembling " << x->first); + if (fun) { + fun->setName(x->first); + remaining_functions.push_back(fun); + LOG4CXX_DEBUG(logger, "Disasembling " << x->first); + } else { + LOG4CXX_DEBUG(logger, "Function at " << std::hex << result + << " already disassembled as " << manager->getFunction(result)->getName()); + } } } @@ -368,6 +383,12 @@ uint64_t LLVMDisassembler::entryAddress() { } } +template<> +uint64_t LLVMDisassembler::entryAddress() { + // TODO + return 0; +} + template uint64_t LLVMDisassembler::entryAddress() { const auto elffile = dyn_cast>(o)->getELFFile(); @@ -378,16 +399,21 @@ uint64_t LLVMDisassembler::entryAddress() { template void LLVMDisassembler::splitBlocks(Function* function) { - SectionRef text_section = sections[".text"]; + SectionRef text_section = getTextSection(); StringRef bytes; text_section.getContents(bytes); StringRefMemoryObject ref(bytes); + 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; text_section.getAddress(base_address); @@ -432,6 +458,11 @@ void LLVMDisassembler::readDynamicSymbols() { //TODO } +template<> +void LLVMDisassembler::readDynamicSymbols() { + //TODO +} + template void LLVMDisassembler::readDynamicSymbols() { const auto elffile = dyn_cast>(o)->getELFFile(); @@ -494,7 +525,7 @@ void LLVMDisassembler::printEachInstruction(uint64_t start, uint64_t end, std::function fun) { - SectionRef text_section = sections[".text"]; + SectionRef text_section = getTextSection(); uint64_t base_address; text_section.getAddress(base_address); uint64_t current_address = start - base_address; @@ -540,3 +571,13 @@ void LLVMDisassembler::printEachInstruction(uint64_t start, uint64_t end, current_address += inst_size; } } + +template +SectionRef LLVMDisassembler::getTextSection() { + return sections[".text"]; +} + +template <> +SectionRef LLVMDisassembler::getTextSection() { + return sections["__text"]; +}