2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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.
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.
29 #ifndef __BASE_LOADER_SYMTAB_HH__
30 #define __BASE_LOADER_SYMTAB_HH__
39 #include "base/types.hh"
40 #include "sim/serialize.hh"
61 typedef std::shared_ptr<SymbolTable> SymbolTablePtr;
64 typedef std::vector<Symbol> SymbolVector;
65 // Map addresses to an index into the symbol vector.
66 typedef std::multimap<Addr, int> AddrMap;
67 // Map a symbol name to an index into the symbol vector.
68 typedef std::map<std::string, int> NameMap;
75 upperBound(Addr addr, AddrMap::const_iterator &iter) const
77 // find first key *larger* than desired address
78 iter = addrMap.upper_bound(addr);
80 // if very first key is larger, we're out of luck
81 if (iter == addrMap.begin())
87 typedef std::function<void(SymbolTable &symtab,
88 const Symbol &symbol)> SymTabOp;
90 operate(SymTabOp op) const
92 SymbolTablePtr symtab(new SymbolTable);
93 for (const auto &symbol: symbols)
98 typedef std::function<bool(const Symbol &symbol)> SymTabFilter;
100 filter(SymTabFilter filter) const
102 SymTabOp apply_filter =
103 [filter](SymbolTable &symtab, const Symbol &symbol) {
104 if (filter(symbol)) {
105 symtab.insert(symbol);
108 return operate(apply_filter);
112 filterByBinding(Symbol::Binding binding) const
114 auto filt = [binding](const Symbol &symbol) {
115 return symbol.binding == binding;
121 typedef SymbolVector::iterator iterator;
122 typedef SymbolVector::const_iterator const_iterator;
124 const_iterator begin() const { return symbols.begin(); }
125 const_iterator end() const { return symbols.end(); }
128 // Insert either a single symbol or the contents of an entire symbol table
130 bool insert(const Symbol &symbol);
131 bool insert(const SymbolTable &other);
132 bool empty() const { return symbols.empty(); }
135 offset(Addr by) const
137 SymTabOp op = [by](SymbolTable &symtab, const Symbol &symbol) {
148 SymTabOp op = [m](SymbolTable &symtab, const Symbol &symbol) {
159 return filterByBinding(Symbol::Binding::Global);
165 return filterByBinding(Symbol::Binding::Local);
171 return filterByBinding(Symbol::Binding::Weak);
174 void serialize(const std::string &base, CheckpointOut &cp) const;
175 void unserialize(const std::string &base, CheckpointIn &cp,
176 Symbol::Binding default_binding=Symbol::Binding::Global);
179 find(Addr address) const
181 AddrMap::const_iterator i = addrMap.find(address);
182 if (i == addrMap.end())
185 // There are potentially multiple symbols that map to the same
186 // address. For simplicity, just return the first one.
187 return symbols.begin() + i->second;
191 find(const std::string &name) const
193 NameMap::const_iterator i = nameMap.find(name);
194 if (i == nameMap.end())
197 return symbols.begin() + i->second;
200 /// Find the nearest symbol equal to or less than the supplied
201 /// address (e.g., the label for the enclosing function).
202 /// @param addr The address to look up.
203 /// @param nextaddr Address of following symbol (for
204 /// determining valid range of symbol).
205 /// @retval A const_iterator which points to the symbol if found, or end.
207 findNearest(Addr addr, Addr &nextaddr) const
209 AddrMap::const_iterator i = addrMap.end();
210 if (!upperBound(addr, i))
215 return symbols.begin() + i->second;
218 /// Overload for findNearestSymbol() for callers who don't care
221 findNearest(Addr addr) const
223 AddrMap::const_iterator i = addrMap.end();
224 if (!upperBound(addr, i))
228 return symbols.begin() + i->second;
232 /// Global unified debugging symbol table (for target). Conceptually
233 /// there should be one of these per System object for full system,
234 /// and per Process object for non-full-system, but so far one big
235 /// global one has worked well enough.
236 extern SymbolTable debugSymbolTable;
238 } // namespace Loader
240 #endif // __BASE_LOADER_SYMTAB_HH__