OpenASIP 2.2
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
TPEF::TPEFReader Class Reference

#include <TPEFReader.hh>

Inheritance diagram for TPEF::TPEFReader:
Inheritance graph
Collaboration diagram for TPEF::TPEFReader:
Collaboration graph

Public Member Functions

SectionsectionOfAddress (const ASpaceElement *aSpaceId, AddressImage address) const
 
SectionId aSpaceId ()
 
SectionId strTableId ()
 
void addSectionSize (const Section *sect, Word length)
 
ASpaceSectionaSpaceSection () const
 
- Public Member Functions inherited from TPEF::BinaryReader
virtual ~BinaryReader ()
 

Static Public Member Functions

static BinaryReaderinstance ()
 
- Static Public Member Functions inherited from TPEF::BinaryReader
static BinaryreadBinary (BinaryStream &stream)
 

Protected Member Functions

 TPEFReader (const TPEFReader &)
 
TPEFReaderoperator= (const TPEFReader &)
 
 TPEFReader ()
 
virtual ~TPEFReader ()
 
virtual BinaryreadData (BinaryStream &stream) const
 
virtual bool isMyStreamType (BinaryStream &stream) const
 
- Protected Member Functions inherited from TPEF::BinaryReader
 BinaryReader ()
 

Private Member Functions

SectionreadSection (BinaryStream &stream, FileOffset headerOffset, Byte stringTableId, Byte aSpaceSectionId) const
 

Private Attributes

Binarybinary_
 Last or currently read binary.
 
SectionId aSpaceId_
 Identification code of address space section (there is only one at maximum).
 
SectionId strTableId_
 Identification code of string table containing section name strings.
 
std::map< const Section *, Word > sectionSizes_
 Cache of sizes in addresses of read sections.
 

Static Private Attributes

static TPEFReaderproto_ = NULL
 Prototype instance registered into BinaryReader.
 

Additional Inherited Members

- Public Types inherited from TPEF::BinaryReader
typedef unsigned long Length
 
- Static Protected Member Functions inherited from TPEF::BinaryReader
static void registerBinaryReader (BinaryReader *reader)
 

Detailed Description

Reads binary file that is in TPEF binary format.

Definition at line 52 of file TPEFReader.hh.

Constructor & Destructor Documentation

◆ TPEFReader() [1/2]

TPEF::TPEFReader::TPEFReader ( const TPEFReader )
protected

◆ TPEFReader() [2/2]

TPEF::TPEFReader::TPEFReader ( )
protected

Constructor.

Creates Header and registers itself to BinaryReader.

Definition at line 66 of file TPEFReader.cc.

68}
static void registerBinaryReader(BinaryReader *reader)
SectionId aSpaceId_
Identification code of address space section (there is only one at maximum).
Definition TPEFReader.hh:93
SectionId strTableId_
Identification code of string table containing section name strings.
Definition TPEFReader.hh:95

References TPEF::BinaryReader::registerBinaryReader().

Referenced by instance().

Here is the call graph for this function:

◆ ~TPEFReader()

TPEF::TPEFReader::~TPEFReader ( )
protectedvirtual

Destructor.

Definition at line 73 of file TPEFReader.cc.

73 {
74 if (proto_ != NULL) {
75 auto proto = proto_;
76 proto_ = NULL;
77 delete proto;
78 }
79}
static TPEFReader * proto_
Prototype instance registered into BinaryReader.
Definition TPEFReader.hh:87

References proto_.

Member Function Documentation

◆ addSectionSize()

void TPEF::TPEFReader::addSectionSize ( const Section sect,
Word  length 
)

Stores size of a section as bytes.

Parameters
sectSection whose size is stored.
lengthSection data size in addresses.

Definition at line 320 of file TPEFReader.cc.

320 {
321 sectionSizes_[sect] = length;
322}
std::map< const Section *, Word > sectionSizes_
Cache of sizes in addresses of read sections.
Definition TPEFReader.hh:97

References sectionSizes_.

◆ aSpaceId()

SectionId TPEF::TPEFReader::aSpaceId ( )

Returns the identification code of the address space table section.

Returns
Identification code of the address space table section.

Definition at line 330 of file TPEFReader.cc.

330 {
331 return aSpaceId_;
332}

References aSpaceId_.

Referenced by TPEF::TPEFSectionReader::readData().

◆ aSpaceSection()

ASpaceSection * TPEF::TPEFReader::aSpaceSection ( ) const

Returns address space of file if already read.

Returns
Address space of binary. NULL if there is no ASpace.

Definition at line 351 of file TPEFReader.cc.

351 {
352 for (unsigned int i = 0; i < binary_->sectionCount(); i++) {
353 if (binary_->section(i)->type() == Section::ST_ADDRSP) {
354 return dynamic_cast<ASpaceSection*>(binary_->section(i));
355 }
356 }
357 return NULL;
358}
Word sectionCount() const
Section * section(Word index) const
@ ST_ADDRSP
Address space section.
Definition Section.hh:77
virtual SectionType type() const =0
Returns SectioType of actual section instance.
Binary * binary_
Last or currently read binary.
Definition TPEFReader.hh:90

References binary_, TPEF::Binary::section(), TPEF::Binary::sectionCount(), TPEF::Section::ST_ADDRSP, and TPEF::Section::type().

Referenced by TPEF::TPEFRelocSectionReader::finalize().

Here is the call graph for this function:

◆ instance()

BinaryReader * TPEF::TPEFReader::instance ( )
static

Returns instance of reader.

Returns
Instance used for reading.

Definition at line 262 of file TPEFReader.cc.

262 {
263 if (proto_ == NULL) {
264 proto_ = new TPEFReader();
265 }
266 return proto_;
267}

References proto_, and TPEFReader().

Referenced by TPEF::TPEFSectionReader::parent().

Here is the call graph for this function:

◆ isMyStreamType()

bool TPEF::TPEFReader::isMyStreamType ( BinaryStream stream) const
protectedvirtual

Checks if this reader is capable of reading the given stream.

This method does not modify the stream pointer.

Parameters
streamThe stream to test.
Returns
True if stream contains data in expected format, false otherwise.
Exceptions
UnreachableStreamIf there occurs an exception with stream.

Implements TPEF::BinaryReader.

Definition at line 231 of file TPEFReader.cc.

231 {
232 unsigned long startPos = stream.readPosition();
233
234 try {
235 if (stream.readByte() == 0x7f &&
236 stream.readByte() == 'T' &&
237 stream.readByte() == 'T' &&
238 stream.readByte() == 'A' &&
239 stream.readByte() == '-' &&
240 stream.readByte() == 'P' &&
241 stream.readByte() == 'F' &&
242 stream.readByte() == 0x00) {
243
244 stream.setReadPosition(startPos);
245 return true;
246 }
247 } catch (EndOfFile& e) {
248 stream.setReadPosition(startPos);
249 return false;
250 }
251
252 stream.setReadPosition(startPos);
253 return false;
254}

References TPEF::BinaryStream::readByte(), TPEF::BinaryStream::readPosition(), and TPEF::BinaryStream::setReadPosition().

Here is the call graph for this function:

◆ operator=()

TPEFReader & TPEF::TPEFReader::operator= ( const TPEFReader )
protected

◆ readData()

Binary * TPEF::TPEFReader::readData ( BinaryStream stream) const
protectedvirtual

Does the actual work of reading binary file from stream.

Parameters
streamStream to be read from.
Returns
Pointer to the Binary object created.
Exceptions
UnreachableStreamIf reading of section fails.
KeyAlreadyExistsKey was in use whent rying to register object.
EndOfFileIf end of file were reached while it shouldn't.
OutOfRangeSome of read values were out of range.
WrongSubclassSome class couldn't do what it was asked for.
UnexpectedValueIf there was unexpected value when reading.

Implements TPEF::BinaryReader.

Definition at line 94 of file TPEFReader.cc.

94 {
95 binary_ = new Binary();
96 sectionSizes_.clear();
97
98 // let's read file header stuff
99 FileOffset startOffset = stream.readPosition();
100
101 // read file format identifier (including TPEF version number)
102 stream.setReadPosition(startOffset + TPEFHeaders::FH_ID);
103 for (Word i = 0; i < TPEFHeaders::FH_ID_SIZE; i++) {
104 // TPEF version number is located in the 9th Byte of FH_ID header
105 if (i == 8) {
107 static_cast<TPEFHeaders::TPEFVersion>(stream.readByte());
108 stream.setTPEFVersion(version);
109 binary_->setTPEFVersion(version);
110 } else {
111 stream.readByte();
112 }
113 }
114
115 stream.setReadPosition(startOffset + TPEFHeaders::FH_ARCH);
117 static_cast<Binary::FileArchitecture>(stream.readByte()));
118
119 stream.setReadPosition(startOffset + TPEFHeaders::FH_TYPE);
121 static_cast<Binary::FileType>(stream.readByte()));
122
123 stream.setReadPosition(startOffset + TPEFHeaders::FH_SHOFF);
124 FileOffset offsetToHeader = stream.readWord();
125
126 // skip size of file header
127
128 stream.setReadPosition(startOffset + TPEFHeaders::FH_SHSIZE);
129 HalfWord sectionHeaderSize = stream.readHalfWord();
130
131 stream.setReadPosition(startOffset + TPEFHeaders::FH_SHNUM);
132 HalfWord sectionHeaderCount = stream.readHalfWord();
133
134 // string table of section names
135 stream.setReadPosition(startOffset + TPEFHeaders::FH_SHSTRTAB);
136 Word stringTableFileOffset = stream.readWord();
137
138 if (stringTableFileOffset != 0) {
139 FileOffsetKey fKey(stringTableFileOffset);
140 binary_->setStrings(CREATE_SAFEPOINTER(fKey));
141 } else {
143 }
144
145 // get identification code of string table section of binary
146 if (stringTableFileOffset != 0) {
147 stream.setReadPosition(stringTableFileOffset + TPEFHeaders::SH_ID);
148 SectionId stringTableSectionId = stream.readHalfWord();
149 strTableId_ = stringTableSectionId;
150 } else {
151 strTableId_ = 0;
152 }
153
154 // get identification code of address space section of binary
155 SectionId aSpaceSectionId = 0;
156 bool programSectionWasFound = false;
157 for (Word i = offsetToHeader;
158 i < offsetToHeader + (sectionHeaderCount*sectionHeaderSize);
159 i += sectionHeaderSize) {
160
161 // go to start of section header
162 stream.setReadPosition(i + TPEFHeaders::SH_TYPE);
163
164 Section::SectionType sectionTypeToCheck =
165 static_cast<Section::SectionType>(stream.readByte());
166
167 // if section is ASpaceSection
168 if (sectionTypeToCheck == Section::ST_ADDRSP) {
169 bool brokenBinaryMoreThanOneASpaceSection =
170 (aSpaceSectionId == 0);
171 assert(brokenBinaryMoreThanOneASpaceSection);
172
173 stream.setReadPosition(i + TPEFHeaders::SH_ID);
174 aSpaceSectionId = stream.readHalfWord();
175
176 } else if(Section::isProgramSection(sectionTypeToCheck)) {
177 programSectionWasFound = true;
178 }
179 }
180
181 // there must be address space
182 assert(aSpaceSectionId != 0);
183 aSpaceId_ = aSpaceSectionId;
184
185 // let's do some serious section-reading...
186 for (HalfWord i = 0; i < sectionHeaderCount; i++) {
187
188 stream.setReadPosition(offsetToHeader + TPEFHeaders::SH_TYPE);
189
190 Section* sect = Section::createSection(
191 static_cast<Section::SectionType>(stream.readByte()));
192
193 FileOffsetKey sectionFileOffsetKey(offsetToHeader);
194 SafePointer::addObjectReference(sectionFileOffsetKey, sect);
195
196 stream.setReadPosition(offsetToHeader);
197
198 SectionReader::readSection(stream, sect, proto_);
199
200 // next header
201 offsetToHeader += sectionHeaderSize;
202 binary_->addSection(sect);
203 }
204
205 // NOTE: This check might be better to do in Binary classes addSection
206 // method. Because it's Binary classes job to prevent illegal
207 // section set. This check only prevents adding too many of these
208 // section while reading.
209
210 // make sure that binary has needed sctions and do not have too many
211 // resource or address space sections
216 programSectionWasFound == false));
217
218 return binary_;
219}
#define assert(condition)
void addSection(Section *section)
void setArch(FileArchitecture arch)
void setType(FileType type)
void setTPEFVersion(TPEFHeaders::TPEFVersion version)
void setStrings(StringSection *strTable)
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
static const SafePointer null
The default SafePointer that is used in null references.
static void readSection(BinaryStream &stream, Section *section, BinaryReader *reader)
static Section * createSection(SectionType type)
Definition Section.cc:91
bool isProgramSection() const
@ ST_NULL
NULL Section.
Definition Section.hh:70
@ ST_MR
Machine resources section.
Definition Section.hh:78
@ FH_ARCH
Architecture template.
@ FH_SHSIZE
Size of section header entry.
@ FH_SHSTRTAB
Offset to header of string table.
@ FH_ID
File identification code.
@ FH_SHNUM
Number of section headers.
@ FH_SHOFF
Offset to first section header.
@ FH_TYPE
Type of TTA program.
@ SH_ID
Section identification code.
@ SH_TYPE
Type of section.
const Byte FH_ID_SIZE
Size of file identification code.
HalfWord SectionId
Type for storing binary file section ids.
Word FileOffset
Type for storing absolute file offsets.

References TPEF::ReferenceManager::SafePointer::addObjectReference(), TPEF::Binary::addSection(), aSpaceId_, assert, binary_, TPEF::Section::createSection(), TPEF::TPEFHeaders::FH_ARCH, TPEF::TPEFHeaders::FH_ID, TPEF::TPEFHeaders::FH_ID_SIZE, TPEF::TPEFHeaders::FH_SHNUM, TPEF::TPEFHeaders::FH_SHOFF, TPEF::TPEFHeaders::FH_SHSIZE, TPEF::TPEFHeaders::FH_SHSTRTAB, TPEF::TPEFHeaders::FH_TYPE, TPEF::Section::isProgramSection(), TPEF::ReferenceManager::SafePointer::null, proto_, TPEF::BinaryStream::readByte(), TPEF::BinaryStream::readHalfWord(), TPEF::BinaryStream::readPosition(), TPEF::SectionReader::readSection(), TPEF::BinaryStream::readWord(), TPEF::Binary::sectionCount(), sectionSizes_, TPEF::Binary::setArch(), TPEF::BinaryStream::setReadPosition(), TPEF::Binary::setStrings(), TPEF::Binary::setTPEFVersion(), TPEF::BinaryStream::setTPEFVersion(), TPEF::Binary::setType(), TPEF::TPEFHeaders::SH_ID, TPEF::TPEFHeaders::SH_TYPE, TPEF::Section::ST_ADDRSP, TPEF::Section::ST_MR, TPEF::Section::ST_NULL, and strTableId_.

Here is the call graph for this function:

◆ readSection()

Section * TPEF::TPEFReader::readSection ( BinaryStream stream,
FileOffset  headerOffset,
Byte  stringTableId,
Byte  aSpaceSectionId 
) const
private

◆ sectionOfAddress()

Section * TPEF::TPEFReader::sectionOfAddress ( const ASpaceElement aSpace,
AddressImage  address 
) const

Resolves, which section is in given address in last read binary file.

Parameters
aSpaceAddress space in which section resides in memory image.
addressAddress whose section is resolved.
Returns
Section to which given offset refers.

Definition at line 277 of file TPEFReader.cc.

279 {
280
281 assert(binary_ != NULL);
282
283 for (Word i = 0; i < binary_->sectionCount(); i++) {
284
285 Section *sect = binary_->section(i);
286
287 // if section have same aSpace that in parameter
288 if (sect->aSpace() == aSpace) {
289
290 Word lastSectionAddress = sect->startingAddress();
291
292 if (sect->isCodeSection()) {
293 lastSectionAddress +=
294 dynamic_cast<CodeSection*>(sect)->instructionCount();
295
296 } else {
297 lastSectionAddress +=
298 dynamic_cast<RawSection*>(sect)->lengthInMAUs();
299 }
300
301 // test if address is inside base + sh_size
302 if (address < lastSectionAddress &&
303 address >= sect->startingAddress()) {
304
305 return sect;
306 }
307 }
308 }
309
310 return NULL;
311}
AddressImage startingAddress() const

References TPEF::Section::aSpace(), assert, binary_, TPEF::Section::isCodeSection(), TPEF::Binary::section(), TPEF::Binary::sectionCount(), and TPEF::Section::startingAddress().

Referenced by TPEF::TPEFRelocSectionReader::finalize().

Here is the call graph for this function:

◆ strTableId()

SectionId TPEF::TPEFReader::strTableId ( )

Returns the identification code of string table section that contains the names of the sections.

Returns
Identification code of a string table section.

Definition at line 341 of file TPEFReader.cc.

341 {
342 return strTableId_;
343}

References strTableId_.

Referenced by TPEF::TPEFSectionReader::readData().

Member Data Documentation

◆ aSpaceId_

SectionId TPEF::TPEFReader::aSpaceId_
mutableprivate

Identification code of address space section (there is only one at maximum).

Definition at line 93 of file TPEFReader.hh.

Referenced by aSpaceId(), and readData().

◆ binary_

Binary* TPEF::TPEFReader::binary_
mutableprivate

Last or currently read binary.

Definition at line 90 of file TPEFReader.hh.

Referenced by aSpaceSection(), readData(), and sectionOfAddress().

◆ proto_

TPEFReader * TPEF::TPEFReader::proto_ = NULL
staticprivate

Prototype instance registered into BinaryReader.

Definition at line 87 of file TPEFReader.hh.

Referenced by instance(), readData(), and ~TPEFReader().

◆ sectionSizes_

std::map<const Section*, Word> TPEF::TPEFReader::sectionSizes_
mutableprivate

Cache of sizes in addresses of read sections.

Definition at line 97 of file TPEFReader.hh.

Referenced by addSectionSize(), and readData().

◆ strTableId_

SectionId TPEF::TPEFReader::strTableId_
mutableprivate

Identification code of string table containing section name strings.

Definition at line 95 of file TPEFReader.hh.

Referenced by readData(), and strTableId().


The documentation for this class was generated from the following files: