Linux – Extract the contents of ELF and write to binary file

awkbinary filesddlinuxtail

I have been trying to extract the contents of a firmware and putting it to a binary file, but with no success.

I see the right hex contents, but am not sure how to laid them in bits into a file.

objdump -s -j .text firmware.ko | tail -n +5 | awk '{print "dd if='firmware.ko' of='content.bin' bs=1 count=$["$2 $3 $4 $5 "]"}'

Best Answer

Try this:

objcopy -j .text -O binary firmware.ko firmware.text

The file firmware.text should then contain what you want.


UPDATE: The above should work if the firmware file is in the same format that your machine (running objcopy) uses. If it is not the case, you'll be getting:

objcopy: Unable to recognise the format of the input file `firmware-arm.ko'

Then you'll have to specify the format yourself with -I. Using file will tell you what format your firmware is in, e.g.:

$ file firmware-arm.ko
firmware-arm.ko: ELF 32-bit LSB relocatable, ARM, version 1 (SYSV), BuildID[sha1]=0xec2e703615d915dd1cad09ecc12ff7d57ef186a5, not stripped

And then (for this case where you have an ELF 32 little endian) you'll need:

objcopy -j .text -O binary -I elf32-little firmware-arm.ko firmware-arm.text
Related Question