symtab.cc revision 2665:a124942bacb8
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 <iostream>
32#include <fstream>
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 "sim/host.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 (!addrTable.insert(make_pair(address, symbol)).second)
60        return false;
61
62    if (!symbolTable.insert(make_pair(symbol, address)).second)
63        return false;
64
65    return true;
66}
67
68
69bool
70SymbolTable::load(const string &filename)
71{
72    string buffer;
73    ifstream file(filename.c_str());
74
75    if (!file)
76        fatal("file error: Can't open symbol table file %s\n", filename);
77
78    while (!file.eof()) {
79        getline(file, buffer);
80        if (buffer.empty())
81            continue;
82
83        int idx = buffer.find(',');
84        if (idx == string::npos)
85            return false;
86
87        string address = buffer.substr(0, idx);
88        eat_white(address);
89        if (address.empty())
90            return false;
91
92        string symbol = buffer.substr(idx + 1);
93        eat_white(symbol);
94        if (symbol.empty())
95            return false;
96
97        Addr addr;
98        if (!to_number(address, addr))
99            return false;
100
101        if (!insert(addr, symbol))
102            return false;
103    }
104
105    file.close();
106
107    return true;
108}
109
110void
111SymbolTable::serialize(const string &base, ostream &os)
112{
113    paramOut(os, base + ".size", addrTable.size());
114
115    int i = 0;
116    ATable::const_iterator p, end = addrTable.end();
117    for (p = addrTable.begin(); p != end; ++p) {
118        paramOut(os, csprintf("%s.addr_%d", base, i), p->first);
119        paramOut(os, csprintf("%s.symbol_%d", base, i), p->second);
120        ++i;
121    }
122}
123
124void
125SymbolTable::unserialize(const string &base, Checkpoint *cp,
126                         const string &section)
127{
128    clear();
129    int size;
130    paramIn(cp, section, base + ".size", size);
131    for (int i = 0; i < size; ++i) {
132        Addr addr;
133        std::string symbol;
134
135        paramIn(cp, section, csprintf("%s.addr_%d", base, i), addr);
136        paramIn(cp, section, csprintf("%s.symbol_%d", base, i), symbol);
137        insert(addr, symbol);
138    }
139}
140