[lld] c3d98ea - [lld-macho] Support X86_64_RELOC_GOT

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 17 20:41:54 PDT 2020


Author: Jez Ng
Date: 2020-06-17T20:41:28-07:00
New Revision: c3d98ea89fa4807f06121ec186c00fa18593bfd0

URL: https://github.com/llvm/llvm-project/commit/c3d98ea89fa4807f06121ec186c00fa18593bfd0
DIFF: https://github.com/llvm/llvm-project/commit/c3d98ea89fa4807f06121ec186c00fa18593bfd0.diff

LOG: [lld-macho] Support X86_64_RELOC_GOT

Summary:
As far as I can tell, it's identical to _GOT_LOAD. llvm-mc has the following
comment explaining why _GOT exists:

```
// x86_64 distinguishes movq foo at GOTPCREL so that the linker can
// rewrite the movq to an leaq at link time if the symbol ends up in
// the same linkage unit.
```

Depends on D80855.

Reviewers: ruiu, pcc, MaskRay, smeenai, alexshap, gkm, Ktwu, christylee

Reviewed By: MaskRay, smeenai

Subscribers: llvm-commits

Tags: #llvm

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

Added: 
    

Modified: 
    lld/MachO/Arch/X86_64.cpp
    lld/test/MachO/dylink.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Arch/X86_64.cpp b/lld/MachO/Arch/X86_64.cpp
index 5c4966438306..6353cc241014 100644
--- a/lld/MachO/Arch/X86_64.cpp
+++ b/lld/MachO/Arch/X86_64.cpp
@@ -82,6 +82,7 @@ uint64_t X86_64::getImplicitAddend(MemoryBufferRef mb, const section_64 &sec,
   case X86_64_RELOC_SIGNED_2:
   case X86_64_RELOC_SIGNED_4:
   case X86_64_RELOC_GOT_LOAD:
+  case X86_64_RELOC_GOT:
     if (!rel.r_pcrel)
       fatal(getErrorLocation(mb, sec, rel) + ": relocations of type " +
             std::to_string(rel.r_type) + " must be pcrel");
@@ -120,6 +121,7 @@ void X86_64::relocateOne(uint8_t *loc, const Reloc &r, uint64_t val) const {
   case X86_64_RELOC_SIGNED_2:
   case X86_64_RELOC_SIGNED_4:
   case X86_64_RELOC_GOT_LOAD:
+  case X86_64_RELOC_GOT:
     // These types are only used for pc-relative relocations, so offset by 4
     // since the RIP has advanced by 4 at this point. This is only valid when
     // r_length = 2, which is enforced by validateLength().
@@ -209,13 +211,13 @@ void X86_64::writeStubHelperEntry(uint8_t *buf, const DylibSymbol &sym,
 void X86_64::prepareDylibSymbolRelocation(DylibSymbol &sym, uint8_t type) {
   switch (type) {
   case X86_64_RELOC_GOT_LOAD:
+    // TODO: implement mov -> lea relaxation for non-dynamic symbols
+  case X86_64_RELOC_GOT:
     in.got->addEntry(sym);
     break;
   case X86_64_RELOC_BRANCH:
     in.stubs->addEntry(sym);
     break;
-  case X86_64_RELOC_GOT:
-    fatal("TODO: Unhandled dylib symbol relocation X86_64_RELOC_GOT");
   default:
     llvm_unreachable("Unexpected dylib relocation type");
   }
@@ -224,11 +226,10 @@ void X86_64::prepareDylibSymbolRelocation(DylibSymbol &sym, uint8_t type) {
 uint64_t X86_64::getDylibSymbolVA(const DylibSymbol &sym, uint8_t type) const {
   switch (type) {
   case X86_64_RELOC_GOT_LOAD:
+  case X86_64_RELOC_GOT:
     return in.got->addr + sym.gotIndex * WordSize;
   case X86_64_RELOC_BRANCH:
     return in.stubs->addr + sym.stubsIndex * sizeof(stub);
-  case X86_64_RELOC_GOT:
-    fatal("TODO: Unhandled dylib symbol relocation X86_64_RELOC_GOT");
   default:
     llvm_unreachable("Unexpected dylib relocation type");
   }

diff  --git a/lld/test/MachO/dylink.s b/lld/test/MachO/dylink.s
index e47d9ef8f271..1909e380fc6a 100644
--- a/lld/test/MachO/dylink.s
+++ b/lld/test/MachO/dylink.s
@@ -19,7 +19,7 @@
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/dylink.o
 # RUN: lld -flavor darwinnew -o %t/dylink -Z -L%t -lhello -lgoodbye %t/dylink.o
-# RUN: llvm-objdump --bind -d %t/dylink | FileCheck %s
+# RUN: llvm-objdump --bind -d --no-show-raw-insn %t/dylink | FileCheck %s
 
 # CHECK: movq [[#%u, HELLO_OFF:]](%rip), %rsi
 # CHECK-NEXT: [[#%x, HELLO_RIP:]]:
@@ -27,8 +27,8 @@
 # CHECK: movq [[#%u, HELLO_ITS_ME_OFF:]](%rip), %rsi
 # CHECK-NEXT: [[#%x, HELLO_ITS_ME_RIP:]]:
 
-# CHECK: movq [[#%u, GOODBYE_OFF:]](%rip), %rsi
-# CHECK-NEXT: [[#%x, GOODBYE_RIP:]]:
+# CHECK: pushq [[#%u, GOODBYE_OFF:]](%rip)
+# CHECK-NEXT: [[#%x, GOODBYE_RIP:]]: popq %rsi
 
 # CHECK-LABEL: Bind table:
 # CHECK-DAG: __DATA_CONST __got 0x{{0*}}[[#%x, HELLO_RIP + HELLO_OFF]]               pointer 0 libhello   _hello_world
@@ -53,7 +53,8 @@ _main:
 
   movl $0x2000004, %eax # write() syscall
   mov $1, %rdi # stdout
-  movq _goodbye_world at GOTPCREL(%rip), %rsi
+  pushq _goodbye_world at GOTPCREL(%rip)
+  popq %rsi
   mov $15, %rdx # length of str
   syscall
   mov $0, %rax


        


More information about the llvm-commits mailing list