2015-07-25 05:03:07 +08:00
|
|
|
//===- SymbolTable.cpp ----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SymbolTable.h"
|
2015-09-12 06:42:45 +08:00
|
|
|
#include "Config.h"
|
2015-08-06 23:08:23 +08:00
|
|
|
#include "Error.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "Symbols.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
2015-08-31 09:16:19 +08:00
|
|
|
using namespace llvm::object;
|
2015-09-23 02:19:46 +08:00
|
|
|
using namespace llvm::ELF;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf2;
|
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
template <class ELFT> SymbolTable<ELFT>::SymbolTable() {}
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
template <class ELFT> bool SymbolTable<ELFT>::shouldUseRela() const {
|
2015-09-17 22:02:10 +08:00
|
|
|
ELFKind K = getFirstELF()->getELFKind();
|
|
|
|
return K == ELF64LEKind || K == ELF64BEKind;
|
|
|
|
}
|
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
|
2015-10-02 02:02:21 +08:00
|
|
|
if (auto *AF = dyn_cast<ArchiveFile>(File.get())) {
|
2015-10-10 05:07:25 +08:00
|
|
|
ArchiveFiles.emplace_back(std::move(File));
|
2015-10-02 02:02:21 +08:00
|
|
|
AF->parse();
|
2015-09-05 06:28:10 +08:00
|
|
|
for (Lazy &Sym : AF->getLazySymbols())
|
|
|
|
addLazy(&Sym);
|
|
|
|
return;
|
|
|
|
}
|
2015-10-02 03:52:48 +08:00
|
|
|
if (auto *S = dyn_cast<SharedFileBase>(File.get())) {
|
|
|
|
S->parseSoName();
|
|
|
|
if (!IncludedSoNames.insert(S->getSoName()).second)
|
|
|
|
return;
|
2015-10-10 03:25:07 +08:00
|
|
|
S->parse();
|
|
|
|
} else {
|
|
|
|
cast<ObjectFileBase>(File.get())->parse(Comdats);
|
2015-10-02 03:52:48 +08:00
|
|
|
}
|
2015-10-02 02:02:21 +08:00
|
|
|
addELFFile(cast<ELFFileBase>(File.release()));
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-10-10 05:12:40 +08:00
|
|
|
template <class ELFT>
|
|
|
|
SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) {
|
|
|
|
auto *Sym = new (Alloc) Undefined<ELFT>(Name, Undefined<ELFT>::Required);
|
|
|
|
resolve(Sym);
|
|
|
|
return Sym;
|
2015-09-23 05:24:52 +08:00
|
|
|
}
|
|
|
|
|
2015-10-10 05:12:40 +08:00
|
|
|
template <class ELFT>
|
|
|
|
SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) {
|
|
|
|
auto *Sym = new (Alloc) Undefined<ELFT>(Name, Undefined<ELFT>::Optional);
|
|
|
|
resolve(Sym);
|
|
|
|
return Sym;
|
2015-10-05 17:43:57 +08:00
|
|
|
}
|
|
|
|
|
2015-09-26 02:56:53 +08:00
|
|
|
template <class ELFT>
|
2015-10-10 05:07:25 +08:00
|
|
|
void SymbolTable<ELFT>::addSyntheticSym(StringRef Name,
|
|
|
|
OutputSection<ELFT> &Section,
|
|
|
|
typename ELFFile<ELFT>::uintX_t Value) {
|
2015-09-26 02:56:53 +08:00
|
|
|
typedef typename DefinedSynthetic<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
auto ESym = new (Alloc) Elf_Sym;
|
|
|
|
memset(ESym, 0, sizeof(Elf_Sym));
|
|
|
|
ESym->st_value = Value;
|
|
|
|
auto Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, *ESym, Section);
|
2015-10-10 05:07:25 +08:00
|
|
|
resolve(Sym);
|
2015-09-26 02:56:53 +08:00
|
|
|
}
|
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
template <class ELFT> void SymbolTable<ELFT>::addIgnoredSym(StringRef Name) {
|
2015-10-02 05:22:26 +08:00
|
|
|
auto Sym = new (Alloc)
|
|
|
|
DefinedAbsolute<ELFT>(Name, DefinedAbsolute<ELFT>::IgnoreUndef);
|
2015-10-10 05:07:25 +08:00
|
|
|
resolve(Sym);
|
2015-10-02 05:22:26 +08:00
|
|
|
}
|
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
template <class ELFT> void SymbolTable<ELFT>::addELFFile(ELFFileBase *File) {
|
|
|
|
if (auto *O = dyn_cast<ObjectFile<ELFT>>(File))
|
2015-10-06 23:03:52 +08:00
|
|
|
ObjectFiles.emplace_back(O);
|
|
|
|
else if (auto *S = dyn_cast<SharedFile<ELFT>>(File))
|
|
|
|
SharedFiles.emplace_back(S);
|
|
|
|
|
2015-09-04 08:09:43 +08:00
|
|
|
if (auto *O = dyn_cast<ObjectFileBase>(File)) {
|
|
|
|
for (SymbolBody *Body : O->getSymbols())
|
2015-10-10 05:07:25 +08:00
|
|
|
resolve(Body);
|
2015-09-04 08:09:43 +08:00
|
|
|
}
|
|
|
|
|
2015-09-08 23:50:05 +08:00
|
|
|
if (auto *S = dyn_cast<SharedFile<ELFT>>(File)) {
|
|
|
|
for (SharedSymbol<ELFT> &Body : S->getSharedSymbols())
|
2015-10-10 05:07:25 +08:00
|
|
|
resolve(&Body);
|
2015-08-05 20:03:34 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 22:10:24 +08:00
|
|
|
template <class ELFT>
|
2015-10-10 05:07:25 +08:00
|
|
|
void SymbolTable<ELFT>::reportConflict(const Twine &Message,
|
|
|
|
const SymbolBody &Old,
|
|
|
|
const SymbolBody &New, bool Warning) {
|
2015-09-23 22:10:24 +08:00
|
|
|
typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
|
|
|
|
|
|
|
|
const Elf_Sym &OldE = cast<ELFSymbolBody<ELFT>>(Old).Sym;
|
|
|
|
const Elf_Sym &NewE = cast<ELFSymbolBody<ELFT>>(New).Sym;
|
|
|
|
ELFFileBase *OldFile = nullptr;
|
|
|
|
ELFFileBase *NewFile = nullptr;
|
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
for (const std::unique_ptr<ObjectFile<ELFT>> &File : ObjectFiles) {
|
|
|
|
Elf_Sym_Range Syms = File->getObj().symbols(File->getSymbolTable());
|
2015-09-23 22:10:24 +08:00
|
|
|
if (&OldE > Syms.begin() && &OldE < Syms.end())
|
2015-10-10 05:07:25 +08:00
|
|
|
OldFile = File.get();
|
2015-09-23 22:10:24 +08:00
|
|
|
if (&NewE > Syms.begin() && &NewE < Syms.end())
|
2015-10-10 05:07:25 +08:00
|
|
|
NewFile = File.get();
|
2015-09-23 22:10:24 +08:00
|
|
|
}
|
|
|
|
|
2015-10-09 17:58:39 +08:00
|
|
|
std::string Msg = (Message + ": " + Old.getName() + " in " +
|
2015-09-29 04:30:11 +08:00
|
|
|
OldFile->getName() + " and " + NewFile->getName())
|
|
|
|
.str();
|
2015-10-09 17:58:39 +08:00
|
|
|
if (Warning)
|
2015-09-29 04:30:11 +08:00
|
|
|
warning(Msg);
|
|
|
|
else
|
|
|
|
error(Msg);
|
2015-09-23 22:10:24 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
// This function resolves conflicts if there's an existing symbol with
|
|
|
|
// the same name. Decisions are made based on symbol type.
|
2015-10-10 05:07:25 +08:00
|
|
|
template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) {
|
2015-09-05 06:28:10 +08:00
|
|
|
Symbol *Sym = insert(New);
|
|
|
|
if (Sym->Body == New)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SymbolBody *Existing = Sym->Body;
|
|
|
|
|
|
|
|
if (Lazy *L = dyn_cast<Lazy>(Existing)) {
|
|
|
|
if (New->isUndefined()) {
|
2015-10-06 23:18:50 +08:00
|
|
|
if (New->isWeak()) {
|
|
|
|
// See the explanation in SymbolTable::addLazy
|
|
|
|
L->setUsedInRegularObj();
|
|
|
|
L->setWeak();
|
|
|
|
return;
|
|
|
|
}
|
2015-09-05 06:28:10 +08:00
|
|
|
addMemberFile(L);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found a definition for something also in an archive. Ignore the archive
|
|
|
|
// definition.
|
|
|
|
Sym->Body = New;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:58:39 +08:00
|
|
|
if (New->isTLS() != Existing->isTLS())
|
2015-10-10 05:07:25 +08:00
|
|
|
reportConflict("TLS attribute mismatch for symbol", *Existing, *New, false);
|
2015-10-09 17:58:39 +08:00
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
// compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
|
|
|
|
// equivalent (conflicting), or more preferable, respectively.
|
|
|
|
int comp = Existing->compare<ELFT>(New);
|
|
|
|
if (comp < 0)
|
|
|
|
Sym->Body = New;
|
2015-09-23 22:10:24 +08:00
|
|
|
else if (comp == 0)
|
2015-10-10 05:07:25 +08:00
|
|
|
reportConflict("duplicate symbol", *Existing, *New,
|
|
|
|
Config->AllowMultipleDefinition);
|
2015-09-05 06:28:10 +08:00
|
|
|
}
|
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) {
|
2015-07-25 05:03:07 +08:00
|
|
|
// Find an existing Symbol or create and insert a new one.
|
|
|
|
StringRef Name = New->getName();
|
|
|
|
Symbol *&Sym = Symtab[Name];
|
|
|
|
if (!Sym) {
|
|
|
|
Sym = new (Alloc) Symbol(New);
|
|
|
|
New->setBackref(Sym);
|
2015-09-05 06:28:10 +08:00
|
|
|
return Sym;
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
New->setBackref(Sym);
|
2015-09-05 06:28:10 +08:00
|
|
|
return Sym;
|
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *New) {
|
2015-09-05 06:28:10 +08:00
|
|
|
Symbol *Sym = insert(New);
|
|
|
|
if (Sym->Body == New)
|
|
|
|
return;
|
2015-07-25 05:03:07 +08:00
|
|
|
SymbolBody *Existing = Sym->Body;
|
2015-10-06 22:33:58 +08:00
|
|
|
if (Existing->isDefined() || Existing->isLazy())
|
2015-09-05 06:28:10 +08:00
|
|
|
return;
|
|
|
|
Sym->Body = New;
|
|
|
|
assert(Existing->isUndefined() && "Unexpected symbol kind.");
|
2015-10-06 22:33:58 +08:00
|
|
|
|
|
|
|
// Weak undefined symbols should not fetch members from archives.
|
|
|
|
// If we were to keep old symbol we would not know that an archive member was
|
|
|
|
// available if a strong undefined symbol shows up afterwards in the link.
|
|
|
|
// If a strong undefined symbol never shows up, this lazy symbol will
|
|
|
|
// get to the end of the link and must be treated as the weak undefined one.
|
|
|
|
// We set UsedInRegularObj in a similar way to what is done with shared
|
|
|
|
// symbols and mark it as weak to reduce how many special cases are needed.
|
|
|
|
if (Existing->isWeak()) {
|
|
|
|
New->setUsedInRegularObj();
|
|
|
|
New->setWeak();
|
|
|
|
return;
|
|
|
|
}
|
2015-09-05 06:28:10 +08:00
|
|
|
addMemberFile(New);
|
|
|
|
}
|
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
template <class ELFT> void SymbolTable<ELFT>::addMemberFile(Lazy *Body) {
|
2015-09-05 06:28:10 +08:00
|
|
|
std::unique_ptr<InputFile> File = Body->getMember();
|
|
|
|
|
|
|
|
// getMember returns nullptr if the member was already read from the library.
|
|
|
|
if (!File)
|
|
|
|
return;
|
|
|
|
|
|
|
|
addFile(std::move(File));
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
2015-09-26 02:56:53 +08:00
|
|
|
|
2015-10-10 05:07:25 +08:00
|
|
|
template class lld::elf2::SymbolTable<ELF32LE>;
|
|
|
|
template class lld::elf2::SymbolTable<ELF32BE>;
|
|
|
|
template class lld::elf2::SymbolTable<ELF64LE>;
|
|
|
|
template class lld::elf2::SymbolTable<ELF64BE>;
|