OpenASIP  2.0
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"
40 #include "SectionSizeReplacer.hh"
41 #include "SectionElement.hh"
42 #include "SectionOffsetReplacer.hh"
43 #include "RelocElement.hh"
44 #include "SectionIdReplacer.hh"
45 #include "SectionIndexReplacer.hh"
46 #include "FileOffsetReplacer.hh"
47 #include "RelocSection.hh"
48 #include "TPEFCodeSectionWriter.hh"
49 #include "ImmediateElement.hh"
50 #include "TPEFHeaders.hh"
51 #include "BinaryStream.hh"
52 
53 namespace TPEF {
54 
55 using std::list;
56 using ReferenceManager::SafePointer;
57 using ReferenceManager::SectionKey;
58 using ReferenceManager::SectionOffsetKey;
59 using ReferenceManager::FileOffsetKey;
60 
61 const TPEFRelocSectionWriter TPEFRelocSectionWriter::instance_;
63 
64 /**
65  * Construction.
66  *
67  * Registers itself to SectionWriter.
68  */
71 }
72 
73 /**
74  * Destructor.
75  */
77 }
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  */
86  return Section::ST_RELOC;
87 }
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  */
95 void
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  */
168 Word
169 TPEFRelocSectionWriter::elementSize(const Section* /*section*/) const {
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  */
179 void
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 }
TPEF::BinaryStream::writeHalfWord
void writeHalfWord(HalfWord halfword)
Definition: BinaryStream.cc:336
SectionSizeReplacer.hh
SectionOffsetReplacer.hh
SectionIdReplacer.hh
TPEF::RelocElement::bitOffset
Byte bitOffset() const
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::RelocElement::chunked
bool chunked() const
TPEF::BinaryStream::writePosition
unsigned int writePosition()
Definition: BinaryStream.cc:592
SafePointer.hh
TPEF::TPEFRelocSectionWriter::TPEFRelocSectionWriter
TPEFRelocSectionWriter()
Definition: TPEFRelocSectionWriter.cc:69
TPEFHeaders.hh
TPEF::TPEFRelocSectionWriter::type
virtual Section::SectionType type() const
Definition: TPEFRelocSectionWriter.cc:85
TPEF::TPEFRelocSectionWriter::elementSize_
static const Word elementSize_
The fixed size of reloc element.
Definition: TPEFRelocSectionWriter.hh:71
Byte
unsigned char Byte
Definition: BaseType.hh:116
TPEF::RelocElement::size
Byte size() const
TPEF::RelocElement::symbol
SymbolElement * symbol() const
TPEF::ReferenceManager::SafePointer::addObjectReference
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
Definition: SafePointer.cc:306
ImmediateElement.hh
TPEF::RelocSection
Definition: RelocSection.hh:47
TPEF::ValueReplacer::resolve
void resolve()
Definition: ValueReplacer.cc:90
TPEF::TPEFRelocSectionWriter::actualWriteData
virtual void actualWriteData(BinaryStream &stream, const Section *section) const
Definition: TPEFRelocSectionWriter.cc:96
TPEF::Section
Definition: Section.hh:64
TPEF::TPEFRelocSectionWriter::~TPEFRelocSectionWriter
virtual ~TPEFRelocSectionWriter()
Definition: TPEFRelocSectionWriter.cc:76
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::Section::element
SectionElement * element(Word index) const
TPEF::SectionOffsetReplacer
Definition: SectionOffsetReplacer.hh:47
TPEFRelocSectionWriter.hh
RelocSection.hh
TPEF::SectionWriter::registerSectionWriter
static void registerSectionWriter(const SectionWriter *sWriter)
Definition: SectionWriter.cc:148
TPEF::RelocElement::aSpace
ASpaceElement * aSpace() const
TPEFCodeSectionWriter.hh
TPEF::FileOffset
Word FileOffset
Type for storing absolute file offsets.
Definition: TPEFBaseType.hh:52
TPEF::TPEFRelocSectionWriter::elementSize
virtual Word elementSize(const Section *section) const
Definition: TPEFRelocSectionWriter.cc:169
TPEF::RelocElement::type
RelocType type() const
RelocElement.hh
FileOffsetReplacer.hh
TPEF::BinaryStream::writeByte
void writeByte(Byte byte)
Definition: BinaryStream.cc:310
TPEF::TPEFHeaders::STF_CHUNK
@ STF_CHUNK
Relocation applied to chunk(1) or complete address(0).
Definition: TPEFHeaders.hh:105
TPEF::RelocElement
Definition: RelocElement.hh:51
TPEF::SectionIdReplacer
Definition: SectionIdReplacer.hh:47
TPEF::ReferenceManager::FileOffsetKey
Definition: ReferenceKey.hh:121
SectionElement.hh
TPEF::SectionSizeReplacer::setSize
static void setSize(const SafePointable *obj, Word size)
Definition: SectionSizeReplacer.cc:100
SectionIndexReplacer.hh
TPEF::Section::ST_RELOC
@ ST_RELOC
Relocation section.
Definition: Section.hh:74
TPEF::TPEFRelocSectionWriter::writeInfo
virtual void writeInfo(BinaryStream &stream, const Section *sect) const
Definition: TPEFRelocSectionWriter.cc:180
BinaryStream.hh
TPEF::TPEFRelocSectionWriter::instance_
static const TPEFRelocSectionWriter instance_
An unique instance of class.
Definition: TPEFRelocSectionWriter.hh:69
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::SectionIndexReplacer
Definition: SectionIndexReplacer.hh:48
TPEF::RelocElement::location
SectionElement * location() const
ReferenceKey.hh
TPEF::TPEFSectionWriter
Definition: TPEFSectionWriter.hh:49
TPEF::Section::elementCount
Word elementCount() const
TPEF
Definition: Assembler.hh:43