Skip to content

Commit

Permalink
Fix link order in cosmopolitan.a
Browse files Browse the repository at this point in the history
It turned out that the linker was doing the wrong with the amalgamation
library concerning weak stubs. A regression test has been added and new
binaries have been uploaded to https://justine.lol/cosmopolitan/

Ideally this should be fixed by building a tool that turns multiple .a
files into a single .a file with deduplication. As a workaround for now
the cosmopolitan.a build is restructured to not include LIBC_STUBS which
meant technical debt needed to be paid off where non-stub interfaces
were moved to LIBC_INTRIN and LIBC_NEXGEN32E.

Thank @PerfectProductions in jart#31 for the report!
  • Loading branch information
jart committed Jan 16, 2021
1 parent 04f1d89 commit 9f68d6e
Show file tree
Hide file tree
Showing 121 changed files with 295 additions and 374 deletions.
62 changes: 34 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -236,40 +236,44 @@ loc: o/$(MODE)/tool/build/summy.com
$(XARGS) wc -l | grep total | awk '{print $$1}' | $<

COSMOPOLITAN_OBJECTS = \
APE_LIB \
LIBC \
LIBC_ALG \
LIBC_BITS \
LIBC_CALLS \
LIBC_CALLS_HEFTY \
LIBC_CRYPTO \
LIBC_DNS \
LIBC_ELF \
LIBC_FMT \
LIBC_INTRIN \
LIBC_LOG \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_NT \
LIBC_OHMYPLUS \
LIBC_RAND \
LIBC_RUNTIME \
LIBC_SOCK \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_TIME \
LIBC_TINYMATH \
LIBC_UNICODE \
LIBC_NT_WS2_32 \
LIBC_NT_MSWSOCK \
LIBC_OHMYPLUS \
LIBC_X \
LIBC_ZIPOS \
THIRD_PARTY_COMPILER_RT \
THIRD_PARTY_DLMALLOC \
THIRD_PARTY_GDTOA \
THIRD_PARTY_GETOPT \
LIBC_LOG \
LIBC_UNICODE \
LIBC_TIME \
LIBC_ZIPOS \
THIRD_PARTY_ZLIB \
APE_LIB \
THIRD_PARTY_MUSL \
THIRD_PARTY_REGEX
LIBC_STDIO \
LIBC_CALLS_HEFTY \
THIRD_PARTY_REGEX \
LIBC_ALG \
LIBC_MEM \
THIRD_PARTY_DLMALLOC \
LIBC_BITS \
LIBC_RUNTIME \
LIBC_ELF \
LIBC_CALLS \
LIBC_RAND \
LIBC_SYSV_CALLS \
LIBC_NT_NTDLL \
LIBC_NT_ADVAPI32 \
LIBC_FMT \
THIRD_PARTY_COMPILER_RT \
LIBC_TINYMATH \
LIBC_SYSV \
LIBC_STR \
LIBC_INTRIN \
LIBC_NT_KERNEL32 \
LIBC_NEXGEN32E

COSMOPOLITAN_HEADERS = \
LIBC \
Expand Down Expand Up @@ -304,7 +308,9 @@ COSMOPOLITAN_HEADERS = \
THIRD_PARTY_ZLIB \
THIRD_PARTY_REGEX

o/$(MODE)/cosmopolitan.a: $(filter-out o/libc/stubs/exit11.o,$(foreach x,$(COSMOPOLITAN_OBJECTS),$($(x)_OBJS)))
o/$(MODE)/cosmopolitan.a.txt:
printf "%s\n" $(call reverse,$(call uniq,$(foreach x,$(COSMOPOLITAN_OBJECTS),$($(x)))))
o/$(MODE)/cosmopolitan.a: $(filter-out o/libc/stubs/exit11.o,$(foreach x,$(COSMOPOLITAN_OBJECTS),$($(x)_A_OBJS)))
o/cosmopolitan.h: \
o/$(MODE)/tool/build/rollup.com \
libc/integral/normalize.inc \
Expand Down
8 changes: 2 additions & 6 deletions ape/lib/apelib.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ APE_LIB_A_SRCS = \

APE_LIB_A_OBJS = \
$(APE_LIB_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(APE_LIB_A_SRCS_C:%.c=o/$(MODE)/%.o) \
$(APE_LIB_A_SRCS:%=o/$(MODE)/%.zip.o) \
o/$(MODE)/ape/ape.lds.zip.o \
o/$(MODE)/ape/ape.S.zip.o \
o/$(MODE)/NOTICE.zip.o
$(APE_LIB_A_SRCS_C:%.c=o/$(MODE)/%.o)

APE_LIB_A_CHECKS = $(APE_LIB_A_HDRS:%=o/$(MODE)/%.ok)
APE_LIB_A_DIRECTDEPS = LIBC_STR LIBC_STUBS
APE_LIB_A_DIRECTDEPS = LIBC_STR LIBC_NEXGEN32E LIBC_STUBS
APE_LIB_A_DEPS = $(call uniq,$(foreach x,$(APE_LIB_A_DIRECTDEPS),$($(x))))

$(APE_LIB_A): ape/lib/ $(APE_LIB_A).pkg $(APE_LIB_A_OBJS)
Expand Down
4 changes: 2 additions & 2 deletions build/package
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
if [ "$TOOL_BUILD_PACKAGE" ]; then
set -- "$TOOL_BUILD_PACKAGE" "$@"
else
if [ -x "o/tool/build/package.com" ]; then
set -- "o/tool/build/package.com" "$@"
if [ -x "o/tool/build/package.com.dbg" ]; then
set -- "o/tool/build/package.com.dbg" "$@"
else
MKDIR=${MKDIR:-$(command -v mkdir) -p} || exit
CP=${CP:-$(command -v cp) -f} || exit
Expand Down
1 change: 0 additions & 1 deletion dsp/bmp/bmp.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ DSP_BMP_A_SRCS = \
$(DSP_BMP_A_SRCS_C)

DSP_BMP_A_OBJS = \
$(DSP_BMP_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(DSP_BMP_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(DSP_BMP_A_SRCS_C:%.c=o/$(MODE)/%.o)

Expand Down
1 change: 0 additions & 1 deletion dsp/core/core.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ DSP_CORE_A_SRCS = \
$(DSP_CORE_A_SRCS_C)

DSP_CORE_A_OBJS = \
$(DSP_CORE_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(DSP_CORE_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(DSP_CORE_A_SRCS_C:%.c=o/$(MODE)/%.o)

Expand Down
20 changes: 10 additions & 10 deletions dsp/mpeg/mpeg.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ DSP_MPEG_A_SRCS = \
$(DSP_MPEG_A_SRCS_C)

DSP_MPEG_A_OBJS = \
$(DSP_MPEG_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(DSP_MPEG_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(DSP_MPEG_A_SRCS_C:%.c=o/$(MODE)/%.o)

Expand All @@ -26,19 +25,20 @@ DSP_MPEG_A_CHECKS = \

DSP_MPEG_A_DIRECTDEPS = \
LIBC_CALLS \
LIBC_FMT \
LIBC_INTRIN \
LIBC_LOG \
LIBC_RUNTIME \
LIBC_TINYMATH \
LIBC_TIME \
LIBC_STUBS \
LIBC_STR \
LIBC_LOG \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_MEM \
LIBC_LOG \
LIBC_FMT \
LIBC_UNICODE \
LIBC_TIME \
LIBC_TINYMATH \
LIBC_UNICODE

DSP_MPEG_A_DEPS := \
$(call uniq,$(foreach x,$(DSP_MPEG_A_DIRECTDEPS),$($(x))))
Expand Down
14 changes: 7 additions & 7 deletions dsp/scale/scale.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ DSP_SCALE_A_SRCS = \
$(DSP_SCALE_A_SRCS_C)

DSP_SCALE_A_OBJS = \
$(DSP_SCALE_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(DSP_SCALE_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(DSP_SCALE_A_SRCS_C:%.c=o/$(MODE)/%.o)

Expand All @@ -27,14 +26,15 @@ DSP_SCALE_A_CHECKS = \
DSP_SCALE_A_DIRECTDEPS = \
DSP_CORE \
LIBC_INTRIN \
LIBC_NEXGEN32E \
LIBC_TINYMATH \
LIBC_TIME \
LIBC_RUNTIME \
LIBC_LOG \
LIBC_MEM \
LIBC_X \
LIBC_STUBS
LIBC_NEXGEN32E \
LIBC_RUNTIME \
LIBC_STUBS \
LIBC_STUBS \
LIBC_TIME \
LIBC_TINYMATH \
LIBC_X

DSP_SCALE_A_DEPS := \
$(call uniq,$(foreach x,$(DSP_SCALE_A_DIRECTDEPS),$($(x))))
Expand Down
2 changes: 1 addition & 1 deletion dsp/tty/tty.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ DSP_TTY_A_SRCS = \
$(DSP_TTY_A_SRCS_C)

DSP_TTY_A_OBJS = \
$(DSP_TTY_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(DSP_TTY_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(DSP_TTY_A_SRCS_C:%.c=o/$(MODE)/%.o)

Expand All @@ -29,6 +28,7 @@ DSP_TTY_A_DIRECTDEPS = \
LIBC_ALG \
LIBC_CALLS \
LIBC_FMT \
LIBC_INTRIN \
LIBC_LOG \
LIBC_RUNTIME \
LIBC_MEM \
Expand Down
2 changes: 1 addition & 1 deletion examples/examples.mk
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ EXAMPLES_MAINS = \
$(EXAMPLES_MAINS_CC)

EXAMPLES_OBJS = \
$(EXAMPLES_SRCS:%=o/$(MODE)/%.zip.o) \
$(EXAMPLES_MAINS_S:%.S=o/$(MODE)/%.o) \
$(EXAMPLES_MAINS_C:%.c=o/$(MODE)/%.o) \
$(EXAMPLES_MAINS_CC:%.cc=o/$(MODE)/%.o)
Expand Down Expand Up @@ -47,6 +46,7 @@ EXAMPLES_DIRECTDEPS = \
LIBC_CALLS \
LIBC_CALLS_HEFTY \
LIBC_FMT \
LIBC_INTRIN \
LIBC_LOG \
LIBC_LOG_ASAN \
LIBC_MEM \
Expand Down
3 changes: 1 addition & 2 deletions examples/package/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ EXAMPLES_PACKAGE_BINS = \
# Remaps source file names to object names.
# Also asks a wildcard rule to automatically run tool/build/zipobj.c
EXAMPLES_PACKAGE_OBJS = \
$(EXAMPLES_PACKAGE_SRCS:%.c=o/$(MODE)/%.o) \
$(EXAMPLES_PACKAGE_SRCS:%=o/$(MODE)/%.zip.o)
$(EXAMPLES_PACKAGE_SRCS:%.c=o/$(MODE)/%.o)

# Lists packages whose symbols are or may be directly referenced here.
# Note that linking stubs is always a good idea due to synthetic code.
Expand Down
4 changes: 2 additions & 2 deletions examples/package/lib/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ EXAMPLES_PACKAGE_LIB_A_SRCS = \
# Change suffixes of different languages extensions into object names.
EXAMPLES_PACKAGE_LIB_A_OBJS = \
$(EXAMPLES_PACKAGE_LIB_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(EXAMPLES_PACKAGE_LIB_A_SRCS_C:%.c=o/$(MODE)/%.o) \
$(EXAMPLES_PACKAGE_LIB_A_SRCS:%=o/$(MODE)/%.zip.o)
$(EXAMPLES_PACKAGE_LIB_A_SRCS_C:%.c=o/$(MODE)/%.o)

# Does the two most important things for C/C++ code sustainability.
# 1. Guarantees each header builds, i.e. includes symbols it needs.
Expand All @@ -71,6 +70,7 @@ EXAMPLES_PACKAGE_LIB_A_CHECKS = \
# Note that linking stubs is always a good idea due to synthetic code.
EXAMPLES_PACKAGE_LIB_A_DIRECTDEPS = \
LIBC_STDIO \
LIBC_NEXGEN32E \
LIBC_STUBS

# Evaluates variable as set of transitive package dependencies.
Expand Down
2 changes: 1 addition & 1 deletion libc/alg/alg.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ LIBC_ALG_A_SRCS = \
$(LIBC_ALG_A_SRCS_C)

LIBC_ALG_A_OBJS = \
$(LIBC_ALG_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(LIBC_ALG_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(LIBC_ALG_A_SRCS_C:%.c=o/$(MODE)/%.o)

Expand All @@ -29,6 +28,7 @@ LIBC_ALG_A_DIRECTDEPS = \
LIBC_NEXGEN32E \
LIBC_RUNTIME \
LIBC_STR \
LIBC_INTRIN \
LIBC_STUBS \
LIBC_SYSV

Expand Down
2 changes: 1 addition & 1 deletion libc/bits/bits.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ LIBC_BITS_A_SRCS = \
$(LIBC_BITS_A_SRCS_C)

LIBC_BITS_A_OBJS = \
$(LIBC_BITS_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(LIBC_BITS_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(LIBC_BITS_A_SRCS_C:%.c=o/$(MODE)/%.o)

Expand All @@ -26,6 +25,7 @@ LIBC_BITS_A_CHECKS = \

LIBC_BITS_A_DIRECTDEPS = \
LIBC_STUBS \
LIBC_INTRIN \
LIBC_NEXGEN32E

LIBC_BITS_A_DEPS := \
Expand Down
2 changes: 1 addition & 1 deletion libc/calls/calls.mk
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ LIBC_CALLS_A_SRCS = \
$(LIBC_CALLS_A_SRCS_C)

LIBC_CALLS_A_OBJS = \
$(LIBC_CALLS_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(LIBC_CALLS_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(LIBC_CALLS_A_SRCS_C:%.c=o/$(MODE)/%.o)

Expand All @@ -39,6 +38,7 @@ LIBC_CALLS_A_CHECKS = \

LIBC_CALLS_A_DIRECTDEPS = \
LIBC_FMT \
LIBC_INTRIN \
LIBC_NEXGEN32E \
LIBC_NT_ADVAPI32 \
LIBC_NT_KERNEL32 \
Expand Down
2 changes: 1 addition & 1 deletion libc/calls/hefty/hefty.mk
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ LIBC_CALLS_HEFTY_A_SRCS = \
$(LIBC_CALLS_HEFTY_A_SRCS_C)

LIBC_CALLS_HEFTY_A_OBJS = \
$(LIBC_CALLS_HEFTY_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(LIBC_CALLS_HEFTY_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(LIBC_CALLS_HEFTY_A_SRCS_C:%.c=o/$(MODE)/%.o)

Expand All @@ -38,6 +37,7 @@ LIBC_CALLS_HEFTY_A_DIRECTDEPS = \
LIBC_ALG \
LIBC_CALLS \
LIBC_FMT \
LIBC_INTRIN \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_NT_KERNEL32 \
Expand Down
2 changes: 1 addition & 1 deletion libc/crt/crt.mk
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CRT_ARTIFACTS += CRT
CRT = o/$(MODE)/libc/crt/crt.o
CRT_FILES = libc/crt/crt.S
CRT_SRCS = libc/crt/crt.S
CRT_OBJS = o/$(MODE)/libc/crt/crt.o o/$(MODE)/libc/crt/crt.S.zip.o
CRT_OBJS = o/$(MODE)/libc/crt/crt.o
$(CRT_OBJS): $(BUILD_FILES) libc/crt/crt.mk

.PHONY: o/$(MODE)/libc/crt
Expand Down
2 changes: 1 addition & 1 deletion libc/crypto/crypto.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ LIBC_CRYPTO_A_SRCS = \
$(LIBC_CRYPTO_A_SRCS_C)

LIBC_CRYPTO_A_OBJS = \
$(LIBC_CRYPTO_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(LIBC_CRYPTO_A_SRCS_A:%.s=o/$(MODE)/%.o) \
$(LIBC_CRYPTO_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(LIBC_CRYPTO_A_SRCS_C:%.c=o/$(MODE)/%.o)
Expand All @@ -28,6 +27,7 @@ LIBC_CRYPTO_A_CHECKS = \
$(LIBC_CRYPTO_A_HDRS:%=o/$(MODE)/%.ok)

LIBC_CRYPTO_A_DIRECTDEPS = \
LIBC_INTRIN \
LIBC_STUBS \
LIBC_NEXGEN32E

Expand Down
2 changes: 1 addition & 1 deletion libc/dns/dns.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ LIBC_DNS_A_SRCS = \
$(LIBC_DNS_A_SRCS_C)

LIBC_DNS_A_OBJS = \
$(LIBC_DNS_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(LIBC_DNS_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(LIBC_DNS_A_SRCS_C:%.c=o/$(MODE)/%.o)

Expand All @@ -34,6 +33,7 @@ LIBC_DNS_A_DIRECTDEPS = \
LIBC_RUNTIME \
LIBC_SOCK \
LIBC_STDIO \
LIBC_INTRIN \
LIBC_STUBS \
LIBC_STR \
LIBC_SYSV \
Expand Down
2 changes: 1 addition & 1 deletion libc/elf/elf.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ LIBC_ELF_A_SRCS = \
$(LIBC_ELF_A_SRCS_C)

LIBC_ELF_A_OBJS = \
$(LIBC_ELF_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(LIBC_ELF_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(LIBC_ELF_A_SRCS_C:%.c=o/$(MODE)/%.o)

Expand All @@ -25,6 +24,7 @@ LIBC_ELF_A_CHECKS = \
$(LIBC_ELF_A_HDRS:%=o/$(MODE)/%.ok)

LIBC_ELF_A_DIRECTDEPS = \
LIBC_INTRIN \
LIBC_NEXGEN32E \
LIBC_STR \
LIBC_STUBS
Expand Down
2 changes: 1 addition & 1 deletion libc/fmt/fmt.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ LIBC_FMT_A_SRCS = \
$(LIBC_FMT_A_SRCS_C)

LIBC_FMT_A_OBJS = \
$(LIBC_FMT_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(LIBC_FMT_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(LIBC_FMT_A_SRCS_C:%.c=o/$(MODE)/%.o)

Expand All @@ -37,6 +36,7 @@ LIBC_FMT_A_DIRECTDEPS = \
LIBC_NEXGEN32E \
LIBC_NT_KERNEL32 \
LIBC_STR \
LIBC_INTRIN \
LIBC_STUBS \
LIBC_SYSV \
LIBC_TINYMATH \
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion libc/intrin/intrin.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ LIBC_INTRIN_A_FILES := \
$(wildcard libc/intrin/*)

LIBC_INTRIN_A_OBJS = \
$(LIBC_INTRIN_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(LIBC_INTRIN_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(LIBC_INTRIN_A_SRCS_C:%.c=o/$(MODE)/%.o)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 9f68d6e

Please sign in to comment.