11

I have a hex file that is to be flashed onto an Atmel chip running on an Arduino device.

There are certain aspects of this file that I would like to modify before putting it onto my Arduino, but I don't have the source C++; just the hex file.

Is there any efficient way to take a .hex file and reassemble the C code? If so, how?

underscore_d
  • 6,309
  • 3
  • 38
  • 64
Matt Cashatt
  • 23,490
  • 28
  • 78
  • 111
  • 2
    No, it's impossible to get the original file, or anything even remotely looking like the original file. – Some programmer dude May 23 '13 at 05:18
  • You can decompile it back into C code you can modify, but like Joachim said, it won't look anything like the original. – Ben Voigt May 23 '13 at 05:22
  • @BenVoigt--that is fine. How would I go about doing that? Thanks! – Matt Cashatt May 23 '13 at 05:30
  • @MatthewPatrickCashatt Google "C decompiler". I don't think one exists for AVRs. –  May 23 '13 at 05:33
  • If you can't find a decompiler you can also look for IDA Pro and check if it supports your target. Of course IDA is not a decompiler but a dissassembler, so you might have to disassmeble it and turn it back to C on your own, or apply the changes you want via assembler. IDA is rather expensive though, but it's the best tool for reverse engineering. – Devolus May 23 '13 at 06:39
  • @Devolus - remember to tell him he has to be able to handle AVR assembly ;) – Shirkrin May 23 '13 at 13:09
  • You don't decompile. You get the C source code (or you rewrite it) – Basile Starynkevitch Jul 23 '18 at 08:19

2 Answers2

13

I would look at the output of avr-objdump (gulp):

avr-objdump -j .sec1 -d -m avr5 foo.hex 

You will have to change the words following the "-m" to your architecture. Even when/if this works it will give you the Assembly code, which might not look anything you have ever written. The variable names will different, and the handy Arduino functions will look like messy Assembly junk. I hope there is a better way, sorry.

See also AVR GCC forum - Using avr-objdump to disassemble hex code.

Community
  • 1
  • 1
John b
  • 1,338
  • 8
  • 16
12

This may not have been available at the time the other answers was given. It coverts the .hex back to assembler. You might need to know the architecture of the original AVR it was intended for. Works well for me for code that I wrote and compiled. I tested with AVR-25 for Tiny 85. Hope it helps. Would be nice to have an offline version of same thing! http://www.onlinedisassembler.com/ An alternative commercial option is IDA from https://www.hex-rays.com/

cdplayer
  • 495
  • 5
  • 15