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 nameDescription
.init0Weakly bound to __init(), see The Powerup Routine. If user defines __init(), it will be jumped into immediately after a reset.
.init1Unused. User definable.
.init2In C programs, weakly bound to code which initializes the stack and clears __zero_reg__ (r1).
.init3Unused. User definable.
.init4This 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.
.init5Unused. User definable.
.init6Unused for C programs.
.init7Unused. User definable.
.init8Unused. User definable.
.init9Calls 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 nameDescription
.fini9Unused. User definable.
.fini8Unused. User definable.
.fini7In C programs, weakly bound to initialize the stack, and to clear __zero_reg__ (r1).
.fini6Unused for C program.
.fini5Unused. User definable.
.fini4Unused. User definable.
.fini3Unused for C programs.
.fini2Unused. User definable.
.fini1Unused. User definable.
.fini0Goes into an infinite loop after program termination and completion of any _exit() code (code in the .fini9 thru .fini1 sections).