ROSE 0.11.145.192
BinaryVxcoreParser.h
1#ifndef ROSE_BinaryAnalysis_VxcoreParser_H
2#define ROSE_BinaryAnalysis_VxcoreParser_H
3
4#include <featureTests.h>
5#ifdef ROSE_ENABLE_BINARY_ANALYSIS
6
7#include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics/RegisterState.h>
8#include <boost/filesystem.hpp>
9#include <boost/lexical_cast.hpp>
10#include <Rose/Diagnostics.h>
11#include <Rose/BinaryAnalysis/MemoryMap.h>
12#include <Rose/StringUtility.h>
13
14namespace Rose {
15namespace BinaryAnalysis {
16
48public:
58
60 class Exception: public Rose::Exception {
61 boost::filesystem::path fileName_;
62 size_t offset_;
63
64 public:
66 Exception(const boost::filesystem::path &fileName, size_t offset, const std::string &what)
67 : Rose::Exception(what), fileName_(fileName), offset_(offset) {}
68
69 ~Exception() throw () {}
70
72 std::string toString() const {
73 return StringUtility::cEscape(fileName_.string()) + ":" + boost::lexical_cast<std::string>(offset_) + ": " + what();
74 }
75
79 void print(std::ostream &out) const {
80 out <<toString();
81 }
82
83 friend std::ostream& operator<<(std::ostream &out, const Exception &e) {
84 e.print(out);
85 return out;
86 }
87 };
88
89private:
90 // Message headers for version 2.
91 struct HeaderVersion2 {
92 uint8_t version; // must be 2
93 uint8_t unused0;
94 uint8_t unused1;
95 uint8_t mapFlags; // MemoryMap::{READABLE,WRITABLE,EXECUTABLE}
96 uint32_t payloadSize; // little-endian
97 uint64_t addr; // little-endian
98 };
99
100private:
101 Settings settings_;
102 std::string isaName_; // Parsed instruction set architecture name
103
104public:
106 static void initDiagnostics(); // used internally
107
108public:
112 const Settings& settings() const { return settings_; }
113 Settings& settings() { return settings_; }
114 void settings(const Settings &s) { settings_ = s; }
123 boost::filesystem::path parseUrl(const std::string&);
124
135 void parse(const boost::filesystem::path&, const MemoryMap::Ptr&);
136 void parse(const boost::filesystem::path&, const InstructionSemantics::BaseSemantics::RegisterStatePtr &registers,
138 void parse(const boost::filesystem::path&, const MemoryMap::Ptr&,
142 const InstructionSemantics::BaseSemantics::RiscOperatorsPtr &ops, const std::string &inputName = "input");
157 void unparse(std::ostream&, const MemoryMap::Ptr&, const AddressInterval &memoryLimit,
158 const std::string &outputName = "output");
161 const std::string &outputName = "output");
162 void unparse(std::ostream&, const MemoryMap::Ptr&, const AddressInterval &memoryLimit,
165 const std::string &outputName = "output");
171 const std::string& isaName() const { return isaName_; }
172
173private:
174 // Parse a memory section of the input when given a header and the input stream positioned at the first byte after the
175 // header. Returns false if the header is invalid, throws an Exception if there's a problem reading the data, and returns
176 // true if the memory section was parsed. The memory map can be null, in which case all the normal parsing occurs but
177 // nothing is updated. The inputName is only for the exceptions.
178 bool parseMemory(const std::string &header, std::istream&, const MemoryMap::Ptr&, const std::string &inputName,
179 size_t headerOffset);
180
181 // Similar to parseMemory except for registers.
182 bool parseRegisters(const std::string &header, std::istream&, const InstructionSemantics::BaseSemantics::RegisterStatePtr&,
184 const std::string &inputName, size_t headerOffset);
185};
186
187} // namespace
188} // namespace
189
190#endif
191#endif
Exception(const boost::filesystem::path &fileName, size_t offset, const std::string &what)
Create a new exception with a location and error message.
void print(std::ostream &out) const
Emit this exception to a stream.
std::string toString() const
Render this exception as a string.
Parser for Vxcore format files.
const std::string & isaName() const
Parsed instruction set architecture name.
const Settings & settings() const
Property: Settings for parsing and unparsing.
void settings(const Settings &s)
Property: Settings for parsing and unparsing.
static Sawyer::Message::Facility mlog
Diagnostic facility for vxcore file format.
void parse(std::istream &, const MemoryMap::Ptr &, const InstructionSemantics::BaseSemantics::RegisterStatePtr &registers, const InstructionSemantics::BaseSemantics::RiscOperatorsPtr &ops, const std::string &inputName="input")
Parse input file.
void unparse(std::ostream &, const MemoryMap::Ptr &, const AddressInterval &memoryLimit, const std::string &outputName="output")
Unparse memory and/or registers to this format.
void parse(const boost::filesystem::path &, const MemoryMap::Ptr &)
Parse input file.
Settings & settings()
Property: Settings for parsing and unparsing.
void unparse(std::ostream &, const InstructionSemantics::BaseSemantics::RegisterStatePtr &, const InstructionSemantics::BaseSemantics::RiscOperatorsPtr &, const std::string &outputName="output")
Unparse memory and/or registers to this format.
void parse(const boost::filesystem::path &, const MemoryMap::Ptr &, const InstructionSemantics::BaseSemantics::RegisterStatePtr &registers, const InstructionSemantics::BaseSemantics::RiscOperatorsPtr &ops)
Parse input file.
void unparse(std::ostream &, const MemoryMap::Ptr &, const AddressInterval &memoryLimit, const InstructionSemantics::BaseSemantics::RegisterStatePtr &, const InstructionSemantics::BaseSemantics::RiscOperatorsPtr &, const std::string &outputName="output")
Unparse memory and/or registers to this format.
boost::filesystem::path parseUrl(const std::string &)
Parse a URL and adjust the settings.
void parse(const boost::filesystem::path &, const InstructionSemantics::BaseSemantics::RegisterStatePtr &registers, const InstructionSemantics::BaseSemantics::RiscOperatorsPtr &ops)
Parse input file.
Base class for all ROSE exceptions.
Collection of streams.
Definition Message.h:1606
Holds a value or nothing.
Definition Optional.h:56
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
ROSE_UTIL_API std::string cEscape(const std::string &, char context='"')
Escapes characters that are special to C/C++.
The ROSE library.
Settings that control the parser and unparser.
unsigned protDefault
Default memory protection when none are specified or overridden.
Sawyer::Optional< unsigned > protOverride
Memory protection to overrides parsed values.