12.10 Locating and Reserving Program Memory
In this example, a block of program memory is reserved for a special purpose, such as a bootloader. An arbitrary sized function is allocated in the block, with the remaining space reserved for expansion or other purposes.
The following output section definition is added to a custom linker script:
BOOT_START = 0xA200;
BOOT_LEN = 0x400;
my_boot BOOT_START :
{
*(my_boot);
. = BOOT_LEN; /* advance dot to the maximum length */
} > program
Note the “dot assignment” (.=) that appears inside the section definition after the input sections. Dot is a special variable that represents the location counter, or next fill point, in the current section. It is an offset relative to the start of the section. The statement in effect says “no matter how big the input sections are, make sure the output section is full size.”
The following C function will be allocated in the reserved block:
void __attribute__((section("my_boot"))) func1()
{
/* etc. */
}
The equivalent assembly language would be:
.section my_boot,code
.global _func1
_func1:
; and so on..
return
If the bootloader is allocated at the start of program memory, a custom linker script is not required. Instead, the function could be defined with attribute boot. For example:
void __attribute__((boot)) func1()
{
/* and so on.. */
}
The equivalent definition in assembly language:
.section *,code,boot
.global _func1
_func1:
; and so on..
return
In this case, program memory will be automatically reserved by specifying a CodeGuard Security™ boot segment in FBS configuration word settings, or by specifying a user-defined boot segment with linker command option. See 11.12 Boot and Secure Segments for more information.