OpenASIP 2.2
Loading...
Searching...
No Matches
TPEFWriter.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 TPEFWriter.cc
26 *
27 * Definition of TPEFWriter class.
28 *
29 * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30 *
31 * @note rating: yellow
32 */
33
34#include <list>
35
36#include "TPEFWriter.hh"
37#include "SectionWriter.hh"
38#include "Binary.hh"
39#include "Section.hh"
40#include "ValueReplacer.hh"
41#include "FileOffsetReplacer.hh"
43#include "BinaryStream.hh"
44#include "TPEFHeaders.hh"
45
46namespace TPEF {
47
48using std::list;
49
50BinaryWriter* TPEFWriter::instance_ = NULL;
51
52/**
53 * Constructor.
54 */
58
59/**
60 * Destructor.
61 */
64
65/**
66 * Returns an instance of this class (singleton).
67 *
68 * @return Instance of class.
69 */
71 if (instance_ == NULL) {
72 instance_ = new TPEFWriter();
73 }
74 return *instance_;
75}
76
77/**
78 * Writes binary object model in to a binary stream.
79 *
80 * @param stream Stream where to write.
81 * @param bin Binary to write.
82 */
83void
85 BinaryStream& stream,
86 const Binary* bin) const {
87
88 // check that there is only one resource section and null section
89 if (bin->sectionCount(Section::ST_ADDRSP) > 1 ||
90 bin->sectionCount(Section::ST_MR) > 1) {
91
92 bool onlyOneAddressSpaceAndResourceSectionIsAllowed = false;
93 assert(onlyOneAddressSpaceAndResourceSectionIsAllowed);
94 }
95
96 FileOffset startOffset = stream.writePosition();
97
98 // file header must be written first
99 stream.setWritePosition(startOffset + TPEFHeaders::FH_ID);
100 for (Word i = 0; i < TPEFHeaders::FH_ID_SIZE; i++) {
101 // sets stream tpef version same as binary version
102 if (i == 8) {
103 TPEFHeaders::TPEFVersion version = bin->TPEFVersion();
104 stream.setTPEFVersion(version);
105 stream.writeByte(version);
106 } else {
108 }
109 }
110
111 stream.setWritePosition(startOffset + TPEFHeaders::FH_ARCH);
112 stream.writeByte(bin->arch());
113
114 stream.setWritePosition(startOffset + TPEFHeaders::FH_TYPE);
115 stream.writeByte(bin->type());
116
117 // offset to the first section header
118 stream.setWritePosition(startOffset + TPEFHeaders::FH_SHOFF);
120
121 // size of file header
122 stream.setWritePosition(startOffset + TPEFHeaders::FH_SIZE);
124
125 stream.setWritePosition(startOffset + TPEFHeaders::FH_SHSIZE);
127
128 // number of sections
129 stream.setWritePosition(startOffset + TPEFHeaders::FH_SHNUM);
130 stream.writeHalfWord(bin->sectionCount());
131
132 // file offset to the header of string table section
133 stream.setWritePosition(startOffset + TPEFHeaders::FH_SHSTRTAB);
134 if (bin->strings() != NULL) {
135 FileOffsetReplacer replacer(bin->strings());
136 replacer.resolve();
137 } else {
138 stream.writeWord(0);
139 }
140
141 // offset of first header
142 FileOffset sectionHeaderOffset = TPEFHeaders::FH_HEADER_SIZE;
143
144 Section* section = NULL;
145
146 for (Word i = 0; i < bin->sectionCount(); i++) {
147 section = bin->section(i);
148 try {
149 stream.setWritePosition(startOffset + sectionHeaderOffset);
150 SectionWriter::writeHeader(stream, section, this);
151 sectionHeaderOffset += TPEFHeaders::SH_HEADER_SIZE;
152
153 } catch (const InstanceNotFound& i) {
154 std::cerr << "Writer for a section header not found"
155 << std::endl;
156 continue;
157 }
158 }
159
160 // then the section bodies
161 for (Word i = 0; i < bin->sectionCount(); i++) {
162 section = bin->section(i);
163 try {
164 if (!section->noBits()) {
165 SectionWriter::writeData(stream, section, this);
166 } else {
167 // TODO: If we are in this branch verify that there is
168 // only undef elements in section.
169 // i.e. verify that section is empty...
170 }
171 } catch(const InstanceNotFound& i) {
172 abortWithError("Writer for a section body not found");
173 }
174 }
175}
176
177}
#define abortWithError(message)
#define assert(condition)
void writeByte(Byte byte)
void writeHalfWord(HalfWord halfword)
unsigned int writePosition()
void setTPEFVersion(TPEFHeaders::TPEFVersion version)
void setWritePosition(unsigned int position)
void writeWord(Word word)
void setWriter(const BinaryWriter *writer)
TPEFHeaders::TPEFVersion TPEFVersion() const
Word sectionCount() const
Section * section(Word index) const
StringSection * strings() const
FileArchitecture arch() const
FileType type() const
static void writeHeader(BinaryStream &stream, const Section *sect, const BinaryWriter *writer)
static void writeData(BinaryStream &stream, const Section *sect, const BinaryWriter *writer)
@ ST_ADDRSP
Address space section.
Definition Section.hh:77
@ ST_MR
Machine resources section.
Definition Section.hh:78
bool noBits() const
static const BinaryWriter & instance()
Definition TPEFWriter.cc:70
virtual void actualWriteBinary(BinaryStream &stream, const Binary *bin) const
Definition TPEFWriter.cc:84
static BinaryWriter * instance_
Instance which is used for writing binary file.
Definition TPEFWriter.hh:58
const Byte FH_ID_BYTES[]
File format identification mark (TPEF version 1). See TPEF documentation for more info.
@ FH_ARCH
Architecture template.
@ FH_SHSIZE
Size of section header entry.
@ FH_SHSTRTAB
Offset to header of string table.
@ FH_ID
File identification code.
@ FH_SHNUM
Number of section headers.
@ FH_SHOFF
Offset to first section header.
@ FH_TYPE
Type of TTA program.
@ FH_SIZE
File header size.
const Byte FH_ID_SIZE
Size of file identification code.
const HalfWord SH_HEADER_SIZE
Suze of section header.
const HalfWord FH_HEADER_SIZE
Size of file header.
Word FileOffset
Type for storing absolute file offsets.