]> git.siccegge.de Git - frida/frida.git/blobdiff - src/disassembler/llvm/LLVMDisassembler.cxx
Add doc repo as submodule
[frida/frida.git] / src / disassembler / llvm / LLVMDisassembler.cxx
index 831a1a9da7a09f0e34ca055e49e8c4bb640fcc3d..017ef95c5968df6e4419bb5a55972342dc0cd5dd 100644 (file)
@@ -217,7 +217,8 @@ template <typename ELFT>
 LLVMDisassembler<ELFT>::~LLVMDisassembler() {}
 
 template <typename ELFT>
-Function* LLVMDisassembler<ELFT>::disassembleFunctionAt(uint64_t address, const std::string& name) {
+Function* LLVMDisassembler<ELFT>::disassembleFunctionAt(uint64_t address,
+                                                        const std::string& name) {
        Function * function;
        SectionRef text_section = getTextSection();
        uint64_t base_address, size;
@@ -322,6 +323,15 @@ void LLVMDisassembler<ELFT>::disassembleFunction(Function* function) {
                                                        if (NULL == manager->getFunction(jmptarget))
                                                                called_functions.push_back(jmptarget);
                                                } else {
+                                                       if(jmptarget < base_address || jmptarget > base_address + size) {
+                                                               if (MIA->isConditionalBranch(inst)) {
+                                                                       LOG4CXX_WARN(logger, "Conditional jump out of the text segment. This should never happen!");
+                                                               } else {
+                                                                       LOG4CXX_INFO(logger, "Unconditional jump to PLT. Assuming Tail-Call to some library");
+                                                                       current_address += inst_size;
+                                                                       continue;
+                                                               }
+                                                       }
                                                        current_block->setNextBlock(0, jmptarget);
                                                        if (new_blocks.find(jmptarget) == new_blocks.end()) {
                                                                BasicBlock * block = manager->newBasicBlock(jmptarget);
@@ -381,10 +391,10 @@ void LLVMDisassembler<ELFT>::disassemble() {
        // Assume all function symbols actually start a real function
        for (auto x = symbols.begin(); x != symbols.end(); ++x) {
                uint64_t result;
-               bool contains;
                SymbolRef::Type symbol_type;
 
 #if defined(LLVM_35)
+               bool contains;
                if (text_section.containsSymbol(x->second, contains) || !contains)
 #elif defined(LLVM_36)
                if (!text_section.containsSymbol(x->second))
@@ -556,7 +566,7 @@ void LLVMDisassembler<ELFT>::readDynamicSymbols() {
             it != end;
             ++it) {
                if (it->getType() == 2) { // Function
-                       bool is_default;
+                       bool is_default(false);
                        // TODO: Error handling
                        std::string symbolname = *(elffile->getSymbolName(it));
                        std::string symbolversion = *(elffile->getSymbolVersion(nullptr, &*it, is_default));
@@ -694,83 +704,6 @@ std::vector<Instruction> LLVMDisassembler<ELFT>::getInstructions(const BasicBloc
        return result;
 }
 
-template <typename ELFT>
-void LLVMDisassembler<ELFT>::printEachInstruction(uint64_t start, uint64_t end,
-                                                  std::function<void (uint8_t*, size_t,
-                                                                         const std::string&,
-                                                                         const std::string&)> 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<uint8_t> bytearray(reinterpret_cast<const uint8_t *>(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 <typename ELFT>
 SectionRef LLVMDisassembler<ELFT>::getTextSection() {
        return sections[".text"];