# 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

## 1 Answer

The binary image only provides the contents of the Flash ROM memory; it does not provide an "image" of the RAM. Once programmed, the microcontroller does the same thing every time you turn it on...that wouldn't be possible if we tried to download data to the RAM as part of the programming process, because the RAM data is not saved when we cycle power.

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.

Of course, the ROM must therefore contain the addresses of the RAM regions to be initialized, as well as the address range in the ROM that holds the initial values. Setting all of this up is part of the function of the linker/loader, which the Arduino IDE runs for you.

• "a small bit of C code will copy those values out to RAM" - It's exactly what I'm looking for - the place the code is copied to the RAM from the ROM. I'll try to look it in the binary, any idea how could it be called/found? – macro_controller Jul 16 '19 at 11:23
• 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