From 1fadd0e772468d33c423ecbe737ec2391ae1a364 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sun, 12 Apr 2026 14:59:35 +0200 Subject: [PATCH] builder: use target-specific -march for RISC-V library compilation The -march flag for compiling C libraries (compiler-rt, picolibc) was hardcoded to rv32imac for all riscv32 targets. This is incorrect for targets that lack the atomic extension, such as ESP32-C3 (rv32imc) and TKey (rv32iczmmul). Extract the -march value from the target's cflags when available, falling back to the previous defaults (rv32imac, rv64gc) for targets that don't override it. Fixes #5114 Signed-off-by: deadprogram --- builder/library.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/builder/library.go b/builder/library.go index 94ad5a768..73fa2545f 100644 --- a/builder/library.go +++ b/builder/library.go @@ -167,9 +167,9 @@ func (l *Library) load(config *compileopts.Config, tmpdir string) (job *compileJ // double. args = append(args, "-mdouble=64") case "riscv32": - args = append(args, "-march=rv32imac", "-fforce-enable-int128") + args = append(args, "-march="+riscvMarch(config, "rv32imac"), "-fforce-enable-int128") case "riscv64": - args = append(args, "-march=rv64gc") + args = append(args, "-march="+riscvMarch(config, "rv64gc")) case "mips": args = append(args, "-fno-pic") } @@ -298,3 +298,17 @@ func (l *Library) load(config *compileopts.Config, tmpdir string) (job *compileJ once.Do(unlock) }, nil } + +// riscvMarch returns the -march value for RISC-V library compilation. +// It extracts the value from the target's cflags if present, otherwise +// falls back to the provided default. This ensures libraries are compiled +// with the correct ISA extensions for each target (e.g. rv32imc for +// ESP32-C3 which lacks the atomic extension). +func riscvMarch(config *compileopts.Config, defaultMarch string) string { + for _, flag := range config.Target.CFlags { + if strings.HasPrefix(flag, "-march=") { + return flag[len("-march="):] + } + } + return defaultMarch +}