Index: ELF/Config.h =================================================================== --- ELF/Config.h +++ ELF/Config.h @@ -35,12 +35,17 @@ enum class UnresolvedPolicy { NoUndef, Error, Warn, Ignore }; +struct SymbolVersion { + llvm::StringRef Name; + bool IsExternCpp; +}; + // This struct contains symbols version definition that // can be found in version script if it is used for link. struct Version { Version(llvm::StringRef Name) : Name(Name) {} llvm::StringRef Name; - std::vector Globals; + std::vector Globals; size_t NameOff; // Offset in string table. }; @@ -67,7 +72,7 @@ std::vector DynamicList; std::vector SearchPaths; std::vector Undefined; - std::vector VersionScriptGlobals; + std::vector VersionScriptGlobals; std::vector BuildIdVector; bool AllowMultipleDefinition; bool AsNeeded = false; Index: ELF/SymbolListFile.cpp =================================================================== --- ELF/SymbolListFile.cpp +++ ELF/SymbolListFile.cpp @@ -77,6 +77,7 @@ void run(); private: + void parseExtern(std::vector *Globals); void parseVersion(StringRef Version); void parseLocal(); void parseVersionSymbols(StringRef Version); @@ -112,21 +113,38 @@ Config->VersionScriptGlobalByDefault = false; } +void VersionScriptParser::parseExtern(std::vector *Globals) { + expect("extern"); + expect("C++"); + expect("{"); + + for (;;) { + if (peek() == "}" || Error) + break; + Globals->push_back({next(), true}); + expect(";"); + } + + expect("}"); + expect(";"); +} + void VersionScriptParser::parseVersionSymbols(StringRef Version) { - std::vector *Globals; + std::vector *Globals; if (Version.empty()) Globals = &Config->VersionScriptGlobals; else Globals = &Config->SymbolVersions.back().Globals; for (;;) { + if (peek() == "extern") + parseExtern(Globals); + StringRef Cur = peek(); - if (Cur == "extern") - setError("extern keyword is not supported"); if (Cur == "}" || Cur == "local:" || Error) return; next(); - Globals->push_back(Cur); + Globals->push_back({Cur, false}); expect(";"); } } Index: ELF/SymbolTable.cpp =================================================================== --- ELF/SymbolTable.cpp +++ ELF/SymbolTable.cpp @@ -242,8 +242,9 @@ template std::string SymbolTable::conflictMsg(SymbolBody *Existing, InputFile *NewFile) { - StringRef Sym = Existing->getName(); - return demangle(Sym) + " in " + getFilename(Existing->getSourceFile()) + + std::string Name = + Config->Demangle ? demangle(Existing->getName()) : Existing->getName(); + return Name + " in " + getFilename(Existing->getSourceFile()) + " and " + getFilename(NewFile); } @@ -556,6 +557,13 @@ return S.find_first_of("?*") != StringRef::npos; } +static void overrideSymbolVersion(Symbol *Sym, StringRef Entry, + uint16_t Version) { + if (Sym->VersionId != VER_NDX_GLOBAL && Sym->VersionId != VER_NDX_LOCAL) + warning("duplicate symbol " + Entry + " in version script"); + Sym->VersionId = Version; +} + // This function processes the --version-script option by marking all global // symbols with the VersionScriptGlobal flag, which acts as a filter on the // dynamic symbol table. @@ -563,8 +571,8 @@ // If version script does not contain versions declarations, // we just should mark global symbols. if (!Config->VersionScriptGlobals.empty()) { - for (StringRef S : Config->VersionScriptGlobals) - if (SymbolBody *B = find(S)) + for (SymbolVersion &Sym : Config->VersionScriptGlobals) + if (SymbolBody *B = find(Sym.Name)) B->symbol()->VersionId = VER_NDX_GLOBAL; return; } @@ -576,37 +584,58 @@ // assign version references for each symbol. // Current rules are: // * If there is an exact match for the mangled name, we use it. + // * Otherwise, we look for an extern C++ exact match. // * Otherwise, we look through the wildcard patterns. We look through the // version tags in reverse order. We use the first match we find (the last // matching version tag in the file). + + // Handle exact matches and build a map of demangled externs for + // quick search during next step. + llvm::DenseMap DemangledExterns; for (size_t I = 0, E = Config->SymbolVersions.size(); I < E; ++I) { Version &V = Config->SymbolVersions[I]; - for (StringRef Name : V.Globals) { - if (hasWildcard(Name)) + for (SymbolVersion Sym : V.Globals) { + if (hasWildcard(Sym.Name)) + continue; + + if (Sym.IsExternCpp) { + size_t &Ver = DemangledExterns[Sym.Name]; + if (Ver != 0) + warning("duplicate symbol " + Sym.Name + " in version script"); + Ver = I + 2; continue; + } - SymbolBody *B = find(Name); + SymbolBody *B = find(Sym.Name); if (!B || B->isUndefined()) { if (Config->NoUndefinedVersion) error("version script assignment of " + V.Name + " to symbol " + - Name + " failed: symbol not defined"); + Sym.Name + " failed: symbol not defined"); continue; } - if (B->symbol()->VersionId != VER_NDX_GLOBAL && - B->symbol()->VersionId != VER_NDX_LOCAL) - warning("duplicate symbol " + Name + " in version script"); - B->symbol()->VersionId = I + 2; + overrideSymbolVersion(B->symbol(), Sym.Name, I + 2); + } + } + + // Handle externs. + if (!DemangledExterns.empty()) { + for (auto I = Symtab.begin(); I != Symtab.end(); ++I) { + auto E = DemangledExterns.find(demangle(I->first.Val)); + if (E == DemangledExterns.end()) + continue; + overrideSymbolVersion(SymVector[I->second], E->first, E->second); } } + // Handle wildcards. for (size_t I = Config->SymbolVersions.size() - 1; I != (size_t)-1; --I) { Version &V = Config->SymbolVersions[I]; - for (StringRef Name : V.Globals) { - if (!hasWildcard(Name)) + for (SymbolVersion Sym : V.Globals) { + if (!hasWildcard(Sym.Name)) continue; - for (SymbolBody *B : findAll(Name)) + for (SymbolBody *B : findAll(Sym.Name)) if (B->symbol()->VersionId == VER_NDX_GLOBAL || B->symbol()->VersionId == VER_NDX_LOCAL) B->symbol()->VersionId = I + 2; Index: ELF/Symbols.cpp =================================================================== --- ELF/Symbols.cpp +++ ELF/Symbols.cpp @@ -251,9 +251,6 @@ #if !defined(HAVE_CXXABI_H) return Name; #else - if (!Config->Demangle) - return Name; - // __cxa_demangle can be used to demangle strings other than symbol // names which do not necessarily start with "_Z". Name can be // either a C or C++ symbol. Don't call __cxa_demangle if the name Index: test/ELF/version-script-err.s =================================================================== --- test/ELF/version-script-err.s +++ test/ELF/version-script-err.s @@ -8,8 +8,3 @@ // RUN: not ld.lld --version-script %terr1.script -shared %t.o -o %t.so 2>&1 | \ // RUN: FileCheck -check-prefix=ERR1 %s // ERR1: unclosed quote - -// RUN: echo "VERSION { extern "C++" {}; }; " > %terr2.script -// RUN: not ld.lld --version-script %terr2.script -shared %t.o -o %t.so 2>&1 | \ -// RUN: FileCheck -check-prefix=ERR2 %s -// ERR2: extern keyword is not supported Index: test/ELF/version-script-extern.s =================================================================== --- test/ELF/version-script-extern.s +++ test/ELF/version-script-extern.s @@ -0,0 +1,98 @@ +# REQUIRES: x86 +# XFAIL: win32 + +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o +# RUN: echo "LIBSAMPLE_1.0 { \ +# RUN: global: \ +# RUN: extern "C++" { \ +# RUN: \"foo()\"; \ +# RUN: \"zed()\"; \ +# RUN: }; \ +# RUN: }; \ +# RUN: LIBSAMPLE_2.0 { \ +# RUN: global: \ +# RUN: extern "C++" { \ +# RUN: \"bar()\"; \ +# RUN: }; \ +# RUN: }; " > %t.script +# RUN: ld.lld --version-script %t.script -shared %t.o -o %t.so +# RUN: llvm-readobj -V -dyn-symbols %t.so | FileCheck --check-prefix=DSO %s + +# DSO: DynamicSymbols [ +# DSO-NEXT: Symbol { +# DSO-NEXT: Name: @ +# DSO-NEXT: Value: 0x0 +# DSO-NEXT: Size: 0 +# DSO-NEXT: Binding: Local +# DSO-NEXT: Type: None +# DSO-NEXT: Other: 0 +# DSO-NEXT: Section: Undefined +# DSO-NEXT: } +# DSO-NEXT: Symbol { +# DSO-NEXT: Name: _Z3barv@@LIBSAMPLE_2.0 +# DSO-NEXT: Value: 0x1001 +# DSO-NEXT: Size: 0 +# DSO-NEXT: Binding: Global +# DSO-NEXT: Type: Function +# DSO-NEXT: Other: 0 +# DSO-NEXT: Section: .text +# DSO-NEXT: } +# DSO-NEXT: Symbol { +# DSO-NEXT: Name: _Z3foov@@LIBSAMPLE_1.0 +# DSO-NEXT: Value: 0x1000 +# DSO-NEXT: Size: 0 +# DSO-NEXT: Binding: Global +# DSO-NEXT: Type: Function +# DSO-NEXT: Other: 0 +# DSO-NEXT: Section: .text +# DSO-NEXT: } +# DSO-NEXT: Symbol { +# DSO-NEXT: Name: _Z3zedv@@LIBSAMPLE_1.0 +# DSO-NEXT: Value: 0x1002 +# DSO-NEXT: Size: 0 +# DSO-NEXT: Binding: Global (0x1) +# DSO-NEXT: Type: Function (0x2) +# DSO-NEXT: Other: 0 +# DSO-NEXT: Section: .text (0x6) +# DSO-NEXT: } +# DSO-NEXT: ] +# DSO-NEXT: Version symbols { +# DSO-NEXT: Section Name: .gnu.version +# DSO-NEXT: Address: 0x228 +# DSO-NEXT: Offset: 0x228 +# DSO-NEXT: Link: 1 +# DSO-NEXT: Symbols [ +# DSO-NEXT: Symbol { +# DSO-NEXT: Version: 0 +# DSO-NEXT: Name: @ +# DSO-NEXT: } +# DSO-NEXT: Symbol { +# DSO-NEXT: Version: 3 +# DSO-NEXT: Name: _Z3barv@@LIBSAMPLE_2.0 +# DSO-NEXT: } +# DSO-NEXT: Symbol { +# DSO-NEXT: Version: 2 +# DSO-NEXT: Name: _Z3foov@@LIBSAMPLE_1.0 +# DSO-NEXT: } +# DSO-NEXT: Symbol { +# DSO-NEXT: Version: 2 +# DSO-NEXT: Name: _Z3zedv@@LIBSAMPLE_1.0 +# DSO-NEXT: } +# DSO-NEXT: ] +# DSO-NEXT: } + +.text +.globl _Z3foov +.type _Z3foov,@function +_Z3foov: +retq + +.globl _Z3barv +.type _Z3barv,@function +_Z3barv: +retq + +.globl _Z3zedv +.type _Z3zedv,@function +_Z3zedv: +retq