gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 namespace loader
48 {
49 
50 struct Symbol
51 {
52  enum class Binding
53  {
54  Global,
55  Local,
56  Weak
57  };
58 
60  std::string name;
62 };
63 
65 {
66  public:
67  typedef std::shared_ptr<SymbolTable> SymbolTablePtr;
68 
69  private:
73  typedef std::multimap<Addr, int> AddrMap;
75  typedef std::map<std::string, int> NameMap;
76 
80 
88  bool
89  upperBound(Addr addr, AddrMap::const_iterator &iter) const
90  {
91  // find first key *larger* than desired address
92  iter = addrMap.upper_bound(addr);
93 
94  // if very first key is larger, we're out of luck
95  if (iter == addrMap.begin())
96  return false;
97 
98  return true;
99  }
100 
106  typedef std::function<void(SymbolTable &symtab,
107  const Symbol &symbol)> SymTabOp;
108 
118  {
119  SymbolTablePtr symtab(new SymbolTable);
120  for (const auto &symbol: symbols)
121  op(*symtab, symbol);
122  return symtab;
123  }
124 
129  typedef std::function<bool(const Symbol &symbol)> SymTabFilter;
130 
141  {
142  SymTabOp apply_filter =
143  [filter](SymbolTable &symtab, const Symbol &symbol) {
144  if (filter(symbol)) {
145  symtab.insert(symbol);
146  }
147  };
148  return operate(apply_filter);
149  }
150 
160  {
161  auto filt = [binding](const Symbol &symbol) {
162  return symbol.binding == binding;
163  };
164  return filter(filt);
165  }
166 
167  public:
168  typedef SymbolVector::iterator iterator;
169  typedef SymbolVector::const_iterator const_iterator;
170 
172  const_iterator begin() const { return symbols.begin(); }
173 
175  const_iterator end() const { return symbols.end(); }
176 
178  void clear();
179 
187  bool insert(const Symbol &symbol);
188 
196  bool insert(const SymbolTable &other);
197 
203  bool empty() const { return symbols.empty(); }
204 
213  offset(Addr addr_offset) const
214  {
215  SymTabOp op =
216  [addr_offset](SymbolTable &symtab, const Symbol &symbol) {
217  Symbol sym = symbol;
218  sym.address += addr_offset;
219  symtab.insert(sym);
220  };
221  return operate(op);
222  }
223 
232  mask(Addr m) const
233  {
234  SymTabOp op = [m](SymbolTable &symtab, const Symbol &symbol) {
235  Symbol sym = symbol;
236  sym.address &= m;
237  symtab.insert(sym);
238  };
239  return operate(op);
240  }
241 
250  rename(std::function<void(std::string&)> func) const
251  {
252  SymTabOp op = [func](SymbolTable &symtab, const Symbol &symbol) {
253  Symbol sym = symbol;
254  func(sym.name);
255  symtab.insert(sym);
256  };
257  return operate(op);
258  }
259 
266  globals() const
267  {
269  }
270 
277  locals() const
278  {
280  }
281 
288  weaks() const
289  {
291  }
292 
299  void serialize(const std::string &base, CheckpointOut &cp) const;
300 
309  void unserialize(const std::string &base, CheckpointIn &cp,
310  Symbol::Binding default_binding=Symbol::Binding::Global);
311 
321  find(Addr address) const
322  {
323  AddrMap::const_iterator i = addrMap.find(address);
324  if (i == addrMap.end())
325  return end();
326 
327  // There are potentially multiple symbols that map to the same
328  // address. For simplicity, just return the first one.
329  return symbols.begin() + i->second;
330  }
331 
340  find(const std::string &name) const
341  {
342  NameMap::const_iterator i = nameMap.find(name);
343  if (i == nameMap.end())
344  return end();
345 
346  return symbols.begin() + i->second;
347  }
348 
360  findNearest(Addr addr, Addr &next_addr) const
361  {
362  AddrMap::const_iterator i = addrMap.end();
363  if (!upperBound(addr, i))
364  return end();
365 
366  // If there is no next address, make it 0 since 0 is not larger than
367  // any other address, so it is clear that next is not valid
368  if (i == addrMap.end()) {
369  next_addr = 0;
370  } else {
371  next_addr = i->first;
372  }
373  --i;
374  return symbols.begin() + i->second;
375  }
376 
383  {
384  AddrMap::const_iterator i = addrMap.end();
385  if (!upperBound(addr, i))
386  return end();
387 
388  --i;
389  return symbols.begin() + i->second;
390  }
391 };
392 
399 extern SymbolTable debugSymbolTable;
400 
401 } // namespace loader
402 } // namespace gem5
403 
404 #endif // __BASE_LOADER_SYMTAB_HH__
gem5::loader::Symbol::Binding
Binding
Definition: symtab.hh:52
gem5::loader::SymbolTable::rename
SymbolTablePtr rename(std::function< void(std::string &)> func) const
Modify the symbols' name with a given transform function.
Definition: symtab.hh:250
gem5::loader::Symbol::name
std::string name
Definition: symtab.hh:60
gem5::loader::SymbolTable::filterByBinding
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:159
gem5::loader::SymbolTable::globals
SymbolTablePtr globals() const
Generates a new symbol table containing only global symbols.
Definition: symtab.hh:266
gem5::VegaISA::m
m
Definition: pagetable.hh:52
gem5::loader::Symbol::Binding::Global
@ Global
serialize.hh
gem5::loader::SymbolTable::SymbolVector
std::vector< Symbol > SymbolVector
Vector containing all the symbols in the table.
Definition: symtab.hh:71
gem5::loader::SymbolTable::unserialize
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:106
gem5::CheckpointIn
Definition: serialize.hh:68
gem5::loader::SymbolTable::end
const_iterator end() const
Definition: symtab.hh:175
gem5::loader::SymbolTable
Definition: symtab.hh:64
std::vector< Symbol >
gem5::loader::SymbolTable::find
const_iterator find(const std::string &name) const
Search for a symbol by its name.
Definition: symtab.hh:340
gem5::X86ISA::base
Bitfield< 51, 12 > base
Definition: pagetable.hh:141
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:67
gem5::loader::SymbolTable::addrMap
AddrMap addrMap
Definition: symtab.hh:78
gem5::loader::SymbolTable::const_iterator
SymbolVector::const_iterator const_iterator
Definition: symtab.hh:169
gem5::loader::SymbolTable::nameMap
NameMap nameMap
Definition: symtab.hh:79
gem5::loader::SymbolTable::SymbolTablePtr
std::shared_ptr< SymbolTable > SymbolTablePtr
Definition: symtab.hh:67
gem5::loader::SymbolTable::empty
bool empty() const
Verifies whether the table is empty.
Definition: symtab.hh:203
gem5::loader::SymbolTable::AddrMap
std::multimap< Addr, int > AddrMap
Map addresses to an index into the symbol vector.
Definition: symtab.hh:73
gem5::loader::SymbolTable::findNearest
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:360
gem5::loader::SymbolTable::offset
SymbolTablePtr offset(Addr addr_offset) const
Generate a new table by applying an offset to the symbols of the current table.
Definition: symtab.hh:213
compiler.hh
gem5::loader::Symbol::binding
Binding binding
Definition: symtab.hh:59
gem5::Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
gem5::loader::SymbolTable::findNearest
const_iterator findNearest(Addr addr) const
Overload for findNearestSymbol() for callers who don't care about nextaddr.
Definition: symtab.hh:382
name
const std::string & name()
Definition: trace.cc:48
gem5::loader::SymbolTable::filter
SymbolTablePtr filter(SymTabFilter filter) const
Applies a filter to the symbols of the table to generate a new table.
Definition: symtab.hh:140
gem5::loader::SymbolTable::find
const_iterator find(Addr address) const
Search for a symbol by its address.
Definition: symtab.hh:321
gem5::loader::SymbolTable::begin
const_iterator begin() const
Definition: symtab.hh:172
gem5::loader::Symbol::Binding::Weak
@ Weak
gem5::loader::SymbolTable::upperBound
bool upperBound(Addr addr, AddrMap::const_iterator &iter) const
Get the first address larger than the given address, if any.
Definition: symtab.hh:89
gem5::loader::SymbolTable::insert
bool insert(const Symbol &symbol)
Insert a new symbol in the table if it does not already exist.
Definition: symtab.cc:54
gem5::loader::SymbolTable::iterator
SymbolVector::iterator iterator
Definition: symtab.hh:168
types.hh
gem5::loader::SymbolTable::NameMap
std::map< std::string, int > NameMap
Map a symbol name to an index into the symbol vector.
Definition: symtab.hh:75
gem5::loader::Symbol
Definition: symtab.hh:50
gem5::loader::Symbol::address
Addr address
Definition: symtab.hh:61
gem5::loader::SymbolTable::operate
SymbolTablePtr operate(SymTabOp op) const
Create a derived symbol table by applying an operation on the symbols of the current table.
Definition: symtab.hh:117
gem5::loader::SymbolTable::SymTabFilter
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:129
gem5::CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:66
gem5::loader::SymbolTable::locals
SymbolTablePtr locals() const
Generates a new symbol table containing only local symbols.
Definition: symtab.hh:277
gem5::loader::debugSymbolTable
SymbolTable debugSymbolTable
Global unified debugging symbol table (for target).
Definition: symtab.cc:43
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::loader::SymbolTable::SymTabOp
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:107
gem5::loader::SymbolTable::clear
void clear()
Clears the table.
Definition: symtab.cc:46
gem5::loader::Symbol::Binding::Local
@ Local
gem5::X86ISA::op
Bitfield< 4 > op
Definition: types.hh:83
gem5::loader::SymbolTable::serialize
void serialize(const std::string &base, CheckpointOut &cp) const
Serialize the table's contents.
Definition: symtab.cc:92
gem5::loader::SymbolTable::weaks
SymbolTablePtr weaks() const
Generates a new symbol table containing only weak symbols.
Definition: symtab.hh:288
gem5::loader::SymbolTable::symbols
SymbolVector symbols
Definition: symtab.hh:77
gem5::X86ISA::addr
Bitfield< 3 > addr
Definition: types.hh:84
gem5::loader::SymbolTable::mask
SymbolTablePtr mask(Addr m) const
Generate a new table by a mask to the symbols of the current table.
Definition: symtab.hh:232

Generated on Sun Jul 30 2023 01:56:51 for gem5 by doxygen 1.8.17