symtab.hh revision 1912
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 */
28
29#ifndef __SYMTAB_HH__
30#define __SYMTAB_HH__
31
32#include <map>
33#include "targetarch/isa_traits.hh"	// for Addr
34
35class SymbolTable
36{
37  public:
38    typedef std::map<Addr, std::string> ATable;
39    typedef std::map<std::string, Addr> STable;
40
41  private:
42    ATable addrTable;
43    STable symbolTable;
44
45  private:
46    bool
47    upperBound(Addr addr, ATable::const_iterator &iter) const
48    {
49        // find first key *larger* than desired address
50        iter = addrTable.upper_bound(addr);
51
52        // if very first key is larger, we're out of luck
53        if (iter == addrTable.begin())
54            return false;
55
56        return true;
57    }
58
59  public:
60    SymbolTable() {}
61    SymbolTable(const std::string &file) { load(file); }
62    ~SymbolTable() {}
63
64    bool insert(Addr address, std::string symbol);
65    bool load(const std::string &file);
66
67    const ATable &getAddrTable() const { return addrTable; }
68    const STable &getSymbolTable() const { return symbolTable; }
69
70  public:
71    bool
72    findSymbol(Addr address, std::string &symbol) const
73    {
74        ATable::const_iterator i = addrTable.find(address);
75        if (i == addrTable.end())
76            return false;
77
78        symbol = (*i).second;
79        return true;
80    }
81
82    bool
83    findAddress(const std::string &symbol, Addr &address) const
84    {
85        STable::const_iterator i = symbolTable.find(symbol);
86        if (i == symbolTable.end())
87            return false;
88
89        address = (*i).second;
90        return true;
91    }
92
93    /// Find the nearest symbol equal to or less than the supplied
94    /// address (e.g., the label for the enclosing function).
95    /// @param address The address to look up.
96    /// @param symbol  Return reference for symbol string.
97    /// @param sym_address Return reference for symbol address.
98    /// @param next_sym_address Address of following symbol (for
99    /// determining valid range of symbol).
100    /// @retval True if a symbol was found.
101    bool
102    findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr,
103                      Addr &nextaddr) const
104    {
105        ATable::const_iterator i;
106        if (!upperBound(addr, i))
107            return false;
108
109        nextaddr = i->first;
110        --i;
111        symaddr = i->first;
112        symbol = i->second;
113        return true;
114    }
115
116    /// Overload for findNearestSymbol() for callers who don't care
117    /// about next_sym_address.
118    bool
119    findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr) const
120    {
121        ATable::const_iterator i;
122        if (!upperBound(addr, i))
123            return false;
124
125        --i;
126        symaddr = i->first;
127        symbol = i->second;
128        return true;
129    }
130
131
132    bool
133    findNearestAddr(Addr addr, Addr &symaddr, Addr &nextaddr) const
134    {
135        ATable::const_iterator i;
136        if (!upperBound(addr, i))
137            return false;
138
139        nextaddr = i->first;
140        --i;
141        symaddr = i->first;
142        return true;
143    }
144
145    bool
146    findNearestAddr(Addr addr, Addr &symaddr) const
147    {
148        ATable::const_iterator i;
149        if (!upperBound(addr, i))
150            return false;
151
152        --i;
153        symaddr = i->first;
154        return true;
155    }
156};
157
158/// Global unified debugging symbol table (for target).  Conceptually
159/// there should be one of these per System object for full system,
160/// and per Process object for non-full-system, but so far one big
161/// global one has worked well enough.
162extern SymbolTable *debugSymbolTable;
163
164#endif // __SYMTAB_HH__
165