symtab.cc revision 11537:93e2bd032c3b
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 * Authors: Nathan Binkert
29 */
30
31#include <fstream>
32#include <iostream>
33#include <string>
34#include <vector>
35
36#include "base/loader/symtab.hh"
37#include "base/misc.hh"
38#include "base/str.hh"
39#include "base/types.hh"
40#include "sim/serialize.hh"
41
42using namespace std;
43
44SymbolTable *debugSymbolTable = NULL;
45
46void
47SymbolTable::clear()
48{
49    addrTable.clear();
50    symbolTable.clear();
51}
52
53bool
54SymbolTable::insert(Addr address, string symbol)
55{
56    if (symbol.empty())
57        return false;
58
59    if (!symbolTable.insert(make_pair(symbol, address)).second)
60        return false;
61
62    // There can be multiple symbols for the same address, so always
63    // update the addrTable multimap when we see a new symbol name.
64    addrTable.insert(make_pair(address, symbol));
65
66    return true;
67}
68
69
70bool
71SymbolTable::load(const string &filename)
72{
73    string buffer;
74    ifstream file(filename.c_str());
75
76    if (!file)
77        fatal("file error: Can't open symbol table file %s\n", filename);
78
79    while (!file.eof()) {
80        getline(file, buffer);
81        if (buffer.empty())
82            continue;
83
84        string::size_type idx = buffer.find(',');
85        if (idx == string::npos)
86            return false;
87
88        string address = buffer.substr(0, idx);
89        eat_white(address);
90        if (address.empty())
91            return false;
92
93        string symbol = buffer.substr(idx + 1);
94        eat_white(symbol);
95        if (symbol.empty())
96            return false;
97
98        Addr addr;
99        if (!to_number(address, addr))
100            return false;
101
102        if (!insert(addr, symbol))
103            return false;
104    }
105
106    file.close();
107
108    return true;
109}
110
111void
112SymbolTable::serialize(const string &base, CheckpointOut &cp) const
113{
114    paramOut(cp, base + ".size", addrTable.size());
115
116    int i = 0;
117    ATable::const_iterator p, end = addrTable.end();
118    for (p = addrTable.begin(); p != end; ++p) {
119        paramOut(cp, csprintf("%s.addr_%d", base, i), p->first);
120        paramOut(cp, csprintf("%s.symbol_%d", base, i), p->second);
121        ++i;
122    }
123}
124
125void
126SymbolTable::unserialize(const string &base, CheckpointIn &cp)
127{
128    clear();
129    int size;
130    paramIn(cp, base + ".size", size);
131    for (int i = 0; i < size; ++i) {
132        Addr addr;
133        std::string symbol;
134
135        paramIn(cp, csprintf("%s.addr_%d", base, i), addr);
136        paramIn(cp, csprintf("%s.symbol_%d", base, i), symbol);
137        insert(addr, symbol);
138    }
139}
140