From 4a67c9458e12033ef7ebfab3448257b12a6cc9ac Mon Sep 17 00:00:00 2001 From: esaurez Date: Mon, 8 Jun 2026 13:28:52 -0700 Subject: [PATCH] [libc] F: Weaken _do_start so libnvx_crt0.a can override it cleanly The Nanvix C runtime ships two implementations of `_do_start`: * newlib/libc/sys/nanvix/crt0.S (this file) -- a minimal trampoline that pushes argp/envp and tail-calls `_start`. * Nanvix's `libnvx_crt0.a` (in the nanvix/nanvix repository) -- a stronger replacement that additionally aligns `%esp` to a 16-byte boundary before the CALL instruction. Stack alignment is required by the i386 SysV ABI for SSE-using callees (e.g. anything using `movaps` / `movdqa`). Because both definitions used `.globl _do_start`, every Nanvix guest binary's link pulled the newlib copy (crt files come first in the gcc spec) and silently shadowed the aligned variant. To make the build even succeed, every Nanvix-side Makefile had to pass `-Wl,--allow-multiple-definition`, which hides legitimate symbol collisions. Declaring the newlib copy `.weak` resolves both problems: * Consumers that link `libnvx_crt0.a` get the strong, SSE-aligned override and no longer need `-Wl,--allow-multiple-definition`. * Bare-newlib consumers (no `libnvx_crt0.a` in the link) keep the original behavior because the weak symbol is the only definition visible to the linker. This change is paired with companion work in nanvix/toolchain-gcc to republish the toolchain image with the new newlib, and follow-up cleanup in nanvix/nanvix and the consumer-side Makefiles to drop `-Wl,--allow-multiple-definition` once the new image is in place. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- newlib/libc/sys/nanvix/crt0.S | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/newlib/libc/sys/nanvix/crt0.S b/newlib/libc/sys/nanvix/crt0.S index ca4702887f..c91ddd4f95 100644 --- a/newlib/libc/sys/nanvix/crt0.S +++ b/newlib/libc/sys/nanvix/crt0.S @@ -13,7 +13,33 @@ * Exported Symbols * *============================================================================*/ -.globl _do_start +/* + * `_do_start` is the kernel-entry trampoline that lands here via IRET from + * a freshly-created process trap frame. EDX holds `argp` and ECX holds + * `envp`. This stub pushes both arguments and calls `_start`. + * + * Declared `.weak` so that consumers linking Nanvix's `libnvx_crt0.a` (which + * provides a stronger SSE-aligned `_do_start` that satisfies the i386 SysV + * ABI's 16-byte stack-alignment requirement at the CALL site) cleanly + * override this fallback at link time. Bare-newlib consumers (no + * `libnvx_crt0.a` in the link) still get the original behavior because the + * weak symbol is the only definition the linker sees. + * + * Archive-extraction caveat: standard ld will NOT pull a member out of an + * archive solely to override an already-satisfied weak symbol. For the + * override to take effect, `libnvx_crt0.a`'s `_do_start` member must be + * extracted via some other reference (a co-archive symbol the link + * actually needs) or via `-Wl,--whole-archive`. The current Nanvix + * link lines satisfy this prerequisite; a future refactor that strips + * `libnvx_crt0.a` down to a single-symbol member must re-add a hook + * (an extra reference, or `--whole-archive`) or the broken-for-SSE + * fallback below will silently win again. + * + * This change eliminates the duplicate-strong-symbol clash between + * `crt0.o` and `libnvx_crt0.a` that previously forced every Nanvix guest + * binary's link line to use `-Wl,--allow-multiple-definition`. + */ +.weak _do_start /*============================================================================* * Text Section *