symtab.cc revision 2
12SN/A/*
214045Snikos.nikoleris@arm.com * Copyright (c) 2003 The Regents of The University of Michigan
311932Ssascha.bischoff@arm.com * All rights reserved.
411932Ssascha.bischoff@arm.com *
511932Ssascha.bischoff@arm.com * Redistribution and use in source and binary forms, with or without
611932Ssascha.bischoff@arm.com * modification, are permitted provided that the following conditions are
711932Ssascha.bischoff@arm.com * met: redistributions of source code must retain the above copyright
811932Ssascha.bischoff@arm.com * notice, this list of conditions and the following disclaimer;
911932Ssascha.bischoff@arm.com * redistributions in binary form must reproduce the above copyright
1011932Ssascha.bischoff@arm.com * notice, this list of conditions and the following disclaimer in the
1111932Ssascha.bischoff@arm.com * documentation and/or other materials provided with the distribution;
1211932Ssascha.bischoff@arm.com * neither the name of the copyright holders nor the names of its
1311932Ssascha.bischoff@arm.com * contributors may be used to endorse or promote products derived from
141762SN/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.
272SN/A */
282SN/A
292SN/A#include <iostream>
302SN/A#include <fstream>
312SN/A#include <string>
322SN/A#include <vector>
332SN/A
342SN/A#include "host.hh"
352SN/A#include "misc.hh"
362SN/A#include "str.hh"
372SN/A#include "symtab.hh"
382SN/A
392665Ssaidi@eecs.umich.eduusing namespace std;
402665Ssaidi@eecs.umich.edu
412665Ssaidi@eecs.umich.edubool
4212226Sgiacomo.travaglini@arm.comSymbolTable::insert(Addr address, string symbol)
432SN/A{
442SN/A    if (!addrTable.insert(make_pair(address, symbol)).second)
451112SN/A        return false;
461112SN/A
472SN/A    if (!symbolTable.insert(make_pair(symbol, address)).second)
4811800Sbrandon.potter@amd.com        return false;
4912226Sgiacomo.travaglini@arm.com
5012226Sgiacomo.travaglini@arm.com    return true;
5112226Sgiacomo.travaglini@arm.com}
5212226Sgiacomo.travaglini@arm.com
5312226Sgiacomo.travaglini@arm.com
5412226Sgiacomo.travaglini@arm.combool
552SN/ASymbolTable::load(const string &filename)
562SN/A{
572SN/A    string buffer;
582SN/A    ifstream file(filename.c_str());
592SN/A
602SN/A    if (!file) {
612SN/A        cerr << "Can't open symbol table file " << filename << endl;
622SN/A        fatal("file error");
632SN/A    }
642SN/A
652SN/A    while (!file.eof()) {
662SN/A        getline(file, buffer);
672SN/A        if (buffer.empty())
682SN/A            continue;
692SN/A
702SN/A        int idx = buffer.find(',');
712SN/A        if (idx == string::npos)
722SN/A            return false;
732SN/A
742SN/A        string address = buffer.substr(0, idx);
752SN/A        eat_white(address);
762SN/A        if (address.empty())
772SN/A            return false;
782SN/A
794661Sksewell@umich.edu        string symbol = buffer.substr(idx + 1);
804661Sksewell@umich.edu        eat_white(symbol);
814661Sksewell@umich.edu        if (symbol.empty())
824661Sksewell@umich.edu            return false;
834661Sksewell@umich.edu
844661Sksewell@umich.edu        Addr addr;
854661Sksewell@umich.edu        if (!to_number(address, addr))
864661Sksewell@umich.edu            return false;
874661Sksewell@umich.edu
884661Sksewell@umich.edu        if (!insert(addr, symbol))
894661Sksewell@umich.edu            return false;
903814Ssaidi@eecs.umich.edu    }
913814Ssaidi@eecs.umich.edu
923814Ssaidi@eecs.umich.edu    file.close();
933814Ssaidi@eecs.umich.edu
943814Ssaidi@eecs.umich.edu    return true;
953814Ssaidi@eecs.umich.edu}
963814Ssaidi@eecs.umich.edu
973814Ssaidi@eecs.umich.edubool
983814Ssaidi@eecs.umich.eduSymbolTable::findSymbol(Addr address, string &symbol) const
993814Ssaidi@eecs.umich.edu{
1003814Ssaidi@eecs.umich.edu    ATable::const_iterator i = addrTable.find(address);
1014070Ssaidi@eecs.umich.edu    if (i == addrTable.end())
1024070Ssaidi@eecs.umich.edu        return false;
1034070Ssaidi@eecs.umich.edu
1044070Ssaidi@eecs.umich.edu    symbol = (*i).second;
1054070Ssaidi@eecs.umich.edu    return true;
1064070Ssaidi@eecs.umich.edu}
1073814Ssaidi@eecs.umich.edu
1082SN/Abool
1092SN/ASymbolTable::findAddress(const string &symbol, Addr &address) const
1102SN/A{
1112SN/A    STable::const_iterator i = symbolTable.find(symbol);
11210537Sandreas.hansson@arm.com    if (i == symbolTable.end())
1132SN/A        return false;
1142SN/A
1152SN/A    address = (*i).second;
1162SN/A    return true;
1172SN/A}
1182SN/A
1193422Sgblack@eecs.umich.edustring
1203422Sgblack@eecs.umich.eduSymbolTable::find(Addr addr) const
1213422Sgblack@eecs.umich.edu{
1223422Sgblack@eecs.umich.edu    string s;
1233422Sgblack@eecs.umich.edu    findSymbol(addr, s);
1243422Sgblack@eecs.umich.edu    return s;
1253422Sgblack@eecs.umich.edu}
1263422Sgblack@eecs.umich.edu
1274425Ssaidi@eecs.umich.eduAddr
1283422Sgblack@eecs.umich.eduSymbolTable::find(const string &symbol) const
1294425Ssaidi@eecs.umich.edu{
1303422Sgblack@eecs.umich.edu    Addr a = 0;
1313422Sgblack@eecs.umich.edu    findAddress(symbol, a);
1323422Sgblack@eecs.umich.edu    return a;
1334661Sksewell@umich.edu}
1344661Sksewell@umich.edu