OpenASIP 2.2
Loading...
Searching...
No Matches
TPEFRelocSectionWriter.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 TPEFRelocSectionWriter.cc
26 *
27 * Definitions of TPEFRelocSectionWriter class.
28 *
29 * @author Jussi Nykänen 2003 (nykanen-no.spam-cs.tut.fi)
30 * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
31 *
32 * @note rating: yellow
33 */
34
35#include <list>
36
38#include "SafePointer.hh"
39#include "ReferenceKey.hh"
41#include "SectionElement.hh"
43#include "RelocElement.hh"
44#include "SectionIdReplacer.hh"
46#include "FileOffsetReplacer.hh"
47#include "RelocSection.hh"
49#include "ImmediateElement.hh"
50#include "TPEFHeaders.hh"
51#include "BinaryStream.hh"
52
53namespace TPEF {
54
55using std::list;
56using ReferenceManager::SafePointer;
57using ReferenceManager::SectionKey;
58using ReferenceManager::SectionOffsetKey;
59using ReferenceManager::FileOffsetKey;
60
61const TPEFRelocSectionWriter TPEFRelocSectionWriter::instance_;
63
64/**
65 * Construction.
66 *
67 * Registers itself to SectionWriter.
68 */
72
73/**
74 * Destructor.
75 */
78
79/**
80 * Returns the type of the section this writer can write.
81 *
82 * @return The type of section this writer can write.
83 */
88
89/**
90 * Writes relocation section in to stream.
91 *
92 * @param stream The stream where to write.
93 * @param section The section to be written.
94 */
95void
97 BinaryStream& stream,
98 const Section* section) const {
99
100 FileOffset startOffset = stream.writePosition();
101
102 // file offset to data of section
104 FileOffsetKey(startOffset), section->element(0));
105
106 const RelocSection *rSection =
107 dynamic_cast<const RelocSection*>(section);
108 assert(rSection != NULL);
109
110
111 for (Word i = 0; i < section->elementCount(); i++) {
112 RelocElement* rElem =
113 dynamic_cast<RelocElement*>(section->element(i));
114 assert(rElem != NULL);
115
116 // r_offset
117 if (rElem->location() != NULL) {
118 SectionOffsetReplacer sReplacer(rElem->location());
119 sReplacer.resolve();
120 } else {
121 bool locationOfRelocationMustBeSet = false;
122 assert(locationOfRelocationMustBeSet);
123 }
124
125 // r_symbol
126 if (rElem->symbol() != NULL) {
127 SectionIndexReplacer indexReplacer(rElem->symbol(), sizeof(Word));
128 indexReplacer.resolve();
129 } else {
130 bool symbolFieldOfRelocationMustBeSet = false;
131 assert(symbolFieldOfRelocationMustBeSet);
132 }
133
134 // r_type
135 Byte rType = 0;
136
137 // chunked bit on if necessary.
138 if (rElem->chunked()) {
139 rType = rType | TPEFHeaders::STF_CHUNK;
140 }
141
142 // and relocation type
143 rType = rType | static_cast<Byte>(rElem->type());
144
145 stream.writeByte(rType);
146
147 // r_asp
148 SectionIndexReplacer indexReplacer(rElem->aSpace(), sizeof(Byte));
149 indexReplacer.resolve();
150
151 // r_size bit width of the value of element referred by location
152 stream.writeByte(rElem->size());
153
154 // r_bitpos for chunked relocations
155 stream.writeByte(rElem->bitOffset());
156 }
157
159 stream.writePosition() - startOffset);
160}
161
162/**
163 * Returns size of element.
164 *
165 * @param section Section that is written.
166 * @return Size of element of this section.
167 */
168Word
170 return elementSize_;
171}
172
173/**
174 * Writes Info field of section header.
175 *
176 * @param stream The stream where to write.
177 * @param sect Section to write.
178 */
179void
181 const Section* sect) const {
182
183 const Section* refSection =
184 dynamic_cast<const RelocSection*>(sect)->referencedSection();
185
186 SectionIdReplacer idReplacer(refSection);
187 idReplacer.resolve();
188
189 stream.writeHalfWord(0);
190}
191
192}
#define assert(condition)
unsigned char Byte
Definition BaseType.hh:116
void writeByte(Byte byte)
void writeHalfWord(HalfWord halfword)
unsigned int writePosition()
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
ASpaceElement * aSpace() const
RelocType type() const
SymbolElement * symbol() const
Byte bitOffset() const
bool chunked() const
SectionElement * location() const
Byte size() const
static void setSize(const SafePointable *obj, Word size)
static void registerSectionWriter(const SectionWriter *sWriter)
@ ST_RELOC
Relocation section.
Definition Section.hh:74
SectionElement * element(Word index) const
Word elementCount() const
virtual void writeInfo(BinaryStream &stream, const Section *sect) const
virtual void actualWriteData(BinaryStream &stream, const Section *section) const
static const Word elementSize_
The fixed size of reloc element.
static const TPEFRelocSectionWriter instance_
An unique instance of class.
virtual Section::SectionType type() const
virtual Word elementSize(const Section *section) const
@ STF_CHUNK
Relocation applied to chunk(1) or complete address(0).
Word FileOffset
Type for storing absolute file offsets.