libbacktrace
libbacktrace copied to clipboard
No filename or line info for 32-bit PE/COFF with ASLR enabled
OS: Win10 Pro 20H2, 32-bit Compiler: gcc 11.2.0 (provided by MSYS2) libbacktrace version: master branch downloaded from Github on 2022-03-29 (also tested with the MSYS2 libbacktrace package, version r53.1da441c-3, built on 2022-01-16)
libbacktrace doesn't supply filename or line information for programs compiled on my computer, which I'm pretty sure is a result of it not supporting address space layout randomization for 32-bit PE/COFF. If I clear the DYNAMIC_BASE bit in the executable's COFF header, or if I link with "-Wl,--disable-dynamicbase" to prevent it from getting set in the first place, then libbacktrace works properly.
While debugging this, I tried modifying coff_add() in "pecoff.c" to have it supply an offset to backtrace_dwarf_add() via the base_address argument, to compensate for the ASLR relocation:
uintptr_t base_address = (uintptr_t)GetModuleHandle(NULL) - image_base;
if (!backtrace_dwarf_add (state, base_address, &dwarf_sections,
0, /* FIXME: is_bigendian */
NULL, /* altlink */
error_callback, data, fileline_fn,
NULL /* returned fileline_entry */))
goto fail;
This works when the actual base address (supplied by GetModuleHandle()) is greater than image_base, and it allows me to get debugging info even with ASLR enabled. It doesn't work when the actual base address is lower than image_base, though, since base_address is unsigned.
CC @gingold-adacore
I've created a patch series at https://gcc.gnu.org/pipermail/gcc-patches/2022-December/608031.html which solves this issue.