gpt4 book ai didi

elf - 从 elf/obj 文件中提取字符串变量

转载 作者:行者123 更新时间:2023-12-04 12:08:28 30 4
gpt4 key购买 nike

我试图从 Linux 程序的 elf 文件中提取特定的字符串变量(即符号),甚至从它来自的 .o 中提取。
它在 .rodata 部分,显然我知道符号名称。
是否有一系列 objdump 样式的命令和选项可用于转储字符串?

更新:

例如,.map 文件包括:

.rodata.default_environment 0x000000001013f763 0x615 common/built-in.o
0x000000001013f763 default_environment

变量本身 - default_environment - 是标准的以空字符结尾的文本字符串。

最佳答案

Is there a sequence of objdump-style commands and options I can use to dump out the string?



当然。让我们构建一个例子:
const char foo[] = "Some text";
const char bar[] = "Other text";

const void *fn1() { return foo; }
const void *fn2() { return bar; }

$ gcc -c t.c

假设我们要提取 bar[] 的内容.
$ readelf -Ws t.o | grep bar
10: 000000000000000a 11 OBJECT GLOBAL DEFAULT 5 bar

这告诉我们 bar 的“内容”变量位于第 5 部分,偏移量 0xa , 长度为 11 个字节。

我们可以提取整个第 5 节:
$ readelf -x5 t.o

Hex dump of section '.rodata':
0x00000000 536f6d65 20746578 74004f74 68657220 Some text.Other
0x00000010 74657874 00 text.

并确实找到了我们正在寻找的字符串。如果您真的只想提取 bar 的内容(例如,因为 .rodata 真的很大,和/或因为 bar 包含嵌入的 NUL s):
$ objcopy -j.rodata -O binary t.o t.rodata    # extract just .rodata section
$ dd if=t.rodata of=bar bs=1 skip=10 count=11 # extract just bar

11+0 records in
11+0 records out
11 bytes (11 B) copied, 0.000214501 s, 51.3 kB/s

看结果:
$ xd bar
000000 O t h e r t e x t nul O t h e r t e x t .

QED。

关于elf - 从 elf/obj 文件中提取字符串变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45236977/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com