views:

107

answers:

2

I've tried the following, but the resulting file is still an ELF and not purely the section content.

$ objcopy --only-section=<name> <infile> <outfile>

I just want the contents of the section. Is there any utility that can do this? Any ideas?

+3  A: 

Use the -O binary output format:

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

Just verified with avr-objcopy and an AVR ELF image's .text section.

ndim
When I add this, the resulting <outfile> is empty. It contains neither the ELF section header nor the section content. The command I used in the question does not produce an empty file.
mepcotterell
The following works for me: objcopy -O binary -j .text /usr/bin/lpr mylprtext
Eric Seppanen
+3  A: 

Rather inelegant hack around objdump and 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" $3 "] skip=$[0x" $6 "]"}' |
  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.

In past I did all that manually, without making any scripts, since it is rarely needed.

Dummy00001
+1 for brute force!
bstpierre
That would sound funny coming from me, but why not the straight-forward *proper* solution from @ndim???
Dummy00001
Because it didn't work for all my test cases. The above did.
mepcotterell