]> git.siccegge.de Git - frida/frida.git/blobdiff - src/disassembler/llvm/LLVMDisassembler.cxx
Basic MachO Support
[frida/frida.git] / src / disassembler / llvm / LLVMDisassembler.cxx
index 96418d86bbdded11fd4c530aef48f571c4300cd6..21766bb344cb708a473188f577659d03723bd1c5 100644 (file)
@@ -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<COFFObjectFile>(op)) {
                return new LLVMDisassembler<COFFT>(filename, manager, object);
        }
+       if (MachOObjectFile * object = dyn_cast<MachOObjectFile>(op)) {
+               return new LLVMDisassembler<MACHOT>(filename, manager, object);
+       }
 
        return NULL;
 }
@@ -177,7 +184,7 @@ LLVMDisassembler<ELFT>::~LLVMDisassembler() {}
 template <typename ELFT>
 Function* LLVMDisassembler<ELFT>::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<ELFT>::disassembleFunction(Function* function) {
         * the other ones at the end of the function!
         */
        std::map<uint64_t, BasicBlock*> new_blocks;
-       SectionRef text_section = sections[".text"];
+       SectionRef text_section = getTextSection();
        StringRef bytes;
        text_section.getContents(bytes);
        StringRefMemoryObject ref(bytes);
@@ -308,7 +315,7 @@ void LLVMDisassembler<ELFT>::disassembleFunction(Function* function) {
 
 template <typename ELFT>
 void LLVMDisassembler<ELFT>::disassemble() {
-       SectionRef text_section = sections[".text"];
+       SectionRef text_section = getTextSection();
        std::vector<Function*> remaining_functions;
 
        // Assume all function symbols actually start a real function
@@ -327,9 +334,14 @@ void LLVMDisassembler<ELFT>::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());
+                       }
                }
        }
 
@@ -371,6 +383,12 @@ uint64_t LLVMDisassembler<COFFT>::entryAddress() {
        }
 }
 
+template<>
+uint64_t LLVMDisassembler<MACHOT>::entryAddress() {
+       // TODO
+       return 0;
+}
+
 template <typename ELFT>
 uint64_t LLVMDisassembler<ELFT>::entryAddress() {
        const auto elffile = dyn_cast<ELFObjectFile<ELFT>>(o)->getELFFile();
@@ -381,7 +399,7 @@ uint64_t LLVMDisassembler<ELFT>::entryAddress() {
 
 template <typename ELFT>
 void LLVMDisassembler<ELFT>::splitBlocks(Function* function) {
-       SectionRef text_section = sections[".text"];
+       SectionRef text_section = getTextSection();
        StringRef bytes;
        text_section.getContents(bytes);
        StringRefMemoryObject ref(bytes);
@@ -440,6 +458,11 @@ void LLVMDisassembler<COFFT>::readDynamicSymbols() {
        //TODO
 }
 
+template<>
+void LLVMDisassembler<MACHOT>::readDynamicSymbols() {
+       //TODO
+}
+
 template <typename ELFT>
 void LLVMDisassembler<ELFT>::readDynamicSymbols() {
        const auto elffile = dyn_cast<ELFObjectFile<ELFT>>(o)->getELFFile();
@@ -502,7 +525,7 @@ 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 = sections[".text"];
+       SectionRef text_section = getTextSection();
        uint64_t base_address;
        text_section.getAddress(base_address);
        uint64_t current_address = start - base_address;
@@ -548,3 +571,13 @@ void LLVMDisassembler<ELFT>::printEachInstruction(uint64_t start, uint64_t end,
                current_address += inst_size;
        }
 }
+
+template <typename ELFT>
+SectionRef LLVMDisassembler<ELFT>::getTextSection() {
+       return sections[".text"];
+}
+
+template <>
+SectionRef LLVMDisassembler<MACHOT>::getTextSection() {
+       return sections["__text"];
+}