如何仅提取 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:39:42  来源:igfitidea点击:

How to extract only the raw contents of an ELF section?

linuxshellelfbinutils

提问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 objdumpand dd:

相当不雅的 hackobjdumpdd

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 -hproduces predictable output which contains section offset in the elf file. I made the awkto generate a ddcommand for the shell, since dddoesn'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 binaryoutput format:

使用-O binary输出格式:

objcopy -O binary --only-section=.text foobar.elf foobar.text

Just verified with avr-objcopyand an AVR ELF image's .textsection.

刚刚通过avr-objcopyAVR 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=allocto 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