Skip to content

Commit

Permalink
[ELF] - Resolve references properly when using .symver directive
Browse files Browse the repository at this point in the history
This is PR28414. 
Previously LLD was unable to link following:
(failed with undefined symbol bar)

Version script:
SOME_VERSION { global: *; };

.global _start
.global bar
.symver _start, bar@@SOME_VERSION
_start:
  jmp bar
Manual has next description:

.symver name, name2@@nodename
In this case, the symbol name must exist and be defined within the file being assembled. It is similar to name2@nodename. 
The difference is name2@@nodename will also be used to resolve references to name2 by the linker
https://sourceware.org/binutils/docs/as/Symver.html

Patch implements that. If we have name@@ver symbol and name is undefined, name@@ver is used to resolve references to name.
If name is defined then multiple definition error is emited, that is consistent with what bfd do.

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

llvm-svn: 307077
  • Loading branch information
George Rimar committed Jul 4, 2017
1 parent a951d5c commit 4d2f976
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
24 changes: 20 additions & 4 deletions lld/ELF/SymbolTable.cpp
Expand Up @@ -712,15 +712,31 @@ void SymbolTable<ELFT>::assignWildcardVersion(SymbolVersion Ver,
B->symbol()->VersionId = VersionId;
}

static bool isDefaultVersion(SymbolBody *B) {
return B->isInCurrentDSO() && B->getName().find("@@") != StringRef::npos;
}

// 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 their names.
if (!Config->VersionDefinitions.empty())
for (Symbol *Sym : SymVector)
Sym->body()->parseSymbolVersion();
// Let them parse and update their names to exclude version suffix.
for (Symbol *Sym : SymVector) {
SymbolBody *Body = Sym->body();
bool IsDefault = isDefaultVersion(Body);
Body->parseSymbolVersion();

if (!IsDefault)
continue;

// <name>@@<version> means the symbol is the default version. If that's the
// case, the symbol is not used only to resolve <name> of version <version>
// but also undefined unversioned symbols with name <name>.
SymbolBody *S = find(Body->getName());
if (S && S->isUndefined())
S->copy(Body);
}

// Handle edge cases first.
handleAnonymousVersion();
Expand Down
7 changes: 6 additions & 1 deletion lld/ELF/Symbols.cpp
Expand Up @@ -272,7 +272,12 @@ void SymbolBody::parseSymbolVersion() {
}

// It is an error if the specified version is not defined.
error(toString(File) + ": symbol " + S + " has undefined version " + Verstr);
// Usually version script is not provided when linking executable,
// but we may still want to override a versioned symbol from DSO,
// so we do not report error in this case.
if (Config->Shared)
error(toString(File) + ": symbol " + S + " has undefined version " +
Verstr);
}

Defined::Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
Expand Down
9 changes: 9 additions & 0 deletions lld/test/ELF/version-script-symver.s
@@ -0,0 +1,9 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
# RUN: ld.lld %t.o -o %t

.global _start
.global bar
.symver _start, bar@@VERSION
_start:
jmp bar

0 comments on commit 4d2f976

Please sign in to comment.