[PATCH v2 4/6] x86-64: Set up a standard frame record in _dl_runtime_profile

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


Like _dl_runtime_resolve, _dl_runtime_profile is entered with the link
map and reloc index pushed by the PLT stubs below the return address,
and it saves the caller's RBP below its local storage.  Frame-pointer
unwinders cannot walk through it.

Do the same as in _dl_runtime_resolve: overwrite the reloc index slot
with the caller's RBP so that RBP points to a standard frame record,
and keep the reloc index in the local storage area instead.  The
RBP-relative offsets change accordingly, and both exit paths return
through LEAVE.

The copy of the caller's stack arguments (when _dl_profile_fixup asks
for a frame) used LR_RSP_OFFSET(%rbp) as the source address.  That only
worked because LR_RSP_OFFSET happens to equal 56, the offset of the
first stack argument in the old layout.  Use 16(%rbp) instead.

Signed-off-by: Aliaksei Kandratsenka <alkondratenko@gmail.com>
---
 sysdeps/x86_64/dl-trampoline.h | 100 +++++++++++++++------------------
 1 file changed, 46 insertions(+), 54 deletions(-)

diff --git a/sysdeps/x86_64/dl-trampoline.h b/sysdeps/x86_64/dl-trampoline.h
index 2630655fcc..656c03c2b9 100644
--- a/sysdeps/x86_64/dl-trampoline.h
+++ b/sysdeps/x86_64/dl-trampoline.h
@@ -145,31 +145,32 @@ _dl_runtime_profile:
 	_CET_ENDBR
 	/* 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
+	   be explicitly enforced.  We have to set up a dynamically
 	   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 %rbp, (%rsp)
-	cfi_rel_offset(%rbp, 0)
-
-	/* On the stack:
-		56(%rbp)	parameter #1
-		48(%rbp)	return address
-
-		40(%rbp)	reloc index
-		32(%rbp)	link_map
-
-		24(%rbp)	La_x86_64_regs pointer
-		16(%rbp)	framesize
-		 8(%rbp)	rax
-		  (%rbp)	rbp
+	   has a fixed size and preserves the original stack pointer.
+
+	   As in _dl_runtime_resolve, the reloc index slot pushed by
+	   the PLT is overwritten with the caller's %rbp, so that %rbp
+	   points to a standard frame record.  On the stack:
+		16(%rbp)	parameter #1
+		 8(%rbp)	return address
+		  (%rbp)	caller's %rbp
+		-8(%rbp)	link_map
+		-16(%rbp)	reloc index (copied from 8(%rbp) on entry)
+		-24(%rbp)	framesize
+		-32(%rbp)	La_x86_64_regs pointer
+		-40(%rbp)	rax
 	*/
 
-	movq %rax, 8(%rsp)
-	mov %RSP_LP, %RBP_LP
-	cfi_def_cfa_register(%rbp)
+	mov 8(%rsp), %R11_LP	# Reloc index; its slot gets %rbp.
+	mov %rbp, 8(%rsp)
+	lea 8(%RSP_LP), %RBP_LP
+	cfi_def_cfa (%rbp, 16)
+	cfi_offset (%rbp, -16)
+
+	sub $32, %RSP_LP	# Allocate the local storage.
+	movq %r11, -16(%rbp)
+	movq %rax, -40(%rbp)
 
 	/* Actively align the La_x86_64_regs structure.  */
 	and $-VEC_SIZE, %RSP_LP
@@ -177,7 +178,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(%rbp)
+	movq %rsp, -32(%rbp)
 
 	/* Fill the La_x86_64_regs structure.  */
 	movq %rdx, LR_RDX_OFFSET(%rsp)
@@ -189,7 +190,7 @@ _dl_runtime_profile:
 	movq (%rbp), %rax	# Caller's %rbp, saved above.
 	movq %rax, LR_RBP_OFFSET(%rsp)
 
-	lea 48(%rbp), %RAX_LP
+	lea 8(%rbp), %RAX_LP
 	movq %rax, LR_RSP_OFFSET(%rsp)
 
 	/* We always store the XMM registers even if AVX is available.
@@ -228,15 +229,15 @@ _dl_runtime_profile:
 # endif
 
 	mov %RSP_LP, %RCX_LP	# La_x86_64_regs pointer to %rcx.
-	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
+	mov 8(%rbp), %RDX_LP	# Load return address if needed.
+	mov -16(%rbp), %RSI_LP	# Copy args pushed by PLT in register.
+	mov -8(%rbp), %RDI_LP	# %rdi: link_map, %rsi: reloc_index
+	lea -24(%rbp), %R8_LP	# Address of framesize
 	call _dl_profile_fixup	# Call resolver.
 
 	mov %RAX_LP, %R11_LP	# Save return value.
 
-	movq 8(%rbp), %rax	# Get back register content.
+	movq -40(%rbp), %rax	# Get back register content.
 	movq LR_RDX_OFFSET(%rsp), %rdx
 	movq  LR_R8_OFFSET(%rsp), %r8
 	movq  LR_R9_OFFSET(%rsp), %r9
@@ -328,7 +329,7 @@ _dl_runtime_profile:
 1:
 # endif
 
-	mov  16(%rbp), %RCX_LP	# Anything in framesize?
+	mov  -24(%rbp), %RCX_LP	# Anything in framesize?
 	test %RCX_LP, %RCX_LP
 	jns 3f
 
@@ -340,27 +341,22 @@ _dl_runtime_profile:
 	movq LR_RSI_OFFSET(%rsp), %rsi
 	movq LR_RDI_OFFSET(%rsp), %rdi
 
-	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
-				# (eats the reloc index and link_map)
-	cfi_adjust_cfa_offset(-48)
+	cfi_remember_state
+	leave			# Also pops link_map, leaving %rsp at the
+				# return address.
+	cfi_def_cfa (%rsp, 8)
+	cfi_restore (%rbp)
 	jmp *%r11		# Jump to function address.
 
 3:
-	cfi_adjust_cfa_offset(48)
-	cfi_rel_offset(%rbp, 0)
-	cfi_def_cfa_register(%rbp)
+	cfi_restore_state
 
 	/* 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(%rbp), %RSI_LP # stack
+	lea 16(%rbp), %RSI_LP	# Caller's stack arguments.
 	add $8, %RCX_LP
 	and $-16, %RCX_LP
 	sub %RCX_LP, %RSP_LP
@@ -373,7 +369,7 @@ _dl_runtime_profile:
 
 	call *%r11
 
-	mov 24(%rbp), %RSP_LP	# Drop the copied stack content
+	mov -32(%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,
@@ -418,9 +414,9 @@ _dl_runtime_profile:
 	fstpt LRV_ST0_OFFSET(%rcx)
 	fstpt LRV_ST1_OFFSET(%rcx)
 
-	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
+	movq -32(%rbp), %rdx	# La_x86_64_regs argument to %rdx.
+	movq -16(%rbp), %rsi	# Copy args pushed by PLT in register.
+	movq -8(%rbp), %rdi	# %rdi: link_map, %rsi: reloc_index
 	call _dl_audit_pltexit
 
 	/* Restore return registers.  */
@@ -450,14 +446,10 @@ _dl_runtime_profile:
 	fldt LRV_ST1_OFFSET(%rsp)
 	fldt LRV_ST0_OFFSET(%rsp)
 
-	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
-				# (eats the reloc index and link_map)
-	cfi_adjust_cfa_offset(-48)
+	leave			# Also pops link_map, leaving %rsp at the
+				# return address.
+	cfi_def_cfa (%rsp, 8)
+	cfi_restore (%rbp)
 	retq
 
 	cfi_endproc
-- 
2.55.0



More information about the Libc-alpha mailing list