[llvm] [X86][MC] Support R_X86_64_CODE_4_GOTPC32_TLSDESC (PR #116908)
Feng Zou via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 21 02:40:25 PST 2024
https://github.com/fzou1 updated https://github.com/llvm/llvm-project/pull/116908
>From a4f65f2af4cd1da2b034f6f40a0512e23e98269d Mon Sep 17 00:00:00 2001
From: Feng Zou <feng.zou at intel.com>
Date: Fri, 15 Nov 2024 08:40:55 +0800
Subject: [PATCH 1/2] [X86][MC] Support R_X86_64_CODE_4_GOTPC32_TLSDESC
For
lea name at tlsdesc(%rip), %reg
add
R_X86_64_CODE_4_GOTPC32_TLSDESC = 45
if the instruction starts at 4 bytes before the relocation offset. This should
be used if reg is one of the additional general-purpose registers, r16-r31, in
Intel APX. It is similar to R_X86_64_GOTPC32_TLSDESC and linker optimization
must take the different instruction encoding into account.
Linker can convert the instructions with R_X86_64_CODE_4_GOTPC32_TLSDESC to
mov $name at tpoff, %reg
if the first byte of the instruction at the relocation offset - 4 is
0xd5 (namely, encoded w/REX2 prefix) when possible.
Binutils patch: https://github.com/bminor/binutils-gdb/commit/a533c8df598b5ef99c54a13e2b137c98b34b043c
Binutils mailthread: https://sourceware.org/pipermail/binutils/2023-December/131463.html
ABI discussion: https://groups.google.com/g/x86-64-abi/c/ACwD-UQXVDs/m/vrgTenKyFwAJ
Blog: https://kanrobert.github.io/rfc/All-about-APX-relocation
---
llvm/include/llvm/BinaryFormat/ELFRelocs/x86_64.def | 1 +
.../Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp | 2 ++
.../lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp | 1 +
llvm/test/MC/X86/tlsdesc-64.s | 12 ++++++++++++
4 files changed, 16 insertions(+)
diff --git a/llvm/include/llvm/BinaryFormat/ELFRelocs/x86_64.def b/llvm/include/llvm/BinaryFormat/ELFRelocs/x86_64.def
index 94b1ad9c1f9464..0c026cf443361a 100644
--- a/llvm/include/llvm/BinaryFormat/ELFRelocs/x86_64.def
+++ b/llvm/include/llvm/BinaryFormat/ELFRelocs/x86_64.def
@@ -45,3 +45,4 @@ ELF_RELOC(R_X86_64_GOTPCRELX, 41)
ELF_RELOC(R_X86_64_REX_GOTPCRELX, 42)
ELF_RELOC(R_X86_64_CODE_4_GOTPCRELX, 43)
ELF_RELOC(R_X86_64_CODE_4_GOTTPOFF, 44)
+ELF_RELOC(R_X86_64_CODE_4_GOTPC32_TLSDESC, 45)
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
index aa02934cc99963..0f056ecc71b756 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
@@ -191,6 +191,8 @@ static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
case MCSymbolRefExpr::VK_TLSCALL:
return ELF::R_X86_64_TLSDESC_CALL;
case MCSymbolRefExpr::VK_TLSDESC:
+ if ((unsigned)Kind == X86::reloc_riprel_4byte_relax_rex2)
+ return ELF::R_X86_64_CODE_4_GOTPC32_TLSDESC;
return ELF::R_X86_64_GOTPC32_TLSDESC;
case MCSymbolRefExpr::VK_TLSGD:
checkIs32(Ctx, Loc, Type);
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp
index f9916fa82b1ffd..f12275ffaba8bb 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp
@@ -666,6 +666,7 @@ void X86MCCodeEmitter::emitMemModRMByte(
case X86::SBB64rm:
case X86::SUB64rm:
case X86::XOR64rm:
+ case X86::LEA64r:
return Kind == REX2 ? X86::reloc_riprel_4byte_relax_rex2
: Kind == REX ? X86::reloc_riprel_4byte_relax_rex
: X86::reloc_riprel_4byte_relax;
diff --git a/llvm/test/MC/X86/tlsdesc-64.s b/llvm/test/MC/X86/tlsdesc-64.s
index ebe1710c3e869b..93e0f2bda0691d 100644
--- a/llvm/test/MC/X86/tlsdesc-64.s
+++ b/llvm/test/MC/X86/tlsdesc-64.s
@@ -17,3 +17,15 @@
leaq a at tlsdesc(%rip), %rax
call *a at tlscall(%rax)
addq %fs:0, %rax
+
+# PRINT: leaq a at tlsdesc(%rip), %r16
+# PRINT-NEXT: callq *a at tlscall(%r16)
+
+# CHECK: 12: leaq (%rip), %r16 # 0x1a <{{.*}}>
+# CHECK-NEXT: 0000000000000016: R_X86_64_CODE_4_GOTPC32_TLSDESC a-0x4
+# CHECK-NEXT: 1a: callq *(%r16)
+# CHECK-NEXT: 000000000000001a: R_X86_64_TLSDESC_CALL a
+
+leaq a at tlsdesc(%rip), %r16
+call *a at tlscall(%r16)
+addq %fs:0, %r16
\ No newline at end of file
>From a45ba4f15635da81e4c309263874296cdaad3976 Mon Sep 17 00:00:00 2001
From: Feng Zou <feng.zou at intel.com>
Date: Wed, 20 Nov 2024 11:08:15 +0800
Subject: [PATCH 2/2] Added trailing '\n'.
---
llvm/test/MC/X86/tlsdesc-64.s | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/test/MC/X86/tlsdesc-64.s b/llvm/test/MC/X86/tlsdesc-64.s
index 93e0f2bda0691d..d1d33817510904 100644
--- a/llvm/test/MC/X86/tlsdesc-64.s
+++ b/llvm/test/MC/X86/tlsdesc-64.s
@@ -28,4 +28,4 @@ addq %fs:0, %rax
leaq a at tlsdesc(%rip), %r16
call *a at tlscall(%r16)
-addq %fs:0, %r16
\ No newline at end of file
+addq %fs:0, %r16
More information about the llvm-commits
mailing list