Embedded Design Handbook

ID 683689
Date 8/28/2023
Public
Document Table of Contents

4.2.3.2.2. crt0 Initialization

The crt0 code block contains the C run-time initialization code—software instructions needed to enable execution of C or C++ applications. The crt0 code block can potentially be used by user-defined assembly language procedures as well. The Intel-provided crt0 block performs the following initialization steps:

  1. Calls alt_load macros—If the application is designed to run from flash memory (the .text section runs from flash memory), the remaining sections are copied to volatile memory.

    For additional information, refer to “Configuring the Boot Environment”.

  2. Initializes instruction cache—If the processor has an instruction cache, this cache is initialized. All instruction cache lines are zeroed (without flushing) with the initi instruction.
    Note: Platform Designer determines the processors that have instruction caches, and configures these caches at system generation. The Nios® II Software Build Tools insert the instruction-cache initialization code block if necessary.
  3. Initializes data cache—If the processor has a data cache, this cache is initialized. All data cache lines are zeroed (without flushing) with the initd instruction. As for the instruction caches, this code is enabled if the processor has a data cache.
  4. Sets the stack pointer—The stack pointer is initialized. You can set the stack pointer address.

    For additional information refer to “HAL Linking Behavior”.

  5. Clears the .bss section—The .bss section is initialized to all zeros. You can set the .bss section address.

    For additional information refer to “HAL Linking Behavior”.

  6. Initializes stack overflow protection—Stack overflow checking is initialized.

    For additional information, refer to “Software Debugging in Nios® II Software Build Tools for Eclipse”.

  7. Jumps to alt_main()—The processor jumps to the alt_main() function, which begins initializing the HAL BSP run-time library.
    Note: If you use a third-party RTOS or environment for your BSP library file, the alt_main() function could be different than the one provided by the Nios® II EDS.

If you use a third-party compiler or library, the C run-time initialization behavior may differ from this description.

The crt0 code includes initialization short-cuts only if you perform hardware simulations of your design. You can control these optimizations by turning hal.enable_sim_optimize on or off.

For information about the hal.enable_sim_optimize BSP setting, refer to “Settings Managed by the Software Build Tools” in the Nios® II Software Build Tools Reference chapter of the Nios® II Gen2 Software Developer's Handbook.

The crt0.S source file is located in the <tools installation> /ip/altera/nios2_ip/altera_nios2/HAL/src directory.