ELF: Re-implement -u directly and remove CanKeepUndefined flag.

The semantics of the -u flag are to load the lazy symbol named by the flag. We
were previously relying on this behavior falling out of symbol resolution
against a synthetic undefined symbol, but that didn't quite give us the
correct behavior, so we needed a flag to mark symbols created with -u so
we could treat them specially in the writer. However, it's simpler and less
error prone to implement the required behavior directly and remove the flag.

This fixes an issue where symbols loaded with -u would receive hidden
visibility even when the definition in an object file had wider visibility.

Differential Revision: http://reviews.llvm.org/D19560

llvm-svn: 267639
This commit is contained in:
Peter Collingbourne 2016-04-27 00:05:03 +00:00
parent e7d1e99543
commit 892d498017
7 changed files with 29 additions and 31 deletions

View File

@ -483,9 +483,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
if (HasError)
return; // There were duplicate symbols or incompatible files
for (StringRef S : Config->Undefined)
Symtab.addUndefinedOpt(S);
Symtab.scanUndefinedFlags();
Symtab.scanShlibUndefined();
Symtab.scanDynamicList();
Symtab.scanVersionScript();

View File

@ -151,17 +151,7 @@ template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
template <class ELFT>
SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) {
auto *Sym = new (Alloc)
UndefinedElf<ELFT>(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0, false);
resolve(Sym);
return Sym;
}
// Add an undefined symbol. Unlike addUndefined, that symbol
// doesn't have to be resolved, thus "opt" (optional).
template <class ELFT>
SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) {
auto *Sym = new (Alloc)
UndefinedElf<ELFT>(Name, STB_GLOBAL, STV_HIDDEN, /*Type*/ 0, true);
UndefinedElf<ELFT>(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0);
resolve(Sym);
return Sym;
}
@ -368,6 +358,16 @@ void SymbolTable<ELFT>::addMemberFile(SymbolBody *Undef, Lazy *L) {
addFile(std::move(File));
}
// Process undefined (-u) flags by loading lazy symbols named by those flags.
template <class ELFT>
void SymbolTable<ELFT>::scanUndefinedFlags() {
for (StringRef S : Config->Undefined)
if (SymbolBody *Sym = find(S))
if (auto *L = dyn_cast<Lazy>(Sym))
if (std::unique_ptr<InputFile> File = L->getFile())
addFile(std::move(File));
}
// 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

View File

@ -59,6 +59,7 @@ public:
DefinedRegular<ELFT> *addIgnored(StringRef Name,
uint8_t Visibility = llvm::ELF::STV_HIDDEN);
void scanUndefinedFlags();
void scanShlibUndefined();
void scanDynamicList();
void scanVersionScript();

View File

@ -104,7 +104,6 @@ SymbolBody::SymbolBody(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther,
}
void SymbolBody::init() {
CanKeepUndefined = false;
NeedsCopyOrPltAddr = false;
CanOmitFromDynSym = false;
}
@ -245,11 +244,8 @@ UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
template <typename ELFT>
UndefinedElf<ELFT>::UndefinedElf(StringRef Name, uint8_t Binding,
uint8_t StOther, uint8_t Type,
bool CanKeepUndefined)
: SymbolBody(SymbolBody::UndefinedElfKind, Name, Binding, StOther, Type) {
this->CanKeepUndefined = CanKeepUndefined;
}
uint8_t StOther, uint8_t Type)
: SymbolBody(SymbolBody::UndefinedElfKind, Name, Binding, StOther, Type) {}
template <typename ELFT>
UndefinedElf<ELFT>::UndefinedElf(const Elf_Sym &Sym)

View File

@ -177,8 +177,6 @@ public:
// symbol or if the symbol should point to its plt entry.
unsigned NeedsCopyOrPltAddr : 1;
unsigned CanKeepUndefined : 1;
// The following fields have the same meaning as the ELF symbol attributes.
uint8_t Type; // symbol type
uint8_t Binding; // symbol binding
@ -325,10 +323,7 @@ template <class ELFT> class UndefinedElf : public SymbolBody {
public:
UndefinedElf(StringRef N, const Elf_Sym &Sym);
UndefinedElf(const Elf_Sym &Sym);
UndefinedElf(StringRef Name, uint8_t Binding, uint8_t StOther, uint8_t Type,
bool CanKeepUndefined);
bool canKeepUndefined() const { return CanKeepUndefined; }
UndefinedElf(StringRef Name, uint8_t Binding, uint8_t StOther, uint8_t Type);
uintX_t Size;

View File

@ -1339,11 +1339,8 @@ template <class ELFT> void Writer<ELFT>::createSections() {
if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(Body))
SS->File->IsUsed = true;
if (Body->isUndefined() && !S->isWeak()) {
auto *U = dyn_cast<UndefinedElf<ELFT>>(Body);
if (!U || !U->canKeepUndefined())
reportUndefined<ELFT>(Symtab, Body);
}
if (Body->isUndefined() && !S->isWeak())
reportUndefined<ELFT>(Symtab, Body);
if (auto *C = dyn_cast<DefinedCommon>(Body))
CommonSymbols.push_back(C);

View File

@ -51,5 +51,16 @@
# UNK-UNDEFINED-SO-NOT: Name: unknown
# UNK-UNDEFINED-SO: ]
# Added undefined symbols should appear in the dynamic table if necessary.
# RUN: ld.lld -shared -o %t5 %t.o -u export
# RUN: llvm-readobj --dyn-symbols %t5 | \
# RUN: FileCheck --check-prefix=EXPORT-SO %s
# EXPORT-SO: DynamicSymbols [
# EXPORT-SO: Name: export
# EXPORT-SO: ]
.globl _start;
_start:
.globl export
export: