https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104463
Bug ID: 104463 Summary: Split debug info not loaded from .debug/ if .gnu_debuglink points to binary itself Product: gcc Version: 11.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libbacktrace Assignee: unassigned at gcc dot gnu.org Reporter: matti.niemenmaa+gccbugs at iki dot fi CC: ian at gcc dot gnu.org Target Milestone: --- (Related to PR77631; it seems this issue has been there since the initial implementation.) Some example code triggering UBSan just to get a stack trace: $ cat bug.c #include <limits.h> int main(void) { int x = INT_MAX; return x + 1; } With normal (non-split) debug info, no problem: $ gcc -o bug bug.c -ggdb3 -fsanitize=undefined $ UBSAN_OPTIONS=print_stacktrace=1 ./bug bug.c:4:14: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int' #0 0x55d52e60e182 in main /path/to/bug.c:4 #1 0x7fccca3fb0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) #2 0x55d52e60e08d in _start (/path/to/bug+0x108d) With debug info split into a different filename in .debug, no problem: $ gcc -o bug bug.c -ggdb3 -fsanitize=undefined $ ( rm -rf .debug && mkdir .debug && \ cd .debug && \ objcopy --only-keep-debug ../bug bug.dbg && \ objcopy --strip-debug ../bug && \ objcopy --add-gnu-debuglink=bug.dbg ../bug ) $ UBSAN_OPTIONS=print_stacktrace=1 ./bug bug.c:4:14: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int' #0 0x55d52e60e182 in main /path/to/bug.c:4 #1 0x7fccca3fb0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) #2 0x55d52e60e08d in _start (/path/to/bug+0x108d) But when using the same filename, we lose the file/line number info for main: $ gcc -o bug bug.c -ggdb3 -fsanitize=undefined $ ( rm -rf .debug && mkdir .debug && \ cd .debug && \ objcopy --only-keep-debug ../bug bug && \ objcopy --strip-debug ../bug && \ objcopy --add-gnu-debuglink=bug ../bug ) $ UBSAN_OPTIONS=print_stacktrace=1 ./bug bug.c:4:14: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int' #0 0x55cbd7c4117f in main (/path/to/bug+0x117f) #1 0x7f863518c0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) #2 0x55cbd7c4108d in _start (/path/to/bug+0x108d) And e.g. strace can verify that it never even looks at the file in .debug/. gdb, on the other hand, does load the debug info correctly even in this case: $ gdb -q ./bug </dev/null Reading symbols from ./bug... Reading symbols from /path/to/.debug/bug... (gdb) quit In my "real world" case all shared libraries on the system are built like this, for example libubsan itself: $ ls -l /usr/lib/{.debug/,}libubsan* | cut -d/ -f2- usr/lib/.debug/libubsan.so.1.0.0 usr/lib/libubsan.so.1 -> libubsan.so.1.0.0 usr/lib/libubsan.so.1.0.0 $ readelf -p .gnu_debuglink /usr/lib/libubsan.so.1 String dump of section '.gnu_debuglink': [ 0] libubsan.so.1.0.0 [ 16] n I'm not sure what the most appropriate fix is, but perhaps one of the following would make sense: - if the .gnu_debuglink points to the file itself, ignore it and only look in .debug/ - if the .gnu_debuglink points to the file itself, prefer a .debug/ file to it - if the .gnu_debuglink pointee is successfully loaded but contains no debug info, continue searching in .debug/ I haven't checked what gdb itself does. $ gcc -v Using built-in specs. COLLECT_GCC=gcc-11 COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.1.0-1ubuntu1~20.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --disable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-2V7zgg/gcc-11-11.1.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-2V7zgg/gcc-11-11.1.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2 Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.1.0 (Ubuntu 11.1.0-1ubuntu1~20.04)