OpenASIP  2.0
DumpTPEF.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 DumpTPEF.cc
26  *
27  * Program for outputting TPEF hierarchy in textual format.
28  *
29  * @author Mikael Lepist� 2005 (tmlepist-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include <ostream>
34 
35 #include "Exception.hh"
36 #include "CmdLineOptions.hh"
37 #include "BinaryReader.hh"
38 #include "BinaryStream.hh"
39 #include "Binary.hh"
40 #include "TPEFDumper.hh"
41 #include "tce_config.h"
42 
43 using TPEF::BinaryReader;
44 using TPEF::Binary;
45 using TPEF::BinaryStream;
46 
47 #if 0
48  Those flags that dumptpef support are marked with asterisk.
49 
50  objdump
51  [-a|---archive-headers]
52  [-b bfdname| ---target=bfdname]
53  [-C|---demangle[= style] ]
54  [-d|---disassemble]
55  [-D|---disassemble-all]
56  [-z|---disassemble-zeroes]
57  [-EB|-EL| ---endian={big | little }]
58 (*)[-f|---file-headers]
59  [---file-start-context]
60  [-g|---debugging]
61 (*)[-h|---section-headers| ---headers] NOTE: -h is replaced with -s
62  [-i|---info]
63 (*)[-j section| ---section=section]
64  [-l|---line-numbers]
65  [-S|---source]
66  [-m machine| ---architecture=machine]
67  [-M options| ---disassembler-options=options]
68  [-p|---private-headers]
69 (*)[-r|---reloc]
70  [-R|---dynamic-reloc]
71  [-s|---full-contents]
72  [-G|---stabs]
73 (*)[-t|---syms]
74  [-T|---dynamic-syms]
75  [-x|---all-headers]
76  [-w|---wide]
77  [---start-address= address]
78  [---stop-address= address]
79  [---prefix-addresses]
80  [--[no-]show-raw-insn]
81  [---adjust-vma= offset]
82 (*)[-V|---version]
83 (*)[-H|---help]
84  objfile...
85 #endif
86 
87 /**
88  * Commandline options.
89  */
91 public:
93  CmdLineOptions("Usage: dumptpef [options] tpeffile") {
94 
95  BoolCmdLineOptionParser* fileHeadersFlag =
97  "file-headers", "Print file headers.", "f");
98  addOption(fileHeadersFlag);
99 
100  // NOTE: -h was reserved for help
101  BoolCmdLineOptionParser* sectionHeadersFlag =
103  "section-headers", "Print section headers.", "s");
104  addOption(sectionHeadersFlag);
105 
108  "section",
109  "Print elements of section by section index.", "j");
111 
112  BoolCmdLineOptionParser* relocTablesFlag =
114  "reloc", "Print relocation tables.", "r");
115  addOption(relocTablesFlag);
116 
117  BoolCmdLineOptionParser* symbolTablesFlag =
118  new BoolCmdLineOptionParser("syms", "Print symbol tables.", "t");
119  addOption(symbolTablesFlag);
120 
121  BoolCmdLineOptionParser* memoryInfoFlag =
123  "mem", "Print information about memory usage of reserved sections.", "m");
124  addOption(memoryInfoFlag);
125 
126  BoolCmdLineOptionParser* logicalInfoFlag =
128  "logical", "Print only logical information. "
129  "Can be used for checking if two files contains same program "
130  "and data and connections even if it's in different order in "
131  "tpef file.",
132  "l");
133  addOption(logicalInfoFlag);
134  }
135 
137  return findOption("file-headers")->isDefined();
138  }
139 
141  return findOption("section-headers")->isDefined();
142  }
143 
144  bool printSymbols() {
145  return findOption("syms")->isDefined();
146  }
147 
149  return findOption("reloc")->isDefined();
150  }
151 
153  return findOption("mem")->isDefined();
154  }
155 
157  return findOption("logical")->isDefined();
158  }
159 
161  return findOption("section")->listSize();
162  }
163 
164  int sectionId(int index) {
165  return findOption("section")->integer(index);
166  }
167 
168  void printVersion() const {
169  std::cout << "dumptpef - TPEF Dumper "
171  << std::endl;
172  }
173 };
174 
175 
176 /**
177  * Calls TPEFDumper with parameters given in commandline.
178  */
179 int main(int argc, char* argv[]) {
180 
182 
183  try {
184  options.parse(argv, argc);
185  } catch (ParserStopRequest) {
186  return 0;
187  } catch (IllegalCommandLine& e) {
188  std::cerr << "Illegal command line parameters: "
189  << e.errorMessage() << std::endl;
190 
191  options.printHelp();
192  return 0;
193  }
194 
195  if (options.numberOfArguments() != 1) {
196  std::cerr << "Error: Must give input binary file.\n\n";
197  options.printHelp();
198  return 0;
199  }
200 
201  BinaryStream stream(options.argument(1));
202 
203  Binary* tpef = NULL;
204  try {
205  tpef = BinaryReader::readBinary(stream);
206  } catch (UnresolvedReference& e) {
207  std::cerr
208  << "Misread the input file: " << options.argument(1) << std::endl
209  << "error: " << e.errorMessage() << std::endl;
210  return 0;
211  } catch (const Exception& e) {
212  std::cerr
213  << "Can't read input file: " << options.argument(1) << std::endl
214  << "error: " << e.errorMessage() << std::endl;
215  return 0;
216  }
217 
218  TPEFDumper dumper(*tpef, std::cout);
219 
220  dumper.setOnlyLogical(options.onlyLogicalInfo());
221 
222  if (options.printFileHeaders()) {
223  dumper.fileHeaders();
224  }
225 
226  if (options.printSectionHeaders()) {
227  dumper.sectionHeaders();
228  }
229 
230  if (options.printMemoryInfo()) {
231  dumper.memoryInfo();
232  }
233 
234  if (options.printSymbols()) {
235  dumper.symbolTables();
236  }
237 
238  if (options.printRelocations()) {
239  dumper.relocationTables();
240  }
241 
242  for (int i = 1; i < options.sectionIdCount() + 1; i++) {
243  dumper.section(options.sectionId(i));
244  }
245 
246  delete tpef;
247 
248  return 0;
249 }
DumperCmdLineOptions::printFileHeaders
bool printFileHeaders()
Definition: DumpTPEF.cc:136
MachInfoCmdLineOptions::printHelp
virtual void printHelp() const
Definition: MachInfoCmdLineOptions.cc:89
BinaryReader.hh
UnresolvedReference
Definition: Exception.hh:370
ParserStopRequest
Definition: Exception.hh:491
TPEFDumper::sectionHeaders
void sectionHeaders()
Definition: TPEFDumper.cc:210
machine
TTAMachine::Machine * machine
the architecture definition of the estimated processor
Definition: EstimatorCmdLineUI.cc:59
DumperCmdLineOptions::printSectionHeaders
bool printSectionHeaders()
Definition: DumpTPEF.cc:140
Exception.hh
CmdLineParser::numberOfArguments
virtual int numberOfArguments() const
CmdLineOptionParser::isDefined
bool isDefined()
Definition: CmdLineOptionParser.cc:169
main
int main(int argc, char *argv[])
Definition: DumpTPEF.cc:179
TPEF::Binary
Definition: Binary.hh:49
TPEF::BinaryStream
Definition: BinaryStream.hh:59
CmdLineOptions.hh
TPEFDumper::memoryInfo
void memoryInfo()
Definition: TPEFDumper.cc:232
IllegalCommandLine
Definition: Exception.hh:438
DumperCmdLineOptions::printVersion
void printVersion() const
Definition: DumpTPEF.cc:168
TPEF::BinaryReader
Definition: BinaryReader.hh:51
TPEFDumper::relocationTables
void relocationTables()
Definition: TPEFDumper.cc:321
DumperCmdLineOptions::printMemoryInfo
bool printMemoryInfo()
Definition: DumpTPEF.cc:152
TPEFDumper::setOnlyLogical
void setOnlyLogical(bool flag)
Definition: TPEFDumper.cc:157
DumperCmdLineOptions::sectionId
int sectionId(int index)
Definition: DumpTPEF.cc:164
IntegerListCmdLineOptionParser
Definition: CmdLineOptionParser.hh:310
CmdLineParser::addOption
void addOption(CmdLineOptionParser *opt)
DumperCmdLineOptions
Definition: DumpTPEF.cc:90
CmdLineOptionParser::listSize
virtual int listSize() const
Definition: CmdLineOptionParser.cc:150
TPEFDumper
Definition: TPEFDumper.hh:51
CmdLineOptions
Definition: CmdLineOptions.hh:54
BoolCmdLineOptionParser
Definition: CmdLineOptionParser.hh:278
DumperCmdLineOptions::sectionIdCount
int sectionIdCount()
Definition: DumpTPEF.cc:160
Exception
Definition: Exception.hh:54
DumperCmdLineOptions::onlyLogicalInfo
bool onlyLogicalInfo()
Definition: DumpTPEF.cc:156
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
CmdLineOptions::parse
void parse(char *argv[], int argc)
Definition: CmdLineOptions.cc:107
TPEFDumper::symbolTables
void symbolTables()
Definition: TPEFDumper.cc:336
options
static MachInfoCmdLineOptions options
Definition: MachInfo.cc:46
DumperCmdLineOptions::DumperCmdLineOptions
DumperCmdLineOptions()
Definition: DumpTPEF.cc:92
TPEFDumper.hh
TPEFDumper::fileHeaders
void fileHeaders()
Definition: TPEFDumper.cc:181
R
static RegisterPass< MachineDCE > R("machinedce","Symbol string based machine DCE for removing not used emulation functions", false, true)
DumperCmdLineOptions::printSymbols
bool printSymbols()
Definition: DumpTPEF.cc:144
DumperCmdLineOptions::printRelocations
bool printRelocations()
Definition: DumpTPEF.cc:148
BinaryStream.hh
CmdLineOptionParser::integer
virtual int integer(int index=0) const
Definition: CmdLineOptionParser.cc:84
CmdLineParser::findOption
CmdLineOptionParser * findOption(std::string name) const
Definition: CmdLineParser.cc:160
CmdLineParser::argument
virtual std::string argument(int index) const
Application::TCEVersionString
static std::string TCEVersionString()
Definition: Application.cc:510
TPEFDumper::section
void section(Word sectionIndex)
Definition: TPEFDumper.cc:353
Binary.hh