OpenASIP  2.0
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. More...
 
SectionId aSpaceId_
 Identification code of address space section (there is only one at maximum). More...
 
SectionId strTableId_
 Identification code of string table containing section name strings. More...
 
std::map< const Section *, Word > sectionSizes_
 Cache of sizes in addresses of read sections. More...
 

Static Private Attributes

static TPEFReaderproto_ = NULL
 Prototype instance registered into BinaryReader. More...
 

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.

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 }

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 }

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 }

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) {
106  TPEFHeaders::TPEFVersion version =
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);
116  binary_->setArch(
117  static_cast<Binary::FileArchitecture>(stream.readByte()));
118 
119  stream.setReadPosition(startOffset + TPEFHeaders::FH_TYPE);
120  binary_->setType(
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 }

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::BinaryStream::setTPEFVersion(), TPEF::Binary::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 }

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:
TPEF::SectionId
HalfWord SectionId
Type for storing binary file section ids.
Definition: TPEFBaseType.hh:43
TPEF::TPEFHeaders::FH_TYPE
@ FH_TYPE
Type of TTA program.
Definition: TPEFHeaders.hh:67
TPEF::Binary::FileArchitecture
FileArchitecture
Definition: Binary.hh:67
TPEF::TPEFHeaders::SH_TYPE
@ SH_TYPE
Type of section.
Definition: TPEFHeaders.hh:80
TPEF::Binary::setTPEFVersion
void setTPEFVersion(TPEFHeaders::TPEFVersion version)
TPEF::TPEFReader::sectionSizes_
std::map< const Section *, Word > sectionSizes_
Cache of sizes in addresses of read sections.
Definition: TPEFReader.hh:97
TPEF::TPEFReader::proto_
static TPEFReader * proto_
Prototype instance registered into BinaryReader.
Definition: TPEFReader.hh:87
TPEF::Section::type
virtual SectionType type() const =0
Returns SectioType of actual section instance.
TPEF::ReferenceManager::SafePointer::null
static const SafePointer null
The default SafePointer that is used in null references.
Definition: SafePointer.hh:229
TPEF::TPEFHeaders::FH_ID_SIZE
const Byte FH_ID_SIZE
Size of file identification code.
Definition: TPEFHeaders.hh:50
TPEF::Binary::addSection
void addSection(Section *section)
TPEF::Binary::setType
void setType(FileType type)
TPEF::Binary::section
Section * section(Word index) const
TPEF::Binary::sectionCount
Word sectionCount() const
TPEF::ReferenceManager::SafePointer::addObjectReference
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
Definition: SafePointer.cc:306
TPEF::TPEFHeaders::FH_SHSIZE
@ FH_SHSIZE
Size of section header entry.
Definition: TPEFHeaders.hh:70
TPEF::TPEFHeaders::FH_SHSTRTAB
@ FH_SHSTRTAB
Offset to header of string table.
Definition: TPEFHeaders.hh:72
TPEF::TPEFReader::TPEFReader
TPEFReader()
Definition: TPEFReader.cc:66
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::TPEFHeaders::TPEFVersion
TPEFVersion
Definition: TPEFHeaders.hh:56
TPEF::TPEFReader::strTableId_
SectionId strTableId_
Identification code of string table containing section name strings.
Definition: TPEFReader.hh:95
TPEF::Section::createSection
static Section * createSection(SectionType type)
Definition: Section.cc:91
TPEF::TPEFHeaders::FH_SHOFF
@ FH_SHOFF
Offset to first section header.
Definition: TPEFHeaders.hh:68
TPEF::Binary::FileType
FileType
Definition: Binary.hh:54
TPEF::TPEFHeaders::SH_ID
@ SH_ID
Section identification code.
Definition: TPEFHeaders.hh:85
TPEF::TPEFHeaders::FH_SHNUM
@ FH_SHNUM
Number of section headers.
Definition: TPEFHeaders.hh:71
TPEF::FileOffset
Word FileOffset
Type for storing absolute file offsets.
Definition: TPEFBaseType.hh:52
TPEF::TPEFReader::binary_
Binary * binary_
Last or currently read binary.
Definition: TPEFReader.hh:90
TPEF::BinaryReader::BinaryReader
BinaryReader()
Definition: BinaryReader.cc:58
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::Section::isProgramSection
bool isProgramSection() const
TPEF::Section::ST_NULL
@ ST_NULL
NULL Section.
Definition: Section.hh:70
EndOfFile
Definition: Exception.hh:189
TPEF::TPEFReader::aSpaceId_
SectionId aSpaceId_
Identification code of address space section (there is only one at maximum).
Definition: TPEFReader.hh:93
TPEF::Section::ST_MR
@ ST_MR
Machine resources section.
Definition: Section.hh:78
TPEF::Binary::setStrings
void setStrings(StringSection *strTable)
TPEF::TPEFHeaders::FH_ID
@ FH_ID
File identification code.
Definition: TPEFHeaders.hh:65
TPEF::Section::startingAddress
AddressImage startingAddress() const
TPEF::TPEFHeaders::FH_ARCH
@ FH_ARCH
Architecture template.
Definition: TPEFHeaders.hh:66
TPEF::Section::ST_ADDRSP
@ ST_ADDRSP
Address space section.
Definition: Section.hh:77
TPEF::SectionReader::readSection
static void readSection(BinaryStream &stream, Section *section, BinaryReader *reader)
Definition: SectionReader.cc:84
TPEF::Binary::setArch
void setArch(FileArchitecture arch)
TPEF::BinaryReader::registerBinaryReader
static void registerBinaryReader(BinaryReader *reader)
Definition: BinaryReader.cc:165