Build all-target gdb on ARM

Trying to build the latest GNU gdb on Scaleway ARM instance.

Build command:

apt install bison flex
configure --with-python --without-ncurses --enable-targets=all --prefix=/opt/gdb
make -j4

Neither the version 7.12 nor 8.1 (or even the git sources [1]) build out-of-the-box, while there are no issues on x86_64:

../../opcodes/libopcodes.a(sh64-dis.o): In function `sh64_get_contents_type_disasm':
/root/gdb/build-7.12/opcodes/../../gdb-7.12/opcodes/sh64-dis.c:462: undefined reference to `sh64_get_contents_type'
/root/gdb/build-7.12/opcodes/../../gdb-7.12/opcodes/sh64-dis.c:476: undefined reference to `sh64_get_contents_type'

According to the configure file:

--enable-64-bit-bfd     64-bit support (on hosts with narrower word sizes)

This flag will force building the BFD library with 64-bit support [2]. It seems that the right object file is not included for sh64 targets otherwise.

Setup for remote debug

Let's remote debug x86_64 local netcat (64 bits) using the remote arm scaleway instance (32 bits).

Install netcat-openbsd with the corresponding symbols. To make debug symbols available on Ubuntu (-dbgsym packages), see [3]:

sudo apt install netcat-openbsd netcat-openbsd-dbgsym

Start netcat in listening mode on a random port:

netcat -l -p 6666

Attach the debugger to the running process:

sudo gdbserver :3333 --attach `pidof netcat`

Create a reversed ssh tunnel to make the port of gdbserver locally available on the remote Scaleway ARM instance:

ssh -R 3333:localhost:3333 -i my-scaleway-key root@<address-of-the-instance>

Debug with gdb

Since gdb was build on ARM, its default target needs to be changed to x86_64 [4]:

(gdb) set architecture i386:x86-64
The target architecture is assumed to be i386:x86-64

Attach to gdbserver:

(gdb) target remote :3333
Remote debugging using :3333
Reading /bin/nc.openbsd from remote target...
[...]
0x00007f692555d0a0 in accept () from target:/lib/x86_64-linux-gnu/libc.so.6

Load the symbols manually:

(gdb) file target:/usr/lib/debug/bin//nc.openbsd
/usr/lib/debug/bin//nc.openbsd from remote target...
Reading /usr/lib/debug/bin//nc.openbsd from remote target...
Reading symbols from target:/usr/lib/debug/bin//nc.openbsd...done.

Backtrace:

(gdb) bt
#0  0x00007f692555d0a0 in accept () from target:/lib/x86_64-linux-gnu/libc.so.6
#1  0x00000000004022ae in main (argc=<optimized out>, argv=<optimized out>) at netcat.c:509

The symbols make it possible to match a pointer with the source file and line number (see netcat.c:509).

Execution can be resumed with continue:

(gdb) c
Continuing.
^C
Program received signal SIGINT, Interrupt.
0x00007f692555d0a0 in accept () from target:/lib/x86_64-linux-gnu/libc.so.6

And interrupted with Ctrl+C.

netcat is waiting at the same place again:

(gdb) bt
#0  0x00007f692555d0a0 in accept () from target:/lib/x86_64-linux-gnu/libc.so.6
#1  0x00000000004022ae in main (argc=<optimized out>, argv=<optimized out>) at netcat.c:509

Detach from gdbserver and let netcat run:

(gdb) detach
Detaching from program: target:/usr/lib/debug/bin//nc.openbsd, process 26125
Ending remote debugging.