OpenASIP  2.0
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 
51 namespace TPEF {
52 
53 using ReferenceManager::SafePointer;
54 using ReferenceManager::SectionIndexKey;
55 using ReferenceManager::SectionOffsetKey;
56 using ReferenceManager::SectionKey;
57 
60 
62 
64 
65 /**
66  * Constructor.
67  *
68  * Registers itself to SectionReader.
69  */
72 }
73 
74 /**
75  * Destructor.
76  */
78 }
79 
80 /**
81  * Returns the type of section it is meant to read.
82  *
83  * @return The type of section it can read.
84  */
87  return Section::ST_SYMTAB;
88 }
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  */
102 void
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) {
181  NoTypeSymElement* elem = new NoTypeSymElement();
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 }
TPEF::SectionId
HalfWord SectionId
Type for storing binary file section ids.
Definition: TPEFBaseType.hh:43
TPEF::TPEFSymbolSectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const
Definition: TPEFSymbolSectionReader.cc:103
TPEF::SectionSymElement
Definition: SectionSymElement.hh:44
TPEF::SymbolElement::STT_PROCEDURE
@ STT_PROCEDURE
Symbol gives indicates procedure start position in section.
Definition: SymbolElement.hh:73
TPEF::Section::ST_SYMTAB
@ ST_SYMTAB
Symbol table.
Definition: Section.hh:72
TPEF::TPEFSectionReader::Header::bodyOffset
Word bodyOffset
Definition: TPEFSectionReader.hh:69
CodeSymElement.hh
TPEF::BinaryStream::setReadPosition
void setReadPosition(unsigned int position)
Definition: BinaryStream.cc:629
Exception.hh
TPEFSymbolSectionReader.hh
TPEF::TPEFSymbolSectionReader::createSymbol
SymbolElement * createSymbol(SymbolElement::SymbolType symType, Word aValue, Word aSize, SectionId sectToBelong) const
Definition: TPEFSymbolSectionReader.cc:203
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::SymbolElement::SymbolType
SymbolType
Type of symbol element.
Definition: SymbolElement.hh:66
DataSymElement.hh
SafePointer.hh
TPEF::FileSymElement
Definition: FileSymElement.hh:44
TPEFHeaders.hh
TPEF::SymbolElement::STT_FILE
@ STT_FILE
Name of symbol gives the name of source file associated with this object file.
Definition: SymbolElement.hh:71
TPEF::TPEFSymbolSectionReader::SYMBOL_TYPE_MASK
static const Byte SYMBOL_TYPE_MASK
Mask for getting type of symbol from st_info field of symbol element.
Definition: TPEFSymbolSectionReader.hh:69
FileSymElement.hh
Byte
unsigned char Byte
Definition: BaseType.hh:116
TPEF::SymbolElement::binding
SymbolBinding binding() const
TPEF::SymbolElement::STT_SECTION
@ STT_SECTION
Associated with section.
Definition: SymbolElement.hh:70
TPEF::ReferenceManager::SectionIndexKey
Definition: ReferenceKey.hh:65
TPEF::SymbolElement::setBinding
void setBinding(SymbolBinding aBinding)
TPEF::ReferenceManager::SafePointer::addObjectReference
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
Definition: SafePointer.cc:306
TPEF::Section
Definition: Section.hh:64
TPEF::TPEFSectionReader::Header::elementSize
Word elementSize
Definition: TPEFSectionReader.hh:68
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::Section::addElement
virtual void addElement(SectionElement *element)
Definition: Section.cc:133
SectionSymElement.hh
TPEF::SymbolElement::type
virtual SymbolType type() const =0
Returns type of symbol.
SymbolSection.hh
TPEF::SymbolSection
Definition: SymbolSection.hh:44
TPEF::SymbolBinding
SymbolElement::SymbolBinding SymbolBinding
Definition: TPEFSymbolSectionReader.cc:59
TPEF::TPEFSymbolSectionReader::type
virtual Section::SectionType type() const
Definition: TPEFSymbolSectionReader.cc:86
SectionReader.hh
TPEF::SymbolElement::setSection
void setSection(Section *aSect)
TPEF::TPEFSymbolSectionReader::proto_
static TPEFSymbolSectionReader proto_
Prototype instance of TPEFSymbolSectionReader to be registered to SectionReader.
Definition: TPEFSymbolSectionReader.hh:72
TPEF::SectionReader::registerSectionReader
static void registerSectionReader(const SectionReader *sReader)
Definition: SectionReader.cc:145
TPEF::SymbolElement
Definition: SymbolElement.hh:52
TPEF::SymbolElement::absolute
bool absolute() const
TPEF::BinaryStream::readHalfWord
HalfWord readHalfWord()
Definition: BinaryStream.cc:150
TPEF::SymbolElement::setName
void setName(Chunk *aName)
TPEF::TPEFSectionReader
Definition: TPEFSectionReader.hh:48
TPEF::DataSymElement
Definition: DataSymElement.hh:41
TPEF::BinaryStream::readByte
Byte readByte()
Definition: BinaryStream.cc:120
TPEF::SymbolElement::STT_CODE
@ STT_CODE
Associated with executable code.
Definition: SymbolElement.hh:69
TPEF::Section::noBits
bool noBits() const
TPEF::TPEFSymbolSectionReader
Definition: TPEFSymbolSectionReader.hh:47
TPEF::SectionOffset
Word SectionOffset
Type for storing offsets relative to a given base offset value.
Definition: TPEFBaseType.hh:49
TPEF::ReferenceManager::SectionKey
Definition: ReferenceKey.hh:145
TPEF::NoTypeSymElement
Definition: NoTypeSymElement.hh:44
TPEF::ProcedSymElement
Definition: ProcedSymElement.hh:45
TPEF::TPEFSectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const
Definition: TPEFSectionReader.cc:86
TPEF::SymbolElement::SymbolBinding
SymbolBinding
Binding types of symbol.
Definition: SymbolElement.hh:55
BYTE_BITWIDTH
const Byte BYTE_BITWIDTH
Definition: BaseType.hh:136
SymbolElement.hh
TPEF::SymbolElement::setAbsolute
void setAbsolute(bool anAbsoluteness)
TPEF::SymbolElement::STT_DATA
@ STT_DATA
Associated with data object.
Definition: SymbolElement.hh:68
TPEF::ReferenceManager::SectionOffsetKey
Definition: ReferenceKey.hh:93
BinaryStream.hh
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::CodeSymElement
Definition: CodeSymElement.hh:46
ProcedSymElement.hh
TPEF::SymbolElement::STT_NOTYPE
@ STT_NOTYPE
Type is not defined.
Definition: SymbolElement.hh:67
TPEFBaseType.hh
TPEF::SectionIndex
Word SectionIndex
Type for storing section indexes.
Definition: TPEFBaseType.hh:46
TPEF::SymbolType
SymbolElement::SymbolType SymbolType
Definition: TPEFSymbolSectionReader.cc:58
ReferenceKey.hh
TPEF::TPEFSymbolSectionReader::~TPEFSymbolSectionReader
virtual ~TPEFSymbolSectionReader()
Definition: TPEFSymbolSectionReader.cc:77
TPEF::BinaryStream::readWord
Word readWord()
Definition: BinaryStream.cc:187
NoTypeSymElement.hh
TPEF::TPEFSymbolSectionReader::TPEFSymbolSectionReader
TPEFSymbolSectionReader()
Definition: TPEFSymbolSectionReader.cc:70
TPEF::SymbolElement::STB_LOCAL
@ STB_LOCAL
Not visible outside the object file that contains it's definition.
Definition: SymbolElement.hh:56
TPEF::TPEFSectionReader::header
static const Header & header()
Definition: TPEFSectionReader.cc:174
TPEF::TPEFHeaders::STO_ABS
@ STO_ABS
Section is absolute, not relocating.
Definition: TPEFHeaders.hh:97
TPEF
Definition: Assembler.hh:43