[PATCH v2 6/6] x86-64: Set up a standard frame record in _dl_tlsdesc_dynamic
Aliaksei Kandratsenka
alkondratenko@gmail.com
Tue Nov 3 00:02:06 GMT 2026
The slow path of _dl_tlsdesc_dynamic uses RBP as the CFA register, but
it saves the caller's RBP in the red zone at -24(%rsp), below the saved
RDI and RSI, not directly below the return address. Frame-pointer
unwinders cannot walk through it.
Have the fast path save RSI and RDI one slot lower, which frees
-8(%rsp). The slow path stores the caller's RBP there and points RBP
at it, and it now returns through LEAVE.
Also make the slow path reserve the three red-zone slots explicitly,
by subtracting 24 from RSP before realigning it. Previously the
reservation was folded into the size of the save area (BZ #31501), in
different ways for XSAVE (TLSDESC_CALL_REGISTER_SAVE_AREA, added to
the state size in cpu-features.c) and FXSAVE (an extra
STATE_SAVE_ALIGNMENT), which made the stack layout hard to follow.
Remove TLSDESC_CALL_REGISTER_SAVE_AREA and describe the layout in a
comment next to the code instead of in sysdeps/x86/sysdep.h.
This also drops the unneeded 24 bytes from the area _dl_runtime_resolve
allocates.
Signed-off-by: Aliaksei Kandratsenka <alkondratenko@gmail.com>
---
sysdeps/x86/cpu-features.c | 5 +--
sysdeps/x86/sysdep.h | 61 ++++-------------------------
sysdeps/x86_64/dl-tlsdesc-dynamic.h | 54 +++++++++++++++++--------
3 files changed, 47 insertions(+), 73 deletions(-)
diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c
index b318f30947..32cd6b89d3 100644
--- a/sysdeps/x86/cpu-features.c
+++ b/sysdeps/x86/cpu-features.c
@@ -319,7 +319,7 @@ update_active (struct cpu_features *cpu_features)
/* NB: On AMX capable processors, ebx always includes AMX
states. */
unsigned int xsave_state_full_size
- = ALIGN_UP (ebx + TLSDESC_CALL_REGISTER_SAVE_AREA, 64);
+ = ALIGN_UP (ebx + STATE_SAVE_OFFSET, 64);
cpu_features->xsave_state_size
= xsave_state_full_size;
@@ -370,8 +370,7 @@ update_active (struct cpu_features *cpu_features)
+ xstate_comp_sizes[X86_XSTATE_MAX_ID]);
if (size)
{
- size = ALIGN_UP (size + TLSDESC_CALL_REGISTER_SAVE_AREA,
- 64);
+ size = ALIGN_UP (size + STATE_SAVE_OFFSET, 64);
#ifdef __x86_64__
_dl_x86_features_tlsdesc_state_size = size;
/* Exclude the AMX space from the start of TILECFG
diff --git a/sysdeps/x86/sysdep.h b/sysdeps/x86/sysdep.h
index 37276c6220..11b8c83e80 100644
--- a/sysdeps/x86/sysdep.h
+++ b/sysdeps/x86/sysdep.h
@@ -36,61 +36,15 @@
#define X86_XSTATE_APX_F_ID 19
#ifdef __x86_64__
-/* Offset for fxsave/xsave area used by _dl_runtime_resolve. Also need
- space to preserve RCX, RDX, RSI, RDI, R8, R9 and RAX. It must be
- aligned to 16 bytes for fxsave and 64 bytes for xsave. It is non-zero
- because MOV, instead of PUSH, is used to save registers onto stack.
-
- +==================+<- stack frame start aligned at 8 or 16 bytes
- | |<- paddings for stack realignment of 64 bytes
- |------------------|<- xsave buffer end aligned at 64 bytes
- | |<-
- | |<-
- | |<-
- |------------------|<- xsave buffer start at STATE_SAVE_OFFSET(%rsp)
- | |<- 8-byte padding for 64-byte alignment
- | |<- R9
- | |<- R8
- | |<- RDI
- | |<- RSI
- | |<- RDX
- | |<- RCX
- | |<- RAX
- +==================+<- RSP aligned at 64 bytes
-
- */
+/* Size of the integer register save area below the fxsave/xsave
+ buffer in _dl_runtime_resolve and _dl_tlsdesc_dynamic. It has room
+ for eight 8-byte slots and keeps the buffer at STATE_SAVE_OFFSET(%rsp)
+ aligned to 64 bytes when %rsp is. It is non-zero because registers
+ are saved with MOV instead of PUSH. The slot layout is defined by
+ each user (REGISTER_SAVE_* in dl-trampoline.S and dl-tlsdesc.S); the
+ frame layouts are described next to the code. */
# define STATE_SAVE_OFFSET (8 * 7 + 8)
-/* _dl_tlsdesc_dynamic preserves RDI, RSI and RBX before realigning
- stack. After realigning stack, it saves RCX, RDX, R8, R9, R10 and
- R11. Allocate space for RDI, RSI and RBX to avoid clobbering saved
- RDI, RSI and RBX values on stack by xsave.
-
- +==================+<- stack frame start aligned at 8 or 16 bytes
- | |<- RDI saved in the red zone
- | |<- RSI saved in the red zone
- | |<- RBX saved in the red zone
- | |<- paddings for stack realignment of 64 bytes
- |------------------|<- xsave buffer end aligned at 64 bytes
- | |<-
- | |<-
- | |<-
- |------------------|<- xsave buffer start at STATE_SAVE_OFFSET(%rsp)
- | |<- 8-byte padding for 64-byte alignment
- | |<- 8-byte padding for 64-byte alignment
- | |<- R11
- | |<- R10
- | |<- R9
- | |<- R8
- | |<- RDX
- | |<- RCX
- +==================+<- RSP aligned at 64 bytes
-
- Define the total register save area size for all integer registers by
- adding 24 to STATE_SAVE_OFFSET since RDI, RSI and RBX are saved onto
- stack without adjusting stack pointer first, using the red-zone. */
-# define TLSDESC_CALL_REGISTER_SAVE_AREA (STATE_SAVE_OFFSET + 24)
-
/* Save SSE, AVX, AVX512, mask, bound and APX registers. Bound and APX
registers are mutually exclusive. */
# define STATE_SAVE_MASK \
@@ -116,7 +70,6 @@
/* Offset for fxsave/xsave area used by _dl_tlsdesc_dynamic. Since i386
uses PUSH to save registers onto stack, use 0 here. */
# define STATE_SAVE_OFFSET 0
-# define TLSDESC_CALL_REGISTER_SAVE_AREA 0
/* Save SSE, AVX, AXV512, mask and bound registers. */
# define STATE_SAVE_MASK \
diff --git a/sysdeps/x86_64/dl-tlsdesc-dynamic.h b/sysdeps/x86_64/dl-tlsdesc-dynamic.h
index 2b9b9983c9..6ed046902a 100644
--- a/sysdeps/x86_64/dl-tlsdesc-dynamic.h
+++ b/sysdeps/x86_64/dl-tlsdesc-dynamic.h
@@ -59,10 +59,12 @@ _dl_tlsdesc_dynamic (register struct tlsdesc *tdp asm ("%rax"))
_dl_tlsdesc_dynamic:
_CET_ENDBR
/* Preserve call-clobbered registers that we modify.
- We need two scratch regs anyway. */
- movq %rsi, -16(%rsp)
+ We need two scratch regs anyway. -8(%rsp) is left free for
+ the slow path below, which saves %rbp there to form a standard
+ frame record. */
+ movq %rsi, -24(%rsp)
mov %fs:DTV_OFFSET, %RSI_LP
- movq %rdi, -8(%rsp)
+ movq %rdi, -16(%rsp)
movq TLSDESC_ARG(%rax), %rdi
movq (%rsi), %rax
cmpq %rax, TLSDESC_GEN_COUNT(%rdi)
@@ -74,20 +76,40 @@ _dl_tlsdesc_dynamic:
je 2f
addq TLSDESC_MODOFF(%rdi), %rax
1:
- movq -16(%rsp), %rsi
+ movq -24(%rsp), %rsi
sub %fs:0, %RAX_LP
- movq -8(%rsp), %rdi
+ movq -16(%rsp), %rdi
ret
2:
- movq %rbp, -24(%rsp)
- mov %RSP_LP, %RBP_LP
- cfi_def_cfa_register(%rbp)
+ /* Slow path. R is %rsp on entry. It is not assumed to be
+ 16-byte aligned plus 8: code built with reduced stack
+ alignment (e.g. clang -mstack-alignment=8), or by GCC
+ versions affected by PR 58066, can make the TLS descriptor
+ call with a misaligned stack.
+
+ R+0 return address
+ R-8 caller's %rbp <- %rbp (frame record)
+ R-16 %rdi (red zone, saved by the fast path)
+ R-24 %rsi (red zone, saved by the fast path)
+ padding (realign to STATE_SAVE_ALIGNMENT)
+ fxsave/xsave area <- STATE_SAVE_OFFSET(%rsp)
+ two 8-byte padding slots
+ r11 r10 r9 r8 rdx rcx <- %rsp
+ */
+ movq %rbp, -8(%rsp)
+ lea -8(%RSP_LP), %RBP_LP
+ cfi_def_cfa (%rbp, 16)
+ cfi_offset (%rbp, -16)
+
+ /* Reserve the three slots above so that the realigned save area
+ cannot overlap them. */
+ sub $24, %RSP_LP
and $-STATE_SAVE_ALIGNMENT, %RSP_LP
+
#ifdef REGISTER_SAVE_AREA
- /* STATE_SAVE_OFFSET has space for 8 integer registers. But we
- need space for RCX, RDX, RSI, RDI, R8, R9, R10 and R11, plus
- RBP above. */
- sub $(REGISTER_SAVE_AREA + STATE_SAVE_ALIGNMENT), %RSP_LP
+ /* STATE_SAVE_OFFSET bytes for the integer registers plus 512
+ bytes for fxsave. */
+ sub $REGISTER_SAVE_AREA, %RSP_LP
#else
/* Allocate stack space of the required size to save the state. */
sub _dl_x86_features_tlsdesc_state_size(%rip), %RSP_LP
@@ -141,10 +163,10 @@ _dl_tlsdesc_dynamic:
movq REGISTER_SAVE_R8(%rsp), %r8
movq REGISTER_SAVE_RDX(%rsp), %rdx
movq REGISTER_SAVE_RCX(%rsp), %rcx
- mov %RBP_LP, %RSP_LP
- cfi_def_cfa_register(%rsp)
- movq -24(%rsp), %rbp
- cfi_restore(%rbp)
+
+ leave
+ cfi_def_cfa (%rsp, 8)
+ cfi_restore (%rbp)
jmp 1b
cfi_endproc
.size _dl_tlsdesc_dynamic, .-_dl_tlsdesc_dynamic
--
2.55.0
More information about the Libc-alpha
mailing list