Skip to content

Commit f714d96

Browse files
committed
Apply -fno-plt __tls_get_addr fix
Taken from https://reviews.llvm.org/D64304.
1 parent 2f4d367 commit f714d96

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

llvm/lib/Target/X86/X86MCInstLower.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,13 @@ 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+
bool UseGot = MMI->getModule()->getRtLibUseGOT() &&
714+
Ctx.getAsmInfo()->canRelaxRelocations();
709715

710716
if (Is64Bits) {
711717
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)