[llvm] r361910 - [X86] Fix x86-64 call *foo at tlsdesc(%rax) and support R_386_TLSGOTDESC R_386_TLS_DESC_CALL
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Tue May 28 19:02:59 PDT 2019
Author: maskray
Date: Tue May 28 19:02:59 2019
New Revision: 361910
URL: http://llvm.org/viewvc/llvm-project?rev=361910&view=rev
Log:
[X86] Fix x86-64 call *foo at tlsdesc(%rax) and support R_386_TLSGOTDESC R_386_TLS_DESC_CALL
D18885 emitted 5 bytes for call *foo at tlsdesc(%rax). It should use the
2-byte form instead and let R_X86_64_TLSDESC_CALL apply to the beginning
of the call instruction.
The 2-byte form was deliberately chosen to make ->LE and ->IE relaxation work:
0: 48 8d 05 00 00 00 00 lea 0x0(%rip),%rax # 7 <.text+0x7>
3: R_X86_64_GOTPC32_TLSDESC a-0x4
7: ff 10 callq *(%rax)
7: R_X86_64_TLSDESC_CALL a
=>
0: 48 c7 c0 fc ff ff ff mov $0xfffffffffffffffc,%rax
7: 66 90 xchg %ax,%ax
Also change the symbol type to STT_TLS when VK_TLSCALL or VK_TLSDESC is
seen.
Reviewed By: compnerd
Differential Revision: https://reviews.llvm.org/D62512
Added:
llvm/trunk/test/MC/X86/tlsdesc-32.s
llvm/trunk/test/MC/X86/tlsdesc-64.s
Removed:
llvm/trunk/test/MC/ELF/relocation-tls.s
Modified:
llvm/trunk/lib/MC/MCELFStreamer.cpp
llvm/trunk/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
llvm/trunk/lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp
Modified: llvm/trunk/lib/MC/MCELFStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCELFStreamer.cpp?rev=361910&r1=361909&r2=361910&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCELFStreamer.cpp (original)
+++ llvm/trunk/lib/MC/MCELFStreamer.cpp Tue May 28 19:02:59 2019
@@ -400,6 +400,8 @@ void MCELFStreamer::fixSymbolsInTLSFixup
case MCSymbolRefExpr::VK_INDNTPOFF:
case MCSymbolRefExpr::VK_NTPOFF:
case MCSymbolRefExpr::VK_GOTNTPOFF:
+ case MCSymbolRefExpr::VK_TLSCALL:
+ case MCSymbolRefExpr::VK_TLSDESC:
case MCSymbolRefExpr::VK_TLSGD:
case MCSymbolRefExpr::VK_TLSLD:
case MCSymbolRefExpr::VK_TLSLDM:
Modified: llvm/trunk/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp?rev=361910&r1=361909&r2=361910&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp (original)
+++ llvm/trunk/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp Tue May 28 19:02:59 2019
@@ -271,6 +271,10 @@ static unsigned getRelocType32(MCContext
assert(Type == RT32_32);
assert(!IsPCRel);
return ELF::R_386_GOTOFF;
+ case MCSymbolRefExpr::VK_TLSCALL:
+ return ELF::R_386_TLS_DESC_CALL;
+ case MCSymbolRefExpr::VK_TLSDESC:
+ return ELF::R_386_TLS_GOTDESC;
case MCSymbolRefExpr::VK_TPOFF:
assert(Type == RT32_32);
assert(!IsPCRel);
Modified: llvm/trunk/lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp?rev=361910&r1=361909&r2=361910&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp (original)
+++ llvm/trunk/lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp Tue May 28 19:02:59 2019
@@ -524,9 +524,23 @@ void X86MCCodeEmitter::emitMemModRMByte(
// indirect register encoding, this handles addresses like [EAX]. The
// encoding for [EBP] with no displacement means [disp32] so we handle it
// by emitting a displacement of 0 below.
- if (Disp.isImm() && Disp.getImm() == 0 && BaseRegNo != N86::EBP) {
- EmitByte(ModRMByte(0, RegOpcodeField, BaseRegNo), CurByte, OS);
- return;
+ if (BaseRegNo != N86::EBP) {
+ if (Disp.isImm() && Disp.getImm() == 0) {
+ EmitByte(ModRMByte(0, RegOpcodeField, BaseRegNo), CurByte, OS);
+ return;
+ }
+
+ // If the displacement is @tlscall, treat it as a zero.
+ if (Disp.isExpr()) {
+ auto *Sym = dyn_cast<MCSymbolRefExpr>(Disp.getExpr());
+ if (Sym && Sym->getKind() == MCSymbolRefExpr::VK_TLSCALL) {
+ // This is exclusively used by call *a at tlscall(base). The relocation
+ // (R_386_TLSCALL or R_X86_64_TLSCALL) applies to the beginning.
+ Fixups.push_back(MCFixup::create(0, Sym, FK_NONE, MI.getLoc()));
+ EmitByte(ModRMByte(0, RegOpcodeField, BaseRegNo), CurByte, OS);
+ return;
+ }
+ }
}
// Otherwise, if the displacement fits in a byte, encode as [REG+disp8].
Removed: llvm/trunk/test/MC/ELF/relocation-tls.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/relocation-tls.s?rev=361909&view=auto
==============================================================================
--- llvm/trunk/test/MC/ELF/relocation-tls.s (original)
+++ llvm/trunk/test/MC/ELF/relocation-tls.s (removed)
@@ -1,26 +0,0 @@
-// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | llvm-readobj -S --sr | FileCheck %s
-
-// Test that we produce the correct relocation.
-
- leaq _ZL3ccc at TLSDESC(%rip), %rax
- call *_ZL3ccc at TLSCALL(%rax)
- addq %fs:0, %rax
-
-// CHECK: Section {
-// CHECK: Index:
-// CHECK: Name: .rela.text
-// CHECK-NEXT: Type: SHT_RELA
-// CHECK-NEXT: Flags [
-// CHECK-NEXT: ]
-// CHECK-NEXT: Address: 0x0
-// CHECK-NEXT: Offset:
-// CHECK-NEXT: Size:
-// CHECK-NEXT: Link:
-// CHECK-NEXT: Info:
-// CHECK-NEXT: AddressAlignment: 8
-// CHECK-NEXT: EntrySize: 24
-// CHECK-NEXT: Relocations [
-// CHECK-NEXT: 0x3 R_X86_64_GOTPC32_TLSDESC _ZL3ccc 0xFFFFFFFFFFFFFFFC
-// CHECK-NEXT: 0x9 R_X86_64_TLSDESC_CALL _ZL3ccc 0x0
-// CHECK-NEXT: ]
-// CHECK-NEXT: }
Added: llvm/trunk/test/MC/X86/tlsdesc-32.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/X86/tlsdesc-32.s?rev=361910&view=auto
==============================================================================
--- llvm/trunk/test/MC/X86/tlsdesc-32.s (added)
+++ llvm/trunk/test/MC/X86/tlsdesc-32.s Tue May 28 19:02:59 2019
@@ -0,0 +1,19 @@
+# RUN: llvm-mc -triple i386-pc-linux-musl %s | FileCheck --check-prefix=PRINT %s
+
+# RUN: llvm-mc -filetype=obj -triple i386-pc-linux-musl %s -o %t
+# RUN: llvm-readelf -s %t | FileCheck --check-prefix=SYM %s
+# RUN: llvm-objdump -dr --no-show-raw-insn %t | FileCheck %s
+
+# PRINT: leal a at tlsdesc(%ebx), %eax
+# PRINT-NEXT: calll *a at tlscall(%eax)
+
+# SYM: TLS GLOBAL DEFAULT UND a
+
+# CHECK: 0: leal (%ebx), %eax
+# CHECK-NEXT: 00000002: R_386_TLS_GOTDESC a
+# CHECK-NEXT: 6: calll *(%eax)
+# CHECK-NEXT: 00000006: R_386_TLS_DESC_CALL a
+
+leal a at tlsdesc(%ebx), %eax
+call *a at tlscall(%eax)
+addl %gs:0, %eax
Added: llvm/trunk/test/MC/X86/tlsdesc-64.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/X86/tlsdesc-64.s?rev=361910&view=auto
==============================================================================
--- llvm/trunk/test/MC/X86/tlsdesc-64.s (added)
+++ llvm/trunk/test/MC/X86/tlsdesc-64.s Tue May 28 19:02:59 2019
@@ -0,0 +1,19 @@
+# RUN: llvm-mc -triple x86_64-pc-linux-musl %s | FileCheck --check-prefix=PRINT %s
+
+# RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-musl %s -o %t
+# RUN: llvm-readelf -s %t | FileCheck --check-prefix=SYM %s
+# RUN: llvm-objdump -dr --no-show-raw-insn %t | FileCheck --match-full-lines %s
+
+# PRINT: leaq a at tlsdesc(%rip), %rax
+# PRINT-NEXT: callq *a at tlscall(%rax)
+
+# SYM: TLS GLOBAL DEFAULT UND a
+
+# CHECK: 0: leaq (%rip), %rax
+# CHECK-NEXT: 0000000000000003: R_X86_64_GOTPC32_TLSDESC a-4
+# CHECK-NEXT: 7: callq *(%rax)
+# CHECK-NEXT: 0000000000000007: R_X86_64_TLSDESC_CALL a
+
+leaq a at tlsdesc(%rip), %rax
+call *a at tlscall(%rax)
+addq %fs:0, %rax
More information about the llvm-commits
mailing list