# Find ROM and RAM in .bin file for micro controller

I use the Arduino IDE to compile some code to a 32-bit ARM Cortex-M3 based microcontroller.

The result of the compilation is .bin file. The file is a stripped binary file, containing the RAM and ROM images to be programmed on the microcontroller.

Another possible result of the compilation is .elf file for the same code and the same microcontroller. In the elf file, I can easily find the RAM sections - .text, and the ROM sections - .bss, LOAD (ro-data) and so on.

What I can't understand, is how the bootloader of the microcontroller knows to distinguish between the RAM and the ROM while programming the board and loading the image? I can't see in the .bin file any reference to the sections, or anything that could hint it, and the RAM code comes just after the ROM in the .bin.

Any ideas how is it possible?

• May be the bootloader knows before hand ? – MaNyYaCk Jul 16 '19 at 10:24
• There are no RAM sections there. In particular, .text typically holds the code which is programmed into Flash ROM'. '.bss' typically contains statically allocated variables, but those aren't found in a .bin file because they're variable and don't have a fixed value at startup. – brhans Jul 16 '19 at 11:17

Now, when programming in C we know that we can give an initial value to our variables. In reality, that initial value will be part of the ROM binary image and a small bit of C code will copy those values out to RAM when the processor is reset. Likewise, simple variables that are defined without an initial value will be set to zero. The RAM is initialized as part of the system initialization in C. In the Arduino environment, the initialization of RAM is performed before your setup() function is invoked.
• I found it in the Reset_Handler, thanks! – macro_controller Jul 16 '19 at 11:26
• I've never tried to look at an Arduino binary, but you should find a label for main and that will be where the system initialization will begin. It looks like the C runtime is provided as object files, you might be able to view the code in the appropriate file such as crtatmega328.o` – Elliot Alderson Jul 16 '19 at 11:31