X-Git-Url: https://git.siccegge.de//index.cgi?p=frida%2Ffrida.git;a=blobdiff_plain;f=src%2Fdisassembler%2Fllvm%2FLLVMDisassembler.cxx;h=049f1fc90477b82d1a30427bbd7352a9d7a98cf3;hp=d9d615ab1ff2c90302c7fbe4106d918805d3e4d5;hb=5d65588185f3d19fc6fa311f642cc5fd78966087;hpb=556bf2788e006ab6be39c3ea5748b46e6265c8f2 diff --git a/src/disassembler/llvm/LLVMDisassembler.cxx b/src/disassembler/llvm/LLVMDisassembler.cxx index d9d615a..049f1fc 100644 --- a/src/disassembler/llvm/LLVMDisassembler.cxx +++ b/src/disassembler/llvm/LLVMDisassembler.cxx @@ -261,31 +261,33 @@ void LLVMDisassembler::disassembleFunction(Function* function) { std::map 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 bytearray(reinterpret_cast(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); - 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 LOG4CXX_DEBUG(logger, "Text section at " << std::hex << base_address << " with size " << size); while (remaining_blocks.size()) { @@ -574,11 +576,13 @@ void LLVMDisassembler::readSymbols() { symbol_iterator si(o->symbol_begin()), se(o->symbol_end()); for (; si != se; ++si) { StringRef name; + uint64_t address; + si->getAddress(address); if ((ec = si->getName(name))) { LOG4CXX_ERROR(logger, ec.message()); break; } - LOG4CXX_DEBUG(logger, "Added symbol " << name.str()); + LOG4CXX_DEBUG(logger, "Added symbol " << name.str() << " at address " << std::hex << address); symbols.insert(make_pair(name.str(), *si)); } } @@ -690,83 +694,6 @@ std::vector LLVMDisassembler::getInstructions(const BasicBloc return result; } -template -void LLVMDisassembler::printEachInstruction(uint64_t start, uint64_t end, - std::function fun) { - SectionRef text_section = getTextSection(); - uint64_t base_address; -#if defined(LLVM_35) - text_section.getAddress(base_address); -#elif defined(LLVM_36) - base_address = text_section.getAddress(); -#endif - - uint64_t current_address = start - base_address; - - StringRef bytes; - text_section.getContents(bytes); -#if defined(LLVM_35) - StringRefMemoryObject ref(bytes); -#elif defined(LLVM_36) - ArrayRef bytearray(reinterpret_cast(bytes.data()), - bytes.size()); -#endif - - - while (current_address < end - base_address) { - uint64_t inst_size; - 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 - - uint8_t bytes[inst_size+2]; -#if defined(LLVM_35) - ref.readBytes(current_address, inst_size, bytes); -#elif defined(LLVM_36) - size_t bytesindex(0); - for (uint8_t byte : bytearray.slice(current_address, inst_size)) { - bytes[bytesindex++] = byte; - } -#endif - - uint64_t jmptarget; - std::string ref(""); - IP->printInst(&inst, s, ""); - if (MIA->evaluateBranch(inst, current_address, inst_size, jmptarget)) { - std::stringstream stream; - if (MIA->isCall(inst)) - stream << "function:"; - else - stream << "block:"; - - stream << std::hex << (base_address + jmptarget); - ref = stream.str(); - } - - - fun(bytes, inst_size, s.str(), ref); - } else { - LOG4CXX_WARN(logger, "Invalid byte at" << std::hex << current_address + base_address); - fun(NULL, 0, "Invalid Byte", ""); - inst_size = 1; - } - - current_address += inst_size; - } -} - template SectionRef LLVMDisassembler::getTextSection() { return sections[".text"];