From ee6566a1529463c3991c4d1b4c3b3accafe9c1fb Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Mon, 10 Jul 2023 05:55:00 -0700 Subject: [PATCH] Further improve usability of cosmocc - Support -s flag in cosmocc - Support posix_spawn() setsid() feature - Disable monorepo debug path prefix stripping --- .github/workflows/build.yml | 2 +- build/definitions.mk | 4 +--- libc/calls/setsid.c | 7 ++++++- libc/integral/c.inc | 1 - libc/stdio/posix_spawn.c | 6 ++++++ libc/stdio/posix_spawn.h | 13 +++++++------ libc/stdio/posix_spawnattr.c | 8 +++++--- tool/scripts/cosmoc++ | 7 +++++++ tool/scripts/cosmocc | 7 +++++++ 9 files changed, 40 insertions(+), 15 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4b0798a0b..0163f7e57 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -17,7 +17,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - mode: ["", tiny, rel, nox87, aarch64, tinylinux, optlinux] + mode: ["", tiny, rel, aarch64, tinylinux, optlinux] steps: - uses: actions/checkout@v3 diff --git a/build/definitions.mk b/build/definitions.mk index e041cdbcd..1f17ec9bf 100644 --- a/build/definitions.mk +++ b/build/definitions.mk @@ -172,7 +172,6 @@ DEFAULT_CCFLAGS += \ -Wall \ -Werror \ -fno-omit-frame-pointer \ - -fdebug-prefix-map='$(PWD)'= \ -frecord-gcc-switches DEFAULT_COPTS ?= \ @@ -259,8 +258,7 @@ endif ASONLYFLAGS = \ -c \ - -g \ - --debug-prefix-map='$(PWD)'= + -g DEFAULT_LDLIBS = diff --git a/libc/calls/setsid.c b/libc/calls/setsid.c index 8b2d3ab64..876eab85c 100644 --- a/libc/calls/setsid.c +++ b/libc/calls/setsid.c @@ -18,6 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" #include "libc/calls/syscall-sysv.internal.h" +#include "libc/dce.h" #include "libc/intrin/strace.internal.h" /** @@ -26,7 +27,11 @@ */ int setsid(void) { int rc; - rc = sys_setsid(); + if (!IsWindows() && !IsMetal()) { + rc = sys_setsid(); + } else { + rc = 0; + } STRACE("setsid() → %d% m", rc); return rc; } diff --git a/libc/integral/c.inc b/libc/integral/c.inc index 7be14c688..a7195f1af 100644 --- a/libc/integral/c.inc +++ b/libc/integral/c.inc @@ -735,7 +735,6 @@ void abort(void) wontreturn; #pragma GCC diagnostic error "-Wnonnull" #pragma GCC diagnostic error "-Wunused-result" #pragma GCC diagnostic error "-Wstrict-aliasing" -#pragma GCC diagnostic error "-Wshift-negative-value" #ifndef __cplusplus #pragma GCC diagnostic error "-Wimplicit-function-declaration" #if __GNUC__ >= 6 diff --git a/libc/stdio/posix_spawn.c b/libc/stdio/posix_spawn.c index da956d810..3805e983d 100644 --- a/libc/stdio/posix_spawn.c +++ b/libc/stdio/posix_spawn.c @@ -84,6 +84,12 @@ int posix_spawn(int *pid, const char *path, if (!(child = vfork())) { if (attrp && *attrp) { posix_spawnattr_getflags(attrp, &flags); + if (flags & POSIX_SPAWN_SETSID) { + if (setsid()) { + STRACE("posix_spawn fail #%d", 1); + _Exit(127); + } + } if (flags & POSIX_SPAWN_SETPGROUP) { if (setpgid(0, (*attrp)->pgroup)) { STRACE("posix_spawn fail #%d", 1); diff --git a/libc/stdio/posix_spawn.h b/libc/stdio/posix_spawn.h index 8f177d854..1c47992e7 100644 --- a/libc/stdio/posix_spawn.h +++ b/libc/stdio/posix_spawn.h @@ -3,12 +3,13 @@ #include "libc/calls/struct/sched_param.h" #include "libc/calls/struct/sigset.h" -#define POSIX_SPAWN_RESETIDS 0x01 -#define POSIX_SPAWN_SETPGROUP 0x02 -#define POSIX_SPAWN_SETSIGDEF 0x04 -#define POSIX_SPAWN_SETSIGMASK 0x08 -#define POSIX_SPAWN_SETSCHEDPARAM 0x10 -#define POSIX_SPAWN_SETSCHEDULER 0x20 +#define POSIX_SPAWN_RESETIDS 1 +#define POSIX_SPAWN_SETPGROUP 2 +#define POSIX_SPAWN_SETSIGDEF 4 +#define POSIX_SPAWN_SETSIGMASK 8 +#define POSIX_SPAWN_SETSCHEDPARAM 16 +#define POSIX_SPAWN_SETSCHEDULER 32 +#define POSIX_SPAWN_SETSID 128 #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ diff --git a/libc/stdio/posix_spawnattr.c b/libc/stdio/posix_spawnattr.c index f16603a3e..622c2f1ca 100644 --- a/libc/stdio/posix_spawnattr.c +++ b/libc/stdio/posix_spawnattr.c @@ -89,6 +89,7 @@ int posix_spawnattr_getflags(const posix_spawnattr_t *attr, short *flags) { * - `POSIX_SPAWN_SETSIGMASK` * - `POSIX_SPAWN_SETSCHEDPARAM` * - `POSIX_SPAWN_SETSCHEDULER` + * - `POSIX_SPAWN_SETSID` * @return 0 on success, or errno on error * @raise EINVAL if `flags` has invalid bits */ @@ -96,9 +97,10 @@ int posix_spawnattr_setflags(posix_spawnattr_t *attr, short flags) { if (!(IsLinux() || IsFreebsd() || IsNetbsd())) { flags &= ~(POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER); } - if (flags & ~(POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP | - POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK | - POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER)) { + if (flags & + ~(POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF | + POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSCHEDPARAM | + POSIX_SPAWN_SETSCHEDULER | POSIX_SPAWN_SETSID)) { return EINVAL; } (*attr)->flags = flags; diff --git a/tool/scripts/cosmoc++ b/tool/scripts/cosmoc++ index 1dabd19ba..8de17d06a 100755 --- a/tool/scripts/cosmoc++ +++ b/tool/scripts/cosmoc++ @@ -78,6 +78,7 @@ APEFLAGS="-L$COSMOS/lib -Wl,--gc-sections -Wl,-T,$COSMO/o/$MODE/ape/public/ape.l LDLIBS="$COSMO/o/$MODE/third_party/libcxx/libcxx.a $COSMO/o/$MODE/cosmopolitan.a" CXX="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-g++" +STRIP="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-strip" OBJCOPY="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-objcopy" FIXUPOBJ="$COSMO/o/$MODE/tool/build/fixupobj.com" ZIPCOPY="$COSMO/o/$MODE/tool/build/zipcopy.com" @@ -115,6 +116,7 @@ done OPT= FIRST=1 OUTPUT= +SFLAG=0 INTENT=ld NEED_OUTPUT= RELOCATABLE=0 @@ -140,6 +142,9 @@ for x; do OPT=$x elif [ x"$x" = x"-c" ]; then INTENT=cc + elif [ x"$x" = x"-s" ]; then + SFLAG=1 + continue elif [ x"$x" = x"-r" ]; then RELOCATABLE=1 elif [ x"$x" = x"-E" ]; then @@ -204,6 +209,8 @@ if [ -n "$OUTPUT" ] && [ -f "$OUTPUT" ]; then mv -f "$OUTPUT" "$OUTPUT.dbg" || exit "$OBJCOPY" -S -O binary "$OUTPUT.dbg" "$OUTPUT" || exit "$ZIPCOPY" "$OUTPUT.dbg" "$OUTPUT" || exit + elif [ $SFLAG -eq 1 ]; then + "$STRIP" "$OUTPUT" || exit fi fi fi diff --git a/tool/scripts/cosmocc b/tool/scripts/cosmocc index 4315525d9..11073d930 100755 --- a/tool/scripts/cosmocc +++ b/tool/scripts/cosmocc @@ -78,6 +78,7 @@ APEFLAGS="-L$COSMOS/lib -Wl,--gc-sections -Wl,-T,$COSMO/o/$MODE/ape/public/ape.l LDLIBS="$COSMO/o/$MODE/cosmopolitan.a" CC="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-gcc" +STRIP="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-strip" OBJCOPY="$COSMO/o/third_party/gcc/bin/x86_64-linux-musl-objcopy" FIXUPOBJ="$COSMO/o/$MODE/tool/build/fixupobj.com" ZIPCOPY="$COSMO/o/$MODE/tool/build/zipcopy.com" @@ -115,6 +116,7 @@ done OPT= FIRST=1 OUTPUT= +SFLAG=0 INTENT=ld NEED_OUTPUT= RELOCATABLE=0 @@ -138,6 +140,9 @@ for x; do OPT=$x elif [ x"$x" = x"-c" ]; then INTENT=cc + elif [ x"$x" = x"-s" ]; then + SFLAG=1 + continue elif [ x"$x" = x"-r" ]; then RELOCATABLE=1 elif [ x"$x" = x"-E" ]; then @@ -202,6 +207,8 @@ if [ -n "$OUTPUT" ] && [ -f "$OUTPUT" ]; then mv -f "$OUTPUT" "$OUTPUT.dbg" || exit "$OBJCOPY" -S -O binary "$OUTPUT.dbg" "$OUTPUT" || exit "$ZIPCOPY" "$OUTPUT.dbg" "$OUTPUT" || exit + elif [ $SFLAG -eq 1 ]; then + "$STRIP" "$OUTPUT" || exit fi fi fi