Jon Flow Jon Flow - 17 days ago 5x
C Question

DWARF DI with generated C code?

I can't seem to find any good documentation for the DWARF DI format to answer my question. I'm writing a language that compiles to C, is there any way that I can produce DWARF Debug Information into the C code or is it assembly only?

Is it impossible to add debug information to some (generated) C code other than using primitive stuff that isn't as good like

and the


I guess you'll compile the generated C with debug info (-g), so what you're asking is how to have additional DWARF records that describe your high level language. Conceivable approaches:

(1) edit the object file to augment the DWARF records for the C code. I don't know of an existing tool, perhaps doable with libdwarf or pyelftools.

(2) find a way to "smuggle" extra DWARF records through the compiler, e.g. somewhat as linker commands can be given through #pragmas. However I don't know of a compiler that supports this.

(3) LLVM has support for debug records in the IR representation . You could use clang to compile your generated C to IR (clang foo.c -S -emit-llvm -target arm -o foo.ll), then augment foo.ll, then generate the .o (llc foo.ll), ready for linking.

The third option seems likely to be the easiest.