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