0
\$\begingroup\$

As I am working myself into the AT91 Bootstrap project, I have found that its main() function apparently returns the address of the next-level software component:

int main(void)
{
  ...
#ifdef CONFIG_BACKUP_MODE
    ret = backup_mode_resume();
    if (ret) {
        /* ...jump to Linux here */
        return ret;
    }
#endif
  ...
#if !defined(CONFIG_LOAD_NONE)
    return JUMP_ADDR;
#else
    return 0;
#endif
}

I am wondering, what happens at this point under the hood:

  • Is the return value stored according the normal calling conventions or the return value of main() is treated in a specific way?
  • What processor instructions get executed after the main() returns? Is it something in some epilogue of main() or it is some hardware-level behavior of the SAMA5 processor? How exactly does MPU use the return value?
  • To what extent is such behavior generic: SAMA5? ARM?
\$\endgroup\$
1
2
\$\begingroup\$

The main is just like any other function. The project has custom statup code and what it does after returning from main depends on with what defines it was compiled with. One option is that it remains in an infinite loop so it halts. Another option is that the return value is an address of function to execute. Startup code for my ARM just resets the MCU so it restarts.

UPD(by Skobls): The custom startup code resides in /crt0_gnu.S file. Here is the interesting part (condensed version):

/* Branch on C code Main function (with interworking) */
_branch_main:
    ldr     r4, = main
    mov     lr, pc
    bx      r4

    mov     lr, pc
    bx      r0

The first block is a standard construct. The bx r4 instruction makes the CPU jump to the main() function. When the latter returns, by Procedure Call Standard for the ARM® Architecture it stores its result in the r0 register. Thus, the bx r0 instruction makes an indirect jump to whatever address main() had returned. In particular, the NULL return value from main() will restart the AT91 Bootstrap without fully resetting the CPU and without executing the 1st-Level Bootloader from the CPU ROM

\$\endgroup\$

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.