OpenASIP  2.0
TPEFSectionReader.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file TPEFSectionReader.cc
26  *
27  * Definitions of TPEFSectionReader class.
28  *
29  * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30  *
31  * @note rating: yellow
32  */
33 
34 #include "TPEFSectionReader.hh"
35 #include "BinaryStream.hh"
36 #include "TPEFHeaders.hh"
37 
38 namespace TPEF {
39 
40 TPEFSectionReader::Header TPEFSectionReader::header_;
41 
42 using ReferenceManager::SectionKey;
43 using ReferenceManager::SectionIndexKey;
44 using ReferenceManager::SectionOffsetKey;
45 using ReferenceManager::SafePointer;
46 
47 /**
48  * Constructor
49  */
51 }
52 
53 /**
54  * Destructor
55  */
57 }
58 
59 /**
60  * Returns binary reader instance which uses TPEFSectionReader classes.
61  *
62  * @return Binary reader which uses TPEFSectionReader classes.
63  */
66  return TPEFReader::instance();
67 }
68 
69 /**
70  * Reads section data from TPEF binary file.
71  *
72  * Base implementation for all TPEF section readers. Reads and keeps section
73  * header data for later use by concrete section readers. Header data is
74  * accessed by readData methods of subclasses.
75  *
76  * @param stream Stream to be read from.
77  * @param section Section where the information is to be stored.
78  * @exception UnreachableStream If reading of section fails.
79  * @exception KeyAlreadyExists Key was in use when trying to register object.
80  * @exception EndOfFile If end of file were reached while it shouldn't.
81  * @exception OutOfRange Some of read values were out of range.
82  * @exception WrongSubclass Some class couldn't do what it was asked for.
83  * @exception UnexpectedValue If there was unexpected value when reading.
84  */
85 void
87  TPEFReader* tpefReader = dynamic_cast<TPEFReader*>(parent());
88 
89  FileOffset startOffset = stream.readPosition();
90  stream.setReadPosition(startOffset + TPEFHeaders::SH_FLAGS);
91  Byte sectionFlags = stream.readByte();
92 
93  // if section vLen flag is not equal to the read value
94  if(section->vLen() != ((sectionFlags & Section::SF_VLEN) != 0)) {
95  throw UnexpectedValue(
96  __FILE__, __LINE__, __func__,
97  "read SF_VLEN flag doesn't match for section");
98  }
99 
100  section->setFlags(sectionFlags);
101 
102  stream.setReadPosition(startOffset + TPEFHeaders::SH_ADDR);
103  section->setStartingAddress(stream.readWord());
104 
105  stream.setReadPosition(startOffset + TPEFHeaders::SH_LINK);
106  header_.linkId = stream.readHalfWord();
107 
108  SectionKey linkKey(header_.linkId);
109  section->setLink(CREATE_SAFEPOINTER(linkKey));
110 
111  // set address space reference
112  stream.setReadPosition(startOffset + TPEFHeaders::SH_ASPACE);
113  Byte aSpaceIndex = stream.readByte();
114  SectionIndexKey aSpaceKey(tpefReader->aSpaceId(), aSpaceIndex);
115  section->setASpace(CREATE_SAFEPOINTER(aSpaceKey));
116 
117  // set name
118  stream.setReadPosition(startOffset + TPEFHeaders::SH_NAME);
119  Word sectionOffsetOfName = stream.readWord();
120 
121  if (tpefReader->strTableId() != 0) {
122  SectionOffsetKey nameKey(tpefReader->strTableId(),
123  sectionOffsetOfName);
124 
125  section->setName(CREATE_SAFEPOINTER(nameKey));
126  } else {
127  section->setName(&SafePointer::null);
128  }
129 
130  // add section key for new section
131  stream.setReadPosition(startOffset + TPEFHeaders::SH_ID);
132  header_.sectionId = stream.readHalfWord();
133 
134  SectionKey sectionKey(header_.sectionId);
135  SafePointer::addObjectReference(sectionKey, section);
136 
137  stream.setReadPosition(startOffset + TPEFHeaders::SH_INFO);
138  readInfo(stream, section);
139 
140  stream.setReadPosition(startOffset + TPEFHeaders::SH_ENTSIZE);
141  header_.elementSize = stream.readWord();
142 
143  // read offset of section body
144  stream.setReadPosition(startOffset + TPEFHeaders::SH_OFFSET);
145  header_.bodyOffset = stream.readWord();
146 
147  stream.setReadPosition(startOffset + TPEFHeaders::SH_SIZE);
148  header_.bodyLength = stream.readWord();
149 
150  // actual class does reading of section body
152 }
153 
154 /**
155  * Reads info field of section header.
156  *
157  * Read position of stream will be moved 4 bytes forward.
158  *
159  * @param stream Stream where from info word is read.
160  */
161 void
163  Section* /*sect*/) const {
164  // move four bytes forward by default
165  stream.readWord();
166 }
167 
168 /**
169  * Returns headers of section that we are currently reading.
170  *
171  * @return Headers of section that we are currently reading.
172  */
175  return header_;
176 }
177 
178 }
TPEF::TPEFReader::instance
static BinaryReader * instance()
Definition: TPEFReader.cc:262
TPEF::TPEFHeaders::SH_LINK
@ SH_LINK
Section identifier link.
Definition: TPEFHeaders.hh:88
TPEF::Section::vLen
bool vLen() const
TPEF::BinaryStream::readPosition
unsigned int readPosition()
Definition: BinaryStream.cc:561
TPEF::TPEFSectionReader::TPEFSectionReader
TPEFSectionReader()
Definition: TPEFSectionReader.cc:50
TPEF::TPEFSectionReader::Header::bodyOffset
Word bodyOffset
Definition: TPEFSectionReader.hh:69
TPEF::BinaryStream::setReadPosition
void setReadPosition(unsigned int position)
Definition: BinaryStream.cc:629
TPEF::TPEFHeaders::SH_OFFSET
@ SH_OFFSET
Offset to section data.
Definition: TPEFHeaders.hh:83
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::TPEFSectionReader::~TPEFSectionReader
virtual ~TPEFSectionReader()
Definition: TPEFSectionReader.cc:56
TPEF::ReferenceManager::SafePointer::null
static const SafePointer null
The default SafePointer that is used in null references.
Definition: SafePointer.hh:229
TPEFHeaders.hh
TPEF::TPEFHeaders::SH_ASPACE
@ SH_ASPACE
Section address space identifier.
Definition: TPEFHeaders.hh:86
TPEF::TPEFHeaders::SH_INFO
@ SH_INFO
Section specific information, usually zero.
Definition: TPEFHeaders.hh:89
Byte
unsigned char Byte
Definition: BaseType.hh:116
TPEF::ReferenceManager::SectionIndexKey
Definition: ReferenceKey.hh:65
TPEF::Section::setFlags
void setFlags(Byte flagByte)
TPEF::ReferenceManager::SafePointer::addObjectReference
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
Definition: SafePointer.cc:306
TPEF::BinaryReader
Definition: BinaryReader.hh:51
TPEF::Section
Definition: Section.hh:64
TPEF::TPEFSectionReader::Header::elementSize
Word elementSize
Definition: TPEFSectionReader.hh:68
TPEF::TPEFHeaders::SH_ENTSIZE
@ SH_ENTSIZE
Size of section elements (if fixed size).
Definition: TPEFHeaders.hh:90
UnexpectedValue
Definition: Exception.hh:455
TPEF::TPEFHeaders::SH_ID
@ SH_ID
Section identification code.
Definition: TPEFHeaders.hh:85
TPEF::TPEFSectionReader::readInfo
virtual void readInfo(BinaryStream &stream, Section *sect) const
Definition: TPEFSectionReader.cc:162
__func__
#define __func__
Definition: Application.hh:67
TPEF::FileOffset
Word FileOffset
Type for storing absolute file offsets.
Definition: TPEFBaseType.hh:52
TPEFSectionReader.hh
TPEF::SectionReader
Definition: SectionReader.hh:56
TPEF::BinaryStream::readHalfWord
HalfWord readHalfWord()
Definition: BinaryStream.cc:150
TPEF::BinaryStream::readByte
Byte readByte()
Definition: BinaryStream.cc:120
TPEF::Section::setName
void setName(const ReferenceManager::SafePointer *sectionName)
TPEF::TPEFReader::aSpaceId
SectionId aSpaceId()
Definition: TPEFReader.cc:330
TPEF::Section::setStartingAddress
void setStartingAddress(AddressImage address)
TPEF::TPEFReader::strTableId
SectionId strTableId()
Definition: TPEFReader.cc:341
TPEF::TPEFSectionReader::Header::linkId
SectionId linkId
Definition: TPEFSectionReader.hh:66
TPEF::TPEFSectionReader::parent
virtual BinaryReader * parent() const
Definition: TPEFSectionReader.cc:65
TPEF::TPEFHeaders::SH_SIZE
@ SH_SIZE
Size of section data.
Definition: TPEFHeaders.hh:84
TPEF::ReferenceManager::SectionKey
Definition: ReferenceKey.hh:145
TPEF::Section::SF_VLEN
@ SF_VLEN
Contains elements with variable length.
Definition: Section.hh:90
TPEF::TPEFSectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const
Definition: TPEFSectionReader.cc:86
TPEF::TPEFSectionReader::Header
Definition: TPEFSectionReader.hh:64
TPEF::TPEFReader
Definition: TPEFReader.hh:52
TPEF::Section::setASpace
void setASpace(const ReferenceManager::SafePointer *addrSpace)
TPEF::TPEFHeaders::SH_ADDR
@ SH_ADDR
Starting memory address of program section.
Definition: TPEFHeaders.hh:82
TPEF::Section::setLink
void setLink(const ReferenceManager::SafePointer *aLink)
TPEF::ReferenceManager::SectionOffsetKey
Definition: ReferenceKey.hh:93
BinaryStream.hh
TPEF::TPEFHeaders::SH_FLAGS
@ SH_FLAGS
Flags of section.
Definition: TPEFHeaders.hh:81
TPEF::TPEFHeaders::SH_NAME
@ SH_NAME
Section offset to name.
Definition: TPEFHeaders.hh:79
TPEF::BinaryStream::readWord
Word readWord()
Definition: BinaryStream.cc:187
TPEF::TPEFSectionReader::header_
static Header header_
Stores values that are needed in actual SectionReader classes.
Definition: TPEFSectionReader.hh:79
TPEF::TPEFSectionReader::header
static const Header & header()
Definition: TPEFSectionReader.cc:174
TPEF::TPEFSectionReader::Header::bodyLength
Word bodyLength
Definition: TPEFSectionReader.hh:70
TPEF
Definition: Assembler.hh:43
TPEF::TPEFSectionReader::Header::sectionId
SectionId sectionId
Definition: TPEFSectionReader.hh:65