Bug 31634 - gdb doesn't correctly fetch the soname of shared libraries
Summary: gdb doesn't correctly fetch the soname of shared libraries
Status: UNCONFIRMED
Alias: None
Product: gdb
Classification: Unclassified
Component: corefiles (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-04-12 11:38 UTC by Pablo Galindo Salgado
Modified: 2024-04-26 14:39 UTC (History)
2 users (show)

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


Attachments
core file + libfoo (190.23 KB, application/zip)
2024-04-22 11:20 UTC, Pablo Galindo Salgado
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Pablo Galindo Salgado 2024-04-12 11:38:10 UTC
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]
 ...
Comment 1 Pablo Galindo Salgado 2024-04-12 11:39:08 UTC
This was done with gdb HEAD and GCC 13.2.1 202308
Comment 2 Aaron Merey 2024-04-19 19:53:19 UTC
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.
Comment 3 Pablo Galindo Salgado 2024-04-22 11:20:42 UTC
Created attachment 15477 [details]
core file + libfoo

I am attaching the requested files to this ticket
Comment 4 Aaron Merey 2024-04-25 23:25:19 UTC
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.
Comment 5 Pablo Galindo Salgado 2024-04-26 00:27:37 UTC
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