[lld] 6939fe6 - [lld-macho] Support X86_64_RELOC_SIGNED_{1,2,4}

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon May 4 15:15:44 PDT 2020


Author: Fangrui Song
Date: 2020-05-04T15:15:35-07:00
New Revision: 6939fe6e0853638c2f25569e4b42a1363914d7f9

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

LOG: [lld-macho] Support X86_64_RELOC_SIGNED_{1,2,4}

We currently only support extern relocations.
`X86_64_RELOC_SIGNED_{1,2,4}` are like X86_64_RELOC_SIGNED, but with the
implicit addend fixed to 1, 2, and 4, respectively.
See the comment in `lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp RecordX86_64Relocation`.

Reviewed By: int3

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

Added: 
    lld/test/MachO/x86-64-reloc-signed.s

Modified: 
    lld/MachO/Arch/X86_64.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Arch/X86_64.cpp b/lld/MachO/Arch/X86_64.cpp
index 2fcfab5df28c..6c13ac53c747 100644
--- a/lld/MachO/Arch/X86_64.cpp
+++ b/lld/MachO/Arch/X86_64.cpp
@@ -33,6 +33,9 @@ uint64_t X86_64::getImplicitAddend(const uint8_t *loc, uint8_t type) const {
   switch (type) {
   case X86_64_RELOC_BRANCH:
   case X86_64_RELOC_SIGNED:
+  case X86_64_RELOC_SIGNED_1:
+  case X86_64_RELOC_SIGNED_2:
+  case X86_64_RELOC_SIGNED_4:
   case X86_64_RELOC_GOT_LOAD:
     return read32le(loc);
   default:
@@ -45,6 +48,9 @@ void X86_64::relocateOne(uint8_t *loc, uint8_t type, uint64_t val) const {
   switch (type) {
   case X86_64_RELOC_BRANCH:
   case X86_64_RELOC_SIGNED:
+  case X86_64_RELOC_SIGNED_1:
+  case X86_64_RELOC_SIGNED_2:
+  case X86_64_RELOC_SIGNED_4:
   case X86_64_RELOC_GOT_LOAD:
     // These types are only used for pc-relative relocations, so offset by 4
     // since the RIP has advanced by 4 at this point.

diff  --git a/lld/test/MachO/x86-64-reloc-signed.s b/lld/test/MachO/x86-64-reloc-signed.s
new file mode 100644
index 000000000000..9ff91567d764
--- /dev/null
+++ b/lld/test/MachO/x86-64-reloc-signed.s
@@ -0,0 +1,37 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
+# RUN: lld -flavor darwinnew -o %t %t.o
+# RUN: llvm-objdump -d %t | FileCheck %s
+
+# CHECK:      <_main>:
+# CHECK-NEXT:   movl {{.*}}  # 2000 <_s>
+# CHECK-NEXT:   callq {{.*}}
+# CHECK-NEXT:   movl {{.*}}  # 2002 <_s+0x2>
+# CHECK-NEXT:   callq {{.*}}
+# CHECK-NEXT:   movb {{.*}}  # 2000 <_s>
+# CHECK-NEXT:   callq {{.*}}
+
+.section __TEXT,__text
+.globl _main
+_main:
+  movl $0x434241, _s(%rip)  # X86_64_RELOC_SIGNED_4
+  callq _f
+  movl $0x44, _s+2(%rip)    # X86_64_RELOC_SIGNED_2
+  callq _f
+  movb $0x45, _s(%rip)      # X86_64_RELOC_SIGNED_1
+  callq _f
+  xorq %rax, %rax
+  ret
+
+_f:
+  movl $0x2000004, %eax # write() syscall
+  mov $1, %rdi # stdout
+  leaq _s(%rip), %rsi
+  mov $3, %rdx # length
+  syscall
+  ret
+
+.section __DATA,__data
+.globl _s
+_s:
+  .space 5


        


More information about the llvm-commits mailing list