-3

Before running main() function in users' application, it will IMPORT __main and execute __main, so I wonder that what does this function do?

__main

copy rw variables from flash to ram? initialize bss section? initialzie stack/heap section? anything else?

Does it initialize according to the scater file which defines the execute region?

George Ma
  • 11
  • 1
  • 1
    Please do not post links to images. Post code instead. That will increase the likeliness of actually getting an answer dramatically. – Refugnic Eternium Dec 02 '22 at 06:47
  • 3
    Why not generate a binary and disassemble it? – Tom V Dec 02 '22 at 06:55
  • Welcome to StackOverflow! Please take the [tour] to learn how this site works, and read "[ask]". -- There are a lot of prerequisites a compliant program can expect before `main()` is called. I assume that this function fulfills this, at least in parts. To learn about it, read a good C book and/or the C standard, and do as TomV suggests. – the busybee Dec 02 '22 at 07:31
  • It's very hard to say for sure without viewing what is at those labels. I imagine that `SystemInit` is some sort of setup sequence that clears the system's RAM, sets up the stack pointer, default interrupt handlers, etc. Probably typical C standard library stuff. – puppydrum64 Dec 02 '22 at 17:04

1 Answers1

0

Copied from https://developer.arm.com/documentation/100748/0618/Embedded-Software-Development/Application-startup

Application startup

In most embedded systems, an initialization sequence executes to set up the system before the main task is executed.

The following figure shows the default initialization sequence.

Figure 1. Default initialization sequence enter image description here

Default initialization sequence __main is responsible for setting up the memory and __rt_entry is responsible for setting up the run-time environment.

__main performs code and data copying, decompression, and zero initialization of the ZI data. It then branches to __rt_entry to set up the stack and heap, initialize the library functions and static data, and call any top level C++ constructors. __rt_entry then branches to main(), the entry to your application. When the main application has finished executing, __rt_entry shuts down the library, then hands control back to the debugger.

The function label main() has a special significance. The presence of a main() function forces the linker to link in the initialization code in __main and __rt_entry. Without a function labeled main(), the initialization sequence is not linked in, and as a result, some standard C library functionality is not supported.

kkrambo
  • 6,643
  • 1
  • 17
  • 30