]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[SFrame-V3] gas: sframe: testsuite: handle .cfi_offset for RA
authorIndu Bhagat <indu.bhagat@oracle.com>
Fri, 16 Jan 2026 00:43:16 +0000 (16:43 -0800)
committerIndu Bhagat <indu.bhagat@oracle.com>
Fri, 16 Jan 2026 01:02:26 +0000 (17:02 -0800)
With the introduction of flex FDE type, handling .cfi_offset for
RA needs adjustment.

On architectures like x86_64, the return address (RA) is typically saved
at a fixed offset from the CFA. Previous versions of the SFrame format
assumed this fixed offset was invariant for the entire function on such
architectures. Consequently, GAS would warn and suppress SFrame
generation if it encountered a .cfi_offset directive for the RA
register that deviated from this fixed default.

SFrame V3 introduces "Flex FDEs" which allows tracking the RA location
explicitly even on architectures where it is usually fixed.

This patch updates sframe_xlate_do_offset () to leverage Flex FDEs. When
processing a .cfi_offset for the RA register:
  - The check for non-representable RA offsets is relaxed. If the ABI
    supports Flex FDEs (SFrame V3), GAS proceeds instead of issuing a
    warning.
  - For ABIs without explicit RA tracking (like AMD64), if the RA
    offset differs from the default fixed offset, the FDE is marked as a
    Flex FDE, and the new stack location is recorded.
  - Logic is added to detect when the RA is restored to its standard
    fixed offset. In this case, the tracking state is reset (ra_loc set
    to SFRAME_FRE_ELEM_LOC_NONE), deferring to the ABI's default fixed RA
    offset behavior.

gas/
* gen-sframe.c (sframe_xlate_do_offset): Support .cfi_offset for RA
by switching to Flex FDEs when necessary.
gas/testsuite/
* gas/cfi-sframe/cfi-sframe-x86_64-6.d: New test.
* gas/cfi-sframe/cfi-sframe-x86_64-6.s: New test to check
transition of location of REG_RA from register to CFA-8 (default
location on AMD64).  Flex FDE in effect.
* gas/cfi-sframe/cfi-sframe.exp: Add new test.

gas/gen-sframe.c
gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-6.d [new file with mode: 0644]
gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-6.s [new file with mode: 0644]
gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-7.d [new file with mode: 0644]
gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-7.s [new file with mode: 0644]
gas/testsuite/gas/cfi-sframe/cfi-sframe.exp

index e5850e5aa00e53447a5f60d14fe087dbe5ef6040..e038c522d917bfa5b64677dc89424a148ab46f52 100644 (file)
@@ -1393,8 +1393,10 @@ sframe_xlate_do_offset (struct sframe_xlate_ctx *xlate_ctx,
 
   /* For ABIs not tracking RA, the return address is expected to be in a
      specific location.  Explicit manourvering to a different offset (than the
-     default offset) is non-representable in SFrame.  */
-  if (!sframe_ra_tracking_p () && cfi_insn->u.ri.reg == SFRAME_CFA_RA_REG
+     default offset) is non-representable in SFrame, unless flex FDE generation
+     is supported for the ABI.  */
+  if (!sframe_support_flex_fde_p () && !sframe_ra_tracking_p ()
+      && cfi_insn->u.ri.reg == SFRAME_CFA_RA_REG
       && cfi_insn->u.ri.offset != sframe_cfa_ra_offset ())
     {
       as_warn (_("no SFrame FDE emitted; %s register %u in .cfi_offset"),
@@ -1412,16 +1414,35 @@ sframe_xlate_do_offset (struct sframe_xlate_ctx *xlate_ctx,
       cur_fre->fp_deref_p = true;
       cur_fre->merge_candidate = false;
     }
-  else if (sframe_ra_tracking_p ()
-          && cfi_insn->u.ri.reg == SFRAME_CFA_RA_REG)
+  /* Either the ABI has enabled RA tracking, in which case we must process the
+     DW_CFA_offset opcode for REG_RA like usual.  Or if the ABI has not enabled
+     RA tracking, but flex FDE generation is supported, distinguish between
+     whether its time to reset the RA tracking state or not.  */
+  else if (cfi_insn->u.ri.reg == SFRAME_CFA_RA_REG)
     {
-      sframe_fre_set_ra_track (cur_fre, cfi_insn->u.ri.offset);
-      cur_fre->ra_reg = SFRAME_FRE_REG_INVALID;
-      cur_fre->ra_deref_p = true;
-      cur_fre->merge_candidate = false;
+      if (!sframe_ra_tracking_p ()
+         && cfi_insn->u.ri.offset == sframe_cfa_ra_offset ())
+       {
+         /* Reset RA tracking info, if fixed offset.  */
+         cur_fre->ra_reg = SFRAME_FRE_REG_INVALID;
+         cur_fre->ra_loc = SFRAME_FRE_ELEM_LOC_NONE;
+         cur_fre->ra_deref_p = false;
+         cur_fre->merge_candidate = false;
+       }
+      else
+       {
+         sframe_fre_set_ra_track (cur_fre, cfi_insn->u.ri.offset);
+         cur_fre->ra_reg = SFRAME_FRE_REG_INVALID;
+         cur_fre->ra_loc = SFRAME_FRE_ELEM_LOC_STACK;
+         cur_fre->ra_deref_p = true;
+         cur_fre->merge_candidate = false;
+
+         if (!sframe_ra_tracking_p () && sframe_support_flex_fde_p ())
+           xlate_ctx->flex_p = true;
+       }
     }
-  /* This is used to track changes to non-rsp registers, skip all others
-     except FP / RA for now.  */
+
+  /* Skip all other registers.  */
   return SFRAME_XLATE_OK;
 }
 
diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-6.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-6.d
new file mode 100644 (file)
index 0000000..a8aa9a6
--- /dev/null
@@ -0,0 +1,25 @@
+#as: --gsframe
+#objdump: --sframe=.sframe
+#name: Flex FDE with RA state transition
+#...
+Contents of the SFrame section .sframe:
+
+  Header :
+
+    Version: SFRAME_VERSION_3
+    Flags: SFRAME_F_FDE_FUNC_START_PCREL
+    CFA fixed RA offset: \-8
+    Num FDEs: 1
+    Num FREs: 5
+
+  Function Index :
+
+    func idx \[0\]: pc = 0x0, size = 16 bytes, attr = "F"
+    STARTPC +CFA +FP +RA +
+    0+0000 +sp\+8 +u +f +
+    0+0004 +sp\+40 +u +f +
+    0+0008 +sp\+40 +u +r3\+0 +
+    0+000b +sp\+40 +u +f +
+    0+000f +sp\+8 +u +f +
+
+#pass
diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-6.s b/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-6.s
new file mode 100644 (file)
index 0000000..a4600dc
--- /dev/null
@@ -0,0 +1,16 @@
+# Testcase for transition of tracked entity (rip) from
+# register to CFA+offset.
+       .type   foo, @function
+foo:
+       .cfi_startproc
+       sub     $0x20,%rsp
+       .cfi_adjust_cfa_offset 0x20
+       .long 0
+       .cfi_register rip, 3
+       mov %rax, %rdi
+       .cfi_offset rip, -8
+       .long 0
+       .cfi_def_cfa rsp, 8
+       ret
+       .cfi_endproc
+       .size   foo, .-foo
diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-7.d b/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-7.d
new file mode 100644 (file)
index 0000000..2d910f3
--- /dev/null
@@ -0,0 +1,25 @@
+#as: --gsframe
+#objdump: --sframe=.sframe
+#name: Flex FDE with RA state transition 2
+#...
+Contents of the SFrame section .sframe:
+
+  Header :
+
+    Version: SFRAME_VERSION_3
+    Flags: SFRAME_F_FDE_FUNC_START_PCREL
+    CFA fixed RA offset: \-8
+    Num FDEs: 1
+    Num FREs: 5
+
+  Function Index :
+
+    func idx \[0\]: pc = 0x0, size = 20 bytes, attr = "F"
+    STARTPC +CFA +FP +RA +
+    0+0000 +sp\+8 +u +f +
+    0+0004 +sp\+40 +u +f +
+    0+000b +sp\+40 +u +c-16 +
+    0+000f +sp\+8 +u +c-16 +
+    0+0013 +sp\+8 +u +f +
+
+#pass
diff --git a/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-7.s b/gas/testsuite/gas/cfi-sframe/cfi-sframe-x86_64-7.s
new file mode 100644 (file)
index 0000000..bad89ea
--- /dev/null
@@ -0,0 +1,17 @@
+# Testcase for creation of flex FDE for offset divergence from fixed offset on
+# AMD64
+       .type   foo, @function
+foo:
+       .cfi_startproc
+       sub     $0x20,%rsp
+       .cfi_adjust_cfa_offset 0x20
+       .long 0
+       mov     %rax, %rdi
+       .cfi_offset rip, -16
+       .long 0
+       .cfi_def_cfa rsp, 8
+       .long 0
+       .cfi_offset rip, -8
+       ret
+       .cfi_endproc
+       .size   foo, .-foo
index 4fbb04f838f7d77a56b405d85bb3aead98bba937..18f89c7e18921dfb83d6d3f7038e2a6f3175dae4 100644 (file)
@@ -63,6 +63,8 @@ if { [istarget "x86_64-*-*"] && [gas_sframe_check] } then {
        run_dump_test "cfi-sframe-x86_64-3"
        run_dump_test "cfi-sframe-x86_64-4"
        run_dump_test "cfi-sframe-x86_64-5"
+       run_dump_test "cfi-sframe-x86_64-6"
+       run_dump_test "cfi-sframe-x86_64-7"
        run_dump_test "cfi-sframe-x86_64-esc-expr-1"
        run_dump_test "cfi-sframe-x86_64-esc-expr-2"
        run_dump_test "cfi-sframe-x86_64-esc-expr-3"