nuttx-apps/examples/nxflatxip/module/Makefile
Marco Casaroli 2492317f15 examples/nxflatxip: Run an NXFLAT module in place out of xipfs.
The point of a writable execute-in-place file system is that a module can
arrive after the firmware was built and still run without being copied into
RAM.  This demonstrates exactly that, end to end: it writes an NXFLAT module
into xipfs the way a download would, declaring the size up front so the
extent is exact, checks the file system can hand out a real flash pointer
for it, and runs two instances concurrently.

Both instances report the address of their own text and of their stack.  The
text address is the same in both and is inside the flash window -- it is the
address the file occupies on the media -- while the stacks differ.  While
they run, the extent carries one pin per instance, which is what stops the
defragmenter relocating code that is executing.

Two subcommands cover the file system rather than the module.  bench times a
bulk read out of the mapped flash before and after a write, which on the
RP2350 is the cost of whichever path the driver used to restore XIP.  defrag
fills the volume, deletes every other file until an allocation genuinely
cannot be satisfied, compacts, retries the same allocation, then verifies
every relocated file byte for byte and again after a remount, printing a
block map at each step.

The module has neither static data nor string constants, and reports through
a callback into the firmware instead of formatting its own output.  That is
not stylistic: the ldnxflat in circulation resolves the GOT entries for .bss
objects into the import table, and drops the addend when it resolves the
PC-relative references to .rodata, so a module using either quietly computes
wrong addresses.  The comment in module/xipmod.c records both.  The callback
also exercises the other direction of the interface, firmware code entered
with the module's data base still live in r10.

Assisted-by: Claude Code:claude-opus-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
2026-07-28 16:47:49 +08:00

94 lines
2.8 KiB
Makefile

############################################################################
# apps/examples/nxflatxip/module/Makefile
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
include $(APPDIR)/Make.defs
# The NXFLAT module the demo downloads into xipfs. Built exactly the way
# apps/examples/nxflat builds its test programs, so it needs the same two
# host tools from the NuttX toolchain: mknxflat and ldnxflat.
BIN = xipmod
R1SRCS = $(BIN).c
R1OBJS = $(R1SRCS:.c=.o)
R2SRC = $(BIN)-thunk.S
R2OBJ = $(R2SRC:.S=.o)
EXAMPLDIR = $(APPDIR)$(DELIM)examples$(DELIM)nxflatxip
BINHDR = $(EXAMPLDIR)$(DELIM)$(BIN)_bin.h
SYMTAB = $(EXAMPLDIR)$(DELIM)symtab.c
all: $(BINHDR) $(SYMTAB)
.PHONY: all clean
$(R1OBJS): %.o: %.c
@echo "CC: $<"
$(Q) $(CC) -c $(CPICFLAGS) $< -o $@
$(R2OBJ): %.o: %.S
@echo "AS: $<"
$(Q) $(CC) -c $(CPICFLAGS) $< -o $@
$(BIN).r1: $(R1OBJS)
@echo "LD: $<"
$(Q) $(LD) $(NXFLATLDFLAGS1) -o $@ $^
$(R2SRC): $(BIN).r1
@echo "MK: $<"
$(Q) $(MKNXFLAT) -o $@ $^
$(BIN).r2: $(R2OBJ)
@echo "LD: $<"
$(Q) $(LD) $(NXFLATLDFLAGS2) -o $@ $(R1OBJS) $(R2OBJ)
$(BIN): $(BIN).r2
@echo "LD: $<"
$(Q) $(LDNXFLAT) $(LDNXFLATFLAGS) -o $@ $^
# Embed the module in the demo. It is not the demo's own text, it is a
# payload the demo writes into the filesystem, so a plain byte array is what
# is wanted.
$(BINHDR): $(BIN)
@echo "GEN: $@"
$(Q) xxd -i -n g_xipmod $(BIN) | \
sed -e "s/^unsigned char/static const unsigned char/" \
-e "s/^unsigned int/static const unsigned int/" >$@.tmp
$(Q) $(call TESTANDREPLACEFILE, $@.tmp, $@)
# The symbols the module imports from the firmware. mknxflat left the list
# in the thunk file it generated, so that is what is scanned.
$(SYMTAB): $(R2SRC)
@echo "GEN: $@"
$(Q) $(APPDIR)$(DELIM)tools$(DELIM)mksymtab.sh . g_nxflat >$@.tmp
$(Q) $(call TESTANDREPLACEFILE, $@.tmp, $@)
clean:
$(call DELFILE, $(BIN))
$(call DELFILE, $(R2SRC))
$(call DELFILE, *.r1)
$(call DELFILE, *.r2)
$(call DELFILE, $(BINHDR))
$(call DELFILE, $(SYMTAB))
$(call CLEAN)