OpenASIP  2.0
TPEFTools.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 TPEFTools.cc
26  *
27  * Implementation of TPEFTools class.
28  *
29  * @author Mikael Lepistö 2005 (tmlepist-no.spam-cs.tut.fi)
30  * @note rating: yellow
31  */
32 
33 #include "TPEFTools.hh"
34 
35 #include "MapTools.hh"
36 #include "Binary.hh"
37 #include "ASpaceSection.hh"
38 #include "StringSection.hh"
39 #include "ASpaceElement.hh"
40 #include "ResourceElement.hh"
41 #include "ResourceSection.hh"
42 #include "RelocElement.hh"
43 #include "CodeSection.hh"
44 #include "InstructionElement.hh"
45 #include "Application.hh"
46 
47 namespace TPEF {
48 
49 /**
50  * Constructor.
51  *
52  * @param aTpef TPEF that is accessed with toolkit.
53  */
54 TPEFTools::TPEFTools(const Binary &aTpef) : tpef_(aTpef) {
55 }
56 
57 /**
58  * Clears cache of object.
59  */
60 void
62  relocationCache_.clear();
63 }
64 
65 /**
66  * Return section of the requested element.
67  *
68  * @param element Element whose section is needed.
69  * @return Section of the requested element.
70  */
71 Section&
73  return sectionOfElement(tpef_, element);
74 }
75 
76 /**
77  * Finds name string of a resource.
78  *
79  * @param type Type of the resource.
80  * @param resourceId Id of the resource.
81  * @return Name of the requested resource.
82  */
83 std::string
85  HalfWord resourceId) const {
86  return resourceName(tpef_, type, resourceId);
87 }
88 
89 /**
90  * Finds name string of an address space element.
91  *
92  * @param aSpace Element whose name is needed.
93  * @return Name of the requested address space.
94  */
95 std::string
97  return addressSpaceName(tpef_, aSpace);
98 }
99 
100 /**
101  * Checks if the requested immediate or chunk is relocated.
102  *
103  * @param element Element whose name is needed.
104  * @return True if the requested element has corresponding relocation element.
105  */
106 bool
109  return MapTools::containsKey(relocationCache_, &element);
110 }
111 
112 /**
113  * Retrieves the relocation element requested immediate or chunk is relocated.
114  *
115  * @param element Element relocations are checked.
116  * @return Relocation element for the element given in parameter.
117  */
118 const RelocElement&
119 TPEFTools::relocation(const SectionElement& element) const {
121  return *MapTools::valueForKey<const RelocElement*>(
122  relocationCache_, &element);
123 }
124 
125 /**
126  * Return section of the requested element.
127  *
128  * NOTE: method does not work for LineNumElements
129  * (line number section element desing is wrong).
130  *
131  * @param bin TPEF which is checked.
132  * @param element Element whose section is needed.
133  * @return Section of the requested element.
134  */
135 Section&
137  const Binary &bin,
138  const SectionElement &element) {
139 
140  // check if chunkable element
141  const Chunk* chunk = dynamic_cast<const Chunk*>(&element);
142 
143  if (chunk != NULL) {
144  for (Word i = 0; i < bin.sectionCount(); i++) {
145  RawSection* currSect =
146  dynamic_cast<RawSection*>(bin.section(i));
147 
148  if (currSect == NULL) {
149  continue;
150  }
151 
152  if (chunk->offset() < currSect->length() &&
153  currSect->chunk(chunk->offset()) == chunk) {
154  return *currSect;
155  }
156  }
157 
158  } else {
159  for (Word i = 0; i < bin.sectionCount(); i++) {
160  Section* currSect = bin.section(i);
161 
162  if (currSect->isChunkable()) {
163  continue;
164  }
165 
166  // optimization for code section
167  if (currSect->isCodeSection()) {
168  CodeSection* codeSect =
169  dynamic_cast<CodeSection*>(currSect);
170 
171  const InstructionElement* instr =
172  dynamic_cast<const InstructionElement*>(&element);
173 
174  if (instr == NULL) {
175  continue;
176  }
177 
178  if (codeSect->isInSection(*instr)) {
179  return *currSect;
180  } else {
181  continue;
182  }
183  }
184 
185  // other sections a bit slower
186  for (Word j = 0; j < currSect->elementCount(); j++) {
187  if (&element == currSect->element(j)) {
188  return *currSect;
189  }
190  }
191  }
192  }
193 
194  throw NotAvailable(
195  __FILE__, __LINE__, __func__,
196  "Requested element is not found from any section of binary.");
197 }
198 
199 /**
200  * Finds name string of a resource.
201  *
202  * @param bin TPEF which is checked.
203  * @param type Type of the resource.
204  * @param resourceId Id of the resource.
205  * @return Name of the requested resource.
206  */
207 std::string
209  const Binary &bin, ResourceElement::ResourceType type,
210  HalfWord resourceId) {
211 
212  for (Word i = 0; i < bin.sectionCount(Section::ST_MR); i++) {
213  ResourceSection* resources =
214  dynamic_cast<ResourceSection*>(bin.section(Section::ST_MR, i));
215 
216  assert(resources != NULL);
217 
218  if (resources->hasResource(type, resourceId)) {
219 
220  ResourceElement& resource =
221  dynamic_cast<ResourceElement&>(
222  resources->findResource(type, resourceId));
223 
224  StringSection *strings =
225  dynamic_cast<StringSection*>(resources->link());
226 
227  assert(strings != NULL);
228 
229  return strings->chunk2String(resource.name());
230  }
231  }
232 
233  throw NotAvailable(
234  __FILE__, __LINE__, __func__,
235  "Resource wasn't found id:" + Conversion::toString(resourceId));
236 }
237 
238 /**
239  * Finds name string of an address space element.
240  *
241  * @param bin TPEF which is checked.
242  * @param aSpace Element whose name is needed.
243  * @return Name of the requested address space.
244  */
245 std::string
247 
249 
250  Section *aSpaceSection = bin.section(Section::ST_ADDRSP, 0);
251 
252  StringSection *strings = dynamic_cast<StringSection*>(
253  aSpaceSection->link());
254 
255  assert(strings != NULL);
256 
257  return strings->chunk2String(aSpace.name());
258 }
259 
260 /**
261  * Checks if the requested immediate or chunk is relocated.
262  *
263  * @param bin TPEF which is checked.
264  * @param element Element whose name is needed.
265  * @return True if the requested element has corresponding relocation element.
266  */
267 bool
268 TPEFTools::hasRelocation(const Binary& bin, const SectionElement& element) {
269 
270  for (Word i = 0; i < bin.sectionCount(Section::ST_RELOC); i++) {
271  Section *relocSect = bin.section(Section::ST_RELOC, i);
272 
273  for (Word j = 0; j < relocSect->elementCount(); j++) {
274  RelocElement *reloc =
275  dynamic_cast<RelocElement*>(relocSect->element(j));
276 
277  assert(reloc != NULL);
278 
279  if (reloc->location() == &element) {
280  return true;
281  }
282  }
283  }
284 
285  return false;
286 }
287 
288 /**
289  * Retrieves the relocation element requested immediate or chunk is relocated.
290  *
291  * @param bin TPEF which is checked.
292  * @param element Element relocations are checked.
293  * @return Relocation element for the element given in parameter.
294  */
295 const RelocElement&
296 TPEFTools::relocation(const Binary& bin, const SectionElement& element) {
297 
298  for (Word i = 0; i < bin.sectionCount(Section::ST_RELOC); i++) {
299  Section *relocSect = bin.section(Section::ST_RELOC, i);
300 
301  for (Word j = 0; j < relocSect->elementCount(); j++) {
302  RelocElement *reloc =
303  dynamic_cast<RelocElement*>(relocSect->element(j));
304 
305  assert(reloc != NULL);
306 
307  if (reloc->location() == &element) {
308  return *reloc;
309  }
310  }
311  }
312 
313  throw NotAvailable(
314  __FILE__, __LINE__, __func__,
315  "There is no relocation for requested element.");
316 }
317 
318 
319 /**
320  * Inits cache for hasRelocation and relocation methods.
321  */
322 void
324 
325  if (relocationCache_.empty()) {
326  for (Word i = 0; i < tpef_.sectionCount(Section::ST_RELOC); i++) {
327  Section *relocSect = tpef_.section(Section::ST_RELOC, i);
328 
329  for (Word j = 0; j < relocSect->elementCount(); j++) {
330  RelocElement *reloc =
331  dynamic_cast<RelocElement*>(relocSect->element(j));
332 
333  assert(reloc != NULL);
334 
335  relocationCache_[reloc->location()] = reloc;
336  }
337  }
338  }
339 }
340 
341 }
342 
ASpaceElement.hh
TPEF::Section::isChunkable
virtual bool isChunkable() const
Definition: Section.cc:157
TPEF::ResourceSection::hasResource
bool hasResource(ResourceElement::ResourceType aType, HalfWord anId) const
Definition: ResourceSection.cc:120
TPEF::ResourceSection
Definition: ResourceSection.hh:47
TPEF::TPEFTools::addressSpaceName
std::string addressSpaceName(const ASpaceElement &aSpace) const
Definition: TPEFTools.cc:96
TPEF::InstructionElement
Definition: InstructionElement.hh:77
TPEF::Binary
Definition: Binary.hh:49
MapTools.hh
TPEF::ResourceElement
Definition: ResourceElement.hh:47
TPEF::TPEFTools::hasRelocation
bool hasRelocation(const SectionElement &element) const
Definition: TPEFTools.cc:107
TPEF::Binary::section
Section * section(Word index) const
TPEF::StringSection::chunk2String
std::string chunk2String(const Chunk *chunk) const
Definition: StringSection.cc:72
InstructionElement.hh
TPEF::Binary::sectionCount
Word sectionCount() const
TPEF::ResourceSection::findResource
ResourceElement & findResource(ResourceElement::ResourceType aType, HalfWord anId) const
Definition: ResourceSection.cc:91
StringSection.hh
ResourceSection.hh
Conversion::toString
static std::string toString(const T &source)
NotAvailable
Definition: Exception.hh:728
TPEF::Section
Definition: Section.hh:64
TPEF::StringSection
Definition: StringSection.hh:48
TPEF::ResourceElement::name
Chunk * name() const
TPEF::Section::link
Section * link() const
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::Section::element
SectionElement * element(Word index) const
TPEF::ASpaceElement
Definition: ASpaceElement.hh:48
ResourceElement.hh
TPEF::TPEFTools::initRelocationCache
void initRelocationCache() const
Definition: TPEFTools.cc:323
Application.hh
TPEF::SectionElement
Definition: SectionElement.hh:44
__func__
#define __func__
Definition: Application.hh:67
RelocElement.hh
TPEF::ASpaceElement::name
Chunk * name() const
TPEF::ResourceElement::ResourceType
ResourceType
Resource types.
Definition: ResourceElement.hh:51
TPEF::TPEFTools::resourceName
std::string resourceName(ResourceElement::ResourceType type, HalfWord resourceId) const
Definition: TPEFTools.cc:84
TPEF::CodeSection
Definition: CodeSection.hh:44
TPEF::CodeSection::isInSection
bool isInSection(const InstructionElement &elem) const
Definition: CodeSection.cc:214
ASpaceSection.hh
TPEF::TPEFTools::relocationCache_
std::map< const SectionElement *, const RelocElement * > relocationCache_
Cache of Chunks and immediates, which are relocated.
Definition: TPEFTools.hh:110
TPEF::RelocElement
Definition: RelocElement.hh:51
MapTools::containsKey
static bool containsKey(const MapType &aMap, const KeyType &aKey)
TPEF::Chunk::offset
SectionOffset offset() const
TPEF::TPEFTools::tpef_
const Binary & tpef_
TPEF whose resources are accessed.
Definition: TPEFTools.hh:106
TPEF::RawSection
Definition: Section.hh:196
TPEFTools.hh
TPEF::Section::ST_RELOC
@ ST_RELOC
Relocation section.
Definition: Section.hh:74
TPEF::TPEFTools::TPEFTools
TPEFTools(const Binary &aTpef)
Definition: TPEFTools.cc:54
TPEF::RawSection::length
virtual Word length() const
Definition: Section.cc:275
TPEF::TPEFTools::sectionOfElement
Section & sectionOfElement(const SectionElement &element)
Definition: TPEFTools.cc:72
TPEF::RelocElement::location
SectionElement * location() const
TPEF::Section::isCodeSection
virtual bool isCodeSection() const
Definition: Section.hh:143
TPEF::Section::ST_MR
@ ST_MR
Machine resources section.
Definition: Section.hh:78
TPEF::RawSection::chunk
virtual Chunk * chunk(SectionOffset offset) const
Definition: Section.cc:212
TPEF::Chunk
Definition: Chunk.hh:45
CodeSection.hh
TPEF::Section::ST_ADDRSP
@ ST_ADDRSP
Address space section.
Definition: Section.hh:77
TPEF::Section::elementCount
Word elementCount() const
TPEF
Definition: Assembler.hh:43
Binary.hh
TPEF::TPEFTools::relocation
const RelocElement & relocation(const SectionElement &element) const
Definition: TPEFTools.cc:119
TPEF::TPEFTools::clearCache
void clearCache() const
Definition: TPEFTools.cc:61