[lld] r334432 - [ELF] Fix copy relocation when two symbols share the same Symbol instance.

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 11 12:42:57 PDT 2018


Author: maskray
Date: Mon Jun 11 12:42:57 2018
New Revision: 334432

URL: http://llvm.org/viewvc/llvm-project?rev=334432&view=rev
Log:
[ELF] Fix copy relocation when two symbols share the same Symbol instance.

In glibc libc.so.6, the multiple versions of sys_errlist share the same Symbol instance. When sys_errlist is copy relocated, we would replace SharedSymbol with Defined in the first iteration of the following loop:

  for (SharedSymbol *Sym : getSymbolsAt<ELFT>(SS))

Then in the second iteration, we think the symbol (which has been changed to Defined) is still SharedSymbol and screw up (the address ends up in the `Size` field).

Added:
    lld/trunk/test/ELF/Inputs/copy-rel-version.s
    lld/trunk/test/ELF/copy-rel-version.s
Modified:
    lld/trunk/ELF/Relocations.cpp

Modified: lld/trunk/ELF/Relocations.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Relocations.cpp?rev=334432&r1=334431&r2=334432&view=diff
==============================================================================
--- lld/trunk/ELF/Relocations.cpp (original)
+++ lld/trunk/ELF/Relocations.cpp Mon Jun 11 12:42:57 2018
@@ -52,6 +52,7 @@
 #include "Thunks.h"
 #include "lld/Common/Memory.h"
 #include "lld/Common/Strings.h"
+#include "llvm/ADT/SmallSet.h"
 #include "llvm/Support/Endian.h"
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
@@ -432,14 +433,14 @@ template <class ELFT> static bool isRead
 //
 // If two or more symbols are at the same offset, and at least one of
 // them are copied by a copy relocation, all of them need to be copied.
-// Otherwise, they would refer different places at runtime.
+// Otherwise, they would refer to different places at runtime.
 template <class ELFT>
-static std::vector<SharedSymbol *> getSymbolsAt(SharedSymbol &SS) {
+static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &SS) {
   typedef typename ELFT::Sym Elf_Sym;
 
   SharedFile<ELFT> &File = SS.getFile<ELFT>();
 
-  std::vector<SharedSymbol *> Ret;
+  SmallSet<SharedSymbol *, 4> Ret;
   for (const Elf_Sym &S : File.getGlobalELFSyms()) {
     if (S.st_shndx == SHN_UNDEF || S.st_shndx == SHN_ABS ||
         S.st_value != SS.Value)
@@ -447,7 +448,7 @@ static std::vector<SharedSymbol *> getSy
     StringRef Name = check(S.getName(File.getStringTable()));
     Symbol *Sym = Symtab->find(Name);
     if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym))
-      Ret.push_back(Alias);
+      Ret.insert(Alias);
   }
   return Ret;
 }

Added: lld/trunk/test/ELF/Inputs/copy-rel-version.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/copy-rel-version.s?rev=334432&view=auto
==============================================================================
--- lld/trunk/test/ELF/Inputs/copy-rel-version.s (added)
+++ lld/trunk/test/ELF/Inputs/copy-rel-version.s Mon Jun 11 12:42:57 2018
@@ -0,0 +1,11 @@
+.data
+.global foo at v1
+.type foo at v1, @object
+.size foo at v1, 4
+.global foo@@v2
+.type foo@@v2, @object
+.size foo@@v2, 8
+foo at v1:
+foo@@v2:
+.int 0
+.int 0

Added: lld/trunk/test/ELF/copy-rel-version.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/copy-rel-version.s?rev=334432&view=auto
==============================================================================
--- lld/trunk/test/ELF/copy-rel-version.s (added)
+++ lld/trunk/test/ELF/copy-rel-version.s Mon Jun 11 12:42:57 2018
@@ -0,0 +1,15 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/copy-rel-version.s -o %t1.o
+// RUN: echo "v1 {}; v2 {};" > %t.ver
+// RUN: ld.lld %t1.o -shared -soname t1.so --version-script=%t.ver -o %t1.so
+// RUN: ld.lld %t.o %t1.so -o %t
+// RUN: llvm-readobj -t %t | FileCheck %s
+
+.global _start
+_start:
+  leaq foo, %rax
+
+// CHECK:      Name: foo (
+// CHECK-NEXT: Value:
+// CHECK-NEXT: Size: 8




More information about the llvm-commits mailing list