Extract the contents of ELF and write to binary file

2

1

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 "]"}'

Ursa Major

Posted 2014-01-04T01:18:51.650

Reputation: 123

http://stackoverflow.com/questions/20898354/objdump-to-extract-contents-of-text-segment-to-a-binary-format – Ciro Santilli 新疆改造中心法轮功六四事件 – 2015-09-04T13:03:03.107

Answers

6

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

nickie

Posted 2014-01-04T01:18:51.650

Reputation: 322

I could not just objcopy as

objcopy: Unable to recognise the format of the input file – Ursa Major – 2014-01-05T02:18:47.300

1If objdump works, I cannot see why objcopy should not work. What is the format of your file? You can specify it with -I if you know it, e.g. -I elf64-x86-64. – nickie – 2014-01-05T11:26:48.410

Using objdump -s -j firmware.ko. It states that it is > file format elf32-little Using file formware.ko. It states that it is ELF 32-bit LSB relocatable, ARM, version 1 (SYSV)

Thank you, @nickie. – Ursa Major – 2014-01-05T11:49:49.820

I used objcopy -I elf32-little -j .text -O binary firmware.ko content.bin

I think it works now.

Thank you, @nickie. You saved the day. – Ursa Major – 2014-01-05T11:54:47.923

Assuming you're also running objcopy on your ARM machine, I don't think you'd need anything special. If not, I'd suggest -I elf32-arm but I don't know if your objcopy supports that. – nickie – 2014-01-05T11:58:26.347

1OK, I think I've figured this out. I'm updating the answer... – nickie – 2014-01-05T12:08:32.777

1Just to make sure my comment above does not confuse anybody: I don't know if an elf32-arm format is recognized by any version of objcopy. If not, try elf32-little, this should work. – nickie – 2014-01-05T12:16:52.560