gem5 v24.0.0.0
Loading...
Searching...
No Matches
symtab.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Arm Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 */
40
41#include "base/loader/symtab.hh"
42
43#include <fstream>
44#include <iostream>
45
46#include "base/logging.hh"
47#include "base/str.hh"
48
49namespace gem5
50{
51
52namespace loader
53{
54
56
57void
59{
60 addrMap.clear();
61 nameMap.clear();
62 symbols.clear();
63}
64
65bool
67{
68 if (symbol.name().empty())
69 return false;
70
71 int idx = symbols.size();
72
73 if (!nameMap.insert({ symbol.name(), idx }).second)
74 return false;
75
76 // There can be multiple symbols for the same address, so always
77 // update the addrTable multimap when we see a new symbol name.
78 addrMap.insert({ symbol.address(), idx });
79
80 symbols.emplace_back(symbol);
81
82 return true;
83}
84
85bool
87{
88 // Check if any symbol in other already exists in our table.
89 NameMap intersection;
90 std::set_intersection(other.nameMap.begin(), other.nameMap.end(),
91 nameMap.begin(), nameMap.end(),
92 std::inserter(intersection, intersection.begin()),
93 nameMap.value_comp());
94 if (!intersection.empty()) {
95 warn("Cannot insert a new symbol table due to name collisions. "
96 "Adding prefix to each symbol's name can resolve this issue.");
97 return false;
98 }
99
100 for (const Symbol &symbol: other)
101 insert(symbol);
102
103 return true;
104}
105
106void
107SymbolTable::serialize(const std::string &base, CheckpointOut &cp) const
108{
109 paramOut(cp, base + ".size", symbols.size());
110
111 int i = 0;
112 for (auto &symbol: symbols) {
113 paramOut(cp, csprintf("%s.addr_%d", base, i), symbol.address());
114 if (symbol.sizeIsValid()) {
115 paramOut(cp, csprintf("%s.size_%d", base, i),
116 symbol.sizeOrDefault(0x0));
117 }
118 paramOut(cp, csprintf("%s.symbol_%d", base, i), symbol.name());
119 paramOut(cp, csprintf("%s.binding_%d", base, i),
120 (int)symbol.binding());
121 paramOut(cp, csprintf("%s.type_%d", base, i), (int)symbol.type());
122 i++;
123 }
124}
125
126void
128 Symbol::Binding default_binding)
129{
130 clear();
131 int size;
132 paramIn(cp, base + ".size", size);
133 for (int i = 0; i < size; ++i) {
134 Addr address;
135 size_t size;
136 std::string name;
137 Symbol::Binding binding = default_binding;
139
140 paramIn(cp, csprintf("%s.addr_%d", base, i), address);
141 bool size_present = optParamIn(
142 cp, csprintf("%s.size_%d", base, i), size, false);
143 paramIn(cp, csprintf("%s.symbol_%d", base, i), name);
144 if (!optParamIn(cp, csprintf("%s.binding_%d", base, i), binding))
145 binding = default_binding;
146 if (!optParamIn(cp, csprintf("%s.type_%d", base, i), type))
148 if (size_present) {
149 insert(Symbol(binding, type, name, address, size));
150 } else {
151 warn_once(
152 "warning: one or more Symbols does not have a valid size.");
153 insert(Symbol(binding, type, name, address));
154 }
155 }
156}
157
158} // namespace loader
159} // namespace gem5
std::map< std::string, int > NameMap
Map a symbol name to an index into the symbol vector.
Definition symtab.hh:162
void clear()
Clears the table.
Definition symtab.cc:58
void unserialize(const std::string &base, CheckpointIn &cp, Symbol::Binding default_binding=Symbol::Binding::Global)
Populate the table by unserializing a checkpoint.
Definition symtab.cc:127
void serialize(const std::string &base, CheckpointOut &cp) const
Serialize the table's contents.
Definition symtab.cc:107
bool insert(const Symbol &symbol)
Insert a new symbol in the table if it does not already exist.
Definition symtab.cc:66
std::string name() const
Definition symtab.hh:106
Addr address() const
Definition symtab.hh:114
bool optParamIn(CheckpointIn &cp, const std::string &name, T &param, bool do_warn=true)
This function is used for restoring optional parameters from the checkpoint.
Definition serialize.hh:357
#define warn(...)
Definition logging.hh:256
#define warn_once(...)
Definition logging.hh:260
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 51, 12 > base
Definition pagetable.hh:141
SymbolTable debugSymbolTable
Global unified debugging symbol table (for target).
Definition symtab.cc:55
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
std::ostream CheckpointOut
Definition serialize.hh:66
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
void paramOut(CheckpointOut &cp, const std::string &name, ExtMachInst const &machInst)
Definition types.cc:40
void paramIn(CheckpointIn &cp, const std::string &name, ExtMachInst &machInst)
Definition types.cc:72
std::string csprintf(const char *format, const Args &...args)
Definition cprintf.hh:161
const std::string & name()
Definition trace.cc:48

Generated on Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0