27.3 Changing Non-Auto Variable Allocation
Another way to locate data is by placing the variable into a user-defined section, and specifying the starting address of that section in a custom linker script. This is done as follows:
- Modify the data declaration in the C source to specify a user-defined section.
- Add the user-defined section to a custom linker script file to specify the starting address of the section.
For example, to locate the variable Mabonga
at address
0x1000 in data memory, first declare the variable as follows in the C source:
int __attribute__((__section__(".myDataSection"))) Mabonga =
1;
The section attribute specifies that the variable should be placed in a
section named .myDataSection
, rather than the default
.data
section. It does not specify where the user-defined section is to
be located. Again, that must be done in a custom linker script, as follows. Using the
device-specific linker script as a base, add the following section definition:
.myDataSection 0x1000 :
{
*(.myDataSection);
} >data
This specifies that the output file should contain a section named
.myDataSection
starting at location 0x1000 and containing all input
sections named .myDataSection
. Since, in this example, there is a single
variable Mabonga
in that section, then the variable will be located at
address 0x1000 in data memory.