[lld] cc5c03e - [lld-macho] Properly test subtractor relocations & fix their attributes

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 27 09:32:01 PST 2021


Author: Jez Ng
Date: 2021-02-27T12:31:34-05:00
New Revision: cc5c03e10957b8dd4ba4efbc5583084c7636a3ba

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

LOG: [lld-macho] Properly test subtractor relocations & fix their attributes

`llvm-mc` doesn't generate any relocations for subtractions
between local symbols -- they must be global -- so the previous test
wasn't actually testing any relocation logic. I've fixed that and
extended the test to cover r_length=3 relocations as well as both x86_64
and arm64.

Reviewed By: #lld-macho, smeenai

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

Added: 
    lld/test/MachO/reloc-subtractor.s

Modified: 
    lld/MachO/Arch/ARM64.cpp
    lld/MachO/Arch/X86_64.cpp

Removed: 
    lld/test/MachO/x86-64-reloc-subtract.s


################################################################################
diff  --git a/lld/MachO/Arch/ARM64.cpp b/lld/MachO/Arch/ARM64.cpp
index c67c9d5b6156..d70574c285d3 100644
--- a/lld/MachO/Arch/ARM64.cpp
+++ b/lld/MachO/Arch/ARM64.cpp
@@ -58,7 +58,7 @@ const TargetInfo::RelocAttrs &ARM64::getRelocAttrs(uint8_t type) const {
 #define B(x) RelocAttrBits::x
       {"UNSIGNED", B(UNSIGNED) | B(ABSOLUTE) | B(EXTERN) | B(LOCAL) |
                        B(DYSYM8) | B(BYTE4) | B(BYTE8)},
-      {"SUBTRACTOR", B(SUBTRAHEND) | B(BYTE8)},
+      {"SUBTRACTOR", B(SUBTRAHEND) | B(BYTE4) | B(BYTE8)},
       {"BRANCH26", B(PCREL) | B(EXTERN) | B(BRANCH) | B(BYTE4)},
       {"PAGE21", B(PCREL) | B(EXTERN) | B(BYTE4)},
       {"PAGEOFF12", B(ABSOLUTE) | B(EXTERN) | B(BYTE4)},

diff  --git a/lld/MachO/Arch/X86_64.cpp b/lld/MachO/Arch/X86_64.cpp
index c7bb49392c37..e7624b5e5558 100644
--- a/lld/MachO/Arch/X86_64.cpp
+++ b/lld/MachO/Arch/X86_64.cpp
@@ -51,7 +51,7 @@ const TargetInfo::RelocAttrs &X86_64::getRelocAttrs(uint8_t type) const {
       {"BRANCH", B(PCREL) | B(EXTERN) | B(BRANCH) | B(BYTE4)},
       {"GOT_LOAD", B(PCREL) | B(EXTERN) | B(GOT) | B(LOAD) | B(BYTE4)},
       {"GOT", B(PCREL) | B(EXTERN) | B(GOT) | B(POINTER) | B(BYTE4)},
-      {"SUBTRACTOR", B(SUBTRAHEND)},
+      {"SUBTRACTOR", B(SUBTRAHEND) | B(BYTE4) | B(BYTE8)},
       {"SIGNED_1", B(PCREL) | B(EXTERN) | B(LOCAL) | B(BYTE4)},
       {"SIGNED_2", B(PCREL) | B(EXTERN) | B(LOCAL) | B(BYTE4)},
       {"SIGNED_4", B(PCREL) | B(EXTERN) | B(LOCAL) | B(BYTE4)},

diff  --git a/lld/test/MachO/reloc-subtractor.s b/lld/test/MachO/reloc-subtractor.s
new file mode 100644
index 000000000000..14355f377182
--- /dev/null
+++ b/lld/test/MachO/reloc-subtractor.s
@@ -0,0 +1,47 @@
+# REQUIRES: x86, aarch64
+# RUN: rm -rf %t; mkdir %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/x86_64.o
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %s -o %t/arm64.o
+# RUN: %lld -lSystem %t/x86_64.o -o %t/x86_64
+# RUN: llvm-objdump --syms --full-contents %t/x86_64 | FileCheck %s
+# RUN: %lld -arch arm64 -lSystem %t/arm64.o -o %t/arm64
+# RUN: llvm-objdump --syms --full-contents %t/arm64 | FileCheck %s
+
+# CHECK-LABEL: SYMBOL TABLE:
+# CHECK:       {{0*}}[[#%x, SUB1ADDR:]] l {{.*}} __DATA,__data _sub1
+# CHECK:       {{0*}}[[#%x, SUB2ADDR:]] l {{.*}} __DATA,__data _sub2
+# CHECK:       {{0*}}[[#%x, SUB3ADDR:]] l {{.*}} __DATA,__data _sub3
+# CHECK:       {{0*}}[[#%x, SUB4ADDR:]] l {{.*}} __DATA,__data _sub4
+# CHECK-LABEL: Contents of section __DATA,__data:
+# CHECK:       [[#SUB1ADDR]] 10000000
+# CHECK-NEXT:  [[#SUB2ADDR]] f0ffffff
+# CHECK-NEXT:  [[#SUB3ADDR]] 10000000 00000000
+# CHECK-NEXT:  [[#SUB4ADDR]] f0ffffff ffffffff
+
+.globl _main, _subtrahend_1, _subtrahend_2, _minued1, _minued2
+
+.data
+_subtrahend_1:
+  .space 16
+_minuend_1:
+  .space 16
+_minuend_2:
+  .space 16
+_subtrahend_2:
+  .space 16
+_sub1:
+  .long _minuend_1 - _subtrahend_1
+  .space 12
+_sub2:
+  .long _minuend_2 - _subtrahend_2
+  .space 12
+_sub3:
+  .quad _minuend_1 - _subtrahend_1
+  .space 8
+_sub4:
+  .quad _minuend_2 - _subtrahend_2
+
+.text
+.p2align 2
+_main:
+  ret

diff  --git a/lld/test/MachO/x86-64-reloc-subtract.s b/lld/test/MachO/x86-64-reloc-subtract.s
deleted file mode 100644
index 6b16d0ee41a5..000000000000
--- a/lld/test/MachO/x86-64-reloc-subtract.s
+++ /dev/null
@@ -1,33 +0,0 @@
-# REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
-# RUN: %lld -o %t %t.o
-# RUN: llvm-objdump --syms --full-contents %t | FileCheck %s
-
-# CHECK-LABEL: SYMBOL TABLE:
-# CHECK: {{0*}}[[#%x, SUB1ADDR:]] g {{.*}} __DATA,subby _sub1
-# CHECK: {{0*}}[[#%x, SUB2ADDR:]] g {{.*}} __DATA,subby _sub2
-# CHECK-LABEL: Contents of section __DATA,subby:
-# CHECK: [[#SUB1ADDR]] 10000000
-# CHECK: [[#SUB2ADDR]] f0ffffff
-
-.globl _main, _sub1, _sub2
-
-.section __DATA,subby
-L_.subtrahend_1:
-  .space 16
-L_.minuend_1:
-  .space 16
-L_.minuend_2:
-  .space 16
-L_.subtrahend_2:
-  .space 16
-_sub1:
-  .long L_.minuend_1 - L_.subtrahend_1
-  .space 12
-_sub2:
-  .long L_.minuend_2 - L_.subtrahend_2
-
-.text
-_main:
-  mov $0, %rax
-  ret


        


More information about the llvm-commits mailing list