How the generated memory.x is protected
The build feature can write the whole memory.x of a firmware from its layout. A second
memory.x that the linker finds first would silently replace it, and the firmware would link
against regions that the layout does not describe. This page explains why that can happen and how
nanoflash detects it. The steps to use the feature are in Generate memory.x and layout
constants.
Why the file must be named memory.x
The linker writer produces nanoflash_memory.x, the MEMORY block of the layout. The link.x of
cortex-m-rt starts with INCLUDE memory.x and uses the regions FLASH and RAM. A
-Tnanoflash_memory.x argument therefore cannot replace the memory.x of the firmware: the file
must be named memory.x and be on the linker search path.
Build::memory(name, origin, length) adds a region that is not a partition (the RAM). generate
then writes the whole memory.x (the regions of the layout, then the regions given) to OUT_DIR,
which generate puts on the search path.
Three checks and one limit
1. A memory.x in the working directory of the linker. The linker looks first in its working
directory. Cargo runs rustc, and so the linker, from the workspace root for a package under it
(path_args in Cargo), and from the package directory otherwise. A memory.x in the workspace
root would win over the generated file. generate refuses a memory.x in the workspace root and
in the package directory when memory was called (BuildError::MemoryScriptShadowed). It finds
the workspace root with the walk of the cargo module (workspace_root: a [workspace] table in
the Cargo.toml itself, package.workspace, or the nearest ancestor with a [workspace] table),
without a cargo metadata process. Without memory, a memory.x that includes
nanoflash_memory.x works as before.
2. A memory.x added after a build. The first check runs only when the build script runs. A
build script that prints rerun-if-changed runs again only when a listed path changes. A path that
does not exist counts as changed on every build, and a directory is scanned with target/ in it.
So the script cannot list the two memory.x paths or the directories without rebuilding the
firmware on every build. Instead:
generatewritesnanoflash_memory_guard.xtoOUT_DIRwhenmemorywas called, and printscargo:rustc-link-arg-bins=-T<that file>.- The
memory.xof nanoflash ends with__nanoflash_memory_x = 1;. The guard isASSERT(__nanoflash_memory_x == 1, "..."). - The guard reads the marker and does not test
DEFINED(__nanoflash_memory_x). rust-lld evaluatesDEFINEDin the order of the scripts on the command line, and the build script of a firmware prints-Tlink.xaftergenerate, soDEFINEDfails a correct link. A read of the marker gives the same result in every order, in rust-lld (measured with thelink.xofcortex-m-rt0.7, withoutflip-link) and in GNU ld (measured on test scripts). - When another
memory.xwas linked, the marker does not exist. Both linkers then fail and name the symbol and the line of the guard, not the message of the assertion. rust-lld reportsnanoflash_memory_guard.x:5: symbol not found: __nanoflash_memory_x, and GNU ld reports the same symbol as unresolved in an expression. The message and a comment in the guard explain the symbol. - The link runs again only when the package changes. A
memory.xadded after a build therefore fails the next link, not the nextcargo buildwithout a change. Build::package()printsrerun-if-changedfor theCargo.tomlof the package too, so a change ofboard =runs the script again.Build::layout(path)lists only the layout file.- A package that does not call
memorygets neither the marker nor the guard, andgenerateremoves a guard and amemory.xthat an earlier build left inOUT_DIR. cargo:rustc-link-arg-binsreaches every binary of the package.memorytherefore requires that every binary links the generatedmemory.x, through thelink.xofcortex-m-rtor a script that includes it. A binary for the host in the same package, or a runtime whose script does not includememory.x, fails to link and names the marker. Such a binary belongs in another package.
3. Invalid region names. A region given to memory is refused when its name is not valid
(TableError::InvalidRegionName, the rule of the linker.region attribute), when an earlier
region given has the same name (TableError::RepeatedRegion), or when it has the name of a region
of the layout (TableError::DuplicateRegion).
The limit: another memory.x on the search path is not checked. HAL crates with a memory-x
feature (embassy-stm32, stm32-metapac) write one to their own output directory, and the order
of the search directories decides which file wins. Turn that feature off when you call memory.