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