-1
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ May be the bootloader knows before hand ? \$\endgroup\$ – MaNyYaCk Jul 16 '19 at 10:24
  • \$\begingroup\$ 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. \$\endgroup\$ – brhans Jul 16 '19 at 11:17
1
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ "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? \$\endgroup\$ – macro_controller Jul 16 '19 at 11:23
  • \$\begingroup\$ I found it in the Reset_Handler, thanks! \$\endgroup\$ – macro_controller Jul 16 '19 at 11:26
  • 2
    \$\begingroup\$ 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 \$\endgroup\$ – Elliot Alderson Jul 16 '19 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.