OpenASIP 2.2
Loading...
Searching...
No Matches
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"
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
51namespace TPEF {
52
53using std::list;
54using ReferenceManager::SafePointer;
55using ReferenceManager::SectionKey;
56using ReferenceManager::FileOffsetKey;
57using ReferenceManager::SectionIndexKey;
58
59const TPEFSymbolSectionWriter TPEFSymbolSectionWriter::instance_;
61
62/**
63 * Constructor.
64 *
65 * Registers itself to SectionWriter.
66 */
70
71/**
72 * Destructor.
73 */
76
77/**
78 * Returns the section type this writer can write.
79 *
80 * @return The section type writer can write.
81 */
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 */
93void
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 */
166void
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 */
253Word
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 */
263void
267
268}
#define assert(condition)
const Byte BYTE_BITWIDTH
Definition BaseType.hh:136
unsigned char Byte
Definition BaseType.hh:116
void writeByte(Byte byte)
void writeHalfWord(HalfWord halfword)
unsigned int writePosition()
void writeWord(Word word)
SectionOffset offset() const
InstructionElement * reference() const
Chunk * reference() const
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
static SectionKey sectionKeyFor(const SafePointable *obj)
static void setSize(const SafePointable *obj, Word size)
static void registerSectionWriter(const SectionWriter *sWriter)
@ ST_SYMTAB
Symbol table.
Definition Section.hh:72
SectionElement * element(Word index) const
Word elementCount() const
@ 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.
bool absolute() const
Section * section() const
SymbolBinding binding() const
Chunk * name() const
@ STB_LOCAL
Not visible outside the object file that contains it's definition.
virtual void createKeys(const Section *sect) const
void writeValueAndSize(BinaryStream &stream, const SymbolElement *elem) const
virtual void actualWriteData(BinaryStream &stream, const Section *section) const
static const TPEFSymbolSectionWriter instance_
An unique instance of class.
static const Word elementSize_
The fixed size address space element.
virtual Section::SectionType type() const
virtual Word elementSize(const Section *section) const
@ STO_ABS
Section is absolute, not relocating.
HalfWord SectionId
Type for storing binary file section ids.
Word FileOffset
Type for storing absolute file offsets.