Catching inadvertant divides and mods in CDFJ

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andrew Davie

Working with Claude...

Background: ARM7TDMI (the CDFJ+ coprocessor) has no hardware divide instruction. Any / or % against a value the compiler can't fully constant-fold gets replaced with a call into libgcc's software divide routine (__aeabi_uidivmod, __udivsi3, etc.) — which is not just slow, it's a real correctness risk in a bare-metal/no-libc environment like this. In our case it caused actual crashes, not just a slowdown.

The gotcha: GCC can turn x / CONST into a cheap multiply-by-reciprocal when CONST is known at compile time — but that's a heuristic, not a promise. It can silently decline to do the fold depending on optimization level and, in our case, LTO. We'd been bitten by this before and thought we had a safe way to check for it ("run nm/objdump on the object file and look for div symbols") — but that check is worthless once you're building with -flto: individual .o files under LTO are bitcode, not real machine code, so there's nothing meaningful to grep. The actual instructions only get generated at final link time. We'd been checking the wrong artifact.

The fix — automate the check instead of trusting memory:

Added a rule to the Makefile that runs immediately after the final ARM ELF is linked. It does an nm on the actual linked binary (not an intermediate .o) and greps for the libgcc integer div/mod symbol names:


__aeabi_*div*, __aeabi_*mod*, __udivsi3, __divsi3, __udivdi3, __divdi3, __umodsi3, __umoddi3
If any of those symbols show up in the linked ELF, the build hard-fails right there with an error explaining what happened and printing the offending symbols — instead of silently producing a ROM that happens to call into a nonexistent-on-this-target divide routine and crash at runtime. This check only runs for the ARM7TDMI target; our PlusCart (Cortex-M4) build target is exempted since that chip actually has a hardware divide unit, so a divide there is fine.

Net effect: it's no longer possible to accidentally ship a runtime-divide regression without the build itself telling you immediately, at the moment it's introduced, with the offending symbol names in front of you — rather than finding out later from a mysterious crash.


Here's a minimal, self-contained version stripped of this project's specific variable names — the pattern anyone could drop into their own Makefile:

# ARM7TDMI has no hardware divide unit. Any of these libgcc symbols showing up in the
# final linked ELF means a runtime divide/modulo slipped through -- fail loudly instead
# of shipping a ROM that crashes calling into a divide routine the CPU can't run.
DIVIDE_SYMBOLS = __aeabi_[a-z0-9]*div[a-z0-9]*|__aeabi_[a-z0-9]*mod[a-z0-9]*|__u?divsi3|__u?divdi3|__u?modsi3|__u?moddi3

game.elf: $(OBJS)
    $(CC) $(CFLAGS) -o $@ $(OBJS) $(LDFLAGS)
    @BAD="$$($(NM) $@ | grep -E '$(DIVIDE_SYMBOLS)')"; \
    if [ -n "$$BAD" ]; then \
        echo "ERROR: runtime divide/modulo reached the linked ELF:"; \
        echo "$$BAD"; \
        exit 1; \
    fi

Notes:

Check the linked ELF, not an intermediate .o. If you build with -flto, .o files are LTO bitcode, not real machine code — grepping them for div symbols proves nothing either way. Only the final link reflects what actually got generated.
NM should be your arm-none-eabi-nm (or whatever your cross toolchain calls it).
If you also target a chip with a hardware divide unit (e.g. Cortex-M4 for PlusCart-style carts), gate this rule out for that target — it's only meaningful where divide is actually unavailable/expensive.