OpenASIP 2.2
Loading...
Searching...
No Matches
TPEFSectionReader.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 TPEFSectionReader.cc
26 *
27 * Definitions of TPEFSectionReader class.
28 *
29 * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30 *
31 * @note rating: yellow
32 */
33
34#include "TPEFSectionReader.hh"
35#include "BinaryStream.hh"
36#include "TPEFHeaders.hh"
37
38namespace TPEF {
39
40TPEFSectionReader::Header TPEFSectionReader::header_;
41
42using ReferenceManager::SectionKey;
43using ReferenceManager::SectionIndexKey;
44using ReferenceManager::SectionOffsetKey;
45using ReferenceManager::SafePointer;
46
47/**
48 * Constructor
49 */
52
53/**
54 * Destructor
55 */
58
59/**
60 * Returns binary reader instance which uses TPEFSectionReader classes.
61 *
62 * @return Binary reader which uses TPEFSectionReader classes.
63 */
68
69/**
70 * Reads section data from TPEF binary file.
71 *
72 * Base implementation for all TPEF section readers. Reads and keeps section
73 * header data for later use by concrete section readers. Header data is
74 * accessed by readData methods of subclasses.
75 *
76 * @param stream Stream to be read from.
77 * @param section Section where the information is to be stored.
78 * @exception UnreachableStream If reading of section fails.
79 * @exception KeyAlreadyExists Key was in use when trying to register object.
80 * @exception EndOfFile If end of file were reached while it shouldn't.
81 * @exception OutOfRange Some of read values were out of range.
82 * @exception WrongSubclass Some class couldn't do what it was asked for.
83 * @exception UnexpectedValue If there was unexpected value when reading.
84 */
85void
87 TPEFReader* tpefReader = dynamic_cast<TPEFReader*>(parent());
88
89 FileOffset startOffset = stream.readPosition();
90 stream.setReadPosition(startOffset + TPEFHeaders::SH_FLAGS);
91 Byte sectionFlags = stream.readByte();
92
93 // if section vLen flag is not equal to the read value
94 if(section->vLen() != ((sectionFlags & Section::SF_VLEN) != 0)) {
95 throw UnexpectedValue(
96 __FILE__, __LINE__, __func__,
97 "read SF_VLEN flag doesn't match for section");
98 }
99
100 section->setFlags(sectionFlags);
101
102 stream.setReadPosition(startOffset + TPEFHeaders::SH_ADDR);
103 section->setStartingAddress(stream.readWord());
104
105 stream.setReadPosition(startOffset + TPEFHeaders::SH_LINK);
106 header_.linkId = stream.readHalfWord();
107
108 SectionKey linkKey(header_.linkId);
109 section->setLink(CREATE_SAFEPOINTER(linkKey));
110
111 // set address space reference
112 stream.setReadPosition(startOffset + TPEFHeaders::SH_ASPACE);
113 Byte aSpaceIndex = stream.readByte();
114 SectionIndexKey aSpaceKey(tpefReader->aSpaceId(), aSpaceIndex);
115 section->setASpace(CREATE_SAFEPOINTER(aSpaceKey));
116
117 // set name
118 stream.setReadPosition(startOffset + TPEFHeaders::SH_NAME);
119 Word sectionOffsetOfName = stream.readWord();
120
121 if (tpefReader->strTableId() != 0) {
122 SectionOffsetKey nameKey(tpefReader->strTableId(),
123 sectionOffsetOfName);
124
125 section->setName(CREATE_SAFEPOINTER(nameKey));
126 } else {
127 section->setName(&SafePointer::null);
128 }
129
130 // add section key for new section
131 stream.setReadPosition(startOffset + TPEFHeaders::SH_ID);
132 header_.sectionId = stream.readHalfWord();
133
134 SectionKey sectionKey(header_.sectionId);
135 SafePointer::addObjectReference(sectionKey, section);
136
137 stream.setReadPosition(startOffset + TPEFHeaders::SH_INFO);
138 readInfo(stream, section);
139
140 stream.setReadPosition(startOffset + TPEFHeaders::SH_ENTSIZE);
141 header_.elementSize = stream.readWord();
142
143 // read offset of section body
144 stream.setReadPosition(startOffset + TPEFHeaders::SH_OFFSET);
145 header_.bodyOffset = stream.readWord();
146
147 stream.setReadPosition(startOffset + TPEFHeaders::SH_SIZE);
148 header_.bodyLength = stream.readWord();
149
150 // actual class does reading of section body
152}
153
154/**
155 * Reads info field of section header.
156 *
157 * Read position of stream will be moved 4 bytes forward.
158 *
159 * @param stream Stream where from info word is read.
160 */
161void
163 Section* /*sect*/) const {
164 // move four bytes forward by default
165 stream.readWord();
166}
167
168/**
169 * Returns headers of section that we are currently reading.
170 *
171 * @return Headers of section that we are currently reading.
172 */
177
178}
#define __func__
unsigned char Byte
Definition BaseType.hh:116
void setReadPosition(unsigned int position)
unsigned int readPosition()
HalfWord readHalfWord()
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
static const SafePointer null
The default SafePointer that is used in null references.
bool vLen() const
void setStartingAddress(AddressImage address)
void setFlags(Byte flagByte)
void setLink(const ReferenceManager::SafePointer *aLink)
void setASpace(const ReferenceManager::SafePointer *addrSpace)
void setName(const ReferenceManager::SafePointer *sectionName)
@ SF_VLEN
Contains elements with variable length.
Definition Section.hh:90
SectionId aSpaceId()
SectionId strTableId()
static BinaryReader * instance()
virtual BinaryReader * parent() const
virtual void readData(BinaryStream &stream, Section *section) const
static Header header_
Stores values that are needed in actual SectionReader classes.
static const Header & header()
virtual void readInfo(BinaryStream &stream, Section *sect) const
@ SH_LINK
Section identifier link.
@ SH_FLAGS
Flags of section.
@ SH_ASPACE
Section address space identifier.
@ SH_OFFSET
Offset to section data.
@ SH_ID
Section identification code.
@ SH_INFO
Section specific information, usually zero.
@ SH_ADDR
Starting memory address of program section.
@ SH_SIZE
Size of section data.
@ SH_NAME
Section offset to name.
@ SH_ENTSIZE
Size of section elements (if fixed size).
Word FileOffset
Type for storing absolute file offsets.