ELF文件中的可变大小与GDB报告的大小不同吗?

时间:2020-10-20 04:10:12

标签: gdb elf

ELF symtab表报告我的变量的大小为16。特别是,在运行readelf -s mybinary之后,我看到以下输出:

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

但是,当我打开GDB检查myVar的大小时,会看到以下输出:

print sizeof(myVar)
$1 = 4

我不确定这种差异是从哪里产生的。在某些背景下,我使用的是x86-32,并且无法访问源文件,因此我不知道myVar的实际类型。

1 个答案:

答案 0 :(得分:1)

您必须具有旧的GDB版本(低于8.1)。

从GDB新闻文件中:

*** 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

如文字所述,在8.1之前,GDB将没有调试信息的任何变量都视为具有int类型的变量,而sizeof(int)在系统上为4。

Online version of the NEWS的文字略有不同:

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

P.S。

我已经尝试过了。都没有提供任何额外的信息

提出问题时,有助于提供所有相关信息并说明您已经尝试过的内容。

统计使用的GDB版本也会有所帮助。