Index: ELF/Config.h =================================================================== --- ELF/Config.h +++ ELF/Config.h @@ -71,8 +71,8 @@ // can be found in version script if it is used for link. struct VersionDefinition { llvm::StringRef name; - uint16_t id = 0; - std::vector globals; + uint16_t id; + std::vector names; }; // This struct contains the global configuration for the linker. @@ -117,8 +117,6 @@ std::vector symbolOrderingFile; std::vector undefined; std::vector dynamicList; - std::vector versionScriptGlobals; - std::vector versionScriptLocals; std::vector buildIdVector; llvm::MapVector, uint64_t> @@ -224,7 +222,6 @@ ARMVFPArgKind armVFPArgs = ARMVFPArgKind::Default; BuildIdKind buildId = BuildIdKind::None; ELFKind ekind = ELFNoneKind; - uint16_t defaultSymbolVersion = llvm::ELF::VER_NDX_GLOBAL; uint16_t emachine = llvm::ELF::EM_NONE; llvm::Optional imageBase; uint64_t commonPageSize; Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -1011,14 +1011,19 @@ } } + assert(config->versionDefinitions.empty()); + config->versionDefinitions.push_back({"local", (uint16_t)VER_NDX_LOCAL, {}}); + config->versionDefinitions.push_back({"global", (uint16_t)VER_NDX_GLOBAL, {}}); + // If --retain-symbol-file is used, we'll keep only the symbols listed in // the file and discard all others. if (auto *arg = args.getLastArg(OPT_retain_symbols_file)) { - config->defaultSymbolVersion = VER_NDX_LOCAL; + config->versionDefinitions[0].names.push_back( + {"*", /*isExternCpp=*/false, /*hasWildcard=*/true}); if (Optional buffer = readFile(arg->getValue())) for (StringRef s : args::getLines(*buffer)) - config->versionScriptGlobals.push_back( - {s, /*IsExternCpp*/ false, /*HasWildcard*/ false}); + config->versionDefinitions[1].names.push_back( + {s, /*isExternCpp=*/false, /*hasWildcard=*/false}); } bool hasExportDynamic = Index: ELF/ScriptParser.cpp =================================================================== --- ELF/ScriptParser.cpp +++ ELF/ScriptParser.cpp @@ -1344,16 +1344,8 @@ std::vector locals; std::vector globals; std::tie(locals, globals) = readSymbols(); - - for (SymbolVersion v : locals) { - if (v.name == "*") - config->defaultSymbolVersion = VER_NDX_LOCAL; - else - config->versionScriptLocals.push_back(v); - } - - for (SymbolVersion v : globals) - config->versionScriptGlobals.push_back(v); + llvm::copy(locals, std::back_inserter(config->versionDefinitions[0].names)); + llvm::copy(globals, std::back_inserter(config->versionDefinitions[1].names)); expect(";"); } @@ -1365,22 +1357,16 @@ std::vector locals; std::vector globals; std::tie(locals, globals) = readSymbols(); - - for (SymbolVersion v : locals) { - if (v.name == "*") - config->defaultSymbolVersion = VER_NDX_LOCAL; - else - config->versionScriptLocals.push_back(v); - } + llvm::copy(locals, std::back_inserter(config->versionDefinitions[0].names)); // Create a new version definition and add that to the global symbols. VersionDefinition ver; ver.name = verStr; - ver.globals = globals; + ver.names = globals; // User-defined version number starts from 2 because 0 and 1 are // reserved for VER_NDX_LOCAL and VER_NDX_GLOBAL, respectively. - ver.id = config->versionDefinitions.size() + 2; + ver.id = config->versionDefinitions.size(); config->versionDefinitions.push_back(ver); // Each version may have a parent version. For example, "Ver2" Index: ELF/SymbolTable.cpp =================================================================== --- ELF/SymbolTable.cpp +++ ELF/SymbolTable.cpp @@ -73,7 +73,7 @@ sym->setName(name); sym->symbolKind = Symbol::PlaceholderKind; - sym->versionId = config->defaultSymbolVersion; + sym->versionId = VER_NDX_GLOBAL; sym->visibility = STV_DEFAULT; sym->isUsedInRegularObj = false; sym->exportDynamic = false; @@ -192,7 +192,7 @@ return "VER_NDX_LOCAL"; if (ver == VER_NDX_GLOBAL) return "VER_NDX_GLOBAL"; - return ("version '" + config->versionDefinitions[ver - 2].name + "'").str(); + return ("version '" + config->versionDefinitions[ver].name + "'").str(); }; // Assign the version. @@ -203,8 +203,10 @@ if (sym->getName().contains('@')) continue; - if (sym->versionId == config->defaultSymbolVersion) + if (sym->verdefIndex == UINT32_C(-1)) { + sym->verdefIndex = 0; sym->versionId = versionId; + } if (sym->versionId == versionId) continue; @@ -214,15 +216,14 @@ } void SymbolTable::assignWildcardVersion(SymbolVersion ver, uint16_t versionId) { - if (!ver.hasWildcard) - return; - // Exact matching takes precendence over fuzzy matching, // so we set a version to a symbol only if no version has been assigned // to the symbol. This behavior is compatible with GNU. for (Symbol *b : findAllByVersion(ver)) - if (b->versionId == config->defaultSymbolVersion) + if (b->verdefIndex == UINT32_C(-1)) { + b->verdefIndex = 0; b->versionId = versionId; + } } // This function processes version scripts by updating the versionId @@ -233,26 +234,24 @@ void SymbolTable::scanVersionScript() { // First, we assign versions to exact matching symbols, // i.e. version definitions not containing any glob meta-characters. - for (SymbolVersion &ver : config->versionScriptGlobals) - assignExactVersion(ver, VER_NDX_GLOBAL, "global"); - for (SymbolVersion &ver : config->versionScriptLocals) - assignExactVersion(ver, VER_NDX_LOCAL, "local"); for (VersionDefinition &v : config->versionDefinitions) - for (SymbolVersion &ver : v.globals) + for (SymbolVersion &ver : v.names) assignExactVersion(ver, v.id, v.name); - // Next, we assign versions to fuzzy matching symbols, - // i.e. version definitions containing glob meta-characters. - for (SymbolVersion &ver : config->versionScriptGlobals) - assignWildcardVersion(ver, VER_NDX_GLOBAL); - for (SymbolVersion &ver : config->versionScriptLocals) - assignWildcardVersion(ver, VER_NDX_LOCAL); - - // Note that because the last match takes precedence over previous matches, - // we iterate over the definitions in the reverse order. + // Next, assign versions to wildcards that are not "*". Note that because the + // last match takes precedence over previous matches, we iterate over the + // definitions in the reverse order. for (VersionDefinition &v : llvm::reverse(config->versionDefinitions)) - for (SymbolVersion &ver : v.globals) - assignWildcardVersion(ver, v.id); + for (SymbolVersion &ver : v.names) + if (ver.hasWildcard && ver.name != "*") + assignWildcardVersion(ver, v.id); + + // Then, assign versions to "*". In GNU linkers they have lower priority than + // other wildcard names. + for (VersionDefinition &v : config->versionDefinitions) + for (SymbolVersion &ver : v.names) + if (ver.hasWildcard && ver.name == "*") + assignWildcardVersion(ver, v.id); // Symbol themselves might know their versions because symbols // can contain versions in the form of @. @@ -262,7 +261,6 @@ // isPreemptible is false at this point. To correctly compute the binding of a // Defined (which is used by includeInDynsym()), we need to know if it is - // VER_NDX_LOCAL or not. If defaultSymbolVersion is VER_NDX_LOCAL, we should - // compute symbol versions before handling --dynamic-list. + // VER_NDX_LOCAL or not. Compute symbol versions before handling --dynamic-list. handleDynamicList(); } Index: ELF/Symbols.cpp =================================================================== --- ELF/Symbols.cpp +++ ELF/Symbols.cpp @@ -227,7 +227,7 @@ if (isDefault) verstr = verstr.substr(1); - for (VersionDefinition &ver : config->versionDefinitions) { + for (auto &ver : makeArrayRef(config->versionDefinitions).slice(2)) { if (ver.name != verstr) continue; Index: ELF/SyntheticSections.cpp =================================================================== --- ELF/SyntheticSections.cpp +++ ELF/SyntheticSections.cpp @@ -1162,10 +1162,10 @@ } } -// Returns the number of version definition entries. Because the first entry -// is for the version definition itself, it is the number of versioned symbols -// plus one. Note that we don't support multiple versions yet. -static unsigned getVerDefNum() { return config->versionDefinitions.size() + 1; } +// Returns the number of entries in .gnu.version_d: the number of +// non-VER_NDX_LOCAL-non-VER_NDX_GLOBAL definitions, plus 1. Note that we don't +// support vd_cnt > 1 yet. +static unsigned getVerDefNum() { return config->versionDefinitions.size() - 1; } template DynamicSection::DynamicSection() @@ -2771,7 +2771,8 @@ void VersionDefinitionSection::finalizeContents() { fileDefNameOff = getPartition().dynStrTab->addString(getFileDefName()); - for (VersionDefinition &v : config->versionDefinitions) + for (const VersionDefinition &v : + makeArrayRef(config->versionDefinitions).slice(2)) verDefNameOffs.push_back(getPartition().dynStrTab->addString(v.name)); if (OutputSection *sec = getPartition().dynStrTab->getParent()) @@ -2805,7 +2806,8 @@ writeOne(buf, 1, getFileDefName(), fileDefNameOff); auto nameOffIt = verDefNameOffs.begin(); - for (VersionDefinition &v : config->versionDefinitions) { + for (const VersionDefinition &v : + makeArrayRef(config->versionDefinitions).slice(2)) { buf += EntrySize; writeOne(buf, v.id, v.name, *nameOffIt++); } Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -396,7 +396,7 @@ part.verSym = make(); add(part.verSym); - if (!config->versionDefinitions.empty()) { + if (config->versionDefinitions.size() > 2) { part.verDef = make(); add(part.verDef); } Index: test/ELF/linkerscript/version-script.s =================================================================== --- test/ELF/linkerscript/version-script.s +++ test/ELF/linkerscript/version-script.s @@ -17,7 +17,7 @@ # CHECK-NEXT: Name: # CHECK-NEXT: } # CHECK-NEXT: Symbol { -# CHECK-NEXT: Version: 0 +# CHECK-NEXT: Version: 1 # CHECK-NEXT: Name: und # CHECK-NEXT: } # CHECK-NEXT: Symbol { Index: test/ELF/version-script-reassign-glob.s =================================================================== --- /dev/null +++ test/ELF/version-script-reassign-glob.s @@ -0,0 +1,19 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o + +# RUN: echo 'foo { foo*; }; bar { *; };' > %t.ver +# RUN: ld.lld --version-script %t.ver %t.o -shared -o %t.so --fatal-warnings +# RUN: llvm-readelf --dyn-syms %t.so | FileCheck %s + +# RUN: echo 'foo { foo*; }; bar { f*; };' > %t.ver +# RUN: ld.lld --version-script %t.ver %t.o -shared -o %t.so --fatal-warnings +# RUN: llvm-readelf --dyn-syms %t.so | FileCheck --check-prefix=BAR %s + +## If both a non-* glob and a * match, non-* wins. +## This is GNU linkers' behavior. We don't feel strongly this should be supported. +# CHECK: GLOBAL DEFAULT 7 foo@@foo + +# BAR: GLOBAL DEFAULT 7 foo@@bar + +.globl foo +foo: Index: test/ELF/version-script-reassign.s =================================================================== --- test/ELF/version-script-reassign.s +++ test/ELF/version-script-reassign.s @@ -1,18 +1,22 @@ # REQUIRES: x86 # RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o +# RUN: echo '{ global: foo; };' > %tg.ver # RUN: echo '{ local: foo; };' > %tl.ver -# RUN: echo '{ global: foo; local: *; };' > %tg.ver +# RUN: echo '{ global: foo; local: *; };' > %tgl.ver # RUN: echo 'V1 { global: foo; };' > %t1.ver # RUN: echo 'V2 { global: foo; };' > %t2.ver # RUN: echo 'V2 { global: notexist; local: f*; };' > %t2w.ver -## Note, ld.bfd errors on the two cases. +## Note, ld.bfd errors on these cases. # RUN: ld.lld -shared %t.o --version-script %tl.ver --version-script %t1.ver \ # RUN: -o %t.so 2>&1 | FileCheck --check-prefix=LOCAL %s # RUN: llvm-readelf --dyn-syms %t.so | FileCheck --check-prefix=LOCAL-SYM %s # RUN: ld.lld -shared %t.o --version-script %tg.ver --version-script %t1.ver \ # RUN: -o %t.so 2>&1 | FileCheck --check-prefix=GLOBAL %s # RUN: llvm-readelf --dyn-syms %t.so | FileCheck --check-prefix=GLOBAL-SYM %s +# RUN: ld.lld -shared %t.o --version-script %tgl.ver --version-script %t1.ver \ +# RUN: -o %t.so 2>&1 | FileCheck --check-prefix=GLOBAL %s +# RUN: llvm-readelf --dyn-syms %t.so | FileCheck --check-prefix=GLOBAL-SYM %s ## Note, ld.bfd silently accepts this case. # RUN: ld.lld -shared %t.o --version-script %t1.ver --version-script %t2.ver \