ROSE  0.11.145.0
SRecord.h
1 #ifndef ROSE_BinaryAnalysis_SRecord_H
2 #define ROSE_BinaryAnalysis_SRecord_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 namespace Rose {
7 namespace BinaryAnalysis {
8 
27 class SRecord {
28 public:
30  enum Syntax {
34  };
35 
37  enum Type {
38  // Motorola
50  // Intel
59  };
60 
61 private:
62  Type type_;
63  rose_addr_t addr_;
64  std::vector<uint8_t> data_;
65  std::string error_;
66 
68  // Constructors, etc.
70 public:
72  SRecord(): type_(SREC_UNKNOWN_TYPE), addr_(0) {}
73 
77  SRecord(Type type, rose_addr_t addr, const std::vector<uint8_t> data)
78  : type_(type), addr_(addr), data_(data) {}
79  SRecord(Type type, rose_addr_t addr, const uint8_t *buffer, size_t bufsz)
80  : type_(type), addr_(addr), data_(buffer+0, buffer+bufsz) {}
84  // High-level functions.
87 public:
91  static SRecord parse(const std::string&);
92 
97  static std::vector<SRecord> parse(std::istream&);
98 
103  static AddressIntervalSet dataAddresses(const std::vector<SRecord>&);
104 
116  static AddressIntervalSet createSegments(const std::vector<SRecord>&, const MemoryMap::Ptr &map, rose_addr_t alignment,
117  unsigned accessPermissions, const std::string &segmentName,
118  MemoryMap::Clobber clobber);
119 
125  static Sawyer::Optional<rose_addr_t> load(const std::vector<SRecord>&, const MemoryMap::Ptr&);
126 
132  static Sawyer::Optional<rose_addr_t> load(const std::vector<SRecord>&, const MemoryMap::Ptr&, rose_addr_t alignment,
133  unsigned accessPerms, const std::string &name, MemoryMap::Clobber);
134 
136  static std::vector<SRecord> create(const MemoryMap::Ptr&, Syntax, size_t bytesPerRecord=28, size_t preferredAddrSize=4);
137 
139  std::string toString() const;
140 
144  void print(std::ostream&) const;
145  friend std::ostream& operator<<(std::ostream&, const SRecord&);
148  // S-Record properties.
151 public:
155  Syntax syntax() const;
156 
160  Type type() const { return type_; }
161  SRecord& type(Type type) { type_ = type; return *this; }
172  rose_addr_t address() const { return addr_; }
173  SRecord& address(rose_addr_t addr) { addr_ = addr; return *this; }
179  const std::vector<uint8_t>& data() const { return data_; }
180  std::vector<uint8_t>& data() { return data_; }
181  SRecord& data(const std::vector<uint8_t>& data) { data_ = data; return *this; }
187  const std::string& error() const { return error_; }
188  SRecord& error(const std::string &error) { error_ = error; return *this; }
193  // Low-level functions.
196 public:
199  Sawyer::Optional<rose_addr_t> dataVa_, executionVa_;
200  size_t nDataRecords_;
201  public:
204  : nDataRecords_(0) {}
205 
209  void insert(const SRecord&);
210 
213 
217  rose_addr_t baseDataAddress() const;
218 
221 
223  size_t nDataRecords() const { return nDataRecords_; }
224  };
225 
227  static SRecord parseMotorola(const std::string&);
228 
230  static SRecord parseIntel(const std::string&);
231 
238  static Sawyer::Optional<rose_addr_t> parseBigEndianInteger(const std::string&, size_t start, size_t nChars);
239 
241  static unsigned bigEndian(const std::vector<uint8_t> &data, size_t nBytes);
242 
246  static size_t addressNBytes(Type);
247 
252  static uint8_t checksum(Syntax, const std::string&, size_t start, size_t n);
253 };
254 
255 } // namespace
256 } // namespace
257 
258 #endif
259 #endif
static Sawyer::Optional< rose_addr_t > parseBigEndianInteger(const std::string &, size_t start, size_t nChars)
Parse a hexadecimal string to an unsigned integer.
const std::string & error() const
Property: record error messsage.
Definition: SRecord.h:187
Motorola 16-bit count of data records.
Definition: SRecord.h:44
Motorola 24-bit starting address.
Definition: SRecord.h:47
static unsigned bigEndian(const std::vector< uint8_t > &data, size_t nBytes)
Convert the first few bytes of the buffer to a big-endian integer.
rose_addr_t address() const
Property: record starting address.
Definition: SRecord.h:172
void print(std::ostream &) const
Print an S-Record to a stream.
SRecord & address(rose_addr_t addr)
Property: record starting address.
Definition: SRecord.h:173
static SRecord parse(const std::string &)
Construct an S-Record by parsing a string.
Intel starting linear address.
Definition: SRecord.h:56
Intel extended linear address.
Definition: SRecord.h:55
const std::vector< uint8_t > & data() const
Property: record data.
Definition: SRecord.h:179
Motorola 32-bit starting address.
Definition: SRecord.h:46
Motorola data with 16-bit address.
Definition: SRecord.h:40
Motorola data with 32-bit address.
Definition: SRecord.h:42
static size_t addressNBytes(Type)
Size of address in bytes.
static SRecord parseMotorola(const std::string &)
Construct a Motorola S-Record by parsing a string.
SRecord()
Construct an empty S-Record.
Definition: SRecord.h:72
Main namespace for the ROSE library.
static std::vector< SRecord > create(const MemoryMap::Ptr &, Syntax, size_t bytesPerRecord=28, size_t preferredAddrSize=4)
Create S-Records from a memory map.
Motorola data with 24-bit address.
Definition: SRecord.h:41
static Sawyer::Optional< rose_addr_t > load(const std::vector< SRecord > &, const MemoryMap::Ptr &)
Load S-Records into a memory map.
friend std::ostream & operator<<(std::ostream &, const SRecord &)
Print an S-Record to a stream.
Motorola 24-bit count of data records.
Definition: SRecord.h:45
Sawyer::Optional< rose_addr_t > executionAddress() const
Returns the execution starting address if one was specified.
Intel extended segment address.
Definition: SRecord.h:53
S-Record hexadecimal data formats.
Definition: SRecord.h:27
std::vector< uint8_t > & data()
Property: record data.
Definition: SRecord.h:180
static AddressIntervalSet createSegments(const std::vector< SRecord > &, const MemoryMap::Ptr &map, rose_addr_t alignment, unsigned accessPermissions, const std::string &segmentName, MemoryMap::Clobber clobber)
Create segments for S-Records.
Syntax syntax() const
Property: syntax family.
Intel starting segment address.
Definition: SRecord.h:54
static uint8_t checksum(Syntax, const std::string &, size_t start, size_t n)
Compute a checksum for part of a record.
Motorola 16-bit starting address.
Definition: SRecord.h:48
Sawyer::Optional< rose_addr_t > dataAddress(const SRecord &) const
Returns the memory address for the start of a data record, or nothing.
Motorola syntax S-Record.
Definition: SRecord.h:31
Type
Type of S-Record.
Definition: SRecord.h:37
SRecord & error(const std::string &error)
Property: record error messsage.
Definition: SRecord.h:188
std::string toString() const
Convert an S-Record to a string.
SRecord(Type type, rose_addr_t addr, const std::vector< uint8_t > data)
Construct an S-Record with data.
Definition: SRecord.h:77
static AddressIntervalSet dataAddresses(const std::vector< SRecord > &)
Addresses occupied by all data.
SRecord(Type type, rose_addr_t addr, const uint8_t *buffer, size_t bufsz)
Construct an S-Record with data.
Definition: SRecord.h:79
SRecord & data(const std::vector< uint8_t > &data)
Property: record data.
Definition: SRecord.h:181
Run-length encoding information.
Definition: SRecord.h:198
void insert(const SRecord &)
Insert an S-Record into the data sumarized by this class.
static SRecord parseIntel(const std::string &)
Construct an Intel S-Record by parsing a string.
Type type() const
Property: record type.
Definition: SRecord.h:160
SRecord & type(Type type)
Property: record type.
Definition: SRecord.h:161
size_t nDataRecords() const
Number of data records seen.
Definition: SRecord.h:223
rose_addr_t baseDataAddress() const
Base data address.
Clobber
Overwrite (parts of) existing segments?
Definition: MemoryMap.h:138
Syntax
Syntax of the S-Record.
Definition: SRecord.h:30
Motorola vendor-specific header, usually NUL-terminated ASCII.
Definition: SRecord.h:39