[lld] r261576 - Don't create copy relocs in shared libraries.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 22 13:23:32 PST 2016


Author: rafael
Date: Mon Feb 22 15:23:29 2016
New Revision: 261576

URL: http://llvm.org/viewvc/llvm-project?rev=261576&view=rev
Log:
Don't create copy relocs in shared libraries.

This fixes PR26699.

Added:
    lld/trunk/test/ELF/Inputs/copy-in-shared.s
    lld/trunk/test/ELF/copy-in-shared.s
Modified:
    lld/trunk/ELF/Target.cpp
    lld/trunk/ELF/Target.h

Modified: lld/trunk/ELF/Target.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Target.cpp?rev=261576&r1=261575&r2=261576&view=diff
==============================================================================
--- lld/trunk/ELF/Target.cpp (original)
+++ lld/trunk/ELF/Target.cpp Mon Feb 22 15:23:29 2016
@@ -90,7 +90,7 @@ public:
   void writePltZero(uint8_t *Buf) const override;
   void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
                 int32_t Index, unsigned RelOff) const override;
-  bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
+  bool needsCopyRelImpl(uint32_t Type) const override;
   bool needsDynRelative(unsigned Type) const override;
   bool needsGot(uint32_t Type, SymbolBody &S) const override;
   PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const override;
@@ -125,7 +125,7 @@ public:
   void writePltZero(uint8_t *Buf) const override;
   void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
                 int32_t Index, unsigned RelOff) const override;
-  bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
+  bool needsCopyRelImpl(uint32_t Type) const override;
   bool needsGot(uint32_t Type, SymbolBody &S) const override;
   PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const override;
   void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
@@ -181,7 +181,7 @@ public:
                 int32_t Index, unsigned RelOff) const override;
   unsigned getTlsGotRel(unsigned Type) const override;
   bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
-  bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
+  bool needsCopyRelImpl(uint32_t Type) const override;
   bool needsGot(uint32_t Type, SymbolBody &S) const override;
   PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const override;
   void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
@@ -217,7 +217,7 @@ public:
   void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
                 int32_t Index, unsigned RelOff) const override;
   void writeGotHeader(uint8_t *Buf) const override;
-  bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
+  bool needsCopyRelImpl(uint32_t Type) const override;
   bool needsGot(uint32_t Type, SymbolBody &S) const override;
   PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const override;
   void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
@@ -263,8 +263,36 @@ bool TargetInfo::canRelaxTls(unsigned Ty
 
 uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
 
+bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
+
+template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
+  if (Config->Shared)
+    return false;
+  auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
+  if (!SS)
+    return false;
+  return SS->Sym.getType() == STT_OBJECT;
+}
+
 bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
-  return false;
+  bool MayNeed;
+  switch (Config->EKind) {
+  case ELF32LEKind:
+    MayNeed = mayNeedCopy<ELF32LE>(S);
+    break;
+  case ELF64LEKind:
+    MayNeed = mayNeedCopy<ELF64LE>(S);
+    break;
+  case ELF32BEKind:
+    MayNeed = mayNeedCopy<ELF32BE>(S);
+    break;
+  case ELF64BEKind:
+    MayNeed = mayNeedCopy<ELF64BE>(S);
+    break;
+  default:
+    llvm_unreachable("Invalid ELF kind");
+  }
+  return MayNeed && needsCopyRelImpl(Type);
 }
 
 bool TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
@@ -395,11 +423,8 @@ void X86TargetInfo::writePlt(uint8_t *Bu
   write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
 }
 
-bool X86TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
-  if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
-    if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
-      return SS->Sym.getType() == STT_OBJECT;
-  return false;
+bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
+  return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
 }
 
 bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
@@ -665,12 +690,9 @@ void X86_64TargetInfo::writePlt(uint8_t
   write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
 }
 
-bool X86_64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
-  if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
-      Type == R_X86_64_64)
-    if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
-      return SS->Sym.getType() == STT_OBJECT;
-  return false;
+bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
+  return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
+         Type == R_X86_64_64;
 }
 
 bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
@@ -736,9 +758,11 @@ TargetInfo::PltNeed X86_64TargetInfo::ne
     // that points to the real function is a dedicated got entry used by the
     // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
     // R_386_JMP_SLOT, etc).
-    if (!S.isShared())
-      return Plt_No;
-    return Plt_Implicit;
+    if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S)) {
+      if (SS->Sym.getType() == STT_FUNC)
+        return Plt_Implicit;
+    }
+    return Plt_No;
   case R_X86_64_PLT32:
     if (canBePreempted(&S, true))
       return Plt_Explicit;
@@ -1276,9 +1300,7 @@ bool AArch64TargetInfo::isTlsDynRel(unsi
          Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
 }
 
-bool AArch64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
-  if (Config->Shared)
-    return false;
+bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
   switch (Type) {
   default:
     return false;
@@ -1293,9 +1315,7 @@ bool AArch64TargetInfo::needsCopyRel(uin
   case R_AARCH64_LDST32_ABS_LO12_NC:
   case R_AARCH64_LDST64_ABS_LO12_NC:
   case R_AARCH64_LDST128_ABS_LO12_NC:
-    if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
-      return SS->Sym.getType() == STT_OBJECT;
-    return false;
+    return true;
   }
 }
 
@@ -1676,14 +1696,8 @@ void MipsTargetInfo<ELFT>::writePlt(uint
 }
 
 template <class ELFT>
-bool MipsTargetInfo<ELFT>::needsCopyRel(uint32_t Type,
-                                        const SymbolBody &S) const {
-  if (Config->Shared)
-    return false;
-  if (Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type))
-    if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S))
-      return SS->Sym.getType() == STT_OBJECT;
-  return false;
+bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
+  return Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type);
 }
 
 template <class ELFT>

Modified: lld/trunk/ELF/Target.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Target.h?rev=261576&r1=261575&r2=261576&view=diff
==============================================================================
--- lld/trunk/ELF/Target.h (original)
+++ lld/trunk/ELF/Target.h Mon Feb 22 15:23:29 2016
@@ -63,7 +63,7 @@ public:
                            uint8_t *PairedLoc = nullptr) const = 0;
   virtual bool isGotRelative(uint32_t Type) const;
   virtual bool canRelaxTls(unsigned Type, const SymbolBody *S) const;
-  virtual bool needsCopyRel(uint32_t Type, const SymbolBody &S) const;
+  bool needsCopyRel(uint32_t Type, const SymbolBody &S) const;
   virtual unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
                             uint64_t P, uint64_t SA, const SymbolBody *S) const;
   virtual ~TargetInfo();
@@ -91,6 +91,9 @@ public:
   unsigned GotHeaderEntriesNum = 0;
   unsigned GotPltHeaderEntriesNum = 3;
   bool UseLazyBinding = false;
+
+private:
+  virtual bool needsCopyRelImpl(uint32_t Type) const;
 };
 
 uint64_t getPPC64TocBase();

Added: lld/trunk/test/ELF/Inputs/copy-in-shared.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/copy-in-shared.s?rev=261576&view=auto
==============================================================================
--- lld/trunk/test/ELF/Inputs/copy-in-shared.s (added)
+++ lld/trunk/test/ELF/Inputs/copy-in-shared.s Mon Feb 22 15:23:29 2016
@@ -0,0 +1,3 @@
+.type foo, @object
+.global foo
+foo:

Added: lld/trunk/test/ELF/copy-in-shared.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/copy-in-shared.s?rev=261576&view=auto
==============================================================================
--- lld/trunk/test/ELF/copy-in-shared.s (added)
+++ lld/trunk/test/ELF/copy-in-shared.s Mon Feb 22 15:23:29 2016
@@ -0,0 +1,14 @@
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/copy-in-shared.s -o %t1.o
+// RUN: ld.lld -shared %t1.o -o %t1.so
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t2.o
+// RUN: ld.lld %t2.o %t1.so -o %t2.so -shared
+// RUN: llvm-readobj -r %t2.so | FileCheck %s
+// REQUIRES: x86
+
+.quad foo
+
+// CHECK:      Relocations [
+// CHECK-NEXT:   Section ({{.*}}) .rela.dyn {
+// CHECK-NEXT:     R_X86_64_64 foo 0x0
+// CHECK-NEXT:   }
+// CHECK-NEXT: ]




More information about the llvm-commits mailing list