2025-03-05 6:15 AM - edited 2025-03-05 6:16 AM
I do have this demo program in which main.c is disabled so it would start from main_pinpong.c which is the only file with
int main( void )
{
included in target build
startup_stm32l476xx.s is supposed to be the startup file but I do not see any reference to
main.c or main_pinpong.c
I see that main.c is not included in target build but main_pinpong.c is included
so they question is what points the compiler to execute int main( void ) from main_pinpong.c
Solved! Go to Solution.
2025-03-05 6:26 AM - edited 2025-03-05 7:22 AM
The program starts from ResetHandler which is typically in startup_stm32*.s.
main() is called at the end
Reset_Handler:
ldr sp, =_estack /* set stack pointer */
...
/* Call static constructors */
bl __libc_init_array
/* Call the application's entry point.*/
bl main
When the program is being linked, the linker looks for the "main" symbol and jumps to that on the "bl main" line. It doesn't care which file it's in. If multiple "main" functions are compiled in your project, the linker will give you an error about duplicate symbols.
2025-03-05 6:26 AM - edited 2025-03-05 7:22 AM
The program starts from ResetHandler which is typically in startup_stm32*.s.
main() is called at the end
Reset_Handler:
ldr sp, =_estack /* set stack pointer */
...
/* Call static constructors */
bl __libc_init_array
/* Call the application's entry point.*/
bl main
When the program is being linked, the linker looks for the "main" symbol and jumps to that on the "bl main" line. It doesn't care which file it's in. If multiple "main" functions are compiled in your project, the linker will give you an error about duplicate symbols.
2025-03-05 6:40 AM
@MNapi wrote:what points the compiler to execute int main( void ) from main_pinpong.c
The compiler doesn't execute any of the source code - it just compiles it.
The C language specification states that the entry point is called main.
As @TDK said, the startup code populates the Reset vector and provides the jump to main.
2025-03-05 8:03 AM
thanks for clarification this is only reference to main I can find, I guess this must be the place.
2025-03-05 8:13 AM - edited 2025-03-05 8:19 AM
In Keil __main is a wrapper function that calls scatterload and then main
You can generate a disassembly using FROMELF -CSD project.axf >project.lst
The scatter loader unpacks the RAM initialization from FLASH, basically the equivalent code to that is used by GNU/GAS to copy and zero memory space, but via tables generated by the linker.
2025-03-05 8:16 AM
@MNapi wrote:thanks for clarification this is only reference to main I can find, I guess this must be the place.
Yes, it is.
This is the "call":
LDR R0, =__main
BX R0
Please see How to insert source code - not as an image!