symtab.cc revision 11793
12SN/A/*
21762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
292SN/A */
302SN/A
3111793Sbrandon.potter@amd.com#include "base/loader/symtab.hh"
3211793Sbrandon.potter@amd.com
338229Snate@binkert.org#include <fstream>
342SN/A#include <iostream>
352SN/A#include <string>
362SN/A#include <vector>
372SN/A
3856SN/A#include "base/misc.hh"
3956SN/A#include "base/str.hh"
406214Snate@binkert.org#include "base/types.hh"
411984SN/A#include "sim/serialize.hh"
422SN/A
432SN/Ausing namespace std;
442SN/A
451158SN/ASymbolTable *debugSymbolTable = NULL;
461158SN/A
471984SN/Avoid
481984SN/ASymbolTable::clear()
491984SN/A{
501984SN/A    addrTable.clear();
511984SN/A    symbolTable.clear();
521984SN/A}
531984SN/A
542SN/Abool
552SN/ASymbolTable::insert(Addr address, string symbol)
562SN/A{
571984SN/A    if (symbol.empty())
581984SN/A        return false;
591984SN/A
6011537Sandreas.sandberg@arm.com    if (!symbolTable.insert(make_pair(symbol, address)).second)
612SN/A        return false;
622SN/A
6311537Sandreas.sandberg@arm.com    // There can be multiple symbols for the same address, so always
6411537Sandreas.sandberg@arm.com    // update the addrTable multimap when we see a new symbol name.
6511537Sandreas.sandberg@arm.com    addrTable.insert(make_pair(address, symbol));
662SN/A
672SN/A    return true;
682SN/A}
692SN/A
702SN/A
712SN/Abool
722SN/ASymbolTable::load(const string &filename)
732SN/A{
742SN/A    string buffer;
752SN/A    ifstream file(filename.c_str());
762SN/A
771984SN/A    if (!file)
781984SN/A        fatal("file error: Can't open symbol table file %s\n", filename);
792SN/A
802SN/A    while (!file.eof()) {
812SN/A        getline(file, buffer);
822SN/A        if (buffer.empty())
832SN/A            continue;
842SN/A
856227Snate@binkert.org        string::size_type idx = buffer.find(',');
862SN/A        if (idx == string::npos)
872SN/A            return false;
882SN/A
892SN/A        string address = buffer.substr(0, idx);
902SN/A        eat_white(address);
912SN/A        if (address.empty())
922SN/A            return false;
932SN/A
942SN/A        string symbol = buffer.substr(idx + 1);
952SN/A        eat_white(symbol);
962SN/A        if (symbol.empty())
972SN/A            return false;
982SN/A
992SN/A        Addr addr;
1002SN/A        if (!to_number(address, addr))
1012SN/A            return false;
1022SN/A
1032SN/A        if (!insert(addr, symbol))
1042SN/A            return false;
1052SN/A    }
1062SN/A
1072SN/A    file.close();
1082SN/A
1092SN/A    return true;
1102SN/A}
1111984SN/A
1121984SN/Avoid
11310905Sandreas.sandberg@arm.comSymbolTable::serialize(const string &base, CheckpointOut &cp) const
1141984SN/A{
11510905Sandreas.sandberg@arm.com    paramOut(cp, base + ".size", addrTable.size());
1161984SN/A
1171984SN/A    int i = 0;
1181984SN/A    ATable::const_iterator p, end = addrTable.end();
1191984SN/A    for (p = addrTable.begin(); p != end; ++p) {
12010905Sandreas.sandberg@arm.com        paramOut(cp, csprintf("%s.addr_%d", base, i), p->first);
12110905Sandreas.sandberg@arm.com        paramOut(cp, csprintf("%s.symbol_%d", base, i), p->second);
1221984SN/A        ++i;
1231984SN/A    }
1241984SN/A}
1251984SN/A
1261984SN/Avoid
12710905Sandreas.sandberg@arm.comSymbolTable::unserialize(const string &base, CheckpointIn &cp)
1281984SN/A{
1291984SN/A    clear();
1301984SN/A    int size;
13110905Sandreas.sandberg@arm.com    paramIn(cp, base + ".size", size);
1321984SN/A    for (int i = 0; i < size; ++i) {
1331984SN/A        Addr addr;
1341984SN/A        std::string symbol;
1351984SN/A
13610905Sandreas.sandberg@arm.com        paramIn(cp, csprintf("%s.addr_%d", base, i), addr);
13710905Sandreas.sandberg@arm.com        paramIn(cp, csprintf("%s.symbol_%d", base, i), symbol);
1381984SN/A        insert(addr, symbol);
1391984SN/A    }
1401984SN/A}
141