[PATCH v2 2/6] x86-64: Use RBP instead of RBX as CFA register in PLT trampolines

Aliaksei Kandratsenka alkondratenko@gmail.com
Tue Nov 3 00:02:02 GMT 2026


_dl_runtime_resolve and _dl_runtime_profile save the machine state on
the stack around the call to _dl_fixup and _dl_profile_fixup.  Because
the stack is realigned, they use RBX as the CFA register for the
duration of that call.  This is valid DWARF and works for GDB and
libgcc, but not for unwinders that track only SP, FP and RA (SFrame,
the Linux kernel user-space unwinder, aw-backtrace).

Such unwinders can use a CFA based on another register only in the
topmost frame, because callees may have clobbered that register.  As a
result, a backtrace taken while _dl_fixup (or anything it calls) is
running stops at _dl_runtime_resolve.  The SFrame v3 specification's
example unwinder (Appendix A of "The SFrame Format") makes the same
restriction: it returns ERR_SFRAME_UNSAFE_UNWIND for a non-SP/FP CFA
base in any frame other than the topmost one.

Use RBP, the conventional frame pointer register, instead of RBX.  The
change is mechanical except in _dl_runtime_profile, where
La_x86_64_regs.lr_rbp is now loaded from the stack slot holding the
caller's RBP, since RBP itself has already been overwritten at that
point.

Signed-off-by: Aliaksei Kandratsenka <alkondratenko@gmail.com>
---
 sysdeps/x86_64/dl-trampoline-state.h |  4 +-
 sysdeps/x86_64/dl-trampoline.h       | 85 ++++++++++++++--------------
 2 files changed, 45 insertions(+), 44 deletions(-)

diff --git a/sysdeps/x86_64/dl-trampoline-state.h b/sysdeps/x86_64/dl-trampoline-state.h
index d3a3d5d7c9..65eb080cf7 100644
--- a/sysdeps/x86_64/dl-trampoline-state.h
+++ b/sysdeps/x86_64/dl-trampoline-state.h
@@ -24,9 +24,9 @@
 # error STATE_SAVE_OFFSET must be multiple of STATE_SAVE_ALIGNMENT
 #endif
 
-/* Local stack area before jumping to function address: RBX.  */
+/* Local stack area before jumping to function address: RBP.  */
 #define LOCAL_STORAGE_AREA	8
-#define BASE			rbx
+#define BASE			rbp
 #ifdef USE_FXSAVE
 /* Use fxsave to save XMM registers.  */
 # define REGISTER_SAVE_AREA	(512 + STATE_SAVE_OFFSET)
diff --git a/sysdeps/x86_64/dl-trampoline.h b/sysdeps/x86_64/dl-trampoline.h
index 6316b6e719..c9cb12dee6 100644
--- a/sysdeps/x86_64/dl-trampoline.h
+++ b/sysdeps/x86_64/dl-trampoline.h
@@ -40,11 +40,11 @@ _dl_runtime_resolve:
 # if LOCAL_STORAGE_AREA != 8
 #  error LOCAL_STORAGE_AREA must be 8
 # endif
-	pushq %rbx			# push subtracts stack by 8.
+	pushq %rbp			# push subtracts stack by 8.
 	cfi_adjust_cfa_offset(8)
-	cfi_rel_offset(%rbx, 0)
-	mov %RSP_LP, %RBX_LP
-	cfi_def_cfa_register(%rbx)
+	cfi_rel_offset(%rbp, 0)
+	mov %RSP_LP, %RBP_LP
+	cfi_def_cfa_register(%rbp)
 	and $-STATE_SAVE_ALIGNMENT, %RSP_LP
 # ifdef REGISTER_SAVE_AREA
 	sub $REGISTER_SAVE_AREA, %RSP_LP
@@ -107,10 +107,10 @@ _dl_runtime_resolve:
 	movq REGISTER_SAVE_RDX(%rsp), %rdx
 	movq REGISTER_SAVE_RCX(%rsp), %rcx
 	movq REGISTER_SAVE_RAX(%rsp), %rax
-	mov %RBX_LP, %RSP_LP
+	mov %RBP_LP, %RSP_LP
 	cfi_def_cfa_register(%rsp)
-	movq (%rsp), %rbx
-	cfi_restore(%rbx)
+	movq (%rsp), %rbp
+	cfi_restore(%rbp)
 	# Adjust stack(PLT did 2 pushes)
 	add $(LOCAL_STORAGE_AREA + 16), %RSP_LP
 	cfi_adjust_cfa_offset(-(LOCAL_STORAGE_AREA + 16))
@@ -136,30 +136,30 @@ _dl_runtime_profile:
 	/* The La_x86_64_regs data structure pointed to by the
 	   fourth parameter must be VEC_SIZE-byte aligned.  This must
 	   be explicitly enforced.  We have the set up a dynamically
-	   sized stack frame.  %rbx points to the top half which
+	   sized stack frame.  %rbp points to the top half which
 	   has a fixed size and preserves the original stack pointer.  */
 
 	sub $32, %RSP_LP	# Allocate the local storage.
 	cfi_adjust_cfa_offset(32)
-	movq %rbx, (%rsp)
-	cfi_rel_offset(%rbx, 0)
+	movq %rbp, (%rsp)
+	cfi_rel_offset(%rbp, 0)
 
 	/* On the stack:
-		56(%rbx)	parameter #1
-		48(%rbx)	return address
+		56(%rbp)	parameter #1
+		48(%rbp)	return address
 
-		40(%rbx)	reloc index
-		32(%rbx)	link_map
+		40(%rbp)	reloc index
+		32(%rbp)	link_map
 
-		24(%rbx)	La_x86_64_regs pointer
-		16(%rbx)	framesize
-		 8(%rbx)	rax
-		  (%rbx)	rbx
+		24(%rbp)	La_x86_64_regs pointer
+		16(%rbp)	framesize
+		 8(%rbp)	rax
+		  (%rbp)	rbp
 	*/
 
 	movq %rax, 8(%rsp)
-	mov %RSP_LP, %RBX_LP
-	cfi_def_cfa_register(%rbx)
+	mov %RSP_LP, %RBP_LP
+	cfi_def_cfa_register(%rbp)
 
 	/* Actively align the La_x86_64_regs structure.  */
 	and $-VEC_SIZE, %RSP_LP
@@ -167,7 +167,7 @@ _dl_runtime_profile:
 	   to detect if any xmm0-xmm7 registers are changed by audit
 	   module.  */
 	sub $(LR_SIZE + XMM_SIZE*8), %RSP_LP
-	movq %rsp, 24(%rbx)
+	movq %rsp, 24(%rbp)
 
 	/* Fill the La_x86_64_regs structure.  */
 	movq %rdx, LR_RDX_OFFSET(%rsp)
@@ -176,9 +176,10 @@ _dl_runtime_profile:
 	movq %rcx, LR_RCX_OFFSET(%rsp)
 	movq %rsi, LR_RSI_OFFSET(%rsp)
 	movq %rdi, LR_RDI_OFFSET(%rsp)
-	movq %rbp, LR_RBP_OFFSET(%rsp)
+	movq (%rbp), %rax	# Caller's %rbp, saved above.
+	movq %rax, LR_RBP_OFFSET(%rsp)
 
-	lea 48(%rbx), %RAX_LP
+	lea 48(%rbp), %RAX_LP
 	movq %rax, LR_RSP_OFFSET(%rsp)
 
 	/* We always store the XMM registers even if AVX is available.
@@ -217,15 +218,15 @@ _dl_runtime_profile:
 # endif
 
 	mov %RSP_LP, %RCX_LP	# La_x86_64_regs pointer to %rcx.
-	mov 48(%rbx), %RDX_LP	# Load return address if needed.
-	mov 40(%rbx), %RSI_LP	# Copy args pushed by PLT in register.
-	mov 32(%rbx), %RDI_LP	# %rdi: link_map, %rsi: reloc_index
-	lea 16(%rbx), %R8_LP	# Address of framesize
+	mov 48(%rbp), %RDX_LP	# Load return address if needed.
+	mov 40(%rbp), %RSI_LP	# Copy args pushed by PLT in register.
+	mov 32(%rbp), %RDI_LP	# %rdi: link_map, %rsi: reloc_index
+	lea 16(%rbp), %R8_LP	# Address of framesize
 	call _dl_profile_fixup	# Call resolver.
 
 	mov %RAX_LP, %R11_LP	# Save return value.
 
-	movq 8(%rbx), %rax	# Get back register content.
+	movq 8(%rbp), %rax	# Get back register content.
 	movq LR_RDX_OFFSET(%rsp), %rdx
 	movq  LR_R8_OFFSET(%rsp), %r8
 	movq  LR_R9_OFFSET(%rsp), %r9
@@ -317,7 +318,7 @@ _dl_runtime_profile:
 1:
 # endif
 
-	mov  16(%rbx), %RCX_LP	# Anything in framesize?
+	mov  16(%rbp), %RCX_LP	# Anything in framesize?
 	test %RCX_LP, %RCX_LP
 	jns 3f
 
@@ -329,9 +330,9 @@ _dl_runtime_profile:
 	movq LR_RSI_OFFSET(%rsp), %rsi
 	movq LR_RDI_OFFSET(%rsp), %rdi
 
-	mov %RBX_LP, %RSP_LP
-	movq (%rsp), %rbx
-	cfi_restore(%rbx)
+	mov %RBP_LP, %RSP_LP
+	movq (%rsp), %rbp
+	cfi_restore(%rbp)
 	cfi_def_cfa_register(%rsp)
 
 	add $48, %RSP_LP	# Adjust the stack to the return value
@@ -341,15 +342,15 @@ _dl_runtime_profile:
 
 3:
 	cfi_adjust_cfa_offset(48)
-	cfi_rel_offset(%rbx, 0)
-	cfi_def_cfa_register(%rbx)
+	cfi_rel_offset(%rbp, 0)
+	cfi_def_cfa_register(%rbp)
 
 	/* At this point we need to prepare new stack for the function
 	   which has to be called.  We copy the original stack to a
 	   temporary buffer of the size specified by the 'framesize'
 	   returned from _dl_profile_fixup */
 
-	lea LR_RSP_OFFSET(%rbx), %RSI_LP # stack
+	lea LR_RSP_OFFSET(%rbp), %RSI_LP # stack
 	add $8, %RCX_LP
 	and $-16, %RCX_LP
 	sub %RCX_LP, %RSP_LP
@@ -362,7 +363,7 @@ _dl_runtime_profile:
 
 	call *%r11
 
-	mov 24(%rbx), %RSP_LP	# Drop the copied stack content
+	mov 24(%rbp), %RSP_LP	# Drop the copied stack content
 
 	/* Now we have to prepare the La_x86_64_retval structure for the
 	   _dl_audit_pltexit.  The La_x86_64_regs is being pointed by rsp now,
@@ -407,9 +408,9 @@ _dl_runtime_profile:
 	fstpt LRV_ST0_OFFSET(%rcx)
 	fstpt LRV_ST1_OFFSET(%rcx)
 
-	movq 24(%rbx), %rdx	# La_x86_64_regs argument to %rdx.
-	movq 40(%rbx), %rsi	# Copy args pushed by PLT in register.
-	movq 32(%rbx), %rdi	# %rdi: link_map, %rsi: reloc_index
+	movq 24(%rbp), %rdx	# La_x86_64_regs argument to %rdx.
+	movq 40(%rbp), %rsi	# Copy args pushed by PLT in register.
+	movq 32(%rbp), %rdi	# %rdi: link_map, %rsi: reloc_index
 	call _dl_audit_pltexit
 
 	/* Restore return registers.  */
@@ -439,9 +440,9 @@ _dl_runtime_profile:
 	fldt LRV_ST1_OFFSET(%rsp)
 	fldt LRV_ST0_OFFSET(%rsp)
 
-	mov %RBX_LP, %RSP_LP
-	movq (%rsp), %rbx
-	cfi_restore(%rbx)
+	mov %RBP_LP, %RSP_LP
+	movq (%rsp), %rbp
+	cfi_restore(%rbp)
 	cfi_def_cfa_register(%rsp)
 
 	add $48, %RSP_LP	# Adjust the stack to the return value
-- 
2.55.0



More information about the Libc-alpha mailing list