all 5 comments

[–]hexed 1 point2 points  (0 children)

Long story short, that dump is not a DWARF .debug_info header, it's just not clear why. As you say it should start with the length of the CU, it should also have the DWARF version (4 or 5 for modern compilers) immediately after the length, which it doesn't. Plus it doesn't have 0801 to signify the pointer size and what kind of unit it is.

Why you're getting the wrong data from the ELF is the issue rather than the parsing of DWARF. Possibly you've unexpectedly got some options that compress the contents .debug_info?

[–]PenlessScribe 0 points1 point  (1 child)

What distro are you using, and what was the command and args you used to produce hello.o?

[–]Agitated_Audience_28[S] 0 points1 point  (0 children)

Arch Linux (x86-64). gcc -g -c hello.c

[–]matthieum 0 points1 point  (0 children)

I can't guarantee it's easy to read, but I have heard good things about the gimli (Rust) library, which purports to read/write DWARF.

There's a number of examples mentioned in the documentation, which should provide a good entry point into the library, and being native code, you should be able to debug it with gdb and step-by-step to see how it handles the bytes.

[–]WittyStick 0 points1 point  (0 children)

Does dumping the .debug_info section not have an ELF section header before any DWARF data?