[PATCH] D48929: [ELF] Update addends in non-allocatable sections for REL targets when creating a relocatable output.

Igor Kudrin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 5 21:23:13 PDT 2018


ikudrin updated this revision to Diff 154350.
ikudrin added a comment.

- Shortened relocateNonAllocForRelocatable();
- Made it static;
- Removed an unused argument;
- Changed an expression for Bits.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D48929

Files:
  ELF/InputSection.cpp
  test/ELF/pr37735.s


Index: test/ELF/pr37735.s
===================================================================
--- /dev/null
+++ test/ELF/pr37735.s
@@ -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
Index: ELF/InputSection.cpp
===================================================================
--- ELF/InputSection.cpp
+++ ELF/InputSection.cpp
@@ -727,15 +727,34 @@
   }
 }
 
+// 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) {
     relocateAlloc(Buf, BufEnd);
     return;
   }
 
   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>());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48929.154350.patch
Type: text/x-patch
Size: 1934 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180706/264cf564/attachment.bin>


More information about the llvm-commits mailing list