Author Topic: nasm with gdb  (Read 15851 times)

Offline dullhans

  • Jr. Member
  • *
  • Posts: 7
nasm with gdb
« on: June 03, 2013, 01:11:25 AM »
Hi,

I'm trying to debug a simple nasm program with gdb.

Code: [Select]
extern _printf
extern _exit

global _main
section .data
fl1 dd 3.54
fl2 dd 7.42
mstr db "%f", 0

section .bss
fl0 resd 1

section .text
_main:
nop

_start:
nop
fld dword [fl1]
fld dword [fl2]
fadd st0, st1
fst dword [fl0]

push dword [fl0]
push mstr
call _printf

push 0
call _exit

I assemble the programm to an elf object file and link it.
Then I use gdb, and try to set a breakingpoint at _start. But when I use the command:
break *_start
I always get
No symbol "_start" in current context.
And when I use:
break _start
I get the message
Function "_start" not defined.

So how am I supposed to set a breakingpoint at _start? I don't have any orientation otherwise.
« Last Edit: June 03, 2013, 01:16:10 AM by dullhans »

Offline TightCoderEx

  • Full Member
  • **
  • Posts: 103
Re: nasm with gdb
« Reply #1 on: June 03, 2013, 01:24:37 AM »
Change global _main -> global _start and remove _main: and the nop.

Offline dullhans

  • Jr. Member
  • *
  • Posts: 7
Re: nasm with gdb
« Reply #2 on: June 03, 2013, 01:31:00 AM »
Problem is, I can't even link the elf file if I replace _main with _start.

after
nasm -felf a.asm
gcc a.o


I get the message:

j:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../libmingw32.a(main.o): In function
 `main':
C:\MinGW\msys\1.0\src\mingwrt/../mingw/main.c:73: undefined reference to `WinMai
n@16'
collect2: ld returned 1 exit status

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: nasm with gdb
« Reply #3 on: June 03, 2013, 01:46:48 AM »
Adding "-F dwarf" to Nasm's command line may help. But... this doesn't look like ELF code to me (underscores on _main, _printf, etc.)

In any case, printf always expects a "float" to be double-precision. Either make f10 a qword and push [f10 + 4] then [f10], or
Code: [Select]
sub esp, 8
fst qword [esp]

Best,
Frank

In view of your last post, leave the underscores on, and use "-f win32" instead of "-f elf", I suspect..


Offline dullhans

  • Jr. Member
  • *
  • Posts: 7
Re: nasm with gdb
« Reply #4 on: June 03, 2013, 02:41:13 AM »
Adding "-F dwarf" to Nasm's command line may help. But... this doesn't look like ELF code to me (underscores on _main, _printf, etc.)
Strange that you're saying this, because for me underscores are really the only way to link an elf file with gcc.
Anyway, I tried the -F dwarf option, and now I'm either getting the error message:
No symbol table is loaded.  Use the "file" command.
or the same error messages as before.

When I use the win32 format, I also get the "No symbol table" message. *shrug*

However, if I use the same method as in my first post, I can set a breakpoint at __main with 2 underscores, break *__main. But I'm not sure if __main is the same entrypoint as _main.

In any case, printf always expects a "float" to be double-precision. Either make f10 a qword and push [f10 + 4] then [f10], or
Code: [Select]
sub esp, 8
fst qword [esp]
Your latter proposal didn't seem to change anything, but the f10 qword thing works like a charm. Thanks!
« Last Edit: June 03, 2013, 02:43:17 AM by dullhans »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: nasm with gdb
« Reply #5 on: June 03, 2013, 03:22:26 AM »
I'm not familiar with MinGW's gcc. Whatever works, I guess. Would adding "-g" to gcc's command line help at all?

Best,
Frank


Offline dullhans

  • Jr. Member
  • *
  • Posts: 7
Re: nasm with gdb
« Reply #6 on: June 03, 2013, 01:34:34 PM »
Unfortunately not. Dunno, perhaps I try another linker, or I just have to ask around at mingw sites.
BTW, what are you using for debugging? Perhaps that could work for me as well.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: nasm with gdb
« Reply #7 on: June 03, 2013, 09:02:21 PM »
http://home.myfairpoint.net/fbkotler/debug-0.0.21.tgz is what I use for a debugger. Written by Terry Loveall, his "Coughing up a Furball" page has apparently vanished, so I put it there. It is for Linux, and probably won't help you. For Windows, "Ollydbg" is popular, or "windbg". Because I find gdb "unfriendly", I don't use it much. Because I don't use it much, I don't know how to use it. Because I don't know how to use it, I find it "unfriendly". Viscious circle! Learn to use gdb, if you can...

I googled "mingw nasm" and found some information. Apparently, you're not the only one who finds that "-f elf" works. Apparently, I'm not the only one who thinks that it isn't really right, although it apparently works in some cases. I really think that "-f win32" is right. Apparently, the MinGW tools like ".o" as an extension, not ".obj" as Nasm defaults to. "-o myfile.o" should fix this.

Nasm's "-f win32" doesn't have a "debug information" format - adding "-g" is silently ignored. Still, there is apparently enough symbolic information in the executable for Ollydbg to work with. Adding "-g" to gcc's command line may cause gcc to pass "whatever it's got" to the executable (we want it in Linux, anyway).

One of the things I found was a tutorial right here on the Nasm Forum. It involves running the MinGW tools in Linux to "cross compile" to a Windows executable. Not what you want, but there might be a "clue" there(?).

Never give up, never surrender, never let 'em see you sweat! Or... it might be more practical to just use Ollydbg. :)

Best,
Frank


Offline dullhans

  • Jr. Member
  • *
  • Posts: 7
Re: nasm with gdb
« Reply #8 on: June 03, 2013, 10:55:10 PM »
lol, thank you very much Frank. I guess I'll use -f win32 in future.
I'll post here again if I find out what I've made wrong with gdb. Although, lazy as I am, I'll probably end up using Ollydbg.

Offline dullhans

  • Jr. Member
  • *
  • Posts: 7
Re: nasm with gdb
« Reply #9 on: June 04, 2013, 10:10:16 PM »
Well, "break start" still doesn't work, but I could set a breakpoint simply by using "break main" (without underscore)  ::)
That's sufficient for my needs. So that's a solution. Hope everybody here could benefit from my brilliance.
« Last Edit: June 04, 2013, 10:15:07 PM by dullhans »

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: nasm with gdb
« Reply #10 on: June 05, 2013, 01:05:53 AM »
Looking at the errors and your environment it would seem you are usig mingw to build Win32 executables.  However, your code is all 32-bit nasm so you can exclude using mingw and simply use an appropriate linker.  You are also suffering from Windows mangling function names for Win32 environments.  Allow me to suggest that before you deep dive into Windows assembly programming that you download and install the  NASMX package which contains the tools you need to build Win32/Win64 programs along with numerous demo programs where you're sure to find clues as to what you're trying to accomplish.

For debugging gdb is very good ( although somewhat archaic and complex but extremely useful in the hands of a master ).  Under Windows, as Frank stated, you can also use Windbg or Ollydbg.

Offline dullhans

  • Jr. Member
  • *
  • Posts: 7
Re: nasm with gdb
« Reply #11 on: June 05, 2013, 03:53:44 PM »
Nice stuff, Thank you very much! Perhaps I'll rather use Golink in future.