Variable size in ELF file is different from size reported by GDB?

Viewed 148

The ELF symtab table reports that my variable has size 16. In particular, after running readelf -s mybinary, I see the following output:

Num:    Value  Size Type    Bind   Vis      Ndx Name
83: 0804a718    16 OBJECT  LOCAL  DEFAULT   25 myVar

However, when I open GDB to examine the size of myVar, I see the following output:

print sizeof(myVar)
$1 = 4

I am not sure where this discrepancy is arising from. For some background, I am using x86-32, and I do not have access to the source file, so I do not know the actual type of myVar.

1 Answers

You must have an old GDB version (older than 8.1).

From the GDB NEWS file:

*** Changes in GDB 8.1
...
  GDB no longer assumes functions with no debug information return
  'int'.
...
  Similarly, GDB no longer assumes that global variables with no debug
  info have type 'int', and refuses to print the variable's value
  unless you tell it the variable's type:

    (gdb) p var
    'var' has unknown type; cast it to its declared type
    (gdb) p (float) var
    $3 = 3.14

As the text says, before 8.1 GDB treated any variable without debug info as having int type, and sizeof(int) is 4 on your system.

Online version of the NEWS has slightly different text:

GDB no longer makes assumptions about the type of symbols without
debugging information to avoid producing erroneous and often confusing results;

P.S.

I already tried this. Neither gives any extra information

When asking a question, it helps to supply all relevant info and state what you have already tried.

Stating the version of GDB you are using would have helped as well.

Related