symtab.hh revision 1912
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 292SN/A#ifndef __SYMTAB_HH__ 302SN/A#define __SYMTAB_HH__ 311464SN/A 321464SN/A#include <map> 332SN/A#include "targetarch/isa_traits.hh" // for Addr 342SN/A 352SN/Aclass SymbolTable 362SN/A{ 372972Sgblack@eecs.umich.edu public: 388229Snate@binkert.org typedef std::map<Addr, std::string> ATable; 397720Sgblack@eecs.umich.edu typedef std::map<std::string, Addr> STable; 4056SN/A 412439SN/A private: 4256SN/A ATable addrTable; 436216Snate@binkert.org STable symbolTable; 448229Snate@binkert.org 452410SN/A private: 467878Sgblack@eecs.umich.edu bool 472SN/A upperBound(Addr addr, ATable::const_iterator &iter) const 482SN/A { 491061SN/A // find first key *larger* than desired address 502296SN/A iter = addrTable.upper_bound(addr); 512296SN/A 522680Sktlim@umich.edu // if very first key is larger, we're out of luck 53705SN/A if (iter == addrTable.begin()) 542623SN/A return false; 551464SN/A 565597Sgblack@eecs.umich.edu return true; 575597Sgblack@eecs.umich.edu } 585597Sgblack@eecs.umich.edu 595865Sksewell@umich.edu public: 605865Sksewell@umich.edu SymbolTable() {} 612296SN/A SymbolTable(const std::string &file) { load(file); } 622312SN/A ~SymbolTable() {} 63716SN/A 642623SN/A bool insert(Addr address, std::string symbol); 652623SN/A bool load(const std::string &file); 661128SN/A 672SN/A const ATable &getAddrTable() const { return addrTable; } 684572Sacolyte@umich.edu const STable &getSymbolTable() const { return symbolTable; } 692SN/A 702SN/A public: 712SN/A bool 722SN/A findSymbol(Addr address, std::string &symbol) const 732SN/A { 742SN/A ATable::const_iterator i = addrTable.find(address); 752SN/A if (i == addrTable.end()) 762SN/A return false; 772SN/A 782SN/A symbol = (*i).second; 792SN/A return true; 802SN/A } 812SN/A 822SN/A bool 832SN/A findAddress(const std::string &symbol, Addr &address) const 847619Sgblack@eecs.umich.edu { 852SN/A STable::const_iterator i = symbolTable.find(symbol); 862SN/A if (i == symbolTable.end()) 872SN/A return false; 882SN/A 892SN/A address = (*i).second; 902SN/A return true; 912SN/A } 922SN/A 932SN/A /// Find the nearest symbol equal to or less than the supplied 942SN/A /// address (e.g., the label for the enclosing function). 952SN/A /// @param address The address to look up. 96753SN/A /// @param symbol Return reference for symbol string. 972SN/A /// @param sym_address Return reference for symbol address. 982SN/A /// @param next_sym_address Address of following symbol (for 992SN/A /// determining valid range of symbol). 100512SN/A /// @retval True if a symbol was found. 101512SN/A bool 102512SN/A findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr, 103512SN/A Addr &nextaddr) const 104512SN/A { 105512SN/A ATable::const_iterator i; 1062SN/A if (!upperBound(addr, i)) 1072SN/A return false; 1085543Ssaidi@eecs.umich.edu 1092SN/A nextaddr = i->first; 1105543Ssaidi@eecs.umich.edu --i; 1115543Ssaidi@eecs.umich.edu symaddr = i->first; 1122SN/A symbol = i->second; 1135543Ssaidi@eecs.umich.edu return true; 1145543Ssaidi@eecs.umich.edu } 1155543Ssaidi@eecs.umich.edu 1162336SN/A /// Overload for findNearestSymbol() for callers who don't care 1175222Sksewell@umich.edu /// about next_sym_address. 1185543Ssaidi@eecs.umich.edu bool 1195543Ssaidi@eecs.umich.edu findNearestSymbol(Addr addr, std::string &symbol, Addr &symaddr) const 1202SN/A { 1215543Ssaidi@eecs.umich.edu ATable::const_iterator i; 1225543Ssaidi@eecs.umich.edu if (!upperBound(addr, i)) 1235543Ssaidi@eecs.umich.edu return false; 1245543Ssaidi@eecs.umich.edu 1255543Ssaidi@eecs.umich.edu --i; 1265543Ssaidi@eecs.umich.edu symaddr = i->first; 1275543Ssaidi@eecs.umich.edu symbol = i->second; 1282SN/A return true; 1292135SN/A } 1302135SN/A 1315543Ssaidi@eecs.umich.edu 132512SN/A bool 1335543Ssaidi@eecs.umich.edu findNearestAddr(Addr addr, Addr &symaddr, Addr &nextaddr) const 134512SN/A { 1352103SN/A ATable::const_iterator i; 1362103SN/A if (!upperBound(addr, i)) 1375543Ssaidi@eecs.umich.edu return false; 1385543Ssaidi@eecs.umich.edu 1395922Sgblack@eecs.umich.edu nextaddr = i->first; 1405222Sksewell@umich.edu --i; 1412SN/A symaddr = i->first; 142725SN/A return true; 1432336SN/A } 1442227SN/A 1452336SN/A bool 1462336SN/A findNearestAddr(Addr addr, Addr &symaddr) const 147725SN/A { 1484828Sgblack@eecs.umich.edu ATable::const_iterator i; 1494828Sgblack@eecs.umich.edu if (!upperBound(addr, i)) 1504828Sgblack@eecs.umich.edu return false; 1513271Sgblack@eecs.umich.edu 1524539Sgblack@eecs.umich.edu --i; 1535543Ssaidi@eecs.umich.edu symaddr = i->first; 1545543Ssaidi@eecs.umich.edu return true; 1555543Ssaidi@eecs.umich.edu } 1565543Ssaidi@eecs.umich.edu}; 1573271Sgblack@eecs.umich.edu 1585543Ssaidi@eecs.umich.edu/// Global unified debugging symbol table (for target). Conceptually 1595222Sksewell@umich.edu/// there should be one of these per System object for full system, 1607784SAli.Saidi@ARM.com/// and per Process object for non-full-system, but so far one big 1612SN/A/// global one has worked well enough. 1622SN/Aextern SymbolTable *debugSymbolTable; 1632SN/A 1647619Sgblack@eecs.umich.edu#endif // __SYMTAB_HH__ 1657619Sgblack@eecs.umich.edu