binutils-gdb/gdb/arch
Matthew Malcomson 807f647cac GDB: aarch64: Add ability to displaced step over a BR/BLR instruction
Enable displaced stepping over a BR/BLR instruction

Displaced stepping over an instruction executes a instruction in a
scratch area and then manually fixes up the PC address to leave
execution where it would have been if the instruction were in its
original location.

The BR instruction does not need modification in order to run correctly
at a different address, but the displaced step fixup method should not
manually adjust the PC since the BR instruction sets that value already.

The BLR instruction should also avoid such a fixup, but must also have
the link register modified to point to just after the original code
location rather than back to the scratch location.

This patch adds the above functionality.
We add this functionality by modifying aarch64_displaced_step_others
rather than by adding a new visitor method to aarch64_insn_visitor.
We choose this since it seems that visitor approach is designed
specifically for PC relative instructions (which must always be modified
when executed in a different location).

It seems that the BR and BLR instructions are more like the RET
instruction which is already handled specially in
aarch64_displaced_step_others.

This also means the gdbserver code to relocate an instruction when
creating a fast tracepoint does not need to be modified, since nothing
special is needed for the BR and BLR instructions there.

Regression tests showed nothing untoward on native aarch64 (though it
took a while for me to get the testcase to account for PIE).

------#####
Original observed (mis)behaviour before was that displaced stepping over
a BR or BLR instruction would not execute the function they called.
Most easily seen by putting a breakpoint with a condition on such an
instruction and a print statement in the functions they called.
When run with the breakpoint enabled the function is not called and
"numargs called" is not printed.
When run with the breakpoint disabled the function is called and the
message is printed.

--- GDB Session
~ [15:57:14] % gdb ../using-blr
Reading symbols from ../using-blr...done.
(gdb) disassemble blr_call_value
Dump of assembler code for function blr_call_value:
...
   0x0000000000400560 <+28>:    blr     x2
...
   0x00000000004005b8 <+116>:   ret
End of assembler dump.
(gdb) break *0x0000000000400560
Breakpoint 1 at 0x400560: file ../using-blr.c, line 22.
(gdb) condition 1 10 == 0
(gdb) run
Starting program: /home/matmal01/using-blr
[Inferior 1 (process 33279) exited with code 012]
(gdb) disable 1
(gdb) run
Starting program: /home/matmal01/using-blr
numargs called
[Inferior 1 (process 33289) exited with code 012]
(gdb)

Test program:
---- using-blr ----
\#include <stdio.h>
typedef int (foo) (int, int);
typedef void (bar) (int, int);
struct sls_testclass {
    foo *x;
    bar *y;
    int left;
    int right;
};

__attribute__ ((noinline))
int blr_call_value (struct sls_testclass x)
{
  int retval = x.x(x.left, x.right);
  if (retval % 10)
    return 100;
  return 9;
}

__attribute__ ((noinline))
int blr_call (struct sls_testclass x)
{
  x.y(x.left, x.right);
  if (x.left % 10)
    return 100;
  return 9;
}

int
numargs (__attribute__ ((unused)) int left, __attribute__ ((unused)) int right)
{
        printf("numargs called\n");
        return 10;
}

void
altfunc (__attribute__ ((unused)) int left, __attribute__ ((unused)) int right)
{
        printf("altfunc called\n");
}

int main(int argc, char **argv)
{
  struct sls_testclass x = { .x = numargs, .y = altfunc, .left = 1, .right = 2 };
  if (argc > 2)
  {
        blr_call (x);
  }
  else
        blr_call_value (x);
  return 10;
}
2021-01-27 17:12:25 +00:00
..
aarch32.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
aarch32.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
aarch64-insn.c gdb: convert aarch64 to new-style debug macros 2021-01-11 16:52:42 -05:00
aarch64-insn.h GDB: aarch64: Add ability to displaced step over a BR/BLR instruction 2021-01-27 17:12:25 +00:00
aarch64.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
aarch64.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
amd64.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
amd64.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
arc.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
arc.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
arm-get-next-pcs.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
arm-get-next-pcs.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
arm-linux.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
arm-linux.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
arm.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
arm.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
i386.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
i386.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
ppc-linux-common.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
ppc-linux-common.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
ppc-linux-tdesc.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
riscv.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
riscv.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
tic6x.c Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
tic6x.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00
xtensa.h Update copyright year range in all GDB files 2021-01-01 12:12:21 +04:00