Symbols and the dependency contract

Symbols and the dependency contract

Apps link -nostdlib. Nothing ships with the binary that satisfies a reference; at load time the host resolves every undefined symbol from its own live memory. The app’s undefined-symbol list is therefore its real dependency contract. make check prints it; pm check interprets it against the host surface.

The surface

An app can call symbols exposed by the loader’s enabled export tables. A global symbol in firmware.elf is not an export by itself. The generated customer table is controlled by these SDK-root lists:

  • symbols.list - the PocketMage pm_* ABI plus a handful of host primitives. Its pm_* block is generated; do not hand edit it (see below).
  • host_baseline.list - the selected C/C++ runtime and newlib symbols emitted into the customer table. Generated by symbols.py --write-baseline; do not hand edit it. Availability still depends on the compiled firmware configuration.
  • host_exclusions.list - symbols deliberately withheld from the export surface: the raw stdio, dirent and POSIX file calls plus their newlib reentrant forms. All card access goes through the pm_sd_* gateway API, so an app referencing an excluded symbol fails the gate and the load. Exclusions are applied at generation (--exclude-file) and the baseline is rewritten to match (--write-baseline), so regenerating the table can never silently restore them. Kept on the surface: stdout-only calls (fflush, setvbuf) and the newlib-internal plumbing printf chains need (_fstat_r, _write_r, _read_r, _lseek_r, _putc_r, _fflush_r, _sfread_r, __seofread), none of which can reach the card once fopen/open are unobtainable.

Without --host-elf, the pm gate uses the curated lists and generated table as a provisional surface. With --host-elf, only entries in the firmware’s compiled loader registry count. The registry is the one the loader’s default resolver elf_find_sym_default() searches, so disabled tables and unexported global symbols cannot produce false passes. The first enabled table containing a name wins. Firmware without this registry must be rebuilt before running an exact check. Runtime-registered symbols cannot be confirmed statically.

The C and C++ runtime. libc, libm, libgcc, libstdc++ and libsupc++ symbols the firmware already links are exported so apps can use third-party libraries, and host_baseline.list records them. tools/symbols.py derives this set by reading the toolchain archives with nm and keeping the symbols that are present in the firmware image:

python3 tools/symbols.py --list symbols.list --baseline host_baseline.list \
  --host-elf <firmware.elf> --cpp \
  --include-header pm_app_api.h --include-header pm_sdk_app.h \
  --output-file ../../src/ELF_SYMS/esp_all_symbol.cpp \
  --exclude-file host_exclusions.list \
  --write-baseline host_baseline.list \
  --toolchain-lib-dir <toolchain>/xtensa-esp-elf/lib/esp32s3/no-rtti \
  --framework-lib-dir <framework-libs>/esp32s3/lib \
  --print-cost
libc        252 symbols   4,501 bytes
libm         18 symbols     303 bytes
libstdc++   103 symbols   4,229 bytes
libsupc++    58 symbols   2,362 bytes
libgcc       65 symbols   1,552 bytes
libnewlib   100 symbols   2,669 bytes
total       735 symbols  19,193 bytes (18.7 KB)

--toolchain-lib-dir must point at the full newlib archives (xtensa-esp-elf/lib/<target>/no-rtti), not the picolibc stubs under picolibc/...: the picolibc libm.a is empty and its libc.a omits the dtoa and stdio-reentrancy helpers. libgcc.a is found by ascending to the toolchain root, so any lib dir below it resolves.

--baseline host_baseline.list folds the recorded baseline back in as runtime externs. --exclude-file still removes the denied file APIs.

That total includes curated SDK entries. It estimates table and name storage, not the complete linked code cost, and is paid once by the firmware. Counts change with the linked runtime. Rebuild after regeneration before running an exact check against that firmware.

A symbol in symbols.list that the firmware does not define is reported as a warning but still emitted into the table. A newly added wrapper cannot appear in --host-elf until the host is rebuilt, so dropping it would omit it on the one run that adds it. Regenerate once more after building and the warning should be gone.

Runtime export is on by default. Pass --no-runtime to export only the curated SDK list, which restricts apps to the SDK surface and no libraries.

Membership is decided by which archive a symbol comes from, not by its name. A _ZN... prefix also matches the SDK’s own C++ classes, so matching on prefix shape would export firmware internals by accident.

GCC reserves __atomic_*, __sync_* and __builtin_* names as builtins and rejects taking their address. Those exports are declared under a private pm_elfsym_ identifier with an asm label, then listed by their literal name in the table.

The curated SDK table. symbols.list is PocketMage’s own export promise. Its pm_* block is generated by tools/gen_app_api.py, which walks the SDK headers, projects each C++ method to a C wrapper, and writes the wrapper names into both symbols.list (SDK side) and src/ELF_SYMS/host_exports.list (OS side) so the two can never drift. tools/symbols.py is a different tool: it derives the runtime baseline above from the toolchain archives. Host-side, host_exports.list tracks a superset of symbols.list, validated against the firmware image. Both lists feed the same generated g_customer_elfsyms table.

Do not hand edit the pm_* block of symbols.list. To change the app-facing surface, change the SDK header and regenerate with gen_app_api.py; it rewrites the generated files together.

Linking a third-party library

A vendored library’s source is compiled into the app by overriding APP_SRCS. Anything the library references that is not in the app is resolved by the host at load time. Two consequences are worth knowing.

Libraries compiled at -Os need fewer symbols than the same source at -O0. A std::vector<std::string> app at -O0 wants 13 symbols including std::allocator<char> constructors; at -Os it wants 7, and all 7 resolve.

sscanf is not exported by default. It exists in newlib, but nothing in the firmware references it, so it was dropped by --gc-sections, and referencing it pulls in about 190 KB of stdio reentrancy. A library that needs it must either avoid it or the host must export it deliberately.

Reading an app’s contract

make check   # prints entry + every undefined symbol
pm check     # classifies each symbol and exits non-zero on a broken contract

Every import must resolve through the loader. pm check --host-elf verifies the compiled static tables in that exact firmware, not its general symbol table. Dynamically registered exports and shared objects cannot be proven available by this offline check.

Without a host ELF, the generated table and curated lists are a provisional check. They can detect missing entries but cannot prove that the table was enabled and linked into the deployed firmware. Use the matching firmware.elf for release verification.

Editing the surface

Rules for SDK authors:

  • Additions only. Removing or renaming an exported symbol breaks every app that references it at runtime.
  • Every added SDK symbol must exist as a GLOBAL in the firmware image and be added to both symbols.list and the host’s host_exports.list, then regenerate the host table with tools/symbols.py --host-elf ....
  • Regenerate the host table whenever the firmware’s linked symbol set changes. symbols.py only exports runtime symbols the firmware already links, so a firmware that starts using a new libc or libstdc++ function makes it available on the next regeneration.
  • Run pm exports before committing a surface change. It reconciles symbols.list against host_exports.list and fails when a curated symbol is not exported.
  • Bump pocketmage_sdk_version when the surface changes. It is exported, and stored so the OS can version apps against the surface.

Exporting a broad slice of the firmware binds the ABI to all of it. That is a deliberate trade here: it is what lets apps use libraries.

Per-app scope

The broad surface is the default, but an app can be narrowed to a subset of the pm_* surface through the scope field in its app.properties manifest (default all). At load time the runner parses the scope and installs a scoped resolver: a pm_* symbol outside the app’s scope resolves to nothing, so the load fails fast with ELF needs symbols outside its scope rather than letting the app reach an API it did not declare. Only pm_* symbols are gated; the runtime baseline (libc, libstdc++, ESP-IDF) stays reachable for any scope, so libraries keep working. See app-abi.md for the manifest fields and the runner’s resolver behavior.

Versioning and gating

pocketmage_sdk_version is exported so apps can adapt to surface changes. pm check gates an app against the host export set before release: the mirror of make check, with the generated table as the reference when no host ELF is available.

Ship a new version with pm release <major|minor|patch> (add --message TEXT for a CHANGELOG bullet). It validates VERSION, the pocketmage_sdk_version literal in pocketmage_globals.cpp, pocketmage_app_version.h, and library.json first, then rewrites all four and folds an [N.N.N] header at the top of CHANGELOG.md. No write happens if any file disagrees. The command stages nothing in git; commit the four files and the changelog together.