Looks like gdb_bfd_read_elf_soname fails to correctly fetch the soname of some libraries. For example, consider the following: $ cat main.c extern void foo(int); int main() { foo(13); return 0; } $ cat foo.c #include <unistd.h> void foo(int x); void foo1(int x); void foo2(int x); void foo3(int x); void foo(int x) { return foo1(x); } void foo1(int x) { return foo2(x); } void foo2(int x) { return foo3(x); } void foo3(int x) { sleep(1000); } $ gcc foo.c -o libfoo.so --shared -fPIC -g3 -O0 -Wl,-hlibfoo.so $ gcc main.c -g3 -O0 -lfoo -L . $ gdb ./a.out Reading symbols from ./a.out... (gdb) r Starting program: /home/pablogsal/github/binutils-gdb/lel/a.out [Thread debugging using libthread_db enabled] Using host libthread_db library "/usr/lib/libthread_db.so.1". ^C Program received signal SIGINT, Interrupt. 0x00007ffff7e792f7 in clock_nanosleep () from /usr/lib/libc.so.6 (gdb) gcore warning: Memory read failed for corefile section, 4096 bytes at 0xffffffffff600000. Saved corefile core.1263203 $ gdb --args --gdb -c core.1263203 -e ./a.out (gdb) b gdb_bfd_read_elf_soname (gdb) r ... Thread 1 "gdb" hit Breakpoint 1, gdb_bfd_read_elf_soname (filename=0x5555563acf50 "/home/pablogsal/github/binutils-gdb/lel/libfoo.so") at solib.c:1600 1600 { (gdb) finish ... Value returned is $1 = std::unique_ptr<char> = {get() = 0x55555631c340 "gisterTMCloneTable"} As you can see the returned soname is some garbage (gisterTMCloneTable) but the real soname is correctly set: $ readelf -d libfoo.so Dynamic section at offset 0x2df8 contains 25 entries: Tag Type Name/Value 0x0000000000000001 (NEEDED) Shared library: [libc.so.6] 0x000000000000000e (SONAME) Library soname: [libfoo.so] ...
This was done with gdb HEAD and GCC 13.2.1 202308
Thanks for the detailed bug report. I tried to reproduce this but it worked correctly for me: Thread 1 "gdb" hit Breakpoint 1, gdb_bfd_read_elf_soname (filename=0xea0680 "/home/amerey/test/solib/libfoo.so") at solib.c:1599 1599 gdb_bfd_ref_ptr abfd = gdb_bfd_open (filename, gnutarget); (gdb) finish [...] Value returned is $1 = std::unique_ptr<char> = { get() = 0xeaa860 "libfoo.so" } If you're able to upload your corefile where this fails I'll take a look at it.
Created attachment 15477 [details] core file + libfoo I am attaching the requested files to this ticket
Hi Pablo, I'm still not able to reproduce this bug with the binaries you provided. Valgrind did not detect any invalid reads or writes while gdb read the corefile that was uploaded. I'm not yet sure what could be causing the junk soname you're seeing.
Thanks a lot for checking! I am going to try to reproduce this myself in other systems to see if I can provide more insights here or provide a better way to reproduce. I have checked and this happens consistently in my system with gdb HEAD so maybe I can dig a bit into what is going on. I will also try running valgrind or asan over it