Bug 30010 - [gdb/tdep, aarch64] Incorrect frame address for last insn (non-leaf case)
Summary: [gdb/tdep, aarch64] Incorrect frame address for last insn (non-leaf case)
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: tdep (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: 14.1
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-01-17 13:24 UTC by Tom de Vries
Modified: 2023-01-23 15:53 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tom de Vries 2023-01-17 13:24:50 UTC
[ Spinoff from PR29721. ]

Consider the following test-case:
...
$ cat test.c
void
foo (const char *s)
{
}

int
main (void)
{
  foo ("foo");
  return 0;
}
...

Compiled without debug info:
...
$ gcc test.c
...

Disassembly of main:
...
0000000000400578 <main>:
  400578:       a9bf7bfd        stp     x29, x30, [sp, #-16]!
  40057c:       910003fd        mov     x29, sp
  400580:       90000000        adrp    x0, 400000 <_init-0x408>
  400584:       91192000        add     x0, x0, #0x648
  400588:       97fffff7        bl      400564 <foo>
  40058c:       52800000        mov     w0, #0x0                        // #0
  400590:       a8c17bfd        ldp     x29, x30, [sp], #16
  400594:       d65f03c0        ret
...

Now, let's do "info frame" at each insn in main (so, using ni to skip over foo), as well as printing $sp and $fp:
...
$ gdb a.out
Reading symbols from a.out...
(gdb) b *main
Breakpoint 1 at 0x400578
(gdb) r
Starting program: a.out 

Breakpoint 1, 0x0000000000400578 in main ()
(gdb) display /x $sp
1: /x $sp = 0xfffffffff3b0
(gdb) display /x $fp
2: /x $fp = 0xfffffffff3b0
(gdb) info frame
Stack level 0, frame at 0xfffffffff3b0:
 pc = 0x400578 in main; saved pc = 0xfffff7e5bfa0
 Arglist at 0xfffffffff3b0, args: 
 Locals at 0xfffffffff3b0, Previous frame's sp is 0xfffffffff3b0
(gdb) ni
0x000000000040057c in main ()
1: /x $sp = 0xfffffffff3a0
2: /x $fp = 0xfffffffff3a0
(gdb) info frame
Stack level 0, frame at 0xfffffffff3b0:
 pc = 0x40057c in main; saved pc = 0xfffff7e5bfa0
 Arglist at 0xfffffffff3a0, args: 
 Locals at 0xfffffffff3a0, Previous frame's sp is 0xfffffffff3b0
 Saved registers:
  x29 at 0xfffffffff3a0, x30 at 0xfffffffff3a8
(gdb) ni
0x0000000000400580 in main ()
1: /x $sp = 0xfffffffff3a0
2: /x $fp = 0xfffffffff3a0
(gdb) info frame
Stack level 0, frame at 0xfffffffff3b0:
 pc = 0x400580 in main; saved pc = 0xfffff7e5bfa0
 Arglist at 0xfffffffff3a0, args: 
 Locals at 0xfffffffff3a0, Previous frame's sp is 0xfffffffff3b0
 Saved registers:
  x29 at 0xfffffffff3a0, x30 at 0xfffffffff3a8
(gdb) ni
0x0000000000400584 in main ()
1: /x $sp = 0xfffffffff3a0
2: /x $fp = 0xfffffffff3a0
(gdb) info frame
Stack level 0, frame at 0xfffffffff3b0:
 pc = 0x400584 in main; saved pc = 0xfffff7e5bfa0
 Arglist at 0xfffffffff3a0, args: 
 Locals at 0xfffffffff3a0, Previous frame's sp is 0xfffffffff3b0
 Saved registers:
  x29 at 0xfffffffff3a0, x30 at 0xfffffffff3a8
(gdb) ni
0x0000000000400588 in main ()
1: /x $sp = 0xfffffffff3a0
2: /x $fp = 0xfffffffff3a0
(gdb) info frame
Stack level 0, frame at 0xfffffffff3b0:
 pc = 0x400588 in main; saved pc = 0xfffff7e5bfa0
 Arglist at 0xfffffffff3a0, args: 
 Locals at 0xfffffffff3a0, Previous frame's sp is 0xfffffffff3b0
 Saved registers:
  x29 at 0xfffffffff3a0, x30 at 0xfffffffff3a8
(gdb) ni
0x000000000040058c in main ()
1: /x $sp = 0xfffffffff3a0
2: /x $fp = 0xfffffffff3a0
(gdb) info frame
Stack level 0, frame at 0xfffffffff3b0:
 pc = 0x40058c in main; saved pc = 0xfffff7e5bfa0
 Arglist at 0xfffffffff3a0, args: 
 Locals at 0xfffffffff3a0, Previous frame's sp is 0xfffffffff3b0
 Saved registers:
  x29 at 0xfffffffff3a0, x30 at 0xfffffffff3a8
(gdb) ni
0x0000000000400590 in main ()
1: /x $sp = 0xfffffffff3a0
2: /x $fp = 0xfffffffff3a0
(gdb) info frame
Stack level 0, frame at 0xfffffffff3b0:
 pc = 0x400590 in main; saved pc = 0xfffff7e5bfa0
 Arglist at 0xfffffffff3a0, args: 
 Locals at 0xfffffffff3a0, Previous frame's sp is 0xfffffffff3b0
 Saved registers:
  x29 at 0xfffffffff3a0, x30 at 0xfffffffff3a8
(gdb) ni
0x0000000000400594 in main ()
1: /x $sp = 0xfffffffff3b0
2: /x $fp = 0xfffffffff3b0
(gdb) info frame
Stack level 0, frame at 0xfffffffff3c0:
 pc = 0x400594 in main; saved pc = 0x4004a4
 Arglist at 0xfffffffff3b0, args: 
 Locals at 0xfffffffff3b0, Previous frame's sp is 0xfffffffff3c0
 Saved registers:
  x29 at 0xfffffffff3b0, x30 at 0xfffffffff3b8
(gdb) ni
0x0000fffff7e5bfa0 in __libc_start_main () from /lib64/libc.so.6
1: /x $sp = 0xfffffffff3b0
2: /x $fp = 0xfffffffff3b0
...

The "frame at" bit lists 0xfffffffff3b0 except at the last insn.

The frame address is calculated here in aarch64_make_prologue_cache_1:
...
  unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
  if (unwound_fp == 0)
    return;

  cache->prev_sp = unwound_fp + cache->framesize;
...

For insns after the prologue, we have cache->framereg == fp and cache->framesize == 16, so unwound_fp + cache->framesize gives the wrong answer once fp has been restored to entry value by the before-last insn.
Comment 1 Tom de Vries 2023-01-19 11:13:20 UTC
Proposed patch to trigger this PR in test-case gdb.base/unwind-on-each-insn.exp: https://sourceware.org/pipermail/gdb-patches/2023-January/195923.html
Comment 2 Sourceware Commits 2023-01-23 15:49:40 UTC
The master branch has been updated by Tom de Vries <vries@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=29e09a42f1d1af3d97652e54b6b3cac68cf61d15

commit 29e09a42f1d1af3d97652e54b6b3cac68cf61d15
Author: Tom de Vries <tdevries@suse.de>
Date:   Mon Jan 23 16:49:36 2023 +0100

    [gdb/tdep, aarch64] Fix frame address of last insn
    
    Consider the test-case test.c, compiled without debug info:
    ...
    void
    foo (const char *s)
    {
    }
    
    int
    main (void)
    {
      foo ("foo");
      return 0;
    }
    ...
    
    Disassembly of foo:
    ...
    0000000000400564 <foo>:
      400564:       d10043ff        sub     sp, sp, #0x10
      400568:       f90007e0        str     x0, [sp, #8]
      40056c:       d503201f        nop
      400570:       910043ff        add     sp, sp, #0x10
      400574:       d65f03c0        ret
    ...
    
    Now, let's do "info frame" at each insn in foo, as well as printing $sp
    and $x29 (and strip the output of info frame to the first line, for brevity):
    ...
    $ gdb -q a.out
    Reading symbols from a.out...
    (gdb) b *foo
    Breakpoint 1 at 0x400564
    (gdb) r
    Starting program: a.out
    
    Breakpoint 1, 0x0000000000400564 in foo ()
    (gdb) display /x $sp
    1: /x $sp = 0xfffffffff3a0
    (gdb) display /x $x29
    2: /x $x29 = 0xfffffffff3a0
    (gdb) info frame
    Stack level 0, frame at 0xfffffffff3a0:
    (gdb) si
    0x0000000000400568 in foo ()
    1: /x $sp = 0xfffffffff390
    2: /x $x29 = 0xfffffffff3a0
    (gdb) info frame
    Stack level 0, frame at 0xfffffffff3a0:
    (gdb) si
    0x000000000040056c in foo ()
    1: /x $sp = 0xfffffffff390
    2: /x $x29 = 0xfffffffff3a0
    (gdb) info frame
    Stack level 0, frame at 0xfffffffff3a0:
    (gdb) si
    0x0000000000400570 in foo ()
    1: /x $sp = 0xfffffffff390
    2: /x $x29 = 0xfffffffff3a0
    (gdb) info frame
    Stack level 0, frame at 0xfffffffff3a0:
    (gdb) si
    0x0000000000400574 in foo ()
    1: /x $sp = 0xfffffffff3a0
    2: /x $x29 = 0xfffffffff3a0
    (gdb) info frame
    Stack level 0, frame at 0xfffffffff3b0:
     pc = 0x400574 in foo; saved pc = 0x40058c
    (gdb) si
    0x000000000040058c in main ()
    1: /x $sp = 0xfffffffff3a0
    2: /x $x29 = 0xfffffffff3a0
    ...
    
    The "frame at" bit lists 0xfffffffff3a0 except at the last insn, where it
    lists 0xfffffffff3b0.
    
    The frame address is calculated here in aarch64_make_prologue_cache_1:
    ...
      unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
      if (unwound_fp == 0)
        return;
    
      cache->prev_sp = unwound_fp + cache->framesize;
    ...
    
    For insns after the prologue, we have cache->framereg == sp and
    cache->framesize == 16, so unwound_fp + cache->framesize gives the wrong
    answer once sp has been restored to entry value by the before-last insn.
    
    Fix this by detecting the situation that the sp has been restored.
    
    This fixes PRs tdep/30010 and tdep/30011.
    
    This also fixes the aarch64 FAILs in gdb.reverse/solib-precsave.exp and
    gdb.reverse/solib-reverse.exp I reported in PR gdb/PR29721.
    
    Tested on aarch64-linux.
    PR tdep/30010
    PR tdep/30011
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30010
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30011
Comment 3 Tom de Vries 2023-01-23 15:53:48 UTC
Fixed by commit.