ROSE 0.11.145.192
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#include <Rose/BinaryAnalysis/Address.h>
7#include <Rose/BinaryAnalysis/AddressIntervalSet.h>
8#include <Rose/BinaryAnalysis/MemoryMap.h>
9
10#include <Sawyer/Optional.h>
11
12#include <ostream>
13#include <string>
14#include <vector>
15
16namespace Rose {
17namespace BinaryAnalysis {
18
37class SRecord {
38public:
45
70
71private:
72 Type type_;
73 rose_addr_t addr_;
74 std::vector<uint8_t> data_;
75 std::string error_;
76
78 // Constructors, etc.
80public:
82 SRecord(): type_(SREC_UNKNOWN_TYPE), addr_(0) {}
83
87 SRecord(Type type, rose_addr_t addr, const std::vector<uint8_t> data)
88 : type_(type), addr_(addr), data_(data) {}
89 SRecord(Type type, rose_addr_t addr, const uint8_t *buffer, size_t bufsz)
90 : type_(type), addr_(addr), data_(buffer+0, buffer+bufsz) {}
95 // High-level functions.
97public:
101 static SRecord parse(const std::string&);
102
107 static std::vector<SRecord> parse(std::istream&);
108
113 static AddressIntervalSet dataAddresses(const std::vector<SRecord>&);
114
126 static AddressIntervalSet createSegments(const std::vector<SRecord>&, const MemoryMapPtr &map, rose_addr_t alignment,
127 unsigned accessPermissions, const std::string &segmentName,
128 MemoryMap::Clobber clobber);
129
135 static Sawyer::Optional<rose_addr_t> load(const std::vector<SRecord>&, const MemoryMapPtr&);
136
142 static Sawyer::Optional<rose_addr_t> load(const std::vector<SRecord>&, const MemoryMapPtr&, rose_addr_t alignment,
143 unsigned accessPerms, const std::string &name, MemoryMap::Clobber);
144
146 static std::vector<SRecord> create(const MemoryMapPtr&, Syntax, size_t bytesPerRecord=28, size_t preferredAddrSize=4);
147
149 std::string toString() const;
150
154 void print(std::ostream&) const;
155 friend std::ostream& operator<<(std::ostream&, const SRecord&);
159 // S-Record properties.
161public:
165 Syntax syntax() const;
166
170 Type type() const { return type_; }
171 SRecord& type(Type type) { type_ = type; return *this; }
182 rose_addr_t address() const { return addr_; }
183 SRecord& address(rose_addr_t addr) { addr_ = addr; return *this; }
189 const std::vector<uint8_t>& data() const { return data_; }
190 std::vector<uint8_t>& data() { return data_; }
191 SRecord& data(const std::vector<uint8_t>& data) { data_ = data; return *this; }
197 const std::string& error() const { return error_; }
198 SRecord& error(const std::string &error) { error_ = error; return *this; }
204 // Low-level functions.
206public:
209 Sawyer::Optional<rose_addr_t> dataVa_, executionVa_;
210 size_t nDataRecords_;
211 public:
214 : nDataRecords_(0) {}
215
219 void insert(const SRecord&);
220
223
227 rose_addr_t baseDataAddress() const;
228
231
233 size_t nDataRecords() const { return nDataRecords_; }
234 };
235
237 static SRecord parseMotorola(const std::string&);
238
240 static SRecord parseIntel(const std::string&);
241
248 static Sawyer::Optional<rose_addr_t> parseBigEndianInteger(const std::string&, size_t start, size_t nChars);
249
251 static unsigned bigEndian(const std::vector<uint8_t> &data, size_t nBytes);
252
256 static size_t addressNBytes(Type);
257
262 static uint8_t checksum(Syntax, const std::string&, size_t start, size_t n);
263};
264
265} // namespace
266} // namespace
267
268#endif
269#endif
Clobber
Overwrite (parts of) existing segments?
Definition MemoryMap.h:141
Run-length encoding information.
Definition SRecord.h:208
void insert(const SRecord &)
Insert an S-Record into the data sumarized by this class.
rose_addr_t baseDataAddress() const
Base data address.
Sawyer::Optional< rose_addr_t > executionAddress() const
Returns the execution starting address if one was specified.
size_t nDataRecords() const
Number of data records seen.
Definition SRecord.h:233
Sawyer::Optional< rose_addr_t > dataAddress(const SRecord &) const
Returns the memory address for the start of a data record, or nothing.
S-Record hexadecimal data formats.
Definition SRecord.h:37
const std::string & error() const
Property: record error messsage.
Definition SRecord.h:197
static AddressIntervalSet dataAddresses(const std::vector< SRecord > &)
Addresses occupied by all data.
static Sawyer::Optional< rose_addr_t > load(const std::vector< SRecord > &, const MemoryMapPtr &)
Load S-Records into a memory map.
static AddressIntervalSet createSegments(const std::vector< SRecord > &, const MemoryMapPtr &map, rose_addr_t alignment, unsigned accessPermissions, const std::string &segmentName, MemoryMap::Clobber clobber)
Create segments for S-Records.
const std::vector< uint8_t > & data() const
Property: record data.
Definition SRecord.h:189
std::vector< uint8_t > & data()
Property: record data.
Definition SRecord.h:190
static SRecord parse(const std::string &)
Construct an S-Record by parsing a string.
void print(std::ostream &) const
Print an S-Record to a stream.
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.
friend std::ostream & operator<<(std::ostream &, const SRecord &)
Print an S-Record to a stream.
static Sawyer::Optional< rose_addr_t > parseBigEndianInteger(const std::string &, size_t start, size_t nChars)
Parse a hexadecimal string to an unsigned integer.
static std::vector< SRecord > create(const MemoryMapPtr &, Syntax, size_t bytesPerRecord=28, size_t preferredAddrSize=4)
Create S-Records from a memory map.
SRecord & type(Type type)
Property: record type.
Definition SRecord.h:171
static std::vector< SRecord > parse(std::istream &)
Construct S-Records by parsing a file.
SRecord(Type type, rose_addr_t addr, const std::vector< uint8_t > data)
Construct an S-Record with data.
Definition SRecord.h:87
Syntax syntax() const
Property: syntax family.
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.
std::string toString() const
Convert an S-Record to a string.
SRecord & data(const std::vector< uint8_t > &data)
Property: record data.
Definition SRecord.h:191
Type type() const
Property: record type.
Definition SRecord.h:170
SRecord & error(const std::string &error)
Property: record error messsage.
Definition SRecord.h:198
rose_addr_t address() const
Property: record starting address.
Definition SRecord.h:182
static SRecord parseIntel(const std::string &)
Construct an Intel S-Record by parsing a string.
SRecord(Type type, rose_addr_t addr, const uint8_t *buffer, size_t bufsz)
Construct an S-Record with data.
Definition SRecord.h:89
static Sawyer::Optional< rose_addr_t > load(const std::vector< SRecord > &, const MemoryMapPtr &, rose_addr_t alignment, unsigned accessPerms, const std::string &name, MemoryMap::Clobber)
Load S-Records into a memory map, creating segments if necessary.
SRecord()
Construct an empty S-Record.
Definition SRecord.h:82
static uint8_t checksum(Syntax, const std::string &, size_t start, size_t n)
Compute a checksum for part of a record.
Syntax
Syntax of the S-Record.
Definition SRecord.h:40
@ SREC_UNKNOWN_SYNTAX
Unknown or no syntax.
Definition SRecord.h:43
@ SREC_MOTOROLA
Motorola syntax S-Record.
Definition SRecord.h:41
@ SREC_INTEL
Intel syntax S-Record.
Definition SRecord.h:42
SRecord & address(rose_addr_t addr)
Property: record starting address.
Definition SRecord.h:183
Type
Type of S-Record.
Definition SRecord.h:47
@ SREC_M_RESERVED
Motorola reserved.
Definition SRecord.h:53
@ SREC_M_DATA16
Motorola data with 16-bit address.
Definition SRecord.h:50
@ SREC_M_COUNT24
Motorola 24-bit count of data records.
Definition SRecord.h:55
@ SREC_M_DATA24
Motorola data with 24-bit address.
Definition SRecord.h:51
@ SREC_M_COUNT16
Motorola 16-bit count of data records.
Definition SRecord.h:54
@ SREC_M_START16
Motorola 16-bit starting address.
Definition SRecord.h:58
@ SREC_I_EXTENDED_SA
Intel extended segment address.
Definition SRecord.h:63
@ SREC_I_END
Intel end record.
Definition SRecord.h:62
@ SREC_I_START_SA
Intel starting segment address.
Definition SRecord.h:64
@ SREC_I_DATA
Intel data record.
Definition SRecord.h:61
@ SREC_UNKNOWN_TYPE
Not a valid S-Record.
Definition SRecord.h:68
@ SREC_M_DATA32
Motorola data with 32-bit address.
Definition SRecord.h:52
@ SREC_I_EXTENDED_LA
Intel extended linear address.
Definition SRecord.h:65
@ SREC_M_START32
Motorola 32-bit starting address.
Definition SRecord.h:56
@ SREC_I_START_LA
Intel starting linear address.
Definition SRecord.h:66
@ SREC_M_START24
Motorola 24-bit starting address.
Definition SRecord.h:57
@ SREC_M_HEADER
Motorola vendor-specific header, usually NUL-terminated ASCII.
Definition SRecord.h:49
Holds a value or nothing.
Definition Optional.h:56
The ROSE library.