Files
clang-p2996/lld/test/MachO/tlv.s
Jez Ng daaaed6bb8 [lld-macho] Fix TLV data initialization
We were mishandling the case where both `__tbss` and `__thread_data` sections were
present.

TLVP relocations should be encoded as offsets from the start of `__thread_data`,
even if the symbol is actually located in `__thread_bss`. Previously, we were
writing the offset from the start of the containing section, which doesn't
really make sense since there's no way `tlv_get_addr()` can know which section a
given `tlv$init` symbol is in at runtime.

In addition, this patch ensures that we place `__thread_data` immediately before
`__thread_bss`. This is what ld64 does, likely for performance reasons. Zerofill
sections must also be at the end of their segments; we were already doing this,
but now we ensure that `__thread_bss` occurs before `__bss`, so that it's always
possible to have it contiguous with `__thread_data`.

Fixes llvm.org/PR48657.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D94329
2021-01-08 18:48:12 -05:00

118 lines
4.2 KiB
ArmAsm

# REQUIRES: x86
# RUN: rm -rf %t; split-file %s %t
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/regular.s -o %t/regular.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/tbss.s -o %t/tbss.o
# RUN: %lld -lSystem -no_pie -o %t/regular %t/regular.o
# RUN: llvm-readobj --file-headers %t/regular | FileCheck %s --check-prefix=HEADER
# RUN: llvm-objdump -d --bind --rebase %t/regular | FileCheck %s --check-prefixes=REG,LINKEDIT
# RUN: llvm-objdump --macho --section=__DATA,__thread_vars %t/regular | \
# RUN: FileCheck %s --check-prefix=REG-TLVP
# RUN: %lld -lSystem -pie %t/regular.o -o %t/regular-pie
# RUN: llvm-readobj --file-headers %t/regular-pie | FileCheck %s --check-prefix=HEADER
# RUN: llvm-objdump -d --bind --rebase %t/regular-pie | FileCheck %s --check-prefixes=REG,LINKEDIT
# RUN: llvm-objdump --macho --section=__DATA,__thread_vars %t/regular-pie | \
# RUN: FileCheck %s --check-prefix=REG-TLVP
# RUN: %lld -lSystem %t/tbss.o -o %t/tbss -e _f
# RUN: llvm-objdump -d --bind --rebase %t/tbss | FileCheck %s --check-prefixes=TBSS,LINKEDIT
# RUN: llvm-objdump --macho --section=__DATA,__thread_vars %t/tbss | \
# RUN: FileCheck %s --check-prefix=TBSS-TLVP
# RUN: %lld -lSystem %t/regular.o %t/tbss.o -o %t/regular-and-tbss
# RUN: llvm-objdump -d --bind --rebase %t/regular-and-tbss | FileCheck %s --check-prefixes=REG,TBSS,LINKEDIT
# RUN: llvm-objdump --macho --section=__DATA,__thread_vars %t/regular-and-tbss | \
# RUN: FileCheck %s --check-prefix=REG-TBSS-TLVP
# RUN: llvm-objdump --section-headers %t/regular-and-tbss | FileCheck %s --check-prefix=SECTION-ORDER
## Check that we always put __thread_bss immediately after __thread_data,
## regardless of the order of the input files.
# RUN: %lld -lSystem %t/tbss.o %t/regular.o -o %t/regular-and-tbss
# RUN: llvm-objdump --section-headers %t/regular-and-tbss | FileCheck %s --check-prefix=SECTION-ORDER
# HEADER: MH_HAS_TLV_DESCRIPTORS
# REG: <_main>:
# REG-NEXT: leaq {{.*}}(%rip), %rax # {{.*}} <_foo>
# REG-NEXT: leaq {{.*}}(%rip), %rax # {{.*}} <_bar>
# REG-NEXT: retq
# TBSS: <_f>:
# TBSS-NEXT: leaq {{.*}}(%rip), %rax # {{.*}} <_baz>
# TBSS-NEXT: leaq {{.*}}(%rip), %rax # {{.*}} <_qux>
# TBSS-NEXT: retq
# REG-TLVP: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# REG-TLVP-NEXT: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# REG-TLVP-NEXT: 00 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00
# TBSS-TLVP: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# TBSS-TLVP-NEXT: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# TBSS-TLVP-NEXT: 00 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00
# REG-TBSS-TLVP: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# REG-TBSS-TLVP-NEXT: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# REG-TBSS-TLVP-NEXT: 00 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00
# REG-TBSS-TLVP-NEXT: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# REG-TBSS-TLVP-NEXT: 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# REG-TBSS-TLVP-NEXT: 00 00 00 00 00 00 00 00 18 00 00 00 00 00 00 00
## Make sure we don't emit rebase opcodes for relocations in __thread_vars.
# LINKEDIT: Rebase table:
# LINKEDIT-NEXT: segment section address type
# LINKEDIT-NEXT: Bind table:
# LINKEDIT: __DATA __thread_vars 0x{{[0-9a-f]*}} pointer 0 libSystem __tlv_bootstrap
# LINKEDIT: __DATA __thread_vars 0x{{[0-9a-f]*}} pointer 0 libSystem __tlv_bootstrap
# SECTION-ORDER: __thread_data
# SECTION-ORDER: more_thread_data
# SECTION-ORDER-NEXT: __thread_bss
#--- regular.s
.globl _main
_main:
mov _foo@TLVP(%rip), %rax
mov _bar@TLVP(%rip), %rax
ret
.section __DATA,__thread_data,thread_local_regular
_foo$tlv$init:
.quad 123
.section __DATA,more_thread_data,thread_local_regular
_bar$tlv$init:
.quad 123
.section __DATA,__thread_vars,thread_local_variables
.globl _foo, _bar
_foo:
.quad __tlv_bootstrap
.quad 0
.quad _foo$tlv$init
_bar:
.quad __tlv_bootstrap
.quad 0
.quad _bar$tlv$init
#--- tbss.s
.globl _f
_f:
mov _baz@TLVP(%rip), %rax
mov _qux@TLVP(%rip), %rax
ret
.tbss _baz$tlv$init, 8, 3
.tbss _qux$tlv$init, 8, 3
.section __DATA,__thread_vars,thread_local_variables
_baz:
.quad __tlv_bootstrap
.quad 0
.quad _baz$tlv$init
_qux:
.quad __tlv_bootstrap
.quad 0
.quad _qux$tlv$init