1 //===- Core/SymbolTable.cpp - Main Symbol Table ---------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lld/Core/SymbolTable.h"
11 #include "lld/Core/AbsoluteAtom.h"
12 #include "lld/Core/Atom.h"
13 #include "lld/Core/DefinedAtom.h"
14 #include "lld/Core/File.h"
15 #include "lld/Core/LLVM.h"
16 #include "lld/Core/LinkingContext.h"
17 #include "lld/Core/Resolver.h"
18 #include "lld/Core/SharedLibraryAtom.h"
19 #include "lld/Core/UndefinedAtom.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/DenseMapInfo.h"
22 #include "llvm/ADT/Hashing.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
31 SymbolTable::SymbolTable(LinkingContext &context) : _context(context) {}
33 bool SymbolTable::add(const UndefinedAtom &atom) { return addByName(atom); }
35 bool SymbolTable::add(const SharedLibraryAtom &atom) { return addByName(atom); }
37 bool SymbolTable::add(const AbsoluteAtom &atom) { return addByName(atom); }
39 bool SymbolTable::add(const DefinedAtom &atom) {
40 if (!atom.name().empty() &&
41 atom.scope() != DefinedAtom::scopeTranslationUnit) {
42 // Named atoms cannot be merged by content.
43 assert(atom.merge() != DefinedAtom::mergeByContent);
44 // Track named atoms that are not scoped to file (static).
45 return addByName(atom);
47 if (atom.merge() == DefinedAtom::mergeByContent) {
48 // Named atoms cannot be merged by content.
49 assert(atom.name().empty());
50 // Currently only read-only constants can be merged.
51 if (atom.permissions() == DefinedAtom::permR__)
52 return addByContent(atom);
53 // TODO: support mergeByContent of data atoms by comparing content & fixups.
58 const Atom *SymbolTable::findGroup(StringRef sym) {
59 NameToAtom::iterator pos = _groupTable.find(sym);
60 if (pos == _groupTable.end())
65 bool SymbolTable::addGroup(const DefinedAtom &da) {
66 StringRef name = da.name();
67 assert(!name.empty());
68 const Atom *existing = findGroup(name);
69 if (existing == nullptr) {
70 _groupTable[name] = &da;
73 _replacedAtoms[&da] = existing;
77 enum NameCollisionResolution {
86 static NameCollisionResolution cases[4][4] = {
87 //regular absolute undef sharedLib
90 NCR_DupDef, NCR_Error, NCR_First, NCR_First
94 NCR_Error, NCR_Error, NCR_First, NCR_First
98 NCR_Second, NCR_Second, NCR_DupUndef, NCR_Second
101 // first is sharedLib
102 NCR_Second, NCR_Second, NCR_First, NCR_DupShLib
106 static NameCollisionResolution collide(Atom::Definition first,
107 Atom::Definition second) {
108 return cases[first][second];
111 enum MergeResolution {
119 static MergeResolution mergeCases[][6] = {
120 // no tentative weak weakAddress sameNameAndSize largest
121 {MCR_Error, MCR_First, MCR_First, MCR_First, MCR_SameSize, MCR_Largest}, // no
122 {MCR_Second, MCR_Largest, MCR_Second, MCR_Second, MCR_SameSize, MCR_Largest}, // tentative
123 {MCR_Second, MCR_First, MCR_First, MCR_Second, MCR_SameSize, MCR_Largest}, // weak
124 {MCR_Second, MCR_First, MCR_First, MCR_First, MCR_SameSize, MCR_Largest}, // weakAddress
125 {MCR_SameSize, MCR_SameSize, MCR_SameSize, MCR_SameSize, MCR_SameSize, MCR_SameSize}, // sameSize
126 {MCR_Largest, MCR_Largest, MCR_Largest, MCR_Largest, MCR_SameSize, MCR_Largest}, // largest
129 static MergeResolution mergeSelect(DefinedAtom::Merge first,
130 DefinedAtom::Merge second) {
131 assert(first != DefinedAtom::mergeByContent);
132 assert(second != DefinedAtom::mergeByContent);
133 return mergeCases[first][second];
136 bool SymbolTable::addByName(const Atom &newAtom) {
137 StringRef name = newAtom.name();
138 assert(!name.empty());
139 const Atom *existing = findByName(name);
140 if (existing == nullptr) {
141 // Name is not in symbol table yet, add it associate with this atom.
142 _nameTable[name] = &newAtom;
146 // Do nothing if the same object is added more than once.
147 if (existing == &newAtom)
150 // Name is already in symbol table and associated with another atom.
152 switch (collide(existing->definition(), newAtom.definition())) {
160 const auto *existingDef = cast<DefinedAtom>(existing);
161 const auto *newDef = cast<DefinedAtom>(&newAtom);
162 switch (mergeSelect(existingDef->merge(), newDef->merge())) {
170 uint64_t existingSize = existingDef->sectionSize();
171 uint64_t newSize = newDef->sectionSize();
172 useNew = (newSize >= existingSize);
176 uint64_t existingSize = existingDef->sectionSize();
177 uint64_t newSize = newDef->sectionSize();
178 if (existingSize == newSize) {
182 llvm::errs() << "Size mismatch: "
183 << existing->name() << " (" << existingSize << ") "
184 << newAtom.name() << " (" << newSize << ")\n";
188 if (!_context.getAllowDuplicates()) {
189 llvm::errs() << "Duplicate symbols: "
192 << existing->file().path()
196 << newAtom.file().path()
198 llvm::report_fatal_error("duplicate symbol error");
206 const UndefinedAtom* existingUndef = cast<UndefinedAtom>(existing);
207 const UndefinedAtom* newUndef = cast<UndefinedAtom>(&newAtom);
209 bool sameCanBeNull = (existingUndef->canBeNull() == newUndef->canBeNull());
210 if (!sameCanBeNull &&
211 _context.warnIfCoalesableAtomsHaveDifferentCanBeNull()) {
212 llvm::errs() << "lld warning: undefined symbol "
213 << existingUndef->name()
214 << " has different weakness in "
215 << existingUndef->file().path()
216 << " and in " << newUndef->file().path() << "\n";
219 const UndefinedAtom *existingFallback = existingUndef->fallback();
220 const UndefinedAtom *newFallback = newUndef->fallback();
221 bool hasDifferentFallback =
222 (existingFallback && newFallback &&
223 existingFallback->name() != newFallback->name());
224 if (hasDifferentFallback) {
225 llvm::errs() << "lld warning: undefined symbol "
226 << existingUndef->name() << " has different fallback: "
227 << existingFallback->name() << " in "
228 << existingUndef->file().path() << " and "
229 << newFallback->name() << " in "
230 << newUndef->file().path() << "\n";
233 bool hasNewFallback = newUndef->fallback();
235 useNew = hasNewFallback;
237 useNew = (newUndef->canBeNull() < existingUndef->canBeNull());
241 const SharedLibraryAtom *curShLib = cast<SharedLibraryAtom>(existing);
242 const SharedLibraryAtom *newShLib = cast<SharedLibraryAtom>(&newAtom);
244 (curShLib->canBeNullAtRuntime() == newShLib->canBeNullAtRuntime());
245 bool sameName = curShLib->loadName().equals(newShLib->loadName());
246 if (sameName && !sameNullness &&
247 _context.warnIfCoalesableAtomsHaveDifferentCanBeNull()) {
248 // FIXME: need diagonstics interface for writing warning messages
249 llvm::errs() << "lld warning: shared library symbol "
250 << curShLib->name() << " has different weakness in "
251 << curShLib->file().path() << " and in "
252 << newShLib->file().path();
254 if (!sameName && _context.warnIfCoalesableAtomsHaveDifferentLoadName()) {
255 // FIXME: need diagonstics interface for writing warning messages
256 llvm::errs() << "lld warning: shared library symbol "
257 << curShLib->name() << " has different load path in "
258 << curShLib->file().path() << " and in "
259 << newShLib->file().path();
265 llvm::errs() << "SymbolTable: error while merging " << name << "\n";
266 llvm::report_fatal_error("duplicate symbol error");
270 // Give context a chance to change which is kept.
271 _context.notifySymbolTableCoalesce(existing, &newAtom, useNew);
274 // Update name table to use new atom.
275 _nameTable[name] = &newAtom;
276 // Add existing atom to replacement table.
277 _replacedAtoms[existing] = &newAtom;
279 // New atom is not being used. Add it to replacement table.
280 _replacedAtoms[&newAtom] = existing;
285 unsigned SymbolTable::AtomMappingInfo::getHashValue(const DefinedAtom *atom) {
286 auto content = atom->rawContent();
287 return llvm::hash_combine(atom->size(),
289 llvm::hash_combine_range(content.begin(),
293 bool SymbolTable::AtomMappingInfo::isEqual(const DefinedAtom * const l,
294 const DefinedAtom * const r) {
297 if (l == getEmptyKey())
299 if (r == getEmptyKey())
301 if (l == getTombstoneKey())
303 if (r == getTombstoneKey())
305 if (l->contentType() != r->contentType())
307 if (l->size() != r->size())
309 if (l->sectionChoice() != r->sectionChoice())
311 if (l->sectionChoice() == DefinedAtom::sectionCustomRequired) {
312 if (!l->customSectionName().equals(r->customSectionName()))
315 ArrayRef<uint8_t> lc = l->rawContent();
316 ArrayRef<uint8_t> rc = r->rawContent();
317 return memcmp(lc.data(), rc.data(), lc.size()) == 0;
320 bool SymbolTable::addByContent(const DefinedAtom &newAtom) {
321 AtomContentSet::iterator pos = _contentTable.find(&newAtom);
322 if (pos == _contentTable.end()) {
323 _contentTable.insert(&newAtom);
326 const Atom* existing = *pos;
327 // New atom is not being used. Add it to replacement table.
328 _replacedAtoms[&newAtom] = existing;
332 const Atom *SymbolTable::findByName(StringRef sym) {
333 NameToAtom::iterator pos = _nameTable.find(sym);
334 if (pos == _nameTable.end())
339 bool SymbolTable::isDefined(StringRef sym) {
340 if (const Atom *atom = findByName(sym))
341 return !isa<UndefinedAtom>(atom);
345 void SymbolTable::addReplacement(const Atom *replaced,
346 const Atom *replacement) {
347 _replacedAtoms[replaced] = replacement;
350 const Atom *SymbolTable::replacement(const Atom *atom) {
351 // Find the replacement for a given atom. Atoms in _replacedAtoms
352 // may be chained, so find the last one.
354 AtomToAtom::iterator pos = _replacedAtoms.find(atom);
355 if (pos == _replacedAtoms.end())
361 bool SymbolTable::isCoalescedAway(const Atom *atom) {
362 return _replacedAtoms.count(atom) > 0;
365 std::vector<const UndefinedAtom *> SymbolTable::undefines() {
366 std::vector<const UndefinedAtom *> ret;
367 for (auto it : _nameTable) {
368 const Atom *atom = it.second;
369 assert(atom != nullptr);
370 if (const auto *undef = dyn_cast<const UndefinedAtom>(atom))
371 if (_replacedAtoms.count(undef) == 0)
372 ret.push_back(undef);
377 std::vector<StringRef> SymbolTable::tentativeDefinitions() {
378 std::vector<StringRef> ret;
379 for (auto entry : _nameTable) {
380 const Atom *atom = entry.second;
381 StringRef name = entry.first;
382 assert(atom != nullptr);
383 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom))
384 if (defAtom->merge() == DefinedAtom::mergeAsTentative)