6

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
  • 851
  • 7
  • 25
  • 47
  • why not using objdump to find the offset and size of .text section and then dump it using dd? – tristan Jan 03 '14 at 07:13
  • actually, I am also trying to do in C system call, so it has to be preferably in 1 line if possible. – Ursa Major Jan 03 '14 at 07:16
  • http://superuser.com/questions/696780/extract-the-contents-of-elf-and-write-to-binary-file || http://stackoverflow.com/questions/3925075/how-do-you-extract-only-the-contents-of-an-elf-section – Ciro Santilli OurBigBook.com Sep 04 '15 at 13:02

2 Answers2

9

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
  • 851
  • 7
  • 25
  • 47
5

You can use objcopy instead

objcopy -O binary --only-section=.text /path/firmware.ko /content.bin
tristan
  • 4,235
  • 2
  • 21
  • 45
  • 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