symtab.cc revision 10905
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 318229Snate@binkert.org#include <fstream> 322SN/A#include <iostream> 332SN/A#include <string> 342SN/A#include <vector> 352SN/A 361984SN/A#include "base/loader/symtab.hh" 3756SN/A#include "base/misc.hh" 3856SN/A#include "base/str.hh" 396214Snate@binkert.org#include "base/types.hh" 401984SN/A#include "sim/serialize.hh" 412SN/A 422SN/Ausing namespace std; 432SN/A 441158SN/ASymbolTable *debugSymbolTable = NULL; 451158SN/A 461984SN/Avoid 471984SN/ASymbolTable::clear() 481984SN/A{ 491984SN/A addrTable.clear(); 501984SN/A symbolTable.clear(); 511984SN/A} 521984SN/A 532SN/Abool 542SN/ASymbolTable::insert(Addr address, string symbol) 552SN/A{ 561984SN/A if (symbol.empty()) 571984SN/A return false; 581984SN/A 592SN/A if (!addrTable.insert(make_pair(address, symbol)).second) 602SN/A return false; 612SN/A 622SN/A if (!symbolTable.insert(make_pair(symbol, address)).second) 632SN/A return false; 642SN/A 652SN/A return true; 662SN/A} 672SN/A 682SN/A 692SN/Abool 702SN/ASymbolTable::load(const string &filename) 712SN/A{ 722SN/A string buffer; 732SN/A ifstream file(filename.c_str()); 742SN/A 751984SN/A if (!file) 761984SN/A fatal("file error: Can't open symbol table file %s\n", filename); 772SN/A 782SN/A while (!file.eof()) { 792SN/A getline(file, buffer); 802SN/A if (buffer.empty()) 812SN/A continue; 822SN/A 836227Snate@binkert.org string::size_type idx = buffer.find(','); 842SN/A if (idx == string::npos) 852SN/A return false; 862SN/A 872SN/A string address = buffer.substr(0, idx); 882SN/A eat_white(address); 892SN/A if (address.empty()) 902SN/A return false; 912SN/A 922SN/A string symbol = buffer.substr(idx + 1); 932SN/A eat_white(symbol); 942SN/A if (symbol.empty()) 952SN/A return false; 962SN/A 972SN/A Addr addr; 982SN/A if (!to_number(address, addr)) 992SN/A return false; 1002SN/A 1012SN/A if (!insert(addr, symbol)) 1022SN/A return false; 1032SN/A } 1042SN/A 1052SN/A file.close(); 1062SN/A 1072SN/A return true; 1082SN/A} 1091984SN/A 1101984SN/Avoid 11110905Sandreas.sandberg@arm.comSymbolTable::serialize(const string &base, CheckpointOut &cp) const 1121984SN/A{ 11310905Sandreas.sandberg@arm.com paramOut(cp, base + ".size", addrTable.size()); 1141984SN/A 1151984SN/A int i = 0; 1161984SN/A ATable::const_iterator p, end = addrTable.end(); 1171984SN/A for (p = addrTable.begin(); p != end; ++p) { 11810905Sandreas.sandberg@arm.com paramOut(cp, csprintf("%s.addr_%d", base, i), p->first); 11910905Sandreas.sandberg@arm.com paramOut(cp, csprintf("%s.symbol_%d", base, i), p->second); 1201984SN/A ++i; 1211984SN/A } 1221984SN/A} 1231984SN/A 1241984SN/Avoid 12510905Sandreas.sandberg@arm.comSymbolTable::unserialize(const string &base, CheckpointIn &cp) 1261984SN/A{ 1271984SN/A clear(); 1281984SN/A int size; 12910905Sandreas.sandberg@arm.com paramIn(cp, base + ".size", size); 1301984SN/A for (int i = 0; i < size; ++i) { 1311984SN/A Addr addr; 1321984SN/A std::string symbol; 1331984SN/A 13410905Sandreas.sandberg@arm.com paramIn(cp, csprintf("%s.addr_%d", base, i), addr); 13510905Sandreas.sandberg@arm.com paramIn(cp, csprintf("%s.symbol_%d", base, i), symbol); 1361984SN/A insert(addr, symbol); 1371984SN/A } 1381984SN/A} 139