[lld] c931e12 - [lld/mac] Make sure __thread_ptrs is in front of __thread_bss

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 19 09:57:26 PDT 2021


Author: Nico Weber
Date: 2021-06-19T12:56:43-04:00
New Revision: c931e12b1d517ca2d0b1dbcb90f65b770046ef3e

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

LOG: [lld/mac] Make sure __thread_ptrs is in front of __thread_bss

The exact location doesn't matter, but it should be in front
of __thread_bss. We put it right in front of __thread_data
which is where ld64 seems to put it as well.

Fixes PR50769.

(As mentioned on the bug, there is probably a more structural
fix too, see comment 5. If we don't address this, it's likely
we'll run into this again with other synthetic sections. But
for now, let's fix the immediate breakage.)

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/lld/MachO/OutputSegment.cpp b/lld/MachO/OutputSegment.cpp
index 1be19e2ffdc3..9a775ce0e5ca 100644
--- a/lld/MachO/OutputSegment.cpp
+++ b/lld/MachO/OutputSegment.cpp
@@ -100,6 +100,8 @@ static int sectionOrder(OutputSection *osec) {
     // sections can be zerofills, we end up putting all TLV data sections at the
     // end of the segment.
     switch (sectionType(osec->flags)) {
+    case S_THREAD_LOCAL_VARIABLE_POINTERS:
+      return std::numeric_limits<int>::max() - 3;
     case S_THREAD_LOCAL_REGULAR:
       return std::numeric_limits<int>::max() - 2;
     case S_THREAD_LOCAL_ZEROFILL:

diff  --git a/lld/test/MachO/tlv-dylib.s b/lld/test/MachO/tlv-dylib.s
index d02f78765675..2d452a2b9caa 100644
--- a/lld/test/MachO/tlv-dylib.s
+++ b/lld/test/MachO/tlv-dylib.s
@@ -26,6 +26,8 @@
 # CHECK-DAG: __DATA       __thread_ptrs  0x{{0*}}[[#%x, BAR]] pointer 0   libtlv   _bar
 # CHECK-DAG: __DATA_CONST __got          0x{{0*}}[[#%x, BAZ]] pointer 0   libtlv   _baz
 
+## Check `type` on the various TLV sections, and check that
+## nothing's after S_THREAD_LOCAL_ZEROFILL.
 # RUN: llvm-otool -lv %t/test | FileCheck --check-prefix=FLAGS %s
 # FLAGS:       sectname __got
 # FLAGS-NEXT:   segname __DATA_CONST
@@ -36,6 +38,15 @@
 # FLAGS-NEXT:    reloff 0
 # FLAGS-NEXT:    nreloc 0
 # FLAGS-NEXT:      type S_NON_LAZY_SYMBOL_POINTERS
+# FLAGS:       sectname __thread_vars
+# FLAGS-NEXT:   segname __DATA
+# FLAGS-NEXT:      addr
+# FLAGS-NEXT:      size 0x0000000000000030
+# FLAGS-NEXT:    offset
+# FLAGS-NEXT:     align
+# FLAGS-NEXT:    reloff 0
+# FLAGS-NEXT:    nreloc 0
+# FLAGS-NEXT:      type S_THREAD_LOCAL_VARIABLES
 # FLAGS:       sectname __thread_ptrs
 # FLAGS-NEXT:   segname __DATA
 # FLAGS-NEXT:      addr
@@ -45,6 +56,24 @@
 # FLAGS-NEXT:    reloff 0
 # FLAGS-NEXT:    nreloc 0
 # FLAGS-NEXT:      type S_THREAD_LOCAL_VARIABLE_POINTERS
+# FLAGS:       sectname __thread_data
+# FLAGS-NEXT:   segname __DATA
+# FLAGS-NEXT:      addr
+# FLAGS-NEXT:      size 0x0000000000000008
+# FLAGS-NEXT:    offset
+# FLAGS-NEXT:     align
+# FLAGS-NEXT:    reloff 0
+# FLAGS-NEXT:    nreloc 0
+# FLAGS-NEXT:      type S_THREAD_LOCAL_REGULAR
+# FLAGS:       sectname __thread_bss
+# FLAGS-NEXT:   segname __DATA
+# FLAGS-NEXT:      addr
+# FLAGS-NEXT:      size 0x0000000000000008
+# FLAGS-NEXT:    offset
+# FLAGS-NEXT:     align 2^3 (8)
+# FLAGS-NEXT:    reloff 0
+# FLAGS-NEXT:    nreloc 0
+# FLAGS-NEXT:      type S_THREAD_LOCAL_ZEROFILL
 
 #--- libtlv.s
 .section __DATA,__thread_vars,thread_local_variables
@@ -63,3 +92,22 @@ _main:
 ## Add a GOT entry to make sure we don't mix it up with TLVs
   mov _baz at GOTPCREL(%rip), %rax
   ret
+
+## Add some TLVs to test too, so that we can test the ordering
+## of __thread_ptrs, __thread_data, and __thread_bss.
+.section __DATA,__thread_data,thread_local_regular
+_tfoo$tlv$init:
+  .quad 123
+
+.tbss _tbaz$tlv$init, 8, 3
+
+.section __DATA,__thread_vars,thread_local_variables
+.globl  _tfoo, _tbar
+_tfoo:
+  .quad  __tlv_bootstrap
+  .quad  0
+  .quad  _tfoo$tlv$init
+_tbaz:
+  .quad  __tlv_bootstrap
+  .quad  0
+  .quad  _tbaz$tlv$init


        


More information about the llvm-commits mailing list