[lld] r336799 - [ELF] Update addends in non-allocatable sections for REL targets when creating a relocatable output.

Igor Kudrin via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 11 05:52:05 PDT 2018


Author: ikudrin
Date: Wed Jul 11 05:52:04 2018
New Revision: 336799

URL: http://llvm.org/viewvc/llvm-project?rev=336799&view=rev
Log:
[ELF] Update addends in non-allocatable sections for REL targets when creating a relocatable output.

This fixes PR37735.

Differential Revision: https://reviews.llvm.org/D48929

Added:
    lld/trunk/test/ELF/pr37735.s
Modified:
    lld/trunk/ELF/InputSection.cpp

Modified: lld/trunk/ELF/InputSection.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.cpp?rev=336799&r1=336798&r2=336799&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.cpp (original)
+++ lld/trunk/ELF/InputSection.cpp Wed Jul 11 05:52:04 2018
@@ -729,6 +729,23 @@ void InputSection::relocateNonAlloc(uint
   }
 }
 
+// This is used when '-r' is given.
+// For REL targets, InputSection::copyRelocations() may store artificial
+// relocations aimed to update addends. They are handled in relocateAlloc()
+// for allocatable sections, and this function does the same for
+// non-allocatable sections, such as sections with debug information.
+static void relocateNonAllocForRelocatable(InputSection *Sec, uint8_t *Buf) {
+  const unsigned Bits = Config->Is64 ? 64 : 32;
+
+  for (const Relocation &Rel : Sec->Relocations) {
+    // InputSection::copyRelocations() adds only R_ABS relocations.
+    assert(Rel.Expr == R_ABS);
+    uint8_t *BufLoc = Buf + Rel.Offset + Sec->OutSecOff;
+    uint64_t TargetVA = SignExtend64(Rel.Sym->getVA(Rel.Addend), Bits);
+    Target->relocateOne(BufLoc, Rel.Type, TargetVA);
+  }
+}
+
 template <class ELFT>
 void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
   if (Flags & SHF_ALLOC) {
@@ -737,7 +754,9 @@ void InputSectionBase::relocate(uint8_t
   }
 
   auto *Sec = cast<InputSection>(this);
-  if (Sec->AreRelocsRela)
+  if (Config->Relocatable)
+    relocateNonAllocForRelocatable(Sec, Buf);
+  else if (Sec->AreRelocsRela)
     Sec->relocateNonAlloc<ELFT>(Buf, Sec->template relas<ELFT>());
   else
     Sec->relocateNonAlloc<ELFT>(Buf, Sec->template rels<ELFT>());

Added: lld/trunk/test/ELF/pr37735.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/pr37735.s?rev=336799&view=auto
==============================================================================
--- lld/trunk/test/ELF/pr37735.s (added)
+++ lld/trunk/test/ELF/pr37735.s Wed Jul 11 05:52:04 2018
@@ -0,0 +1,12 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=i386-pc-linux-gnu %s -o %t.o
+# RUN: ld.lld -r %t.o %t.o -o %t1.o
+# RUN: llvm-objdump -s -section=.bar %t1.o | FileCheck %s
+
+.section .foo
+	.byte 0
+
+# CHECK:      Contents of section .bar:
+# CHECK-NEXT:  0000 00000000 01000000
+.section .bar
+	.dc.a .foo




More information about the llvm-commits mailing list