Debugging Assembly code for Arm64 with GDB

I'm starting to learn assembly for ARMV8(Aarch64). I've got a Khadas Vim 3 board with a Amlogic Cortex A73 processor. I wanted to learn how to use GDB (the Gnu Debugger) tonight so I grabbed a bit of code that wouldn't compile and got into it.

This is from chapter 26 of Thinkingeek's tutorial.

// ch26.s

.data

one_var : .word 42
another_var : .word 66

.globl result_var             /* mark result_var as global */
result_var : .word 0

.text

.globl main
main:
    ldr x0, addr_one_var      /* r0 ← &one_var */
    ldr x0, [x0]              /* r0 ← *r0 */
    ldr x1, addr_another_var  /* r1 ← &another_var */
    ldr x1, [x1]              /* r1 ← *r1 */
    add x0, x0, x1            /* r0 ← r0 + r1 */
    ldr x1, addr_result       /* r1 ← &result */
    str x0, [x1]              /* *r1 ← r0 */
    bl result_var             /* call to inc_result */
    mov x0, #0                /* r0 ← 0 */
    ret                     /* return */
   

addr_one_var  : .word one_var
addr_another_var  : .word another_var
addr_result  : .word result_var	

Now to assemble it

$ aarch64-linux-gnu-as -c -g -o ch26.o ch26.s

and then link it

$ aarch64-linux-gnu-gcc -static -g -o ch26 ch26.o

To run it

$ ./ch26
Segmentation Fault

Oh. Here we go.

Let's try the GNU Debugger, GDB

$ gdb ch26
GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-git
Copyright (C) 2024 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "aarch64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
.
Find the GDB manual and other documentation resources online at:
    .

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ch26...
(gdb) run
Starting program: /home/khadas/Desktop/ch26 

This GDB supports auto-downloading debuginfo from the following URLs:
  
Enable debuginfod for this session? (y or [n]) y
Debuginfod has been enabled.
To make this setting permanent, add 'set debuginfod enabled on' to .gdbinit.
Downloading separate debug info for system-supplied DSO at 0x7ff7fff000
                                                                                
Program received signal SIGSEGV, Segmentation fault.
main () at ch26.s:15
15	    ldr x0, [x0]              /* r0 ← *r0 */
(gdb) 

Ok! First debug. It doesn't like line 15. Commented it out for now along with line 17. Ran again. it doesn't like str. Changed that line to:

mov x0, x1              /* *r1 ← r0 */

And it passes. Now it doesn't like the way I modified the inc_result line, but look, there is data there.

Program received signal SIGSEGV, Segmentation fault.
0x0000000000490048 in result_var ()

I changed that line to addr_result

Program received signal SIGILL, Illegal instruction.
0x00000000004007cc in addr_result () at ch26.s:23
23	    ret                     /* return */

Well it still doesn't work. Maybe inc_result was the right thing to put but it wasn't defined in this snippet. I should have read the whole page first before doing this. It turns out there is another source file that gets linked and it was an example of relocations. Still it was a great way for me to learn debugging Arm6 to Arm8 code.

Check out some other Bands on Bandcamp.com. Crazy Fingers (Vancouver 1991), Altogether Steve and the Mercenaries, and Hammy Ham Hands.

Proudly powered by an IDE, a Text Editor, and some Internet Searches.

2024 dispelled.ca end of file.