2

I want to see the assembly code of my code written in the Arduino IDE. What I've tried is:

Sketch > Export compiled binary

Then, I found that I have a gcc toolset in my ESP8266 directory in:

Documents\ArduinoData\packages\esp8266\tools\xtensa-lx106-elf-gcc\2.5.0-4-b40a506\bin>

And there I have a xtensa-lx106-elf-objdump.exe

So I tried to run this objdump on the outputted compiled binary, but I got:

PS> xtensa-lx106-elf-objdump.exe -D <path/to/binfile>
file format not recognized
YoavKlein
  • 225
  • 2
  • 12
  • What was the cmdline you typed to get that error? – Kartman Aug 11 '21 at 06:37
  • By binary, do you mean literally the .bin output, or .hex or .elf? – Justme Aug 11 '21 at 06:46
  • Well, when you click on the "Export compiled binary" in the IDE, you get a .bin file – YoavKlein Aug 11 '21 at 06:48
  • How do you expect objdump to understand what the binary file contains? How would it know what is code or data in the binary and where they are located? Try giving it an executable, an .elf file which does contain that information. Or maybe force the linking stage to produce an assembly listing of the executable, so you don't even have to disassemble anything? – Justme Aug 11 '21 at 08:32
  • @Justme, I don't know much about the low-level details of these. My guess was that this bin file was compiled using this toolchain (i.e. xtensa-lx106-elf-g++.exe and xtensa...ld.exe) so that the objdump tool (provided by the same toolchain) would know about the structure of the produced file. – YoavKlein Aug 11 '21 at 08:40

2 Answers2

1

-D is for disassemble -b bin tells objdump to expect a bin file

There may be ways to inject compiler/linker directives in Arduino, but I would not know. Platformio certainly does allow this, so you could get the asm as emitted from the compiler.

Kartman
  • 6,258
  • 2
  • 7
  • 13
  • It gives me: "invalid bfd target". Any idea? – YoavKlein Aug 11 '21 at 07:49
  • the binary contains very little information. but since arduino either outputs, or is configurable to (additionally) output elf files, it would be better to use objdump on the elf file instead. For maximal understandability, make sure you turn off compiler optimizations and enable debug before compiling. – mo FEAR Feb 27 '22 at 14:08
1

Try this fork of esp-bin2elf for a way to reconstruct an .elf file from the .bin. The former can be disassembled using ...elf-objdump.exe.

Jonathan
  • 111
  • 1