4.9.2 Runtime Startup Code

A C program requires certain objects to be initialized and the device to be in a particular state before it can begin execution of its function main(). It is the job of the runtime startup code to perform these tasks, specifically (and in no particular order):

  • Initialization of static storage duration objects assigned a value when defined
  • Clearing of non-initialized static storage duration objects
  • General set up of registers or device state
  • Calling the main() function

One of several runtime startup code object files which provide the runtime startup code is linked into your program.

The runtime startup code assumes that the device has just come out of Reset and that registers will be holding their power-on-reset value. Note that when the watchdog or RST_SWRST_bm resets the device, the registers will be reset to their known, default settings; whereas, jumping to the reset vector will not change the registers and they will be left in their previous state.

The sections used to hold the runtime startup code are tabulated below.

Table 4-8. Runtime Startup Code Sections used Before main
Section name Description
.init0 Weakly bound to __init(), see 4.9.3 The Powerup Routine. If user defines __init(), it will be jumped into immediately after a reset.
.init1 Unused. User definable.
.init2 In C programs, weakly bound to code which initializes the stack and clears __zero_reg__ (r1).
.init3 Unused. User definable.
.init4 This section contains the code from libgcc.a that copies the contents of .data from the program to data memory, as well as the code to clear the .bss section.
.init5 Unused. User definable.
.init6 Unused for C programs.
.init7 Unused. User definable.
.init8 Unused. User definable.
.init9 Calls the main() function.

The main() function returns to code that is also provided by the runtime startup code. You can have code executed after main() has returned by placing code in the sections tabulated below.

Table 4-9. Runtime Startup Code Sections used After main
Section name Description
.fini9 Unused. User definable.
.fini8 Unused. User definable.
.fini7 In C programs, weakly bound to initialize the stack, and to clear __zero_reg__ (r1).
.fini6 Unused for C program.
.fini5 Unused. User definable.
.fini4 Unused. User definable.
.fini3 Unused for C programs.
.fini2 Unused. User definable.
.fini1 Unused. User definable.
.fini0 Goes into an infinite loop after program termination and completion of any _exit() code (code in the .fini9 thru .fini1 sections).