This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Support non-RAX/non-adjacent R_X86_64_GOTPC32_TLSDESC/R_X86_64_TLSDESC_CALL
ClosedPublic

Authored by MaskRay on Nov 22 2021, 9:49 PM.

Details

Summary

The current TLSDESC optimization code assumes:

leaq x@tlsdesc(%rip), %rax
call *x@tlscall(%rax)       # adjacent

From https://gitlab.freedesktop.org/mesa/mesa/-/issues/5665 , it seems that the
two instructions may not be adjacent in GCC 10's output:

leaq x@tlsdesc(%rip), %rax
something else
call *x@tlscall(%rax)

This patch supports the case. While here, support non-RAX registers for
R_X86_64_GOTPC32_TLSDESC, in case the compiler generates inefficient:

leaq x@tlsdesc(%rip), %rcx  # or %rdx, %rbx, %rdi, ...
movq %rcx, %rax
call *x@tlscall(%rax)       # GNU ld/gold error for non-RAX

Diff Detail

Event Timeline

MaskRay created this revision.Nov 22 2021, 9:49 PM
skan added a subscriber: skan.Nov 22 2021, 11:20 PM
skan added inline comments.
lld/ELF/Arch/X86_64.cpp
452

REX.X and REX.B does not affect RIP-relative addressing. I believe we should use mask
0xf8 for loc[-3].

skan added inline comments.Nov 23 2021, 12:01 AM
lld/ELF/Arch/X86_64.cpp
459

Here we'd like to keep the fixed prefix 0100 and set REX.B to the value of REX.R. We already checked REX.W =1 before. I think code can be simplified to

loc[-3] = 0x48 | ((loc[-3] >>2) & 1);

461

Here we set mod=11, reg=000, and set the rm to the value of reg. LGTM.

MaskRay updated this revision to Diff 389105.Nov 23 2021, 12:23 AM

address comments

MaskRay added inline comments.Nov 23 2021, 12:50 AM
lld/ELF/Arch/X86_64.cpp
452

I think we should still use 0xfb. I cannot get an assembler to use the REX.X and REX.B bits.

MaskRay updated this revision to Diff 389250.Nov 23 2021, 10:20 AM
MaskRay retitled this revision from [ELF] Support non-RAX R_X86_64_GOTPC32_TLSDESC to [ELF] Support non-RAX/non-adjacent R_X86_64_GOTPC32_TLSDESC/R_X86_64_TLSDESC_CALL.
MaskRay edited the summary of this revision. (Show Details)

Change description

MaskRay updated this revision to Diff 389256.Nov 23 2021, 10:28 AM
MaskRay edited the summary of this revision. (Show Details)

update description

This revision was not accepted when it landed; it landed in state Draft.Nov 23 2021, 10:30 AM
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptNov 23 2021, 10:30 AM