[lld] r306813 - [ELF] - Resolve references properly when using .symver directive

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 30 03:19:54 PDT 2017


Author: grimar
Date: Fri Jun 30 03:19:54 2017
New Revision: 306813

URL: http://llvm.org/viewvc/llvm-project?rev=306813&view=rev
Log:
[ELF] - Resolve references properly when using .symver directive

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 at 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.

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

Added:
    lld/trunk/test/ELF/version-script-symver.s
Modified:
    lld/trunk/ELF/SymbolTable.cpp

Modified: lld/trunk/ELF/SymbolTable.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.cpp?rev=306813&r1=306812&r2=306813&view=diff
==============================================================================
--- lld/trunk/ELF/SymbolTable.cpp (original)
+++ lld/trunk/ELF/SymbolTable.cpp Fri Jun 30 03:19:54 2017
@@ -712,15 +712,31 @@ void SymbolTable<ELFT>::assignWildcardVe
       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();

Added: lld/trunk/test/ELF/version-script-symver.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/version-script-symver.s?rev=306813&view=auto
==============================================================================
--- lld/trunk/test/ELF/version-script-symver.s (added)
+++ lld/trunk/test/ELF/version-script-symver.s Fri Jun 30 03:19:54 2017
@@ -0,0 +1,11 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: echo "VERSION { global: *; };" > %t.map
+# RUN: ld.lld %t.o --version-script %t.map -o %t
+
+.global _start
+.global bar
+.symver _start, bar@@VERSION
+_start:
+  jmp bar




More information about the llvm-commits mailing list