symtab.cc revision 56
1/* 2 * Copyright (c) 2003 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 "sim/host.hh" 35#include "base/misc.hh" 36#include "base/str.hh" 37#include "base/loader/symtab.hh" 38 39using namespace std; 40 41bool 42SymbolTable::insert(Addr address, string symbol) 43{ 44 if (!addrTable.insert(make_pair(address, symbol)).second) 45 return false; 46 47 if (!symbolTable.insert(make_pair(symbol, address)).second) 48 return false; 49 50 return true; 51} 52 53 54bool 55SymbolTable::load(const string &filename) 56{ 57 string buffer; 58 ifstream file(filename.c_str()); 59 60 if (!file) { 61 cerr << "Can't open symbol table file " << filename << endl; 62 fatal("file error"); 63 } 64 65 while (!file.eof()) { 66 getline(file, buffer); 67 if (buffer.empty()) 68 continue; 69 70 int idx = buffer.find(','); 71 if (idx == string::npos) 72 return false; 73 74 string address = buffer.substr(0, idx); 75 eat_white(address); 76 if (address.empty()) 77 return false; 78 79 string symbol = buffer.substr(idx + 1); 80 eat_white(symbol); 81 if (symbol.empty()) 82 return false; 83 84 Addr addr; 85 if (!to_number(address, addr)) 86 return false; 87 88 if (!insert(addr, symbol)) 89 return false; 90 } 91 92 file.close(); 93 94 return true; 95} 96 97bool 98SymbolTable::findSymbol(Addr address, string &symbol) const 99{ 100 ATable::const_iterator i = addrTable.find(address); 101 if (i == addrTable.end()) 102 return false; 103 104 symbol = (*i).second; 105 return true; 106} 107 108bool 109SymbolTable::findAddress(const string &symbol, Addr &address) const 110{ 111 STable::const_iterator i = symbolTable.find(symbol); 112 if (i == symbolTable.end()) 113 return false; 114 115 address = (*i).second; 116 return true; 117} 118 119string 120SymbolTable::find(Addr addr) const 121{ 122 string s; 123 findSymbol(addr, s); 124 return s; 125} 126 127Addr 128SymbolTable::find(const string &symbol) const 129{ 130 Addr a = 0; 131 findAddress(symbol, a); 132 return a; 133} 134