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
|
2016-01-06 04:47:37 +08:00
|
|
|
// all input files to the symbol table. The symbol table is basically
|
2015-10-14 03:51:57 +08:00
|
|
|
// 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"
|
2016-01-08 01:20:07 +08:00
|
|
|
#include "llvm/Support/StringSaver.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-01-06 04:47:37 +08:00
|
|
|
// All input object files must be for the same architecture
|
|
|
|
// (e.g. it does not make sense to link x86 object files with
|
|
|
|
// MIPS object files.) This function checks for that error.
|
2015-12-17 07:31:22 +08:00
|
|
|
template <class ELFT>
|
|
|
|
static void checkCompatibility(InputFile *FileP) {
|
|
|
|
auto *F = dyn_cast<ELFFileBase<ELFT>>(FileP);
|
|
|
|
if (!F)
|
|
|
|
return;
|
|
|
|
if (F->getELFKind() == Config->EKind && F->getEMachine() == Config->EMachine)
|
|
|
|
return;
|
|
|
|
StringRef A = F->getName();
|
|
|
|
StringRef B = Config->Emulation;
|
|
|
|
if (B.empty())
|
|
|
|
B = Config->FirstElf->getName();
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal(A + " is incompatible with " + B);
|
2015-12-17 07:31:22 +08:00
|
|
|
}
|
|
|
|
|
2016-01-06 04:47:37 +08:00
|
|
|
// Add symbols in File to the symbol table.
|
2015-10-10 05:07:25 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
|
2015-12-23 22:35:51 +08:00
|
|
|
InputFile *FileP = File.get();
|
2015-12-17 07:31:22 +08:00
|
|
|
checkCompatibility<ELFT>(FileP);
|
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)) {
|
2015-12-23 22:35:51 +08:00
|
|
|
ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release()));
|
2015-12-17 06:59:13 +08:00
|
|
|
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();
|
2016-01-09 06:17:42 +08:00
|
|
|
if (!SoNames.insert(F->getSoName()).second)
|
2015-10-02 03:52:48 +08:00
|
|
|
return;
|
2015-12-17 06:59:13 +08:00
|
|
|
|
2015-12-23 22:35:51 +08:00
|
|
|
SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release()));
|
2016-01-06 09:56:36 +08:00
|
|
|
F->parseRest();
|
2015-12-17 06:59:13 +08:00
|
|
|
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);
|
2015-12-23 22:35:51 +08:00
|
|
|
ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release()));
|
2016-01-06 10:06:33 +08:00
|
|
|
F->parse(ComdatGroups);
|
2015-12-17 06:59:13 +08:00
|
|
|
for (SymbolBody *B : F->getSymbols())
|
|
|
|
resolve(B);
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-12-24 18:37:32 +08:00
|
|
|
// Add an undefined symbol.
|
2015-10-10 05:12:40 +08:00
|
|
|
template <class ELFT>
|
|
|
|
SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) {
|
2015-12-23 07:00:50 +08:00
|
|
|
auto *Sym = new (Alloc) Undefined(Name, false, STV_DEFAULT, false);
|
2015-10-10 05:12:40 +08:00
|
|
|
resolve(Sym);
|
|
|
|
return Sym;
|
2015-09-23 05:24:52 +08:00
|
|
|
}
|
|
|
|
|
2015-12-24 18:37:32 +08:00
|
|
|
// Add an undefined symbol. Unlike addUndefined, that symbol
|
|
|
|
// doesn't have to be resolved, thus "opt" (optional).
|
2015-10-10 05:12:40 +08:00
|
|
|
template <class ELFT>
|
|
|
|
SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) {
|
2015-12-23 07:00:50 +08:00
|
|
|
auto *Sym = new (Alloc) Undefined(Name, false, STV_HIDDEN, true);
|
2015-10-10 05:12:40 +08:00
|
|
|
resolve(Sym);
|
|
|
|
return Sym;
|
2015-10-05 17:43:57 +08:00
|
|
|
}
|
|
|
|
|
2015-11-06 15:43:03 +08:00
|
|
|
template <class ELFT>
|
2016-01-09 05:53:28 +08:00
|
|
|
SymbolBody *SymbolTable<ELFT>::addAbsolute(StringRef Name, Elf_Sym &ESym) {
|
|
|
|
// Pass nullptr because absolute symbols have no corresponding input sections.
|
|
|
|
auto *Sym = new (Alloc) DefinedRegular<ELFT>(Name, ESym, nullptr);
|
|
|
|
resolve(Sym);
|
|
|
|
return Sym;
|
2015-11-06 15:43:03 +08:00
|
|
|
}
|
|
|
|
|
2015-09-26 02:56:53 +08:00
|
|
|
template <class ELFT>
|
2016-01-09 05:53:28 +08:00
|
|
|
SymbolBody *SymbolTable<ELFT>::addSynthetic(StringRef Name,
|
|
|
|
OutputSectionBase<ELFT> &Section,
|
|
|
|
uintX_t Value) {
|
2015-12-24 08:47:42 +08:00
|
|
|
auto *Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, Value, Section);
|
2015-10-10 05:07:25 +08:00
|
|
|
resolve(Sym);
|
2016-01-09 05:53:28 +08:00
|
|
|
return Sym;
|
2015-09-26 02:56:53 +08:00
|
|
|
}
|
|
|
|
|
2016-01-06 04:47:37 +08:00
|
|
|
// Add Name as an "ignored" symbol. An ignored symbol is a regular
|
|
|
|
// linker-synthesized defined symbol, but it is not recorded to the output
|
|
|
|
// file's symbol table. Such symbols are useful for some linker-defined symbols.
|
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) {
|
2016-01-20 05:19:52 +08:00
|
|
|
return addAbsolute(Name, ElfSym<ELFT>::Ignored);
|
2015-10-02 05:22:26 +08:00
|
|
|
}
|
|
|
|
|
2016-01-08 01:20:07 +08:00
|
|
|
// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
|
|
|
|
// Used to implement --wrap.
|
|
|
|
template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
|
|
|
|
if (Symtab.count(Name) == 0)
|
|
|
|
return;
|
|
|
|
StringSaver Saver(Alloc);
|
|
|
|
Symbol *Sym = addUndefined(Name)->getSymbol();
|
|
|
|
Symbol *Real = addUndefined(Saver.save("__real_" + Name))->getSymbol();
|
|
|
|
Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name))->getSymbol();
|
|
|
|
Real->Body = Sym->Body;
|
|
|
|
Sym->Body = Wrap->Body;
|
|
|
|
}
|
|
|
|
|
2015-12-17 06:26:48 +08:00
|
|
|
// Returns a file from which symbol B was created.
|
2016-01-06 04:01:29 +08:00
|
|
|
// If B does not belong to any file, returns a nullptr.
|
2015-09-23 22:10:24 +08:00
|
|
|
template <class ELFT>
|
2016-01-06 04:01:29 +08:00
|
|
|
ELFFileBase<ELFT> *SymbolTable<ELFT>::findFile(SymbolBody *B) {
|
2015-12-17 06:26:48 +08:00
|
|
|
for (const std::unique_ptr<ObjectFile<ELFT>> &F : ObjectFiles) {
|
2015-12-23 07:00:50 +08:00
|
|
|
ArrayRef<SymbolBody *> Syms = F->getSymbols();
|
|
|
|
if (std::find(Syms.begin(), Syms.end(), B) != Syms.end())
|
2015-12-17 06:26:48 +08:00
|
|
|
return F.get();
|
2015-09-23 22:10:24 +08:00
|
|
|
}
|
2015-12-17 06:26:48 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-01-09 06:01:33 +08:00
|
|
|
// Construct a string in the form of "Sym in File1 and File2".
|
|
|
|
// Used to construct an error message.
|
2015-12-17 06:26:48 +08:00
|
|
|
template <class ELFT>
|
|
|
|
std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Old, SymbolBody *New) {
|
2016-01-06 04:01:29 +08:00
|
|
|
ELFFileBase<ELFT> *OldFile = findFile(Old);
|
|
|
|
ELFFileBase<ELFT> *NewFile = findFile(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)";
|
2016-01-14 02:55:39 +08:00
|
|
|
return (demangle(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-23 07:00:50 +08:00
|
|
|
if (auto *Undef = dyn_cast<Undefined>(New)) {
|
2015-12-17 07:23:14 +08:00
|
|
|
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-12-17 08:04:18 +08:00
|
|
|
if (New->isTls() != Existing->isTls())
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("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.
|
2016-01-06 08:09:39 +08:00
|
|
|
int Comp = Existing->compare<ELFT>(New);
|
|
|
|
if (Comp == 0) {
|
2015-12-17 06:26:45 +08:00
|
|
|
std::string S = "duplicate symbol: " + conflictMsg(Existing, New);
|
|
|
|
if (!Config->AllowMultipleDefinition)
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal(S);
|
2015-12-17 06:26:45 +08:00
|
|
|
warning(S);
|
|
|
|
return;
|
|
|
|
}
|
2016-01-06 08:09:39 +08:00
|
|
|
if (Comp < 0)
|
2015-09-05 06:28:10 +08:00
|
|
|
Sym->Body = New;
|
|
|
|
}
|
|
|
|
|
2016-01-09 06:01:33 +08:00
|
|
|
// Find an existing symbol or create and insert a new one.
|
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
|
|
|
StringRef Name = New->getName();
|
|
|
|
Symbol *&Sym = Symtab[Name];
|
2015-12-17 07:25:31 +08:00
|
|
|
if (!Sym)
|
2015-12-17 08:01:25 +08:00
|
|
|
Sym = new (Alloc) Symbol{New};
|
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-23 07:00:50 +08:00
|
|
|
if (auto *Undef = dyn_cast<Undefined>(Sym->Body)) {
|
2015-12-17 07:23:14 +08:00
|
|
|
Sym->Body = L;
|
|
|
|
addMemberFile(Undef, L);
|
2015-10-06 22:33:58 +08:00
|
|
|
}
|
2015-09-05 06:28:10 +08:00
|
|
|
}
|
|
|
|
|
2015-12-17 07:23:14 +08:00
|
|
|
template <class ELFT>
|
2015-12-23 07:00:50 +08:00
|
|
|
void SymbolTable<ELFT>::addMemberFile(Undefined *Undef, Lazy *L) {
|
2015-12-17 07:23:14 +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 (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();
|
|
|
|
}
|
|
|
|
|
2016-01-06 08:09:41 +08:00
|
|
|
template class elf2::SymbolTable<ELF32LE>;
|
|
|
|
template class elf2::SymbolTable<ELF32BE>;
|
|
|
|
template class elf2::SymbolTable<ELF64LE>;
|
|
|
|
template class elf2::SymbolTable<ELF64BE>;
|