[ELF] - Give a symbol version extracted from name a priority over version set by script.

This fixes PR33712.

Imagine following script and code:

VER1 { global: foo; local: *; };
VER2 { global: foo; };

.global bar
bar:
.symver bar, foo@VER1

.global zed
zed:
.symver zed, foo@@VER2

We add foo@@VER2 as foo to symbol table, because have to resolve references to
foo for default symbols.
Later we are trying to assign symbol versions from script. For that we are searching for 'foo'
again. Here it is placed under VER1 and VER2 at the same time, we find it twice and trying to
set version again both times, hence LLD shows a warning.
Though sample code is correct: we have 2 different versions of foo.

Patch gives a symbol version extracted from name a priority over version set by script.

Differential revision: https://reviews.llvm.org/D35207

llvm-svn: 307792
This commit is contained in:
George Rimar 2017-07-12 13:54:42 +00:00
parent 5314521328
commit 3e8a461bdf
2 changed files with 40 additions and 8 deletions

View File

@ -695,6 +695,12 @@ void SymbolTable<ELFT>::assignExactVersion(SymbolVersion Ver, uint16_t VersionId
// Assign the version.
for (SymbolBody *B : Syms) {
// Skip symbols containing version info because symbol versions
// specified by symbol names take precedence over version scripts.
// See parseSymbolVersion().
if (B->getName().find('@') != StringRef::npos)
continue;
Symbol *Sym = B->symbol();
if (Sym->VersionId != Config->DefaultSymbolVersion)
warn("duplicate symbol '" + Ver.Name + "' in version script");
@ -719,17 +725,9 @@ void SymbolTable<ELFT>::assignWildcardVersion(SymbolVersion Ver,
// This function processes version scripts by updating VersionId
// member of symbols.
template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
// Symbol themselves might know their versions because symbols
// can contain versions in the form of <name>@<version>.
// Let them parse and update their names to exclude version suffix.
for (Symbol *Sym : SymVector)
Sym->body()->parseSymbolVersion();
// Handle edge cases first.
handleAnonymousVersion();
if (Config->VersionDefinitions.empty())
return;
// Now we have version definitions, so we need to set version ids to symbols.
// Each version definition has a glob pattern, and all symbols that match
@ -748,6 +746,12 @@ template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
for (SymbolVersion &Ver : V.Globals)
assignWildcardVersion(Ver, V.Id);
// Symbol themselves might know their versions because symbols
// can contain versions in the form of <name>@<version>.
// Let them parse and update their names to exclude version suffix.
for (Symbol *Sym : SymVector)
Sym->body()->parseSymbolVersion();
}
template class elf::SymbolTable<ELF32LE>;

View File

@ -0,0 +1,28 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
# RUN: echo "VER1 { global: foo; local: *; }; VER2 { global: foo; }; VER3 { global: foo; };" > %t.map
# RUN: ld.lld -shared %t.o --version-script %t.map -o %t.so --fatal-warnings
# RUN: llvm-readobj -V %t.so | FileCheck %s
# CHECK: Symbols [
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Version: 0
# CHECK-NEXT: Name: @
# CHECK-NEXT: }
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Version: 3
# CHECK-NEXT: Name: foo@@VER2
# CHECK-NEXT: }
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Version: 2
# CHECK-NEXT: Name: foo@VER1
# CHECK-NEXT: }
# CHECK-NEXT: ]
.global bar
bar:
.symver bar, foo@VER1
.global zed
zed:
.symver zed, foo@@VER2