Newer
Older
//===- Core/SymbolTable.cpp - Main Symbol Table ---------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lld/Core/SymbolTable.h"
#include "lld/Core/AbsoluteAtom.h"
#include "lld/Core/Atom.h"
Michael J. Spencer
committed
#include "lld/Core/DefinedAtom.h"
#include "lld/Core/File.h"
Michael J. Spencer
committed
#include "lld/Core/LLVM.h"
#include "lld/Core/Resolver.h"
Michael J. Spencer
committed
#include "lld/Core/SharedLibraryAtom.h"
Michael J. Spencer
committed
#include "lld/Core/UndefinedAtom.h"
#include "llvm/ADT/ArrayRef.h"
Michael J. Spencer
committed
#include "llvm/ADT/DenseMapInfo.h"
Michael J. Spencer
committed
#include "llvm/ADT/Hashing.h"
Michael J. Spencer
committed
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
Michael J. Spencer
committed
#include <cstdlib>
#include <vector>
namespace lld {
SymbolTable::SymbolTable(const LinkingContext &context) : _context(context) {}
void SymbolTable::add(const UndefinedAtom &atom) {
this->addByName(atom);
}
void SymbolTable::add(const SharedLibraryAtom &atom) {
this->addByName(atom);
}
void SymbolTable::add(const AbsoluteAtom &atom) {
this->addByName(atom);
}
void SymbolTable::add(const DefinedAtom &atom) {
(atom.scope() != DefinedAtom::scopeTranslationUnit)) {
// Named atoms cannot be merged by content.
assert(atom.merge() != DefinedAtom::mergeByContent);
// Track named atoms that are not scoped to file (static).
this->addByName(atom);
} else if (atom.merge() == DefinedAtom::mergeByContent) {
// Named atoms cannot be merged by content.
assert(atom.name().empty());
this->addByContent(atom);
}
}
enum NameCollisionResolution {
NCR_First,
NCR_Second,
NCR_DupDef,
NCR_DupUndef,
NCR_DupShLib,
NCR_Error
};
static NameCollisionResolution cases[4][4] = {
//regular absolute undef sharedLib
{
// first is regular
NCR_DupDef, NCR_Error, NCR_First, NCR_First
},
{
// first is absolute
NCR_Error, NCR_Error, NCR_First, NCR_First
},
{
// first is undef
NCR_Second, NCR_Second, NCR_DupUndef, NCR_Second
},
{
// first is sharedLib
NCR_Second, NCR_Second, NCR_First, NCR_DupShLib
}
};
static NameCollisionResolution collide(Atom::Definition first,
Atom::Definition second) {
return cases[first][second];
}
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
enum MergeResolution {
MCR_First,
MCR_Second,
MCR_Largest,
MCR_Error
};
static MergeResolution mergeCases[4][4] = {
// no tentative weak weakAddressUsed
{
// first is no
MCR_Error, MCR_First, MCR_First, MCR_First
},
{
// first is tentative
MCR_Second, MCR_Largest, MCR_Second, MCR_Second
},
{
// first is weak
MCR_Second, MCR_First, MCR_First, MCR_Second
},
{
// first is weakAddressUsed
MCR_Second, MCR_First, MCR_First, MCR_First
}
};
static MergeResolution mergeSelect(DefinedAtom::Merge first,
DefinedAtom::Merge second) {
return mergeCases[first][second];
}
void SymbolTable::addByName(const Atom & newAtom) {
Michael J. Spencer
committed
StringRef name = newAtom.name();
assert(!name.empty());
const Atom *existing = this->findByName(name);
// Name is not in symbol table yet, add it associate with this atom.
_nameTable[name] = &newAtom;
// Name is already in symbol table and associated with another atom.
bool useNew = true;
switch (collide(existing->definition(), newAtom.definition())) {
case NCR_First:
useNew = false;
break;
case NCR_Second:
useNew = true;
break;
case NCR_DupDef:
assert(existing->definition() == Atom::definitionRegular);
assert(newAtom.definition() == Atom::definitionRegular);
switch (mergeSelect(((DefinedAtom*)existing)->merge(),
((DefinedAtom*)(&newAtom))->merge())) {
case MCR_First:
useNew = false;
break;
useNew = true;
break;
break;
case MCR_Error:
llvm::errs() << "Duplicate symbols: "
<< existing->name()
<< ":"
<< existing->file().path()
<< " and "
<< newAtom.name()
<< ":"
<< newAtom.file().path()
<< "\n";
llvm::report_fatal_error("duplicate symbol error");
break;
}
break;
case NCR_DupUndef: {
const UndefinedAtom* existingUndef =
dyn_cast<UndefinedAtom>(existing);
const UndefinedAtom* newUndef =
dyn_cast<UndefinedAtom>(&newAtom);
assert(existingUndef != nullptr);
assert(newUndef != nullptr);
if (existingUndef->canBeNull() == newUndef->canBeNull()) {
useNew = false;
} else {
if (_context.warnIfCoalesableAtomsHaveDifferentCanBeNull()) {
// FIXME: need diagonstics interface for writing warning messages
llvm::errs() << "lld warning: undefined symbol "
<< existingUndef->name()
<< " has different weakness in "
<< existingUndef->file().path()
<< " and in "
<< newUndef->file().path();
}
useNew = (newUndef->canBeNull() < existingUndef->canBeNull());
}
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
break;
case NCR_DupShLib: {
const SharedLibraryAtom* curShLib =
dyn_cast<SharedLibraryAtom>(existing);
const SharedLibraryAtom* newShLib =
dyn_cast<SharedLibraryAtom>(&newAtom);
assert(curShLib != nullptr);
assert(newShLib != nullptr);
bool sameNullness = (curShLib->canBeNullAtRuntime()
== newShLib->canBeNullAtRuntime());
bool sameName = curShLib->loadName().equals(newShLib->loadName());
if (!sameName) {
useNew = false;
if (_context.warnIfCoalesableAtomsHaveDifferentLoadName()) {
// FIXME: need diagonstics interface for writing warning messages
llvm::errs() << "lld warning: shared library symbol "
<< curShLib->name()
<< " has different load path in "
<< curShLib->file().path()
<< " and in "
<< newShLib->file().path();
}
} else if (!sameNullness) {
useNew = false;
if (_context.warnIfCoalesableAtomsHaveDifferentCanBeNull()) {
// FIXME: need diagonstics interface for writing warning messages
llvm::errs() << "lld warning: shared library symbol "
<< curShLib->name()
<< " has different weakness in "
<< curShLib->file().path()
<< " and in "
<< newShLib->file().path();
}
} else {
// Both shlib atoms are identical and can be coalesced.
useNew = false;
}
break;
default:
llvm::report_fatal_error("SymbolTable::addByName(): unhandled switch clause");
}
if (useNew) {
// Update name table to use new atom.
_nameTable[name] = &newAtom;
// Add existing atom to replacement table.
_replacedAtoms[existing] = &newAtom;
} else {
// New atom is not being used. Add it to replacement table.
_replacedAtoms[&newAtom] = existing;
}
}
Michael J. Spencer
committed
unsigned SymbolTable::AtomMappingInfo::getHashValue(const DefinedAtom *atom) {
auto content = atom->rawContent();
return llvm::hash_combine(atom->size(),
atom->contentType(),
llvm::hash_combine_range(content.begin(),
content.end()));
}
bool SymbolTable::AtomMappingInfo::isEqual(const DefinedAtom * const l,
Michael J. Spencer
committed
const DefinedAtom * const r) {
return true;
return false;
return false;
return false;
return false;
Michael J. Spencer
committed
if (l->contentType() != r->contentType())
return false;
return false;
Michael J. Spencer
committed
ArrayRef<uint8_t> lc = l->rawContent();
ArrayRef<uint8_t> rc = r->rawContent();
Michael J. Spencer
committed
return memcmp(lc.data(), rc.data(), lc.size()) == 0;
}
void SymbolTable::addByContent(const DefinedAtom & newAtom) {
// Currently only read-only constants can be merged.
assert(newAtom.permissions() == DefinedAtom::permR__);
AtomContentSet::iterator pos = _contentTable.find(&newAtom);
_contentTable.insert(&newAtom);
return;
}
const Atom* existing = *pos;
// New atom is not being used. Add it to replacement table.
_replacedAtoms[&newAtom] = existing;
}
Michael J. Spencer
committed
const Atom *SymbolTable::findByName(StringRef sym) {
NameToAtom::iterator pos = _nameTable.find(sym);
if (pos == _nameTable.end())
return pos->second;
}
Michael J. Spencer
committed
bool SymbolTable::isDefined(StringRef sym) {
const Atom *atom = this->findByName(sym);
return false;
if (atom->definition() == Atom::definitionUndefined)
return false;
return true;
}
void SymbolTable::addReplacement(const Atom *replaced,
const Atom *replacement) {
_replacedAtoms[replaced] = replacement;
}
const Atom *SymbolTable::replacement(const Atom *atom) {
AtomToAtom::iterator pos = _replacedAtoms.find(atom);
if (pos == _replacedAtoms.end())
return atom;
// might be chain, recurse to end
return this->replacement(pos->second);
}
unsigned int SymbolTable::size() {
return _nameTable.size();
}
Michael J. Spencer
committed
void SymbolTable::undefines(std::vector<const UndefinedAtom *> &undefs) {
for (NameToAtom::iterator it = _nameTable.begin(),
end = _nameTable.end(); it != end; ++it) {
const Atom *atom = it->second;
if (const auto undef = dyn_cast<const UndefinedAtom>(atom)) {
AtomToAtom::iterator pos = _replacedAtoms.find(undef);
if (pos != _replacedAtoms.end())
continue;
Michael J. Spencer
committed
undefs.push_back(undef);
}
}
void SymbolTable::tentativeDefinitions(std::vector<StringRef> &names) {
for (auto entry : _nameTable) {
const Atom *atom = entry.second;
StringRef name = entry.first;
assert(atom != nullptr);
if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom))
if (defAtom->merge() == DefinedAtom::mergeAsTentative)
names.push_back(name);
}
}
} // namespace lld