]> git.siccegge.de Git - frida/frida.git/blobdiff - src/disassembler/llvm/LLVMDisassembler.cxx
Some code cleanup
[frida/frida.git] / src / disassembler / llvm / LLVMDisassembler.cxx
index d9d615ab1ff2c90302c7fbe4106d918805d3e4d5..831a1a9da7a09f0e34ca055e49e8c4bb640fcc3d 100644 (file)
@@ -261,31 +261,33 @@ void LLVMDisassembler<ELFT>::disassembleFunction(Function* function) {
        std::map<uint64_t, BasicBlock*> new_blocks;
        SectionRef text_section = getTextSection();
        StringRef bytes;
        std::map<uint64_t, BasicBlock*> 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.getContents(bytes);
 #if defined(LLVM_35)
        StringRefMemoryObject ref(bytes);
+       text_section.getAddress(base_address);
+       text_section.getSize(size);
 #elif defined(LLVM_36)
        ArrayRef<uint8_t> bytearray(reinterpret_cast<const uint8_t *>(bytes.data()),
                           bytes.size());
 #elif defined(LLVM_36)
        ArrayRef<uint8_t> bytearray(reinterpret_cast<const uint8_t *>(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());
 
 #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);
 
        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()) {
        LOG4CXX_DEBUG(logger, "Text section at " << std::hex << base_address << " with size " << size);
 
        while (remaining_blocks.size()) {
@@ -574,11 +576,13 @@ void LLVMDisassembler<ELFT>::readSymbols() {
        symbol_iterator si(o->symbol_begin()), se(o->symbol_end());
        for (; si != se; ++si) {
                StringRef name;
        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;
                }
                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));
        }
 }
                symbols.insert(make_pair(name.str(), *si));
        }
 }