Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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:

  • generate writes nanoflash_memory_guard.x to OUT_DIR when memory was called, and prints cargo:rustc-link-arg-bins=-T<that file>.
  • The memory.x of nanoflash ends with __nanoflash_memory_x = 1;. The guard is ASSERT(__nanoflash_memory_x == 1, "...").
  • The guard reads the marker and does not test DEFINED(__nanoflash_memory_x). rust-lld evaluates DEFINED in the order of the scripts on the command line, and the build script of a firmware prints -Tlink.x after generate, so DEFINED fails a correct link. A read of the marker gives the same result in every order, in rust-lld (measured with the link.x of cortex-m-rt 0.7, without flip-link) and in GNU ld (measured on test scripts).
  • When another memory.x was 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 reports nanoflash_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.x added after a build therefore fails the next link, not the next cargo build without a change.
  • Build::package() prints rerun-if-changed for the Cargo.toml of the package too, so a change of board = runs the script again. Build::layout(path) lists only the layout file.
  • A package that does not call memory gets neither the marker nor the guard, and generate removes a guard and a memory.x that an earlier build left in OUT_DIR.
  • cargo:rustc-link-arg-bins reaches every binary of the package. memory therefore requires that every binary links the generated memory.x, through the link.x of cortex-m-rt or a script that includes it. A binary for the host in the same package, or a runtime whose script does not include memory.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.