1 Memory Sections
Need to list all the sections which are available to the avr.
- Weak Bindings
-
FIXME: need to discuss the .weak directive.
The .text Section
The .text section contains the actual machine instructions which make up your program. This section is further subdivided by the .initN and .finiN sections dicussed below.
The avr-size
program (part of binutils), coming
from a Unix background, doesn't account for the .data initialization space
added to the .text section, so in order to know how much flash the final
program will consume, one needs to add the values for both, .text and .data
(but not .bss), while the amount of pre-allocated SRAM is the sum of .data
and .bss.
The .data Section
This section contains static data which was defined in your code. Things like the following would end up in .data:
It is possible to tell the linker the SRAM address of the beginning of
the .data section. This is accomplished by adding
-Wl,-Tdata,addr
to the
avr-gcc
command used to the link your program. Not that
addr
must be offset by adding 0x800000 the to real
SRAM address so that the linker knows that the address is in the SRAM memory space.
Thus, if you want the .data section to start at 0x1100, pass 0x801100 at the address
to the linker. [offset explained]
When using malloc()
in the
application (which could even happen inside library calls), additional adjustments are
required.
The .bss Section
Uninitialized global or static variables end up in the .bss section.
The .eeprom Section
This is where eeprom variables are stored.
The .noinit Section
This sections is a part of the .bss section. What makes the .noinit section special is that variables which are defined as such:
will not be initialized to zero during startup as would normal .bss data.
Only uninitialized variables can be placed in the .noinit section. Thus,
the following code will cause avr-gcc
to issue an error:
It is possible to tell the linker explicitly where to place the .noinit
section by adding -Wl,--section-start=.noinit=0x802000
to the
avr-gcc
command line at the linking stage. For example, suppose
you wish to place the .noinit section at SRAM address 0x2000:
The .initN Sections
These sections are used to define the startup code from reset up through the start of main(). These all are subparts of the .text section.
The purpose of these sections is to allow for more specific placement of code within your program.
Sometimes, it is convenient to think of the .initN and .finiN sections as functions, but in reality they are just symbolic names which tell the linker where to stick a chunk of code which is not a function. Notice that the examples for asm and C can not be called as functions and should not be jumped into.
- .init0:
-
Weakly bound to __init(). If user defines __init(), it will be jumped into immediately after a reset.
- .init1:
-
Unused. User definable.
- .init2:
-
In C programs, weakly bound to initialize the stack, and to clear __zero_reg__ (r1).
- .init3:
-
Unused. User definable.
- .init4:
- .init5:
-
Unused. User definable.
- .init6:
-
Unused for C programs, but used for constructors in C++ programs.
- .init7:
-
Unused. User definable.
- .init8:
-
Unused. User definable.
- .init9:
-
Jumps into main().
The .finiN Sections
These sections are used to define the exit code executed after return from main() or a call to exit(). These all are subparts of the .text section.
The .finiN sections are executed in descending order from 9 to 0.
- .finit9:
-
Unused. User definable. This is effectively where _exit() starts.
- .fini8:
-
Unused. User definable.
- .fini7:
-
Unused. User definable.
- .fini6:
-
Unused for C programs, but used for destructors in C++ programs.
- .fini5:
-
Unused. User definable.
- .fini4:
-
Unused. User definable.
- .fini3:
-
Unused. User definable.
- .fini2:
-
Unused. User definable.
- .fini1:
-
Unused. User definable.
- .fini0:
-
Goes into an infinite loop after program termination and completion of any _exit() code (execution of code in the .fini9 -> .fini1 sections).
The .note.gnu.avr.deviceinfo Section
This section contains device specific information picked up from the device header file and compiler builtin macros. The layout conforms to the standard ELF note section layout (http://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-18048.html).
The section contents are laid out as below.
Using Sections in Assembler Code
Example:
The ,"ax",@progbits
tells the assembler
that the section is allocatable ("a"), executable ("x") and contains data
("@progbits"). For more detailed information on the .section directive, see
the gas user manual.
Using Sections in C Code
Example:
Section .init3 is used in this example, as this ensures the
inernal __zero_reg__
has already been set up. The code
generated by the compiler might blindly rely on
__zero_reg__
being really 0. __attribute__
((used))
tells the compiler that code must be generated for
this function even if it appears that the function is not referenced - this
is necessary to prevent compiler optimizations (like LTO) from eliminating
the function.