Bug 22501 - Incorrect sizeof(symbol) == 4 on a 64-bit platform
Summary: Incorrect sizeof(symbol) == 4 on a 64-bit platform
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: gdb (show other bugs)
Version: 7.12
: P2 normal
Target Milestone: 8.1
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-11-27 11:27 UTC by Philip Withnall
Modified: 2024-01-03 20:01 UTC (History)
2 users (show)

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 Philip Withnall 2017-11-27 11:27:13 UTC
If I compile GLib normally on a 64-bit platform, without CFLAGS=-g, run a test program, and `print sizeof(__glib_assert_msg)`, gdb (incorrectly) tells me it’s 4 bytes: However, it’s a char*, and its size according to objdump is (correctly) 8 bytes:

(gdb) print sizeof(__glib_assert_msg)
$1 = 4
(gdb) print sizeof(char*)
$2 = 8

objdump -t ./glib/.libs/libglib-2.0.so.0.5400.2 | grep assert_msg
000000000032c3e0 g     O .bss    0000000000000008              __glib_assert_msg

No debug information is being loaded while running gdb:

(gdb) info sharedlibrary
From                To                  Syms Read   Shared Object Library
0x00007ffff7dd9aa0  0x00007ffff7df51b0  Yes         /lib64/ld-linux-x86-64.so.2
0x00007ffff7ac6e90  0x00007ffff7b5ae90  Yes (*)     /tmp/build-snapshot.Ry9JWQsWNS/s/glib/.libs/libglib-2.0.so.0
0x00007ffff772c910  0x00007ffff7856423  Yes         /lib/x86_64-linux-gnu/libc.so.6
0x00007ffff749b5d0  0x00007ffff74edab1  Yes (*)     /lib/x86_64-linux-gnu/libpcre.so.3
0x00007ffff7282ab0  0x00007ffff728f811  Yes         /lib/x86_64-linux-gnu/libpthread.so.0
(*): Shared library is missing debugging information.

I can provide other information if needed.

---

This looks similar to bug #12281. The downstream GLib bug which triggered this is https://bugzilla.gnome.org/show_bug.cgi?id=782057.
Comment 1 Pedro Alves 2017-11-27 11:45:14 UTC
The problem is that without debug info (-g), GDB can't know that "__glib_assert_msg" is a "char *".  And then, in all released versions, 
if GDB doesn't know the type of the object, it assumes "int", 
which has sizeof 4.

This assumption was recently removed in GDB master (soon-to-be gdb 8.1), exactly because it's very confusing:
  https://sourceware.org/ml/gdb-patches/2017-07/msg00137.html
specifically:
  https://sourceware.org/ml/gdb-patches/2017-07/msg00150.html
Comment 2 Philip Withnall 2017-11-27 12:00:40 UTC
(In reply to Pedro Alves from comment #1)
> The problem is that without debug info (-g), GDB can't know that
> "__glib_assert_msg" is a "char *".  And then, in all released versions, 
> if GDB doesn't know the type of the object, it assumes "int", 
> which has sizeof 4.
> 
> This assumption was recently removed in GDB master (soon-to-be gdb 8.1),
> exactly because it's very confusing:
>   https://sourceware.org/ml/gdb-patches/2017-07/msg00137.html
> specifically:
>   https://sourceware.org/ml/gdb-patches/2017-07/msg00150.html

I see, that explains things. I look forward to your patch series landing. Is there a bug which this one should be duped to?
Comment 3 Pedro Alves 2017-11-27 12:06:13 UTC
(In reply to Philip Withnall from comment #2)

> I see, that explains things. I look forward to your patch series landing. 

It already landed a while ago.  If you try out current master, you'll get an 
error instead of "= 4".

> Is there a bug which this one should be duped to?

There probably is, though I haven't looked.  This is a very frequent source of confusion.

For sizeof in particular, I'm wondering whether it makes sense (or rather if it could ever be a problem) for GDB to use/print the size as recorded in the ELF symbol table, when it is available.  That might be doable.  (though I probably won't be able to try it myself soon enough).  Not sure it's worth the trouble, though.
Comment 4 Hannes Domani 2024-01-03 20:01:07 UTC
Was fixed some time ago.