ROSE  0.11.2.0
BinaryVxcoreParser.h
1 #ifndef ROSE_BinaryAnalysis_VxcoreParser_H
2 #define ROSE_BinaryAnalysis_VxcoreParser_H
3 
4 #include <rosePublicConfig.h>
5 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
6 
7 #include <BaseSemanticsRegisterState.h>
8 #include <boost/filesystem.hpp>
9 #include <boost/lexical_cast.hpp>
10 #include <Diagnostics.h>
11 #include <MemoryMap.h>
12 #include <StringUtility.h>
13 
14 namespace Rose {
15 namespace BinaryAnalysis {
16 
38 class VxcoreParser {
39 public:
41  struct Settings {
42  unsigned version;
44  unsigned protDefault;
46  Settings()
47  : version(1) {}
48  };
49 
51  class Exception: public Rose::Exception {
52  boost::filesystem::path fileName_;
53  size_t offset_;
54 
55  public:
57  Exception(const boost::filesystem::path &fileName, size_t offset, const std::string &what)
58  : Rose::Exception(what), fileName_(fileName), offset_(offset) {}
59 
60  ~Exception() throw () {}
61 
63  std::string toString() const {
64  return StringUtility::cEscape(fileName_.string()) + ":" + boost::lexical_cast<std::string>(offset_) + ": " + what();
65  }
66 
70  void print(std::ostream &out) const {
71  out <<toString();
72  }
73 
74  friend std::ostream& operator<<(std::ostream &out, const Exception &e) {
75  e.print(out);
76  return out;
77  }
78  };
79 
80 private:
81  Settings settings_;
82  std::string isaName_; // Parsed instruction set architecture name
83 
84 public:
86  static void initDiagnostics(); // used internally
87 
88 public:
92  const Settings& settings() const { return settings_; }
93  Settings& settings() { return settings_; }
94  void settings(const Settings &s) { settings_ = s; }
103  boost::filesystem::path parseUrl(const std::string&);
104 
115  void parse(const boost::filesystem::path&, const MemoryMap::Ptr&);
116  void parse(const boost::filesystem::path&, const InstructionSemantics2::BaseSemantics::RegisterStatePtr &registers,
118  void parse(const boost::filesystem::path&, const MemoryMap::Ptr&,
121  void parse(std::istream&, const MemoryMap::Ptr&, const InstructionSemantics2::BaseSemantics::RegisterStatePtr &registers,
122  const InstructionSemantics2::BaseSemantics::RiscOperatorsPtr &ops, const std::string &inputName = "input");
137  void unparse(std::ostream&, const MemoryMap::Ptr&, const AddressInterval &memoryLimit,
138  const std::string &outputName = "output");
141  const std::string &outputName = "output");
142  void unparse(std::ostream&, const MemoryMap::Ptr&, const AddressInterval &memoryLimit,
145  const std::string &outputName = "output");
151  const std::string& isaName() const { return isaName_; }
152 
153 private:
154  // Parse a memory section of the input when given a header and the input stream positioned at the first byte after the
155  // header. Returns false if the header is invalid, throws an Exception if there's a problem reading the data, and returns
156  // true if the memory section was parsed. The memory map can be null, in which case all the normal parsing occurs but
157  // nothing is updated. The inputName is only for the exceptions.
158  bool parseMemory(const std::string &header, std::istream&, const MemoryMap::Ptr&, const std::string &inputName,
159  size_t headerOffset);
160 
161  // Similar to parseMemory except for registers.
162  bool parseRegisters(const std::string &header, std::istream&, const InstructionSemantics2::BaseSemantics::RegisterStatePtr&,
164  const std::string &inputName, size_t headerOffset);
165 };
166 
167 } // namespace
168 } // namespace
169 
170 #endif
171 #endif
Exception(const boost::filesystem::path &fileName, size_t offset, const std::string &what)
Create a new exception with a location and error message.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
Collection of streams.
Definition: Message.h:1599
void print(std::ostream &out) const
Emit this exception to a stream.
unsigned version
Format version number.
void settings(const Settings &s)
Property: Settings for parsing and unparsing.
Main namespace for the ROSE library.
ROSE_UTIL_API std::string cEscape(const std::string &, char context= '"')
Escapes characters that are special to C/C++.
const Settings & settings() const
Property: Settings for parsing and unparsing.
Exception thrown for parse errors.
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 MemoryMap::Ptr &, const AddressInterval &memoryLimit, const std::string &outputName="output")
Unparse memory and/or registers to this format.
static Sawyer::Message::Facility mlog
Diagnostic facility for vxcore file format.
Sawyer::Optional< unsigned > protOverride
Memory protection to overrides parsed values.
Parser for Vxcore format files.
unsigned protDefault
Default memory protection when none are specified or overridden.
boost::filesystem::path parseUrl(const std::string &)
Parse a URL and adjust the settings.
const std::string & isaName() const
Parsed instruction set architecture name.
Base class for all ROSE exceptions.
Definition: RoseException.h:9
Settings that control the parser and unparser.
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
std::string toString() const
Render this exception as a string.