1

I have a simple Hello World program for Windows in pure x86 assembly code that I have compiled and linked with nasm and ld. The problem I am running into is that I can't get DWARF debugging to work. I am using gdb from Mingw64 (i686-posix-dwarf-rev1). This same problem happens if I use gcc to link instead of ld. But, the program builds fine, and if I use STABS debugging, then everything is fine and dandy.

EDIT: Oops, I completely forgot to give the error that gdb shows.

...Dwarf Error: bad offset (0x407000) in compilation unit header (offset 0x0 
+ 6) [in module C:\Projects\AsmProjects\HelloWorldWin32\bin\x86\hello32.exe]
(no debugging symbols found)...done

The versions of each program are:

gdb 7.10.1  
nasm 2.12.02  
ld 2.25  
gcc 6.2.0  

These are the flags I'm sending to nasm: -f elf32 -Fdwarf -g

These are the flags for gcc link: -o $(BDIR)/x86/$@.exe $^ -L$(Mingw64-x86libs) -lkernel32 -luser32

And these are from ld link:

-mi386pe -o $(BDIR)/x86/$@.exe $^ -L$(Mingw64-x86libs) -lkernel32 -luser32  

I have a pretty big makefile, so I'm trying to give the least information that is absolutely neccessary.

Here is the source code for the program:

global  _main
extern  _GetStdHandle@4
extern  _WriteFile@20
extern  _ExitProcess@4

section .text
_main:
    push    ebp
    mov     ebp,esp

    ; GetstdHandle( STD_OUTPUT_HANDLE)
    push    -11
    call    _GetStdHandle@4
    mov     ebx, eax    

    ; WriteFile( hstdOut, message, length(message), &bytes, 0);
    push    0
    push    esp
    push    message_end
    push    message
    push    ebx
    call    _WriteFile@20

    ; ExitProcess(0)
    push    0
    call    _ExitProcess@4

section .data
message         db      'Hello, World',10
message_end     equ     $ - message
SeanRamey
  • 665
  • 7
  • 19
  • STABS and DWARF debug symbols have a broad overlap. For basic debugging, either should work fine. Why do you want to use DWARF debugging? – Matthew Fisher Sep 15 '16 at 12:59
  • I don't care particularly which one I use, but I do want to know why DWARF doesn't work, when, as far as I can tell, it should work just fine. – SeanRamey Sep 15 '16 at 20:24
  • Can you run dwarfdump on the executable? – Matthew Fisher Sep 15 '16 at 20:31
  • Does dwarfdump come with Mingw64? I'm googleing it, and I can't really find anything. So far I've seen that I can get the sources, but good luck building it on Windows. – SeanRamey Sep 15 '16 at 20:49
  • My experience is that DWARF info is not generated correctly by nasm. `readelf` complains when you try to read the DWARF sections. – BeeOnRope Oct 29 '17 at 10:02

1 Answers1

2

This is not a proper answer but was too long for the comment section.

I compiled on Ubuntu and then ran dwarfdump

It gave an error that may be related to the offset error.

dwarfdump ERROR:  dwarf_get_globals:  DW_DLE_PUBNAMES_VERSION_ERROR (123)

From a similar error on LLVM, I conclude that the dwarf version information is possibly corrupt or unsupported.

This post indicates that the dwarf information is sensitive to the proper section names. The example appears to have the section names right however.

Have you tried a 64-bit version? Perhaps a clue will appear.

This program appears to work fine Ubuntu. Can you try it on Mingw64?

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

_start:                                         ;tell linker entry point

    mov     edx,len                             ;message length
    mov     ecx,msg                             ;message to write
    mov     ebx,1                               ;file descriptor (stdout)
    mov     eax,4                               ;system call number (sys_write)
    int     0x80                                ;call kernel

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

section     .data

msg     db  'Hello, world!',0xa                 ;our dear string
len     equ $ - msg                             ;length of our dear string
Community
  • 1
  • 1
Matthew Fisher
  • 2,258
  • 2
  • 14
  • 23
  • No, it won't work because the code is not compatible with Windows. Well, I'll just go ahead and try it though because why not. Also, I have tried this with an x64 version as well, and it does the same thing as the x86 version. – SeanRamey Sep 15 '16 at 21:12
  • Surprisingly, it does build without error, but it has the exact same issue as my Windows version of the code, and when I run it, it crashes with a Segmentation Fault. – SeanRamey Sep 15 '16 at 21:18
  • Best guess is that particular version of nasm has a bug with the dwarf encoding. The example worked fine on Ubuntu under gdb. – Matthew Fisher Sep 15 '16 at 21:21
  • Well, I did go try a couple older versions of nasm, and it still gives the same error. – SeanRamey Sep 15 '16 at 21:23
  • Can you write any program that actually works with the -Fdwarf? I would think the dwarf creation would all be bundled inside nasm but maybe it has a library dependency – Matthew Fisher Sep 15 '16 at 21:28
  • Do you, or anybody have a copy of Windows that you could try my project build with? I can send you all the files. The size of all files is about 67Kb. – SeanRamey Sep 15 '16 at 21:29
  • Well, my program runs just fine, and everything goes as expected. The only problem is when trying to use DWARF debugging. Note I can still run the program with gdb with DWARF but debugging doesn't work. – SeanRamey Sep 15 '16 at 21:30
  • Sorry, I run OS X and Linux. – Matthew Fisher Sep 15 '16 at 21:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123483/discussion-between-matthew-fisher-and-seanramey). – Matthew Fisher Sep 15 '16 at 21:31
  • Upvoted because this is a helpful answer at the very least. – SeanRamey Sep 15 '16 at 23:58