Search code examples
linuxshellelfbinutils

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


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?


Solution

  • 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.