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

ELF文件中的变量大小与GDB报告的大小不同?,gdb,elf,Gdb,Elf,ELF symtab表报告我的变量大小为16。特别是,在运行readelf-s mybinary之后,我看到以下输出: Num: Value Size Type Bind Vis Ndx Name 83: 0804a718 16 OBJECT LOCAL DEFAULT 25 myVar print sizeof(myVar) $1 = 4 但是,当我打开GDB检查myVar的大小时,我看到以下输出: Num: Value Size Type

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

Num:    Value  Size Type    Bind   Vis      Ndx Name
83: 0804a718    16 OBJECT  LOCAL  DEFAULT   25 myVar
print sizeof(myVar)
$1 = 4
但是,当我打开GDB检查myVar的大小时,我看到以下输出:

Num:    Value  Size Type    Bind   Vis      Ndx Name
83: 0804a718    16 OBJECT  LOCAL  DEFAULT   25 myVar
print sizeof(myVar)
$1 = 4

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

您必须使用旧的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

文本略有不同:

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

附言


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

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


说明您正在使用的GDB版本也会有所帮助。

了解GDB对
myVar
的了解应该是您的第一步。
myVar
ptype myVar
说什么?我已经试过了。两者都不提供任何额外信息: