From patchwork Mon Oct 19 22:17:29 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aliaksei Kandratsenka X-Patchwork-Id: 146377 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0161048FE549 for ; Mon, 19 Oct 2026 22:18:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0161048FE549 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=L0rXTsgj X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x835.google.com (mail-qt1-x835.google.com [IPv6:2607:f8b0:4864:20::835]) by sourceware.org (Postfix) with ESMTPS id 498ED4AB0F1F for ; Mon, 19 Oct 2026 22:17:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 498ED4AB0F1F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 498ED4AB0F1F Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::835 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1792448263; cv=none; b=Vuvja7Ms/3NzrqQ4fS/OfSD3iGz6v54A6vEaXEULdYdusoGog7vfpf7D8vF++MOE3B/CHoR2PhRQAdb9TsurHAhr4xgfxh6IdlAbdu3suE3mjq6qvUa2PailiXoXLpwzGiTNDZ7qL9kSwaQSxduTU7l+2Pbo0iLSIi4UkZeoi+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1792448263; c=relaxed/simple; bh=KzhJTcnUEB4b5sOVnLkbIgekSJPZ13DReI3H8cmgFj0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=dt/CrimgNGf5gRaFaevxcuJ8jrlgSj6+huCbtS21vrNPnOP9Ex2MTMqkrHKgAzFYDUL6dZjTUcT7pZQsfd4a+d5CtXTHsADTMpmZgVJJszFtoH9fpE03jVC3FPC40JZRZYI1VTgi5+k7qY4ZawtwymidQFif20VDyeZ6IyUl9cY= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=L0rXTsgj DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 498ED4AB0F1F Received: by mail-qt1-x835.google.com with SMTP id d75a77b69052e-53381e53b80so32183781cf.1 for ; Mon, 19 Oct 2026 15:17:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1792448263; x=1793053063; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=OVUTutKKpCMejKxB3HcGlchJxWaD9tAlIykIt125vH0=; b=L0rXTsgjq3+dVVUi6SvloZUEEEh9S74zE5eC756UEotXpIg7OIPzBubLNR23oo/wEU dTg3q6wl6hNj9MkoXArUIco7vN5kJFh4Muk9rzINdpvxG3vzphRh8rfiRyTD5Q+dLYJg CgTNxkU4UvmllcLbP+yY3AU/xeNL5fxjT7uU47uDw2GfWw99khAxRpK6KNBxTr2bSwKL vRL+1gOtDqSgvN0ew8rZrgXcBVuzuaWyXTvtiberImf0mXQ+W+ANE7lEAzZvjpUHxFPU 7ZjHslqoLK/gZn2KRbR0DGPsrnvryBLXTni5CP/3HwpO69KF6wnnP/MtLFQcgJnATbeO yzRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792448263; x=1793053063; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=OVUTutKKpCMejKxB3HcGlchJxWaD9tAlIykIt125vH0=; b=syAsdZ3SsJu5iCDGL8gbSjYM0B5y19BkZy0Ho0KRVLLbLx1GwzNqFO91z9BUwaDewW 7RlrUL1X4n8LKFdV49a1Jm/paJqEsUJojwR5392I+EfftiDXOfgcS+qwoW8HiJaDbSOh GzKAMO53+UIDJfp5C3RE1ZPF9N9QPrlUlEGuch3giAtbuNK1W6qTekIn9JpMbxWK2kMH u3chCJ2spC6vqIRxCt1rfz6zQSkaJmyfoEnp2zwbJemi6pdEtfymhMOgo1P+LZ1OpxPK 8VgPLnr/EieHtQyT/vYc6P8y/30g7gXf8kDYt7OmlDG81mweXDkXdzS166fiRXhcpWbn McpQ== X-Gm-Message-State: AFq9FYI5eA9YF55K/43OCoK5V1t04wqMZw7/EEEnMVKAl1Vqucf0o8f7 NESe6gQUQlpo4NTlBzv9qfk+698v5YzjpoDPEQV3fsd0yRM0uF7sUrva9DkZ8g== X-Gm-Gg: AYBFou3nescY04S0GxaNj2ZkQzbpdqzcX1l7oqTZx8rw233kQN3dUfcKB5dj+Zi64my zYSZql4WLFUt0XTIq+ttWth0equ4y13oglQxVlNp5hnPvI9POwexd8ek08S6mxhaEphpK8YFlIl QxsS19JaGQ3Yx8usUdqrbBenat0TejFuEYeKugiwQrkWAbT0b+Uy4103S3OSItjQImziHQ7Hz3j sd9hNO5msHJWH9sUQteTUR446rbJtk6XsmX//bM2eXNyCwto8t8r/BNX64Tt10t6J8D/UwZJr6V xfIwkzDN2hsF3OUiPcbhhWsVZiZJDXUTcbmQmVxugPIEJV0IrvtCtrSVOKszSufMIa4LCjgJ+5z cp2fOfeOjUbh0emZwsrSRZfpQ9LQ3BY93UTRNKeAuIBrYJLC73xKU70wQX5SIRenwkcQ4efkFn1 qI6xFlIW2B4QrmD1DVIdZZGMcUDxYWS14nAY0Uy7Vaia0QdBsET5/GJJO6mR5s3gwH5VpW6qgLL /iD35tlzz7mMUq/IcXSKkVjrUA+oBVTQXX7wRu6+zMmYUv5Pttd X-Received: by 2002:a05:622a:489b:b0:532:91f2:dd45 with SMTP id d75a77b69052e-53645fb4762mr117985791cf.44.1792448262510; Mon, 19 Oct 2026 15:17:42 -0700 (PDT) Received: from big.tail15e8b8.ts.net ([2601:18c:9280:1311:c177:e036:9004:588c]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-5362a8e5831sm109874521cf.10.2026.10.19.15.17.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Oct 2026 15:17:41 -0700 (PDT) From: Aliaksei Kandratsenka To: libc-alpha@sourceware.org Cc: hjl.tools@gmail.com, Aliaksei Kandratsenka Subject: [PATCH] x86-64: Use RBP instead of RBX as CFA register in PLT trampolines Date: Mon, 19 Oct 2026 18:17:29 -0400 Message-ID: <20261019221729.3350862-1-alkondratenko@gmail.com> X-Mailer: git-send-email 2.55.0 MIME-Version: 1.0 X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_PBL, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org _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 --- I found this while testing my aw-backtrace project (https://github.com/gperftools/aw-backtrace). The test single-steps code, runs a signal handler at every instruction boundary, and checks that the captured backtrace matches the actual call/ret sequence. In general, glibc (together with GCC) keeps unwind info on x86-64 correct and simple; _dl_runtime_resolve is a notable exception. Tested on x86_64-linux-gnu: make check on x86_64 and x32, no regressions compared to the parent commit.. LD_AUDIT tested manually. Also verified that aw-backtrace's backtrace-comparer now passes on /usr/bin/true without suppressing _dl_fixup or _dl_runtime_resolve. 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 21cc9d5082..9a2703c159 100644 --- a/sysdeps/x86_64/dl-trampoline-state.h +++ b/sysdeps/x86_64/dl-trampoline-state.h @@ -25,9 +25,9 @@ #endif #if DL_RUNTIME_RESOLVE_REALIGN_STACK -/* 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 b07f29c55a..7717f539b7 100644 --- a/sysdeps/x86_64/dl-trampoline.h +++ b/sysdeps/x86_64/dl-trampoline.h @@ -41,11 +41,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 # endif # ifdef REGISTER_SAVE_AREA @@ -113,10 +113,10 @@ _dl_runtime_resolve: movq REGISTER_SAVE_RCX(%rsp), %rcx movq REGISTER_SAVE_RAX(%rsp), %rax # if DL_RUNTIME_RESOLVE_REALIGN_STACK - 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) # endif # Adjust stack(PLT did 2 pushes) add $(LOCAL_STORAGE_AREA + 16), %RSP_LP @@ -143,30 +143,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 @@ -174,7 +174,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) @@ -183,9 +183,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. @@ -224,15 +225,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 @@ -324,7 +325,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 @@ -336,9 +337,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 @@ -348,15 +349,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 @@ -369,7 +370,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, @@ -414,9 +415,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. */ @@ -446,9 +447,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