[lld] r247837 - Add DT_RELA and DT_RELASZ to the dynamic table.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 16 11:52:43 PDT 2015


Author: rafael
Date: Wed Sep 16 13:52:42 2015
New Revision: 247837

URL: http://llvm.org/viewvc/llvm-project?rev=247837&view=rev
Log:
Add DT_RELA and DT_RELASZ to the dynamic table.

Modified:
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/elf2/dynamic-reloc.s

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=247837&r1=247836&r2=247837&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Wed Sep 16 13:52:42 2015
@@ -326,17 +326,23 @@ class DynamicSection final : public Outp
   typedef typename Base::Elf_Dyn Elf_Dyn;
 
 public:
-  DynamicSection(SymbolTable &SymTab, HashTableSection<ELFT> &HashSec)
+  DynamicSection(SymbolTable &SymTab, HashTableSection<ELFT> &HashSec,
+                 RelocationSection<ELFT> &RelaDynSec)
       : OutputSectionBase<ELFT::Is64Bits>(".dynamic", SHT_DYNAMIC,
                                           SHF_ALLOC | SHF_WRITE),
         HashSec(HashSec), DynSymSec(HashSec.getDynSymSec()),
-        DynStrSec(DynSymSec.getStrTabSec()), SymTab(SymTab) {
+        DynStrSec(DynSymSec.getStrTabSec()), RelaDynSec(RelaDynSec),
+        SymTab(SymTab) {
     typename Base::HeaderT &Header = this->Header;
     Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
     Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
 
     unsigned NumEntries = 0;
 
+    if (RelaDynSec.hasReocs()) {
+      ++NumEntries; // DT_RELA
+      ++NumEntries; // DT_RELASZ
+    }
     ++NumEntries; // DT_SYMTAB
     ++NumEntries; // DT_STRTAB
     ++NumEntries; // DT_STRSZ
@@ -366,6 +372,16 @@ public:
   void writeTo(uint8_t *Buf) override {
     auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
 
+    if (RelaDynSec.hasReocs()) {
+      P->d_tag = DT_RELA;
+      P->d_un.d_ptr = RelaDynSec.getVA();
+      ++P;
+
+      P->d_tag = DT_RELASZ;
+      P->d_un.d_val = RelaDynSec.getSize();
+      ++P;
+    }
+
     P->d_tag = DT_SYMTAB;
     P->d_un.d_ptr = DynSymSec.getVA();
     ++P;
@@ -406,6 +422,7 @@ private:
   HashTableSection<ELFT> &HashSec;
   SymbolTableSection<ELFT> &DynSymSec;
   StringTableSection<ELFT::Is64Bits> &DynStrSec;
+  RelocationSection<ELFT> &RelaDynSec;
   SymbolTable &SymTab;
 };
 
@@ -420,7 +437,8 @@ public:
   typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
   Writer(SymbolTable *T)
       : SymTabSec(*this, *T, StrTabSec), DynSymSec(*this, *T, DynStrSec),
-        RelaDynSec(DynSymSec), HashSec(DynSymSec), DynamicSec(*T, HashSec) {}
+        RelaDynSec(DynSymSec), HashSec(DynSymSec),
+        DynamicSec(*T, HashSec, RelaDynSec) {}
   void run();
 
   const OutputSection<ELFT> &getBSS() const {

Modified: lld/trunk/test/elf2/dynamic-reloc.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/dynamic-reloc.s?rev=247837&r1=247836&r2=247837&view=diff
==============================================================================
--- lld/trunk/test/elf2/dynamic-reloc.s (original)
+++ lld/trunk/test/elf2/dynamic-reloc.s Wed Sep 16 13:52:42 2015
@@ -2,7 +2,7 @@
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/shared.s -o %t2.o
 // RUN: lld -flavor gnu2 -shared %t2.o -o %t2.so
 // RUN: lld -flavor gnu2 %t.o %t2.so -o %t
-// RUN: llvm-readobj -r --expand-relocs -s %t | FileCheck %s
+// RUN: llvm-readobj -dynamic-table -r --expand-relocs -s %t | FileCheck %s
 // REQUIRES: x86
 
 // CHECK:      Name: .text
@@ -21,9 +21,9 @@
 // CHECK-NEXT: Flags [
 // CHECK-NEXT:   SHF_ALLOC
 // CHECK-NEXT: ]
-// CHECK-NEXT: Address: 0x16000
+// CHECK-NEXT: Address: [[RELAADDR:.*]]
 // CHECK-NEXT: Offset: 0x6000
-// CHECK-NEXT: Size: 24
+// CHECK-NEXT: Size: [[RELASIZE:.*]]
 // CHECK-NEXT: Link: 4
 // CHECK-NEXT: Info: 0
 // CHECK-NEXT: AddressAlignment: 8
@@ -40,6 +40,11 @@
 // CHECK-NEXT:   }
 // CHECK-NEXT: ]
 
+// CHECK: DynamicSection [
+// CHECK-NEXT:  Tag                Type                 Name/Value
+// CHECK-NEXT:  0x0000000000000007 RELA                 [[RELAADDR]]
+// CHECK-NEXT:  0x0000000000000008 RELASZ               [[RELASIZE]] (bytes)
+
 .global _start
 _start:
 .quad bar + 0x42




More information about the llvm-commits mailing list