Skip to content

Commit 57cdf76

Browse files
matthijskooijmanfpistm
authored andcommitted
Support a .noinit section for variables
This is inspired by the linker sripts on the AVR architecture, which support a .noinit section (or any section starting with .noinit, actually) for variables that should be allocated an address in RAM, but not be initialized to any particular value (not even zero) on startup. These can then be used to remember values across resets. From the sketch perspective, this works exactly the same as on AVR: Just annote a global variable with `__attribute__((__section__(".noinit")))` and it will have an unpredictable value on power-up and retain its value during resets. To implement this without having to change all board-specific linker scripts, the linker commandline is changed to pass the board-specific linker script to the `--default-script` linker script, and change the main linker script (passed to `--script`, previously `-T`) to a generic "override" linker script. This new generic linker script contains an `INSERT BEFORE` command, which causes the linker to load it *in addition to* the default linker script, while adding an extra `.noinit` output section in the right place. Because these new variables take up RAM but have their own section in the .elf file, they should be accounted for in the size summary after compilation. This is done by adapting the `recipe.size.regex.data` entry to include this new section.
1 parent 6889728 commit 57cdf76

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

Diff for: platform.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ recipe.S.o.pattern="{compiler.path}{compiler.S.cmd}" {compiler.S.flags} {build.i
128128
recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} {compiler.ar.extra_flags} "{archive_file_path}" "{object_file}"
129129

130130
## Combine gc-sections, archives, and objects
131-
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} "-T{build.variant.path}/{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" {compiler.c.elf.extra_flags} {compiler.ldflags} {compiler.arm.cmsis.ldflags} -o "{build.path}/{build.project_name}.elf" "-L{build.path}" -Wl,--start-group {object_files} {compiler.libraries.ldflags} "{archive_file_path}" -lc -Wl,--end-group -lm -lgcc -lstdc++
131+
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} "-Wl,--default-script={build.variant.path}/{build.ldscript}" "-Wl,--script={build.system.path}/ldscript.ld" "-Wl,-Map,{build.path}/{build.project_name}.map" {compiler.c.elf.extra_flags} {compiler.ldflags} {compiler.arm.cmsis.ldflags} -o "{build.path}/{build.project_name}.elf" "-L{build.path}" -Wl,--start-group {object_files} {compiler.libraries.ldflags} "{archive_file_path}" -lc -Wl,--end-group -lm -lgcc -lstdc++
132132

133133
## Create output (.bin file)
134134
recipe.objcopy.bin.pattern="{compiler.path}{compiler.objcopy.cmd}" {compiler.elf2bin.flags} {compiler.elf2bin.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.bin"
@@ -147,7 +147,7 @@ recipe.output.save_file={build.project_name}.{build.variant}.hex
147147
## Compute size
148148
recipe.size.pattern="{compiler.path}{compiler.size.cmd}" -A "{build.path}/{build.project_name}.elf"
149149
recipe.size.regex=^(?:\.text|\.data|\.rodata)\s+([0-9]+).*
150-
recipe.size.regex.data=^(?:\.data|\.bss)\s+([0-9]+).*
150+
recipe.size.regex.data=^(?:\.data|\.bss|\.noinit)\s+([0-9]+).*
151151
recipe.size.regex.eeprom=^(?:\.eeprom)\s+([0-9]+).*
152152

153153

Diff for: system/ldscript.ld

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This script extends the default linker script to add a .noinit
3+
* section. This section is just mapped to RAM, but it is emitted
4+
* separately from the .data and .bss sections (both of which are
5+
* initialized by startup code), so any variables in this section are
6+
* untouched on startup (so they survive across resets).
7+
*
8+
* This script is intended to supplied to the linker's -T / --script
9+
* option as the primary linker script. When the linker sees an INSERT
10+
* command, this will cause it to *also* read the default linker script
11+
* (after reading this script) and then executing the INSERT commands
12+
* after both scripts have been read.
13+
*
14+
* Note that parsing of linker scripts is a bit peculiar, e.g. INSERT
15+
* does not textually inserts, it inserts any generated output sections.
16+
* Also, because this script is read *first*, we cannot refer to things
17+
* in the default script. In particular, it would make sense to add >
18+
* RAM to the output section below to ensure that the section is mapped
19+
* into RAM, but the RAM region is not defined yet (I think it would
20+
* work though, but produces warnings). Instead, we just rely on the
21+
* defaults used by the linker: If no region is defined for an output
22+
* section, it will just map to first address after the previous section
23+
* (.bss in this case, which is fine).
24+
*/
25+
SECTIONS
26+
{
27+
/* Define a noinit output section and mark it as NOLOAD to prevent
28+
* putting its contents into the resulting .bin file (which is the
29+
* default). */
30+
.noinit (NOLOAD) :
31+
{
32+
/* Ensure output is aligned */
33+
. = ALIGN(4);
34+
/* Define a global _snoinit (and _enoinit below) symbol just in case
35+
* code wants to iterate over all noinit variables for some reason */
36+
_snoinit = .;
37+
/* Actually import the .noinit and .noinit* import sections */
38+
*(.noinit)
39+
*(.noinit*)
40+
. = ALIGN(4);
41+
_enoinit = .;
42+
}
43+
}
44+
INSERT AFTER .bss;

0 commit comments

Comments
 (0)