Skip to main content
Graduate II
March 5, 2025
Solved

which file starts STM32

  • March 5, 2025
  • 2 replies
  • 946 views

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

 

 

 Untitled.png

    This topic has been closed for replies.
    Best answer by TDK

    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.

    2 replies

    TDKAnswer
    Super User
    March 5, 2025

    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.

    MNapiAuthor
    Graduate II
    March 5, 2025

    thanks for clarification this is only reference to main I can find, I guess this must be the place.

     

    Untitled1.png

    Graduate II
    March 5, 2025

    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.

    Super User
    March 5, 2025

    @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.