consmirror.c: the BSD notice is replaced (Makronix's own file). Files with changes of other sessions pending are left for their commit. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
15 KiB
PORTING-USERLAND.md -- how to port a program of src/ to the phone
How one NetBSD program from our src/ becomes a program on the phone's
Linux (aarch64, glibc). Written on 2026-11-11 while porting the first one,
cat (C1.1): every step below was done, every pitfall was hit. The plan
these ports belong to is NETBSD-USERLAND.md; its section 2 (rules) and
the item tables say what to port and in which order.
1. The idea in one paragraph
NetBSD already builds its own programs for a foreign operating system: for
a Linux build host, build.sh tools compiles nbcat, nbsed, nbmake and
about 70 more with src/tools/Makefile.host, which takes the program's own
src/<dir>/Makefile and links it against libnbcompat (src/tools/compat,
the BSD functions glibc lacks). We use that machinery unchanged and make
the phone its "host": HOST_CC is an aarch64 compiler whose glibc is the
phone's. A port is therefore two lines of Makefile; the work is in the
frame, which exists now, and in the BSD interfaces Linux does not have
(libmxlinux, NETBSD-USERLAND.md section 4), which a program may need.
2. Once per machine: what the frame needs
All of it is made by make userland in mobile/ when it is missing; listed
here so you know what is where and why.
| what | made by | why |
|---|---|---|
Void rootfs build/rootfs-void-<dev>.tar |
make rootfs |
the phone's image: its glibc is what we link against |
sysroot build/sysroot-<dev>/ |
tools/mksysroot.sh (make sysroot) |
the rootfs plus the headers of base/void/sysroot-packages |
tooldir build/tooldir.<host>-aarch64-linux-gnu |
tools/mktooldir.sh (make tooldir, ~30 min) |
build.sh tools of src/ for aarch64-linux-gnu: gcc 14.4.0, binutils 2.46.0, nbmake, nbmtree, install |
| gcc headers, libgcc, libstdc++ in it | tools/mktargetlibs.sh (make targetlibs) |
what build.sh tools leaves to NetBSD's own Makefiles (section 8) |
compiler build/toolchain-<dev>/bin/cc |
tools/mktoolchain.sh (make toolchain) |
the tooldir's gcc with the sysroot flags NetBSD uses |
libnbcompat.a for aarch64 |
make userland step mx-compat |
src/tools/compat built with that compiler, uninstalled |
| destdir hierarchy | make userland step mx-dirs |
userland/mk/mx.dist laid down with nbmtree |
Host packages: a host C/C++ compiler for build.sh, and qemu-user with binfmt_misc registered for aarch64 (ls /proc/sys/fs/binfmt_misc | grep aarch64).
3. Porting one program, step by step
Take PROG = the program's directory name, SRC = its directory under
src/ (bin/cat, usr.bin/sed).
-
Is it free?
sh tools/userland-status.sh. If not listed, claim it: createuserland/PROG/AGENTS.mdwithstate: claimed,agent:,item:(the id from NETBSD-USERLAND.md) and commit just that file (git add -- userland/PROG/AGENTS.md && git commit -m ... -- userland/PROG/AGENTS.md). -
Read first.
src/SRC/PROG.1(or .8),src/SRC/Makefile,src/SRC/../Makefile.inc(it sets BINDIR), the comment at the top of the main .c file, andsrc/tests/SRC/if it exists. Note PROG, SRCS, LDADD, DPADD, CPPFLAGS and anything.if'd on MACHINE or MK*. -
Check what it uses that glibc lacks: the API table in NETBSD-USERLAND.md section 1, or scan it yourself:
grep -h -o -E '\b(sysctl|kvm_[a-z]+|getmntinfo|kqueue|kevent|humanize_number|getbsize|setproctitle|login_cap|devname|chflags|reboot|swapctl|mount)\b' src/SRC/*.c | sort | uniq -c. Everythinglibnbcompatgives (efun, fparseln, strtoi, strmode, setmode, pwcache, vis, fts, reallocarr, strsuftoll, strtonum, errc ...) is free. So is everythinglibmxlinuxgives (lib/mxlinux/include/mxlinux.h: NetBSD's getopt, SIGINFO, timespec macros, sys_signame/signalname, humanize_number, getbsize, fgetwln, LC_C_LOCALE). Both headers are forced into every compile by the frame; a port never includes them. If it needs a B item of NETBSD-USERLAND.md section 4 that is not done, mark the portstate: blocked: B<n>and take another one. -
The Makefile,
userland/PROG/Makefile:# PROG(1) for the phone, from src/SRC (docs/PORTING-USERLAND.md) MX_SRCDIR= SRC .include "../mk/mx.prog.mk"Anything the port needs goes here, before the include, with a comment that says why:
CPPFLAGS+=,LDADD+=, aSRCS:=that drops a NetBSD-only source file. Never editsrc/SRC/Makefilefor the phone. -
Build:
make userland PROG=PROG. Read the log from the bottom; the build stops at the first error. Fix in this order: flags in your Makefile; the frame (userland/mk, only if every port needs it); libnbcompat (src/tools/compat, shared with the desktop's tools build); libmxlinux; last,src/SRCitself, behind#ifdef __linux__, keeping the NetBSD build green. -
mxcheck runs by itself after the link: the binary must be aarch64, every library it needs must be in the sysroot, and no symbol version it needs may be newer than the sysroot's library defines. It failing is a real problem (section 5.3), not a check to switch off.
-
Test (
userland/PROG/test.sh CMD DATADIR): first the cases of NetBSD's own testsrc/tests/SRC/t_*.sh, translated fromatf_checkto plain sh with its data files (atf is not on the phone yet, C-T1); then the main options of the man page. PrintPASS name/FAIL name, end withPROG: N failed, exit non-zero on a failure.userland/cat/test.shis the model.- on the host:
make userland-test PROG=PROG QEMU=1 - on the phone:
make userland-test PROG=PROG(copies to/tmp/userland-test/PROG, never into /system)
- on the host:
-
REPLACES: the Void files the port supersedes and their package, one
path<TAB>packagea line (xbps-query -o /usr/bin/PROGon the phone names the package). -
AGENTS.md of the port:
state: done, and the evidence -- the log files, mxcheck's line, the test's PASS count under qemu and on the phone, anything you had to change and why. -
Commit the port's directory with explicit paths. One port, one commit, unless a frame change is needed -- then that is its own commit first.
4. What the frame does (so you can debug it)
make userland in mobile/ → tools/mxmake.sh (environment) →
userland/Makefile (steps) → userland/PROG/Makefile →
userland/mk/mx.prog.mk → src/tools/Makefile.host → src/SRC/Makefile +
bsd.hostprog.mk.
- tools/mxmake.sh is to the phone what build.sh's
nbmake-amd64is to the desktop: the same variables (NETBSDSRCDIR, TOOLDIR, USETOOLS=yes, MKUNPRIVED,-m src/share/mk), plusHOST_CC= the phone's compiler,MAKEOBJDIR=build/userland/<dev>/obj/<path in the repo>,DESTDIR=build/userland/<dev>/destdir,QEMU_LD_PREFIX= the sysroot, and two command-line overrides (INSTALL,HOST_MKDEP, section 5.6/5.7). - mx.prog.mk sets
HOST_SRCDIR=../src/${MX_SRCDIR}(section 5.8), turns off the host's libnbcompat (NOCOMPATLIB) and includes ours uninstalled (COMPATLIB_UNINSTALLED=yes+ itsdefs.mk), maps BINDIR onto Void's layout (/bin,/sbin,/usr/sbin→/usr/bin), setsINSTPRIVfor an unprivileged install, and hangs mxcheck ontorealall. - Objects:
build/userland/<dev>/obj/mobile/userland/PROG/; the compat library:build/userland/<dev>/obj/src/tools/compat/libnbcompat.a. - Installed:
build/userland/<dev>/destdir/usr/bin/PROG, man pages underusr/share/man/manN/, owners and modes of what bsd.man.mk/bsd.files.mk install indestdir/METALOG. The program itself is installed by Makefile.host's own rule (mode 555, no METALOG line).
5. Pitfalls met on the way (2026-11-11), and what fixed them
- (History, before the tooldir.) The cross gcc's own glibc wins over --sysroot. Debian's
aarch64-linux-gnu-gcc --sysroot=Sstill took headers,Scrt1.oandlibc.sofrom/usr/aarch64-linux-gnu(glibc 2.42) ---print-file-name=libc.soand-Wl,--verboseshowed it; the phone has glibc 2.41. Fix intools/mktoolchain.sh:-nostdinc -isystem <gcc include> -isystem S/usr/include -B S/usr/lib/ -L S/usr/lib. Check after any toolchain change:cc -Wl,--verbose ... | grep succeededmust show libc and crt1 from the sysroot. - (History.) clang --sysroot is strict but incomplete here: it then needs
crtbeginS.oandlibgccinside the sysroot, which the image has not. Not used. - The sysroot can drift from the image. Installing
zlib-develinto the sysroot upgraded zlib 1.3.1 → 1.4 (Void's repository rolls); a program linked against zlib 1.4 symbols would not run on the phone. mxcheck catches that per program. Cure when it hits: rebuild the image (make rootfs) so both have the same versions, or link the program without that library. - configure cannot run its test programs ("cannot run C compiled
programs"): binfmt hands the aarch64 binary to qemu, which looks for
/lib/ld-linux-aarch64.so.1on the host.QEMU_LD_PREFIX=<sysroot>(set by mxmake.sh) points it at the phone's dynamic linker. With it, configure says "cross compiling... no" and its run-time checks see the phone's glibc -- better answers than a cross configure would guess. - libnbcompat's defs.mk links
-lrt -lzinto every program (its configure found them). Harmless: the linker records only libraries that are used (cat needs libc.so.6 only); mxcheck would see a stray one. - host-mkdep "not found" in src/tools/compat: its Makefile (line 163)
wants an uninstalled host-mkdep in its own object directory. mxmake.sh
passes
HOST_MKDEP=on the command line (a command-line variable beats a Makefile assignment) pointing at the tooldir'snbhost-mkdep. aarch64--netbsd-install: not found: bsd.own.mk:482 names install after MACHINE_GNU_PLATFORM. Since the phone has its own tooldir, mxmake.sh setsMACHINE_GNU_PLATFORM=aarch64-linux-gnuand the name is right by itself (before, borrowing the desktop's tooldir, it passedINSTALL=on the command line).- Makefile.host finds the program relative to itself:
.PROGDIR:= ${.CURDIR}/../../${HOST_SRCDIR}-- right for src/tools/PROG, wrong for mobile/userland/PROG.HOST_SRCDIR=../src/SRClands on src/SRC from there; nothing in src/ changed. - The man page went for the host's /usr/share/man as root🛞
without DESTDIR, bsd.man.mk installs to the real path. It failed only
because the host has no group wheel; nothing was written (checked).
mxmake.sh sets
DESTDIR; never run a port'smake installwithout mxmake.sh. - "unknown group wheel" with DESTDIR set: bsd.own.mk:1765 sets the
unprivileged
INSTPRIV(-U -M METALOG -N src/etc) only for directories inside NETBSDSRCDIR. mx.prog.mk sets it. - install: mkstemp: No such file or directory: NetBSD's install(1)
creates no directories; build.sh lays the tree down first (distrib-dirs,
src/etc/mtree/NetBSD.dist). Ours:
userland/mk/mx.dist, step mx-dirs,nbmtree -N src/etc -deU(without -N: "unknown group wheel" again). A port that installs into a directory not in mx.dist: add the directory there (frame change, own commit). - HTML man pages were formatted (cat.html1) and would be installed:
MKHTML=noin mxmake.sh.
6. What a finished port looks like (cat)
mobile/userland/cat/Makefile MX_SRCDIR= bin/cat + include
mobile/userland/cat/test.sh t_cat.sh's 3 cases + 2 of the man page
mobile/userland/cat/REPLACES /usr/bin/cat coreutils
mobile/userland/cat/AGENTS.md state: done, the evidence
$ make userland PROG=cat → make userland exit=0
$ make userland-test PROG=cat QEMU=1 → cat: 0 failed
$ make userland-test PROG=cat → 5 PASS on the phone
7. What the frame does not do yet
- Remove what a port REPLACES from the Void side (D1/D4): today a port's file simply overwrites Void's at the same path; the Void package stays installed and its other files with it.
- libmxlinux (A9, B items): the first port that needs humanize_number, getmntinfo, sysctl ... starts it.
- C++ programs: mktoolchain.sh makes no c++ (Debian's libstdc++ headers are not the phone's).
- atf on the phone (C-T1); until then test.sh carries NetBSD's cases.
- libmxlinux has what the C1 ports needed; the B items of NETBSD-USERLAND.md section 4 that need kernel interfaces (getmntinfo, sysctl, kvm, kqueue, login_cap, reboot, mount) are still open.
7a. Into the image (A7, 2026-11-11)
make roottar / make rootimg now carry the userland: tools/lib.sh
userland_stage copies build/userland//destdir (without METALOG) and
our libstdc++.so.6.0.33 + libgcc_s.so.1 (stripped, from the tooldir), and
refuses if a program there belongs to a port that is not state: done;
assemble-rootfs.sh lays it over the Void packages, before base/ and soc/.
A port's program replaces Void's file at the same path (Void is usr-merged,
so /bin/cat is /usr/bin/cat); /usr/lib/libstdc++.so.6 points at ours.
So after make userland, make update-root puts the NetBSD programs on
the phone; /system/previous keeps the tree before.
Behaviour that differs from what Void had, met on the phone: NetBSD's
head rejects -c 0 ("illegal byte count"), GNU's prints nothing. [ is
NetBSD's too since the frame installs LINKS (hard link of test).
8. The toolchain: build.sh tools for Linux (2026-11-11)
make tooldir runs build.sh tools, unchanged, with
-m evbarm64 -D <sysroot> -V MACHINE_GNU_PLATFORM=aarch64-linux-gnu
(bsd.own.mk:1065 sets it with ?=). gcc is configured as NetBSD configures
its own (src/tools/gcc/Makefile: languages c c++ objc fortran -- no Ada --,
--with-sysroot, gmp/mpfr/mpc/isl from the tooldir, --disable-nls, the
cortex-a53 fixes, the new libstdc++ ABI). make targetlibs does what
NetBSD does elsewhere, and four NetBSD particulars had to be met there:
- gcc's own headers (stddef.h, stdarg.h, limits.h, the aarch64
intrinsics) are not copied: NetBSD's gcc/Makefile.in:3308 reads
if false; then, because NetBSD's libc ships them. mktargetlibs.sh copies the same files the same way, the lists from gcc's own Makefile. - -DNETBSD_TOOLS drops the sysroot's /lib and /usr/lib from gcc's
startfile search (gcc.cc:8605,8615): "ld: cannot find crti.o". NetBSD
passes
--sysroot -L${DESTDIR}/lib -L${DESTDIR}/usr/lib -B${DESTDIR}/usr/lib/(src/tools/gcc/Makefile:162); so do mktargetlibs.sh and mktoolchain.sh. - NetBSD's libstdc++ import is pruned for its own Makefiles: Makefile.in:424
has no
srcin SUBDIRS (Makefile.am:30 has), andpois not imported. The subdirectories are built one by one; three include paths of NetBSD's lib/libstdc++-v3/Makefile:127-129 are given to src. - NetBSD's locale_facets_nonio.h:339 declares a specialization
__timepunct<wchar_t>::~__timepunct()that glibc's "gnu" locale model does not define ("undefined symbol: _ZNSt11__timepunctIwED1Ev" at run time); NetBSD's own model (dragonfly) needs <xlocale.h> and BSD *_l functions glibc lacks. libstdc++ is configured with --enable-clocale=generic: C++ locales know only "C". A finding.
Measured: C and C++ build and run under qemu against the phone's glibc 2.41 with libstdc++.so.6.0.33 of gcc 14.4.0 (the phone has Void's 6.0.32, gcc 13: C++ programs need ours, A7). All 28 ports of 2026-11-11 rebuilt with it, 28/28 tests pass under qemu and on the phone (build/logs/userland-{qemu,phone}-all-2026-11-11.log).