OpenASIP  2.0
Protected Member Functions | Private Member Functions | Static Private Attributes | List of all members
TPEF::TPEFRelocSectionReader Class Reference

#include <TPEFRelocSectionReader.hh>

Inheritance diagram for TPEF::TPEFRelocSectionReader:
Inheritance graph
Collaboration diagram for TPEF::TPEFRelocSectionReader:
Collaboration graph

Protected Member Functions

 TPEFRelocSectionReader ()
 
virtual ~TPEFRelocSectionReader ()
 
virtual void finalize (Section *section) const
 
virtual void readData (BinaryStream &stream, Section *section) const
 
virtual Section::SectionType type () const
 
void readInfo (BinaryStream &stream, Section *sect) const
 
- Protected Member Functions inherited from TPEF::TPEFSectionReader
 TPEFSectionReader ()
 
virtual BinaryReaderparent () const
 
- Protected Member Functions inherited from TPEF::SectionReader
 SectionReader ()
 

Private Member Functions

 TPEFRelocSectionReader (const TPEFRelocSectionReader &)
 Copying not allowed. More...
 
TPEFRelocSectionReaderoperator= (TPEFRelocSectionReader &)
 Assignment not allowed. More...
 

Static Private Attributes

static TPEFRelocSectionReader proto_
 Prototype instance of TPEFRelocSectionReader to be registered to SectionReader. More...
 
static SectionId refSectionId_ = 0
 Section id of last referenced section. More...
 

Additional Inherited Members

- Public Types inherited from TPEF::SectionReader
typedef BinaryReader::Length Length
 
- Public Member Functions inherited from TPEF::TPEFSectionReader
virtual ~TPEFSectionReader ()
 
- Public Member Functions inherited from TPEF::SectionReader
virtual ~SectionReader ()
 
- Static Public Member Functions inherited from TPEF::SectionReader
static void readSection (BinaryStream &stream, Section *section, BinaryReader *reader)
 
static void finalizeBinary (Binary *binaryToFinalize, BinaryReader *reader)
 
- Static Protected Member Functions inherited from TPEF::TPEFSectionReader
static const Headerheader ()
 
- Static Protected Member Functions inherited from TPEF::SectionReader
static const SectionReaderfindSectionReader (const Section::SectionType type, const BinaryReader *bReader)
 
static void registerSectionReader (const SectionReader *sReader)
 

Detailed Description

Reads relocation section from TPEF binary file.

Definition at line 47 of file TPEFRelocSectionReader.hh.

Constructor & Destructor Documentation

◆ TPEFRelocSectionReader() [1/2]

TPEF::TPEFRelocSectionReader::TPEFRelocSectionReader ( )
protected

Constructor.

Registers itself to SectionReader.

Definition at line 77 of file TPEFRelocSectionReader.cc.

References TPEF::SectionReader::registerSectionReader().

Here is the call graph for this function:

◆ ~TPEFRelocSectionReader()

TPEF::TPEFRelocSectionReader::~TPEFRelocSectionReader ( )
protectedvirtual

Destructor.

Definition at line 84 of file TPEFRelocSectionReader.cc.

84  {
85 }

◆ TPEFRelocSectionReader() [2/2]

TPEF::TPEFRelocSectionReader::TPEFRelocSectionReader ( const TPEFRelocSectionReader )
private

Copying not allowed.

Member Function Documentation

◆ finalize()

void TPEF::TPEFRelocSectionReader::finalize ( Section section) const
protectedvirtual

Finalizer method for TPEF relocation sections.

Resolves destination fields of relocation elements.

Parameters
sectionSection to finalize.

Reimplemented from TPEF::SectionReader.

Definition at line 182 of file TPEFRelocSectionReader.cc.

182  {
183 
184  Section *refSection =
185  dynamic_cast<RelocSection*>(section)->referencedSection();
186 
187  TPEFReader* tpefReader = dynamic_cast<TPEFReader*>(parent());
188 
189  for (Word i = 0; i < section->elementCount(); i++) {
190 
191  RelocElement *elem =
192  dynamic_cast<RelocElement*>(section->element(i));
193 
194  assert(elem->location() != NULL);
195 
196  // file offset of destination element to find
197  AddressImage address = 0;
198 
199  // should we dig up code or data section
200  if (refSection->isCodeSection()) {
201 
202  // value of immediate element, is an address to that element,
203  // which to immediate refers
204  ImmediateElement *imm =
205  dynamic_cast<ImmediateElement*>(elem->location());
206 
207  // relocation must have Immediate or Chunk element as location
208  assert(imm != NULL);
209  assert(imm->length() <= sizeof(address));
210 
211  // read destination address of relocation
212  unsigned int neededBits = MathTools::requiredBits(imm->word());
213  if (neededBits > elem->size()) {
214  throw OutOfRange(
215  __FILE__, __LINE__, __func__,
216  (boost::format(
217  "Destination address width %d of relocation is "
218  "bigger than the field size %d.")
219  % neededBits % elem->size()).str());
220  }
221 
222  address = imm->word();
223 
224 
225  } else if (refSection->type() == Section::ST_DATA) {
226 
227  try {
228  DataSection *dataSect =
229  dynamic_cast<DataSection*>(refSection);
230 
231  int startIndex =
232  dataSect->chunkToMAUIndex(
233  dynamic_cast<Chunk*>(elem->location()));
234 
235  int mauSize = dataSect->aSpace()->MAU();
236 
237  // BE
238  for (int i = 0; i < elem->size() / mauSize; i++) {
239  address = address << mauSize;
240  address = address | dataSect->MAU(startIndex+i);
241  }
242 
243  } catch (const OutOfRange &e) {
244  bool requestedChunkWasOutOfRange = false;
245  assert(requestedChunkWasOutOfRange);
246  }
247 
248  } else if (refSection->type() == Section::ST_LEDATA) {
249 
250  try {
251  LEDataSection *dataSect =
252  dynamic_cast<LEDataSection*>(refSection);
253 
254  int startIndex =
255  dataSect->chunkToMAUIndex(
256  dynamic_cast<Chunk*>(elem->location()));
257 
258  int mauSize = dataSect->aSpace()->MAU();
259 
260  // LE
261  for (int i = 0; i < elem->size() / mauSize; i++) {
262  address |= (dataSect->MAU(startIndex+i) << (mauSize*i));
263  }
264 
265  } catch (const OutOfRange &e) {
266  bool requestedChunkWasOutOfRange = false;
267  assert(requestedChunkWasOutOfRange);
268  }
269 
270 
271 
272  } else {
273  bool referencedSectionMustBeEitherCodeOrData = false;
274  assert(referencedSectionMustBeEitherCodeOrData);
275  }
276 
277  // if unresolved relocation leave destination to undefined
278  if (tpefReader->aSpaceSection()->isUndefined(elem->aSpace())) {
279  continue;
280  }
281 
282  // absolute address depending on relocation type and value
283  AddressImage absoluteAddress =
284  Locator::absoluteAddress(address, elem->type());
285 
286  // here we need to resolve section of section referenced
287  // by value of location
288  Section *destSection =
289  tpefReader->sectionOfAddress(elem->aSpace(), absoluteAddress);
290 
291  assert(destSection != NULL);
292 
293  // do the magic!
294 
295  // find section identification code
296  SectionId destSectionId =
297  SafePointer::sectionKeyFor(destSection).sectionId();
298 
299  SectionOffset sectOffsetOfElement;
300 
301  // if code section check instructionsize and encoding from
302  // refSection...
303  if (destSection->isCodeSection()) {
304  CodeSection *codeSection =
305  dynamic_cast<CodeSection*>(destSection);
306 
307  // find instruction number of absolute address
308  Word instructionNumber =
309  absoluteAddress - codeSection->startingAddress();
310 
311  // every index is stored for every instruction element that is
312  // start of instruction
313  SectionIndexKey sectIndexKey(destSectionId, instructionNumber);
314  elem->setDestination(CREATE_SAFEPOINTER(sectIndexKey));
315 
316  } else {
317 
318  Word mauInBytes =
319  static_cast<Word>(
320  ceil(static_cast<double>(elem->aSpace()->MAU()) /
321  static_cast<double>(BYTE_BITWIDTH)));
322 
323  // else find just section offset
324  sectOffsetOfElement =
325  (absoluteAddress - destSection->startingAddress()) *
326  mauInBytes;
327 
328  SectionOffsetKey sectOffKey(destSectionId, sectOffsetOfElement);
329  elem->setDestination(CREATE_SAFEPOINTER(sectOffKey));
330  }
331  }
332 }

References __func__, TPEF::Locator::absoluteAddress(), TPEF::RelocElement::aSpace(), TPEF::Section::aSpace(), TPEF::TPEFReader::aSpaceSection(), assert, BYTE_BITWIDTH, TPEF::RawSection::chunkToMAUIndex(), TPEF::Section::element(), TPEF::Section::elementCount(), TPEF::Section::isCodeSection(), TPEF::ASpaceSection::isUndefined(), TPEF::ImmediateElement::length(), TPEF::RelocElement::location(), TPEF::ASpaceElement::MAU(), TPEF::DataSection::MAU(), TPEF::TPEFSectionReader::parent(), MathTools::requiredBits(), TPEF::ReferenceManager::SectionKey::sectionId(), TPEF::ReferenceManager::SafePointer::sectionKeyFor(), TPEF::TPEFReader::sectionOfAddress(), TPEF::RelocElement::setDestination(), TPEF::RelocElement::size(), TPEF::Section::ST_DATA, TPEF::Section::ST_LEDATA, TPEF::Section::startingAddress(), TPEF::RelocElement::type(), TPEF::Section::type(), and TPEF::ImmediateElement::word().

Here is the call graph for this function:

◆ operator=()

TPEFRelocSectionReader& TPEF::TPEFRelocSectionReader::operator= ( TPEFRelocSectionReader )
private

Assignment not allowed.

◆ readData()

void TPEF::TPEFRelocSectionReader::readData ( BinaryStream stream,
Section section 
) const
protectedvirtual

Reads section data from TPEF binary file.

Parameters
streamStream to be read from.
sectionSection where the information is to be stored.
Exceptions
UnreachableStreamIf reading of section fails.
KeyAlreadyExistsKey was in use when trying 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.
UnexpectedValueIft here was unexpected value when reading.

Reimplemented from TPEF::TPEFSectionReader.

Definition at line 110 of file TPEFRelocSectionReader.cc.

110  {
111  // base classes implementation must be called with these.
112  TPEFSectionReader::readData(stream, section);
113 
114  RelocSection* relocSection = dynamic_cast<RelocSection*>(section);
115  assert(relocSection != NULL);
116 
117  // check that link section is defined properly
118  assert(header().linkId != 0);
119 
120  if (!section->noBits()) {
121 
122  // store start of first element
123  SectionOffset elementStart = header().bodyOffset;
124  SectionIndex id = 0;
125 
126  while (elementStart + header().elementSize <=
127  header().bodyOffset + header().bodyLength) {
128 
129  RelocElement *elem = new RelocElement();
130 
131  SectionIndexKey sectionIndexKey(header().sectionId, id);
132  SafePointer::addObjectReference(sectionIndexKey, elem);
133 
134  // r_offset is source element section offset
135  Word rOffset = stream.readWord();
136  SectionOffsetKey sKey(refSectionId_, rOffset);
137  elem->setLocation(CREATE_SAFEPOINTER(sKey));
138 
139  // r_symbol
140  Word rSymbol = stream.readWord();
141  SectionIndexKey indexKey(header().linkId, rSymbol);
142  elem->setSymbol(CREATE_SAFEPOINTER(indexKey));
143 
144  // r_type
145  Byte rType = stream.readByte();
146  elem->setType(
147  static_cast<RelocElement::RelocType>(
149 
150  elem->setChunked(rType & TPEFHeaders::STF_CHUNK);
151 
152  // r_asp
153  Byte rASP = stream.readByte();
154  SectionIndexKey aSpaceIndexKey(
155  dynamic_cast<TPEFReader*>(parent())->aSpaceId(), rASP);
156  elem->setASpace(CREATE_SAFEPOINTER(aSpaceIndexKey));
157 
158  // r_size
159  elem->setSize(stream.readByte());
160 
161  // r_bitpos skipped for now
162  elem->setBitOffset(stream.readByte());
163 
164  section->addElement(elem);
165 
166  elementStart += header().elementSize;
167  stream.setReadPosition(elementStart);
168 
169  id++;
170  }
171  }
172 }

References TPEF::Section::addElement(), TPEF::ReferenceManager::SafePointer::addObjectReference(), assert, TPEF::TPEFSectionReader::Header::bodyOffset, TPEF::TPEFSectionReader::Header::elementSize, TPEF::TPEFSectionReader::header(), TPEF::Section::noBits(), TPEF::TPEFSectionReader::parent(), TPEF::BinaryStream::readByte(), TPEF::TPEFSectionReader::readData(), TPEF::BinaryStream::readWord(), refSectionId_, TPEF::RelocElement::setASpace(), TPEF::RelocElement::setBitOffset(), TPEF::RelocElement::setChunked(), TPEF::RelocElement::setLocation(), TPEF::BinaryStream::setReadPosition(), TPEF::RelocElement::setSize(), TPEF::RelocElement::setSymbol(), TPEF::RelocElement::setType(), TPEF::TPEFHeaders::STF_CHUNK, and TPEF::TPEFHeaders::STF_RELOCATION_TYPE_MASK.

Here is the call graph for this function:

◆ readInfo()

void TPEF::TPEFRelocSectionReader::readInfo ( BinaryStream stream,
Section sect 
) const
protectedvirtual

Reads info field of section header.

Read position of stream will be moved 4 bytes forward.

Parameters
streamStream where from info word is read.
sectionReloc section whose info field is read.

Reimplemented from TPEF::TPEFSectionReader.

Definition at line 343 of file TPEFRelocSectionReader.cc.

344  {
345 
346  // referenced section identifier is in first 2 bytes of info field
347  refSectionId_ = stream.readHalfWord();
348 
349  if (refSectionId_ != 0) {
350  SectionKey sKey(refSectionId_);
351  dynamic_cast<RelocSection*>(sect)->setReferencedSection(
352  CREATE_SAFEPOINTER(sKey));
353  } else {
354  bool referencedSectionOfRelocationMustNotBeNullSection = false;
355  assert(referencedSectionOfRelocationMustNotBeNullSection);
356  }
357 
358  // skip rest 2 of 4 bytes
359  stream.readHalfWord();
360 }

References assert, TPEF::BinaryStream::readHalfWord(), and refSectionId_.

Here is the call graph for this function:

◆ type()

Section::SectionType TPEF::TPEFRelocSectionReader::type ( ) const
protectedvirtual

Returns the type of section which the reader can read.

Returns
The type of section which reader can read.

Implements TPEF::SectionReader.

Definition at line 93 of file TPEFRelocSectionReader.cc.

93  {
94  return Section::ST_RELOC;
95 }

References TPEF::Section::ST_RELOC.

Member Data Documentation

◆ proto_

TPEFRelocSectionReader TPEF::TPEFRelocSectionReader::proto_
staticprivate

Prototype instance of TPEFRelocSectionReader to be registered to SectionReader.

Definition at line 68 of file TPEFRelocSectionReader.hh.

◆ refSectionId_

SectionId TPEF::TPEFRelocSectionReader::refSectionId_ = 0
staticprivate

Section id of last referenced section.

Definition at line 71 of file TPEFRelocSectionReader.hh.

Referenced by readData(), and readInfo().


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::TPEFSectionReader::TPEFSectionReader
TPEFSectionReader()
Definition: TPEFSectionReader.cc:50
TPEF::TPEFSectionReader::Header::bodyOffset
Word bodyOffset
Definition: TPEFSectionReader.hh:69
TPEF::TPEFRelocSectionReader::refSectionId_
static SectionId refSectionId_
Section id of last referenced section.
Definition: TPEFRelocSectionReader.hh:71
OutOfRange
Definition: Exception.hh:320
TPEF::TPEFHeaders::STF_RELOCATION_TYPE_MASK
@ STF_RELOCATION_TYPE_MASK
Mask for getting reloc type.
Definition: TPEFHeaders.hh:104
TPEF::ReferenceManager::SectionKey::sectionId
SectionId sectionId() const
Byte
unsigned char Byte
Definition: BaseType.hh:116
TPEF::ReferenceManager::SafePointer::addObjectReference
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
Definition: SafePointer.cc:306
TPEF::TPEFSectionReader::Header::elementSize
Word elementSize
Definition: TPEFSectionReader.hh:68
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::Section::ST_DATA
@ ST_DATA
Initialized data section.
Definition: Section.hh:80
TPEF::Section::ST_LEDATA
@ ST_LEDATA
Initialized little endian data section.
Definition: Section.hh:82
TPEF::Locator::absoluteAddress
static AddressImage absoluteAddress(AddressImage relocationValue, RelocElement::RelocType relocType)
Definition: Locator.cc:46
TPEF::ReferenceManager::SafePointer::sectionKeyFor
static SectionKey sectionKeyFor(const SafePointable *obj)
Definition: SafePointer.cc:408
__func__
#define __func__
Definition: Application.hh:67
TPEF::SectionReader::registerSectionReader
static void registerSectionReader(const SectionReader *sReader)
Definition: SectionReader.cc:145
MathTools::requiredBits
static int requiredBits(unsigned long int number)
TPEF::TPEFHeaders::STF_CHUNK
@ STF_CHUNK
Relocation applied to chunk(1) or complete address(0).
Definition: TPEFHeaders.hh:105
AddressImage
UInt32 AddressImage
Type for storing addresses to memory image.
Definition: BaseType.hh:179
TPEF::TPEFSectionReader::parent
virtual BinaryReader * parent() const
Definition: TPEFSectionReader.cc:65
TPEF::SectionOffset
Word SectionOffset
Type for storing offsets relative to a given base offset value.
Definition: TPEFBaseType.hh:49
TPEF::TPEFSectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const
Definition: TPEFSectionReader.cc:86
BYTE_BITWIDTH
const Byte BYTE_BITWIDTH
Definition: BaseType.hh:136
TPEF::Section::ST_RELOC
@ ST_RELOC
Relocation section.
Definition: Section.hh:74
TPEF::RelocElement::RelocType
RelocType
Definition: RelocElement.hh:56
TPEF::SectionIndex
Word SectionIndex
Type for storing section indexes.
Definition: TPEFBaseType.hh:46
TPEF::TPEFSectionReader::header
static const Header & header()
Definition: TPEFSectionReader.cc:174