gimli-rs / gimli

A library for reading and writing the DWARF debugging format
https://docs.rs/gimli/
Apache License 2.0
849 stars 108 forks source link

Simple write example #746

Closed Cr0a3 closed 2 months ago

Cr0a3 commented 2 months ago

Hi, I tried the simple write example: https://github.com/gimli-rs/gimli/blob/master/crates/examples/src/bin/simple_write.rs I ran it, got the object file. Then when i tried to compile it into an executable i got following errors:

PS C:\Users\toni-\Desktop\gimli_test> gcc hello.o 
hello.o:hello.c:(.debug_line+0x22): relocation truncated to fit: IMAGE_REL_AMD64_ADDR32 against `.debug_line_str'
hello.o:hello.c:(.debug_line+0x2c): relocation truncated to fit: IMAGE_REL_AMD64_ADDR32 against `.debug_line_str'
hello.o:hello.c:(.debug_line+0x31): relocation truncated to fit: IMAGE_REL_AMD64_ADDR32 against `.debug_line_str'
hello.o:hello.c:(.debug_info+0x8): relocation truncated to fit: IMAGE_REL_AMD64_ADDR32 against `.debug_abbrev'
hello.o:hello.c:(.debug_info+0x31): relocation truncated to fit: IMAGE_REL_AMD64_ADDR32 against `.debug_rnglists'
hello.o:hello.c:(.debug_info+0x35): relocation truncated to fit: IMAGE_REL_AMD64_ADDR32 against `.debug_line'
collect2.exe: error: ld returned 1 exit status

So i gone ahed and changched the format from Dwarf32 to Dwarf64. This time the linking actually worked. But then as i gone to test it in gdb. I got following error:

Dwarf Error: bad offset (0x140010000) in compilation unit header (offset 0x0 + 6) [in module C:\Users\toni-\Desktop\gimli_test\a.exe]

My gcc is:

gcc.exe (MinGW-W64 x86_64-msvcrt-posix-seh) 14.1.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Bye

Cr0a3 commented 2 months ago

The error is only under windows (i tested it under linux and it worked fine)

philipc commented 2 months ago

It's using the wrong relocation kind. I never tested it under windows. It needs to be RelocationKind::SectionOffset for references to other DWARF sections (so for RelocationTarget::Section the resulting relocation type is IMAGE_REL_AMD64_SECREL). You still need to use IMAGE_REL_AMD64_ADDR64 for RelocationTarget::Symbol.

Changing to Dwarf64 is the wrong way to try to fix this. That is only needed when the DWARF sections are large. It has nothing to do with address size.

In general, when something like this doesn't work, the easiest way to figure out why it should be doing is to look at what gcc or clang generate.

Cr0a3 commented 2 months ago

Thank you that I fixed it.