gem5 v24.0.0.0
Loading...
Searching...
No Matches
symtab.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Arm Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2021 Daniel R. Carvalho
15 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42#ifndef __BASE_LOADER_SYMTAB_HH__
43#define __BASE_LOADER_SYMTAB_HH__
44
45#include <functional>
46#include <iosfwd>
47#include <map>
48#include <memory>
49#include <string>
50#include <vector>
51
52#include "base/compiler.hh"
53#include "base/types.hh"
54#include "sim/serialize.hh"
55
56namespace gem5
57{
58
59namespace loader
60{
61
62class Symbol
63{
64 public:
65 enum class Binding
66 {
67 Global,
68 Local,
69 Weak
70 };
71
72 // The ELF64_ST_TYPE field of gelf's st_info
73 enum class SymbolType
74 {
75 NoType,
76 Object,
78 Section,
79 File,
80 Other
81 };
82
84 const std::string & name, const Addr addr, const size_t size)
86 _size(size), _sizeIsValid(true)
87 {}
88
90 const std::string & name, const Addr addr)
92 _size(0x0), _sizeIsValid(false)
93 {}
94
95 Symbol(const Symbol & other) = default;
96 Symbol & operator=(const Symbol & other) = default;
97
98 Binding binding() const {
99 return _binding;
100 }
101
102 SymbolType type() const {
103 return _type;
104 }
105
106 std::string name() const {
107 return _name;
108 }
109
110 void rename(const std::string & new_name) {
111 _name = new_name;
112 }
113
114 Addr address() const {
115 return _address;
116 }
117
118 void relocate(const Addr new_addr) {
119 _address = new_addr;
120 }
121
130 size_t sizeOrDefault(const size_t default_size) const {
131 return _sizeIsValid ? _size : default_size;
132 }
133
137 bool sizeIsValid() const {
138 return _sizeIsValid;
139 }
140
141 private:
144 std::string _name;
146 size_t _size;
148};
149
150
152{
153 public:
154 typedef std::shared_ptr<SymbolTable> SymbolTablePtr;
155
156 private:
160 typedef std::multimap<Addr, int> AddrMap;
162 typedef std::map<std::string, int> NameMap;
163
167
175 bool
176 upperBound(Addr addr, AddrMap::const_iterator &iter) const
177 {
178 // find first key *larger* than desired address
179 iter = addrMap.upper_bound(addr);
180
181 // if very first key is larger, we're out of luck
182 if (iter == addrMap.begin())
183 return false;
184
185 return true;
186 }
187
193 typedef std::function<void(SymbolTable &symtab,
194 const Symbol &symbol)> SymTabOp;
195
205 {
206 SymbolTablePtr symtab(new SymbolTable);
207 for (const auto &symbol: symbols)
208 op(*symtab, symbol);
209 return symtab;
210 }
211
216 typedef std::function<bool(const Symbol &symbol)> SymTabFilter;
217
228 {
229 SymTabOp apply_filter =
230 [filter](SymbolTable &symtab, const Symbol &symbol) {
231 if (filter(symbol)) {
232 symtab.insert(symbol);
233 }
234 };
235 return operate(apply_filter);
236 }
237
247 {
248 auto filt = [binding](const Symbol &symbol) {
249 return symbol.binding() == binding;
250 };
251 return filter(filt);
252 }
253
262 filterBySymbolType(const Symbol::SymbolType& symbol_type) const
263 {
264 auto filt = [symbol_type](const Symbol &symbol) {
265 return symbol.type() == symbol_type;
266 };
267 return filter(filt);
268 }
269
270 public:
271 typedef SymbolVector::iterator iterator;
272 typedef SymbolVector::const_iterator const_iterator;
273
275 const_iterator begin() const { return symbols.begin(); }
276
278 const_iterator end() const { return symbols.end(); }
279
281 void clear();
282
290 bool insert(const Symbol &symbol);
291
299 bool insert(const SymbolTable &other);
300
306 bool empty() const { return symbols.empty(); }
307
316 offset(Addr addr_offset) const
317 {
318 SymTabOp op =
319 [addr_offset](SymbolTable &symtab, const Symbol &symbol) {
320 symtab.insert(
321 Symbol(symbol.binding(), symbol.type(), symbol.name(),
322 symbol.address() + addr_offset));
323 };
324 return operate(op);
325 }
326
335 mask(Addr m) const
336 {
337 SymTabOp op = [m](SymbolTable &symtab, const Symbol &symbol) {
338 symtab.insert(
339 Symbol(symbol.binding(), symbol.type(), symbol.name(),
340 symbol.address() & m));
341 };
342 return operate(op);
343 }
344
353 rename(std::function<std::string (const std::string&)> func) const
354 {
355 SymTabOp op = [func](SymbolTable &symtab, const Symbol &symbol) {
356 Symbol sym = symbol;
357 sym.rename(func(sym.name()));
358 symtab.insert(sym);
359 };
360 return operate(op);
361 }
362
369 globals() const
370 {
372 }
373
380 locals() const
381 {
383 }
384
391 weaks() const
392 {
394 }
395
406
413 void serialize(const std::string &base, CheckpointOut &cp) const;
414
423 void unserialize(const std::string &base, CheckpointIn &cp,
425
435 find(Addr address) const
436 {
437 AddrMap::const_iterator i = addrMap.find(address);
438 if (i == addrMap.end())
439 return end();
440
441 // There are potentially multiple symbols that map to the same
442 // address. For simplicity, just return the first one.
443 return symbols.begin() + i->second;
444 }
445
454 find(const std::string &name) const
455 {
456 NameMap::const_iterator i = nameMap.find(name);
457 if (i == nameMap.end())
458 return end();
459
460 return symbols.begin() + i->second;
461 }
462
474 findNearest(Addr addr, Addr &next_addr) const
475 {
476 AddrMap::const_iterator i = addrMap.end();
477 if (!upperBound(addr, i))
478 return end();
479
480 // If there is no next address, make it 0 since 0 is not larger than
481 // any other address, so it is clear that next is not valid
482 if (i == addrMap.end()) {
483 next_addr = 0;
484 } else {
485 next_addr = i->first;
486 }
487 --i;
488 return symbols.begin() + i->second;
489 }
490
497 {
498 AddrMap::const_iterator i = addrMap.end();
499 if (!upperBound(addr, i))
500 return end();
501
502 --i;
503 return symbols.begin() + i->second;
504 }
505};
506
513extern SymbolTable debugSymbolTable;
514
515} // namespace loader
516} // namespace gem5
517
518#endif // __BASE_LOADER_SYMTAB_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
SymbolTablePtr functionSymbols() const
Generates a new symbol table containing only function symbols.
Definition symtab.hh:402
SymbolTablePtr locals() const
Generates a new symbol table containing only local symbols.
Definition symtab.hh:380
SymbolVector::const_iterator const_iterator
Definition symtab.hh:272
SymbolTablePtr weaks() const
Generates a new symbol table containing only weak symbols.
Definition symtab.hh:391
bool empty() const
Verifies whether the table is empty.
Definition symtab.hh:306
SymbolTablePtr globals() const
Generates a new symbol table containing only global symbols.
Definition symtab.hh:369
std::shared_ptr< SymbolTable > SymbolTablePtr
Definition symtab.hh:154
SymbolTablePtr rename(std::function< std::string(const std::string &)> func) const
Modify the symbols' name with a given transform function.
Definition symtab.hh:353
std::map< std::string, int > NameMap
Map a symbol name to an index into the symbol vector.
Definition symtab.hh:162
SymbolTablePtr filter(SymTabFilter filter) const
Applies a filter to the symbols of the table to generate a new table.
Definition symtab.hh:227
void clear()
Clears the table.
Definition symtab.cc:58
std::multimap< Addr, int > AddrMap
Map addresses to an index into the symbol vector.
Definition symtab.hh:160
const_iterator end() const
Definition symtab.hh:278
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:127
SymbolTablePtr operate(SymTabOp op) const
Create a derived symbol table by applying an operation on the symbols of the current table.
Definition symtab.hh:204
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:246
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:194
const_iterator findNearest(Addr addr) const
Overload for findNearestSymbol() for callers who don't care about nextaddr.
Definition symtab.hh:496
const_iterator begin() const
Definition symtab.hh:275
SymbolVector::iterator iterator
Definition symtab.hh:271
std::vector< Symbol > SymbolVector
Vector containing all the symbols in the table.
Definition symtab.hh:158
SymbolTablePtr filterBySymbolType(const Symbol::SymbolType &symbol_type) const
Generate a new table by applying a filter that only accepts the symbols whose type matches the given ...
Definition symtab.hh:262
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:474
SymbolTablePtr mask(Addr m) const
Generate a new table by a mask to the symbols of the current table.
Definition symtab.hh:335
bool upperBound(Addr addr, AddrMap::const_iterator &iter) const
Get the first address larger than the given address, if any.
Definition symtab.hh:176
const_iterator find(const std::string &name) const
Search for a symbol by its name.
Definition symtab.hh:454
SymbolTablePtr offset(Addr addr_offset) const
Generate a new table by applying an offset to the symbols of the current table.
Definition symtab.hh:316
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:216
void serialize(const std::string &base, CheckpointOut &cp) const
Serialize the table's contents.
Definition symtab.cc:107
bool insert(const Symbol &symbol)
Insert a new symbol in the table if it does not already exist.
Definition symtab.cc:66
const_iterator find(Addr address) const
Search for a symbol by its address.
Definition symtab.hh:435
SymbolType type() const
Definition symtab.hh:102
Binding binding() const
Definition symtab.hh:98
Symbol & operator=(const Symbol &other)=default
std::string name() const
Definition symtab.hh:106
bool sizeIsValid() const
Return whether the Symbol size is valid or not.
Definition symtab.hh:137
size_t sizeOrDefault(const size_t default_size) const
Return the Symbol size if it is valid, otherwise return the default value supplied.
Definition symtab.hh:130
Symbol(const Binding binding, const SymbolType type, const std::string &name, const Addr addr, const size_t size)
Definition symtab.hh:83
Symbol(const Symbol &other)=default
Addr address() const
Definition symtab.hh:114
void rename(const std::string &new_name)
Definition symtab.hh:110
SymbolType _type
Definition symtab.hh:143
void relocate(const Addr new_addr)
Definition symtab.hh:118
std::string _name
Definition symtab.hh:144
Symbol(const Binding binding, const SymbolType type, const std::string &name, const Addr addr)
Definition symtab.hh:89
STL vector class.
Definition stl.hh:37
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 0 > m
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:55
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
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
const std::string & name()
Definition trace.cc:48

Generated on Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0