Skip to content

Commit

Permalink
ELF: Re-implement -u directly and remove CanKeepUndefined flag.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pcc committed Apr 27, 2016
1 parent e7d1e99 commit 892d498
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 31 deletions.
4 changes: 1 addition & 3 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 11 additions & 11 deletions lld/ELF/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lld/ELF/SymbolTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ template <class ELFT> class SymbolTable {
DefinedRegular<ELFT> *addIgnored(StringRef Name,
uint8_t Visibility = llvm::ELF::STV_HIDDEN);

void scanUndefinedFlags();
void scanShlibUndefined();
void scanDynamicList();
void scanVersionScript();
Expand Down
8 changes: 2 additions & 6 deletions lld/ELF/Symbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions lld/ELF/Symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ class SymbolBody {
// 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
Expand Down Expand Up @@ -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;

Expand Down
7 changes: 2 additions & 5 deletions lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions lld/test/ELF/undefined-opt.s
Original file line number Diff line number Diff line change
Expand Up @@ -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:

0 comments on commit 892d498

Please sign in to comment.