Bug 30012 - [gdb/tdep, aarch64] fp and sp register aliases ignored
Summary: [gdb/tdep, aarch64] fp and sp register aliases ignored
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 15:12 UTC by Tom de Vries
Modified: 2023-01-19 13:06 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 15:12:33 UTC
Consider 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 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
...

This looks interesting:
...
$ gdb -q -batch a.out \
    -ex "display /x \$sp" \
    -ex "display /x \$x29" \
    -ex "display /x \$fp" \
    -ex "b *foo" \
    -ex run \
    -ex stepi
Breakpoint 1 at 0x400564

Breakpoint 1, 0x0000000000400564 in foo ()
1: /x $sp = 0xfffffffff3a0
2: /x $x29 = 0xfffffffff3a0
3: /x $fp = 0xfffffffff3a0
0x0000000000400568 in foo ()
1: /x $sp = 0xfffffffff390
2: /x $x29 = 0xfffffffff3a0
3: /x $fp = 0xfffffffff390
...

The effect of "sub sp, sp, #0x10" on sp (the canonical name in gdb for x31) is as expected, it drops 0x10.

The effect on x29, the frame pointer register, is none, again as expected.

However, fp is supposed to be an alias for x29, according to:
...
static const struct
{
  const char *const name;
  int regnum;
} aarch64_register_aliases[] =
{
  /* 64-bit register names.  */
  {"fp", AARCH64_FP_REGNUM},
  {"lr", AARCH64_LR_REGNUM},
  {"sp", AARCH64_SP_REGNUM},
  /*  specials */
  {"ip0", AARCH64_X0_REGNUM + 16},
  {"ip1", AARCH64_X0_REGNUM + 17}
};
...

So we'd expect fp to be unaffected, but that's not the case.

The reason for this is this:
...
void
_initialize_frame_reg ()
{
  /* Frame based $fp, $pc, $sp and $ps.  These only come into play                            
     when the target does not define its own version of these                                 
     registers.  */
  user_reg_add_builtin ("fp", value_of_builtin_frame_fp_reg, NULL);
  user_reg_add_builtin ("pc", value_of_builtin_frame_pc_reg, NULL);
  user_reg_add_builtin ("sp", value_of_builtin_frame_sp_reg, NULL);
  user_reg_add_builtin ("ps", value_of_builtin_frame_ps_reg, NULL);
}
...

So, the fp register alias is simply ignored.

Note that the sp alias is likewise ignored, given that the canonical name for "x31" is "sp".

We probably should:
- error out when creating aliases with a name that's already taken.
- remove the sp and fp aliases
Comment 2 Sourceware Commits 2023-01-19 13:05:12 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=ea2f6fad7e104fb65da6a438bb0ec753210f1e90

commit ea2f6fad7e104fb65da6a438bb0ec753210f1e90
Author: Tom de Vries <tdevries@space.suse.cz>
Date:   Thu Jan 19 14:05:08 2023 +0100

    [gdb/tdep, aarch64] Remove fp and sp reg aliases, add x31 reg alias
    
    In aarch64-tdep.c we find these register aliases:
    ...
    {
      /* 64-bit register names.  */
      {"fp", AARCH64_FP_REGNUM},
      {"lr", AARCH64_LR_REGNUM},
      {"sp", AARCH64_SP_REGNUM},
    ...
    
    The sp alias is superfluous, because the canonical name of x31 is already sp.
    
    The fp alias is superfluous, because it's already taken by the default meaning
    of fp, assigned here in _initialize_frame_reg:
    ...
      user_reg_add_builtin ("fp", value_of_builtin_frame_fp_reg, NULL);
    ...
    
    Fix this by removing the fp and sp aliases.
    
    While we're at it, add an x31 alias for sp.
    
    Approved-By: Luis Machado <luis.machado@arm.com>
    
    Tested on aarch64-linux.
    PR tdep/30012
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30012
Comment 3 Tom de Vries 2023-01-19 13:06:05 UTC
Fixed.