5

How do we use objdump to output to a binary file?

This is definitely not the right way to do so:

objdump -s -j .text /path/firmware.ko > /content.bin

as it is only presenting text format. I only require the bytes of the text segment to be extracted and to be set in binary forms.

Ursa Major
  • 881
  • 6
  • 23
  • 47

2 Answers2

8

We have to specify the file format explicitly using the -I.

objcopy -I #file type format# -j #ELF segment contents to copy# -O #data type to output, binary, etc# #input file# #output file#

eg.

 
objcopy -I elf32-little -j .text -O binary firmware.ko content.bin 
Ursa Major
  • 881
  • 6
  • 23
  • 47
5

You can use objcopy instead

objcopy -O binary --only-section=.text /path/firmware.ko /content.bin
tristan
  • 4,133
  • 2
  • 20
  • 42
  • Hi, It states: objcopy: Unable to recognise the format of the input file '/path/firmware.ko'. – Ursa Major Jan 03 '14 at 08:06
  • sorry i don't know your architecture but did you run the objcopy that is from the same arch as the .ko file? – tristan Jan 03 '14 at 08:23
  • The file format is "file format elf32-little". It was stated by objdump. Is there a way to specify this in objcopy? Thank you, @tristan, for your patient guidance. – Ursa Major Jan 03 '14 at 08:29
  • I see: elf32-little (header little endian, data little endian) i386 l1om k1om plugin – Ursa Major Jan 03 '14 at 17:16
  • How about something like:
    objdump -s -j .text firmware.ko | awk '{print "dd if='firmware.ko' of='content.bin' bs=1 count=$[0x" $3 "] skip=$[0x" $6 "]"}' | bash
    
    ? but the syntax is still not correct.
    – Ursa Major Jan 03 '14 at 18:58
  • I manage to get abit closer, but it's still text
    objdump -s -j .text firmware.ko | awk '{print "dd if='firmware.ko' of='content.bin' bs=1 count=$["$2 $3 $4 $5 "]"}' 
    
    eg. dd if=firmware.ko of=content.bin bs=1 count=$[] dd if=firmware.ko of=content.bin bs=1 count=$[fileformatelf32-little] dd if=firmware.ko of=content.bin bs=1 count=$[] dd if=firmware.ko of=content.bin bs=1 count=$[ofsection.text:] dd if=firmware.ko of=content.bin bs=1 count=$[1101a0e102c0a0e1004025e90540a081] : and the 1st 4 lines is not needed. How do I filter?
    – Ursa Major Jan 03 '14 at 19:22