gem5  v20.1.0.0
symtab.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2005 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 "base/loader/symtab.hh"
30 
31 #include <fstream>
32 #include <iostream>
33 #include <string>
34 #include <vector>
35 
36 #include "base/logging.hh"
37 #include "base/str.hh"
38 #include "base/trace.hh"
39 #include "base/types.hh"
40 #include "sim/serialize.hh"
41 
42 using namespace std;
43 
44 namespace Loader
45 {
46 
48 
49 void
50 SymbolTable::clear()
51 {
52  addrMap.clear();
53  nameMap.clear();
54  symbols.clear();
55 }
56 
57 bool
58 SymbolTable::insert(const Symbol &symbol)
59 {
60  if (symbol.name.empty())
61  return false;
62 
63  int idx = symbols.size();
64 
65  if (!nameMap.insert({ symbol.name, idx }).second)
66  return false;
67 
68  // There can be multiple symbols for the same address, so always
69  // update the addrTable multimap when we see a new symbol name.
70  addrMap.insert({ symbol.address, idx });
71 
72  symbols.emplace_back(symbol);
73 
74  return true;
75 }
76 
77 bool
78 SymbolTable::insert(const SymbolTable &other)
79 {
80  // Check if any symbol in other already exists in our table.
81  NameMap intersection;
82  std::set_intersection(other.nameMap.begin(), other.nameMap.end(),
83  nameMap.begin(), nameMap.end(),
84  std::inserter(intersection, intersection.begin()),
85  nameMap.value_comp());
86  if (!intersection.empty())
87  return false;
88 
89  for (const Symbol &symbol: other)
90  insert(symbol);
91 
92  return true;
93 }
94 
95 bool
96 SymbolTable::load(const string &filename)
97 {
98  string buffer;
99  ifstream file(filename.c_str());
100 
101  if (!file)
102  fatal("file error: Can't open symbol table file %s\n", filename);
103 
104  while (!file.eof()) {
105  getline(file, buffer);
106  if (buffer.empty())
107  continue;
108 
109  string::size_type idx = buffer.find(',');
110  if (idx == string::npos)
111  return false;
112 
113  string address = buffer.substr(0, idx);
114  eat_white(address);
115  if (address.empty())
116  return false;
117 
118  string name = buffer.substr(idx + 1);
119  eat_white(name);
120  if (name.empty())
121  return false;
122 
123  Addr addr;
124  if (!to_number(address, addr))
125  return false;
126 
127  if (!insert({ Symbol::Binding::Global, name, addr }))
128  return false;
129  }
130 
131  file.close();
132  return true;
133 }
134 
135 void
137 {
138  paramOut(cp, base + ".size", symbols.size());
139 
140  int i = 0;
141  for (auto &symbol: symbols) {
142  paramOut(cp, csprintf("%s.addr_%d", base, i), symbol.address);
143  paramOut(cp, csprintf("%s.symbol_%d", base, i), symbol.name);
144  paramOut(cp, csprintf("%s.binding_%d", base, i), (int)symbol.binding);
145  i++;
146  }
147 }
148 
149 void
151  Symbol::Binding default_binding)
152 {
153  clear();
154  int size;
155  paramIn(cp, base + ".size", size);
156  for (int i = 0; i < size; ++i) {
157  Addr address;
158  std::string name;
159  Symbol::Binding binding = default_binding;
160 
161  paramIn(cp, csprintf("%s.addr_%d", base, i), address);
162  paramIn(cp, csprintf("%s.symbol_%d", base, i), name);
163  if (!optParamIn(cp, csprintf("%s.binding_%d", base, i), binding))
164  binding = default_binding;
165  insert({binding, name, address});
166  }
167 }
168 
169 } // namespace Loader
fatal
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:183
Loader::Symbol::Binding
Binding
Definition: symtab.hh:48
Loader::Symbol::address
Addr address
Definition: symtab.hh:56
serialize.hh
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
Loader::SymbolTable
Definition: symtab.hh:59
serialize
void serialize(const ThreadContext &tc, CheckpointOut &cp)
Thread context serialization helpers.
Definition: thread_context.cc:142
X86ISA::base
Bitfield< 51, 12 > base
Definition: pagetable.hh:141
Loader::SymbolTable::nameMap
NameMap nameMap
Definition: symtab.hh:73
paramOut
void paramOut(CheckpointOut &cp, const string &name, ExtMachInst const &machInst)
Definition: types.cc:38
unserialize
void unserialize(ThreadContext &tc, CheckpointIn &cp)
Definition: thread_context.cc:183
str.hh
to_number
bool to_number(const std::string &value, VecPredRegContainer< NumBits, Packed > &p)
Helper functions used for serialization/de-serialization.
Definition: vec_pred_reg.hh:379
Loader
Definition: process.hh:39
Loader::debugSymbolTable
SymbolTable debugSymbolTable
Global unified debugging symbol table (for target).
Definition: symtab.cc:47
cp
Definition: cprintf.cc:40
Stats::nameMap
NameMapType & nameMap()
Definition: statistics.cc:149
Loader::SymbolTable::NameMap
std::map< std::string, int > NameMap
Definition: symtab.hh:69
optParamIn
bool optParamIn(CheckpointIn &cp, const std::string &name, T &param, bool warn=true)
This function is used for restoring optional parameters from the checkpoint.
Definition: serialize.hh:507
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
name
const std::string & name()
Definition: trace.cc:50
eat_white
void eat_white(std::string &s)
Definition: str.hh:64
Loader::Symbol::name
std::string name
Definition: symtab.hh:55
Loader::Symbol
Definition: symtab.hh:46
std
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
types.hh
Loader::SymbolTable::clear
void clear()
Definition: symtab.cc:50
addr
ip6_addr_t addr
Definition: inet.hh:423
paramIn
void paramIn(CheckpointIn &cp, const string &name, ExtMachInst &machInst)
Definition: types.cc:69
logging.hh
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:63
trace.hh
Stats::size_type
unsigned int size_type
Definition: types.hh:54
symtab.hh
CheckpointIn
Definition: serialize.hh:67
csprintf
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:158

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17