refactor: libc - share printf, split the float path, fix relink deps

This commit is contained in:
2026-08-04 17:29:24 +02:00
parent edc3452d61
commit 75d92d560c
39 changed files with 388 additions and 586 deletions
+26 -4
View File
@@ -10,9 +10,11 @@ TOOLCHAIN_PREFIX := $(shell cd ../../.. && pwd)/toolchain/local/bin/x86_64-monta
ifneq ($(wildcard $(TOOLCHAIN_PREFIX)gcc),)
CC := $(TOOLCHAIN_PREFIX)gcc
AR := $(TOOLCHAIN_PREFIX)ar
LD := $(TOOLCHAIN_PREFIX)ld
else
CC := gcc
AR := ar
LD := ld
endif
# ---- Paths ----
@@ -63,16 +65,25 @@ CRT_CFLAGS := \
# ---- Target ----
TARGET := liblibc.a
SDK_TARGET := liblibc-full.a
CRT_OBJS := crt1.o crti.o crtn.o
.PHONY: all clean
all: $(TARGET) $(CRT_OBJS)
all: $(TARGET) $(SDK_TARGET) $(CRT_OBJS)
$(TARGET): $(OBJDIR)/libc.o $(ASM_OBJS)
# printf_float.o is deliberately a SEPARATE archive member: a weak undefined
# reference does not extract an archive member, so programs that never format
# a float leave the decimal-conversion and pow/exp/log code behind. Programs
# that do need %e/%f/%g must link with -Wl,-u,_pf_putfloat to force it in.
$(TARGET): $(OBJDIR)/libc.o $(OBJDIR)/printf_float.o $(ASM_OBJS)
$(AR) rcs $@ $^
$(OBJDIR)/libc.o: libc.c Makefile
$(OBJDIR)/libc.o: libc.c printf_internal.h Makefile
@mkdir -p $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/printf_float.o: printf_float.c printf_internal.h Makefile
@mkdir -p $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
@@ -80,6 +91,17 @@ $(OBJDIR)/setjmp.o: setjmp.S Makefile
@mkdir -p $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
# Variant shipped in the on-OS sysroots (0:/sdk/lib/libc.a and tcc's), where
# nobody can pass -Wl,-u,_pf_putfloat by hand. `ld -r` binds vsnprintf's weak
# reference to the float module at merge time, so %e/%f/%g just work for code
# compiled ON MontaukOS. Function-level --gc-sections still applies to the
# rest; only the float path is pinned. Image size is not a concern here.
$(SDK_TARGET): $(OBJDIR)/libc-merged.o $(ASM_OBJS)
$(AR) rcs $@ $^
$(OBJDIR)/libc-merged.o: $(OBJDIR)/libc.o $(OBJDIR)/printf_float.o
$(LD) -r -o $@ $(OBJDIR)/libc.o $(OBJDIR)/printf_float.o
crt1.o: $(CRTDIR)/crt1.c Makefile
$(CC) $(CRT_CFLAGS) -c $< -o $@
@@ -90,4 +112,4 @@ crtn.o: $(CRTDIR)/crtn.c Makefile
$(CC) $(CRT_CFLAGS) -c $< -o $@
clean:
rm -rf $(OBJDIR) $(TARGET) $(CRT_OBJS)
rm -rf $(OBJDIR) $(TARGET) $(SDK_TARGET) $(CRT_OBJS)