OpenASIP  2.0
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"
42 #include "SectionSizeReplacer.hh"
43 #include "BinaryStream.hh"
44 #include "TPEFHeaders.hh"
45 
46 namespace TPEF {
47 
48 using std::list;
49 
50 BinaryWriter* TPEFWriter::instance_ = NULL;
51 
52 /**
53  * Constructor.
54  */
56  setWriter(this);
57 }
58 
59 /**
60  * Destructor.
61  */
63 }
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  */
83 void
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 }
TPEF::SectionWriter::writeData
static void writeData(BinaryStream &stream, const Section *sect, const BinaryWriter *writer)
Definition: SectionWriter.cc:79
TPEF::BinaryStream::writeHalfWord
void writeHalfWord(HalfWord halfword)
Definition: BinaryStream.cc:336
SectionSizeReplacer.hh
TPEF::BinaryWriter::setWriter
void setWriter(const BinaryWriter *writer)
Definition: BinaryWriter.cc:131
TPEF::TPEFHeaders::FH_TYPE
@ FH_TYPE
Type of TTA program.
Definition: TPEFHeaders.hh:67
ValueReplacer.hh
SectionWriter.hh
TPEF::Binary
Definition: Binary.hh:49
TPEF::BinaryStream::writeWord
void writeWord(Word word)
Definition: BinaryStream.cc:368
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::BinaryStream::writePosition
unsigned int writePosition()
Definition: BinaryStream.cc:592
TPEF::TPEFHeaders::FH_ID_SIZE
const Byte FH_ID_SIZE
Size of file identification code.
Definition: TPEFHeaders.hh:50
TPEFHeaders.hh
TPEF::Binary::section
Section * section(Word index) const
TPEF::Binary::sectionCount
Word sectionCount() const
TPEF::BinaryStream::setTPEFVersion
void setTPEFVersion(TPEFHeaders::TPEFVersion version)
Definition: BinaryStream.cc:86
TPEF::TPEFHeaders::FH_SHSIZE
@ FH_SHSIZE
Size of section header entry.
Definition: TPEFHeaders.hh:70
TPEF::FileOffsetReplacer
Definition: FileOffsetReplacer.hh:48
TPEF::TPEFHeaders::FH_SHSTRTAB
@ FH_SHSTRTAB
Offset to header of string table.
Definition: TPEFHeaders.hh:72
TPEF::ValueReplacer::resolve
void resolve()
Definition: ValueReplacer.cc:90
TPEF::Section
Definition: Section.hh:64
TPEF::TPEFWriter::TPEFWriter
TPEFWriter()
Definition: TPEFWriter.cc:55
TPEF::TPEFHeaders::FH_ID_BYTES
const Byte FH_ID_BYTES[]
File format identification mark (TPEF version 1). See TPEF documentation for more info.
Definition: TPEFHeaders.hh:45
TPEF::TPEFHeaders::FH_HEADER_SIZE
const HalfWord FH_HEADER_SIZE
Size of file header.
Definition: TPEFHeaders.hh:52
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::TPEFHeaders::TPEFVersion
TPEFVersion
Definition: TPEFHeaders.hh:56
TPEF::Binary::strings
StringSection * strings() const
abortWithError
#define abortWithError(message)
Definition: Application.hh:72
TPEF::TPEFWriter::instance_
static BinaryWriter * instance_
Instance which is used for writing binary file.
Definition: TPEFWriter.hh:58
TPEF::Binary::type
FileType type() const
TPEF::TPEFHeaders::FH_SHOFF
@ FH_SHOFF
Offset to first section header.
Definition: TPEFHeaders.hh:68
TPEF::BinaryStream::setWritePosition
void setWritePosition(unsigned int position)
Definition: BinaryStream.cc:689
TPEF::SectionWriter::writeHeader
static void writeHeader(BinaryStream &stream, const Section *sect, const BinaryWriter *writer)
Definition: SectionWriter.cc:62
TPEF::TPEFWriter::~TPEFWriter
~TPEFWriter()
Definition: TPEFWriter.cc:62
TPEF::TPEFHeaders::SH_HEADER_SIZE
const HalfWord SH_HEADER_SIZE
Suze of section header.
Definition: TPEFHeaders.hh:54
TPEF::TPEFHeaders::FH_SHNUM
@ FH_SHNUM
Number of section headers.
Definition: TPEFHeaders.hh:71
TPEF::FileOffset
Word FileOffset
Type for storing absolute file offsets.
Definition: TPEFBaseType.hh:52
TPEF::TPEFWriter::instance
static const BinaryWriter & instance()
Definition: TPEFWriter.cc:70
FileOffsetReplacer.hh
TPEF::BinaryStream::writeByte
void writeByte(Byte byte)
Definition: BinaryStream.cc:310
TPEF::Binary::arch
FileArchitecture arch() const
TPEF::Section::noBits
bool noBits() const
Section.hh
BinaryStream.hh
TPEFWriter.hh
TPEF::TPEFWriter::actualWriteBinary
virtual void actualWriteBinary(BinaryStream &stream, const Binary *bin) const
Definition: TPEFWriter.cc:84
TPEF::Binary::TPEFVersion
TPEFHeaders::TPEFVersion TPEFVersion() const
TPEF::Section::ST_MR
@ ST_MR
Machine resources section.
Definition: Section.hh:78
TPEF::TPEFHeaders::FH_SIZE
@ FH_SIZE
File header size.
Definition: TPEFHeaders.hh:69
TPEF::TPEFHeaders::FH_ID
@ FH_ID
File identification code.
Definition: TPEFHeaders.hh:65
TPEF::TPEFHeaders::FH_ARCH
@ FH_ARCH
Architecture template.
Definition: TPEFHeaders.hh:66
TPEF::Section::ST_ADDRSP
@ ST_ADDRSP
Address space section.
Definition: Section.hh:77
InstanceNotFound
Definition: Exception.hh:304
TPEF::BinaryWriter
Definition: BinaryWriter.hh:50
TPEF
Definition: Assembler.hh:43
Binary.hh