OpenASIP  2.0
TPEFSymbolSectionWriter.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 TPEFSymbolSectionWriter.cc
26  *
27  * Definition of TPEFSymbolSectionWriter class.
28  *
29  * @author Mikael Lepistö 2004 (tmlepist-no.spam-cs.tut.fi)
30  *
31  * @note rating: yellow
32  */
33 
34 #include <list>
35 
37 #include "SafePointer.hh"
38 #include "ReferenceKey.hh"
39 #include "SectionElement.hh"
40 #include "SectionSizeReplacer.hh"
41 #include "SectionOffsetReplacer.hh"
42 #include "SectionIdReplacer.hh"
43 #include "NoTypeSymElement.hh"
44 #include "CodeSymElement.hh"
45 #include "ProcedSymElement.hh"
46 #include "DataSymElement.hh"
47 #include "FileSymElement.hh"
48 #include "SectionSymElement.hh"
49 #include "BinaryStream.hh"
50 
51 namespace TPEF {
52 
53 using std::list;
54 using ReferenceManager::SafePointer;
55 using ReferenceManager::SectionKey;
56 using ReferenceManager::FileOffsetKey;
57 using ReferenceManager::SectionIndexKey;
58 
59 const TPEFSymbolSectionWriter TPEFSymbolSectionWriter::instance_;
61 
62 /**
63  * Constructor.
64  *
65  * Registers itself to SectionWriter.
66  */
69 }
70 
71 /**
72  * Destructor.
73  */
75 }
76 
77 /**
78  * Returns the section type this writer can write.
79  *
80  * @return The section type writer can write.
81  */
84  return Section::ST_SYMTAB;
85 }
86 
87 /**
88  * Writes the data of the section in to stream.
89  *
90  * @param stream The stream to be written to.
91  * @param sect The section to be written.
92  */
93 void
95  BinaryStream& stream,
96  const Section* sect) const {
97 
98  FileOffset startOffset = stream.writePosition();
99 
100  // file offset to data of section
102  FileOffsetKey(startOffset), sect->element(0));
103 
104  // resolve identification code of currently written section
105  SectionKey sectionKey = SafePointer::sectionKeyFor(sect);
106  SectionId sectionId = sectionKey.sectionId();
107 
108  // first element should match with undefined symbol
109  SymbolElement *undef = dynamic_cast<SymbolElement*>(sect->element(0));
110  assert(undef != NULL);
112  assert(undef->absolute() == true);
114  assert(undef->name()->offset() == 0);
115 
116  // symbols might have to be organized in a way local symbols are
117  // before weak symbols and global syms will come after everything else
118  for (Word i = 0; i < sect->elementCount(); i++) {
119 
120  SymbolElement* elem = dynamic_cast<SymbolElement*>(sect->element(i));
121  assert(elem != NULL);
122 
123  // each element must be registered with its index
125  SectionIndexKey(sectionId, i), elem);
126 
127  // write name
128  assert(elem->name() != NULL);
129  SectionOffsetReplacer sOffReplacer(elem->name());
130  sOffReplacer.resolve();
131 
132  writeValueAndSize(stream, elem);
133 
134  // write info
135  stream.writeByte(
136  (elem->binding() << (BYTE_BITWIDTH / 2)) |
137  static_cast<Byte>(elem->type()));
138 
139  // other field, right now only STO_ABS is used
140  Byte otherField = 0;
141  if (elem->absolute()) {
142  otherField = otherField | TPEFHeaders::STO_ABS;
143  }
144  stream.writeByte(otherField);
145 
146  // section to which the symbol belongs
147  // NOTE: Maybe nullSection should be used for these,
148  // so NULL would be error.
149 
150  if (elem->section() != NULL) {
151  SectionIdReplacer sectIdReplacer(elem->section());
152  sectIdReplacer.resolve();
153  } else {
154  stream.writeHalfWord(0);
155  }
156  }
157  SectionSizeReplacer::setSize(sect, stream.writePosition() - startOffset);
158 }
159 
160 /**
161  * Writes value and size fields of symbol.
162  *
163  * @param stream Stream where to write.
164  * @param elem Element to write.
165  */
166 void
168  BinaryStream &stream,
169  const SymbolElement *elem) const {
170 
171  switch (elem->type()) {
173  // write value
174  // write size
175  stream.writeWord(0);
176  stream.writeWord(0);
177  break;
178  }
179 
182  const CodeSymElement* codeSym =
183  dynamic_cast<const CodeSymElement*>(elem);
184  assert(codeSym != NULL);
185 
186  // write value
187  if (codeSym->reference() != NULL) {
188  SectionOffsetReplacer replacer(codeSym->reference());
189  replacer.resolve();
190  } else {
191  stream.writeWord(0);
192  }
193 
194  // write size
195  stream.writeWord(codeSym->size());
196  break;
197  }
198 
200  const DataSymElement* dataSym =
201  dynamic_cast<const DataSymElement*>(elem);
202  assert(dataSym != NULL);
203 
204  // write value
205  if (dataSym->reference() != NULL) {
206  SectionOffsetReplacer replacer(dataSym->reference());
207  replacer.resolve();
208  } else {
209  stream.writeWord(0);
210  }
211 
212  // write size
213  stream.writeWord(dataSym->size());
214  break;
215  }
216 
218  const SectionSymElement* sectSym =
219  dynamic_cast<const SectionSymElement*>(elem);
220  assert(sectSym != NULL);
221 
222  // write value
223  stream.writeWord(sectSym->value());
224  // write size
225  stream.writeWord(sectSym->size());
226  break;
227  }
228 
230  const FileSymElement* fileSym =
231  dynamic_cast<const FileSymElement*>(elem);
232  assert(fileSym != NULL);
233 
234  // write value
235  stream.writeWord(fileSym->value());
236  // write size
237  stream.writeWord(0);
238  break;
239  }
240 
241  default: {
242  bool unknownSymbolType = false;
243  assert(unknownSymbolType);
244  }
245  }
246 }
247 
248 /**
249  * Returns the fixed size length of section elements.
250  *
251  * @return The fixed size length of section elements.
252  */
253 Word
255  return elementSize_;
256 }
257 
258 /**
259  * Creates needed keys for section if noBits flag is set.
260  *
261  * @param sect Section which for keys will be created.
262  */
263 void
265  SectionSizeReplacer::setSize(sect, 0x0);
266 }
267 
268 }
TPEF::SectionId
HalfWord SectionId
Type for storing binary file section ids.
Definition: TPEFBaseType.hh:43
TPEF::BinaryStream::writeHalfWord
void writeHalfWord(HalfWord halfword)
Definition: BinaryStream.cc:336
SectionSizeReplacer.hh
TPEF::TPEFSymbolSectionWriter::actualWriteData
virtual void actualWriteData(BinaryStream &stream, const Section *section) const
Definition: TPEFSymbolSectionWriter.cc:94
TPEF::SectionSymElement
Definition: SectionSymElement.hh:44
SectionOffsetReplacer.hh
TPEF::SymbolElement::STT_PROCEDURE
@ STT_PROCEDURE
Symbol gives indicates procedure start position in section.
Definition: SymbolElement.hh:73
SectionIdReplacer.hh
TPEF::Section::ST_SYMTAB
@ ST_SYMTAB
Symbol table.
Definition: Section.hh:72
CodeSymElement.hh
TPEF::TPEFSymbolSectionWriter::createKeys
virtual void createKeys(const Section *sect) const
Definition: TPEFSymbolSectionWriter.cc:264
TPEF::SectionSymElement::size
Word size() const
Definition: SectionSymElement.cc:92
TPEF::BinaryStream::writeWord
void writeWord(Word word)
Definition: BinaryStream.cc:368
TPEF::TPEFSymbolSectionWriter::~TPEFSymbolSectionWriter
virtual ~TPEFSymbolSectionWriter()
Definition: TPEFSymbolSectionWriter.cc:74
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::BinaryStream::writePosition
unsigned int writePosition()
Definition: BinaryStream.cc:592
DataSymElement.hh
SafePointer.hh
TPEF::FileSymElement
Definition: FileSymElement.hh:44
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::ReferenceManager::SectionKey::sectionId
SectionId sectionId() const
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::ReferenceManager::SafePointer::addObjectReference
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
Definition: SafePointer.cc:306
TPEF::ValueReplacer::resolve
void resolve()
Definition: ValueReplacer.cc:90
TPEF::Section
Definition: Section.hh:64
TPEF::SymbolElement::section
Section * section() const
TPEF::SymbolElement::name
Chunk * name() const
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::Section::element
SectionElement * element(Word index) const
TPEF::SectionOffsetReplacer
Definition: SectionOffsetReplacer.hh:47
SectionSymElement.hh
TPEF::SymbolElement::type
virtual SymbolType type() const =0
Returns type of symbol.
TPEF::DataSymElement::reference
Chunk * reference() const
Definition: DataSymElement.cc:71
TPEF::SectionWriter::registerSectionWriter
static void registerSectionWriter(const SectionWriter *sWriter)
Definition: SectionWriter.cc:148
TPEF::ReferenceManager::SafePointer::sectionKeyFor
static SectionKey sectionKeyFor(const SafePointable *obj)
Definition: SafePointer.cc:408
TPEF::TPEFSymbolSectionWriter::elementSize_
static const Word elementSize_
The fixed size address space element.
Definition: TPEFSymbolSectionWriter.hh:75
TPEF::FileOffset
Word FileOffset
Type for storing absolute file offsets.
Definition: TPEFBaseType.hh:52
TPEFSymbolSectionWriter.hh
TPEF::SymbolElement
Definition: SymbolElement.hh:52
TPEF::SymbolElement::absolute
bool absolute() const
TPEF::BinaryStream::writeByte
void writeByte(Byte byte)
Definition: BinaryStream.cc:310
TPEF::DataSymElement
Definition: DataSymElement.hh:41
TPEF::TPEFSymbolSectionWriter::TPEFSymbolSectionWriter
TPEFSymbolSectionWriter()
Definition: TPEFSymbolSectionWriter.cc:67
TPEF::CodeSymElement::reference
InstructionElement * reference() const
Definition: CodeSymElement.cc:71
TPEF::TPEFSymbolSectionWriter::elementSize
virtual Word elementSize(const Section *section) const
Definition: TPEFSymbolSectionWriter.cc:254
TPEF::SymbolElement::STT_CODE
@ STT_CODE
Associated with executable code.
Definition: SymbolElement.hh:69
TPEF::FileSymElement::value
Word value() const
Definition: FileSymElement.cc:70
TPEF::TPEFSymbolSectionWriter::type
virtual Section::SectionType type() const
Definition: TPEFSymbolSectionWriter.cc:83
TPEF::SectionIdReplacer
Definition: SectionIdReplacer.hh:47
TPEF::ReferenceManager::SectionKey
Definition: ReferenceKey.hh:145
TPEF::TPEFSymbolSectionWriter::writeValueAndSize
void writeValueAndSize(BinaryStream &stream, const SymbolElement *elem) const
Definition: TPEFSymbolSectionWriter.cc:167
TPEF::ReferenceManager::FileOffsetKey
Definition: ReferenceKey.hh:121
TPEF::Chunk::offset
SectionOffset offset() const
SectionElement.hh
TPEF::SectionSizeReplacer::setSize
static void setSize(const SafePointable *obj, Word size)
Definition: SectionSizeReplacer.cc:100
BYTE_BITWIDTH
const Byte BYTE_BITWIDTH
Definition: BaseType.hh:136
TPEF::SymbolElement::STT_DATA
@ STT_DATA
Associated with data object.
Definition: SymbolElement.hh:68
BinaryStream.hh
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::CodeSymElement
Definition: CodeSymElement.hh:46
ProcedSymElement.hh
TPEF::CodeSymElement::size
Word size() const
Definition: CodeSymElement.cc:102
TPEF::SymbolElement::STT_NOTYPE
@ STT_NOTYPE
Type is not defined.
Definition: SymbolElement.hh:67
TPEF::DataSymElement::size
Word size() const
Definition: DataSymElement.cc:102
ReferenceKey.hh
TPEF::SectionSymElement::value
Word value() const
Definition: SectionSymElement.cc:70
NoTypeSymElement.hh
TPEF::TPEFSectionWriter
Definition: TPEFSectionWriter.hh:49
TPEF::SymbolElement::STB_LOCAL
@ STB_LOCAL
Not visible outside the object file that contains it's definition.
Definition: SymbolElement.hh:56
TPEF::Section::elementCount
Word elementCount() const
TPEF::TPEFHeaders::STO_ABS
@ STO_ABS
Section is absolute, not relocating.
Definition: TPEFHeaders.hh:97
TPEF
Definition: Assembler.hh:43
TPEF::TPEFSymbolSectionWriter::instance_
static const TPEFSymbolSectionWriter instance_
An unique instance of class.
Definition: TPEFSymbolSectionWriter.hh:73