使用命令行实用程序通过变量名从 .elf 二进制文件中检索 const 字符串值?

问题描述 投票:0回答:1

考虑以下

main.c

#include <stdio.h>

const char greeting[] = "hello world";

int main() {
    printf("%s!\n", greeting);
    return 0;
}

我在 Ubuntu 中编译了这个:

gcc -g main.c -o main.exe

我想检索名为

greeting
的变量的值;考虑到它是
const
,它不会改变,所以应该可以从可执行文件中检索值“hello world”。

基本上,我可以使用以下命令查看二进制文件中的变量名:

$ readelf -p .rodata main.exe | grep hello
  [     8]  hello world

...我可以看到使用的价值:

$ readelf -s main.exe | grep greeting
    59: 0000000000002008    12 OBJECT  GLOBAL DEFAULT   18 greeting

我可以尝试解析

readelf -s
readelf -p
的输出以获得我想要的(检索名为
greeting
的变量的值),但我很确定我会把它搞砸。

那么是否存在 bintools 实用程序(或任何命令行程序)的开关组合,它们将执行与以下伪代码等效的操作:

$ [tool] --get-value-of-variable-name greeting --program=main.exe
"hello world"

甚至:

$ [tool] --verbose --get-value-of-variable-name greeting --program=main.exe
The constant value of the variable "greeting" in `main.exe` is:
"hello world"
debugging binaryfiles elf
1个回答
1
投票

是否存在 bintools 实用程序(或任何命令行程序)的开关组合,它们将执行与以下伪代码等效的操作:

当然:

  • 你需要找到符号所在的部分,以及该部分内的地址,以及数据的长度,以及
  • 您需要找到该部分本身在文件中的位置,并且
  • 您需要从文件中的正确偏移量转储长度字节。

将所有这些放在一起(我的文件与您的数据略有不同):

readelf -Ws main.exe | grep greeting
    29: 0000000000002008    12 OBJECT  GLOBAL DEFAULT   17 greeting

readelf -WS main.exe | grep '\[17\]'
  [17] .rodata           PROGBITS        0000000000002000 002000 000019 00   A  0   0  8

这告诉我我需要转储

12
字节(实际上是11,因为我不想要终止
\0
),从偏移量
0x2000 + (0x2008 (symbol address) - 0x2000 (section address))
开始。

dd if=main.exe bs=1 skip=$((0x2008)) count=11 2>/dev/null
hello world

现在,从

readelf
输出中解析这些数据比它的价值更麻烦——编写一个简单的
C++
程序来产生所需的输出要容易得多。使用 ELFIO 应该使这非常容易。

© www.soinside.com 2019 - 2024. All rights reserved.