1 //===- SymbolTable.cpp ----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Symbol table is a bag of all known symbols. We put all symbols of
10 // all input files to the symbol table. The symbol table is basically
11 // a hash table with the logic to resolve symbol name conflicts using
12 // the symbol types.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "SymbolTable.h"
17 #include "Config.h"
18 #include "InputFiles.h"
19 #include "Symbols.h"
20 #include "lld/Common/ErrorHandler.h"
21 #include "lld/Common/Memory.h"
22 #include "lld/Common/Strings.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/Demangle/Demangle.h"
25
26 using namespace llvm;
27 using namespace llvm::object;
28 using namespace llvm::ELF;
29 using namespace lld;
30 using namespace lld::elf;
31
32 SymbolTable elf::symtab;
33
wrap(Symbol * sym,Symbol * real,Symbol * wrap)34 void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) {
35 // Redirect __real_foo to the original foo and foo to the original __wrap_foo.
36 int &idx1 = symMap[CachedHashStringRef(sym->getName())];
37 int &idx2 = symMap[CachedHashStringRef(real->getName())];
38 int &idx3 = symMap[CachedHashStringRef(wrap->getName())];
39
40 idx2 = idx1;
41 idx1 = idx3;
42
43 // Propagate symbol usage information to the redirected symbols.
44 if (sym->isUsedInRegularObj)
45 wrap->isUsedInRegularObj = true;
46 if (real->isUsedInRegularObj)
47 sym->isUsedInRegularObj = true;
48 else if (!sym->isDefined())
49 // Now that all references to sym have been redirected to wrap, if there are
50 // no references to real (which has been redirected to sym), we only need to
51 // keep sym if it was defined, otherwise it's unused and can be dropped.
52 sym->isUsedInRegularObj = false;
53
54 // Now renaming is complete, and no one refers to real. We drop real from
55 // .symtab and .dynsym. If real is undefined, it is important that we don't
56 // leave it in .dynsym, because otherwise it might lead to an undefined symbol
57 // error in a subsequent link. If real is defined, we could emit real as an
58 // alias for sym, but that could degrade the user experience of some tools
59 // that can print out only one symbol for each location: sym is a preferred
60 // name than real, but they might print out real instead.
61 memcpy(real, sym, sizeof(SymbolUnion));
62 real->isUsedInRegularObj = false;
63 }
64
65 // Find an existing symbol or create a new one.
insert(StringRef name)66 Symbol *SymbolTable::insert(StringRef name) {
67 // <name>@@<version> means the symbol is the default version. In that
68 // case <name>@@<version> will be used to resolve references to <name>.
69 //
70 // Since this is a hot path, the following string search code is
71 // optimized for speed. StringRef::find(char) is much faster than
72 // StringRef::find(StringRef).
73 StringRef stem = name;
74 size_t pos = name.find('@');
75 if (pos != StringRef::npos && pos + 1 < name.size() && name[pos + 1] == '@')
76 stem = name.take_front(pos);
77
78 auto p = symMap.insert({CachedHashStringRef(stem), (int)symVector.size()});
79 if (!p.second) {
80 Symbol *sym = symVector[p.first->second];
81 if (stem.size() != name.size()) {
82 sym->setName(name);
83 sym->hasVersionSuffix = true;
84 }
85 return sym;
86 }
87
88 Symbol *sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
89 symVector.push_back(sym);
90
91 // *sym was not initialized by a constructor. Initialize all Symbol fields.
92 memset(sym, 0, sizeof(Symbol));
93 sym->setName(name);
94 sym->partition = 1;
95 sym->verdefIndex = -1;
96 sym->gwarn = false;
97 sym->versionId = VER_NDX_GLOBAL;
98 if (pos != StringRef::npos)
99 sym->hasVersionSuffix = true;
100 return sym;
101 }
102
103 // This variant of addSymbol is used by BinaryFile::parse to check duplicate
104 // symbol errors.
addAndCheckDuplicate(const Defined & newSym)105 Symbol *SymbolTable::addAndCheckDuplicate(const Defined &newSym) {
106 Symbol *sym = insert(newSym.getName());
107 if (sym->isDefined())
108 sym->checkDuplicate(newSym);
109 sym->resolve(newSym);
110 sym->isUsedInRegularObj = true;
111 return sym;
112 }
113
find(StringRef name)114 Symbol *SymbolTable::find(StringRef name) {
115 auto it = symMap.find(CachedHashStringRef(name));
116 if (it == symMap.end())
117 return nullptr;
118 return symVector[it->second];
119 }
120
121 // A version script/dynamic list is only meaningful for a Defined symbol.
122 // A CommonSymbol will be converted to a Defined in replaceCommonSymbols().
123 // A lazy symbol may be made Defined if an LTO libcall extracts it.
canBeVersioned(const Symbol & sym)124 static bool canBeVersioned(const Symbol &sym) {
125 return sym.isDefined() || sym.isCommon() || sym.isLazy();
126 }
127
128 // Initialize demangledSyms with a map from demangled symbols to symbol
129 // objects. Used to handle "extern C++" directive in version scripts.
130 //
131 // The map will contain all demangled symbols. That can be very large,
132 // and in LLD we generally want to avoid do anything for each symbol.
133 // Then, why are we doing this? Here's why.
134 //
135 // Users can use "extern C++ {}" directive to match against demangled
136 // C++ symbols. For example, you can write a pattern such as
137 // "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
138 // other than trying to match a pattern against all demangled symbols.
139 // So, if "extern C++" feature is used, we need to demangle all known
140 // symbols.
getDemangledSyms()141 StringMap<SmallVector<Symbol *, 0>> &SymbolTable::getDemangledSyms() {
142 if (!demangledSyms) {
143 demangledSyms.emplace();
144 std::string demangled;
145 for (Symbol *sym : symVector)
146 if (canBeVersioned(*sym)) {
147 StringRef name = sym->getName();
148 size_t pos = name.find('@');
149 if (pos == std::string::npos)
150 demangled = demangle(name.str());
151 else if (pos + 1 == name.size() || name[pos + 1] == '@')
152 demangled = demangle(name.substr(0, pos).str());
153 else
154 demangled =
155 (demangle(name.substr(0, pos).str()) + name.substr(pos)).str();
156 (*demangledSyms)[demangled].push_back(sym);
157 }
158 }
159 return *demangledSyms;
160 }
161
findByVersion(SymbolVersion ver)162 SmallVector<Symbol *, 0> SymbolTable::findByVersion(SymbolVersion ver) {
163 if (ver.isExternCpp)
164 return getDemangledSyms().lookup(ver.name);
165 if (Symbol *sym = find(ver.name))
166 if (canBeVersioned(*sym))
167 return {sym};
168 return {};
169 }
170
findAllByVersion(SymbolVersion ver,bool includeNonDefault)171 SmallVector<Symbol *, 0> SymbolTable::findAllByVersion(SymbolVersion ver,
172 bool includeNonDefault) {
173 SmallVector<Symbol *, 0> res;
174 SingleStringMatcher m(ver.name);
175 auto check = [&](StringRef name) {
176 size_t pos = name.find('@');
177 if (!includeNonDefault)
178 return pos == StringRef::npos;
179 return !(pos + 1 < name.size() && name[pos + 1] == '@');
180 };
181
182 if (ver.isExternCpp) {
183 for (auto &p : getDemangledSyms())
184 if (m.match(p.first()))
185 for (Symbol *sym : p.second)
186 if (check(sym->getName()))
187 res.push_back(sym);
188 return res;
189 }
190
191 for (Symbol *sym : symVector)
192 if (canBeVersioned(*sym) && check(sym->getName()) &&
193 m.match(sym->getName()))
194 res.push_back(sym);
195 return res;
196 }
197
handleDynamicList()198 void SymbolTable::handleDynamicList() {
199 SmallVector<Symbol *, 0> syms;
200 for (SymbolVersion &ver : config->dynamicList) {
201 if (ver.hasWildcard)
202 syms = findAllByVersion(ver, /*includeNonDefault=*/true);
203 else
204 syms = findByVersion(ver);
205
206 for (Symbol *sym : syms)
207 sym->inDynamicList = true;
208 }
209 }
210
211 // Set symbol versions to symbols. This function handles patterns containing no
212 // wildcard characters. Return false if no symbol definition matches ver.
assignExactVersion(SymbolVersion ver,uint16_t versionId,StringRef versionName,bool includeNonDefault)213 bool SymbolTable::assignExactVersion(SymbolVersion ver, uint16_t versionId,
214 StringRef versionName,
215 bool includeNonDefault) {
216 // Get a list of symbols which we need to assign the version to.
217 SmallVector<Symbol *, 0> syms = findByVersion(ver);
218
219 auto getName = [](uint16_t ver) -> std::string {
220 if (ver == VER_NDX_LOCAL)
221 return "VER_NDX_LOCAL";
222 if (ver == VER_NDX_GLOBAL)
223 return "VER_NDX_GLOBAL";
224 return ("version '" + config->versionDefinitions[ver].name + "'").str();
225 };
226
227 // Assign the version.
228 for (Symbol *sym : syms) {
229 // For a non-local versionId, skip symbols containing version info because
230 // symbol versions specified by symbol names take precedence over version
231 // scripts. See parseSymbolVersion().
232 if (!includeNonDefault && versionId != VER_NDX_LOCAL &&
233 sym->getName().contains('@'))
234 continue;
235
236 // If the version has not been assigned, verdefIndex is -1. Use an arbitrary
237 // number (0) to indicate the version has been assigned.
238 if (sym->verdefIndex == uint16_t(-1)) {
239 sym->verdefIndex = 0;
240 sym->versionId = versionId;
241 }
242 if (sym->versionId == versionId)
243 continue;
244
245 warn("attempt to reassign symbol '" + ver.name + "' of " +
246 getName(sym->versionId) + " to " + getName(versionId));
247 }
248 return !syms.empty();
249 }
250
assignWildcardVersion(SymbolVersion ver,uint16_t versionId,bool includeNonDefault)251 void SymbolTable::assignWildcardVersion(SymbolVersion ver, uint16_t versionId,
252 bool includeNonDefault) {
253 // Exact matching takes precedence over fuzzy matching,
254 // so we set a version to a symbol only if no version has been assigned
255 // to the symbol. This behavior is compatible with GNU.
256 for (Symbol *sym : findAllByVersion(ver, includeNonDefault))
257 if (sym->verdefIndex == uint16_t(-1)) {
258 sym->verdefIndex = 0;
259 sym->versionId = versionId;
260 }
261 }
262
263 // This function processes version scripts by updating the versionId
264 // member of symbols.
265 // If there's only one anonymous version definition in a version
266 // script file, the script does not actually define any symbol version,
267 // but just specifies symbols visibilities.
scanVersionScript()268 void SymbolTable::scanVersionScript() {
269 SmallString<128> buf;
270 // First, we assign versions to exact matching symbols,
271 // i.e. version definitions not containing any glob meta-characters.
272 for (VersionDefinition &v : config->versionDefinitions) {
273 auto assignExact = [&](SymbolVersion pat, uint16_t id, StringRef ver) {
274 bool found =
275 assignExactVersion(pat, id, ver, /*includeNonDefault=*/false);
276 buf.clear();
277 found |= assignExactVersion({(pat.name + "@" + v.name).toStringRef(buf),
278 pat.isExternCpp, /*hasWildCard=*/false},
279 id, ver, /*includeNonDefault=*/true);
280 if (!found && !config->undefinedVersion)
281 warn("version script assignment of '" + ver + "' to symbol '" +
282 pat.name + "' failed: symbol not defined");
283 };
284 for (SymbolVersion &pat : v.nonLocalPatterns)
285 if (!pat.hasWildcard)
286 assignExact(pat, v.id, v.name);
287 for (SymbolVersion pat : v.localPatterns)
288 if (!pat.hasWildcard)
289 assignExact(pat, VER_NDX_LOCAL, "local");
290 }
291
292 // Next, assign versions to wildcards that are not "*". Note that because the
293 // last match takes precedence over previous matches, we iterate over the
294 // definitions in the reverse order.
295 auto assignWildcard = [&](SymbolVersion pat, uint16_t id, StringRef ver) {
296 assignWildcardVersion(pat, id, /*includeNonDefault=*/false);
297 buf.clear();
298 assignWildcardVersion({(pat.name + "@" + ver).toStringRef(buf),
299 pat.isExternCpp, /*hasWildCard=*/true},
300 id,
301 /*includeNonDefault=*/true);
302 };
303 for (VersionDefinition &v : llvm::reverse(config->versionDefinitions)) {
304 for (SymbolVersion &pat : v.nonLocalPatterns)
305 if (pat.hasWildcard && pat.name != "*")
306 assignWildcard(pat, v.id, v.name);
307 for (SymbolVersion &pat : v.localPatterns)
308 if (pat.hasWildcard && pat.name != "*")
309 assignWildcard(pat, VER_NDX_LOCAL, v.name);
310 }
311
312 // Then, assign versions to "*". In GNU linkers they have lower priority than
313 // other wildcards.
314 for (VersionDefinition &v : config->versionDefinitions) {
315 for (SymbolVersion &pat : v.nonLocalPatterns)
316 if (pat.hasWildcard && pat.name == "*")
317 assignWildcard(pat, v.id, v.name);
318 for (SymbolVersion &pat : v.localPatterns)
319 if (pat.hasWildcard && pat.name == "*")
320 assignWildcard(pat, VER_NDX_LOCAL, v.name);
321 }
322
323 // Symbol themselves might know their versions because symbols
324 // can contain versions in the form of <name>@<version>.
325 // Let them parse and update their names to exclude version suffix.
326 for (Symbol *sym : symVector)
327 if (sym->hasVersionSuffix)
328 sym->parseSymbolVersion();
329
330 // isPreemptible is false at this point. To correctly compute the binding of a
331 // Defined (which is used by includeInDynsym()), we need to know if it is
332 // VER_NDX_LOCAL or not. Compute symbol versions before handling
333 // --dynamic-list.
334 handleDynamicList();
335 }
336