OpenASIP 2.2
Loading...
Searching...
No Matches
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
47namespace TPEF {
48
49/**
50 * Constructor.
51 *
52 * @param aTpef TPEF that is accessed with toolkit.
53 */
54TPEFTools::TPEFTools(const Binary &aTpef) : tpef_(aTpef) {
55}
56
57/**
58 * Clears cache of object.
59 */
60void
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 */
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 */
83std::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 */
95std::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 */
106bool
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 */
118const RelocElement&
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 */
135Section&
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 */
207std::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 */
245std::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 */
267bool
268TPEFTools::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 */
295const RelocElement&
296TPEFTools::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 */
322void
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
#define __func__
#define assert(condition)
static std::string toString(const T &source)
static KeyType keyForValue(const MapType &aMap, const ValueType &aValue)
static bool containsKey(const MapType &aMap, const KeyType &aKey)
Chunk * name() const
Word sectionCount() const
Section * section(Word index) const
SectionOffset offset() const
bool isInSection(const InstructionElement &elem) const
virtual Chunk * chunk(SectionOffset offset) const
Definition Section.cc:212
virtual Word length() const
Definition Section.cc:275
SectionElement * location() const
ResourceType
Resource types.
Chunk * name() const
ResourceElement & findResource(ResourceElement::ResourceType aType, HalfWord anId) const
bool hasResource(ResourceElement::ResourceType aType, HalfWord anId) const
Section * link() const
@ ST_ADDRSP
Address space section.
Definition Section.hh:77
@ ST_RELOC
Relocation section.
Definition Section.hh:74
@ ST_MR
Machine resources section.
Definition Section.hh:78
SectionElement * element(Word index) const
virtual bool isCodeSection() const
Definition Section.hh:143
Word elementCount() const
virtual bool isChunkable() const
Definition Section.cc:157
std::string chunk2String(const Chunk *chunk) const
TPEFTools(const Binary &aTpef)
Definition TPEFTools.cc:54
std::map< const SectionElement *, const RelocElement * > relocationCache_
Cache of Chunks and immediates, which are relocated.
Definition TPEFTools.hh:110
std::string resourceName(ResourceElement::ResourceType type, HalfWord resourceId) const
Definition TPEFTools.cc:84
const Binary & tpef_
TPEF whose resources are accessed.
Definition TPEFTools.hh:106
Section & sectionOfElement(const SectionElement &element)
Definition TPEFTools.cc:72
void clearCache() const
Definition TPEFTools.cc:61
const RelocElement & relocation(const SectionElement &element) const
Definition TPEFTools.cc:119
std::string addressSpaceName(const ASpaceElement &aSpace) const
Definition TPEFTools.cc:96
void initRelocationCache() const
Definition TPEFTools.cc:323
bool hasRelocation(const SectionElement &element) const
Definition TPEFTools.cc:107