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 1/2] gdb/amd64: handle pushp, push2 and push2p in the prologue analyzer
Date: Fri, 30 Oct 2026 22:02:00 +0000	[thread overview]
Message-ID: <20261030220201.2288060-2-stephan.rohr@intel.com> (raw)
In-Reply-To: <20261030220201.2288060-1-stephan.rohr@intel.com>

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

With Intel APX, compilers save registers in the prologue with pushp
(push with the PPX hint), push2 and push2p (two registers per
instruction), and also push the extended GPRs %r16 - %r31.

Extend amd64_analyze_register_saves to support the following Intel APX
specific push instructions:

  - REX2 push and pushp
  - EVEX push2 and push2p

Extend amd64_arch_regmap and the saved registers of the frame cache to
%r16 - %r31, and analyze up to 32 pushes instead of 16.

The encodings are described in the Intel APX Architecture Specification,
revision 9.0 (August 2026).

amd64-apx-extended-prologue-analysis.exp is structured like
amd64-extended-prologue-analysis.exp: each case is tested with code
compiled from its C source and with the assembly GCC 14.2 generated from
it, with and without .cfi directives, without a frame pointer, and with
only the EGPR feature, which saves %r16 - %r31 with single pushes.  The
test requires APX hardware.

Tested on Diamond Rapids.
---
 gdb/amd64-tdep.c                              | 102 +++++++-
 ...md64-apx-extended-prologue-analysis-egpr.S | 187 +++++++++++++
 ...64-apx-extended-prologue-analysis-no-cfi.S | 156 +++++++++++
 ...64-apx-extended-prologue-analysis-offset.S | 153 +++++++++++
 .../amd64-apx-extended-prologue-analysis.S    | 247 ++++++++++++++++++
 .../amd64-apx-extended-prologue-analysis.c    |  93 +++++++
 .../amd64-apx-extended-prologue-analysis.exp  | 192 ++++++++++++++
 7 files changed, 1122 insertions(+), 8 deletions(-)
 create mode 100644 gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis-egpr.S
 create mode 100644 gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis-no-cfi.S
 create mode 100644 gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis-offset.S
 create mode 100644 gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.S
 create mode 100644 gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.c
 create mode 100644 gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.exp

diff --git a/gdb/amd64-tdep.c b/gdb/amd64-tdep.c
index 18b8f2a4c0b..c43ffca7b8b 100644
--- a/gdb/amd64-tdep.c
+++ b/gdb/amd64-tdep.c
@@ -328,7 +328,7 @@ amd64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
 
 /* Map architectural register numbers to gdb register numbers.  */
 
-static const int amd64_arch_regmap[16] =
+static const int amd64_arch_regmap[AMD64_APX_NUM_GPRS] =
 {
   AMD64_RAX_REGNUM,	/* %rax */
   AMD64_RCX_REGNUM,	/* %rcx */
@@ -345,7 +345,23 @@ static const int amd64_arch_regmap[16] =
   AMD64_R12_REGNUM,	/* %r12 */
   AMD64_R13_REGNUM,	/* %r13 */
   AMD64_R14_REGNUM,	/* %r14 */
-  AMD64_R15_REGNUM	/* %r15 */
+  AMD64_R15_REGNUM,	/* %r15 */
+  AMD64_R16_REGNUM,	/* %r16 */
+  AMD64_R16_REGNUM + 1,	/* %r17 */
+  AMD64_R16_REGNUM + 2,	/* %r18 */
+  AMD64_R16_REGNUM + 3,	/* %r19 */
+  AMD64_R16_REGNUM + 4,	/* %r20 */
+  AMD64_R16_REGNUM + 5,	/* %r21 */
+  AMD64_R16_REGNUM + 6,	/* %r22 */
+  AMD64_R16_REGNUM + 7,	/* %r23 */
+  AMD64_R16_REGNUM + 8,	/* %r24 */
+  AMD64_R16_REGNUM + 9,	/* %r25 */
+  AMD64_R16_REGNUM + 10,	/* %r26 */
+  AMD64_R16_REGNUM + 11,	/* %r27 */
+  AMD64_R16_REGNUM + 12,	/* %r28 */
+  AMD64_R16_REGNUM + 13,	/* %r29 */
+  AMD64_R16_REGNUM + 14,	/* %r30 */
+  AMD64_R16_REGNUM + 15,	/* %r31 */
 };
 
 static const int amd64_arch_regmap_len =
@@ -2230,8 +2246,9 @@ amd64_relocate_instruction (struct gdbarch *gdbarch,
 }
 
 \f
-/* The maximum number of saved registers.  This should include %rip.  */
-#define AMD64_NUM_SAVED_REGS	AMD64_NUM_GREGS
+/* The maximum number of saved registers.  This should include %rip and
+   %r16 - %r31 as of Intel APX.  */
+#define AMD64_NUM_SAVED_REGS	(AMD64_R31_REGNUM + 1)
 
 struct amd64_frame_cache
 {
@@ -2715,7 +2732,9 @@ amd64_analyze_frame_setup (gdbarch *gdbarch, CORE_ADDR pc,
    update CACHE and return pc after those pushes or CURRENT_PC, whichever is
    smaller.  Otherwise, return PC passed to this function.
 
-   In AMD64 prologue, we only expect GPRs being pushed onto the stack.  */
+   In AMD64 prologue, we only expect GPRs being pushed onto the stack.  With
+   Intel APX, these include the extended GPRs %r16 - %r31, and the registers
+   may also be pushed with pushp, push2 and push2p.  */
 
 static CORE_ADDR
 amd64_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
@@ -2723,8 +2742,8 @@ amd64_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
 {
   gdb_byte op;
 
-  /* Limit iterating to 16 GPRs available.  */
-  for (int i = 0; i < 16 && pc < current_pc; i++)
+  /* Limit iterating to the GPRs available with Intel APX.  */
+  for (int i = 0; i < AMD64_APX_NUM_GPRS && pc < current_pc; i++)
     {
       int reg = 0;
       int pc_offset = 0;
@@ -2744,11 +2763,78 @@ amd64_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
 	  if (target_read_code (pc + 1, &op, 1) == -1)
 	    return pc;
 	}
+      else if (rex2_prefix_p (op))
+	{
+	  if (target_read_code (pc + 1, &op, 1) == -1)
+	    return pc;
+
+	  /* For the REX2 prefix, see "REX2 prefix" in the Intel APX
+	     Architecture Specification.  push and pushp are only in opcode
+	     map 0, REX2.M0 must be zero.  */
+	  if ((op & (REX2_M << 4)) != 0)
+	    break;
+
+	  /* REX2.B3 and REX2.B4 extend the register number.  */
+	  reg += (op & (REX_B << 4)) | ((op & REX_B) << 3);
+	  pc_offset = 2;
+
+	  if (target_read_code (pc + 2, &op, 1) == -1)
+	    return pc;
+	}
+      else if (evex_prefix_p (op))
+	{
+	  gdb_byte insn[6];
+
+	  if (target_read_code (pc, insn, sizeof (insn)) == -1)
+	    return pc;
+
+	  /* push2 and push2p, see "PUSH2 and POP2" in the Intel APX
+	     Architecture Specification:
+
+	       byte | bit 7  6  5  4  3 | 2  1  0
+	       -----+-------------------+--------
+	       0    | 0x62
+	       1    |    R3 X3 B3 R4 B4 | 1  0  0   (map 4)
+	       2    |    W  V3 V2 V1 V0 | 1  0  0   (U = 1, pp = 0)
+	       3    |    0  0  0  ND V4 | 0  0  0   (ND = 1)
+
+	     followed by opcode 0xff and ModR/M with mod = 3, reg = 6.  */
+	  if ((insn[1] & 0x7) != 4 || (insn[2] & 0x3) != 0
+	      || (insn[3] & 0x10) == 0 || insn[4] != 0xff
+	      || MODRM_MOD_FIELD (insn[5]) != 3
+	      || MODRM_REG_FIELD (insn[5]) != 6)
+	    break;
+
+	  /* Order of pushes: register in EVEX.V4:vvvv is pushed first, then
+	     the one in EVEX.B4:B3 and ModR/M.rm.  All of these register
+	     bits but B4 are stored inverted.  */
+	  const gdb_byte p0 = insn[1];
+	  const gdb_byte p0_inv = ~insn[1];
+	  const gdb_byte p1_inv = ~insn[2];
+	  const gdb_byte p2_inv = ~insn[3];
+	  int regs[2]
+	    = { ((p2_inv << 1) & 0x10) | ((p1_inv >> 3) & 0x0f),
+		((p0 << 1) & 0x10) | ((p0_inv >> 2) & 0x08)
+		| MODRM_RM_FIELD (insn[5]) };
+
+	  cache->sp_offset += 8;
+	  cache->saved_regs[amd64_arch_reg_to_regnum (regs[0])]
+	    = -cache->sp_offset;
+	  cache->sp_offset += 8;
+	  cache->saved_regs[amd64_arch_reg_to_regnum (regs[1])]
+	    = -cache->sp_offset;
+
+	  pc += sizeof (insn);
+	  continue;
+	}
 
       /* push %rax|%rcx|%rdx|%rbx|%rsp|%rbp|%rsi|%rdi
 
 	 or with 0x41 prefix:
-	 push %r8|%r9|%r10|%r11|%r12|%r13|%r14|%r15.  */
+	 push %r8|%r9|%r10|%r11|%r12|%r13|%r14|%r15
+
+	 or with REX2 prefix (Intel APX):
+	 push or pushp of any of %rax - %r31.  */
       if (op < 0x50 || op > 0x57)
 	break;
 
diff --git a/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis-egpr.S b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis-egpr.S
new file mode 100644
index 00000000000..533581c97ef
--- /dev/null
+++ b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis-egpr.S
@@ -0,0 +1,187 @@
+/* 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 gcc 14.2.0 with flags: -g0 -O1 -S -mapx-features=egpr
+   -mgeneral-regs-only -fno-omit-frame-pointer
+   -fno-asynchronous-unwind-tables.  */
+
+	.file	"amd64-apx-extended-prologue-analysis.c"
+	.text
+	.globl	bar
+	.type	bar, @function
+bar:
+	endbr64
+	leal	(%rdi,%rdi), %eax
+	ret
+	.size	bar, .-bar
+	.globl	foo
+	.type	foo, @function
+foo:
+	endbr64
+	pushq	%rbp
+	movq	%rsp, %rbp
+	pushq	%r15
+	pushq	%r14
+	pushq	%r13
+	pushq	%r12
+	pushq	%rbx
+	subq	$280, %rsp
+	movl	%edi, %ebx
+	movl	%esi, %r14d
+	movl	%edx, %r13d
+	movl	%ecx, %r12d
+	movq	%fs:40, %rax
+	movq	%rax, -56(%rbp)
+	xorl	%eax, %eax
+	.p2align 5
+.L3:
+	leal	(%rbx,%rax), %ecx
+	movslq	%eax, %rdx
+	movb	%cl, -312(%rbp,%rdx)
+	addl	$1, %eax
+	cmpl	$256, %eax
+	jne	.L3
+	movl	%ebx, %edi
+	call	bar
+	movl	%eax, %r15d
+	movl	%r14d, %edi
+	call	bar
+	leal	(%r15,%rax), %r14d
+	movl	%r13d, %edi
+	call	bar
+	addl	%eax, %r14d
+	movl	%r12d, %edi
+	call	bar
+	addl	%r14d, %eax
+	addl	%ebx, %eax
+	movq	-56(%rbp), %rdx
+	subq	%fs:40, %rdx
+	jne	.L7
+	addq	$280, %rsp
+	popq	%rbx
+	popq	%r12
+	popq	%r13
+	popq	%r14
+	popq	%r15
+	popq	%rbp
+	ret
+.L7:
+	call	__stack_chk_fail@PLT
+	.size	foo, .-foo
+	.globl	nocsr
+	.type	nocsr, @function
+nocsr:
+	endbr64
+	pushq	%rbp
+	movq	%rsp, %rbp
+	pushq	%r31
+	pushq	%r30
+	pushq	%r29
+	pushq	%r28
+	pushq	%r27
+	pushq	%r26
+	pushq	%r25
+	pushq	%r24
+	pushq	%r23
+	pushq	%r22
+	pushq	%r21
+	pushq	%r20
+	pushq	%r19
+	pushq	%r18
+	pushq	%r17
+	pushq	%r16
+	pushq	%r11
+	pushq	%r10
+	pushq	%r9
+	pushq	%r8
+	pushq	%rdi
+	pushq	%rsi
+	pushq	%rcx
+	pushq	%rdx
+	call	bar
+	popq	%rdx
+	popq	%rcx
+	popq	%rsi
+	popq	%rdi
+	popq	%r8
+	popq	%r9
+	popq	%r10
+	popq	%r11
+	popq	%r16
+	popq	%r17
+	popq	%r18
+	popq	%r19
+	popq	%r20
+	popq	%r21
+	popq	%r22
+	popq	%r23
+	popq	%r24
+	popq	%r25
+	popq	%r26
+	popq	%r27
+	popq	%r28
+	popq	%r29
+	popq	%r30
+	popq	%r31
+	popq	%rbp
+	ret
+	.size	nocsr, .-nocsr
+	.globl	main
+	.type	main, @function
+main:
+	endbr64
+	pushq	%rbp
+	movq	%rsp, %rbp
+	pushq	%r12
+	pushq	%rbx
+	subq	$16, %rsp
+	movl	%edi, %ebx
+	leal	(%rdi,%rdi), %ecx
+	leal	2(%rdi), %edx
+	leal	1(%rdi), %esi
+	call	foo
+	movl	%eax, %r12d
+	movl	%ebx, %edi
+	call	nocsr
+	addl	%eax, %r12d
+	movl	%r12d, -20(%rbp)
+	movl	-20(%rbp), %eax
+	addq	$16, %rsp
+	popq	%rbx
+	popq	%r12
+	popq	%rbp
+	ret
+	.size	main, .-main
+	.ident	"GCC: (Ubuntu 14.2.0-4ubuntu2~24.04.1) 14.2.0"
+	.section	.note.GNU-stack,"",@progbits
+	.section	.note.gnu.property,"a"
+	.align 8
+	.long	1f - 0f
+	.long	4f - 1f
+	.long	5
+0:
+	.string	"GNU"
+1:
+	.align 8
+	.long	0xc0000002
+	.long	3f - 2f
+2:
+	.long	0x3
+3:
+	.align 8
+4:
diff --git a/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis-no-cfi.S b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis-no-cfi.S
new file mode 100644
index 00000000000..d0059004b3c
--- /dev/null
+++ b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis-no-cfi.S
@@ -0,0 +1,156 @@
+/* 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 gcc 14.2.0 with flags: -g0 -O1 -S -mapxf -mgeneral-regs-only
+   -fno-omit-frame-pointer -fno-asynchronous-unwind-tables.  */
+
+	.file	"amd64-apx-extended-prologue-analysis.c"
+	.text
+	.globl	bar
+	.type	bar, @function
+bar:
+	endbr64
+	leal	(%rdi,%rdi), %eax
+	ret
+	.size	bar, .-bar
+	.globl	foo
+	.type	foo, @function
+foo:
+	endbr64
+	pushq	%rbp
+	movq	%rsp, %rbp
+	push2p	%r14, %r15
+	push2p	%r12, %r13
+	pushp	%rbx
+	subq	$280, %rsp
+	movl	%edi, %ebx
+	movl	%esi, %r14d
+	movl	%edx, %r13d
+	movl	%ecx, %r12d
+	movq	%fs:40, %rax
+	movq	%rax, -56(%rbp)
+	xorl	%eax, %eax
+	.p2align 5
+.L3:
+	leal	(%rbx,%rax), %ecx
+	movslq	%eax, %rdx
+	movb	%cl, -312(%rbp,%rdx)
+	addl	$1, %eax
+	cmpl	$256, %eax
+	jne	.L3
+	movl	%ebx, %edi
+	call	bar
+	movl	%eax, %r15d
+	movl	%r14d, %edi
+	call	bar
+	leal	(%r15,%rax), %r14d
+	movl	%r13d, %edi
+	call	bar
+	addl	%eax, %r14d
+	movl	%r12d, %edi
+	call	bar
+	addl	%r14d, %eax
+	addl	%ebx, %eax
+	movq	-56(%rbp), %rdx
+	subq	%fs:40, %rdx
+	jne	.L7
+	addq	$280, %rsp
+	popp	%rbx
+	pop2p	%r13, %r12
+	pop2p	%r15, %r14
+	popq	%rbp
+	ret
+.L7:
+	call	__stack_chk_fail@PLT
+	.size	foo, .-foo
+	.globl	nocsr
+	.type	nocsr, @function
+nocsr:
+	endbr64
+	pushq	%rbp
+	movq	%rsp, %rbp
+	push2p	%r30, %r31
+	push2p	%r28, %r29
+	push2p	%r26, %r27
+	push2p	%r24, %r25
+	push2p	%r22, %r23
+	push2p	%r20, %r21
+	push2p	%r18, %r19
+	push2p	%r16, %r17
+	push2p	%r10, %r11
+	push2p	%r8, %r9
+	push2p	%rsi, %rdi
+	push2p	%rdx, %rcx
+	call	bar
+	pop2p	%rcx, %rdx
+	pop2p	%rdi, %rsi
+	pop2p	%r9, %r8
+	pop2p	%r11, %r10
+	pop2p	%r17, %r16
+	pop2p	%r19, %r18
+	pop2p	%r21, %r20
+	pop2p	%r23, %r22
+	pop2p	%r25, %r24
+	pop2p	%r27, %r26
+	pop2p	%r29, %r28
+	pop2p	%r31, %r30
+	popq	%rbp
+	ret
+	.size	nocsr, .-nocsr
+	.globl	main
+	.type	main, @function
+main:
+	endbr64
+	pushq	%rbp
+	movq	%rsp, %rbp
+	push2p	%rbx, %r12
+	subq	$16, %rsp
+	movl	%edi, %ebx
+	leal	(%rdi,%rdi), %ecx
+	leal	2(%rdi), %edx
+	leal	1(%rdi), %esi
+	call	foo
+	movl	%eax, %r12d
+	movl	%ebx, %edi
+	call	nocsr
+	addl	%eax, %r12d
+	movl	%r12d, -20(%rbp)
+	movl	-20(%rbp), %eax
+	addq	$16, %rsp
+	pop2p	%r12, %rbx
+	popq	%rbp
+	ret
+	.size	main, .-main
+	.ident	"GCC: (Ubuntu 14.2.0-4ubuntu2~24.04.1) 14.2.0"
+	.section	.note.GNU-stack,"",@progbits
+	.section	.note.gnu.property,"a"
+	.align 8
+	.long	1f - 0f
+	.long	4f - 1f
+	.long	5
+0:
+	.string	"GNU"
+1:
+	.align 8
+	.long	0xc0000002
+	.long	3f - 2f
+2:
+	.long	0x3
+3:
+	.align 8
+4:
diff --git a/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis-offset.S b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis-offset.S
new file mode 100644
index 00000000000..20e47b9b02b
--- /dev/null
+++ b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis-offset.S
@@ -0,0 +1,153 @@
+/* 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 gcc 14.2.0 with flags: -g0 -O1 -S -mapxf -mgeneral-regs-only
+   -fomit-frame-pointer -fno-asynchronous-unwind-tables.  */
+
+	.file	"amd64-apx-extended-prologue-analysis.c"
+	.text
+	.globl	bar
+	.type	bar, @function
+bar:
+	endbr64
+	leal	(%rdi,%rdi), %eax
+	ret
+	.size	bar, .-bar
+	.globl	foo
+	.type	foo, @function
+foo:
+	endbr64
+	pushp	%r14
+	push2p	%r12, %r13
+	push2p	%rbx, %rbp
+	subq	$272, %rsp
+	movl	%edi, %ebx
+	movl	%esi, %r13d
+	movl	%edx, %r12d
+	movl	%ecx, %ebp
+	movq	%fs:40, %rax
+	movq	%rax, 264(%rsp)
+	xorl	%eax, %eax
+	.p2align 5
+.L3:
+	leal	(%rbx,%rax), %ecx
+	movslq	%eax, %rdx
+	movb	%cl, 8(%rsp,%rdx)
+	addl	$1, %eax
+	cmpl	$256, %eax
+	jne	.L3
+	movl	%ebx, %edi
+	call	bar
+	movl	%eax, %r14d
+	movl	%r13d, %edi
+	call	bar
+	leal	(%r14,%rax), %r13d
+	movl	%r12d, %edi
+	call	bar
+	addl	%eax, %r13d
+	movl	%ebp, %edi
+	call	bar
+	addl	%r13d, %eax
+	addl	%ebx, %eax
+	movq	264(%rsp), %rdx
+	subq	%fs:40, %rdx
+	jne	.L7
+	addq	$272, %rsp
+	pop2p	%rbp, %rbx
+	pop2p	%r13, %r12
+	popp	%r14
+	ret
+.L7:
+	call	__stack_chk_fail@PLT
+	.size	foo, .-foo
+	.globl	nocsr
+	.type	nocsr, @function
+nocsr:
+	endbr64
+	pushp	%r31
+	push2p	%r29, %r30
+	push2p	%r27, %r28
+	push2p	%r25, %r26
+	push2p	%r23, %r24
+	push2p	%r21, %r22
+	push2p	%r19, %r20
+	push2p	%r17, %r18
+	push2p	%r11, %r16
+	push2p	%r9, %r10
+	push2p	%rdi, %r8
+	push2p	%rcx, %rsi
+	pushp	%rdx
+	subq	$8, %rsp
+	call	bar
+	addq	$8, %rsp
+	popp	%rdx
+	pop2p	%rsi, %rcx
+	pop2p	%r8, %rdi
+	pop2p	%r10, %r9
+	pop2p	%r16, %r11
+	pop2p	%r18, %r17
+	pop2p	%r20, %r19
+	pop2p	%r22, %r21
+	pop2p	%r24, %r23
+	pop2p	%r26, %r25
+	pop2p	%r28, %r27
+	pop2p	%r30, %r29
+	popp	%r31
+	ret
+	.size	nocsr, .-nocsr
+	.globl	main
+	.type	main, @function
+main:
+	endbr64
+	pushp	%rbp
+	pushp	%rbx
+	subq	$24, %rsp
+	movl	%edi, %ebx
+	leal	(%rdi,%rdi), %ecx
+	leal	2(%rdi), %edx
+	leal	1(%rdi), %esi
+	call	foo
+	movl	%eax, %ebp
+	movl	%ebx, %edi
+	call	nocsr
+	addl	%eax, %ebp
+	movl	%ebp, 12(%rsp)
+	movl	12(%rsp), %eax
+	addq	$24, %rsp
+	popp	%rbx
+	popp	%rbp
+	ret
+	.size	main, .-main
+	.ident	"GCC: (Ubuntu 14.2.0-4ubuntu2~24.04.1) 14.2.0"
+	.section	.note.GNU-stack,"",@progbits
+	.section	.note.gnu.property,"a"
+	.align 8
+	.long	1f - 0f
+	.long	4f - 1f
+	.long	5
+0:
+	.string	"GNU"
+1:
+	.align 8
+	.long	0xc0000002
+	.long	3f - 2f
+2:
+	.long	0x3
+3:
+	.align 8
+4:
diff --git a/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.S b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.S
new file mode 100644
index 00000000000..956c43bf812
--- /dev/null
+++ b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.S
@@ -0,0 +1,247 @@
+/* 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 gcc 14.2.0 with flags: -g0 -O1 -S -mapxf -mgeneral-regs-only
+   -fno-omit-frame-pointer.  */
+
+	.file	"amd64-apx-extended-prologue-analysis.c"
+	.text
+	.globl	bar
+	.type	bar, @function
+bar:
+.LFB0:
+	.cfi_startproc
+	endbr64
+	leal	(%rdi,%rdi), %eax
+	ret
+	.cfi_endproc
+.LFE0:
+	.size	bar, .-bar
+	.globl	foo
+	.type	foo, @function
+foo:
+.LFB1:
+	.cfi_startproc
+	endbr64
+	pushq	%rbp
+	.cfi_def_cfa_offset 16
+	.cfi_offset 6, -16
+	movq	%rsp, %rbp
+	.cfi_def_cfa_register 6
+	push2p	%r14, %r15
+	push2p	%r12, %r13
+	pushp	%rbx
+	subq	$280, %rsp
+	.cfi_offset 15, -24
+	.cfi_offset 14, -32
+	.cfi_offset 13, -40
+	.cfi_offset 12, -48
+	.cfi_offset 3, -56
+	movl	%edi, %ebx
+	movl	%esi, %r14d
+	movl	%edx, %r13d
+	movl	%ecx, %r12d
+	movq	%fs:40, %rax
+	movq	%rax, -56(%rbp)
+	xorl	%eax, %eax
+	.p2align 5
+.L3:
+	leal	(%rbx,%rax), %ecx
+	movslq	%eax, %rdx
+	movb	%cl, -312(%rbp,%rdx)
+	addl	$1, %eax
+	cmpl	$256, %eax
+	jne	.L3
+	movl	%ebx, %edi
+	call	bar
+	movl	%eax, %r15d
+	movl	%r14d, %edi
+	call	bar
+	leal	(%r15,%rax), %r14d
+	movl	%r13d, %edi
+	call	bar
+	addl	%eax, %r14d
+	movl	%r12d, %edi
+	call	bar
+	addl	%r14d, %eax
+	addl	%ebx, %eax
+	movq	-56(%rbp), %rdx
+	subq	%fs:40, %rdx
+	jne	.L7
+	addq	$280, %rsp
+	popp	%rbx
+	pop2p	%r13, %r12
+	.cfi_remember_state
+	.cfi_restore 13
+	.cfi_restore 12
+	pop2p	%r15, %r14
+	.cfi_restore 15
+	.cfi_restore 14
+	popq	%rbp
+	.cfi_def_cfa 7, 8
+	ret
+.L7:
+	.cfi_restore_state
+	call	__stack_chk_fail@PLT
+	.cfi_endproc
+.LFE1:
+	.size	foo, .-foo
+	.globl	nocsr
+	.type	nocsr, @function
+nocsr:
+.LFB2:
+	.cfi_startproc
+	endbr64
+	pushq	%rbp
+	.cfi_def_cfa_offset 16
+	.cfi_offset 6, -16
+	movq	%rsp, %rbp
+	.cfi_def_cfa_register 6
+	push2p	%r30, %r31
+	push2p	%r28, %r29
+	push2p	%r26, %r27
+	push2p	%r24, %r25
+	push2p	%r22, %r23
+	push2p	%r20, %r21
+	push2p	%r18, %r19
+	push2p	%r16, %r17
+	push2p	%r10, %r11
+	push2p	%r8, %r9
+	push2p	%rsi, %rdi
+	push2p	%rdx, %rcx
+	.cfi_offset 145, -24
+	.cfi_offset 144, -32
+	.cfi_offset 143, -40
+	.cfi_offset 142, -48
+	.cfi_offset 141, -56
+	.cfi_offset 140, -64
+	.cfi_offset 139, -72
+	.cfi_offset 138, -80
+	.cfi_offset 137, -88
+	.cfi_offset 136, -96
+	.cfi_offset 135, -104
+	.cfi_offset 134, -112
+	.cfi_offset 133, -120
+	.cfi_offset 132, -128
+	.cfi_offset 131, -136
+	.cfi_offset 130, -144
+	.cfi_offset 11, -152
+	.cfi_offset 10, -160
+	.cfi_offset 9, -168
+	.cfi_offset 8, -176
+	.cfi_offset 5, -184
+	.cfi_offset 4, -192
+	.cfi_offset 2, -200
+	.cfi_offset 1, -208
+	call	bar
+	pop2p	%rcx, %rdx
+	.cfi_restore 2
+	.cfi_restore 1
+	pop2p	%rdi, %rsi
+	.cfi_restore 5
+	.cfi_restore 4
+	pop2p	%r9, %r8
+	.cfi_restore 9
+	.cfi_restore 8
+	pop2p	%r11, %r10
+	.cfi_restore 11
+	.cfi_restore 10
+	pop2p	%r17, %r16
+	.cfi_restore 131
+	.cfi_restore 130
+	pop2p	%r19, %r18
+	.cfi_restore 133
+	.cfi_restore 132
+	pop2p	%r21, %r20
+	.cfi_restore 135
+	.cfi_restore 134
+	pop2p	%r23, %r22
+	.cfi_restore 137
+	.cfi_restore 136
+	pop2p	%r25, %r24
+	.cfi_restore 139
+	.cfi_restore 138
+	pop2p	%r27, %r26
+	.cfi_restore 141
+	.cfi_restore 140
+	pop2p	%r29, %r28
+	.cfi_restore 143
+	.cfi_restore 142
+	pop2p	%r31, %r30
+	.cfi_restore 145
+	.cfi_restore 144
+	popq	%rbp
+	.cfi_def_cfa 7, 8
+	ret
+	.cfi_endproc
+.LFE2:
+	.size	nocsr, .-nocsr
+	.globl	main
+	.type	main, @function
+main:
+.LFB3:
+	.cfi_startproc
+	endbr64
+	pushq	%rbp
+	.cfi_def_cfa_offset 16
+	.cfi_offset 6, -16
+	movq	%rsp, %rbp
+	.cfi_def_cfa_register 6
+	push2p	%rbx, %r12
+	subq	$16, %rsp
+	.cfi_offset 12, -24
+	.cfi_offset 3, -32
+	movl	%edi, %ebx
+	leal	(%rdi,%rdi), %ecx
+	leal	2(%rdi), %edx
+	leal	1(%rdi), %esi
+	call	foo
+	movl	%eax, %r12d
+	movl	%ebx, %edi
+	call	nocsr
+	addl	%eax, %r12d
+	movl	%r12d, -20(%rbp)
+	movl	-20(%rbp), %eax
+	addq	$16, %rsp
+	pop2p	%r12, %rbx
+	.cfi_restore 12
+	.cfi_restore 3
+	popq	%rbp
+	.cfi_def_cfa 7, 8
+	ret
+	.cfi_endproc
+.LFE3:
+	.size	main, .-main
+	.ident	"GCC: (Ubuntu 14.2.0-4ubuntu2~24.04.1) 14.2.0"
+	.section	.note.GNU-stack,"",@progbits
+	.section	.note.gnu.property,"a"
+	.align 8
+	.long	1f - 0f
+	.long	4f - 1f
+	.long	5
+0:
+	.string	"GNU"
+1:
+	.align 8
+	.long	0xc0000002
+	.long	3f - 2f
+2:
+	.long	0x3
+3:
+	.align 8
+4:
diff --git a/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.c b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.c
new file mode 100644
index 00000000000..ee137e7845d
--- /dev/null
+++ b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.c
@@ -0,0 +1,93 @@
+/* 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/>.  */
+int __attribute__ ((noinline))
+bar (int x)
+{
+  return x + x;
+}
+
+/* With -mapxf and a frame pointer, this function should generate a
+   prologue in shape of:
+    push    %rbp
+    .cfi_def_cfa_offset 16
+    .cfi_offset %rbp, -16
+    mov     %rsp, %rbp
+    .cfi_def_cfa_register %rbp
+    push2p  %reg1, %reg2
+    push2p  %reg3, %reg4
+    pushp   %reg5
+    sub     $XXX, %rsp
+    .cfi_offset %reg2, -24
+    .cfi_offset %reg1, -32
+    .cfi_offset %reg4, -40
+    .cfi_offset %reg3, -48
+    .cfi_offset %reg5, -56
+
+   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.
+
+   So to be able to unwind a register, GDB needs to skip the prologue
+   past the register pushes and stack allocation (to access .cfi
+   directives).  */
+int __attribute__ ((noinline))
+foo (int a, int b, int c, int d)
+{
+  /* "volatile" alone isn't enough for clang to not optimize it out and
+     allocate space on the stack.  */
+  volatile char s[256];
+  for (int i = 0; i < 256; i++)
+    s[i] = (char) (a + i);
+
+  a += bar (a) + bar (b) + bar (c) + bar (d);
+  return a;
+}
+
+/* This function saves all registers that the call to bar may change, the
+   extended GPRs %r16 - %r31 included, except %rax, which returns the
+   result.  With -mapxf and a frame pointer, it should generate a prologue
+   in shape of:
+    push    %rbp
+    .cfi_def_cfa_offset 16
+    .cfi_offset %rbp, -16
+    mov     %rsp, %rbp
+    .cfi_def_cfa_register %rbp
+    push2p  %r30, %r31
+    ...
+    push2p  %r16, %r17
+    push2p  %r10, %r11
+    ...
+    push2p  %rdx, %rcx
+    .cfi_offset %r31, -24
+    ...
+    .cfi_offset %rdx, -208
+
+   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.  */
+__attribute__ ((noinline, no_caller_saved_registers)) int
+nocsr (int x)
+{
+  return bar (x);
+}
+
+int
+main (int argc, char **argv)
+{
+  volatile int a = foo (argc, argc + 1, argc + 2, argc * 2) + nocsr (argc);
+  return a;
+}
diff --git a/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.exp b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.exp
new file mode 100644
index 00000000000..ccc8a134bf8
--- /dev/null
+++ b/gdb/testsuite/gdb.arch/amd64-apx-extended-prologue-analysis.exp
@@ -0,0 +1,192 @@
+# Copyright 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/>.
+
+# Test that the amd64 prologue analyzer handles the Intel APX instructions
+# pushp, push2 and push2p, and pushes of the extended GPRs %r16 - %r31, in
+# functions without debug info.  When stopped after the prologue, the
+# registers it saves must be unwound.
+#
+# The cases are:
+#
+#  - w/o .cfi directives: GDB places the breakpoint after the prologue and
+#    unwinds the saved registers with the prologue analyzer only.
+#
+#  - with .cfi directives: GDB still places the breakpoint with the
+#    analyzer, but unwinds with the .cfi directives.  GDB can only unwind
+#    the saved registers if the breakpoint is past the prologue.
+#
+#  - offset initialization: without a frame pointer, GDB places the
+#    breakpoint at the function entry.  The test places a breakpoint after
+#    the prologue.  GDB computes the frame address from %rsp and the stack
+#    space the analyzed prologue uses.  A single push aligns the stack for
+#    the subsequent push2.
+#
+#  - single pushes of %r16 - %r31: with only EGPR enabled, the compiler saves
+#    the registers of nocsr with more than 16 single pushes.
+#
+# 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
+# analyzer is known to handle if compilers change how they generate them.
+
+require allow_apx_tests
+
+standard_testfile .c -no-cfi.S .S -offset.S -egpr.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.
+proc prologue_info { func } {
+    set insns {}
+    gdb_test_multiple "disassemble $func" "" -lbl {
+	-re "\r\n\\s*($::hex) <\\+$::decimal>:\\s+(\[^\r\n\]*)(?=\r\n)" {
+	    set insn [regsub {^\{rex2[^\}]*\}} $expect_out(2,string) ""]
+	    set insn [regsub -all {\s+} [string trim $insn] " "]
+	    lappend insns [list $expect_out(1,string) $insn]
+	    exp_continue
+	}
+	-re -wrap "" {
+	    pass $gdb_test_name
+	}
+    }
+
+    set i 0
+    if { [lindex $insns $i 1] eq "endbr64" } {
+	incr i
+    }
+
+    set has_frame 0
+    set regs {}
+    if { [lindex $insns $i 1] eq "push %rbp"
+	 && [lindex $insns [expr {$i + 1}] 1] eq "mov %rsp,%rbp" } {
+	set has_frame 1
+	lappend regs rbp
+	incr i 2
+    }
+
+    while { [regexp {^push2?p? %(\w+)(?:,%(\w+))?$} [lindex $insns $i 1] \
+		 -> reg1 reg2] } {
+	# push2 pushes the second register first.
+	if { $reg2 ne "" } {
+	    lappend regs $reg2
+	}
+	lappend regs $reg1
+	incr i
+    }
+
+    if { [regexp {^sub \$0x[0-9a-f]+,%rsp$} [lindex $insns $i 1]] } {
+	incr i
+    }
+
+    return [list $has_frame [lindex $insns $i 0] $regs]
+}
+
+# Run to FUNC, past its prologue, and check that the registers it saves are
+# unwound.  CFI is true if the binary has .cfi directives.
+proc test_function { func cfi } {
+    lassign [prologue_info $func] has_frame end regs
+
+    if { $has_frame } {
+	# For a function with a frame pointer setup, GDB places the
+	# breakpoint after the prologue.
+	gdb_breakpoint $func
+	gdb_continue_to_breakpoint "continue to $func"
+	gdb_test "print \$pc == $end" " = 1" "stopped after the prologue"
+    } else {
+	gdb_breakpoint "*$end"
+	gdb_continue_to_breakpoint "continue past the prologue of $func"
+    }
+
+    gdb_test "backtrace" \
+	"#0 \[^\r\n\]+ $func \\(\\)\r\n#1 \[^\r\n\]+ main \\(\\)" \
+	"backtrace in $func"
+
+    foreach reg $regs {
+	# With a frame pointer, %rbp already holds the frame address of FUNC.
+	if { $has_frame && $reg eq "rbp" } {
+	    continue
+	}
+
+	# %rax returns the result, so a compiler may push it only to align
+	# the stack, as clang does in nocsr, and then not describe it in the
+	# CFI.
+	if { $cfi && $reg eq "rax" } {
+	    continue
+	}
+
+	# FUNC has not changed the saved registers yet.
+	with_test_prefix $reg {
+	    set caller_val [get_hexadecimal_valueof "\$$reg" "null"]
+	    gdb_test_no_output "set var \$$reg = \$$reg + 1"
+	    gdb_test "up" " main \\(\\)" "go up to main"
+	    gdb_test "print/x \$$reg" " = $caller_val" "$reg unwound"
+	    gdb_test "down" " $func \\(\\)" "go back down to $func"
+	}
+    }
+}
+
+# Build EXE from SRCFILES with OPTS and test the prologues of foo and nocsr.
+# CFI is true if the binary has .cfi directives.
+proc test_case { exe srcfiles opts cfi } {
+    if { [prepare_for_testing "failed to prepare" $exe $srcfiles $opts] } {
+	return
+    }
+
+    foreach func { foo nocsr } {
+	with_test_prefix $func {
+	    clean_restart $exe
+	    if { ![runto_main] } {
+		return
+	    }
+	    test_function $func $cfi
+	}
+    }
+}
+
+# Name, assembly file, compiler flags of each case, and whether it has .cfi
+# directives.  -mgeneral-regs-only is needed for the
+# no_caller_saved_registers attribute of nocsr.
+set cases [list \
+    "w/o .cfi directives" $srcfile2 \
+	{-mapxf -fno-omit-frame-pointer -fno-asynchronous-unwind-tables} 0 \
+    "with .cfi directives" $srcfile3 \
+	{-mapxf -fno-omit-frame-pointer} 1 \
+    "offset initialization" $srcfile4 \
+	{-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]
+
+set n 0
+foreach { name asm_file flags cfi } $cases {
+    with_test_prefix $name {
+	incr n
+	set opts [list nodebug optimize=-O1 \
+		      additional_flags=-mgeneral-regs-only]
+	foreach flag $flags {
+	    lappend opts additional_flags=$flag
+	}
+
+	with_test_prefix "compiler gen" {
+	    test_case $testfile-$n-C $srcfile $opts $cfi
+	}
+
+	with_test_prefix "ASM source" {
+	    test_case $testfile-$n-S $asm_file nodebug $cfi
+	}
+    }
+}
-- 
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.


  reply	other threads:[~2026-10-30 22:02 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 ` Stephan Rohr [this message]
2026-10-30 22:02 ` [PATCH 2/2] gdb/amd64: recognize pushp %rbp as a frame setup Stephan Rohr
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-2-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