symtab.cc revision 11793
14202Sbinkertn@umich.edu/*
24202Sbinkertn@umich.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan
34202Sbinkertn@umich.edu * All rights reserved.
44202Sbinkertn@umich.edu *
54202Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
64202Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are
74202Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright
84202Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer;
94202Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright
104202Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the
114202Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution;
124202Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its
134202Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from
144202Sbinkertn@umich.edu * this software without specific prior written permission.
154202Sbinkertn@umich.edu *
164202Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174202Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184202Sbinkertn@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
194202Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
204202Sbinkertn@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
214202Sbinkertn@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
224202Sbinkertn@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
234202Sbinkertn@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244202Sbinkertn@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254202Sbinkertn@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
264202Sbinkertn@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274202Sbinkertn@umich.edu *
284202Sbinkertn@umich.edu * Authors: Nathan Binkert
294202Sbinkertn@umich.edu */
304202Sbinkertn@umich.edu
314202Sbinkertn@umich.edu#include "base/loader/symtab.hh"
324202Sbinkertn@umich.edu
335628Sgblack@eecs.umich.edu#include <fstream>
344486Sbinkertn@umich.edu#include <iostream>
354486Sbinkertn@umich.edu#include <string>
364776Sgblack@eecs.umich.edu#include <vector>
374486Sbinkertn@umich.edu
384202Sbinkertn@umich.edu#include "base/misc.hh"
394202Sbinkertn@umich.edu#include "base/str.hh"
404202Sbinkertn@umich.edu#include "base/types.hh"
414202Sbinkertn@umich.edu#include "sim/serialize.hh"
424202Sbinkertn@umich.edu
435522Snate@binkert.orgusing namespace std;
446143Snate@binkert.org
455780Ssteve.reinhardt@amd.comSymbolTable *debugSymbolTable = NULL;
464202Sbinkertn@umich.edu
474202Sbinkertn@umich.eduvoid
484202Sbinkertn@umich.eduSymbolTable::clear()
494202Sbinkertn@umich.edu{
504202Sbinkertn@umich.edu    addrTable.clear();
514202Sbinkertn@umich.edu    symbolTable.clear();
524202Sbinkertn@umich.edu}
534202Sbinkertn@umich.edu
544202Sbinkertn@umich.edubool
554202Sbinkertn@umich.eduSymbolTable::insert(Addr address, string symbol)
564826Ssaidi@eecs.umich.edu{
574202Sbinkertn@umich.edu    if (symbol.empty())
585016Sgblack@eecs.umich.edu        return false;
594486Sbinkertn@umich.edu
604486Sbinkertn@umich.edu    if (!symbolTable.insert(make_pair(symbol, address)).second)
614202Sbinkertn@umich.edu        return false;
624202Sbinkertn@umich.edu
635192Ssaidi@eecs.umich.edu    // There can be multiple symbols for the same address, so always
645192Ssaidi@eecs.umich.edu    // update the addrTable multimap when we see a new symbol name.
655192Ssaidi@eecs.umich.edu    addrTable.insert(make_pair(address, symbol));
665192Ssaidi@eecs.umich.edu
675192Ssaidi@eecs.umich.edu    return true;
685192Ssaidi@eecs.umich.edu}
695192Ssaidi@eecs.umich.edu
705192Ssaidi@eecs.umich.edu
715192Ssaidi@eecs.umich.edubool
725192Ssaidi@eecs.umich.eduSymbolTable::load(const string &filename)
735192Ssaidi@eecs.umich.edu{
745192Ssaidi@eecs.umich.edu    string buffer;
755192Ssaidi@eecs.umich.edu    ifstream file(filename.c_str());
765192Ssaidi@eecs.umich.edu
775192Ssaidi@eecs.umich.edu    if (!file)
78        fatal("file error: Can't open symbol table file %s\n", filename);
79
80    while (!file.eof()) {
81        getline(file, buffer);
82        if (buffer.empty())
83            continue;
84
85        string::size_type idx = buffer.find(',');
86        if (idx == string::npos)
87            return false;
88
89        string address = buffer.substr(0, idx);
90        eat_white(address);
91        if (address.empty())
92            return false;
93
94        string symbol = buffer.substr(idx + 1);
95        eat_white(symbol);
96        if (symbol.empty())
97            return false;
98
99        Addr addr;
100        if (!to_number(address, addr))
101            return false;
102
103        if (!insert(addr, symbol))
104            return false;
105    }
106
107    file.close();
108
109    return true;
110}
111
112void
113SymbolTable::serialize(const string &base, CheckpointOut &cp) const
114{
115    paramOut(cp, base + ".size", addrTable.size());
116
117    int i = 0;
118    ATable::const_iterator p, end = addrTable.end();
119    for (p = addrTable.begin(); p != end; ++p) {
120        paramOut(cp, csprintf("%s.addr_%d", base, i), p->first);
121        paramOut(cp, csprintf("%s.symbol_%d", base, i), p->second);
122        ++i;
123    }
124}
125
126void
127SymbolTable::unserialize(const string &base, CheckpointIn &cp)
128{
129    clear();
130    int size;
131    paramIn(cp, base + ".size", size);
132    for (int i = 0; i < size; ++i) {
133        Addr addr;
134        std::string symbol;
135
136        paramIn(cp, csprintf("%s.addr_%d", base, i), addr);
137        paramIn(cp, csprintf("%s.symbol_%d", base, i), symbol);
138        insert(addr, symbol);
139    }
140}
141