More logging in LLVMDisassembler
[frida/frida.git] / src / disassembler / llvm / LLVMDisassembler.cxx
index aeca994c1339c6c72964f334a15746d252abdc22..96418d86bbdded11fd4c530aef48f571c4300cd6 100644 (file)
@@ -11,6 +11,12 @@ using namespace llvm;
 using namespace llvm::object;
 using std::error_code;
 
+namespace {
+       class COFFT {
+
+       };
+}
+
 /*
  *
  */
@@ -35,6 +41,9 @@ Disassembler * createLLVMDisassembler(const std::string& filename, InformationMa
        if (ELF64BEObjectFile * object = dyn_cast<ELF64BEObjectFile>(op)) {
                return new LLVMDisassembler<ELFType<support::big, 2, true>>(filename, manager, object);
        }
+       if (COFFObjectFile * object = dyn_cast<COFFObjectFile>(op)) {
+               return new LLVMDisassembler<COFFT>(filename, manager, object);
+       }
 
        return NULL;
 }
@@ -47,13 +56,13 @@ Disassembler * createLLVMDisassembler(const std::string& filename, InformationMa
 template <typename ELFT>
 LLVMDisassembler<ELFT>::LLVMDisassembler(const std::string& filename,
                                          InformationManager* manager,
-                                         ELFObjectFile<ELFT>* file)
-       : Disassembler(filename, manager)
-       , logger(log4cxx::Logger::getLogger("LLVMDisassembler"))
+                                         ObjectFile* file)
+       : Disassembler()
+       , logger(log4cxx::Logger::getLogger("disassembler.LLVMDisassembler"))
        , triple("unknown-unknown-unknown")
        , manager(manager)
 {
-       LOG4CXX_DEBUG(logger, "Handling file" << filename);
+       LOG4CXX_DEBUG(logger, "Handling file " << filename);
 
        if (!file) {
                auto result = createBinary(filename);
@@ -67,7 +76,7 @@ LLVMDisassembler<ELFT>::LLVMDisassembler(const std::string& filename,
 
                binary.reset(result.get());
 
-               o = dyn_cast<ELFObjectFile<ELFT>>(binary.get());
+               o = dyn_cast<ObjectFile>(binary.get());
        } else {
                o = file;
                binary.reset(file);
@@ -124,10 +133,10 @@ LLVMDisassembler<ELFT>::LLVMDisassembler(const std::string& filename,
        RelInfo.reset(
                target->createMCRelocationInfo(tripleName, Ctx));
        if (RelInfo) {
-               Symzer.reset(
-                       MCObjectSymbolizer::createObjectSymbolizer(Ctx, std::move(RelInfo), o));
-               if (Symzer)
-                       DisAsm->setSymbolizer(std::move(Symzer));
+               // Symzer.reset(
+               //      MCObjectSymbolizer::createObjectSymbolizer(Ctx, std::move(RelInfo), o));
+               // if (Symzer)
+               //      DisAsm->setSymbolizer(std::move(Symzer));
        }
        RelInfo.release();
        Symzer.release();
@@ -151,12 +160,13 @@ LLVMDisassembler<ELFT>::LLVMDisassembler(const std::string& filename,
        std::unique_ptr<MCObjectDisassembler> OD(
                new MCObjectDisassembler(*o, *DisAsm, *MIA));
        Mod.reset(OD->buildModule(false));
+
+       readSections();
 }
 
 template <typename ELFT>
 void LLVMDisassembler<ELFT>::start() {
        readSymbols();
-       readSections();
        disassemble();
        readDynamicSymbols();
 }
@@ -189,7 +199,6 @@ Function* LLVMDisassembler<ELFT>::disassembleFunctionAt(uint64_t address, const
                        function->setName(name);
                }
                disassembleFunction(function);
-               manager->finishFunction(function);
        }
 
        return function;
@@ -197,6 +206,7 @@ Function* LLVMDisassembler<ELFT>::disassembleFunctionAt(uint64_t address, const
 
 template <typename ELFT>
 void LLVMDisassembler<ELFT>::disassembleFunction(Function* function) {
+       std::vector<uint64_t> called_functions;
        std::stack<BasicBlock*> remaining_blocks;
        /* TODO:
         * Do all blocks get added properly? We should take care to remove
@@ -215,6 +225,11 @@ void LLVMDisassembler<ELFT>::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();
@@ -223,8 +238,6 @@ void LLVMDisassembler<ELFT>::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;
@@ -240,7 +253,7 @@ void LLVMDisassembler<ELFT>::disassembleFunction(Function* function) {
                                        if (!MIA->isIndirectBranch(inst)) {
                                                if (MIA->isCall(inst)) {
                                                        if (NULL == manager->getFunction(jmptarget))
-                                                               disassembleFunctionAt(jmptarget);
+                                                               called_functions.push_back(jmptarget);
                                                } else {
                                                        current_block->setNextBlock(0, jmptarget);
                                                        if (new_blocks.find(jmptarget) == new_blocks.end()) {
@@ -288,7 +301,9 @@ void LLVMDisassembler<ELFT>::disassembleFunction(Function* function) {
        }
        splitBlocks(function);
        LOG4CXX_DEBUG(logger, "Finished function " << function->getName());
-       manager->signal_new_function(function);
+       manager->finishFunction(function);
+       for (uint64_t address : called_functions)
+               disassembleFunctionAt(address);
 }
 
 template <typename ELFT>
@@ -324,10 +339,7 @@ void LLVMDisassembler<ELFT>::disassemble() {
        }
 
        if (binary->isELF()) {
-               const ELFO * elffile = o->getELFFile();
-               const typename ELFO::Elf_Ehdr * header = elffile->getHeader();
-
-               _entryAddress = header->e_entry;
+               uint64_t _entryAddress = entryAddress();
                LOG4CXX_DEBUG(logger, "Adding entryAddress at: " << std::hex << _entryAddress);
                std::stringstream s;
                s << "<_start 0x" << std::hex << _entryAddress << ">";
@@ -343,6 +355,30 @@ void LLVMDisassembler<ELFT>::disassemble() {
        }
 }
 
+template <>
+uint64_t LLVMDisassembler<COFFT>::entryAddress() {
+       const auto coffobject = dyn_cast<COFFObjectFile>(o);
+       const struct pe32_header* pe32_header;
+       const struct pe32plus_header* pe32plus_header;
+
+       coffobject->getPE32PlusHeader(pe32plus_header);
+
+       if (pe32plus_header) {
+               return pe32plus_header->AddressOfEntryPoint;
+       } else {
+               coffobject->getPE32Header(pe32_header);
+               return pe32_header->AddressOfEntryPoint;
+       }
+}
+
+template <typename ELFT>
+uint64_t LLVMDisassembler<ELFT>::entryAddress() {
+       const auto elffile = dyn_cast<ELFObjectFile<ELFT>>(o)->getELFFile();
+       const auto * header = elffile->getHeader();
+
+       return header->e_entry;
+}
+
 template <typename ELFT>
 void LLVMDisassembler<ELFT>::splitBlocks(Function* function) {
        SectionRef text_section = sections[".text"];
@@ -350,11 +386,16 @@ void LLVMDisassembler<ELFT>::splitBlocks(Function* function) {
        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);
@@ -394,11 +435,15 @@ void LLVMDisassembler<ELFT>::splitBlocks(Function* function) {
        }
 }
 
+template<>
+void LLVMDisassembler<COFFT>::readDynamicSymbols() {
+       //TODO
+}
+
 template <typename ELFT>
 void LLVMDisassembler<ELFT>::readDynamicSymbols() {
-       const ELFO * elffile = o->getELFFile();
-       for (typename ELFO::Elf_Sym_Iter
-                    it = elffile->begin_dynamic_symbols(),
+       const auto elffile = dyn_cast<ELFObjectFile<ELFT>>(o)->getELFFile();
+       for (auto it = elffile->begin_dynamic_symbols(),
                     end = elffile->end_dynamic_symbols();
             it != end;
             ++it) {