[llvm-branch-commits] [lld] 3184519 - [lld-macho] Don't emit rebase opcodes for relocs in TLV sections

Jez Ng via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Dec 15 13:05:14 PST 2020


Author: Jez Ng
Date: 2020-12-15T15:58:26-05:00
New Revision: 31845199094418173a3beadb786767b860bfda03

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

LOG: [lld-macho] Don't emit rebase opcodes for relocs in TLV sections

Their addresses are already encoded as section-relative offsets, so
there's no need to rebase them at runtime. {D85080} has some context
on the weirdness of TLV sections.

Fixes llvm.org/PR48491.

Reviewed By: #lld-macho, thakis

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

Added: 
    

Modified: 
    lld/MachO/Arch/X86_64.cpp
    lld/test/MachO/tlv-dylib.s
    lld/test/MachO/tlv.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Arch/X86_64.cpp b/lld/MachO/Arch/X86_64.cpp
index 7f4024cc998dd..c776e21d6f5f5 100644
--- a/lld/MachO/Arch/X86_64.cpp
+++ b/lld/MachO/Arch/X86_64.cpp
@@ -248,7 +248,11 @@ void X86_64::prepareSymbolRelocation(lld::macho::Symbol *sym,
         return;
       }
     }
-    addNonLazyBindingEntries(sym, isec, r.offset, r.addend);
+    // References from thread-local variable sections are treated as offsets
+    // relative to the start of the referent section, and therefore have no
+    // need of rebase opcodes.
+    if (!(isThreadLocalVariables(isec->flags) && isa<Defined>(sym)))
+      addNonLazyBindingEntries(sym, isec, r.offset, r.addend);
     break;
   }
   case X86_64_RELOC_SIGNED:

diff  --git a/lld/test/MachO/tlv-dylib.s b/lld/test/MachO/tlv-dylib.s
index c6f9b9add3a0a..74e27e9e24d1b 100644
--- a/lld/test/MachO/tlv-dylib.s
+++ b/lld/test/MachO/tlv-dylib.s
@@ -4,9 +4,14 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/libtlv.s -o %t/libtlv.o
 # RUN: %lld -dylib -install_name @executable_path/libtlv.dylib \
 # RUN:   -lSystem -o %t/libtlv.dylib %t/libtlv.o
-# RUN: llvm-objdump --exports-trie -d --no-show-raw-insn %t/libtlv.dylib | FileCheck %s --check-prefix=DYLIB
+# RUN: llvm-objdump --macho --exports-trie --rebase %t/libtlv.dylib | \
+# RUN:   FileCheck %s --check-prefix=DYLIB
 # DYLIB-DAG: _foo [per-thread]
 # DYLIB-DAG: _bar [per-thread]
+## Make sure we don't emit rebase opcodes for relocations in __thread_vars.
+# DYLIB:       Rebase table:
+# DYLIB-NEXT:  segment  section            address     type
+# DYLIB-EMPTY:
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
 # RUN: %lld -lSystem -L%t -ltlv %t/test.o -o %t/test

diff  --git a/lld/test/MachO/tlv.s b/lld/test/MachO/tlv.s
index bf0dfcd02b565..d8d0a950d794c 100644
--- a/lld/test/MachO/tlv.s
+++ b/lld/test/MachO/tlv.s
@@ -1,9 +1,14 @@
 # REQUIRES: x86
 # RUN: mkdir -p %t
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o
+
 # RUN: %lld -lSystem -o %t/test %t/test.o
 # RUN: llvm-readobj --file-headers %t/test | FileCheck %s --check-prefix=HEADER
-# RUN: llvm-objdump -D %t/test | FileCheck %s
+# RUN: llvm-objdump -D --bind --rebase %t/test | FileCheck %s
+
+# RUN: %lld -lSystem -pie -o %t/test %t/test.o
+# RUN: llvm-readobj --file-headers %t/test | FileCheck %s --check-prefix=HEADER
+# RUN: llvm-objdump -D --bind --rebase %t/test | FileCheck %s
 
 # HEADER: MH_HAS_TLV_DESCRIPTORS
 
@@ -36,6 +41,13 @@
 # CHECK-NEXT:  00 00
 # CHECK-NEXT:  00 00
 
+## Make sure we don't emit rebase opcodes for relocations in __thread_vars.
+# CHECK:       Rebase table:
+# CHECK-NEXT:  segment  section            address     type
+# CHECK-NEXT:  Bind table:
+# CHECK:       __DATA  __thread_vars   0x{{[0-9a-f]*}}  pointer 0 libSystem __tlv_bootstrap
+# CHECK:       __DATA  __thread_vars   0x{{[0-9a-f]*}}  pointer 0 libSystem __tlv_bootstrap
+
 .globl _main
 _main:
   mov _foo at TLVP(%rip), %rax


        


More information about the llvm-branch-commits mailing list