HPCToolkit
Seg.hpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 
60 #ifndef BinUtil_Seg_hpp
61 #define BinUtil_Seg_hpp
62 
63 //************************* System Include Files ****************************
64 
65 #include <string>
66 #include <vector>
67 #include <iostream>
68 
69 //*************************** User Include Files ****************************
70 
71 #include <include/gcc-attr.h>
72 #include <include/uint.h>
73 #include <include/gnu_bfd.h>
74 
75 #include "LM.hpp"
76 
77 #include <lib/isa/ISATypes.hpp>
78 
79 //*************************** Forward Declarations **************************
80 
81 //***************************************************************************
82 // Seg
83 //***************************************************************************
84 
85 namespace BinUtil {
86 
87 class Proc;
88 class Insn;
89 
90 // --------------------------------------------------------------------------
91 // 'Seg' is a base class for representing file segments/sections
92 // of a 'LoadModule'.
93 // --------------------------------------------------------------------------
94 
95 class Seg {
96 public:
98 
99  Seg(LM* lm, const std::string& name, Type type,
100  VMA beg, VMA end, uint64_t size);
101 
102  virtual ~Seg();
103 
104  LM*
105  lm() const
106  { return m_lm; }
107 
108  // Return name and type of section
109  const std::string&
110  name() const
111  { return m_name; }
112 
113  Type
114  type() const
115  { return m_type; }
116 
117  // Return begin/end virtual memory address for section: [begVMA, endVMA).
118  // Note that the end of a section is equal to the begin address of
119  // the next section (or the end of the file) which is different than
120  // the convention used for a 'Proc'.
121  VMA
122  begVMA() const
123  { return m_begVMA; }
124 
125  VMA
126  endVMA() const
127  { return m_endVMA; }
128 
129  // Return size of section
130  uint64_t
131  size() const
132  { return m_size; }
133 
134  // Return true if virtual memory address 'vma' is within the section
135  // WARNING: vma must be unrelocated
136  bool
137  isIn(VMA vma) const
138  { return (m_begVMA <= vma && vma < m_endVMA); }
139 
140  // Convenient wrappers for the 'LM' versions of the same.
141  MachInsn*
142  findMachInsn(VMA vma, ushort &sz) const
143  { return m_lm->findMachInsn(vma, sz); }
144 
145  Insn*
146  findInsn(VMA vma, ushort opIndex) const
147  { return m_lm->findInsn(vma, opIndex); }
148 
149  bool
150  findSrcCodeInfo(VMA vma, ushort opIndex,
151  std::string& func, std::string& file,
152  SrcFile::ln& line) const
153  { return m_lm->findSrcCodeInfo(vma, opIndex, func, file, line); }
154 
155  bool
157  VMA endVMA, ushort eOpIndex,
158  std::string& func, std::string& file,
159  SrcFile::ln& begLine, SrcFile::ln& endLine,
160  uint flags = 1) const
161  {
162  return m_lm->findSrcCodeInfo(begVMA, bOpIndex, endVMA, eOpIndex,
163  func, file, begLine, endLine, flags);
164  }
165 
166  // -------------------------------------------------------
167  // debugging
168  // -------------------------------------------------------
169  // Current meaning of 'flags'
170  // 0 : short dump (without instructions)
171  // 1 : full dump
172 
173  std::string
174  toString(int flags = LM::DUMP_Short, const char* pre = "") const;
175 
176  virtual void
177  dump(std::ostream& o = std::cerr,
178  int flags = LM::DUMP_Short, const char* pre = "") const;
179 
180  void
181  ddump() const;
182 
183 protected:
184  // Should not be used
185  Seg()
186  { }
187 
189  { }
190 
191  Seg&
193  { return *this; }
194 
195 protected:
196  LM* m_lm; // do not own
197 
198 private:
199  std::string m_name;
201  VMA m_begVMA; // beginning of section
202  VMA m_endVMA; // end of section [equal to the beginning of next section]
203  uint64_t m_size; // size in bytes
204 };
205 
206 } // namespace BinUtil
207 
208 
209 //***************************************************************************
210 // TextSeg
211 //***************************************************************************
212 
213 namespace BinUtil {
214 
215 
216 // --------------------------------------------------------------------------
217 // 'TextSeg' represents a text segment in a 'LM' and
218 // implements special functionality pertaining to it.
219 // --------------------------------------------------------------------------
220 
221 class TextSeg : public Seg {
222 public:
223  typedef std::vector<Proc*> ProcVec;
224 
225 public:
226  TextSeg(LM* lm, const std::string& name,
227  VMA beg, VMA end, uint64_t size);
228  virtual ~TextSeg();
229 
230  // -------------------------------------------------------
231  // Procedures
232  // -------------------------------------------------------
233 
234  uint
235  numProcs() const
236  { return m_procs.size(); }
237 
238 #if 0
240  begin() {
241  if (size() > 0) {
242  return m_lm->procs().find(VMAInterval(begVMA(), begVMA() + 1));
243  }
244  else {
245  return m_lm->procs().end();
246  }
247  }
248 
250  begin() const {
251  return const_cast<TextSeg*>(this)->begin();
252  }
253 
255  end() {
256  if (size() > 0) {
257  return m_lm->procs().find(VMAInterval(endVMA() - 1, endVMA()));
258  }
259  else {
260  return m_lm->procs().end();
261  }
262  }
263 
265  end() const {
266  return const_cast<TextSeg*>(this)->end();
267  }
268 #endif
269 
270 
271  // -------------------------------------------------------
272  // debugging
273  // -------------------------------------------------------
274  virtual void
275  dump(std::ostream& o = std::cerr,
276  int flags = LM::DUMP_Short, const char* pre = "") const;
277 
278 private:
279  // Should not be used
281  { }
282 
284  { }
285 
286  TextSeg&
288  { return *this; }
289 
290  void
291  ctor_initProcs();
292 
293  void
294  ctor_readSegment();
295 
296  void
297  ctor_disassembleProcs();
298 
299  // Construction helpers
300  std::string
301  findProcName(bfd* abfd, asymbol* procSym) const;
302 
303  VMA
304  findProcEnd(int funcSymIndex) const;
305 
306  Insn*
307  makeInsn(bfd* abfd, MachInsn* mi, VMA vma,
308  ushort opIndex, ushort sz) const;
309 
310 protected:
311 private:
312  ProcVec m_procs;
313  char* m_contents; // contents, aligned with a 16-byte boundary
314  char* m_contentsRaw; // allocated memory for section contents (we own)
315 };
316 
317 
318 } // namespace BinUtil
319 
320 //****************************************************************************
321 
322 #endif // BinUtil_Seg_hpp
bool findSrcCodeInfo(VMA begVMA, ushort bOpIndex, VMA endVMA, ushort eOpIndex, std::string &func, std::string &file, SrcFile::ln &begLine, SrcFile::ln &endLine, uint flags=1) const
Definition: Seg.hpp:156
My_t::iterator iterator
ProcMap & procs()
Definition: LM.hpp:273
unsigned int ln
Definition: SrcFile.hpp:66
bfd_vma VMA
Definition: ISATypes.hpp:79
LM * lm() const
Definition: Seg.hpp:105
char * m_contents
Definition: Seg.hpp:313
virtual ~Seg()
Definition: Seg.cpp:114
Type m_type
Definition: Seg.hpp:200
VMA endVMA() const
Definition: Seg.hpp:126
Type type() const
Definition: Seg.hpp:114
Seg(const Seg &GCC_ATTR_UNUSED s)
Definition: Seg.hpp:188
MachInsn * findMachInsn(VMA vma, ushort &size) const
Definition: LM.cpp:503
const std::string & name() const
Definition: Seg.hpp:110
bool isIn(VMA vma) const
Definition: Seg.hpp:137
unsigned short int ushort
Definition: uint.h:120
unsigned int uint
Definition: uint.h:124
void ddump() const
Definition: Seg.cpp:150
VMA m_endVMA
Definition: Seg.hpp:202
std::string toString(int flags=LM::DUMP_Short, const char *pre="") const
Definition: Seg.cpp:121
uint64_t size() const
Definition: Seg.hpp:131
char * m_contentsRaw
Definition: Seg.hpp:314
VMA begVMA() const
Definition: Seg.hpp:122
ProcVec m_procs
Definition: Seg.hpp:312
My_t::const_iterator const_iterator
Insn * findInsn(VMA vma, ushort opIndex) const
Definition: Seg.hpp:146
VMA m_begVMA
Definition: Seg.hpp:201
virtual void dump(std::ostream &o=std::cerr, int flags=LM::DUMP_Short, const char *pre="") const
Definition: Seg.cpp:130
void MachInsn
Definition: ISATypes.hpp:87
LM * m_lm
Definition: Seg.hpp:196
Insn * findInsn(VMA vma, ushort opIndex) const
Definition: LM.hpp:335
bool findSrcCodeInfo(VMA vma, ushort opIndex, std::string &func, std::string &file, SrcFile::ln &line) const
Definition: Seg.hpp:150
iterator find(const key_type &toFind)
std::string m_name
Definition: Seg.hpp:199
TextSeg & operator=(const TextSeg &GCC_ATTR_UNUSED s)
Definition: Seg.hpp:287
bool findSrcCodeInfo(VMA vma, ushort opIndex, std::string &func, std::string &file, SrcFile::ln &line)
Seg & operator=(const Seg &GCC_ATTR_UNUSED s)
Definition: Seg.hpp:192
#define GCC_ATTR_UNUSED
Definition: gcc-attr.h:80
static MachInsn * mi
Definition: x86ISAXed.cpp:91
TextSeg(const TextSeg &GCC_ATTR_UNUSED s)
Definition: Seg.hpp:283
MachInsn * findMachInsn(VMA vma, ushort &sz) const
Definition: Seg.hpp:142
std::vector< Proc * > ProcVec
Definition: Seg.hpp:223
uint64_t m_size
Definition: Seg.hpp:203
uint numProcs() const
Definition: Seg.hpp:235