From: Christoph Egger Date: Wed, 14 Jan 2015 16:35:51 +0000 (+0100) Subject: Make LLVMDisassembler a template class X-Git-Tag: v0.1~130 X-Git-Url: https://git.siccegge.de//index.cgi?a=commitdiff_plain;h=f28fa96b829aa2d45e6e2fbea8da25107f30de3d;p=frida%2Ffrida.git Make LLVMDisassembler a template class --- diff --git a/src/core/InformationManager.cxx b/src/core/InformationManager.cxx index 6e91572..8f916ef 100644 --- a/src/core/InformationManager.cxx +++ b/src/core/InformationManager.cxx @@ -1,9 +1,7 @@ #include "InformationManager.hxx" #include "disassembler/llvm/LLVMDisassembler.hxx" -#include - void InformationManager::reset(const std::string& filename) { - disassembler.reset(new LLVMDisassembler(filename, this)); + disassembler.reset(createLLVMDisassembler(filename, this)); disassembler.get()->start(); } diff --git a/src/disassembler/llvm/LLVMBasicBlock.hxx b/src/disassembler/llvm/LLVMBasicBlock.hxx index 6bc186d..0949f70 100644 --- a/src/disassembler/llvm/LLVMBasicBlock.hxx +++ b/src/disassembler/llvm/LLVMBasicBlock.hxx @@ -3,21 +3,20 @@ #include "disassembler/BasicBlock.hxx" -class LLVMDisassembler; +class Disassembler; class LLVMBasicBlock : public BasicBlock { public: - LLVMBasicBlock(uint64_t start_address, LLVMDisassembler * disassembler) { + LLVMBasicBlock(uint64_t start_address, Disassembler * disassembler) { setStartAddress(start_address); } - LLVMDisassembler * getDisassembler() { + Disassembler * getDisassembler() { return disassembler; } private: - LLVMDisassembler * disassembler; + Disassembler * disassembler; }; - #endif diff --git a/src/disassembler/llvm/LLVMDisassembler.cxx b/src/disassembler/llvm/LLVMDisassembler.cxx index 6a25e9a..1f92ab0 100644 --- a/src/disassembler/llvm/LLVMDisassembler.cxx +++ b/src/disassembler/llvm/LLVMDisassembler.cxx @@ -9,12 +9,42 @@ using namespace llvm; using namespace llvm::object; using std::error_code; +/* + * + */ +Disassembler * createLLVMDisassembler(const std::string& filename, InformationManager* manager) { + std::unique_ptr o; + o.reset(createBinary(filename).get()); + const Binary * op = o.get(); + + // ELFType + if (const ELF32LEObjectFile * _ = dyn_cast(op)) { +#pragma unused(_) + return new LLVMDisassembler>(filename, manager); + } + if (const ELF64LEObjectFile * _ = dyn_cast(op)) { +#pragma unused(_) + return new LLVMDisassembler>(filename, manager); + } + if (const ELF32BEObjectFile * _ = dyn_cast(op)) { +#pragma unused(_) + return new LLVMDisassembler>(filename, manager); + } + if (const ELF64BEObjectFile * _ = dyn_cast(op)) { +#pragma unused(_) + return new LLVMDisassembler>(filename, manager); + } + + return NULL; +} + /* * TODO: fallback code falls die Datei kein ELF/PE/COFF/MacO/.. binary * ist sondern z.B. einfach nur Instruktionen oder ein Bootsektor oder * foo */ -LLVMDisassembler::LLVMDisassembler(const std::string& filename, +template +LLVMDisassembler::LLVMDisassembler(const std::string& filename, InformationManager* manager) : Disassembler(filename, manager) , logger(log4cxx::Logger::getLogger("LLVMDisassembler")) @@ -115,13 +145,15 @@ LLVMDisassembler::LLVMDisassembler(const std::string& filename, Mod.reset(OD->buildModule(false)); } -void LLVMDisassembler::start() { +template +void LLVMDisassembler::start() { readSymbols(); readSections(); disassemble(); } -LLVMDisassembler::~LLVMDisassembler() { +template +LLVMDisassembler::~LLVMDisassembler() { std::for_each(functions.begin(), functions.end(), [](std::pair it) { delete it.second; @@ -132,7 +164,8 @@ LLVMDisassembler::~LLVMDisassembler() { }); } -Function* LLVMDisassembler::disassembleFunctionAt(uint64_t address, const std::string& name) { +template +Function* LLVMDisassembler::disassembleFunctionAt(uint64_t address, const std::string& name) { SectionRef text_section = sections[".text"]; uint64_t base_address, size; text_section.getAddress(base_address); @@ -162,7 +195,8 @@ Function* LLVMDisassembler::disassembleFunctionAt(uint64_t address, const std::s return function; } -void LLVMDisassembler::disassembleFunction(LLVMFunction* function) { +template +void LLVMDisassembler::disassembleFunction(LLVMFunction* function) { std::stack remaining_blocks; SectionRef text_section = sections[".text"]; StringRef bytes; @@ -248,7 +282,8 @@ void LLVMDisassembler::disassembleFunction(LLVMFunction* function) { manager->signal_new_function(function); } -void LLVMDisassembler::disassemble() { +template +void LLVMDisassembler::disassemble() { SectionRef text_section = sections[".text"]; std::vector remaining_functions; @@ -305,7 +340,8 @@ void LLVMDisassembler::disassemble() { } } -void LLVMDisassembler::splitBlocks(LLVMFunction* function) { +template +void LLVMDisassembler::splitBlocks(LLVMFunction* function) { SectionRef text_section = sections[".text"]; StringRef bytes; text_section.getContents(bytes); @@ -353,7 +389,8 @@ void LLVMDisassembler::splitBlocks(LLVMFunction* function) { } } -void LLVMDisassembler::readSymbols() { +template +void LLVMDisassembler::readSymbols() { error_code ec; symbol_iterator si(o->symbol_begin()), se(o->symbol_end()); for (; si != se; ++si) { @@ -367,7 +404,8 @@ void LLVMDisassembler::readSymbols() { } } -void LLVMDisassembler::readSections() { +template +void LLVMDisassembler::readSections() { error_code ec; section_iterator i(o->section_begin()), e(o->section_end()); for (; i != e; ++i) { @@ -382,14 +420,16 @@ void LLVMDisassembler::readSections() { } -void LLVMDisassembler::forEachFunction(std::function callback) { +template +void LLVMDisassembler::forEachFunction(std::function callback) { std::for_each(functions.begin(), functions.end(), [&](std::pair x) { callback(x.first, x.second); }); } -void LLVMDisassembler::printEachInstruction(uint64_t start, uint64_t end, +template +void LLVMDisassembler::printEachInstruction(uint64_t start, uint64_t end, std::function fun) { SectionRef text_section = sections[".text"]; diff --git a/src/disassembler/llvm/LLVMDisassembler.hxx b/src/disassembler/llvm/LLVMDisassembler.hxx index b0ab082..5c54f39 100644 --- a/src/disassembler/llvm/LLVMDisassembler.hxx +++ b/src/disassembler/llvm/LLVMDisassembler.hxx @@ -14,6 +14,9 @@ #include "disassembler/llvm/LLVMFunction.hxx" +Disassembler * createLLVMDisassembler(const std::string& filename, InformationManager* manager); + +template class LLVMDisassembler : public Disassembler { public: diff --git a/src/disassembler/llvm/include_llvm.hxx b/src/disassembler/llvm/include_llvm.hxx index 0ab21a3..8e5babd 100644 --- a/src/disassembler/llvm/include_llvm.hxx +++ b/src/disassembler/llvm/include_llvm.hxx @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include