Bug 24810 - add-symbol-file for shared lib has no effect
Summary: add-symbol-file for shared lib has no effect
Status: NEW
Alias: None
Product: gdb
Classification: Unclassified
Component: symtab (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-07-15 16:08 UTC by Tom de Vries
Modified: 2019-07-15 16:08 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tom de Vries 2019-07-15 16:08:20 UTC
I.

Consider test sources main.c:
...
#include <stddef.h>
extern void foo (int *p);
int
main ()
{
  foo (NULL);
  return 0;
}
...

and foo.c:
...
void
foo (int *p)
{
  *p = 1;
}
...


II.

If we compile it to one executable a.out:
...
$ gcc -g foo.c main.c
...

and create an executable b.out without debug info, and c.out without debuginfo but with debuginfo linked using .gnu_debuglink:
...
$ objcopy --only-keep-debug a.out a.debug
$ objcopy --strip-debug a.out b.out
$ objcopy --add-gnu-debuglink=a.debug b.out c.out
...

then when running the exec without debuginfo, we can load the symbol-file with the linked debuginfo, and get a better backtrace:
...
$ gdb -batch b.out -ex r -ex bt -ex 'symbol-file c.out' -ex bt

Program received signal SIGSEGV, Segmentation fault.
0x00000000004004a3 in foo ()
#0  0x00000000004004a3 in foo ()
#1  0x00000000004004ba in main ()
#0  0x00000000004004a3 in foo (p=0x0) at foo.c:4
#1  0x00000000004004ba in main () at main.c:6
...


III.

Now using the same sources, let's build an executable and a shared library:
...
$ pwd=$(pwd -P)
$ gcc -c -fPIC foo.c -g
$ gcc -shared -o libfoo.so foo.o -g
$ gcc main.c -lfoo -L$pwd -Wl,-rpath=$pwd -g
...

and create a libfoo without debug info:
...
$ cp libfoo.so libfoo.orig.so
$ objcopy --only-keep-debug libfoo.so libfoo.so.debug
$ objcopy --strip-debug libfoo.so libfoo.so.stripped
$ objcopy --add-gnu-debuglink=libfoo.so.debug libfoo.so.stripped libfoo.so.with-gnu-debuglink
...

Now, when when running with the shared lib without debuginfo, we can load the symbol-file with the linked debuginfo, but we do not get a better backtrace:
...
$ cp libfoo.so.stripped libfoo.so
$ ./gdb -batch a.out -ex r -ex bt -ex 'add-symbol-file libfoo.so.with-gnu-debuglink' -ex bt

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7bd5596 in foo () from /data/gdb_versions/devel/libfoo.so
#0  0x00007ffff7bd5596 in foo () from /data/gdb_versions/devel/libfoo.so
#1  0x0000000000400585 in main () at main.c:6
add symbol table from file "libfoo.so.with-gnu-debuglink"
#0  0x00007ffff7bd5596 in foo () from /data/gdb_versions/devel/libfoo.so
#1  0x0000000000400585 in main () at main.c:6
...

The same if we use libfoo.orig.so as symbol file.

The same if we run symbol-file a.out before add-symbol-file.