考虑以下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"
是否存在一些bintools实用程序(或任何命令行程序)的开关组合,这些开关将执行等同于以下伪代码的操作:
确定:
- 你需要找到符号所在的部分,以及该部分的地址和数据长度,以及
- 你需要找到section本身在文件中的起始位置,而
- 你需要从文件的右偏移量中转储长度字节。
将所有这些放在一起(我的文件与您的文件略有不同):
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个字节,因为我不想要终止