From 4b2336f2066f76d2be017ad9731865ac6743abcb Mon Sep 17 00:00:00 2001 From: Christoph Egger Date: Tue, 10 Mar 2015 12:27:24 +0100 Subject: [PATCH] Re-add support for PE binaries --- src/disassembler/llvm/LLVMDisassembler.cxx | 52 ++++++++++++++++++---- src/disassembler/llvm/LLVMDisassembler.hxx | 10 ++--- src/disassembler/llvm/include_llvm.hxx | 1 + 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/src/disassembler/llvm/LLVMDisassembler.cxx b/src/disassembler/llvm/LLVMDisassembler.cxx index f3a99cc..cdcd3cb 100644 --- a/src/disassembler/llvm/LLVMDisassembler.cxx +++ b/src/disassembler/llvm/LLVMDisassembler.cxx @@ -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(op)) { return new LLVMDisassembler>(filename, manager, object); } + if (COFFObjectFile * object = dyn_cast(op)) { + return new LLVMDisassembler(filename, manager, object); + } return NULL; } @@ -47,7 +56,7 @@ Disassembler * createLLVMDisassembler(const std::string& filename, InformationMa template LLVMDisassembler::LLVMDisassembler(const std::string& filename, InformationManager* manager, - ELFObjectFile* file) + ObjectFile* file) : Disassembler() , logger(log4cxx::Logger::getLogger("LLVMDisassembler")) , triple("unknown-unknown-unknown") @@ -67,7 +76,7 @@ LLVMDisassembler::LLVMDisassembler(const std::string& filename, binary.reset(result.get()); - o = dyn_cast>(binary.get()); + o = dyn_cast(binary.get()); } else { o = file; binary.reset(file); @@ -325,10 +334,7 @@ void LLVMDisassembler::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 << ">"; @@ -344,6 +350,30 @@ void LLVMDisassembler::disassemble() { } } +template <> +uint64_t LLVMDisassembler::entryAddress() { + const auto coffobject = dyn_cast(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 +uint64_t LLVMDisassembler::entryAddress() { + const auto elffile = dyn_cast>(o)->getELFFile(); + const auto * header = elffile->getHeader(); + + return header->e_entry; +} + template void LLVMDisassembler::splitBlocks(Function* function) { SectionRef text_section = sections[".text"]; @@ -395,11 +425,15 @@ void LLVMDisassembler::splitBlocks(Function* function) { } } +template<> +void LLVMDisassembler::readDynamicSymbols() { + //TODO +} + template void LLVMDisassembler::readDynamicSymbols() { - const ELFO * elffile = o->getELFFile(); - for (typename ELFO::Elf_Sym_Iter - it = elffile->begin_dynamic_symbols(), + const auto elffile = dyn_cast>(o)->getELFFile(); + for (auto it = elffile->begin_dynamic_symbols(), end = elffile->end_dynamic_symbols(); it != end; ++it) { diff --git a/src/disassembler/llvm/LLVMDisassembler.hxx b/src/disassembler/llvm/LLVMDisassembler.hxx index 6c55b44..d4b13a2 100644 --- a/src/disassembler/llvm/LLVMDisassembler.hxx +++ b/src/disassembler/llvm/LLVMDisassembler.hxx @@ -14,17 +14,17 @@ class BasicBlock; Disassembler * createLLVMDisassembler(const std::string& filename, InformationManager* manager); -template +template class LLVMDisassembler : public Disassembler { public: LLVMDisassembler(const std::string& filename, InformationManager* manager, - llvm::object::ELFObjectFile* file = NULL); + llvm::object::ObjectFile* file = NULL); virtual ~LLVMDisassembler(); void start(); void getSymbols() {} - uint64_t entryAddress() {return _entryAddress;} + uint64_t entryAddress(); void printEachInstruction(uint64_t start, uint64_t end, std::function ELFO; - // http://llvm.org/docs/doxygen/html/MCObjectDisassembler_8cpp_source.html +197 void disassembleFunction(Function* function); void splitBlocks(Function* fun); @@ -50,7 +48,7 @@ private: std::shared_ptr binary; const llvm::Target * target; - llvm::object::ELFObjectFile * o; + llvm::object::ObjectFile * o; std::unique_ptr MRI; std::unique_ptr AsmInfo; diff --git a/src/disassembler/llvm/include_llvm.hxx b/src/disassembler/llvm/include_llvm.hxx index 8e5babd..c6dba98 100644 --- a/src/disassembler/llvm/include_llvm.hxx +++ b/src/disassembler/llvm/include_llvm.hxx @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include -- 2.39.5