Index: lld/trunk/ELF/Driver.cpp =================================================================== --- lld/trunk/ELF/Driver.cpp +++ lld/trunk/ELF/Driver.cpp @@ -157,6 +157,9 @@ if (Symtab.getObjectFiles().empty()) error("no input files."); + for (auto *Arg : Args.filtered(OPT_undefined)) + Symtab.addUndefinedSym(Arg->getValue()); + // Write the result. const ELFFileBase *FirstObj = Symtab.getFirstELF(); switch (FirstObj->getELFKind()) { Index: lld/trunk/ELF/Options.td =================================================================== --- lld/trunk/ELF/Options.td +++ lld/trunk/ELF/Options.td @@ -61,6 +61,9 @@ def sysroot : Joined<["--"], "sysroot=">, HelpText<"Set the system root">; +def undefined : Joined<["--"], "undefined=">, + HelpText<"Force undefined symbol during linking">; + def whole_archive : Flag<["--"], "whole-archive">, HelpText<"Force load of all members in a static library">; @@ -78,6 +81,7 @@ def alias_rpath_rpath : Joined<["-"], "rpath=">, Alias; def alias_soname_h : Separate<["-"], "h">, Alias; def alias_soname_soname : Separate<["-"], "soname">, Alias; +def alias_undefined_u : Separate<["-"], "u">, Alias; // Options listed below are silently ignored now. def O3 : Flag<["-"], "O3">; Index: lld/trunk/ELF/SymbolTable.h =================================================================== --- lld/trunk/ELF/SymbolTable.h +++ lld/trunk/ELF/SymbolTable.h @@ -61,6 +61,8 @@ return EntrySym->getReplacement(); } + void addUndefinedSym(StringRef Name); + template void addSyntheticSym(StringRef Name, OutputSection &Section, typename llvm::object::ELFFile::uintX_t Value); @@ -73,6 +75,7 @@ void addELFFile(ELFFileBase *File); void addLazy(Lazy *New); void addMemberFile(Lazy *Body); + template void addUndefinedSym(StringRef Name); template void init(uint16_t EMachine); template void resolve(SymbolBody *Body); Index: lld/trunk/ELF/SymbolTable.cpp =================================================================== --- lld/trunk/ELF/SymbolTable.cpp +++ lld/trunk/ELF/SymbolTable.cpp @@ -70,6 +70,29 @@ error("Unknown target machine"); } +void SymbolTable::addUndefinedSym(StringRef Name) { + switch (getFirstELF()->getELFKind()) { + case ELF32LEKind: + addUndefinedSym(Name); + break; + case ELF32BEKind: + addUndefinedSym(Name); + break; + case ELF64LEKind: + addUndefinedSym(Name); + break; + case ELF64BEKind: + addUndefinedSym(Name); + break; + } +} + +template void SymbolTable::addUndefinedSym(StringRef Name) { + Undefined::SyntheticOptional.setVisibility(STV_HIDDEN); + resolve(new (Alloc) + Undefined(Name, Undefined::SyntheticOptional)); +} + template void SymbolTable::addSyntheticSym(StringRef Name, OutputSection &Section, typename ELFFile::uintX_t Value) { @@ -94,7 +117,7 @@ return; EntrySym = new (Alloc) Undefined( Config->Entry.empty() ? Target->getDefaultEntry() : Config->Entry, - Undefined::Synthetic); + Undefined::SyntheticRequired); resolve(EntrySym); // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol is magical Index: lld/trunk/ELF/Symbols.h =================================================================== --- lld/trunk/ELF/Symbols.h +++ lld/trunk/ELF/Symbols.h @@ -53,7 +53,6 @@ bool isWeak() const { return IsWeak; } bool isUndefined() const { return SymbolKind == UndefinedKind; } bool isDefined() const { return SymbolKind <= DefinedLast; } - bool isStrongUndefined() const { return !IsWeak && isUndefined(); } bool isCommon() const { return SymbolKind == DefinedCommonKind; } bool isLazy() const { return SymbolKind == LazyKind; } bool isShared() const { return SymbolKind == SharedKind; } @@ -235,7 +234,8 @@ typedef typename Base::Elf_Sym Elf_Sym; public: - static Elf_Sym Synthetic; + static Elf_Sym SyntheticRequired; + static Elf_Sym SyntheticOptional; Undefined(StringRef N, const Elf_Sym &Sym) : ELFSymbolBody(Base::UndefinedKind, N, Sym) {} @@ -243,10 +243,14 @@ static bool classof(const SymbolBody *S) { return S->kind() == Base::UndefinedKind; } + + bool canKeepUndefined() const { return &this->Sym == &SyntheticOptional; } }; template -typename Undefined::Elf_Sym Undefined::Synthetic; +typename Undefined::Elf_Sym Undefined::SyntheticRequired; +template +typename Undefined::Elf_Sym Undefined::SyntheticOptional; template class SharedSymbol : public Defined { typedef Defined Base; Index: lld/trunk/ELF/Writer.cpp =================================================================== --- lld/trunk/ELF/Writer.cpp +++ lld/trunk/ELF/Writer.cpp @@ -334,8 +334,10 @@ for (auto &P : Symtab.getSymbols()) { StringRef Name = P.first; SymbolBody *Body = P.second->Body; - if (Body->isStrongUndefined()) - reportUndefined(Symtab, *Body); + if (auto *U = dyn_cast>(Body)) { + if (!U->isWeak() && !U->canKeepUndefined()) + reportUndefined(Symtab, *Body); + } if (auto *C = dyn_cast>(Body)) CommonSymbols.push_back(C); Index: lld/trunk/test/elf2/undefined-opt.s =================================================================== --- lld/trunk/test/elf2/undefined-opt.s +++ lld/trunk/test/elf2/undefined-opt.s @@ -0,0 +1,51 @@ +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \ +# RUN: %p/Inputs/abs.s -o %tabs.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \ +# RUN: %p/Inputs/shared.s -o %tshared.o +# RUN: rm -f %tar.a +# RUN: llvm-ar rcs %tar.a %tabs.o %tshared.o +# REQUIRES: x86 + +# Symbols from the archive are not in if not needed +# RUN: lld -flavor gnu2 -o %t1 %t.o %tar.a +# RUN: llvm-readobj --symbols %t1 | FileCheck --check-prefix=NO-UNDEFINED %s +# NO-UNDEFINED: Symbols [ +# NO-UNDEFINED-NOT: Name: abs +# NO-UNDEFINED-NOT: Name: big +# NO-UNDEFINED-NOT: Name: bar +# NO-UNDEFINED-NOT: Name: zed +# NO-UNDEFINED: ] + +# Symbols from the archive are in if needed, but only from the +# containing object file +# RUN: lld -flavor gnu2 -o %t2 %t.o %tar.a -u bar +# RUN: llvm-readobj --symbols %t2 | FileCheck --check-prefix=ONE-UNDEFINED %s +# ONE-UNDEFINED: Symbols [ +# ONE-UNDEFINED-NOT: Name: abs +# ONE-UNDEFINED-NOT: Name: big +# ONE-UNDEFINED: Name: bar +# ONE-UNDEFINED: Name: zed +# ONE-UNDEFINED: ] + +# Use the option couple of times, both short and long forms +# RUN: lld -flavor gnu2 -o %t3 %t.o %tar.a -u bar --undefined=abs +# RUN: llvm-readobj --symbols %t3 | FileCheck --check-prefix=TWO-UNDEFINED %s +# TWO-UNDEFINED: Symbols [ +# TWO-UNDEFINED: Name: abs +# TWO-UNDEFINED: Name: big +# TWO-UNDEFINED: Name: bar +# TWO-UNDEFINED: Name: zed +# TWO-UNDEFINED: ] + +# Added undefined symbol may be left undefined without error, but +# shouldn't show up in the dynamic table. +# RUN: lld -flavor gnu2 -shared -o %t4 %t.o %tar.a -u unknown +# RUN: llvm-readobj --dyn-symbols %t4 | \ +# RUN: FileCheck --check-prefix=UNK-UNDEFINED-SO %s +# UNK-UNDEFINED-SO: DynamicSymbols [ +# UNK-UNDEFINED-SO-NOT: Name: unknown +# UNK-UNDEFINED-SO: ] + +.globl _start; +_start: