gem5  v20.1.0.0
symtab.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2005 The Regents of The University of Michigan
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef __SYMTAB_HH__
30 #define __SYMTAB_HH__
31 
32 #include <functional>
33 #include <iosfwd>
34 #include <map>
35 #include <memory>
36 #include <string>
37 #include <vector>
38 
39 #include "base/trace.hh"
40 #include "base/types.hh"
41 #include "sim/serialize.hh"
42 
43 namespace Loader
44 {
45 
46 struct Symbol
47 {
48  enum class Binding {
49  Global,
50  Local,
51  Weak
52  };
53 
55  std::string name;
57 };
58 
60 {
61  public:
62  typedef std::shared_ptr<SymbolTable> SymbolTablePtr;
63 
64  private:
66  // Map addresses to an index into the symbol vector.
67  typedef std::multimap<Addr, int> AddrMap;
68  // Map a symbol name to an index into the symbol vector.
69  typedef std::map<std::string, int> NameMap;
70 
74 
75  bool
76  upperBound(Addr addr, AddrMap::const_iterator &iter) const
77  {
78  // find first key *larger* than desired address
79  iter = addrMap.upper_bound(addr);
80 
81  // if very first key is larger, we're out of luck
82  if (iter == addrMap.begin())
83  return false;
84 
85  return true;
86  }
87 
88  typedef std::function<void(SymbolTable &symtab,
89  const Symbol &symbol)> SymTabOp;
92  {
93  SymbolTablePtr symtab(new SymbolTable);
94  for (const auto &symbol: symbols)
95  op(*symtab, symbol);
96  return symtab;
97  }
98 
99  typedef std::function<bool(const Symbol &symbol)> SymTabFilter;
102  {
103  SymTabOp apply_filter =
104  [filter](SymbolTable &symtab, const Symbol &symbol) {
105  if (filter(symbol)) {
106  symtab.insert(symbol);
107  }
108  };
109  return operate(apply_filter);
110  }
111 
114  {
115  auto filt = [binding](const Symbol &symbol) {
116  return symbol.binding == binding;
117  };
118  return filter(filt);
119  }
120 
121  public:
122  typedef SymbolVector::iterator iterator;
123  typedef SymbolVector::const_iterator const_iterator;
124 
125  const_iterator begin() const { return symbols.begin(); }
126  const_iterator end() const { return symbols.end(); }
127 
128  void clear();
129  // Insert either a single symbol or the contents of an entire symbol table
130  // into this one.
131  bool insert(const Symbol &symbol);
132  bool insert(const SymbolTable &other);
133  bool load(const std::string &file);
134  bool empty() const { return symbols.empty(); }
135 
137  offset(Addr by) const
138  {
139  SymTabOp op = [by](SymbolTable &symtab, const Symbol &symbol) {
140  Symbol sym = symbol;
141  sym.address += by;
142  symtab.insert(sym);
143  };
144  return operate(op);
145  }
146 
148  mask(Addr m) const
149  {
150  SymTabOp op = [m](SymbolTable &symtab, const Symbol &symbol) {
151  Symbol sym = symbol;
152  sym.address &= m;
153  symtab.insert(sym);
154  };
155  return operate(op);
156  }
157 
159  globals() const
160  {
162  }
163 
165  locals() const
166  {
168  }
169 
171  weaks() const
172  {
174  }
175 
176  void serialize(const std::string &base, CheckpointOut &cp) const;
177  void unserialize(const std::string &base, CheckpointIn &cp,
178  Symbol::Binding default_binding=Symbol::Binding::Global);
179 
181  find(Addr address) const
182  {
183  AddrMap::const_iterator i = addrMap.find(address);
184  if (i == addrMap.end())
185  return end();
186 
187  // There are potentially multiple symbols that map to the same
188  // address. For simplicity, just return the first one.
189  return symbols.begin() + i->second;
190  }
191 
193  find(const std::string &name) const
194  {
195  NameMap::const_iterator i = nameMap.find(name);
196  if (i == nameMap.end())
197  return end();
198 
199  return symbols.begin() + i->second;
200  }
201 
209  findNearest(Addr addr, Addr &nextaddr) const
210  {
211  AddrMap::const_iterator i = addrMap.end();
212  if (!upperBound(addr, i))
213  return end();
214 
215  nextaddr = i->first;
216  --i;
217  return symbols.begin() + i->second;
218  }
219 
224  {
225  AddrMap::const_iterator i = addrMap.end();
226  if (!upperBound(addr, i))
227  return end();
228 
229  --i;
230  return symbols.begin() + i->second;
231  }
232 };
233 
238 extern SymbolTable debugSymbolTable;
239 
240 } // namespace Loader
241 
242 #endif // __SYMTAB_HH__
Loader::Symbol::Binding
Binding
Definition: symtab.hh:48
Loader::Symbol::address
Addr address
Definition: symtab.hh:56
Loader::SymbolTable::findNearest
const_iterator findNearest(Addr addr, Addr &nextaddr) const
Find the nearest symbol equal to or less than the supplied address (e.g., the label for the enclosing...
Definition: symtab.hh:209
Loader::SymbolTable::addrMap
AddrMap addrMap
Definition: symtab.hh:72
Loader::Symbol::Binding::Local
@ Local
Loader::SymbolTable::filterByBinding
SymbolTablePtr filterByBinding(Symbol::Binding binding) const
Definition: symtab.hh:113
serialize.hh
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
Loader::SymbolTable::empty
bool empty() const
Definition: symtab.hh:134
Loader::SymbolTable
Definition: symtab.hh:59
Loader::SymbolTable::SymbolTablePtr
std::shared_ptr< SymbolTable > SymbolTablePtr
Definition: symtab.hh:62
Loader::SymbolTable::SymbolVector
std::vector< Symbol > SymbolVector
Definition: symtab.hh:65
Loader::SymbolTable::load
bool load(const std::string &file)
Definition: symtab.cc:96
X86ISA::base
Bitfield< 51, 12 > base
Definition: pagetable.hh:141
X86ISA::op
Bitfield< 4 > op
Definition: types.hh:78
std::vector< Symbol >
Loader::SymbolTable::iterator
SymbolVector::iterator iterator
Definition: symtab.hh:122
Loader::SymbolTable::locals
SymbolTablePtr locals() const
Definition: symtab.hh:165
Loader::SymbolTable::serialize
void serialize(const std::string &base, CheckpointOut &cp) const
Definition: symtab.cc:136
Loader::SymbolTable::nameMap
NameMap nameMap
Definition: symtab.hh:73
Loader::Symbol::Binding::Global
@ Global
Loader::SymbolTable::find
const_iterator find(Addr address) const
Definition: symtab.hh:181
Loader
Definition: process.hh:39
Loader::debugSymbolTable
SymbolTable debugSymbolTable
Global unified debugging symbol table (for target).
Definition: symtab.cc:47
cp
Definition: cprintf.cc:40
Loader::SymbolTable::findNearest
const_iterator findNearest(Addr addr) const
Overload for findNearestSymbol() for callers who don't care about nextaddr.
Definition: symtab.hh:223
Loader::SymbolTable::NameMap
std::map< std::string, int > NameMap
Definition: symtab.hh:69
Loader::SymbolTable::begin
const_iterator begin() const
Definition: symtab.hh:125
Loader::SymbolTable::offset
SymbolTablePtr offset(Addr by) const
Definition: symtab.hh:137
Loader::SymbolTable::unserialize
void unserialize(const std::string &base, CheckpointIn &cp, Symbol::Binding default_binding=Symbol::Binding::Global)
Definition: symtab.cc:150
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
name
const std::string & name()
Definition: trace.cc:50
Loader::SymbolTable::operate
SymbolTablePtr operate(SymTabOp op) const
Definition: symtab.hh:91
Loader::SymbolTable::globals
SymbolTablePtr globals() const
Definition: symtab.hh:159
Loader::SymbolTable::SymTabOp
std::function< void(SymbolTable &symtab, const Symbol &symbol)> SymTabOp
Definition: symtab.hh:89
Loader::SymbolTable::upperBound
bool upperBound(Addr addr, AddrMap::const_iterator &iter) const
Definition: symtab.hh:76
Loader::SymbolTable::AddrMap
std::multimap< Addr, int > AddrMap
Definition: symtab.hh:67
Loader::SymbolTable::end
const_iterator end() const
Definition: symtab.hh:126
Loader::Symbol::name
std::string name
Definition: symtab.hh:55
Loader::SymbolTable::const_iterator
SymbolVector::const_iterator const_iterator
Definition: symtab.hh:123
Loader::Symbol
Definition: symtab.hh:46
types.hh
Loader::SymbolTable::mask
SymbolTablePtr mask(Addr m) const
Definition: symtab.hh:148
Loader::SymbolTable::symbols
SymbolVector symbols
Definition: symtab.hh:71
Loader::SymbolTable::clear
void clear()
Definition: symtab.cc:50
addr
ip6_addr_t addr
Definition: inet.hh:423
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:63
Loader::SymbolTable::SymTabFilter
std::function< bool(const Symbol &symbol)> SymTabFilter
Definition: symtab.hh:99
trace.hh
Loader::SymbolTable::filter
SymbolTablePtr filter(SymTabFilter filter) const
Definition: symtab.hh:101
Loader::SymbolTable::insert
bool insert(const Symbol &symbol)
Definition: symtab.cc:58
Loader::SymbolTable::weaks
SymbolTablePtr weaks() const
Definition: symtab.hh:171
Loader::SymbolTable::find
const_iterator find(const std::string &name) const
Definition: symtab.hh:193
CheckpointIn
Definition: serialize.hh:67
Loader::Symbol::binding
Binding binding
Definition: symtab.hh:54
Loader::Symbol::Binding::Weak
@ Weak
ArmISA::m
Bitfield< 0 > m
Definition: miscregs_types.hh:389

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17