[lld] r182077 - [lld][ELF][All Archs] Addend is used by dynamic relocations

Shankar Easwaran shankare at codeaurora.org
Thu May 16 22:10:30 PDT 2013


Author: shankare
Date: Fri May 17 00:10:30 2013
New Revision: 182077

URL: http://llvm.org/viewvc/llvm-project?rev=182077&view=rev
Log:
[lld][ELF][All Archs] Addend is used by dynamic relocations
only if they are relative. This removes the FIXME when the
relocations are being emitted and checks if the relocation
is relative and only then populates the addend information.

I couldnt add a testcase for this as llvm-readobj lacks     
functionality of printing dynamic relocations.              

When the functionality is added, remove the commented lines
from elf/ifunc.test to test functionality.

Modified:
    lld/trunk/include/lld/ReaderWriter/ELFTargetInfo.h
    lld/trunk/lib/ReaderWriter/ELF/ELFTargetInfo.cpp
    lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonTargetInfo.h
    lld/trunk/lib/ReaderWriter/ELF/PPC/PPCTargetInfo.h
    lld/trunk/lib/ReaderWriter/ELF/SectionChunks.h
    lld/trunk/lib/ReaderWriter/ELF/X86/X86TargetInfo.h
    lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64TargetInfo.h
    lld/trunk/test/elf/ifunc.test

Modified: lld/trunk/include/lld/ReaderWriter/ELFTargetInfo.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/ELFTargetInfo.h?rev=182077&r1=182076&r2=182077&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/ELFTargetInfo.h (original)
+++ lld/trunk/include/lld/ReaderWriter/ELFTargetInfo.h Fri May 17 00:10:30 2013
@@ -86,7 +86,10 @@ public:
   }
 
   /// \brief Does the output have dynamic sections.
-  bool isDynamic() const;
+  virtual bool isDynamic() const;
+
+  /// \brief Is the relocation a relative relocation
+  virtual bool isRelativeReloc(const Reference &r) const;
 
   template <typename ELFT>
   lld::elf::TargetHandler<ELFT> &getTargetHandler() const {

Modified: lld/trunk/lib/ReaderWriter/ELF/ELFTargetInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/ELFTargetInfo.cpp?rev=182077&r1=182076&r2=182077&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/ELFTargetInfo.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/ELFTargetInfo.cpp Fri May 17 00:10:30 2013
@@ -91,8 +91,11 @@ bool ELFTargetInfo::isDynamic() const {
   return false;
 }
 
-error_code ELFTargetInfo::parseFile(std::unique_ptr<MemoryBuffer> &mb,
-                          std::vector<std::unique_ptr<File>> &result) const {
+bool ELFTargetInfo::isRelativeReloc(const Reference &) const { return false; }
+
+error_code
+ELFTargetInfo::parseFile(std::unique_ptr<MemoryBuffer> &mb,
+                         std::vector<std::unique_ptr<File> > &result) const {
   error_code ec = _elfReader->parseFile(mb, result);
   if (ec) {
     // Not an ELF file, check file extension to see if it might be yaml

Modified: lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonTargetInfo.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonTargetInfo.h?rev=182077&r1=182076&r2=182077&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonTargetInfo.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonTargetInfo.h Fri May 17 00:10:30 2013
@@ -22,8 +22,9 @@ namespace elf {
 
 class HexagonTargetInfo LLVM_FINAL : public ELFTargetInfo {
 public:
-  HexagonTargetInfo(llvm::Triple triple) 
-      : ELFTargetInfo(triple, std::unique_ptr<TargetHandlerBase>(new HexagonTargetHandler(*this))) {}
+  HexagonTargetInfo(llvm::Triple triple)
+      : ELFTargetInfo(triple, std::unique_ptr<TargetHandlerBase>(
+                                  new HexagonTargetHandler(*this))) {}
 
   virtual ErrorOr<Reference::Kind> relocKindFromString(StringRef str) const;
   virtual ErrorOr<std::string> stringFromRelocKind(Reference::Kind kind) const;
@@ -48,6 +49,17 @@ public:
       return true;
     default:
       return false;
+    }
+  }
+
+  /// \brief Hexagon has only one relative relocation
+  /// a) for supporting relative relocs - R_HEX_RELATIVE
+  virtual bool isRelativeReloc(const Reference &r) const {
+    switch (r.kind()) {
+    case llvm::ELF::R_HEX_RELATIVE:
+      return true;
+    default:
+      return false;
     }
   }
 };

Modified: lld/trunk/lib/ReaderWriter/ELF/PPC/PPCTargetInfo.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/PPC/PPCTargetInfo.h?rev=182077&r1=182076&r2=182077&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/PPC/PPCTargetInfo.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/PPC/PPCTargetInfo.h Fri May 17 00:10:30 2013
@@ -25,6 +25,10 @@ public:
     : ELFTargetInfo(triple, std::unique_ptr<TargetHandlerBase>(new PPCTargetHandler(*this))) {}
 
   virtual bool isLittleEndian() const { return false; }
+
+  /// \brief PPC has no relative relocations defined
+  virtual bool isRelativeReloc(const Reference &) const { return false; }
+
   virtual ErrorOr<Reference::Kind> relocKindFromString(StringRef str) const;
   virtual ErrorOr<std::string> stringFromRelocKind(Reference::Kind kind) const;
 };

Modified: lld/trunk/lib/ReaderWriter/ELF/SectionChunks.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/SectionChunks.h?rev=182077&r1=182076&r2=182077&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/SectionChunks.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/SectionChunks.h Fri May 17 00:10:30 2013
@@ -933,10 +933,9 @@ public:
       r->setSymbolAndType(index, rel.second->kind());
       r->r_offset =
           writer->addressOfAtom(rel.first) + rel.second->offsetInAtom();
-      // FIXME: The addend is used only by IRELATIVE relocations while static
-      // linking executable statically, check to see how does dynamic linking
-      // work with IFUNC and change accordingly
-      if (!this->_targetInfo.isDynamic())
+      r->r_addend = 0;
+      // The addend is used only by relative relocations
+      if (this->_targetInfo.isRelativeReloc(*rel.second))
         r->r_addend =
             writer->addressOfAtom(rel.second->target()) + rel.second->addend();
       dest += sizeof(Elf_Rela);

Modified: lld/trunk/lib/ReaderWriter/ELF/X86/X86TargetInfo.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/X86/X86TargetInfo.h?rev=182077&r1=182076&r2=182077&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/X86/X86TargetInfo.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/X86/X86TargetInfo.h Fri May 17 00:10:30 2013
@@ -21,9 +21,22 @@ namespace lld {
 namespace elf {
 class X86TargetInfo LLVM_FINAL : public ELFTargetInfo {
 public:
-  X86TargetInfo(llvm::Triple triple) 
-      : ELFTargetInfo(triple, std::unique_ptr<TargetHandlerBase>(new X86TargetHandler(*this))) {}
+  X86TargetInfo(llvm::Triple triple)
+      : ELFTargetInfo(triple, std::unique_ptr<TargetHandlerBase>(
+                                  new X86TargetHandler(*this))) {}
 
+  /// \brief X86 has only two relative relocation
+  /// a) for supporting IFUNC relocs - R_386_IRELATIVE
+  /// b) for supporting relative relocs - R_386_RELATIVE
+  virtual bool isRelativeReloc(const Reference &r) const {
+    switch (r.kind()) {
+    case llvm::ELF::R_386_IRELATIVE:
+    case llvm::ELF::R_386_RELATIVE:
+      return true;
+    default:
+      return false;
+    }
+  }
   virtual ErrorOr<Reference::Kind> relocKindFromString(StringRef str) const;
   virtual ErrorOr<std::string> stringFromRelocKind(Reference::Kind kind) const;
 };

Modified: lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64TargetInfo.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64TargetInfo.h?rev=182077&r1=182076&r2=182077&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64TargetInfo.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64TargetInfo.h Fri May 17 00:10:30 2013
@@ -28,8 +28,9 @@ enum {
 
 class X86_64TargetInfo LLVM_FINAL : public ELFTargetInfo {
 public:
-  X86_64TargetInfo(llvm::Triple triple) 
-    : ELFTargetInfo(triple, std::unique_ptr<TargetHandlerBase>(new X86_64TargetHandler(*this))) {}
+  X86_64TargetInfo(llvm::Triple triple)
+      : ELFTargetInfo(triple, std::unique_ptr<TargetHandlerBase>(
+                                  new X86_64TargetHandler(*this))) {}
 
   virtual void addPasses(PassManager &) const;
 
@@ -58,6 +59,19 @@ public:
       return true;
     default:
       return false;
+    }
+  }
+
+  /// \brief X86_64 has two relative relocations
+  /// a) for supporting IFUNC - R_X86_64_IRELATIVE
+  /// b) for supporting relative relocs - R_X86_64_RELATIVE
+  virtual bool isRelativeReloc(const Reference &r) const {
+    switch (r.kind()) {
+    case llvm::ELF::R_X86_64_IRELATIVE:
+    case llvm::ELF::R_X86_64_RELATIVE:
+      return true;
+    default:
+      return false;
     }
   }
 

Modified: lld/trunk/test/elf/ifunc.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf/ifunc.test?rev=182077&r1=182076&r2=182077&view=diff
==============================================================================
--- lld/trunk/test/elf/ifunc.test (original)
+++ lld/trunk/test/elf/ifunc.test Fri May 17 00:10:30 2013
@@ -10,7 +10,9 @@ RUN: | FileCheck %s --check-prefix=PLT
 RUN: lld -flavor gnu -target x86_64-linux -o %t %p/Inputs/ifunc.x86-64 \
 RUN:   -e main -static %p/Inputs/ifunc.cpp.x86-64 \
 RUN: && llvm-objdump -d -s %t| FileCheck %s --check-prefix=BIN
-
+#REMOVE THE BELOW LINE WHEN llvm-readobj adds functionality to print 
+#Dynamic relocations 
+#llvm-readobj -r %t | FileCheck %s --check-prefix=RELATIVEADDEND
 
 PLT: defined-atoms:
 





More information about the llvm-commits mailing list