1

I coding assembly with Nasm, i want debug the program using gdb, but it not works when i put a breakpoint and run the program.

The program compile fine and link too, the problem is gdb.

Here is the commands to compile:

 nasm -f elf64 -F dwarf -g   types.asm
 nasm -f elf64 -F dwarf -g   functions.asm
 nasm -f elf64 -F dwarf -g   Hello.asm
 ld -g -o Hello Hello.o functions.o types.o

This is the file i want debug Hello.asm:

 %include "functions.asm"
 section .bss
     res: resb 1
     fout: resb 1

 section .text
    global _start:     ;must be declared for linker (ld)

 section .data
      msg db 'Hello, world!', 0xa  ;string to be printed
      len equ $ - msg     ;length of the string 
      filename db 'hello.txt'

  _start:               ;tells linker entry point
       mov ecx,5
       mov edx,4
       call sum
       mov [res],eax
       mov  edx,1     ;message length
       mov  ecx,res  ;message to write
       mov  ebx,1       ;file descriptor (stdout)
       mov  eax,4       ;system call number (sys_write)
       int  0x80        ;call kernel
       write_string msg,len
       create_file filename
       mov [fout],eax
       close_file [fout]


       call print_msg

       mov  eax,1       ;system call number (sys_exit)
       int  0x80       ;call kernel

  sum:
     mov eax,ecx
     add eax,edx
     add eax,'0'
     ret

Next i open gdb:

gdb Hello
(gdb) break _start
Function «_start» not defined
¿Compilación de breakpoint pendiente hasta futura cargada de biblioteca compartida? (y or [n]) y     
 Punto de interrupción 1 (_start) pendiente.
(gdb) run  
 Starting program: /asm/Hello 
 9Hello, world!
 Hello, world!from another file
 [Inferior 1 (process 5811) exited with code 01]
 (gdb) 
LordPaella
  • 143
  • 9
  • 1
    Well, do you **have** a `_start`? You have not shown your code. Does `nm Hello | grep _start` show it? – Jester Dec 12 '18 at 13:46
  • I have a _start, later i show you the code. – LordPaella Dec 12 '18 at 14:18
  • You probably left out `global _start`. Anyway, you can use `starti` in GDB to start the process and pause before the first instruction. – Peter Cordes Dec 12 '18 at 14:21
  • It is quite possible you put your code in the `.data` section.Ensure you have placed code in the `.text` section, otherwise you will not be able to set break points on labels in the code. – Michael Petch Dec 12 '18 at 17:14
  • I put the code. Check it. – LordPaella Dec 12 '18 at 17:27
  • `fout` is only 1 byte, but you're storing a dword. Also, don't use the 32-bit `int 0x80` ABI in 64-bit code. Either assemble/link as 32-bit, or write 64-bit code. [What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code?](https://stackoverflow.com/q/46087730) – Peter Cordes Dec 13 '18 at 04:14

1 Answers1

1

I solved it, i only change position section .data to section .text and the debugger works.I don't know why, but now the gdb take the .start.

LordPaella
  • 143
  • 9
  • 2
    See my earlier comment.under your question.I posted it without seeing the code. Placing code in the data section will not allow you to set break points on those labels. If you want to set breakpoints on running code you must ensure code is in the `.text` section (or another executable section). – Michael Petch Dec 12 '18 at 17:35