tools/nxflat: Add an Apache-licensed NXFLAT converter.

ldnxflat is the last piece of the NXFLAT toolchain that NuttX cannot carry.
It descends from elf2flt through four sets of copyright holders, so it is GPL
by that descent and not merely by its libbfd dependency.  This is a new
implementation, written from include/nxflat.h, from what binfmt/libnxflat does
with the container, and from the ELF specification.  The relocation arithmetic
is that of libs/libc/machine/arm/armv7-m/arch_elf.c, which the ELF loader runs
on the target for the same relocations, and which brings R_ARM_TARGET1 with
it.

NXFLAT is not an ARM format.  Its loader only adds a base to a 32-bit word, so
the segments, the GOT, the relocation records and the header are common to
every architecture.  An architecture supplies a table entry, an entry-point
convention and a relocation handler; an object for a machine with no entry is
refused by name.

The GOT is built here, because ld -r emits none: one entry per symbol that a
GOT-relative reference names, at the start of D-Space, each with a relocation
record of its own.  An entry may hold a function, which is what makes a
function pointer reached through the GOT work.

Two defects of the out-of-tree tool do not survive.  A GOT entry naming a .bss
object lost its section's address and pointed at the start of D-Space, the GOT
itself, so on lm3s6965-ek:qemu-nxflat the longjmp test panics with PC 0 and
the five tests after it never run.  The alignment gap before .bss went missing
from h_bssend as well, leaving D-Space short.

The tool is built and named like the rest of the toolchain.  Makefile.host
builds it, Unix.mk makes a configuration that sets CONFIG_NXFLAT depend on it
beside mknxflat, and LDNXFLAT points at the tool in the tree rather than one
on PATH.

All eight C modules of apps/examples/nxflat/tests convert and run to
completion under qemu-system-arm -M lm3s6965evb.

Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
This commit is contained in:
Marco Casaroli 2026-09-27 11:56:25 +02:00 • committed by Alan C. Assis
parent 31acf10057
commit c9cd9db25f
5 changed files with 1270 additions and 8 deletions

View file

@ -601,12 +601,12 @@ ifeq ($(CONFIG_NXFLAT),y)
MKNXFLAT ?= $(TOPDIR)$(DELIM)tools$(DELIM)mknxflat$(HOSTEXEEXT) -a arm
endif
# ldnxflat is still an out-of-tree tool, so this is a PATH lookup. It is
# named here only so that a board which never assigned it -- lm3s6965-ek
# is one -- does not expand it to nothing and hand make a recipe starting
# with '-e', whose leading dash make then eats as "ignore errors".
# Both tools are in the tree. Naming ldnxflat here also keeps a board that
# never assigned it -- lm3s6965-ek is one -- from expanding it to nothing
# and handing make a recipe that starts with '-e', whose leading dash make
# then eats as "ignore errors".
LDNXFLAT ?= ldnxflat
LDNXFLAT ?= $(TOPDIR)$(DELIM)tools$(DELIM)ldnxflat$(HOSTEXEEXT)
endif
# --fixed-r9 reserves r9 across the base firmware so a callback into module

1
tools/.gitignore vendored
View file

@ -7,6 +7,7 @@
/gencromfs
/initialconfig
/mkconfig
/ldnxflat
/mknxflat
/mkdeps
/cnvwindeps

View file

@ -47,7 +47,7 @@ ifdef HOSTEXEEXT
.PHONY: b16 bdf-converter cmpconfig clean configure kconfig2html mkconfig \
mkdeps mksymtab mksyscall mkversion mkpasswd cnvwindeps nxstyle \
initialconfig gencromfs convert-comments lowhex detab rmcr incdir \
mknxflat
mknxflat ldnxflat
endif
ifdef HOSTDYNEXT
.PHONY: jlink-nuttx
@ -136,16 +136,23 @@ mksymtab: mksymtab$(HOSTEXEEXT)
endif
# mknxflat - Generate the thunk file for an NXFLAT module
# ldnxflat - Convert an ELF object into an NXFLAT module
MKNXFLAT_SRCS = nxflat/mknxflat.c nxflat/thunk_arm.c nxflat/thunk_thumb2.c \
nxflat/nxflat_elf.c
LDNXFLAT_SRCS = nxflat/ldnxflat.c nxflat/nxflat_elf.c
mknxflat$(HOSTEXEEXT): $(MKNXFLAT_SRCS)
$(Q) $(HOSTCC) $(HOSTCFLAGS) -Inxflat -o mknxflat$(HOSTEXEEXT) \
$(MKNXFLAT_SRCS)
ldnxflat$(HOSTEXEEXT): $(LDNXFLAT_SRCS)
$(Q) $(HOSTCC) $(HOSTCFLAGS) -Inxflat -o ldnxflat$(HOSTEXEEXT) \
$(LDNXFLAT_SRCS)
ifdef HOSTEXEEXT
mknxflat: mknxflat$(HOSTEXEEXT)
ldnxflat: ldnxflat$(HOSTEXEEXT)
endif
# bdf-converter - Converts a BDF font to the NuttX font format
@ -286,6 +293,8 @@ clean:
$(call DELFILE, mkconfig.exe)
$(call DELFILE, mknxflat)
$(call DELFILE, mknxflat.exe)
$(call DELFILE, ldnxflat)
$(call DELFILE, ldnxflat.exe)
$(call DELFILE, mkdeps)
$(call DELFILE, mkdeps.exe)
$(call DELFILE, mksymtab)

View file

@ -289,6 +289,9 @@ tools/mkpasswd$(HOSTEXEEXT):
tools/mknxflat$(HOSTEXEEXT):
$(Q) $(MAKE) -C tools -f Makefile.host mknxflat$(HOSTEXEEXT)
tools/ldnxflat$(HOSTEXEEXT):
$(Q) $(MAKE) -C tools -f Makefile.host ldnxflat$(HOSTEXEEXT)
# .dirlinks, and helpers
#
# Directories links. Most of establishing the NuttX configuration involves
@ -673,12 +676,14 @@ ifeq ($(CONFIG_BOARD_ETC_ROMFS_PASSWD_ENABLE),y)
PASSWD_TOOL_DEP += tools/mkpasswd$(HOSTEXEEXT)
endif
# mknxflat generates the import thunks for an NXFLAT module, so it is only
# needed by a configuration that builds them.
# mknxflat generates the import thunks for an NXFLAT module and ldnxflat
# converts it, so both are needed by a configuration that builds them, and by
# no other.
NXFLAT_TOOL_DEP =
ifeq ($(CONFIG_NXFLAT),y)
NXFLAT_TOOL_DEP += tools/mknxflat$(HOSTEXEEXT)
NXFLAT_TOOL_DEP += tools/ldnxflat$(HOSTEXEEXT)
endif
pass1dep: context tools/mkdeps$(HOSTEXEEXT) tools/cnvwindeps$(HOSTEXEEXT) $(PASSWD_TOOL_DEP) $(NXFLAT_TOOL_DEP)

1247
tools/nxflat/ldnxflat.c Normal file

File diff suppressed because it is too large Load diff