如何仅提取 ELF 部分的原始内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3925075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract only the raw contents of an ELF section?
提问by mepcotterell
I've tried the following, but the resulting file is still an ELF and not purely the section content.
我尝试了以下内容,但生成的文件仍然是 ELF 而不是纯粹的部分内容。
$ objcopy --only-section=<name> <infile> <outfile>
I just want the contents of the section. Is there any utility that can do this? Any ideas?
我只想要该部分的内容。有没有可以做到这一点的实用程序?有任何想法吗?
采纳答案by Dummy00001
Rather inelegant hack around objdump
and dd
:
相当不雅的 hackobjdump
和dd
:
IN_F=/bin/echo
OUT_F=./tmp1.bin
SECTION=.text
objdump -h $IN_F |
grep $SECTION |
awk '{print "dd if='$IN_F' of='$OUT_F' bs=1 count=$[0x" "] skip=$[0x" "]"}' |
bash
The objdump -h
produces predictable output which contains section offset in the elf file. I made the awk
to generate a dd
command for the shell, since dd
doesn't support hexadecimal numbers. And fed the command to shell.
所述objdump -h
产生包含部分中的小精灵文件偏移预测的输出。由于不支持十六进制数字,因此我为 shellawk
生成了一个dd
命令dd
。并将命令提供给 shell。
In past I did all that manually, without making any scripts, since it is rarely needed.
过去我手动完成所有这些,没有编写任何脚本,因为很少需要它。
回答by ndim
Use the -O binary
output format:
使用-O binary
输出格式:
objcopy -O binary --only-section=.text foobar.elf foobar.text
Just verified with avr-objcopy
and an AVR ELF image's .text
section.
刚刚通过avr-objcopy
AVR ELF 图像.text
部分进行了验证。
Note that if, as Tim points out below, your section doesn't have the ALLOC flag, you may have to add --set-section-flags .text=alloc
to be able to extract it.
请注意,如果正如 Tim 在下面指出的那样,您的部分没有 ALLOC 标志,您可能必须添加--set-section-flags .text=alloc
才能提取它。
回答by Zibri
Dump all sections in separate files.
将所有部分转储到单独的文件中。
readelf -a filename|grep "NULL\|LOAD"| (x=0;while read a;do echo "$x $a"|awk '{print "dd if=143 of=filename.section."" bs=1 skip=$((" ")) count=$((""))"}';let x=x+1;done)|bash