[PATCH] D47975: [ELF] Fix copy relocation when two symbols share the same address.

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 9 01:53:56 PDT 2018


MaskRay created this revision.
MaskRay added reviewers: ruiu, grimar.
Herald added subscribers: llvm-commits, arichardson, emaste.
Herald added a reviewer: espindola.

In glibc libc.so.6, the multiple versions of _sys_errlist share the same address. 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.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D47975

Files:
  ELF/Relocations.cpp


Index: ELF/Relocations.cpp
===================================================================
--- ELF/Relocations.cpp
+++ ELF/Relocations.cpp
@@ -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>
@@ -444,21 +445,23 @@
 // 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.
+// Use a set so that if two or more symbols share the same address, we only copy
+// one.
 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)
       continue;
     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;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47975.150609.patch
Type: text/x-patch
Size: 1421 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180609/4b0bc0c8/attachment.bin>


More information about the llvm-commits mailing list