2011-12-18 16:27:59 +08:00
|
|
|
//===- 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"
|
2012-02-23 05:56:59 +08:00
|
|
|
#include "lld/Core/AbsoluteAtom.h"
|
2013-01-23 04:49:42 +08:00
|
|
|
#include "lld/Core/Atom.h"
|
2012-03-29 03:04:02 +08:00
|
|
|
#include "lld/Core/DefinedAtom.h"
|
2011-12-18 16:27:59 +08:00
|
|
|
#include "lld/Core/File.h"
|
2012-04-04 02:39:40 +08:00
|
|
|
#include "lld/Core/LLVM.h"
|
2014-10-18 13:33:55 +08:00
|
|
|
#include "lld/Core/LinkingContext.h"
|
2011-12-18 16:27:59 +08:00
|
|
|
#include "lld/Core/Resolver.h"
|
2012-03-29 03:04:02 +08:00
|
|
|
#include "lld/Core/SharedLibraryAtom.h"
|
|
|
|
#include "lld/Core/UndefinedAtom.h"
|
2012-01-10 04:18:15 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2012-03-29 03:04:02 +08:00
|
|
|
#include "llvm/ADT/DenseMapInfo.h"
|
2013-03-21 06:18:22 +08:00
|
|
|
#include "llvm/ADT/Hashing.h"
|
2012-03-29 03:04:02 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-04-19 05:55:06 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-12-18 16:27:59 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
2012-03-29 03:04:02 +08:00
|
|
|
#include <cstdlib>
|
2011-12-18 16:27:59 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace lld {
|
2014-05-15 01:29:27 +08:00
|
|
|
bool SymbolTable::add(const UndefinedAtom &atom) { return addByName(atom); }
|
2012-01-11 09:06:19 +08:00
|
|
|
|
2014-05-15 01:29:27 +08:00
|
|
|
bool SymbolTable::add(const SharedLibraryAtom &atom) { return addByName(atom); }
|
2012-04-04 02:40:27 +08:00
|
|
|
|
2014-05-15 01:29:27 +08:00
|
|
|
bool SymbolTable::add(const AbsoluteAtom &atom) { return addByName(atom); }
|
2012-02-23 05:56:59 +08:00
|
|
|
|
2014-05-15 01:29:27 +08:00
|
|
|
bool SymbolTable::add(const DefinedAtom &atom) {
|
2013-03-15 00:09:49 +08:00
|
|
|
if (!atom.name().empty() &&
|
2013-11-26 01:09:29 +08:00
|
|
|
atom.scope() != DefinedAtom::scopeTranslationUnit) {
|
2013-01-15 08:17:57 +08:00
|
|
|
// Named atoms cannot be merged by content.
|
|
|
|
assert(atom.merge() != DefinedAtom::mergeByContent);
|
|
|
|
// Track named atoms that are not scoped to file (static).
|
2014-05-15 01:29:27 +08:00
|
|
|
return addByName(atom);
|
2013-11-26 01:09:29 +08:00
|
|
|
}
|
|
|
|
if (atom.merge() == DefinedAtom::mergeByContent) {
|
2013-01-15 08:17:57 +08:00
|
|
|
// Named atoms cannot be merged by content.
|
|
|
|
assert(atom.name().empty());
|
2014-05-28 09:16:35 +08:00
|
|
|
// Currently only read-only constants can be merged.
|
|
|
|
if (atom.permissions() == DefinedAtom::permR__)
|
|
|
|
return addByContent(atom);
|
|
|
|
// TODO: support mergeByContent of data atoms by comparing content & fixups.
|
2014-03-27 00:37:13 +08:00
|
|
|
}
|
2014-05-15 01:29:27 +08:00
|
|
|
return false;
|
2014-03-27 00:37:13 +08:00
|
|
|
}
|
|
|
|
|
2011-12-18 16:27:59 +08:00
|
|
|
enum NameCollisionResolution {
|
|
|
|
NCR_First,
|
|
|
|
NCR_Second,
|
2012-02-23 05:56:59 +08:00
|
|
|
NCR_DupDef,
|
|
|
|
NCR_DupUndef,
|
|
|
|
NCR_DupShLib,
|
2011-12-18 16:27:59 +08:00
|
|
|
NCR_Error
|
|
|
|
};
|
|
|
|
|
2012-01-11 09:06:19 +08:00
|
|
|
static NameCollisionResolution cases[4][4] = {
|
|
|
|
//regular absolute undef sharedLib
|
2011-12-18 16:27:59 +08:00
|
|
|
{
|
|
|
|
// first is regular
|
2012-02-23 05:56:59 +08:00
|
|
|
NCR_DupDef, NCR_Error, NCR_First, NCR_First
|
2011-12-18 16:27:59 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// first is absolute
|
2012-01-11 09:06:19 +08:00
|
|
|
NCR_Error, NCR_Error, NCR_First, NCR_First
|
2011-12-18 16:27:59 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// first is undef
|
2012-02-23 05:56:59 +08:00
|
|
|
NCR_Second, NCR_Second, NCR_DupUndef, NCR_Second
|
2011-12-18 16:27:59 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// first is sharedLib
|
2012-02-23 05:56:59 +08:00
|
|
|
NCR_Second, NCR_Second, NCR_First, NCR_DupShLib
|
2011-12-18 16:27:59 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static NameCollisionResolution collide(Atom::Definition first,
|
|
|
|
Atom::Definition second) {
|
|
|
|
return cases[first][second];
|
|
|
|
}
|
|
|
|
|
2012-01-11 09:06:19 +08:00
|
|
|
enum MergeResolution {
|
|
|
|
MCR_First,
|
|
|
|
MCR_Second,
|
|
|
|
MCR_Largest,
|
2014-03-08 07:05:10 +08:00
|
|
|
MCR_SameSize,
|
2012-01-11 09:06:19 +08:00
|
|
|
MCR_Error
|
|
|
|
};
|
|
|
|
|
2014-03-19 03:37:50 +08:00
|
|
|
static MergeResolution mergeCases[][6] = {
|
|
|
|
// no tentative weak weakAddress sameNameAndSize largest
|
|
|
|
{MCR_Error, MCR_First, MCR_First, MCR_First, MCR_SameSize, MCR_Largest}, // no
|
|
|
|
{MCR_Second, MCR_Largest, MCR_Second, MCR_Second, MCR_SameSize, MCR_Largest}, // tentative
|
|
|
|
{MCR_Second, MCR_First, MCR_First, MCR_Second, MCR_SameSize, MCR_Largest}, // weak
|
|
|
|
{MCR_Second, MCR_First, MCR_First, MCR_First, MCR_SameSize, MCR_Largest}, // weakAddress
|
|
|
|
{MCR_SameSize, MCR_SameSize, MCR_SameSize, MCR_SameSize, MCR_SameSize, MCR_SameSize}, // sameSize
|
|
|
|
{MCR_Largest, MCR_Largest, MCR_Largest, MCR_Largest, MCR_SameSize, MCR_Largest}, // largest
|
2012-01-11 09:06:19 +08:00
|
|
|
};
|
|
|
|
|
2012-04-04 02:40:27 +08:00
|
|
|
static MergeResolution mergeSelect(DefinedAtom::Merge first,
|
2012-01-11 09:06:19 +08:00
|
|
|
DefinedAtom::Merge second) {
|
2014-03-08 08:44:01 +08:00
|
|
|
assert(first != DefinedAtom::mergeByContent);
|
|
|
|
assert(second != DefinedAtom::mergeByContent);
|
2012-01-11 09:06:19 +08:00
|
|
|
return mergeCases[first][second];
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:29:27 +08:00
|
|
|
bool SymbolTable::addByName(const Atom &newAtom) {
|
2012-04-04 02:39:40 +08:00
|
|
|
StringRef name = newAtom.name();
|
2013-01-15 08:17:57 +08:00
|
|
|
assert(!name.empty());
|
2014-03-27 00:37:13 +08:00
|
|
|
const Atom *existing = findByName(name);
|
2012-03-30 03:39:14 +08:00
|
|
|
if (existing == nullptr) {
|
2012-01-05 07:58:17 +08:00
|
|
|
// Name is not in symbol table yet, add it associate with this atom.
|
2011-12-22 10:38:01 +08:00
|
|
|
_nameTable[name] = &newAtom;
|
2014-05-15 01:29:27 +08:00
|
|
|
return true;
|
2012-04-04 02:40:27 +08:00
|
|
|
}
|
2013-11-14 07:22:00 +08:00
|
|
|
|
2014-05-15 01:29:27 +08:00
|
|
|
// Do nothing if the same object is added more than once.
|
|
|
|
if (existing == &newAtom)
|
|
|
|
return false;
|
|
|
|
|
2013-11-14 07:22:00 +08:00
|
|
|
// 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;
|
2014-11-14 15:15:43 +08:00
|
|
|
case NCR_DupDef: {
|
|
|
|
const auto *existingDef = cast<DefinedAtom>(existing);
|
|
|
|
const auto *newDef = cast<DefinedAtom>(&newAtom);
|
|
|
|
switch (mergeSelect(existingDef->merge(), newDef->merge())) {
|
2013-11-26 01:09:25 +08:00
|
|
|
case MCR_First:
|
|
|
|
useNew = false;
|
|
|
|
break;
|
|
|
|
case MCR_Second:
|
|
|
|
useNew = true;
|
|
|
|
break;
|
2014-03-19 03:37:50 +08:00
|
|
|
case MCR_Largest: {
|
2015-03-05 05:40:46 +08:00
|
|
|
uint64_t existingSize = existingDef->sectionSize();
|
|
|
|
uint64_t newSize = newDef->sectionSize();
|
2014-03-19 03:37:50 +08:00
|
|
|
useNew = (newSize >= existingSize);
|
2013-11-26 01:09:25 +08:00
|
|
|
break;
|
2014-03-19 03:37:50 +08:00
|
|
|
}
|
2014-03-08 07:05:10 +08:00
|
|
|
case MCR_SameSize: {
|
2015-03-05 05:40:46 +08:00
|
|
|
uint64_t existingSize = existingDef->sectionSize();
|
|
|
|
uint64_t newSize = newDef->sectionSize();
|
2014-03-19 03:37:50 +08:00
|
|
|
if (existingSize == newSize) {
|
2014-03-08 07:05:10 +08:00
|
|
|
useNew = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
llvm::errs() << "Size mismatch: "
|
2014-03-19 03:37:50 +08:00
|
|
|
<< existing->name() << " (" << existingSize << ") "
|
|
|
|
<< newAtom.name() << " (" << newSize << ")\n";
|
2014-03-08 07:05:10 +08:00
|
|
|
// fallthrough
|
|
|
|
}
|
2013-11-26 01:09:25 +08:00
|
|
|
case MCR_Error:
|
2016-02-29 05:59:02 +08:00
|
|
|
llvm::errs() << "Duplicate symbols: "
|
|
|
|
<< existing->name()
|
|
|
|
<< ":"
|
|
|
|
<< existing->file().path()
|
|
|
|
<< " and "
|
|
|
|
<< newAtom.name()
|
|
|
|
<< ":"
|
|
|
|
<< newAtom.file().path()
|
|
|
|
<< "\n";
|
|
|
|
llvm::report_fatal_error("duplicate symbol error");
|
2013-11-26 01:09:25 +08:00
|
|
|
break;
|
2011-12-18 16:27:59 +08:00
|
|
|
}
|
2013-11-14 07:22:00 +08:00
|
|
|
break;
|
2014-11-14 15:15:43 +08:00
|
|
|
}
|
2013-11-14 07:22:00 +08:00
|
|
|
case NCR_DupUndef: {
|
2014-04-05 02:21:51 +08:00
|
|
|
const UndefinedAtom* existingUndef = cast<UndefinedAtom>(existing);
|
|
|
|
const UndefinedAtom* newUndef = cast<UndefinedAtom>(&newAtom);
|
2013-11-15 11:12:24 +08:00
|
|
|
|
|
|
|
bool sameCanBeNull = (existingUndef->canBeNull() == newUndef->canBeNull());
|
|
|
|
if (sameCanBeNull)
|
2016-02-29 00:27:08 +08:00
|
|
|
useNew = false;
|
2013-11-15 11:12:24 +08:00
|
|
|
else
|
|
|
|
useNew = (newUndef->canBeNull() < existingUndef->canBeNull());
|
2013-11-14 07:22:00 +08:00
|
|
|
break;
|
2013-11-15 11:12:24 +08:00
|
|
|
}
|
2013-11-14 07:22:00 +08:00
|
|
|
case NCR_DupShLib: {
|
2014-04-05 02:21:53 +08:00
|
|
|
useNew = false;
|
2013-11-14 07:22:00 +08:00
|
|
|
break;
|
2014-04-05 02:12:27 +08:00
|
|
|
}
|
2013-11-14 07:23:38 +08:00
|
|
|
case NCR_Error:
|
|
|
|
llvm::errs() << "SymbolTable: error while merging " << name << "\n";
|
2013-11-14 14:39:31 +08:00
|
|
|
llvm::report_fatal_error("duplicate symbol error");
|
|
|
|
break;
|
2013-11-14 07:22:00 +08:00
|
|
|
}
|
2013-11-14 07:23:38 +08:00
|
|
|
|
2013-11-14 07:22:00 +08:00
|
|
|
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;
|
2011-12-18 16:27:59 +08:00
|
|
|
}
|
2014-05-15 01:29:27 +08:00
|
|
|
return false;
|
2011-12-18 16:27:59 +08:00
|
|
|
}
|
|
|
|
|
2013-03-21 06:18:22 +08:00
|
|
|
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()));
|
2012-01-10 04:18:15 +08:00
|
|
|
}
|
|
|
|
|
2012-04-04 02:40:27 +08:00
|
|
|
bool SymbolTable::AtomMappingInfo::isEqual(const DefinedAtom * const l,
|
2013-03-21 06:18:22 +08:00
|
|
|
const DefinedAtom * const r) {
|
2013-11-14 07:22:00 +08:00
|
|
|
if (l == r)
|
2012-01-10 04:18:15 +08:00
|
|
|
return true;
|
2016-08-12 20:34:39 +08:00
|
|
|
if (l == getEmptyKey() || r == getEmptyKey())
|
2012-01-10 04:18:15 +08:00
|
|
|
return false;
|
2016-08-12 20:34:39 +08:00
|
|
|
if (l == getTombstoneKey() || r == getTombstoneKey())
|
2012-01-10 04:18:15 +08:00
|
|
|
return false;
|
2013-11-14 07:22:00 +08:00
|
|
|
if (l->contentType() != r->contentType())
|
2012-01-10 04:18:15 +08:00
|
|
|
return false;
|
2013-11-14 07:22:00 +08:00
|
|
|
if (l->size() != r->size())
|
2012-01-10 04:18:15 +08:00
|
|
|
return false;
|
2014-10-03 01:22:05 +08:00
|
|
|
if (l->sectionChoice() != r->sectionChoice())
|
|
|
|
return false;
|
|
|
|
if (l->sectionChoice() == DefinedAtom::sectionCustomRequired) {
|
|
|
|
if (!l->customSectionName().equals(r->customSectionName()))
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-04 02:39:40 +08:00
|
|
|
ArrayRef<uint8_t> lc = l->rawContent();
|
|
|
|
ArrayRef<uint8_t> rc = r->rawContent();
|
2013-03-21 06:18:22 +08:00
|
|
|
return memcmp(lc.data(), rc.data(), lc.size()) == 0;
|
2012-01-10 04:18:15 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:29:27 +08:00
|
|
|
bool SymbolTable::addByContent(const DefinedAtom &newAtom) {
|
2012-01-10 04:18:15 +08:00
|
|
|
AtomContentSet::iterator pos = _contentTable.find(&newAtom);
|
2013-11-14 07:22:00 +08:00
|
|
|
if (pos == _contentTable.end()) {
|
2012-01-10 04:18:15 +08:00
|
|
|
_contentTable.insert(&newAtom);
|
2014-05-15 01:29:27 +08:00
|
|
|
return true;
|
2012-01-10 04:18:15 +08:00
|
|
|
}
|
|
|
|
const Atom* existing = *pos;
|
2013-11-14 07:22:00 +08:00
|
|
|
// New atom is not being used. Add it to replacement table.
|
|
|
|
_replacedAtoms[&newAtom] = existing;
|
2014-05-15 01:29:27 +08:00
|
|
|
return false;
|
2012-01-10 04:18:15 +08:00
|
|
|
}
|
|
|
|
|
2012-04-04 02:39:40 +08:00
|
|
|
const Atom *SymbolTable::findByName(StringRef sym) {
|
2011-12-18 16:27:59 +08:00
|
|
|
NameToAtom::iterator pos = _nameTable.find(sym);
|
|
|
|
if (pos == _nameTable.end())
|
2012-03-30 03:39:14 +08:00
|
|
|
return nullptr;
|
2011-12-18 16:27:59 +08:00
|
|
|
return pos->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Atom *SymbolTable::replacement(const Atom *atom) {
|
2014-04-04 06:58:41 +08:00
|
|
|
// Find the replacement for a given atom. Atoms in _replacedAtoms
|
|
|
|
// may be chained, so find the last one.
|
2014-04-04 06:36:55 +08:00
|
|
|
for (;;) {
|
|
|
|
AtomToAtom::iterator pos = _replacedAtoms.find(atom);
|
|
|
|
if (pos == _replacedAtoms.end())
|
|
|
|
return atom;
|
|
|
|
atom = pos->second;
|
|
|
|
}
|
2011-12-18 16:27:59 +08:00
|
|
|
}
|
|
|
|
|
2014-06-05 15:37:29 +08:00
|
|
|
bool SymbolTable::isCoalescedAway(const Atom *atom) {
|
|
|
|
return _replacedAtoms.count(atom) > 0;
|
|
|
|
}
|
|
|
|
|
2014-04-04 08:15:52 +08:00
|
|
|
std::vector<const UndefinedAtom *> SymbolTable::undefines() {
|
|
|
|
std::vector<const UndefinedAtom *> ret;
|
2013-11-26 01:09:27 +08:00
|
|
|
for (auto it : _nameTable) {
|
|
|
|
const Atom *atom = it.second;
|
2012-03-30 03:39:14 +08:00
|
|
|
assert(atom != nullptr);
|
2014-08-22 10:00:58 +08:00
|
|
|
if (const auto *undef = dyn_cast<const UndefinedAtom>(atom))
|
|
|
|
if (_replacedAtoms.count(undef) == 0)
|
|
|
|
ret.push_back(undef);
|
2011-12-18 16:27:59 +08:00
|
|
|
}
|
2014-04-04 08:15:52 +08:00
|
|
|
return ret;
|
2011-12-18 16:27:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace lld
|