From 2a014774e29e324bc5b5f26143d0384351738ca1 Mon Sep 17 00:00:00 2001 From: Christoph Egger Date: Tue, 11 Feb 2014 00:49:39 +0100 Subject: [PATCH] Separate out several parts of llvm-using code ; Add logger --- CMakeLists.txt | 26 ++++++++++++----- src/Binary.cxx | 3 ++ src/Section.hxx | 21 ++++++++++++++ src/disassembler/BasicBlock.hxx | 8 ++++++ src/disassembler/Disassembler.cxx | 18 ++++++++++++ src/disassembler/Disassembler.hxx | 16 +++++++++++ src/disassembler/Instruction.hxx | 6 ++++ src/disassembler/InstructionReader.cxx | 27 +++++++++++++++++ src/disassembler/InstructionReader.hxx | 18 ++++++++++++ src/disassembler/Target.cxx | 29 +++++++++++++++++++ src/disassembler/Target.hxx | 31 ++++++++++++++++++++ src/gui/Mainwindow.cxx | 40 +++++++++++++++----------- src/gui/qt.hxx | 16 +++++++++++ src/gui/widgets/BasicBlockWidget.cxx | 3 ++ src/gui/widgets/BasicBlockWidget.hxx | 36 +++++++++++++++++++++++ src/main.cxx | 6 ++++ 16 files changed, 280 insertions(+), 24 deletions(-) create mode 100644 src/Section.hxx create mode 100644 src/disassembler/BasicBlock.hxx create mode 100644 src/disassembler/Disassembler.cxx create mode 100644 src/disassembler/Disassembler.hxx create mode 100644 src/disassembler/Instruction.hxx create mode 100644 src/disassembler/InstructionReader.cxx create mode 100644 src/disassembler/InstructionReader.hxx create mode 100644 src/disassembler/Target.cxx create mode 100644 src/disassembler/Target.hxx create mode 100644 src/gui/qt.hxx create mode 100644 src/gui/widgets/BasicBlockWidget.cxx create mode 100644 src/gui/widgets/BasicBlockWidget.hxx diff --git a/CMakeLists.txt b/CMakeLists.txt index c6219ab..c1ef103 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,10 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) find_package(Qt5Widgets) find_package(Qt5 CONFIG REQUIRED Widgets Gui) +find_package(PkgConfig) +pkg_check_modules(LOG4CXX REQUIRED liblog4cxx) + +add_compile_options(${LOG4CXX_CFLAGS}) execute_process(COMMAND llvm-config-3.4 --cflags OUTPUT_VARIABLE LLVM_CFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE) execute_process(COMMAND llvm-config-3.4 --ldflags OUTPUT_VARIABLE LLVM_LDFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE) @@ -22,19 +26,27 @@ separate_arguments(LLVM_LIBS) set(CMAKE_CXX_COMPILER "clang++") -SET(qtlldb_SOURCES src/Binary.cxx src/main.cxx src/gui/Mainwindow.cxx) -SET(qtlldb_HEADERS src/Binary.hxx src/include.hxx src/Section.hxx src/gui/Mainwindow.h++) +SET(qtlldb_SOURCES + src/Binary.cxx + src/main.cxx + src/gui/Mainwindow.cxx + src/gui/widgets/BasicBlockWidget.cxx + src/disassembler/InstructionReader.cxx + src/disassembler/Target.cxx + src/disassembler/Disassembler.cxx) +SET(qtlldb_HEADERS + src/Binary.hxx + src/include.hxx + src/Section.hxx + src/gui/Mainwindow.hxx + src/gui/widgets/BasicBlockWidget.hxx) ADD_EXECUTABLE(qtlldb ${qtlldb_SOURCES} ${qtlldb_HEADERS_MOC}) - -set(CMAKE_LD_FLAGS "${LLVM_LDFLAGS}") -set(CMAKE_LIBS "${LLVM_LIBS}") - ADD_DEFINITIONS(${QT_DEFINITIONS}) qt5_use_modules(qtlldb Widgets) INCLUDE_DIRECTORIES("src") -TARGET_LINK_LIBRARIES(qtlldb ${QT_LIBRARIES} ${LLVM_LDFLAGS} ${LLVM_LIBS}) +TARGET_LINK_LIBRARIES(qtlldb ${QT_LIBRARIES} ${LLVM_LDFLAGS} ${LLVM_LIBS} ${LOG4CXX_LDFLAGS}) diff --git a/src/Binary.cxx b/src/Binary.cxx index 1fcd908..1d64b84 100644 --- a/src/Binary.cxx +++ b/src/Binary.cxx @@ -1,5 +1,7 @@ #include "Binary.hxx" +#include "disassembler/Disassembler.hxx" + #include #include #include @@ -83,6 +85,7 @@ namespace qtlldb { Binary::Binary(const std::string& filename) : triple("unkown-unknown-unknown") { + ::Disassembler d(filename); std::string error; createBinary(filename, binary); diff --git a/src/Section.hxx b/src/Section.hxx new file mode 100644 index 0000000..04e8b0f --- /dev/null +++ b/src/Section.hxx @@ -0,0 +1,21 @@ +#ifndef INCLUDE__Section_hxx +#define INCLUDE__Section_hxx + +#include "include.hxx" + +#include +#include +#include + +using llvm::OwningPtr; + +namespace qtlldb { + class Section { + public: + Section() {} + + private: + + }; +} +#endif diff --git a/src/disassembler/BasicBlock.hxx b/src/disassembler/BasicBlock.hxx new file mode 100644 index 0000000..22a31e0 --- /dev/null +++ b/src/disassembler/BasicBlock.hxx @@ -0,0 +1,8 @@ + + +class BasicBlock { +public: + +private: + +}; diff --git a/src/disassembler/Disassembler.cxx b/src/disassembler/Disassembler.cxx new file mode 100644 index 0000000..babac82 --- /dev/null +++ b/src/disassembler/Disassembler.cxx @@ -0,0 +1,18 @@ +#include "Disassembler.hxx" + +namespace { + llvm::OwningPtr& + constructor_helper(const std::string& filename, + llvm::OwningPtr& binary) { + createBinary(filename, binary); + return binary; + } +} + + +Disassembler::Disassembler(const std::string& filename) + : _target(constructor_helper(filename, _binary)) +{ + +} + diff --git a/src/disassembler/Disassembler.hxx b/src/disassembler/Disassembler.hxx new file mode 100644 index 0000000..5ccd9ff --- /dev/null +++ b/src/disassembler/Disassembler.hxx @@ -0,0 +1,16 @@ +#ifndef INCLUDE__Disassembler_hxx +#define INCLUDE__Disassembler_hxx + +#include "Target.hxx" +#include "include.hxx" + +class Disassembler { +public: + Disassembler(const std::string& filename); +private: + llvm::OwningPtr _binary; + + Target _target; +}; + +#endif diff --git a/src/disassembler/Instruction.hxx b/src/disassembler/Instruction.hxx new file mode 100644 index 0000000..afb5193 --- /dev/null +++ b/src/disassembler/Instruction.hxx @@ -0,0 +1,6 @@ + + +class Instruction { +public: +private: +}; diff --git a/src/disassembler/InstructionReader.cxx b/src/disassembler/InstructionReader.cxx new file mode 100644 index 0000000..8f8df91 --- /dev/null +++ b/src/disassembler/InstructionReader.cxx @@ -0,0 +1,27 @@ +#include "InstructionReader.hxx" + +InstructionReader::InstructionReader(const Target& target) + : _logger(log4cxx::Logger::getLogger("disassembler.Target")) + , _target(target) +{ + DisAsm.reset(_target.getTarget().createMCDisassembler(_target.getSubTargetInfo())); + if (!DisAsm) { + LOG4CXX_ERROR(_logger, "error: no disassembler for target " << _target.getTripleName()) + return; + } +} + +void InstructionReader::readInstruction(std::string& data, size_t& offset, Instruction& inst) { + llvm::MCInst instr; + llvm::StringRefMemoryObject memoryObject(data); + uint64_t size; + + if (DisAsm->getInstruction(instr, size, memoryObject, offset, + llvm::nulls(), llvm::nulls())) { + + } else { + LOG4CXX_WARN(_logger, "warning: invalid instruction encoding"); + if (size == 0) + size = 1; // skip illegible bytes + } +} diff --git a/src/disassembler/InstructionReader.hxx b/src/disassembler/InstructionReader.hxx new file mode 100644 index 0000000..a68644d --- /dev/null +++ b/src/disassembler/InstructionReader.hxx @@ -0,0 +1,18 @@ +#include "include.hxx" +#include "Instruction.hxx" +#include "Target.hxx" + +#include + +#include + + +class InstructionReader { +public: + InstructionReader(const Target& target); + void readInstruction(std::string& data, size_t& offset, Instruction& inst); +private: + log4cxx::LoggerPtr _logger; + const Target& _target; + llvm::OwningPtr DisAsm; +}; diff --git a/src/disassembler/Target.cxx b/src/disassembler/Target.cxx new file mode 100644 index 0000000..56042d5 --- /dev/null +++ b/src/disassembler/Target.cxx @@ -0,0 +1,29 @@ +#include "Target.hxx" +#include "include.hxx" + +#include + +Target:: Target(const llvm::OwningPtr& binary) + : _logger(log4cxx::Logger::getLogger("disassembler.Target")) + , triple("unknown-unknown-unknown") +{ + std::string error; + llvm::object::ObjectFile * o = llvm::dyn_cast(binary.get()); + + triple.setArch(llvm::Triple::ArchType(o->getArch())); + std::string tripleName(triple.getTriple()); + LOG4CXX_INFO(_logger, "Detected triple " << tripleName); + + target = llvm::TargetRegistry::lookupTarget("", triple, error); + if (!target) { + LOG4CXX_ERROR(_logger, "Couldn't create Target: " << error); + return; + } + LOG4CXX_INFO(_logger, "Target: " << target->getName()); + + STI.reset(target->createMCSubtargetInfo(tripleName, "", "")); + if (!STI) { + LOG4CXX_ERROR(_logger, "No subtarget for target " << tripleName); + return; + } +} diff --git a/src/disassembler/Target.hxx b/src/disassembler/Target.hxx new file mode 100644 index 0000000..db3a4be --- /dev/null +++ b/src/disassembler/Target.hxx @@ -0,0 +1,31 @@ +#ifndef INCLUDE__Target_hxx +#define INCLUDE__Target_hxx + +#include + +#include "include.hxx" + +class Target { +public: + Target(const llvm::OwningPtr& binary); + + const llvm::Target& getTarget() const { + return *target; + } + + const llvm::MCSubtargetInfo& getSubTargetInfo() const { + return *STI; + } + + std::string getTripleName() const { + return triple.getTriple(); + } +private: + log4cxx::LoggerPtr _logger; + llvm::Triple triple; + + const llvm::Target * target; + llvm::OwningPtr STI; +}; + +#endif diff --git a/src/gui/Mainwindow.cxx b/src/gui/Mainwindow.cxx index 790c1a3..8d6bb68 100644 --- a/src/gui/Mainwindow.cxx +++ b/src/gui/Mainwindow.cxx @@ -1,24 +1,11 @@ #include "Mainwindow.hxx" +#include "widgets/BasicBlockWidget.hxx" +#include "qt.hxx" #include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace { - -} Mainwindow::Mainwindow() { @@ -103,8 +90,27 @@ void Mainwindow::populateSymbolInformation(const std::string& sym) { // CFG QGraphicsScene * scene = new QGraphicsScene; - QGraphicsRectItem *rect = scene->addRect(QRectF(0, 0, 100, 100)); - rect->setFlag(QGraphicsItem::ItemIsMovable); + + BasicBlockWidget * s1 = new BasicBlockWidget; + scene->addItem(s1); + s1->setFlag(QGraphicsItem::ItemIsMovable, true); + + BasicBlockWidget * s2 = new BasicBlockWidget; + scene->addItem(s2); + s2->setFlag(QGraphicsItem::ItemIsMovable, true); + s2->moveBy(-200, 350); + + BasicBlockWidget * s3 = new BasicBlockWidget; + scene->addItem(s3); + s3->setFlag(QGraphicsItem::ItemIsMovable, true); + s3->moveBy(100, 350); + + BasicBlockWidget * s4 = new BasicBlockWidget; + scene->addItem(s4); + s4->setFlag(QGraphicsItem::ItemIsMovable, true); + s4->moveBy(400, 350); + + QGraphicsView * view = new QGraphicsView(scene); w->addTab(view, "CFG"); diff --git a/src/gui/qt.hxx b/src/gui/qt.hxx new file mode 100644 index 0000000..493e7b2 --- /dev/null +++ b/src/gui/qt.hxx @@ -0,0 +1,16 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include diff --git a/src/gui/widgets/BasicBlockWidget.cxx b/src/gui/widgets/BasicBlockWidget.cxx new file mode 100644 index 0000000..b28b04f --- /dev/null +++ b/src/gui/widgets/BasicBlockWidget.cxx @@ -0,0 +1,3 @@ + + + diff --git a/src/gui/widgets/BasicBlockWidget.hxx b/src/gui/widgets/BasicBlockWidget.hxx new file mode 100644 index 0000000..6ea927b --- /dev/null +++ b/src/gui/widgets/BasicBlockWidget.hxx @@ -0,0 +1,36 @@ +#include "gui/qt.hxx" + +class BasicBlockWidget : public QGraphicsItem +{ +public: + BasicBlockWidget() { + x = -5; + y = -20; + dx = 250; + dy = 270; + _widget.addItem("THIS"); + _widget.addItem("IS"); + _widget.addItem("A"); + _widget.addItem("TEST"); + } + + QRectF boundingRect() const + { + qreal penWidth = 1; + return QRectF(x - penWidth / 2, y - penWidth / 2, + dx + penWidth, dy + penWidth); + } + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget) + { + painter->fillRect(x, y, dx, dy, QColor(0xcc, 0xcc, 0xff, 0xff)); + painter->setPen(QColor(0x00, 0x00, 0xff, 0xff)); + painter->drawRect(x, y, dx, dy); + painter->drawText(0, -5, "BLOCK"); + _widget.render(painter); + } +private: + int x, y, dx, dy; + QListWidget _widget; +}; diff --git a/src/main.cxx b/src/main.cxx index b23eb88..afcf930 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -6,6 +6,9 @@ #include #include +#include "log4cxx/logger.h" +#include "log4cxx/basicconfigurator.h" + #include "Binary.hxx" #include "gui/Mainwindow.hxx" @@ -15,6 +18,9 @@ using std::cerr; int main(int argc, char** argv) { + log4cxx::BasicConfigurator::configure(); + log4cxx::LoggerPtr _logger(log4cxx::Logger::getLogger("main")); + LOG4CXX_DEBUG(_logger, "Initializing LLVM"); llvm::InitializeAllTargetInfos(); llvm::InitializeAllTargetMCs(); llvm::InitializeAllAsmParsers(); -- 2.39.5