Skip to content

Commit f9ca13c

Browse files
committed
[X86] -fno-plt: use GOT __tls_get_addr only if GOTPCRELX is enabled
Summary: As of binutils 2.32, ld has a bogus TLS relaxation error when the GD/LD code sequence using R_X86_64_GOTPCREL (instead of R_X86_64_GOTPCRELX) is attempted to be relaxed to IE/LE (binutils PR24784). gold and lld are good. In gcc/config/i386/i386.md, there is a configure-time check of as/ld support and the GOT relaxation will not be used if as/ld doesn't support it: if (flag_plt || !HAVE_AS_IX86_TLS_GET_ADDR_GOT) return "call\t%P2"; return "call\t{*%p2@GOT(%1)|[DWORD PTR %p2@GOT[%1]]}"; In clang, -DENABLE_X86_RELAX_RELOCATIONS=OFF is the default. The ld.bfd bogus error can be reproduced with: thread_local int a; int main() { return a; } clang -fno-plt -fpic a.cc -fuse-ld=bfd GOTPCRELX gained relative good support in 2016, which is considered relatively new. It is even difficult to conditionally default to -DENABLE_X86_RELAX_RELOCATIONS=ON due to cross compilation reasons. So work around the ld.bfd bug by only using GOT when GOTPCRELX is enabled. Reviewers: dalias, hjl.tools, nikic, rnk Reviewed By: nikic Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D64304 llvm-svn: 365752
1 parent 63f5235 commit f9ca13c

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

llvm/lib/Target/X86/X86MCInstLower.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,14 @@ void X86AsmPrinter::LowerTlsAddr(X86MCInstLower &MCInstLowering,
705705

706706
const MCSymbolRefExpr *Sym = MCSymbolRefExpr::create(
707707
MCInstLowering.GetSymbolFromOperand(MI.getOperand(3)), SRVK, Ctx);
708-
bool UseGot = MMI->getModule()->getRtLibUseGOT();
708+
709+
// As of binutils 2.32, ld has a bogus TLS relaxation error when the GD/LD
710+
// code sequence using R_X86_64_GOTPCREL (instead of R_X86_64_GOTPCRELX) is
711+
// attempted to be relaxed to IE/LE (binutils PR24784). Work around the bug by
712+
// only using GOT when GOTPCRELX is enabled.
713+
// TODO Delete the workaround when GOTPCRELX becomes commonplace.
714+
bool UseGot = MMI->getModule()->getRtLibUseGOT() &&
715+
Ctx.getAsmInfo()->canRelaxRelocations();
709716

710717
if (Is64Bits) {
711718
bool NeedsPadding = SRVK == MCSymbolRefExpr::VK_TLSGD;

llvm/test/CodeGen/X86/tls-no-plt.ll

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
; RUN: llc < %s -mtriple=i386-linux-musl -relocation-model=pic | FileCheck --check-prefixes=CHECK,X86 %s
2-
; RUN: llc < %s -mtriple=x86_64-linux-musl -relocation-model=pic | FileCheck --check-prefixes=CHECK,X64 %s
1+
; RUN: llc < %s -mtriple=i386-linux-musl -relocation-model=pic -relax-elf-relocations=true | FileCheck --check-prefixes=CHECK,X86 %s
2+
; RUN: llc < %s -mtriple=x86_64-linux-musl -relocation-model=pic -relax-elf-relocations=true | FileCheck --check-prefixes=CHECK,X64 %s
3+
4+
;; If GOTPCRELX is disabled, don't use GOT for __tls_get_addr to work around
5+
;; a ld.bfd bug (binutils PR24784).
6+
; RUN: llc < %s -mtriple=i386-linux-musl -relocation-model=pic | FileCheck --check-prefixes=CHECK,X86-PLT %s
7+
; RUN: llc < %s -mtriple=x86_64-linux-musl -relocation-model=pic | FileCheck --check-prefixes=CHECK,X64-PLT %s
38

49
@gd = thread_local global i32 0
510
@ld = internal thread_local global i32 0
@@ -9,9 +14,11 @@ entry:
914
; CHECK-LABEL: get_gd:
1015
; X86: leal gd@TLSGD(%ebx), %eax
1116
; X86: calll *___tls_get_addr@GOT(%ebx)
17+
; X86-PLT: calll ___tls_get_addr@PLT
1218

1319
; X64: leaq gd@TLSGD(%rip), %rdi
1420
; X64: callq *__tls_get_addr@GOTPCREL(%rip)
21+
; X64-PLT: callq __tls_get_addr@PLT
1522
ret i32* @gd
1623
}
1724

@@ -20,9 +27,11 @@ define i32* @get_ld() {
2027
; CHECK-LABEL: get_ld:
2128
; X86: leal ld@TLSLDM(%ebx), %eax
2229
; X86: calll *___tls_get_addr@GOT(%ebx)
30+
; X86-PLT: calll ___tls_get_addr@PLT
2331

2432
; X64: leaq ld@TLSLD(%rip), %rdi
2533
; X64: callq *__tls_get_addr@GOTPCREL(%rip)
34+
; X64-PLT: callq __tls_get_addr@PLT
2635
ret i32* @ld
2736
}
2837

0 commit comments

Comments
 (0)