Files
gem5/src/base/loader/symtab.hh
Gabe Black c5b2b8e19f arch,base,cpu,kern,sim: Encapsulate symbols in a class.
The SymbolTable class had been tracking symbols as two independent
pieces, a name and an address, and acted as a way to translate between
them. Symbols can be more complex than that, and so this change
encapsulates the information associated with a symbol in a new class.

As a step towards simplifying the API for reading symbols from a
binary, this change also adds a "binding" field to that class so that
global, local and weak symbols can all go in the same table and be
differentiated later as needed. That should unify the current API
which has a method for each symbol type.

While the innards of SymbolTable were being reworked, this change
also makes that class more STL like by adding iterators, and begin
and end methods. These iterate over a new vector which holds all the
symbols. The address and name keyed maps now hold indexes into that
vector instead of the other half of the symbol.

Change-Id: I8084f86fd737f697ec041bac86a635a315fd1194
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/24784
Reviewed-by: Giacomo Travaglini <giacomo.travaglini@arm.com>
Maintainer: Gabe Black <gabeblack@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
2020-05-19 22:32:21 +00:00

164 lines
5.1 KiB
C++

/*
* Copyright (c) 2002-2005 The Regents of The University of Michigan
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer;
* redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution;
* neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __SYMTAB_HH__
#define __SYMTAB_HH__
#include <iosfwd>
#include <map>
#include <string>
#include <vector>
#include "base/trace.hh"
#include "base/types.hh"
#include "sim/serialize.hh"
namespace Loader
{
struct Symbol
{
enum class Binding {
Global,
Local,
Weak
};
Binding binding;
std::string name;
Addr address;
};
class SymbolTable
{
private:
typedef std::vector<Symbol> SymbolVector;
// Map addresses to an index into the symbol vector.
typedef std::multimap<Addr, int> AddrMap;
// Map a symbol name to an index into the symbol vector.
typedef std::map<std::string, int> NameMap;
SymbolVector symbols;
AddrMap addrMap;
NameMap nameMap;
bool
upperBound(Addr addr, AddrMap::const_iterator &iter) const
{
// find first key *larger* than desired address
iter = addrMap.upper_bound(addr);
// if very first key is larger, we're out of luck
if (iter == addrMap.begin())
return false;
return true;
}
public:
typedef SymbolVector::iterator iterator;
typedef SymbolVector::const_iterator const_iterator;
const_iterator begin() const { return symbols.begin(); }
const_iterator end() const { return symbols.end(); }
void clear();
// Insert either a single symbol or the contents of an entire symbol table
// into this one.
bool insert(const Symbol &symbol);
bool insert(const SymbolTable &other);
bool load(const std::string &file);
void serialize(const std::string &base, CheckpointOut &cp) const;
void unserialize(const std::string &base, CheckpointIn &cp,
Symbol::Binding default_binding=Symbol::Binding::Global);
const_iterator
find(Addr address) const
{
AddrMap::const_iterator i = addrMap.find(address);
if (i == addrMap.end())
return end();
// There are potentially multiple symbols that map to the same
// address. For simplicity, just return the first one.
return symbols.begin() + i->second;
}
const_iterator
find(const std::string &name) const
{
NameMap::const_iterator i = nameMap.find(name);
if (i == nameMap.end())
return end();
return symbols.begin() + i->second;
}
/// Find the nearest symbol equal to or less than the supplied
/// address (e.g., the label for the enclosing function).
/// @param addr The address to look up.
/// @param nextaddr Address of following symbol (for
/// determining valid range of symbol).
/// @retval A const_iterator which points to the symbol if found, or end.
const_iterator
findNearest(Addr addr, Addr &nextaddr) const
{
AddrMap::const_iterator i = addrMap.end();
if (!upperBound(addr, i))
return end();
nextaddr = i->first;
--i;
return symbols.begin() + i->second;
}
/// Overload for findNearestSymbol() for callers who don't care
/// about nextaddr.
const_iterator
findNearest(Addr addr) const
{
AddrMap::const_iterator i = addrMap.end();
if (!upperBound(addr, i))
return end();
--i;
return symbols.begin() + i->second;
}
};
/// Global unified debugging symbol table (for target). Conceptually
/// there should be one of these per System object for full system,
/// and per Process object for non-full-system, but so far one big
/// global one has worked well enough.
extern SymbolTable *debugSymbolTable;
} // namespace Loader
#endif // __SYMTAB_HH__