From 4d8b6e0ceeb9c8df06decf33b83781adc00cbead Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 26 Jul 2016 21:17:20 +0200 Subject: cleaning. --- CMakeLists.txt | 13 ++++--- Ld.cpp | 60 +++++------------------------- README.md | 7 +++- elfinfo.cpp | 2 +- include-priv/trygvis/antlr.h | 67 ++++++++++++++++++++++++++++++++++ include/trygvis/elfinfo/Ld.h | 85 +++++++++++++++++++++++++++++++++++++++++++ includes/trygvis/elfinfo/Ld.h | 84 ------------------------------------------ 7 files changed, 175 insertions(+), 143 deletions(-) create mode 100644 include-priv/trygvis/antlr.h create mode 100644 include/trygvis/elfinfo/Ld.h delete mode 100644 includes/trygvis/elfinfo/Ld.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ddbe243..9c6b7f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,13 +7,14 @@ find_package(Antlr4) antlr4_add_target(TARGET GnuLd STATIC LEXER GnuLdLexer.g4 PARSER GnuLdParser.g4) -add_executable(elfinfo elfinfo.cpp Ld.cpp includes/trygvis/elfinfo/Ld.h) +add_executable(elfinfo elfinfo.cpp Ld.cpp include/trygvis/elfinfo/Ld.h include-priv/trygvis/antlr.h) target_compile_options(elfinfo PUBLIC "--std=c++14") target_link_libraries(elfinfo elf GnuLd Antlr4::antlr4_shared) -target_include_directories(elfinfo PUBLIC includes/trygvis/elfinfo) +target_include_directories(elfinfo + PUBLIC include + PRIVATE include-priv) INSTALL(TARGETS elfinfo - RUNTIME DESTINATION bin - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib -) + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib) diff --git a/Ld.cpp b/Ld.cpp index 16c40fa..7728fd6 100644 --- a/Ld.cpp +++ b/Ld.cpp @@ -1,4 +1,5 @@ -#include "Ld.h" +#include "trygvis/elfinfo/Ld.h" +#include "trygvis/antlr.h" #include "GnuLdLexer.h" #include "GnuLdParser.h" #include "GnuLdParserBaseListener.h" @@ -9,6 +10,7 @@ namespace elfinfo { using antlr4::ANTLRFileStream; using antlr4::tree::ParseTree; using namespace std; +using namespace trygvis::antlr; static MemoryAttribute valueOf(char c) { switch (c) { @@ -22,7 +24,7 @@ static MemoryAttribute valueOf(char c) { case 'X': return MemoryAttribute::X; default: - throw std::domain_error("Invalid memory attribute: " + c); + throw domain_error("Invalid memory attribute: " + c); } } @@ -30,48 +32,6 @@ static bool endsWith(const string &a, const string &b) { return b.length() <= a.length() && a.compare(a.length() - b.length(), b.length(), b) == 0; } -template -class ParseTreeProperty { -public: - virtual V get(Ref node) { - return get(node.get()); - } - - virtual V get(ParseTree *const node) { - if (!debug) { - return _annotations.at(node); - } - - try { -// cout << "node = " << node->getText() << endl; - return _annotations.at(node); - } catch (std::out_of_range &e) { - cout << "get(" << node << "), text=" << node->getText() << endl; - stringstream buf; - buf << "out of range: " << node << ", text=" << node->getText(); - auto msg = buf.str(); - cout << msg << endl; - throw LdInternalErrorException(msg); - } - } - - virtual void put(ParseTree *const node, V value) { - if (debug) { - cout << "put(" << node << ", " << value << "), text: " << node->getText() << endl; - } - _annotations[node] = value; - } - - virtual V removeFrom(ParseTree *const node) { - return _annotations.erase(node); - } - -protected: - std::map _annotations; - -private: -}; - class ElfinfoGnuLdBaseListener : public GnuLdParserBaseListener { private: public: @@ -260,16 +220,16 @@ LdScriptLoader::LdScriptLoader() : debug_(false) { class LdErrorListener : public BaseErrorListener { public: - std::vector messages; + vector messages; void syntaxError(IRecognizer *recognizer, Token *offendingSymbol, size_t line, int charPositionInLine, - const std::string &msg, std::exception_ptr e) override { + const string &msg, exception_ptr e) override { messages.push_back("line " + to_string(line) + ":" + to_string(charPositionInLine) + ": " + msg); } }; -LdScript LdScriptLoader::load(std::string path) { - std::ifstream stream(path, std::ios::binary); +LdScript LdScriptLoader::load(string path) { + ifstream stream(path, ios::binary); if (!stream.good() || stream.eof()) { return {}; @@ -292,7 +252,7 @@ LdScript LdScriptLoader::load(std::string path) { if (debug_) { for (auto token : tokens.getTokens()) { - std::cout << token->toString() << std::endl; + cout << token->toString() << endl; } } @@ -311,7 +271,7 @@ LdScript LdScriptLoader::load(std::string path) { } if (debug_ && parser.getNumberOfSyntaxErrors() == 0) { - std::cout << file->toStringTree(&parser) << std::endl << std::endl; + cout << file->toStringTree(&parser) << endl; } return { diff --git a/README.md b/README.md index 948b01e..c751014 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ This code currently depend on experimental patches for Antlr4's C++ runtime whic more or less do it: git clone https://github.com/trygvis/antlr4 - cd antlr4 + cd antlr4/runtime/Cpp mkdir build cd build cmake -DCMAKE_INSTALL_PREFIX=$HOME/opt/antlr-cpp .. @@ -43,6 +43,9 @@ This will build and install Antlr4 into $HOME/opt/antlr-cpp. To build this code follow a similar approach: mkdir build - cmake .. -DAntlr4_DIR=$HOME/opt/antlr-cpp/lib/cmake/Antlr4 -DCMAKE_INSTALL_PREFIX=$HOME/opt/elfinfo + cmake -DAntlr4_DIR=$HOME/opt/antlr-cpp/lib/cmake/Antlr4 \ + -DCMAKE_INSTALL_PREFIX=$HOME/opt/elfinfo \ + -DCMAKE_SKIP_RPATH=ON \ + .. make make install diff --git a/elfinfo.cpp b/elfinfo.cpp index 08c6c5b..4f8848d 100644 --- a/elfinfo.cpp +++ b/elfinfo.cpp @@ -15,7 +15,7 @@ #include #include -#include "Ld.h" +#include "trygvis/elfinfo/Ld.h" using namespace std; using namespace trygvis::elfinfo; diff --git a/include-priv/trygvis/antlr.h b/include-priv/trygvis/antlr.h new file mode 100644 index 0000000..f5656ea --- /dev/null +++ b/include-priv/trygvis/antlr.h @@ -0,0 +1,67 @@ +#pragma once + +#include "antlr4-runtime.h" + +namespace trygvis { +namespace antlr { + +// This namespace is shared copied code + +using ParseTree = antlr4::tree::ParseTree; + +class MissingParseTreeProperty : public std::out_of_range { +public: + explicit MissingParseTreeProperty(const std::string &what) : out_of_range(what) { } +}; + +template +class ParseTreeProperty { +public: + virtual V get(Ref node) { + return get(node.get()); + } + + virtual V get(ParseTree *const node) { + if (!debug) { + return _annotations.at(node); + } + + try { +// cerr << "node = " << node->getText() << endl; + return _annotations.at(node); + } catch (std::out_of_range &e) { + std::cerr << "get(" << node << "), text=" << node->getText() << std::endl; + std::stringstream buf; + buf << "out of range: " << node << ", text=" << node->getText(); + auto msg = buf.str(); + std::cerr << msg << std::endl; + throw MissingParseTreeProperty(msg); + } + } + + virtual void put(ParseTree *const node, V value) { + if (debug) { + std::cerr << "put(" << node << ", " << value << "), text: " << node->getText() << std::endl; + } + _annotations[node] = value; + } + + virtual V removeFrom(ParseTree *const node) { + auto it = _annotations.find(node); + + if (it == _annotations.end()) { + throw MissingParseTreeProperty(node->getText()); + } + + return it->second; + } + +protected: + std::map _annotations; + +private: +}; + +} // namespace antlr + +} diff --git a/include/trygvis/elfinfo/Ld.h b/include/trygvis/elfinfo/Ld.h new file mode 100644 index 0000000..9bb81aa --- /dev/null +++ b/include/trygvis/elfinfo/Ld.h @@ -0,0 +1,85 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace trygvis { +namespace elfinfo { + +class LdParseException : public std::runtime_error { +public: + explicit LdParseException(const std::vector &messages) : + runtime_error("Parse error"), messages(messages) {} + + const std::vector messages; +}; + +class LdInternalErrorException : public std::runtime_error { +public: + explicit LdInternalErrorException(const std::string &what) : runtime_error(what) {} +}; + +enum class MemoryAttribute { + R, W, X +}; + +static char to_str(const MemoryAttribute &type) { + switch (type) { + case MemoryAttribute::R: + return 'r'; + case MemoryAttribute::W: + return 'w'; + case MemoryAttribute::X: + return 'x'; + } +} + +struct Section { + std::string name; +}; + +struct MemoryArea { + std::string name; + uint64_t origin; + uint64_t length; + std::set attributes; + + bool contains(uint64_t address, uint64_t size) const { + return contains(address) && contains(address + size); + } + + bool contains(uint64_t address) const { + return origin <= address && address < origin + length; + } + + std::string attributes_string() const { + char buf[4]; + buf[0] = attributes.find(MemoryAttribute::R) == attributes.end() ? '-' : 'r'; + buf[1] = attributes.find(MemoryAttribute::W) == attributes.end() ? '-' : 'w'; + buf[2] = attributes.find(MemoryAttribute::X) == attributes.end() ? '-' : 'x'; + buf[3] = '\0'; + return std::string(buf); + } +}; + +struct LdScript { + std::vector memoryAreas; +}; + +class LdScriptLoader { +public: + LdScriptLoader(); + + LdScript load(std::string path); + + void setDebug(bool debug); + +private: + bool debug_; +}; + +} // namespace elfinfo +} // namespace trygvis diff --git a/includes/trygvis/elfinfo/Ld.h b/includes/trygvis/elfinfo/Ld.h deleted file mode 100644 index 03559a2..0000000 --- a/includes/trygvis/elfinfo/Ld.h +++ /dev/null @@ -1,84 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace trygvis { -namespace elfinfo { - -class LdParseException : public std::runtime_error { -public: - explicit LdParseException(const std::vector &messages) : - runtime_error("Parse error"), messages(messages) {} - - const std::vector messages; -}; - -class LdInternalErrorException : public std::runtime_error { -public: - explicit LdInternalErrorException(const std::string &what) : runtime_error(what) {} -}; - -enum class MemoryAttribute { - R, W, X -}; - -static char to_str(const MemoryAttribute &type) { - switch (type) { - case MemoryAttribute::R: - return 'r'; - case MemoryAttribute::W: - return 'w'; - case MemoryAttribute::X: - return 'x'; - } -} - -struct Section { - std::string name; -}; - -struct MemoryArea { - std::string name; - uint64_t origin; - uint64_t length; - std::set attributes; - - bool contains(uint64_t address, uint64_t size) const { - return contains(address) && contains(address + size); - } - - bool contains(uint64_t address) const { - return origin <= address && address < origin + length; - } - - std::string attributes_string() const { - char buf[4]; - buf[0] = attributes.find(MemoryAttribute::R) == attributes.end() ? '-' : 'r'; - buf[1] = attributes.find(MemoryAttribute::W) == attributes.end() ? '-' : 'w'; - buf[2] = attributes.find(MemoryAttribute::X) == attributes.end() ? '-' : 'x'; - buf[3] = '\0'; - return std::string(buf); - } -}; - -struct LdScript { - std::vector memoryAreas; -}; - -class LdScriptLoader { -public: - LdScriptLoader(); - - LdScript load(std::string path); - - void setDebug(bool debug); - -private: - bool debug_; -}; - -} // namespace elfinfo -} // namespace trygvis -- cgit v1.2.3