[lld] r274739 - [ELF] - Fixed incorrect logic of version assignments when mixing wildcards with values matching.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 7 00:45:27 PDT 2016


Author: grimar
Date: Thu Jul  7 02:45:27 2016
New Revision: 274739

URL: http://llvm.org/viewvc/llvm-project?rev=274739&view=rev
Log:
[ELF] - Fixed incorrect logic of version assignments when mixing wildcards with values matching.

Previously we had incorrect logic here. Imagine we would have the next script:

LIBSAMPLE_1.0
{
  global:
   a_2;
 local:
  *;
};

LIBSAMPLE_2.0
{
  global:
   a*;
};
According to previous logic it would assign version 1 to a_2 and then
would try to reassign it to version 2 because of applying wildcard a*.
And show a warning about that.

Generally Ian Lance Tailor wrote about next rules that should be applied:
(http://www.airs.com/blog/archives/300)

Here are the current rules for gold:

"If there is an exact match for the mangled name, we use it. If there is more than one exact match, we give a warning, and we use the first tag in the script which matches. If a symbol has an exact match as both global and local for the same version tag, we give an error.
Otherwise, we look for an extern C++ or an extern Java exact match. If we find an exact match, we use it. If there is more than one exact match, we give a warning, and we use the first tag in the script which matches. If a symbol has an exact match as both global and local for the same version tag, we give an error.
Otherwise, we look through the wildcard patterns, ignoring “*” patterns. We look through the version tags in reverse order. For each version tag, we look through the global patterns and then the local patterns. We use the first match we find (i.e., the last matching version tag in the file).
Otherwise, we use the “*” pattern if there is one. We give a warning if there are multiple “*” patterns."

Patch makes wildcard matching to be in revered order and to follow after the regular naming matching.

Differential revision: http://reviews.llvm.org/D21894

Modified:
    lld/trunk/ELF/SymbolTable.cpp
    lld/trunk/test/ELF/version-wildcard.test

Modified: lld/trunk/ELF/SymbolTable.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.cpp?rev=274739&r1=274738&r2=274739&view=diff
==============================================================================
--- lld/trunk/ELF/SymbolTable.cpp (original)
+++ lld/trunk/ELF/SymbolTable.cpp Thu Jul  7 02:45:27 2016
@@ -460,15 +460,6 @@ template <class ELFT> SymbolBody *Symbol
 // Returns a list of defined symbols that match with a given glob pattern.
 template <class ELFT>
 std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(StringRef Pattern) {
-  // Fast-path. Fallback to find() if Pattern doesn't contain any wildcard
-  // characters.
-  if (Pattern.find_first_of("?*") == StringRef::npos) {
-    if (SymbolBody *B = find(Pattern))
-      if (!B->isUndefined())
-        return {B};
-    return {};
-  }
-
   std::vector<SymbolBody *> Res;
   for (auto &It : Symtab) {
     StringRef Name = It.first.Val;
@@ -561,6 +552,10 @@ template <class ELFT> void SymbolTable<E
       B->symbol()->ExportDynamic = true;
 }
 
+static bool hasWildcard(StringRef S) {
+  return S.find_first_of("?*") != StringRef::npos;
+}
+
 // 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.
@@ -574,27 +569,48 @@ template <class ELFT> void SymbolTable<E
     return;
   }
 
+  if (Config->SymbolVersions.empty())
+    return;
+
   // If we have symbols version declarations, we should
   // assign version references for each symbol.
-  size_t I = 2;
-  for (Version &V : Config->SymbolVersions) {
+  // Current rules are:
+  // * If there is an exact match for the mangled name, we use it.
+  // * 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).
+  for (size_t I = 0, E = Config->SymbolVersions.size(); I < E; ++I) {
+    Version &V = Config->SymbolVersions[I];
     for (StringRef Name : V.Globals) {
-      std::vector<SymbolBody *> Syms = findAll(Name);
-      if (Syms.empty()) {
+      if (hasWildcard(Name))
+        continue;
+
+      SymbolBody *B = find(Name);
+      if (!B || B->isUndefined()) {
         if (Config->NoUndefinedVersion)
           error("version script assignment of " + V.Name + " to symbol " +
                 Name + " failed: symbol not defined");
         continue;
       }
 
-      for (SymbolBody *B : Syms) {
-        if (B->symbol()->VersionId != VER_NDX_GLOBAL &&
-            B->symbol()->VersionId != VER_NDX_LOCAL)
-          warning("duplicate symbol " + Name + " in version script");
-        B->symbol()->VersionId = I;
-      }
+      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;
+    }
+  }
+
+  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))
+        continue;
+
+      for (SymbolBody *B : findAll(Name))
+        if (B->symbol()->VersionId == VER_NDX_GLOBAL ||
+            B->symbol()->VersionId == VER_NDX_LOCAL)
+          B->symbol()->VersionId = I + 2;
     }
-    ++I;
   }
 }
 

Modified: lld/trunk/test/ELF/version-wildcard.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/version-wildcard.test?rev=274739&r1=274738&r2=274739&view=diff
==============================================================================
--- lld/trunk/test/ELF/version-wildcard.test (original)
+++ lld/trunk/test/ELF/version-wildcard.test Thu Jul  7 02:45:27 2016
@@ -46,6 +46,55 @@
 # CHECK-NEXT:   }
 # CHECK-NEXT: ]
 
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: echo "VERSION_1.0{      \
+# RUN:          global: foo2;  \
+# RUN:          local: *; };   \
+# RUN:       VERSION_2.0{      \
+# RUN:          global: foo*;  \
+# RUN:       }; " > %t2.script
+# RUN: ld.lld --version-script %t2.script -shared %t.o -o %t2.so
+# RUN: llvm-readobj -dyn-symbols %t2.so | FileCheck --check-prefix=MIX %s
+
+# MIX:      DynamicSymbols [
+# MIX-NEXT:   Symbol {
+# MIX-NEXT:     Name: @
+# MIX-NEXT:     Value: 0x0
+# MIX-NEXT:     Size: 0
+# MIX-NEXT:     Binding: Local
+# MIX-NEXT:     Type: None
+# MIX-NEXT:     Other: 0
+# MIX-NEXT:     Section: Undefined
+# MIX-NEXT:   }
+# MIX-NEXT:   Symbol {
+# MIX-NEXT:     Name: foo1@@VERSION_2.0
+# MIX-NEXT:     Value: 0x1000
+# MIX-NEXT:     Size: 0
+# MIX-NEXT:     Binding: Global
+# MIX-NEXT:     Type: None
+# MIX-NEXT:     Other: 0
+# MIX-NEXT:     Section: .text
+# MIX-NEXT:   }
+# MIX-NEXT:   Symbol {
+# MIX-NEXT:     Name: foo2@@VERSION_1.0
+# MIX-NEXT:     Value: 0x1001
+# MIX-NEXT:     Size: 0
+# MIX-NEXT:     Binding: Global
+# MIX-NEXT:     Type: None
+# MIX-NEXT:     Other: 0
+# MIX-NEXT:     Section: .text
+# MIX-NEXT:   }
+# MIX-NEXT:   Symbol {
+# MIX-NEXT:     Name: foo3@@VERSION_2.0
+# MIX-NEXT:     Value: 0x1007
+# MIX-NEXT:     Size: 0
+# MIX-NEXT:     Binding: Global
+# MIX-NEXT:     Type: None
+# MIX-NEXT:     Other: 0
+# MIX-NEXT:     Section: .text
+# MIX-NEXT:   }
+# MIX-NEXT: ]
+
 .globl foo1
 foo1:
   ret




More information about the llvm-commits mailing list