symtab.cc revision 56
12810SN/A/*
212501Snikos.nikoleris@arm.com * Copyright (c) 2003 The Regents of The University of Michigan
39663Suri.wiener@arm.com * All rights reserved.
49663Suri.wiener@arm.com *
59663Suri.wiener@arm.com * Redistribution and use in source and binary forms, with or without
69663Suri.wiener@arm.com * modification, are permitted provided that the following conditions are
79663Suri.wiener@arm.com * met: redistributions of source code must retain the above copyright
89663Suri.wiener@arm.com * notice, this list of conditions and the following disclaimer;
99663Suri.wiener@arm.com * redistributions in binary form must reproduce the above copyright
109663Suri.wiener@arm.com * notice, this list of conditions and the following disclaimer in the
119663Suri.wiener@arm.com * documentation and/or other materials provided with the distribution;
129663Suri.wiener@arm.com * neither the name of the copyright holders nor the names of its
139663Suri.wiener@arm.com * contributors may be used to endorse or promote products derived from
142810SN/A * this software without specific prior written permission.
157636Ssteve.reinhardt@amd.com *
162810SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172810SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182810SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192810SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202810SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212810SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222810SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232810SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242810SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252810SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262810SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272810SN/A */
282810SN/A
292810SN/A#include <iostream>
302810SN/A#include <fstream>
312810SN/A#include <string>
322810SN/A#include <vector>
332810SN/A
342810SN/A#include "sim/host.hh"
352810SN/A#include "base/misc.hh"
362810SN/A#include "base/str.hh"
372810SN/A#include "base/loader/symtab.hh"
382810SN/A
392810SN/Ausing namespace std;
402810SN/A
412810SN/Abool
422810SN/ASymbolTable::insert(Addr address, string symbol)
432810SN/A{
442810SN/A    if (!addrTable.insert(make_pair(address, symbol)).second)
452810SN/A        return false;
462810SN/A
472810SN/A    if (!symbolTable.insert(make_pair(symbol, address)).second)
482810SN/A        return false;
492810SN/A
5011486Snikos.nikoleris@arm.com    return true;
5111486Snikos.nikoleris@arm.com}
526216Snate@binkert.org
532810SN/A
542810SN/Abool
5512334Sgabeblack@google.comSymbolTable::load(const string &filename)
5612727Snikos.nikoleris@arm.com{
576216Snate@binkert.org    string buffer;
588232Snate@binkert.org    ifstream file(filename.c_str());
5912727Snikos.nikoleris@arm.com
6012727Snikos.nikoleris@arm.com    if (!file) {
616216Snate@binkert.org        cerr << "Can't open symbol table file " << filename << endl;
622810SN/A        fatal("file error");
6311375Sandreas.hansson@arm.com    }
6411284Sandreas.hansson@arm.com
6510503SCurtis.Dunham@arm.com    while (!file.eof()) {
6611741Snikos.nikoleris@arm.com        getline(file, buffer);
672810SN/A        if (buffer.empty())
682810SN/A            continue;
692810SN/A
704903SN/A        int idx = buffer.find(',');
7112715Snikos.nikoleris@arm.com        if (idx == string::npos)
7212715Snikos.nikoleris@arm.com            return false;
734903SN/A
744903SN/A        string address = buffer.substr(0, idx);
754903SN/A        eat_white(address);
7611740Snikos.nikoleris@arm.com        if (address.empty())
7711741Snikos.nikoleris@arm.com            return false;
7811741Snikos.nikoleris@arm.com
794903SN/A        string symbol = buffer.substr(idx + 1);
805875Ssteve.reinhardt@amd.com        eat_white(symbol);
8111284Sandreas.hansson@arm.com        if (symbol.empty())
8211284Sandreas.hansson@arm.com            return false;
834903SN/A
844903SN/A        Addr addr;
857669Ssteve.reinhardt@amd.com        if (!to_number(address, addr))
867669Ssteve.reinhardt@amd.com            return false;
877669Ssteve.reinhardt@amd.com
887669Ssteve.reinhardt@amd.com        if (!insert(addr, symbol))
894903SN/A            return false;
904903SN/A    }
9111741Snikos.nikoleris@arm.com
9211741Snikos.nikoleris@arm.com    file.close();
9311741Snikos.nikoleris@arm.com
9411741Snikos.nikoleris@arm.com    return true;
9512715Snikos.nikoleris@arm.com}
9612715Snikos.nikoleris@arm.com
9712715Snikos.nikoleris@arm.combool
9812715Snikos.nikoleris@arm.comSymbolTable::findSymbol(Addr address, string &symbol) const
995318SN/A{
10011740Snikos.nikoleris@arm.com    ATable::const_iterator i = addrTable.find(address);
1014908SN/A    if (i == addrTable.end())
10211740Snikos.nikoleris@arm.com        return false;
10311740Snikos.nikoleris@arm.com
10411740Snikos.nikoleris@arm.com    symbol = (*i).second;
10511740Snikos.nikoleris@arm.com    return true;
10611740Snikos.nikoleris@arm.com}
10711741Snikos.nikoleris@arm.com
10811740Snikos.nikoleris@arm.combool
10911740Snikos.nikoleris@arm.comSymbolTable::findAddress(const string &symbol, Addr &address) const
11011740Snikos.nikoleris@arm.com{
11111740Snikos.nikoleris@arm.com    STable::const_iterator i = symbolTable.find(symbol);
11211740Snikos.nikoleris@arm.com    if (i == symbolTable.end())
11311741Snikos.nikoleris@arm.com        return false;
11411741Snikos.nikoleris@arm.com
11511740Snikos.nikoleris@arm.com    address = (*i).second;
11611741Snikos.nikoleris@arm.com    return true;
1175318SN/A}
1189543Ssascha.bischoff@arm.com
1199543Ssascha.bischoff@arm.comstring
1209543Ssascha.bischoff@arm.comSymbolTable::find(Addr addr) const
1219543Ssascha.bischoff@arm.com{
12211484Snikos.nikoleris@arm.com    string s;
1234908SN/A    findSymbol(addr, s);
1244908SN/A    return s;
12511083Sandreas.hansson@arm.com}
12611083Sandreas.hansson@arm.com
12711083Sandreas.hansson@arm.comAddr
1284908SN/ASymbolTable::find(const string &symbol) const
1294903SN/A{
1304903SN/A    Addr a = 0;
13111741Snikos.nikoleris@arm.com    findAddress(symbol, a);
1324903SN/A    return a;
1334903SN/A}
1344903SN/A