[lld] r253957 - [ELF/AArch64] Add support for R_AARCH64_ADR_GOT_PAGE and R_AARCH64_LD64_GOT_LO12_NC.

Igor Kudrin via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 23 22:48:31 PST 2015


Author: ikudrin
Date: Tue Nov 24 00:48:31 2015
New Revision: 253957

URL: http://llvm.org/viewvc/llvm-project?rev=253957&view=rev
Log:
[ELF/AArch64] Add support for R_AARCH64_ADR_GOT_PAGE and R_AARCH64_LD64_GOT_LO12_NC.

With these relocations, it is now possible to build a simple "hello world"
program for AArch64 Debian.

Differential revision: http://reviews.llvm.org/D14917

Added:
    lld/trunk/test/ELF/got-aarch64.s
Modified:
    lld/trunk/ELF/InputSection.cpp
    lld/trunk/ELF/Target.cpp
    lld/trunk/ELF/Target.h

Modified: lld/trunk/ELF/InputSection.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.cpp?rev=253957&r1=253956&r2=253957&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.cpp (original)
+++ lld/trunk/ELF/InputSection.cpp Tue Nov 24 00:48:31 2015
@@ -140,7 +140,8 @@ void InputSectionBase<ELFT>::relocate(
       Type = Target->getPltRefReloc(Type);
     } else if (Target->relocNeedsGot(Type, Body)) {
       SymVA = Out<ELFT>::Got->getEntryAddr(Body);
-      Type = Body.isTLS() ? Target->getTlsGotReloc() : Target->getGotRefReloc();
+      Type = Body.isTLS() ? Target->getTlsGotReloc()
+                          : Target->getGotRefReloc(Type);
     } else if (Target->relocPointsToGot(Type)) {
       SymVA = Out<ELFT>::Got->getVA();
       Type = Target->getPCRelReloc();

Modified: lld/trunk/ELF/Target.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Target.cpp?rev=253957&r1=253956&r2=253957&view=diff
==============================================================================
--- lld/trunk/ELF/Target.cpp (original)
+++ lld/trunk/ELF/Target.cpp Tue Nov 24 00:48:31 2015
@@ -94,6 +94,7 @@ public:
 class AArch64TargetInfo final : public TargetInfo {
 public:
   AArch64TargetInfo();
+  unsigned getGotRefReloc(unsigned Type) const override;
   unsigned getPltRefReloc(unsigned Type) const override;
   void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
   void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
@@ -153,6 +154,8 @@ bool TargetInfo::relocNeedsCopy(uint32_t
   return false;
 }
 
+unsigned TargetInfo::getGotRefReloc(unsigned Type) const { return GotRefReloc; }
+
 unsigned TargetInfo::getPltRefReloc(unsigned Type) const { return PCRelReloc; }
 
 bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; }
@@ -608,12 +611,15 @@ void PPC64TargetInfo::relocateOne(uint8_
 }
 
 AArch64TargetInfo::AArch64TargetInfo() {
+  GotReloc = R_AARCH64_GLOB_DAT;
   PltReloc = R_AARCH64_JUMP_SLOT;
   LazyRelocations = true;
   PltEntrySize = 16;
   PltZeroEntrySize = 32;
 }
 
+unsigned AArch64TargetInfo::getGotRefReloc(unsigned Type) const { return Type; }
+
 unsigned AArch64TargetInfo::getPltRefReloc(unsigned Type) const { return Type; }
 
 void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
@@ -663,7 +669,8 @@ void AArch64TargetInfo::writePltEntry(ui
 
 bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
                                       const SymbolBody &S) const {
-  return relocNeedsPlt(Type, S);
+  return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC ||
+         relocNeedsPlt(Type, S);
 }
 
 bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
@@ -728,6 +735,7 @@ void AArch64TargetInfo::relocateOne(uint
     updateAArch64Adr(Loc, X & 0x1FFFFF);
     break;
   }
+  case R_AARCH64_ADR_GOT_PAGE:
   case R_AARCH64_ADR_PREL_PG_HI21: {
     uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
     checkAArch64OutOfRange<33>(X, Type);
@@ -745,6 +753,12 @@ void AArch64TargetInfo::relocateOne(uint
     // No overflow check needed.
     or32le(Loc, (SA & 0xFFC) << 8);
     break;
+  case R_AARCH64_LD64_GOT_LO12_NC:
+    if (SA & 0x7)
+      error("Relocation R_AARCH64_LD64_GOT_LO12_NC not aligned");
+    // No overflow check needed.
+    or32le(Loc, (SA & 0xFF8) << 7);
+    break;
   case R_AARCH64_LDST64_ABS_LO12_NC:
     // No overflow check needed.
     or32le(Loc, (SA & 0xFF8) << 7);

Modified: lld/trunk/ELF/Target.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Target.h?rev=253957&r1=253956&r2=253957&view=diff
==============================================================================
--- lld/trunk/ELF/Target.h (original)
+++ lld/trunk/ELF/Target.h Tue Nov 24 00:48:31 2015
@@ -27,7 +27,6 @@ public:
   unsigned getPCRelReloc() const { return PCRelReloc; }
   unsigned getGotReloc() const { return GotReloc; }
   unsigned getPltReloc() const { return PltReloc; }
-  unsigned getGotRefReloc() const { return GotRefReloc; }
   unsigned getRelativeReloc() const { return RelativeReloc; }
   unsigned getTlsGotReloc() const { return TlsGotReloc; }
   unsigned getTlsPcRelGotReloc() const { return TlsPcRelGotReloc; }
@@ -44,6 +43,7 @@ public:
   bool supportsLazyRelocations() const { return LazyRelocations; }
   unsigned getGotHeaderEntriesNum() const { return GotHeaderEntriesNum; }
   unsigned getGotPltHeaderEntriesNum() const { return GotPltHeaderEntriesNum; }
+  virtual unsigned getGotRefReloc(unsigned Type) const;
   virtual unsigned getPltRefReloc(unsigned Type) const;
   virtual void writeGotHeaderEntries(uint8_t *Buf) const;
   virtual void writeGotPltHeaderEntries(uint8_t *Buf) const;

Added: lld/trunk/test/ELF/got-aarch64.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/got-aarch64.s?rev=253957&view=auto
==============================================================================
--- lld/trunk/test/ELF/got-aarch64.s (added)
+++ lld/trunk/test/ELF/got-aarch64.s Tue Nov 24 00:48:31 2015
@@ -0,0 +1,40 @@
+// RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-linux %s -o %t.o
+// RUN: ld.lld -shared %t.o -o %t.so
+// RUN: llvm-readobj -s -r %t.so | FileCheck %s
+// RUN: llvm-objdump -d %t.so | FileCheck --check-prefix=DISASM %s
+// REQUIRES: aarch64
+
+// CHECK:      Name: .got
+// CHECK-NEXT: Type: SHT_PROGBITS
+// CHECK-NEXT: Flags [
+// CHECK-NEXT:   SHF_ALLOC
+// CHECK-NEXT:   SHF_WRITE
+// CHECK-NEXT: ]
+// CHECK-NEXT: Address: 0x2098
+// CHECK-NEXT: Offset:
+// CHECK-NEXT: Size: 8
+// CHECK-NEXT: Link: 0
+// CHECK-NEXT: Info: 0
+// CHECK-NEXT: AddressAlignment: 8
+
+// CHECK:      Relocations [
+// CHECK-NEXT:   Section ({{.*}}) .rela.dyn {
+// CHECK-NEXT:     0x2098 R_AARCH64_GLOB_DAT dat 0x0
+// CHECK-NEXT:   }
+// CHECK-NEXT: ]
+
+// Page(0x2098) - Page(0x1000) = 0x1000 = 4096
+// 0x2098 & 0xff8 = 0x98 = 152
+
+// DISASM: main:
+// DISASM-NEXT:     1000: {{.*}} adrp x0, #4096
+// DISASM-NEXT:     1004: {{.*}} ldr x0, [x0, #152]
+
+.global main,foo,dat
+.text
+main:
+    adrp x0, :got:dat
+    ldr x0, [x0, :got_lo12:dat]
+.data
+dat:
+    .word 42




More information about the llvm-commits mailing list