OpenASIP 2.2
Loading...
Searching...
No Matches
TPEFSymbolSectionReader.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 TPEFSymbolSectionReader.cc
26 *
27 * Definition of TPEFSymbolSectionReader class.
28 *
29 * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30 *
31 * @note rating: yellow
32 */
33
35#include "SafePointer.hh"
36#include "Exception.hh"
37#include "ReferenceKey.hh"
38#include "SectionReader.hh"
39#include "TPEFBaseType.hh"
40#include "SymbolElement.hh"
41#include "SymbolSection.hh"
42#include "NoTypeSymElement.hh"
43#include "CodeSymElement.hh"
44#include "DataSymElement.hh"
45#include "ProcedSymElement.hh"
46#include "FileSymElement.hh"
47#include "SectionSymElement.hh"
48#include "BinaryStream.hh"
49#include "TPEFHeaders.hh"
50
51namespace TPEF {
52
53using ReferenceManager::SafePointer;
54using ReferenceManager::SectionIndexKey;
55using ReferenceManager::SectionOffsetKey;
56using ReferenceManager::SectionKey;
57
60
62
64
65/**
66 * Constructor.
67 *
68 * Registers itself to SectionReader.
69 */
73
74/**
75 * Destructor.
76 */
79
80/**
81 * Returns the type of section it is meant to read.
82 *
83 * @return The type of section it can read.
84 */
89
90/**
91 * Reads section data from TPEF binary file.
92 *
93 * @param stream Stream to be read from.
94 * @param section Section where the information is to be stored.
95 * @exception UnreachableStream If reading of section fails.
96 * @exception KeyAlreadyExists Key was in use when trying to register object.
97 * @exception EndOfFile If end of file were reached while it shouldn't.
98 * @exception OutOfRange Some of read value were out of range.
99 * @exception WrongSubclass Some class couldn't do what it was asked for.
100 * @exception UnexpectedValue If there was unexpected value when reading.
101 */
102void
104 BinaryStream& stream, Section* section) const {
105 // base classes implementation must be called with these.
106 TPEFSectionReader::readData(stream, section);
107
108 SymbolSection* symbolSection =
109 dynamic_cast<SymbolSection*>(section);
110 assert(symbolSection != NULL);
111
112 // create indexs' starting from 0 undefined element is checked
113 SectionIndex index = 0;
114 bool undefSymbolDefined = false;
115
116 // check that link section is defined properly
117 assert(header().linkId != 0);
118
119 if (!section->noBits()) {
120 // start of first element
121 SectionOffset elementStart = header().bodyOffset;
122
123 while (elementStart + header().elementSize <=
124 header().bodyOffset + header().bodyLength) {
125
126 SymbolElement *elem = NULL;
127
128 Word nameOffset = stream.readWord();
129 Word value = stream.readWord();
130 Word size = stream.readWord();
131 Byte info = stream.readByte();
132 Byte otherField = stream.readByte();
133
134 SectionId sectionToBelong = stream.readHalfWord();
135
136 // lower half byte is type of symbol
137 SymbolType typeOfSym =
138 static_cast<SymbolType>(info & SYMBOL_TYPE_MASK);
139
140 // upper half byte is binding
141 SymbolBinding binding =
142 static_cast<SymbolBinding>(info >> (BYTE_BITWIDTH / 2));
143
144 // symbol of right type
145 elem = createSymbol(typeOfSym, value, size, sectionToBelong);
146
147 elem->setAbsolute(otherField & TPEFHeaders::STO_ABS);
148 elem->setBinding(binding);
149
150 SectionOffsetKey sOffKey(header().linkId, nameOffset);
151 elem->setName(CREATE_SAFEPOINTER(sOffKey));
152
153 // section which to symbol belongs
154 SectionKey sKey(sectionToBelong);
155 elem->setSection(CREATE_SAFEPOINTER(sKey));
156
157 SectionIndexKey sectionIndexKey(header().sectionId, index);
158 SafePointer::addObjectReference(sectionIndexKey, elem);
159
160
161 // check undefined symbol
162 if (index == 0) {
165 assert(elem->absolute() == true);
166 assert(nameOffset == 0);
167 undefSymbolDefined = true;
168 }
169
170 section->addElement(elem);
171
172 elementStart += header().elementSize;
173 stream.setReadPosition(elementStart);
174 index++;
175 }
176 }
177
178 // create undefined symbol if not found from table
179 // (e.g. if section had nobits flag set)
180 if (!undefSymbolDefined) {
183 elem->setAbsolute(true);
184 SectionOffsetKey sOffKey(header().linkId, 0);
185 elem->setName(CREATE_SAFEPOINTER(sOffKey));
186 SectionIndexKey sectionIndexKey(header().sectionId, 0);
187 SafePointer::addObjectReference(sectionIndexKey, elem);
188 section->addElement(elem);
189 }
190}
191
192/**
193 * Creates symbol element.
194 *
195 * @param symType Type of symbol to create.
196 * @param aValue Value of element.
197 * @param aSize Size of element.
198 * @param sectToBelong Identification code of the section that contains the
199 * element.
200 * @return Newly created symbol.
201 */
205 Word aValue,
206 Word aSize,
207 SectionId sectToBelong) const {
208
209 SymbolElement *elem = NULL;
210
211 // NOTE: check symbols from latest spec and
212 // add whole element reading stuff here.
213 switch (symType) {
215 elem = new NoTypeSymElement();
216 break;
217
219 elem = new ProcedSymElement();
220 /* fall through */
221
223 if (elem == NULL) {
224 elem = new CodeSymElement();
225 }
226
227 if (sectToBelong != 0) {
228 SectionOffsetKey sOffKey(sectToBelong, aValue);
229 dynamic_cast<CodeSymElement*>
230 (elem)->setReference(CREATE_SAFEPOINTER(sOffKey));
231 dynamic_cast<CodeSymElement*>
232 (elem)->setSize(aSize);
233 }
234
235 break;
236
238 elem = new DataSymElement();
239
240 if (sectToBelong != 0) {
241 SectionOffsetKey sOffKey(sectToBelong, aValue);
242 dynamic_cast<DataSymElement*>
243 (elem)->setReference(CREATE_SAFEPOINTER(sOffKey));
244
245 dynamic_cast<DataSymElement*>
246 (elem)->setSize(aSize);
247 }
248
249 break;
250
252 elem = new SectionSymElement();
253 dynamic_cast<SectionSymElement*>(elem)->setValue(aValue);
254 dynamic_cast<SectionSymElement*>(elem)->setSize(aSize);
255 break;
256
258 elem = new FileSymElement();
259 dynamic_cast<FileSymElement*>(elem)->setValue(aValue);
260 break;
261
262 default:
263 ;
264 }
265
266 return elem;
267}
268
269}
#define assert(condition)
const Byte BYTE_BITWIDTH
Definition BaseType.hh:136
unsigned char Byte
Definition BaseType.hh:116
void setReadPosition(unsigned int position)
HalfWord readHalfWord()
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
static void registerSectionReader(const SectionReader *sReader)
virtual void addElement(SectionElement *element)
Definition Section.cc:133
@ ST_SYMTAB
Symbol table.
Definition Section.hh:72
bool noBits() const
SymbolType
Type of symbol element.
@ STT_PROCEDURE
Symbol gives indicates procedure start position in section.
@ STT_FILE
Name of symbol gives the name of source file associated with this object file.
@ STT_CODE
Associated with executable code.
@ STT_NOTYPE
Type is not defined.
@ STT_SECTION
Associated with section.
@ STT_DATA
Associated with data object.
virtual SymbolType type() const =0
Returns type of symbol.
void setAbsolute(bool anAbsoluteness)
void setName(Chunk *aName)
void setBinding(SymbolBinding aBinding)
bool absolute() const
SymbolBinding binding() const
void setSection(Section *aSect)
SymbolBinding
Binding types of symbol.
@ STB_LOCAL
Not visible outside the object file that contains it's definition.
virtual void readData(BinaryStream &stream, Section *section) const
static const Header & header()
SymbolElement * createSymbol(SymbolElement::SymbolType symType, Word aValue, Word aSize, SectionId sectToBelong) const
virtual Section::SectionType type() const
virtual void readData(BinaryStream &stream, Section *section) const
static TPEFSymbolSectionReader proto_
Prototype instance of TPEFSymbolSectionReader to be registered to SectionReader.
static const Byte SYMBOL_TYPE_MASK
Mask for getting type of symbol from st_info field of symbol element.
@ STO_ABS
Section is absolute, not relocating.
HalfWord SectionId
Type for storing binary file section ids.
SymbolElement::SymbolType SymbolType
Word SectionIndex
Type for storing section indexes.
Word SectionOffset
Type for storing offsets relative to a given base offset value.
SymbolElement::SymbolBinding SymbolBinding