Adding debug info to a raw binary

I’m interested in getting some advice on how I might best implement creating an ELF file with dwarf debug info from a raw binary file. I don’t have the source code but do have the linker map file. Basically I want to reconstruct the binary using the info in the map file and corresponding C header files.

I’ve created a dummy C project that includes libraries used in the map file. I want to parse these header files and insert dwarf info into the binary.

Is there a way to create dwarf info, by parsing clang AST, without needing to compile?

Would the yaml2obj program be useful here if I made a script to convert the map file to yaml?

Thanks,
John

I can think of a few ways to approach this problem.

Clang traverses the AST to produce LLVM IR metadata, and LLVM traverses the metadata to produce DWARF; basically, if you want DWARF with minimum effort, you are compiling the C headers. You could then do object-file editing to paste the DWARF into your ELF file. Getting the object-file addresses right might be tricky, though, as you’re (IIUC) trying to extract those from the linker map.

If you are willing to write your own tooling, you could probably write your own code to traverse the AST and generate DWARF directly, bypassing the IR step. This tool could also parse the linker map to extract function addresses and fill those in directly.

You could also try something trickier, taking advantage of DWARF v5 features to break the problem into two parts. The first part is to compile the C headers as in the first paragraph above, but generate split DWARF. This would create a foo.dwo file containing most of the DWARF, and all the address references would be made through the .debug_addr section. Then, using the link map, you would construct a .debug_addr section to insert into the ELF file. You would need to know what function each .debug_addr entry referred to, but that shouldn’t be super hard to figure out. You might be able to use yaml2obj for constructing the .debug_addr section, although yaml2obj doesn’t have super great support for DWARF in general.

1 Like