Index: ELF/Config.h =================================================================== --- ELF/Config.h +++ ELF/Config.h @@ -53,6 +53,7 @@ bool AllowMultipleDefinition; bool AsNeeded = false; bool Bsymbolic; + bool BsymbolicFunctions; bool Demangle = true; bool DiscardAll; bool DiscardLocals; Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -185,6 +185,7 @@ Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition); Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic); + Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_functions); Config->Demangle = !Args.hasArg(OPT_no_demangle); Config->DiscardAll = Args.hasArg(OPT_discard_all); Config->DiscardLocals = Args.hasArg(OPT_discard_locals); Index: ELF/Options.td =================================================================== --- ELF/Options.td +++ ELF/Options.td @@ -3,6 +3,9 @@ def Bsymbolic: Flag<["-"], "Bsymbolic">, HelpText<"Bind defined symbols locally">; +def Bsymbolic_functions: Flag<["-"], "Bsymbolic-functions">, + HelpText<"Bind defined function symbols locally">; + def Bdynamic: Flag<["-"], "Bdynamic">, HelpText<"Link against shared libraries">; Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -1024,9 +1024,11 @@ // user knows what he or she is doing and can handle a dynamic relocation. return Config->Shared || NeedsGot; } - if (!Config->Shared) + if (!Config->Shared || Body->getVisibility() != STV_DEFAULT) return false; - return Body->getVisibility() == STV_DEFAULT; + if (Config->Bsymbolic || (Config->BsymbolicFunctions && Body->isFunction())) + return false; + return true; } template void OutputSection::writeTo(uint8_t *Buf) { Index: ELF/Symbols.h =================================================================== --- ELF/Symbols.h +++ ELF/Symbols.h @@ -86,6 +86,7 @@ bool isUsedInDynamicReloc() const { return IsUsedInDynamicReloc; } void setUsedInDynamicReloc() { IsUsedInDynamicReloc = true; } bool isTls() const { return IsTls; } + bool isFunction() const { return IsFunction; } // Returns the symbol name. StringRef getName() const { return Name; } @@ -119,9 +120,9 @@ protected: SymbolBody(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility, - bool IsTls) + bool IsTls, bool IsFunction) : SymbolKind(K), IsWeak(IsWeak), Visibility(Visibility), IsTls(IsTls), - Name(Name) { + IsFunction(IsFunction), Name(Name) { IsUsedInRegularObj = K != SharedKind && K != LazyKind; IsUsedInDynamicReloc = 0; } @@ -140,6 +141,7 @@ unsigned IsUsedInDynamicReloc : 1; unsigned IsTls : 1; + unsigned IsFunction : 1; StringRef Name; Symbol *Backref = nullptr; }; @@ -147,7 +149,8 @@ // The base class for any defined symbols. class Defined : public SymbolBody { public: - Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility, bool IsTls); + Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility, bool IsTls, + bool IsFunction); static bool classof(const SymbolBody *S) { return S->isDefined(); } }; @@ -159,7 +162,8 @@ public: DefinedElf(Kind K, StringRef N, const Elf_Sym &Sym) : Defined(K, N, Sym.getBinding() == llvm::ELF::STB_WEAK, - Sym.getVisibility(), Sym.getType() == llvm::ELF::STT_TLS), + Sym.getVisibility(), Sym.getType() == llvm::ELF::STT_TLS, + Sym.getType() == llvm::ELF::STT_FUNC), Sym(Sym) {} const Elf_Sym &Sym; @@ -230,7 +234,8 @@ bool CanKeepUndefined; protected: - Undefined(Kind K, StringRef N, bool IsWeak, uint8_t Visibility, bool IsTls); + Undefined(Kind K, StringRef N, bool IsWeak, uint8_t Visibility, bool IsTls, + bool IsFunction); public: Undefined(StringRef N, bool IsWeak, uint8_t Visibility, @@ -281,7 +286,8 @@ class Lazy : public SymbolBody { public: Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S) - : SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT, false), + : SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT, + /*IsTls*/ false, /*IsFunction*/ false), File(F), Sym(S) {} static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; } Index: ELF/Symbols.cpp =================================================================== --- ELF/Symbols.cpp +++ ELF/Symbols.cpp @@ -76,17 +76,18 @@ } Defined::Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility, - bool IsTls) - : SymbolBody(K, Name, IsWeak, Visibility, IsTls) {} + bool IsTls, bool IsFunction) + : SymbolBody(K, Name, IsWeak, Visibility, IsTls, IsFunction) {} Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak, - uint8_t Visibility, bool IsTls) - : SymbolBody(K, N, IsWeak, Visibility, IsTls), CanKeepUndefined(false) {} + uint8_t Visibility, bool IsTls, bool IsFunction) + : SymbolBody(K, N, IsWeak, Visibility, IsTls, IsFunction), + CanKeepUndefined(false) {} Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility, bool CanKeepUndefined) : Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility, - /*IsTls*/ false) { + /*IsTls*/ false, /*IsFunction*/ false) { this->CanKeepUndefined = CanKeepUndefined; } @@ -94,18 +95,21 @@ UndefinedElf::UndefinedElf(StringRef N, const Elf_Sym &Sym) : Undefined(SymbolBody::UndefinedElfKind, N, Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(), - Sym.getType() == llvm::ELF::STT_TLS), + Sym.getType() == llvm::ELF::STT_TLS, + Sym.getType() == llvm::ELF::STT_FUNC), Sym(Sym) {} template DefinedSynthetic::DefinedSynthetic(StringRef N, uintX_t Value, OutputSectionBase &Section) - : Defined(SymbolBody::DefinedSyntheticKind, N, false, STV_DEFAULT, false), + : Defined(SymbolBody::DefinedSyntheticKind, N, false, STV_DEFAULT, + /*IsTls*/ false, /*IsFunction*/ false), Value(Value), Section(Section) {} DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, bool IsWeak, uint8_t Visibility) - : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, Visibility, false) { + : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, Visibility, + /*IsTls*/ false, /*IsFunction*/ false) { MaxAlignment = Alignment; this->Size = Size; } Index: test/ELF/bsymbolic.s =================================================================== --- test/ELF/bsymbolic.s +++ test/ELF/bsymbolic.s @@ -0,0 +1,30 @@ +// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +// RUN: ld.lld -shared %t.o -o %t0.so +// RUN: ld.lld -shared -Bsymbolic %t.o -o %t1.so +// RUN: ld.lld -shared -Bsymbolic-functions %t.o -o %t2.so +// RUN: llvm-readobj -s %t0.so | FileCheck -check-prefix=NOOPTION %s +// RUN: llvm-readobj -s %t1.so | FileCheck -check-prefix=SYMBOLIC %s +// RUN: llvm-readobj -s %t2.so | FileCheck -check-prefix=SYMBOLIC %s + +// NOOPTION: Section { +// NOOPTION: Name: .plt + +// SYMBOLIC: Section { +// SYMBOLIC-NOT: Name: .plt + +.text +.globl foo +.type foo,@function +foo: +nop + +.globl bar +.type bar,@function +bar: +nop + +.globl do +.type do,@function +do: +callq foo@PLT +callq bar@PLT