The following section describes the role of the C runtime-startup code, crt0.s (and the Cortex-M/Thumb equivalent thumb_crt0.s).

When you create a new project to produce an executable file using a target-specific project template, the crt0.s/thumb_crt0.s file is added to the project. Initially, a shared version of this file is added to the project. If you want to modify this file, right-click it in the Project Explorer and then select Import from the shortcut menu to copy the file to your project directory.

The entry point of the C runtime-startup code is _start. In a typical system, this will be called by the target-specific startup code after it has initialized the target.

The C runtime carries out the following actions:

On return from main or when exit is called…

Program sections

The following program sections are used for the C runtime in section-placement files:

Section name Description
.vectors The exception vector table.
.init Startup code that runs before the call to the application's main function.
.ctors Static constructor function table.
.dtors Static destructor function table.
.text The program code.
.fast Code to copy from flash to RAM for fast execution.
.data The initialized static data.
.bss The zeroed static data.
.rodata The read-only constants and literals of the program.
.ARM.exidx The C++ exception table.
.tbss Thread local storage zero'd data followed by
.tdata Thread local storage initialised data.

Stacks

ARM and Cortex-A/Cortex-R devices have six separate stacks. The position and size of these stacks are specified in the project's section-placement or memory-map file by the following program sections:

Section name Linker size symbol Description
.stack __STACKSIZE__ System and User mode stack.
.stack_svc __STACKSIZE_SVC__ Supervisor mode stack
.stack_irq __STACKSIZE_IRQ__ IRQ mode stack
.stack_fiq __STACKSIZE_FIQ__ FIQ mode stack
.stack_abt __STACKSIZE_ABT__ Abort mode stack
.stack_und __STACKSIZE_UND__ Undefined mode stack

Cortex-M devices have the following stacks and linker symbol stack sizes are defined:

Section name Linker size symbol Description
.stack __STACKSIZE__ Main stack.
.stack_process __STACKSIZE_PROCESS__ Process stack.

The crt0.s/thumb_crt0.s startup code references these sections and initializes each of the stack-pointer registers to point to the appropriate location. To change the location in memory of a particular stack, the section should be moved to the required position in the section-placement or memory-map file.

Should your application not require one or more of these stacks, you can remove those sections from the memory-map file or set the size to 0 and remove the initialization code from the crt0.s/thumb_crt0.s file.

The .data section

The .data section contains the initialized data. If the run address is different from the load address, as it would be in a flash-based application in order to allow the program to run from reset, the crt0.s/thumb_crt0.s startup code will copy the .data section from the load address to the run address before calling the main entry point.

The .fast section

For performance reasons, it is a common requirement for embedded systems to run critical code from fast memory; the .fast section can be used to simplify this. If the .fast section's run address is different from the load address, the crt0.s/thumb_crt0.s startup code will copy the .fast section from the load address to the run address before calling the main entry point.

The .bss Section

The .bss section contains the zero-initialized data. The startup code in crt0.s/thumb_crt0.s references the .bss section and sets its contents to zero.

The heap

The position and size of the heap is specified in the project's section-placement or memory-map file by the .heap program section.

The startup code in crt0.s/thumb_crt0.s references this section and initializes the heap. To change the position of the heap, the section should be moved to the required position in the section-placement or memory-map file.

There is a Heap Size linker project option you can modify in order to alter the heap size. For compatibility with earlier versions of SEGGER Embedded Studio, you can also specify the heap size using the heap section's Size attribute in the section-placement or memory-map file.

Should your application not require the heap functions, you can remove the heap section from the memory-map file or set the size to zero and remove the heap-initialization code from the crt0.s/thumb_crt0.s file.