Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Stephan Rohr <stephan.rohr@intel.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/2] gdb/amd64: recognize pushp %rbp as a frame setup
Date: Fri, 30 Oct 2026 22:02:01 +0000	[thread overview]
Message-ID: <20261030220201.2288060-3-stephan.rohr@intel.com> (raw)
In-Reply-To: <20261030220201.2288060-1-stephan.rohr@intel.com>

From: Klaus Gerlicher <klaus.gerlicher@intel.com>

With PPX enabled, clang and icpx start a frame pointer prologue with
pushp %rbp (REX2 prefix with W set) where GCC emits push %rbp.
amd64_analyze_frame_setup only knew push %rbp, so it did not see the
frame setup.  "break FUNC" on a function without debug info then
stopped at the first instruction instead of after the prologue.

Extend amd64_analyze_frame_setup to support pushp %rbp.

As GCC emits push %rbp, test it with the assembly clang 20.1 generates
from amd64-apx-extended-prologue-analysis.c.

Tested on Diamond Rapids.
---
 gdb/amd64-tdep.c                              |  25 +++-
 ...d64-apx-extended-prologue-analysis-clang.S | 139 ++++++++++++++++++
 .../amd64-apx-extended-prologue-analysis.c    |   7 +-
 .../amd64-apx-extended-prologue-analysis.exp  |  14 +-
 4 files changed, 176 insertions(+), 9 deletions(-)
 create mode 100644 gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis-clang.S

diff --git a/gdb/amd64-tdep.c b/gdb/amd64-tdep.c
index c43ffca7b8b..0bf9ba49bb8 100644
--- a/gdb/amd64-tdep.c
+++ b/gdb/amd64-tdep.c
@@ -2687,11 +2687,30 @@ amd64_analyze_frame_setup (gdbarch *gdbarch, CORE_ADDR pc,
   bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   gdb_byte buf[3];
   gdb_byte op = read_code_unsigned_integer (pc, 1, byte_order);
+  const CORE_ADDR start_pc = pc;
+
+  /* With a REX2 prefix (Intel APX), 0x55 is also "push %rbp", or
+     "pushp %rbp" with the PPX hint.  */
+  if (rex2_prefix_p (op))
+    {
+      gdb_byte payload = read_code_unsigned_integer (pc + 1, 1, byte_order);
+
+      /* 0x55 is push in opcode map 0, so REX2.M0 must be zero, and
+	 REX2.B4 and REX2.B3 must be zero, as they extend the register
+	 number in the opcode.  REX2.W is the PPX hint of pushp, and the
+	 other bits do not affect push.  */
+      const gdb_byte rex2_mask = (REX2_M << 4) | (REX_B << 4) | REX_B;
+      if ((payload & rex2_mask) != 0)
+	return start_pc;
+
+      pc += 2;
+      op = read_code_unsigned_integer (pc, 1, byte_order);
+    }
 
   if (op == 0x55) /* pushq %rbp.  */
     {
-      /* Take into account that we've executed the `pushq %rbp' that
-	 starts this instruction sequence.  */
+      /* Take into account that we've executed the `pushq %rbp' or
+	 `pushp %rbp' that starts this instruction sequence.  */
       cache->saved_regs[AMD64_RBP_REGNUM] = 0;
       cache->sp_offset += 8;
 
@@ -2725,7 +2744,7 @@ amd64_analyze_frame_setup (gdbarch *gdbarch, CORE_ADDR pc,
       return pc + 1;
     }
 
-  return pc;
+  return start_pc;
 }
 
 /* Check whether PC points at code pushing registers onto the stack.  If so,
diff --git a/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis-clang.S b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis-clang.S
new file mode 100644
index 00000000000..8e87b108e80
--- /dev/null
+++ b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis-clang.S
@@ -0,0 +1,139 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2025-2026 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* This file is compiled from gdb.arch/amd64-apx-extended-prologue-analysis.c
+   using clang 20.1.2 with flags: -g0 -O1 -S
+   -mapx-features=egpr,push2pop2,ppx -mgeneral-regs-only
+   -fno-omit-frame-pointer -fno-asynchronous-unwind-tables -fno-addrsig.  */
+
+	.file	"amd64-apx-extended-prologue-analysis.c"
+	.text
+	.globl	bar                             # -- Begin function bar
+	.p2align	4
+	.type	bar,@function
+bar:                                    # @bar
+# %bb.0:
+	pushp	%rbp
+	movq	%rsp, %rbp
+                                        # kill: def $edi killed $edi def $rdi
+	leal	(%rdi,%rdi), %eax
+	popp	%rbp
+	retq
+.Lfunc_end0:
+	.size	bar, .Lfunc_end0-bar
+                                        # -- End function
+	.globl	foo                             # -- Begin function foo
+	.p2align	4
+	.type	foo,@function
+foo:                                    # @foo
+# %bb.0:
+	pushp	%rbp
+	movq	%rsp, %rbp
+	push2p	%r14, %r15
+	push2p	%r12, %r13
+	pushp	%rbx
+	subq	$264, %rsp                      # imm = 0x108
+	movl	%ecx, %r14d
+	movl	%edx, %r15d
+	movl	%esi, %r12d
+	movl	%edi, %ebx
+	movl	%edi, %edi
+	xorl	%eax, %eax
+	.p2align	4
+.LBB1_1:                                # =>This Inner Loop Header: Depth=1
+	leal	(%rdi,%rax), %ecx
+	movb	%cl, -304(%rbp,%rax)
+	incq	%rax
+	cmpq	$256, %rax                      # imm = 0x100
+	jne	.LBB1_1
+# %bb.2:
+                                        # kill: def $edi killed $edi killed $rdi
+	callq	bar
+	movl	%eax, %r13d
+	movl	%r12d, %edi
+	callq	bar
+	movl	%eax, %r12d
+	movl	%r15d, %edi
+	callq	bar
+	movl	%eax, %r15d
+	movl	%r14d, %edi
+	callq	bar
+	addl	%ebx, %r13d
+	addl	%r12d, %r15d
+	addl	%r13d, %r15d
+	addl	%r15d, %eax
+	addq	$264, %rsp                      # imm = 0x108
+	popp	%rbx
+	pop2p	%r13, %r12
+	pop2p	%r15, %r14
+	popp	%rbp
+	retq
+.Lfunc_end1:
+	.size	foo, .Lfunc_end1-foo
+                                        # -- End function
+	.globl	nocsr                           # -- Begin function nocsr
+	.p2align	4
+	.type	nocsr,@function
+nocsr:                                  # @nocsr
+# %bb.0:
+	pushp	%rbp
+	movq	%rsp, %rbp
+	push2p	%r10, %r11
+	push2p	%r8, %r9
+	push2p	%rdx, %rsi
+	pushp	%rcx
+	pushq	%rax
+	callq	bar
+	addq	$8, %rsp
+	popp	%rcx
+	pop2p	%rsi, %rdx
+	pop2p	%r9, %r8
+	pop2p	%r11, %r10
+	popp	%rbp
+	retq
+.Lfunc_end2:
+	.size	nocsr, .Lfunc_end2-nocsr
+                                        # -- End function
+	.globl	main                            # -- Begin function main
+	.p2align	4
+	.type	main,@function
+main:                                   # @main
+# %bb.0:
+	pushp	%rbp
+	movq	%rsp, %rbp
+	pushp	%rbx
+	pushq	%rax
+	movl	%edi, %ebx
+	leal	1(%rbx), %esi
+	leal	2(%rbx), %edx
+	leal	(%rbx,%rbx), %ecx
+	callq	foo
+	movl	%eax, %ecx
+	movl	%ebx, %edi
+	callq	nocsr
+	addl	%ecx, %eax
+	movl	%eax, -12(%rbp)
+	movl	-12(%rbp), %eax
+	addq	$8, %rsp
+	popp	%rbx
+	popp	%rbp
+	retq
+.Lfunc_end3:
+	.size	main, .Lfunc_end3-main
+                                        # -- End function
+	.ident	"Ubuntu clang version 20.1.2 (0ubuntu1~24.04.3)"
+	.section	".note.GNU-stack","",@progbits
diff --git a/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.c b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.c
index ee137e7845d..1f2642b5ec7 100644
--- a/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.c
+++ b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.c
@@ -39,7 +39,8 @@ bar (int x)
 
    push2p pushes its second register first.  Without a frame pointer, a
    single pushp comes first, to align the stack for push2p.  With only
-   EGPR enabled, the registers are saved with push.
+   EGPR enabled, the registers are saved with push, and clang starts the
+   frame setup with pushp %rbp.
 
    So to be able to unwind a register, GDB needs to skip the prologue
    past the register pushes and stack allocation (to access .cfi
@@ -78,7 +79,9 @@ foo (int a, int b, int c, int d)
 
    Without a frame pointer, a single pushp comes first, to align the stack
    for push2p.  With only EGPR enabled, the 24 registers are saved with
-   single pushes, more than there are legacy GPRs.  */
+   single pushes, more than there are legacy GPRs.  clang starts the frame
+   setup with pushp %rbp, does not save %r16 - %r31, and pushes %rax only
+   to align the stack.  */
 __attribute__ ((noinline, no_caller_saved_registers)) int
 nocsr (int x)
 {
diff --git a/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.exp b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.exp
index ccc8a134bf8..d9e45d2c29d 100644
--- a/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.exp
+++ b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.exp
@@ -36,6 +36,9 @@
 #  - single pushes of %r16 - %r31: with only EGPR enabled, the compiler saves
 #    the registers of nocsr with more than 16 single pushes.
 #
+#  - pushp frame setup: with PPX enabled, clang starts the frame setup with
+#    pushp %rbp instead of push %rbp.#
+#
 # As in amd64-extended-prologue-analysis.exp, each case is tested with a
 # binary compiled from the C source, and with a binary assembled from the
 # assembly generated from it.  The assembly keeps testing the prologues the
@@ -43,13 +46,13 @@
 
 require allow_apx_tests
 
-standard_testfile .c -no-cfi.S .S -offset.S -egpr.S
+standard_testfile .c -no-cfi.S .S -offset.S -egpr.S -clang.S
 
 # Return {HAS_FRAME END REGS} for the prologue of FUNC, read from its
 # disassembly: whether it has a frame pointer setup, the address after the
 # prologue and the registers it saves, in push order.  The prologue is an
-# optional endbr64, an optional push %rbp followed by mov %rsp,%rbp, the
-# register pushes and an optional sub from %rsp.
+# optional endbr64, an optional push or pushp of %rbp followed by
+# mov %rsp,%rbp, the register pushes and an optional sub from %rsp.
 proc prologue_info { func } {
     set insns {}
     gdb_test_multiple "disassemble $func" "" -lbl {
@@ -71,7 +74,7 @@ proc prologue_info { func } {
 
     set has_frame 0
     set regs {}
-    if { [lindex $insns $i 1] eq "push %rbp"
+    if { [regexp {^pushp? %rbp$} [lindex $insns $i 1]]
 	 && [lindex $insns [expr {$i + 1}] 1] eq "mov %rsp,%rbp" } {
 	set has_frame 1
 	lappend regs rbp
@@ -169,6 +172,9 @@ set cases [list \
 	{-mapxf -fomit-frame-pointer -fno-asynchronous-unwind-tables} 0 \
     "single pushes of %r16 - %r31" $srcfile5 \
 	{-mapx-features=egpr -fno-omit-frame-pointer \
+	     -fno-asynchronous-unwind-tables} 0 \
+    "pushp frame setup" $srcfile6 \
+	{-mapx-features=egpr,push2pop2,ppx -fno-omit-frame-pointer \
 	     -fno-asynchronous-unwind-tables} 0]
 
 set n 0
-- 
2.43.0

________________________________________
Intel Deutschland GmbH 

Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany 

Tel: +49 (89) 99143-0 

www.intel.de 

Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman

Chairperson of the Supervisory Board: Sonja Pierer

Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.


  parent reply	other threads:[~2026-10-30 22:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-30 22:01 [PATCH 0/2] gdb/amd64: prologue analysis for Intel APX Stephan Rohr
2026-10-30 22:02 ` [PATCH 1/2] gdb/amd64: handle pushp, push2 and push2p in the prologue analyzer Stephan Rohr
2026-10-30 22:02 ` Stephan Rohr [this message]
2026-11-13 15:24 ` [PING][PATCH 0/2] gdb/amd64: prologue analysis for Intel APX Rohr, Stephan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261030220201.2288060-3-stephan.rohr@intel.com \
    --to=stephan.rohr@intel.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox