Pocket article: How to implement and use .noinit RAM | Interrupt

Imagine there’s an embedded system that needs to persist some state when the processor restarts (either intentionally or due to a catastrophic error).


This is a companion discussion topic for the original entry at https://interrupt.memfault.com/blog/noinit-memory

I chuckled at the use of gcc to ‘edit’ the .ld file, replacing, via a -D directive, FLASH__ with either the bootloader or application string.

I too have used the idea of a .ld.in file, i.e. a template linker script with ‘holes’ to fill in, but have always used good old sed as the edit tool.

Surely this is at least as suitable as gcc? Doing this task with gcc just seems wrong to me, something about ‘if you’ve only got a hammer, everything looks like a nail’ ???

1 Like

You caught me! sed is usually a better option for this kind of work; in this case I was trying to keep the set of tools used in the examples as low as possible (eg, windows may not have sed installed :cry: : ).

This approach was inspired by how zephyr assembles linker scripts BTW, if you’re interested in seeing some serious magic:
https://github.com/zephyrproject-rtos/zephyr/blob/a03e7df9f5d855cb85a5b1660f13677dd4590492/cmake/linker/ld/ld_script.cmake

Note that ld itself has a lot of features around taking fragments of script files- for example, you can specify multiple scripts with -T, and ld will combine the SECTIONS and other groups in the scripts:
https://sourceware.org/binutils/docs/ld/Options.html#index-_002dT-script

You can also use the INSERT command in a linker script to control placement:
https://sourceware.org/binutils/docs/ld/Miscellaneous-Commands.html#index-INSERT