0

I am trying to learn the x86 assembly language, and would like to set up a little environment to do it efficiently. For that I have written some simple code (factorial, fibonnaci,etc...), added a main.c for testing and a Makefile to compile.

But I was never able to debug my code with gdb, no matter what flags I have tried...

Here is the code of facto.asm

            global  fact
            section .text
    fact:
            push    rbx
            mov     rcx, rdi
            xor     rax, rax
            xor     rbx, rbx
            inc     rax
            inc     rbx
    loop:
            mov     rdx, rbx
            mul     rdx
            inc     rbx
            dec     rcx
            jnz     loop
            pop     rbx
            ret

The Makefile:


EXE := facto
BIN := $(EXE).o
ASM := $(EXE).asm
SRC := main

all: $(EXE)

$(BIN): $(ASM)
    nasm -f elf64 -gdwarf $(ASM) -o $(BIN)

$(EXE): $(SRC).c $(BIN)
    gcc -no-pie -g  $(SRC).c $(BIN) -o $(EXE)

.PHONY: clean
clean:
    $(RM) *.o
    $(RM) $(EXE)

And finally the main.c in which I am doing the tests:

#include <stdio.h>
extern int fact();

int main(void)
{
    size_t res = fact(5);
    printf("%zu\n",res);
}

When running GDB and trying to "skip" into the assembler code, it just does not get in there and goes directly to the printf, even though I have set the -g flag for gcc and -gdwarf for nasm. Furthermore I am manually setting a break on that line with b.

What is it that I am missing? I would be glad for any clues!

If this can help by any means,I am currently working on an arch linux machine.

Thanks in advance!

Vladouch
  • 165
  • 1
  • 7
  • 1
    Answers to [this](https://stackoverflow.com/questions/72694342/gdb-does-not-load-source-lines-from-nasm) question may be the explaination. You still can switch to `layout asm` and step by instructions in disassembled code. – dimich Nov 22 '22 at 06:09
  • I have effectively followed the discussion of the link you gave, and it seems that it is a bug! It might be fixed in the 2.16 version, I guess until than I will have to find a different way to debug my code. Thank you! – Vladouch Nov 22 '22 at 11:00
  • You should be able to "step into" all code, even if no symbols are loaded or the source file is not available. Please clarify which is the exact issue: missing symbols/source or not stepping through the machine code? You might want to [edit] your question and add a log of your GDB session. (Yes, you can copy and paste from the shell.) – the busybee Nov 22 '22 at 11:41

0 Answers0