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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-10-14 03:51:57 +08:00
|
|
|
//
|
|
|
|
// Symbol table is a bag of all known symbols. We put all symbols of
|
|
|
|
// all input files to the symbol table. The symbol Table is basically
|
|
|
|
// a hash table with the logic to resolve symbol name conflicts using
|
|
|
|
// the symbol types.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
#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-10-12 19:52:31 +08:00
|
|
|
ELFKind K = cast<ELFFileBase<ELFT>>(Config->FirstElf)->getELFKind();
|
2015-09-17 22:02:10 +08:00
|
|
|
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-13 02:03:21 +08:00
|
|
|
checkCompatibility(File);
|
2015-12-17 06:59:13 +08:00
|
|
|
InputFile *FileP = File.release();
|
2015-10-11 11:36:49 +08:00
|
|
|
|
2015-12-17 06:59:13 +08:00
|
|
|
// .a file
|
|
|
|
if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
|
|
|
|
ArchiveFiles.emplace_back(F);
|
|
|
|
F->parse();
|
|
|
|
for (Lazy &Sym : F->getLazySymbols())
|
2015-09-05 06:28:10 +08:00
|
|
|
addLazy(&Sym);
|
|
|
|
return;
|
|
|
|
}
|
2015-10-13 02:03:21 +08:00
|
|
|
|
2015-12-17 06:59:13 +08:00
|
|
|
// .so file
|
|
|
|
if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
|
|
|
|
// DSOs are uniquified not by filename but by soname.
|
|
|
|
F->parseSoName();
|
|
|
|
if (!IncludedSoNames.insert(F->getSoName()).second)
|
2015-10-02 03:52:48 +08:00
|
|
|
return;
|
2015-12-17 06:59:13 +08:00
|
|
|
|
|
|
|
SharedFiles.emplace_back(F);
|
|
|
|
F->parse();
|
|
|
|
for (SharedSymbol<ELFT> &B : F->getSharedSymbols())
|
|
|
|
resolve(&B);
|
|
|
|
return;
|
2015-10-02 03:52:48 +08:00
|
|
|
}
|
2015-12-17 06:59:13 +08:00
|
|
|
|
|
|
|
// .o file
|
|
|
|
auto *F = cast<ObjectFile<ELFT>>(FileP);
|
|
|
|
ObjectFiles.emplace_back(F);
|
|
|
|
F->parse(Comdats);
|
|
|
|
for (SymbolBody *B : F->getSymbols())
|
|
|
|
resolve(B);
|
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-11-06 15:43:03 +08:00
|
|
|
template <class ELFT>
|
2015-12-17 06:31:14 +08:00
|
|
|
void SymbolTable<ELFT>::addAbsolute(StringRef Name,
|
|
|
|
typename ELFFile<ELFT>::Elf_Sym &ESym) {
|
2015-11-06 15:43:03 +08:00
|
|
|
resolve(new (Alloc) DefinedAbsolute<ELFT>(Name, ESym));
|
|
|
|
}
|
|
|
|
|
2015-09-26 02:56:53 +08:00
|
|
|
template <class ELFT>
|
2015-12-17 06:31:14 +08:00
|
|
|
void SymbolTable<ELFT>::addSynthetic(StringRef Name,
|
|
|
|
OutputSectionBase<ELFT> &Section,
|
|
|
|
typename ELFFile<ELFT>::uintX_t Value) {
|
2015-09-26 02:56:53 +08:00
|
|
|
typedef typename DefinedSynthetic<ELFT>::Elf_Sym Elf_Sym;
|
2015-12-17 06:36:10 +08:00
|
|
|
auto *ESym = new (Alloc) Elf_Sym;
|
2015-09-26 02:56:53 +08:00
|
|
|
memset(ESym, 0, sizeof(Elf_Sym));
|
|
|
|
ESym->st_value = Value;
|
2015-12-17 06:36:10 +08:00
|
|
|
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-12-16 22:45:09 +08:00
|
|
|
template <class ELFT>
|
2015-12-17 06:31:14 +08:00
|
|
|
SymbolBody *SymbolTable<ELFT>::addIgnored(StringRef Name) {
|
2015-12-17 06:36:10 +08:00
|
|
|
auto *Sym = new (Alloc)
|
2015-10-02 05:22:26 +08:00
|
|
|
DefinedAbsolute<ELFT>(Name, DefinedAbsolute<ELFT>::IgnoreUndef);
|
2015-10-10 05:07:25 +08:00
|
|
|
resolve(Sym);
|
2015-12-16 22:45:09 +08:00
|
|
|
return Sym;
|
2015-10-02 05:22:26 +08:00
|
|
|
}
|
|
|
|
|
2015-10-16 01:11:03 +08:00
|
|
|
template <class ELFT> bool SymbolTable<ELFT>::isUndefined(StringRef Name) {
|
|
|
|
if (SymbolBody *Sym = find(Name))
|
|
|
|
return Sym->isUndefined();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-17 06:26:48 +08:00
|
|
|
// Returns a file from which symbol B was created.
|
|
|
|
// If B does not belong to any file in ObjectFiles, returns a nullptr.
|
2015-09-23 22:10:24 +08:00
|
|
|
template <class ELFT>
|
2015-12-17 06:26:48 +08:00
|
|
|
static ELFFileBase<ELFT> *
|
|
|
|
findFile(std::vector<std::unique_ptr<ObjectFile<ELFT>>> &ObjectFiles,
|
|
|
|
SymbolBody *B) {
|
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;
|
|
|
|
|
2015-12-17 06:26:48 +08:00
|
|
|
const Elf_Sym *Sym = &cast<ELFSymbolBody<ELFT>>(*B).Sym;
|
|
|
|
for (const std::unique_ptr<ObjectFile<ELFT>> &F : ObjectFiles) {
|
|
|
|
Elf_Sym_Range R = F->getObj().symbols(F->getSymbolTable());
|
|
|
|
if (R.begin() <= Sym && Sym < R.end())
|
|
|
|
return F.get();
|
2015-09-23 22:10:24 +08:00
|
|
|
}
|
2015-12-17 06:26:48 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Old, SymbolBody *New) {
|
|
|
|
ELFFileBase<ELFT> *OldFile = findFile(ObjectFiles, Old);
|
|
|
|
ELFFileBase<ELFT> *NewFile = findFile(ObjectFiles, New);
|
2015-09-23 22:10:24 +08:00
|
|
|
|
2015-12-17 06:26:45 +08:00
|
|
|
StringRef Sym = Old->getName();
|
|
|
|
StringRef F1 = OldFile ? OldFile->getName() : "(internal)";
|
|
|
|
StringRef F2 = NewFile ? NewFile->getName() : "(internal)";
|
|
|
|
return (Sym + " in " + F1 + " and " + F2).str();
|
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)) {
|
2015-12-17 07:23:14 +08:00
|
|
|
if (auto *Undef = dyn_cast<Undefined<ELFT>>(New)) {
|
|
|
|
addMemberFile(Undef, L);
|
2015-09-05 06:28:10 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-12-17 07:23:14 +08:00
|
|
|
// Found a definition for something also in an archive.
|
|
|
|
// Ignore the archive definition.
|
2015-09-05 06:28:10 +08:00
|
|
|
Sym->Body = New;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:58:39 +08:00
|
|
|
if (New->isTLS() != Existing->isTLS())
|
2015-12-17 06:26:45 +08:00
|
|
|
error("TLS attribute mismatch for symbol: " + conflictMsg(Existing, New));
|
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);
|
2015-12-17 06:26:45 +08:00
|
|
|
if (comp == 0) {
|
|
|
|
std::string S = "duplicate symbol: " + conflictMsg(Existing, New);
|
|
|
|
if (!Config->AllowMultipleDefinition)
|
|
|
|
error(S);
|
|
|
|
warning(S);
|
|
|
|
return;
|
|
|
|
}
|
2015-09-05 06:28:10 +08:00
|
|
|
if (comp < 0)
|
|
|
|
Sym->Body = New;
|
|
|
|
}
|
|
|
|
|
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-14 00:34:14 +08:00
|
|
|
template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
|
|
|
|
auto It = Symtab.find(Name);
|
|
|
|
if (It == Symtab.end())
|
|
|
|
return nullptr;
|
|
|
|
return It->second->Body;
|
|
|
|
}
|
|
|
|
|
2015-12-17 07:23:14 +08:00
|
|
|
template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *L) {
|
|
|
|
Symbol *Sym = insert(L);
|
|
|
|
if (Sym->Body == L)
|
2015-10-06 22:33:58 +08:00
|
|
|
return;
|
2015-12-17 07:23:14 +08:00
|
|
|
if (auto *Undef = dyn_cast<Undefined<ELFT>>(Sym->Body)) {
|
|
|
|
Sym->Body = L;
|
|
|
|
addMemberFile(Undef, L);
|
2015-10-06 22:33:58 +08:00
|
|
|
}
|
2015-09-05 06:28:10 +08:00
|
|
|
}
|
|
|
|
|
2015-10-13 02:03:21 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void SymbolTable<ELFT>::checkCompatibility(std::unique_ptr<InputFile> &File) {
|
|
|
|
auto *E = dyn_cast<ELFFileBase<ELFT>>(File.get());
|
|
|
|
if (!E)
|
|
|
|
return;
|
2015-10-14 00:20:50 +08:00
|
|
|
if (E->getELFKind() == Config->EKind && E->getEMachine() == Config->EMachine)
|
2015-10-13 02:03:21 +08:00
|
|
|
return;
|
|
|
|
StringRef A = E->getName();
|
|
|
|
StringRef B = Config->Emulation;
|
|
|
|
if (B.empty())
|
|
|
|
B = Config->FirstElf->getName();
|
|
|
|
error(A + " is incompatible with " + B);
|
|
|
|
}
|
|
|
|
|
2015-12-17 07:23:14 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void SymbolTable<ELFT>::addMemberFile(Undefined<ELFT> *Undef, Lazy *L) {
|
|
|
|
// 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 (Undef->isWeak()) {
|
|
|
|
L->setUsedInRegularObj();
|
|
|
|
L->setWeak();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch a member file that has the definition for L.
|
2015-09-05 06:28:10 +08:00
|
|
|
// getMember returns nullptr if the member was already read from the library.
|
2015-12-17 07:23:14 +08:00
|
|
|
if (std::unique_ptr<InputFile> File = L->getMember())
|
2015-10-15 06:32:10 +08:00
|
|
|
addFile(std::move(File));
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
2015-09-26 02:56:53 +08:00
|
|
|
|
2015-10-14 02:10:33 +08:00
|
|
|
// This function takes care of the case in which shared libraries depend on
|
|
|
|
// the user program (not the other way, which is usual). Shared libraries
|
|
|
|
// may have undefined symbols, expecting that the user program provides
|
|
|
|
// the definitions for them. An example is BSD's __progname symbol.
|
|
|
|
// We need to put such symbols to the main program's .dynsym so that
|
|
|
|
// shared libraries can find them.
|
|
|
|
// Except this, we ignore undefined symbols in DSOs.
|
|
|
|
template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
|
2015-10-14 00:34:14 +08:00
|
|
|
for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
|
|
|
|
for (StringRef U : File->getUndefinedSymbols())
|
|
|
|
if (SymbolBody *Sym = find(U))
|
|
|
|
if (Sym->isDefined())
|
|
|
|
Sym->setUsedInDynamicReloc();
|
|
|
|
}
|
|
|
|
|
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>;
|