Search code examples
assemblyx86gdbnasm

Can't open file when debugging x86 NASM program with GDB


Here is a simplified example of reading file from my code:

section .data
    filename db 'txt.txt', 0x00

section .bss
    fd resd 0x01
    buff resb 0x100

section .text
    global _start

_start:
    ; open file
    mov eax, 0x05     ; SYS_OPEN
    mov ebx, filename
    xor ecx, ecx
    mov edx, 0o400    ; r--------
    int 0x80

    ; check if file was opened successfully
    cmp eax, 0x00
    js _exit
    
    mov [fd], eax    ; copy file descriptor

    ; read file
    mov eax, 0x03    ; SYS_READ
    mov ebx, [fd]
    mov ecx, buff
    mov edx, 0x100
    int 0x80

    ; close file
    mov eax, 0x06    ; SYS_CLOSE
    mov ebx, [fd]
    int 0x80

_exit:
    mov eax, 0x01    ; SYS_EXIT
    mov ebx, 0x00
    int 0x80

When I compile and run the program, the file is read successfully, but when I start the debugging process through GDB, after opening the file there is a negative number in the eax register, not a file descriptor, which indicates an error


Solution

  • Еhe problem was that it was necessary to specify the full path to the file to open when running GDB e.g. ${fileDirname}/<filename>