Skip to content

Commit 8223982

Browse files
authored
[X86][MC] Support R_X86_64_CODE_4_GOTPC32_TLSDESC (#116908)
For lea name@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@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: bminor/binutils-gdb@a533c8d 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
1 parent 2369a58 commit 8223982

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

llvm/include/llvm/BinaryFormat/ELFRelocs/x86_64.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ ELF_RELOC(R_X86_64_GOTPCRELX, 41)
4545
ELF_RELOC(R_X86_64_REX_GOTPCRELX, 42)
4646
ELF_RELOC(R_X86_64_CODE_4_GOTPCRELX, 43)
4747
ELF_RELOC(R_X86_64_CODE_4_GOTTPOFF, 44)
48+
ELF_RELOC(R_X86_64_CODE_4_GOTPC32_TLSDESC, 45)

llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
191191
case MCSymbolRefExpr::VK_TLSCALL:
192192
return ELF::R_X86_64_TLSDESC_CALL;
193193
case MCSymbolRefExpr::VK_TLSDESC:
194-
return ELF::R_X86_64_GOTPC32_TLSDESC;
194+
return ((unsigned)Kind == X86::reloc_riprel_4byte_relax_rex2)
195+
? ELF::R_X86_64_CODE_4_GOTPC32_TLSDESC
196+
: ELF::R_X86_64_GOTPC32_TLSDESC;
195197
case MCSymbolRefExpr::VK_TLSGD:
196198
checkIs32(Ctx, Loc, Type);
197199
return ELF::R_X86_64_TLSGD;

llvm/lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ void X86MCCodeEmitter::emitMemModRMByte(
666666
case X86::SBB64rm:
667667
case X86::SUB64rm:
668668
case X86::XOR64rm:
669+
case X86::LEA64r:
669670
return Kind == REX2 ? X86::reloc_riprel_4byte_relax_rex2
670671
: Kind == REX ? X86::reloc_riprel_4byte_relax_rex
671672
: X86::reloc_riprel_4byte_relax;

llvm/test/MC/X86/tlsdesc-64.s

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,15 @@
1717
leaq a@tlsdesc(%rip), %rax
1818
call *a@tlscall(%rax)
1919
addq %fs:0, %rax
20+
21+
# PRINT: leaq a@tlsdesc(%rip), %r16
22+
# PRINT-NEXT: callq *a@tlscall(%r16)
23+
24+
# CHECK: 12: leaq (%rip), %r16 # 0x1a <{{.*}}>
25+
# CHECK-NEXT: 0000000000000016: R_X86_64_CODE_4_GOTPC32_TLSDESC a-0x4
26+
# CHECK-NEXT: 1a: callq *(%r16)
27+
# CHECK-NEXT: 000000000000001a: R_X86_64_TLSDESC_CALL a
28+
29+
leaq a@tlsdesc(%rip), %r16
30+
call *a@tlscall(%r16)
31+
addq %fs:0, %r16

0 commit comments

Comments
 (0)