Expected behavior of gdb.lookup_symbol()

Gulshan Singh via Gdb <[email protected]>
Newsgroups gmane.comp.gdb.devel
Message-ID <CANEZYrdEQ-9qfM8R=LNMPxqXByA5uf6FWkCy-1kTKvAnhWc_TQ@mail.gmail.com>
Hi,

I've noticed that the `gdb.lookup_symbol()` function in the Python API
returns None for certain types of symbols. For example:

$ gdb -q -nx -ex 'catch load libc' -ex 'run' /bin/ls
Reading symbols from /bin/ls...
(No debugging symbols found in /bin/ls)
Catchpoint 1 (load)
Starting program: /usr/bin/ls
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Catchpoint 1
  Inferior loaded /lib/x86_64-linux-gnu/libselinux.so.1
    /lib/x86_64-linux-gnu/libc.so.6
    /lib/x86_64-linux-gnu/libpcre2-8.so.0
__GI__dl_debug_state () at ./elf/dl-debug.c:116
116     ./elf/dl-debug.c: No such file or directory.
(gdb) p __libc_malloc
$1 = {void *(size_t)} 0x7ffff7df3120 <__GI___libc_malloc>
(gdb) pi gdb.lookup_symbol('__libc_malloc')
(None, False)
(gdb) pi gdb.lookup_symbol('__GI___libc_malloc')
(<gdb.Symbol object at 0x7f896853e760>, False)

Here I was expecting both calls to `gdb.lookup_symbol()` to return a
`gdb.Symbol` object. I have debugging symbols installed. I determined the
name of the file containing the debug symbols like this:

(gdb) pi gdb.lookup_symbol('__GI___libc_malloc')[0].symtab.objfile
<gdb.Objfile
filename=/usr/lib/debug/.build-id/69/389d485a9793dbe873f0ea2c93e02efaa9aa3d.debug>

And I looked for those symbols in that file like this:

$ readelf -a
/usr/lib/debug/.build-id/69/389d485a9793dbe873f0ea2c93e02efaa9aa3d.debug |
grep __libc_malloc
  5933: 00000000000a5120   828 FUNC    LOCAL  DEFAULT   15
__GI___libc_malloc
  9639: 00000000000a5120   828 FUNC    GLOBAL DEFAULT   15 __libc_malloc

It seems that the only difference is one is a local binding and one is a
global binding. Is this expected behavior? If so, why? And is there another
way to get the address of a symbol like this, or should I just resort to
calling `info address` with `gdb.execute()`?

My GDB version is GNU gdb (Ubuntu 12.0.90-0ubuntu1) 12.0.90, and I'm on
Ubuntu 22.04.1 LTS.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.