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

#include <AOutRelocationSectionReader.hh>

Inheritance diagram for TPEF::AOutRelocationSectionReader:
Inheritance graph
Collaboration diagram for TPEF::AOutRelocationSectionReader:
Collaboration graph

Public Member Functions

virtual ~AOutRelocationSectionReader ()
 
- Public Member Functions inherited from TPEF::AOutSectionReader
virtual ~AOutSectionReader ()
 
- Public Member Functions inherited from TPEF::SectionReader
virtual ~SectionReader ()
 

Protected Member Functions

 AOutRelocationSectionReader ()
 
virtual void finalize (Section *section) const
 
virtual void readData (BinaryStream &stream, Section *section) const
 
virtual Section::SectionType type () const
 
- Protected Member Functions inherited from TPEF::AOutSectionReader
 AOutSectionReader ()
 
virtual BinaryReaderparent () const
 
- Protected Member Functions inherited from TPEF::SectionReader
 SectionReader ()
 

Private Types

enum  RelocType { RELOC_8, RELOC_16, RELOC_32, NO_RELOC }
 

Private Member Functions

void initializeRelocElement (BinaryStream &stream, RelocElement *elem, SectionId refSectionID, AOutReader *reader) const
 
bool checkIfExtern (Word word) const
 
 AOutRelocationSectionReader (const AOutRelocationSectionReader &)
 Copying not allowed. More...
 
AOutRelocationSectionReaderoperator= (AOutRelocationSectionReader &)
 Assignment not allowed. More...
 
RelocElement::RelocType aOutToTPEFRelocType (RelocType aOutRelocType) const
 

Static Private Attributes

static AOutRelocationSectionReader proto_
 Class-wide (unique) prototype instance of AOutRelocationSectionReader registered into SectionReader. More...
 
static const Byte RELOCATION_TYPE_MASK = 0x1F
 Mask for extracting relocation type. More...
 

Additional Inherited Members

- Public Types inherited from TPEF::SectionReader
typedef BinaryReader::Length Length
 
- 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::SectionReader
static const SectionReaderfindSectionReader (const Section::SectionType type, const BinaryReader *bReader)
 
static void registerSectionReader (const SectionReader *sReader)
 

Detailed Description

Reads relocation sections of a.out binary.

Definition at line 51 of file AOutRelocationSectionReader.hh.

Member Enumeration Documentation

◆ RelocType

Relocation types of a.out

Enumerator
RELOC_8 

1 byte relocation

RELOC_16 

2 bytes relocation

RELOC_32 

4 bytes relocation

NO_RELOC 

no relocation

Definition at line 68 of file AOutRelocationSectionReader.hh.

68  {
69  RELOC_8, ///< 1 byte relocation
70  RELOC_16, ///< 2 bytes relocation
71  RELOC_32, ///< 4 bytes relocation
72  NO_RELOC ///< no relocation
73  };

Constructor & Destructor Documentation

◆ ~AOutRelocationSectionReader()

TPEF::AOutRelocationSectionReader::~AOutRelocationSectionReader ( )
virtual

Destructor.

Definition at line 76 of file AOutRelocationSectionReader.cc.

76  {
77 }

◆ AOutRelocationSectionReader() [1/2]

TPEF::AOutRelocationSectionReader::AOutRelocationSectionReader ( )
protected

Constructor.

Registers itself to AOutSectionReader.

Definition at line 67 of file AOutRelocationSectionReader.cc.

67  :
69 
71 }

References TPEF::SectionReader::registerSectionReader().

Here is the call graph for this function:

◆ AOutRelocationSectionReader() [2/2]

TPEF::AOutRelocationSectionReader::AOutRelocationSectionReader ( const AOutRelocationSectionReader )
private

Copying not allowed.

Member Function Documentation

◆ aOutToTPEFRelocType()

RelocElement::RelocType TPEF::AOutRelocationSectionReader::aOutToTPEFRelocType ( RelocType  aOutRelocType) const
private

Converts a.out relocation type into TPEF relocation.

Parameters
aOutRelocTypeIdentification code of the relocation type to convert to TPEF format.
Returns
TPEF relocation type.

Definition at line 295 of file AOutRelocationSectionReader.cc.

296  {
297 
298  // NOTE: check this conversion.
299 
300  switch (aOutRelocType) {
301  case RELOC_8:
302  case RELOC_16:
303  case RELOC_32:
304  return RelocElement::RT_SELF;
305  case NO_RELOC:
306  return RelocElement::RT_NOREL;
307  default: {
308  bool unknownAOutRelocationType = false;
309  assert(unknownAOutRelocationType);
310  }
311  }
312 
313  // to prevent compile warnings
314  return RelocElement::RT_NOREL;
315 }

References assert, NO_RELOC, RELOC_16, RELOC_32, RELOC_8, TPEF::RelocElement::RT_NOREL, and TPEF::RelocElement::RT_SELF.

Referenced by initializeRelocElement().

◆ checkIfExtern()

bool TPEF::AOutRelocationSectionReader::checkIfExtern ( Word  word) const
private

Referenced by initializeRelocElement().

◆ finalize()

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

Finalizer method for AOut relocation sections.

Fixes values of elements pointed by location(), MOVE linker does not update values of elements referred from relocation. Also sets address spaces for relocation elements.

Parameters
sectionSection to finalize.

Reimplemented from TPEF::SectionReader.

Definition at line 224 of file AOutRelocationSectionReader.cc.

224  {
225 
226  Section *refSection =
227  dynamic_cast<RelocSection*>(section)->referencedSection();
228 
229  AOutReader *aOutReader = dynamic_cast<AOutReader*>(parent());
230  assert(aOutReader != NULL);
231 
232  for (Word i = 0;
233  i != section->elementCount();
234  i++) {
235 
236  RelocElement *elem = dynamic_cast<RelocElement*>(section->element(i));
237 
238  // address space of section where element is or undef aSpace
239  elem->setASpace(
240  dynamic_cast<AOutReader*>
241  (parent())->aSpaceOfElement(elem->destination()));
242 
243  // relocation is unresolved.
244  if (elem->destination() == NULL) continue;
245 
246  AddressImage destAddress =
247  aOutReader->addressOfElement(elem->destination());
248 
249  // fix value of immediate or chunk
250  if (refSection->isCodeSection()) {
251 
252  // value of immediate element, is file offset to
253  // that element, which to immediate refers
254  ImmediateElement *imm =
255  dynamic_cast<ImmediateElement*>(elem->location());
256 
257  // relocation must have Immediate or Chunk element as location
258  assert(imm != NULL);
259 
260  // fix the address value
261  imm->setWord(destAddress);
262 
263  } else if (refSection->type() == Section::ST_DATA) {
264  Chunk* dataChunk = dynamic_cast<Chunk*>(elem->location());
265  DataSection* dataSection = dynamic_cast<DataSection*>(refSection);
266 
267  // check that section is long enough
268  if (dataChunk->offset() + sizeof(destAddress) >
269  dataSection->length()) {
270  bool requestedChunkWasOutOfRange = false;
271  assert(requestedChunkWasOutOfRange);
272  }
273 
274  // fix address of chunk
275  Word mauIndex = dataSection->chunkToMAUIndex(dataChunk);
276 
277  dataSection->writeValue(
278  mauIndex, 4, static_cast<unsigned long>(destAddress));
279 
280  } else {
281  bool referencedSectionMustBeEitherCodeOrData = false;
282  assert(referencedSectionMustBeEitherCodeOrData);
283  }
284  }
285 }

References TPEF::AOutReader::addressOfElement(), TPEF::AOutReader::aSpaceOfElement(), assert, TPEF::RawSection::chunkToMAUIndex(), TPEF::RelocElement::destination(), TPEF::Section::element(), TPEF::Section::elementCount(), TPEF::Section::isCodeSection(), TPEF::DataSection::length(), TPEF::RelocElement::location(), TPEF::Chunk::offset(), TPEF::AOutSectionReader::parent(), TPEF::RelocElement::setASpace(), TPEF::ImmediateElement::setWord(), TPEF::Section::ST_DATA, TPEF::Section::type(), and TPEF::DataSection::writeValue().

Here is the call graph for this function:

◆ initializeRelocElement()

void TPEF::AOutRelocationSectionReader::initializeRelocElement ( BinaryStream stream,
RelocElement elem,
SectionId  refSectionID,
AOutReader reader 
) const
private

Initializes one RelocElement object with correct values.

Parameters
streamThe stream to be read from.
elemElement to be initialized.
refSectionIDIdentification code of the section that holds relocation source.
readerThe base reader for a.out.
Exceptions
UnexpectedValueIf extern bit is not zero, or if relocation target is not text or data section.
UnreachableStreamIf stream can't be read.
OutOfRangeSection offset of an address is invalid.

Definition at line 158 of file AOutRelocationSectionReader.cc.

160  {
161  Word r_address = stream.readWord();
162 
163  // if referenced section is text section fix r_address to point
164  // starting element of instruction
165  // (r_address pointed in the middle of move to immediate value before)
166  if (refSectionID == AOutReader::ST_TEXT) {
168  }
169 
170  SectionOffsetKey sKey = SectionOffsetKey(refSectionID, r_address);
171  elem->setLocation(CREATE_SAFEPOINTER(sKey));
172 
173  Word secondWord = stream.readWord();
174 
175  Word r_symbolnum = (secondWord >> BYTE_BITWIDTH);
176 
177  // addend is value that is stored in element in location pointed by
178  // r_address
179  AddressImage r_addend = stream.readWord();
180 
181  // It seems that extern flag and addend field is used to tell does
182  // relocation have symbol or section index in symbolnum field.
183  // If there is symbol it means that relocation is unresolved.
184 
185  // NOTE: above doesn't work if there is relocation to the
186  // first instruction of program in this case secondWord seems to
187  // be always 1026...
188  if (r_addend == 0 && checkIfExtern(secondWord) && secondWord != 1026) {
189  SectionIndexKey indexKey(AOutReader::ST_SYMBOL, r_symbolnum + 1);
190  elem->setSymbol(CREATE_SAFEPOINTER(indexKey));
191 
192  } else {
193  // if we are here r_symbolnum is a section id, not a symbol index
194  SectionIndexKey undefSym(AOutReader::ST_SYMBOL,0);
195  elem->setSymbol(CREATE_SAFEPOINTER(undefSym));
196 
197  Word r_offset = reader->sectionOffsetOfAddress(r_addend);
198 
199  SectionOffsetKey offKey =
200  SectionOffsetKey(r_symbolnum, r_offset);
201 
202  elem->setDestination(CREATE_SAFEPOINTER(offKey));
203  }
204 
205  // convert and set relocation type
206  RelocType r_type =
207  static_cast<RelocType>(secondWord & RELOCATION_TYPE_MASK);
208 
209  elem->setType(aOutToTPEFRelocType(r_type));
210 
211  elem->setSize(sizeof(Word)*BYTE_BITWIDTH);
212 }

References aOutToTPEFRelocType(), BYTE_BITWIDTH, checkIfExtern(), TPEF::AOutTextSectionReader::OFFSET_TO_IMMEDIATE_VALUE, TPEF::BinaryStream::readWord(), RELOCATION_TYPE_MASK, TPEF::AOutReader::sectionOffsetOfAddress(), TPEF::RelocElement::setDestination(), TPEF::RelocElement::setLocation(), TPEF::RelocElement::setSize(), TPEF::RelocElement::setSymbol(), TPEF::RelocElement::setType(), TPEF::AOutReader::ST_SYMBOL, and TPEF::AOutReader::ST_TEXT.

Referenced by readData().

Here is the call graph for this function:

◆ operator=()

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

Assignment not allowed.

◆ readData()

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

Reads relocation sections from a.out binary file.

Parameters
streamThe stream to be read from.
sectionSection where to information is 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.
UnexpectedValueIf there was unexpected value when reading.

Implements TPEF::SectionReader.

Definition at line 102 of file AOutRelocationSectionReader.cc.

103  {
104  FileOffset offset = stream.readPosition();
105 
106  AOutReader* aOutReader = dynamic_cast<AOutReader*>(parent());
107  assert(aOutReader != NULL);
108 
109  // find out whether this is for reading text relocation or data
110  // relocation
111  RelocSection* relocSection = dynamic_cast<RelocSection*>(section);
112  assert(relocSection != NULL);
113 
114  const Section* refSection = relocSection->referencedSection();
115  assert(refSection != NULL);
116 
117  SectionId refSectionID;
118 
119  Word length = 0;
120  if (refSection->isCodeSection()) {
121  refSectionID = AOutReader::ST_TEXT;
122  length = aOutReader->header().sectionSizeTextReloc();
123 
124  } else if (refSection->type() == Section::ST_DATA) {
125  refSectionID = AOutReader::ST_DATA;
126  length = aOutReader->header().sectionSizeDataReloc();
127 
128  } else {
129  string msg = "Relocating something else than text or data section";
130  throw WrongSubclass(__FILE__, __LINE__, __func__, msg);
131  }
132 
133  // all relocation information is read; to offset 'offset+length'
134  for (; stream.readPosition() < offset + length;) {
135 
136  // create and read a new relocation element
137  RelocElement* elem = new RelocElement();
138  initializeRelocElement(stream, elem, refSectionID, aOutReader);
139 
140  relocSection->addElement(elem);
141  }
142 }

References __func__, TPEF::Section::addElement(), assert, TPEF::AOutReader::header(), initializeRelocElement(), TPEF::Section::isCodeSection(), TPEF::AOutSectionReader::parent(), TPEF::BinaryStream::readPosition(), TPEF::RelocSection::referencedSection(), TPEF::AOutReader::Header::sectionSizeDataReloc(), TPEF::AOutReader::Header::sectionSizeTextReloc(), TPEF::Section::ST_DATA, TPEF::AOutReader::ST_DATA, TPEF::AOutReader::ST_TEXT, and TPEF::Section::type().

Here is the call graph for this function:

◆ type()

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

Returns the type of section that reader can read.

Returns
The type of section that reader can read.

Implements TPEF::SectionReader.

Definition at line 85 of file AOutRelocationSectionReader.cc.

85  {
86  return Section::ST_RELOC;
87 }

References TPEF::Section::ST_RELOC.

Member Data Documentation

◆ proto_

AOutRelocationSectionReader TPEF::AOutRelocationSectionReader::proto_
staticprivate

Class-wide (unique) prototype instance of AOutRelocationSectionReader registered into SectionReader.

Definition at line 91 of file AOutRelocationSectionReader.hh.

◆ RELOCATION_TYPE_MASK

const Byte TPEF::AOutRelocationSectionReader::RELOCATION_TYPE_MASK = 0x1F
staticprivate

Mask for extracting relocation type.

Definition at line 94 of file AOutRelocationSectionReader.hh.

Referenced by initializeRelocElement().


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::AOutTextSectionReader::OFFSET_TO_IMMEDIATE_VALUE
static const int OFFSET_TO_IMMEDIATE_VALUE
Definition: AOutTextSectionReader.hh:51
TPEF::AOutRelocationSectionReader::RELOCATION_TYPE_MASK
static const Byte RELOCATION_TYPE_MASK
Mask for extracting relocation type.
Definition: AOutRelocationSectionReader.hh:94
TPEF::AOutRelocationSectionReader::initializeRelocElement
void initializeRelocElement(BinaryStream &stream, RelocElement *elem, SectionId refSectionID, AOutReader *reader) const
Definition: AOutRelocationSectionReader.cc:158
TPEF::AOutRelocationSectionReader::aOutToTPEFRelocType
RelocElement::RelocType aOutToTPEFRelocType(RelocType aOutRelocType) const
Definition: AOutRelocationSectionReader.cc:295
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::AOutRelocationSectionReader::RELOC_16
@ RELOC_16
2 bytes relocation
Definition: AOutRelocationSectionReader.hh:70
TPEF::Section::ST_DATA
@ ST_DATA
Initialized data section.
Definition: Section.hh:80
TPEF::AOutSectionReader::parent
virtual BinaryReader * parent() const
Definition: AOutSectionReader.cc:57
TPEF::RelocElement::RT_SELF
@ RT_SELF
Absolute address, relocate relative to address self.
Definition: RelocElement.hh:58
WrongSubclass
Definition: Exception.hh:336
TPEF::AOutRelocationSectionReader::RelocType
RelocType
Definition: AOutRelocationSectionReader.hh:68
__func__
#define __func__
Definition: Application.hh:67
TPEF::FileOffset
Word FileOffset
Type for storing absolute file offsets.
Definition: TPEFBaseType.hh:52
TPEF::SectionReader::registerSectionReader
static void registerSectionReader(const SectionReader *sReader)
Definition: SectionReader.cc:145
TPEF::AOutSectionReader::AOutSectionReader
AOutSectionReader()
Definition: AOutSectionReader.cc:41
TPEF::AOutReader::ST_TEXT
@ ST_TEXT
Text section.
Definition: AOutReader.hh:116
AddressImage
UInt32 AddressImage
Type for storing addresses to memory image.
Definition: BaseType.hh:179
TPEF::RelocElement::RT_NOREL
@ RT_NOREL
No relocation.
Definition: RelocElement.hh:57
TPEF::AOutRelocationSectionReader::checkIfExtern
bool checkIfExtern(Word word) const
BYTE_BITWIDTH
const Byte BYTE_BITWIDTH
Definition: BaseType.hh:136
TPEF::Section::ST_RELOC
@ ST_RELOC
Relocation section.
Definition: Section.hh:74
TPEF::AOutReader::ST_SYMBOL
@ ST_SYMBOL
Symbol table.
Definition: AOutReader.hh:119
TPEF::AOutRelocationSectionReader::RELOC_8
@ RELOC_8
1 byte relocation
Definition: AOutRelocationSectionReader.hh:69
TPEF::AOutRelocationSectionReader::NO_RELOC
@ NO_RELOC
no relocation
Definition: AOutRelocationSectionReader.hh:72
TPEF::AOutReader::ST_DATA
@ ST_DATA
Data section.
Definition: AOutReader.hh:117
TPEF::AOutRelocationSectionReader::RELOC_32
@ RELOC_32
4 bytes relocation
Definition: AOutRelocationSectionReader.hh:71