Introduce cosmocc flags -mdbg -mtiny -moptlinux

The cosmocc.zip toolchain will now include four builds of the libcosmo.a
runtime libraries. You can pass the -mdbg flag if you want to debug your
cosmopolitan runtime. You can pass the -moptlinux flag if you don't want
windows code lurking in your binary. See tool/cosmocc/README.md for more
details on how these flags may be used and their important implications.
This commit is contained in:
Justine Tunney 2024-07-26 05:10:25 -07:00
parent 59692b0882
commit 642e9cb91a
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
22 changed files with 404 additions and 56 deletions

View file

@ -156,6 +156,15 @@ for x; do
elif [ x"$x" = x"-mcosmo" ]; then
MCOSMO=1
continue
elif [ x"$x" = x"-mdbg" ]; then
MODE=dbg
continue
elif [ x"$x" = x"-mtiny" ]; then
MODE=tiny
continue
elif [ x"$x" = x"-moptlinux" ]; then
MODE=optlinux
continue
elif [ x"$x" = x"-fomit-frame-pointer" ]; then
# Quoth Apple: "The frame pointer register must always address a
# valid frame record. Some functions — such as leaf functions or
@ -243,10 +252,10 @@ LDFLAGS="-static -nostdlib -no-pie -fuse-ld=bfd -Wl,-z,noexecstack -Wl,-z,norelr
PRECIOUS="-fno-omit-frame-pointer"
# these features screw with backtraces so avoid them
if [ x"$OPT" != x"-Os" ] && [ x"$MODE" != x"tiny" ]; then
if [ x"$OPT" != x"-Os" ] && [ x"$MODE" != x"tiny" ] && [ x"$MODE" != x"optlinux" ]; then
CFLAGS="$CFLAGS -fno-optimize-sibling-calls -mno-omit-leaf-frame-pointer"
fi
if [ x"$OPT" != x"-O3" ]; then
if [ x"$OPT" != x"-O3" ] && [ x"$MODE" != x"optlinux" ]; then
CFLAGS="$CFLAGS -fno-schedule-insns2"
fi
@ -261,19 +270,39 @@ else
CFLAGS="$CFLAGS -Wno-implicit-int"
fi
CRT_X86_64="$BIN/../x86_64-linux-cosmo/lib/ape.o $BIN/../x86_64-linux-cosmo/lib/crt.o"
CPPFLAGS_X86_64="$CPPFLAGS -mno-red-zone"
if [ x"$MODE" = x"dbg" ]; then
LIB_X86_64="$BIN/../x86_64-linux-cosmo/lib/dbg"
LIB_AARCH64="$BIN/../aarch64-linux-cosmo/lib/dbg"
elif [ x"$MODE" = x"tiny" ]; then
LIB_X86_64="$BIN/../x86_64-linux-cosmo/lib/tiny"
LIB_AARCH64="$BIN/../aarch64-linux-cosmo/lib/tiny"
elif [ x"$MODE" = x"optlinux" ]; then
LIB_X86_64="$BIN/../x86_64-linux-cosmo/lib/optlinux"
LIB_AARCH64="$BIN/../aarch64-linux-cosmo/lib/optlinux"
else
LIB_X86_64="$BIN/../x86_64-linux-cosmo/lib"
LIB_AARCH64="$BIN/../aarch64-linux-cosmo/lib"
fi
CRT_X86_64="$LIB_X86_64/ape.o $LIB_X86_64/crt.o"
CPPFLAGS_X86_64="$CPPFLAGS"
CFLAGS_X86_64="$CFLAGS -mno-tls-direct-seg-refs"
LDFLAGS_X86_64="$LDFLAGS -L$BIN/../x86_64-linux-cosmo/lib -Wl,-T,$BIN/../x86_64-linux-cosmo/lib/ape.lds -Wl,-z,common-page-size=4096 -Wl,-z,max-page-size=16384"
LDFLAGS_X86_64="$LDFLAGS -L$LIB_X86_64 -L$BIN/../x86_64-linux-cosmo/lib -Wl,-T,$LIB_X86_64/ape.lds -Wl,-z,common-page-size=4096 -Wl,-z,max-page-size=16384"
LDLIBS_X86_64="-lcosmo"
CRT_AARCH64="$BIN/../aarch64-linux-cosmo/lib/crt.o"
CRT_AARCH64="$LIB_AARCH64/crt.o"
CPPFLAGS_AARCH64="$CPPFLAGS -fsigned-char"
CFLAGS_AARCH64="$CFLAGS -ffixed-x18 -ffixed-x28 -mno-outline-atomics"
LDFLAGS_AARCH64="$LDFLAGS -L$BIN/../aarch64-linux-cosmo/lib -Wl,-T,$BIN/../aarch64-linux-cosmo/lib/aarch64.lds -Wl,-z,common-page-size=16384 -Wl,-z,max-page-size=16384"
LDFLAGS_AARCH64="$LDFLAGS -L$LIB_AARCH64 -L$BIN/../aarch64-linux-cosmo/lib -Wl,-T,$LIB_AARCH64/aarch64.lds -Wl,-z,common-page-size=16384 -Wl,-z,max-page-size=16384"
LDLIBS_AARCH64="-lcosmo"
if [ x"$OPT" != x"-Os" ] && [ x"$MODE" != x"tiny" ]; then
SUPPORT_VECTOR=-1
if [ x"$MODE" = x"optlinux" ]; then
CPPFLAGS_X86_64="$CPPFLAGS_X86_64 -mno-red-zone"
SUPPORT_VECTOR=linux
fi
if [ x"$OPT" != x"-Os" ] && [ x"$MODE" != x"tiny" ] && [ x"$MODE" != x"optlinux" ]; then
CFLAGS_X86_64="${CFLAGS_X86_64} -fpatchable-function-entry=18,16 -fno-inline-functions-called-once -DFTRACE -DSYSDEBUG"
CFLAGS_AARCH64="${CFLAGS_AARCH64} -fpatchable-function-entry=7,6 -fno-inline-functions-called-once -DFTRACE -DSYSDEBUG"
fi
@ -526,6 +555,7 @@ fi
set -- \
"$BIN/apelink" \
-V "$SUPPORT_VECTOR" \
-l "$BIN/ape-x86_64.elf" \
-l "$BIN/ape-aarch64.elf" \
-M "$BIN/ape-m1.c" \
@ -536,10 +566,12 @@ set -- \
log_command "$@"
"$@" || Exit
set -- \
"$BIN/pecheck" "$OUTPUT"
log_command "$@"
"$@" || Exit
if [ x"$MODE" != "optlinux" ]; then
set -- \
"$BIN/pecheck" "$OUTPUT"
log_command "$@"
"$@" || Exit
fi
if [ $INTENT = ld ] && [ $SAVE_TEMPS -eq 0 ]; then
mv -f "$OUTPUT_X86_64" "${OUTPUT%.com}.com.dbg" || Exit

View file

@ -59,13 +59,33 @@ if [ x"$ARCH" = x"$PROG" ]; then
fatal_error "cosmocross must be run via cross compiler"
fi
for x; do
if [ x"$x" = x"-mtiny" ]; then
MODE=tiny
elif [ x"$x" = x"-mdbg" ]; then
MODE=dbg
elif [ x"$x" = x"-moptlinux" ]; then
MODE=optlinux
fi
done
if [ x"$MODE" = x"dbg" ]; then
LIB="$BIN/../$ARCH-linux-cosmo/lib/dbg"
elif [ x"$MODE" = x"tiny" ]; then
LIB="$BIN/../$ARCH-linux-cosmo/lib/tiny"
elif [ x"$MODE" = x"optlinux" ]; then
LIB="$BIN/../$ARCH-linux-cosmo/lib/optlinux"
else
LIB="$BIN/../$ARCH-linux-cosmo/lib"
fi
CC="$BIN/$ARCH-linux-cosmo-gcc"
CRT="$BIN/../$ARCH-linux-cosmo/lib/crt.o"
CRT="$LIB/crt.o"
LDLIBS="-lcosmo"
if [ -z "$COSMOS" ]; then
LDFLAGS="$LDFLAGS -L$BIN/../$ARCH-linux-cosmo/lib"
LDFLAGS="$LDFLAGS -L$LIB -L$BIN/../$ARCH-linux-cosmo/lib"
else
LDFLAGS="$LDFLAGS -L$COSMOS/lib -L$BIN/../$ARCH-linux-cosmo/lib"
LDFLAGS="$LDFLAGS -L$COSMOS/lib -L$LIB -L$BIN/../$ARCH-linux-cosmo/lib"
CPPFLAGS="$CPPFLAGS -I$COSMOS/include"
fi
if [ x"$PROG" != x"${PROG%++}" ]; then
@ -80,16 +100,20 @@ fi
PAGESZ=4096
if [ x"$ARCH" = x"x86_64" ]; then
OBJCOPYFLAGS="-S -O binary"
CRT="$BIN/../$ARCH-linux-cosmo/lib/ape-no-modify-self.o $CRT"
CPPFLAGS="$CPPFLAGS -mno-red-zone"
CRT="$LIB/ape-no-modify-self.o $CRT"
CFLAGS="$CFLAGS -mno-tls-direct-seg-refs"
LDFLAGS="$LDFLAGS -Wl,-T,$BIN/../$ARCH-linux-cosmo/lib/ape.lds"
LDFLAGS="$LDFLAGS -Wl,-T,$LIB/ape.lds"
if [ x"$MODE" = x"optlinux" ]; then
CPPFLAGS="$CPPFLAGS -mred-zone"
else
CPPFLAGS="$CPPFLAGS -mno-red-zone"
fi
elif [ x"$ARCH" = x"aarch64" ]; then
OBJCOPYFLAGS="-S"
PAGESZ=16384
CPPFLAGS="$CPPFLAGS -fsigned-char"
CFLAGS="$CFLAGS -ffixed-x18 -ffixed-x28 -mno-outline-atomics"
LDFLAGS="$LDFLAGS -Wl,-T,$BIN/../$ARCH-linux-cosmo/lib/aarch64.lds"
LDFLAGS="$LDFLAGS -Wl,-T,$LIB/aarch64.lds"
else
fatal_error "$ARCH: unsupported architecture"
fi
@ -142,6 +166,12 @@ for x; do
elif [ x"$x" = x"-mcosmo" ]; then
CPPFLAGS="$CPPFLAGS -D_COSMO_SOURCE"
continue
elif [ x"$x" = x"-mdbg" ]; then
continue
elif [ x"$x" = x"-mtiny" ]; then
continue
elif [ x"$x" = x"-moptlinux" ]; then
continue
elif [ x"$x" != x"${x#-o}" ]; then
OUTPUT=${x#-o}
elif [ x"$x" = x"-fpic" ]; then
@ -189,6 +219,7 @@ fi
# support --ftrace unless optimizing for size
if [ x"$OPT" != x"-Os" ] && # $OPT != -Os
[ x"$MODE" != x"optlinux" ] && # $MODE not optlinux
[ x"${MODE%tiny}" = x"${MODE}" ]; then # $MODE not in (tiny, aarch64-tiny)
if [ x"$ARCH" = x"x86_64" ]; then
CFLAGS="$CFLAGS -fpatchable-function-entry=18,16 -fno-inline-functions-called-once"
@ -199,10 +230,11 @@ fi
# maximize frame pointers unless optimizing for size
if [ x"$OPT" != x"-Os" ] && # $OPT != "-Os"
[ x"$MODE" != x"optlinux" ] && # $MODE not optlinux
[ x"$MODE" != x"${MODE%tiny}" ]; then # endswith($MODE, "tiny")
CFLAGS="$CFLAGS -fno-optimize-sibling-calls -mno-omit-leaf-frame-pointer"
fi
if [ x"$OPT" != x"-O3" ]; then
if [ x"$OPT" != x"-O3" ] && [ x"$MODE" != x"optlinux" ]; then
CFLAGS="$CFLAGS -fno-schedule-insns2"
fi