gem5  v22.1.0.0
symtab.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 Daniel R. Carvalho
3  * Copyright (c) 2002-2005 The Regents of The University of Michigan
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met: redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer;
10  * redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution;
13  * neither the name of the copyright holders nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef __BASE_LOADER_SYMTAB_HH__
31 #define __BASE_LOADER_SYMTAB_HH__
32 
33 #include <functional>
34 #include <iosfwd>
35 #include <map>
36 #include <memory>
37 #include <string>
38 #include <vector>
39 
40 #include "base/compiler.hh"
41 #include "base/types.hh"
42 #include "sim/serialize.hh"
43 
44 namespace gem5
45 {
46 
47 GEM5_DEPRECATED_NAMESPACE(Loader, loader);
48 namespace loader
49 {
50 
51 struct Symbol
52 {
53  enum class Binding
54  {
55  Global,
56  Local,
57  Weak
58  };
59 
61  std::string name;
63 };
64 
66 {
67  public:
68  typedef std::shared_ptr<SymbolTable> SymbolTablePtr;
69 
70  private:
74  typedef std::multimap<Addr, int> AddrMap;
76  typedef std::map<std::string, int> NameMap;
77 
81 
89  bool
90  upperBound(Addr addr, AddrMap::const_iterator &iter) const
91  {
92  // find first key *larger* than desired address
93  iter = addrMap.upper_bound(addr);
94 
95  // if very first key is larger, we're out of luck
96  if (iter == addrMap.begin())
97  return false;
98 
99  return true;
100  }
101 
107  typedef std::function<void(SymbolTable &symtab,
108  const Symbol &symbol)> SymTabOp;
109 
119  {
120  SymbolTablePtr symtab(new SymbolTable);
121  for (const auto &symbol: symbols)
122  op(*symtab, symbol);
123  return symtab;
124  }
125 
130  typedef std::function<bool(const Symbol &symbol)> SymTabFilter;
131 
142  {
143  SymTabOp apply_filter =
144  [filter](SymbolTable &symtab, const Symbol &symbol) {
145  if (filter(symbol)) {
146  symtab.insert(symbol);
147  }
148  };
149  return operate(apply_filter);
150  }
151 
161  {
162  auto filt = [binding](const Symbol &symbol) {
163  return symbol.binding == binding;
164  };
165  return filter(filt);
166  }
167 
168  public:
169  typedef SymbolVector::iterator iterator;
170  typedef SymbolVector::const_iterator const_iterator;
171 
173  const_iterator begin() const { return symbols.begin(); }
174 
176  const_iterator end() const { return symbols.end(); }
177 
179  void clear();
180 
188  bool insert(const Symbol &symbol);
189 
197  bool insert(const SymbolTable &other);
198 
204  bool empty() const { return symbols.empty(); }
205 
214  offset(Addr addr_offset) const
215  {
216  SymTabOp op =
217  [addr_offset](SymbolTable &symtab, const Symbol &symbol) {
218  Symbol sym = symbol;
219  sym.address += addr_offset;
220  symtab.insert(sym);
221  };
222  return operate(op);
223  }
224 
233  mask(Addr m) const
234  {
235  SymTabOp op = [m](SymbolTable &symtab, const Symbol &symbol) {
236  Symbol sym = symbol;
237  sym.address &= m;
238  symtab.insert(sym);
239  };
240  return operate(op);
241  }
242 
251  rename(std::function<void(std::string&)> func) const
252  {
253  SymTabOp op = [func](SymbolTable &symtab, const Symbol &symbol) {
254  Symbol sym = symbol;
255  func(sym.name);
256  symtab.insert(sym);
257  };
258  return operate(op);
259  }
260 
267  globals() const
268  {
270  }
271 
278  locals() const
279  {
281  }
282 
289  weaks() const
290  {
292  }
293 
300  void serialize(const std::string &base, CheckpointOut &cp) const;
301 
310  void unserialize(const std::string &base, CheckpointIn &cp,
311  Symbol::Binding default_binding=Symbol::Binding::Global);
312 
322  find(Addr address) const
323  {
324  AddrMap::const_iterator i = addrMap.find(address);
325  if (i == addrMap.end())
326  return end();
327 
328  // There are potentially multiple symbols that map to the same
329  // address. For simplicity, just return the first one.
330  return symbols.begin() + i->second;
331  }
332 
341  find(const std::string &name) const
342  {
343  NameMap::const_iterator i = nameMap.find(name);
344  if (i == nameMap.end())
345  return end();
346 
347  return symbols.begin() + i->second;
348  }
349 
361  findNearest(Addr addr, Addr &next_addr) const
362  {
363  AddrMap::const_iterator i = addrMap.end();
364  if (!upperBound(addr, i))
365  return end();
366 
367  // If there is no next address, make it 0 since 0 is not larger than
368  // any other address, so it is clear that next is not valid
369  if (i == addrMap.end()) {
370  next_addr = 0;
371  } else {
372  next_addr = i->first;
373  }
374  --i;
375  return symbols.begin() + i->second;
376  }
377 
384  {
385  AddrMap::const_iterator i = addrMap.end();
386  if (!upperBound(addr, i))
387  return end();
388 
389  --i;
390  return symbols.begin() + i->second;
391  }
392 };
393 
400 extern SymbolTable debugSymbolTable;
401 
402 } // namespace loader
403 } // namespace gem5
404 
405 #endif // __BASE_LOADER_SYMTAB_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
SymbolTablePtr locals() const
Generates a new symbol table containing only local symbols.
Definition: symtab.hh:278
SymbolVector::const_iterator const_iterator
Definition: symtab.hh:170
SymbolTablePtr weaks() const
Generates a new symbol table containing only weak symbols.
Definition: symtab.hh:289
bool empty() const
Verifies whether the table is empty.
Definition: symtab.hh:204
SymbolTablePtr globals() const
Generates a new symbol table containing only global symbols.
Definition: symtab.hh:267
std::shared_ptr< SymbolTable > SymbolTablePtr
Definition: symtab.hh:68
std::map< std::string, int > NameMap
Map a symbol name to an index into the symbol vector.
Definition: symtab.hh:76
SymbolTablePtr filter(SymTabFilter filter) const
Applies a filter to the symbols of the table to generate a new table.
Definition: symtab.hh:141
void clear()
Clears the table.
Definition: symtab.cc:47
std::multimap< Addr, int > AddrMap
Map addresses to an index into the symbol vector.
Definition: symtab.hh:74
SymbolVector symbols
Definition: symtab.hh:78
const_iterator end() const
Definition: symtab.hh:176
void unserialize(const std::string &base, CheckpointIn &cp, Symbol::Binding default_binding=Symbol::Binding::Global)
Populate the table by unserializing a checkpoint.
Definition: symtab.cc:107
SymbolTablePtr operate(SymTabOp op) const
Create a derived symbol table by applying an operation on the symbols of the current table.
Definition: symtab.hh:118
SymbolTablePtr filterByBinding(Symbol::Binding binding) const
Generate a new table by applying a filter that only accepts the symbols whose binding matches the giv...
Definition: symtab.hh:160
std::function< void(SymbolTable &symtab, const Symbol &symbol)> SymTabOp
A function that applies an operation on a symbol with respect to a symbol table.
Definition: symtab.hh:108
const_iterator findNearest(Addr addr) const
Overload for findNearestSymbol() for callers who don't care about nextaddr.
Definition: symtab.hh:383
const_iterator begin() const
Definition: symtab.hh:173
SymbolVector::iterator iterator
Definition: symtab.hh:169
std::vector< Symbol > SymbolVector
Vector containing all the symbols in the table.
Definition: symtab.hh:72
const_iterator findNearest(Addr addr, Addr &next_addr) const
Find the nearest symbol equal to or less than the supplied address (e.g., the label for the enclosing...
Definition: symtab.hh:361
SymbolTablePtr mask(Addr m) const
Generate a new table by a mask to the symbols of the current table.
Definition: symtab.hh:233
bool upperBound(Addr addr, AddrMap::const_iterator &iter) const
Get the first address larger than the given address, if any.
Definition: symtab.hh:90
const_iterator find(const std::string &name) const
Search for a symbol by its name.
Definition: symtab.hh:341
SymbolTablePtr offset(Addr addr_offset) const
Generate a new table by applying an offset to the symbols of the current table.
Definition: symtab.hh:214
SymbolTablePtr rename(std::function< void(std::string &)> func) const
Modify the symbols' name with a given transform function.
Definition: symtab.hh:251
std::function< bool(const Symbol &symbol)> SymTabFilter
A function that applies a condition to the symbol provided to decide whether the symbol is accepted,...
Definition: symtab.hh:130
void serialize(const std::string &base, CheckpointOut &cp) const
Serialize the table's contents.
Definition: symtab.cc:93
bool insert(const Symbol &symbol)
Insert a new symbol in the table if it does not already exist.
Definition: symtab.cc:55
const_iterator find(Addr address) const
Search for a symbol by its address.
Definition: symtab.hh:322
Bitfield< 7 > i
Definition: misc_types.hh:67
Bitfield< 51, 12 > base
Definition: pagetable.hh:141
Bitfield< 4 > op
Definition: types.hh:83
Bitfield< 3 > addr
Definition: types.hh:84
SymbolTable debugSymbolTable
Global unified debugging symbol table (for target).
Definition: symtab.cc:44
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
std::ostream CheckpointOut
Definition: serialize.hh:66
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
GEM5_DEPRECATED_NAMESPACE(GuestABI, guest_abi)
std::string name
Definition: symtab.hh:61
const std::string & name()
Definition: trace.cc:49

Generated on Wed Dec 21 2022 10:22:29 for gem5 by doxygen 1.9.1