Skip to content

Commit 642e9cb

Browse files
committed
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.
1 parent 59692b0 commit 642e9cb

File tree

22 files changed

+404
-56
lines changed

22 files changed

+404
-56
lines changed

build/config.mk

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ CONFIG_CCFLAGS += -O3 -fmerge-all-constants
102102
CONFIG_COPTS += -mred-zone
103103
TARGET_ARCH ?= -march=native
104104
endif
105+
ifeq ($(MODE), x86_64-optlinux)
106+
CONFIG_OFLAGS ?= -g -ggdb
107+
CONFIG_CPPFLAGS += -DNDEBUG -DSYSDEBUG -DSUPPORT_VECTOR=1
108+
CONFIG_CCFLAGS += -O3 -fmerge-all-constants
109+
CONFIG_COPTS += -mred-zone
110+
TARGET_ARCH ?= -march=native
111+
endif
112+
ifeq ($(MODE), aarch64-optlinux)
113+
CONFIG_OFLAGS ?= -g -ggdb
114+
CONFIG_CPPFLAGS += -DNDEBUG -DSYSDEBUG -DSUPPORT_VECTOR=1
115+
CONFIG_CCFLAGS += -O3 -fmerge-all-constants
116+
CONFIG_COPTS += -mred-zone
117+
endif
105118

106119
# Release Mode
107120
#
@@ -136,17 +149,32 @@ endif
136149
ifeq ($(MODE), dbg)
137150
ENABLE_FTRACE = 1
138151
CONFIG_OFLAGS ?= -g -ggdb
139-
CONFIG_CPPFLAGS += -DMODE_DBG -D__SANITIZE_UNDEFINED__
140-
CONFIG_CCFLAGS += $(BACKTRACES) -DSYSDEBUG -O0 -fno-inline
152+
OVERRIDE_CFLAGS += -O0
153+
OVERRIDE_CXXFLAGS += -O0
154+
CONFIG_CPPFLAGS += -DMODE_DBG -D__SANITIZE_UNDEFINED__ -Wno-unused-variable -Wno-unused-but-set-variable
155+
CONFIG_CCFLAGS += $(BACKTRACES) -DSYSDEBUG
156+
CONFIG_COPTS += -fsanitize=undefined
157+
OVERRIDE_CCFLAGS += -fno-pie
158+
QUOTA ?= -C64 -L300
159+
endif
160+
ifeq ($(MODE), x86_64-dbg)
161+
ENABLE_FTRACE = 1
162+
CONFIG_OFLAGS ?= -g -ggdb
163+
OVERRIDE_CFLAGS += -O0
164+
OVERRIDE_CXXFLAGS += -O0
165+
CONFIG_CPPFLAGS += -DMODE_DBG -D__SANITIZE_UNDEFINED__ -Wno-unused-variable -Wno-unused-but-set-variable
166+
CONFIG_CCFLAGS += $(BACKTRACES) -DSYSDEBUG
141167
CONFIG_COPTS += -fsanitize=undefined
142168
OVERRIDE_CCFLAGS += -fno-pie
143169
QUOTA ?= -C64 -L300
144170
endif
145171
ifeq ($(MODE), aarch64-dbg)
146172
ENABLE_FTRACE = 1
147173
CONFIG_OFLAGS ?= -g -ggdb
148-
CONFIG_CPPFLAGS += -DMODE_DBG -D__SANITIZE_UNDEFINED__
149-
CONFIG_CCFLAGS += $(BACKTRACES) -DSYSDEBUG -O0 -fno-inline -fdce
174+
OVERRIDE_CFLAGS += -O0 -fdce
175+
OVERRIDE_CXXFLAGS += -O0 -fdce
176+
CONFIG_CPPFLAGS += -DMODE_DBG -D__SANITIZE_UNDEFINED__ -Wno-unused-variable -Wno-unused-but-set-variable
177+
CONFIG_CCFLAGS += $(BACKTRACES) -DSYSDEBUG
150178
CONFIG_COPTS += -fsanitize=undefined
151179
QUOTA ?= -C64 -L300
152180
endif

libc/calls/tcflush.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/calls/internal.h"
20-
#include "libc/intrin/fds.h"
2120
#include "libc/calls/syscall-nt.internal.h"
2221
#include "libc/calls/syscall-sysv.internal.h"
2322
#include "libc/calls/syscall_support-nt.internal.h"
2423
#include "libc/calls/termios.h"
2524
#include "libc/dce.h"
2625
#include "libc/errno.h"
2726
#include "libc/fmt/itoa.h"
27+
#include "libc/intrin/fds.h"
2828
#include "libc/intrin/strace.h"
2929
#include "libc/mem/alloca.h"
3030
#include "libc/nt/comms.h"
@@ -52,13 +52,17 @@ static const char *DescribeFlush(char buf[12], int action) {
5252
}
5353

5454
static dontinline textwindows int sys_tcflush_nt(int fd, int queue) {
55+
#ifdef __x86_64__
5556
if (!sys_isatty(fd)) {
5657
return -1; // ebadf, enotty
5758
}
5859
if (queue == TCOFLUSH) {
5960
return 0; // windows console output is never buffered
6061
}
6162
return FlushConsoleInputBytes();
63+
#else
64+
return enosys();
65+
#endif
6266
}
6367

6468
/**

libc/dlopen/dlopen.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,9 @@ static void *foreign_thunk_nt(void *func) {
557557
// movabs $tramp,%r10
558558
code[14] = 0x49;
559559
code[15] = 0xba;
560+
#ifdef __x86_64__
560561
WRITE64LE(code + 16, (uintptr_t)__sysv2nt14);
562+
#endif
561563
// jmp *%r10
562564
code[24] = 0x41;
563565
code[25] = 0xff;

libc/intrin/cp.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/calls/blockcancel.internal.h"
2020
#include "libc/calls/cp.internal.h"
21+
#include "libc/intrin/describebacktrace.h"
22+
#include "libc/intrin/kprintf.h"
23+
#include "libc/nexgen32e/stackframe.h"
2124
#include "libc/runtime/internal.h"
2225
#include "libc/runtime/runtime.h"
2326
#include "libc/thread/posixthread.internal.h"
@@ -46,7 +49,11 @@ void end_cancelation_point(int state) {
4649
}
4750
}
4851

49-
void report_cancelation_point(void) {
52+
void report_cancelation_point(int sysv_ordinal, int xnu_ordinal) {
53+
char bt[160];
54+
struct StackFrame *bp = __builtin_frame_address(0);
55+
kprintf("error: report_cancelation_point(%#x, %#x) %s\n", sysv_ordinal,
56+
xnu_ordinal, (DescribeBacktrace)(bt, bp));
5057
__builtin_trap();
5158
}
5259

libc/log/oncrash_arm64.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ static relegated void __oncrash_impl(int sig, siginfo_t *si, ucontext_t *ctx) {
266266
if (j)
267267
Append(b, " ");
268268
Append(b, "%s%016lx%s x%d%s", ColorRegister(r),
269-
ctx->uc_mcontext.regs[r], reset, r, r == 8 || r == 9 ? " " : "");
269+
((uint64_t *)ctx->uc_mcontext.regs)[r], reset, r,
270+
r == 8 || r == 9 ? " " : "");
270271
}
271272
Append(b, "\n");
272273
}

libc/nexgen32e/nt2sysv.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
*
88
* This macro should be used when specifying callbacks in the WIN32 API.
99
*/
10+
#ifdef __x86_64__
1011
#define NT2SYSV(FUNCTION) TRAMPOLINE(FUNCTION, __nt2sysv)
12+
#else
13+
#define NT2SYSV(FUNCTION) FUNCTION
14+
#endif
1115

1216
#endif /* COSMOPOLITAN_LIBC_NEXGEN32E_NT2SYSV_H_ */

libc/proc/posix_spawn.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "libc/calls/calls.h"
2323
#include "libc/calls/internal.h"
2424
#include "libc/calls/state.internal.h"
25-
#include "libc/intrin/fds.h"
2625
#include "libc/calls/struct/rlimit.h"
2726
#include "libc/calls/struct/rlimit.internal.h"
2827
#include "libc/calls/struct/rusage.internal.h"
@@ -39,6 +38,7 @@
3938
#include "libc/intrin/bsf.h"
4039
#include "libc/intrin/describeflags.h"
4140
#include "libc/intrin/dll.h"
41+
#include "libc/intrin/fds.h"
4242
#include "libc/intrin/strace.h"
4343
#include "libc/intrin/weaken.h"
4444
#include "libc/mem/alloca.h"
@@ -95,6 +95,10 @@
9595

9696
#define CLOSER_CONTAINER(e) DLL_CONTAINER(struct Closer, elem, e)
9797

98+
static atomic_bool has_vfork; // i.e. not qemu/wsl/xnu/openbsd
99+
100+
#ifdef __x86_64__
101+
98102
struct Closer {
99103
int64_t handle;
100104
struct Dll elem;
@@ -106,8 +110,6 @@ struct SpawnFds {
106110
struct Dll *closers;
107111
};
108112

109-
static atomic_bool has_vfork; // i.e. not qemu/wsl/xnu/openbsd
110-
111113
static textwindows int64_t spawnfds_handle(struct SpawnFds *fds, int fd) {
112114
if (__is_cloexec(fds->p + fd))
113115
return -1;
@@ -429,6 +431,8 @@ static textwindows dontinline errno_t posix_spawn_nt(
429431
return err;
430432
}
431433

434+
#endif // __x86_64__
435+
432436
/**
433437
* Spawns process, the POSIX way, e.g.
434438
*
@@ -482,8 +486,10 @@ errno_t posix_spawn(int *pid, const char *path,
482486
const posix_spawn_file_actions_t *file_actions,
483487
const posix_spawnattr_t *attrp, char *const argv[],
484488
char *const envp[]) {
489+
#ifdef __x86_64__
485490
if (IsWindows())
486491
return posix_spawn_nt(pid, path, file_actions, attrp, argv, envp);
492+
#endif
487493
int pfds[2];
488494
bool use_pipe;
489495
volatile int status = 0;

libc/runtime/clone.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,9 @@ int sys_clone_linux(int flags, // rdi
589589

590590
static int LinuxThreadEntry(void *arg, int tid) {
591591
struct LinuxCloneArgs *wt = arg;
592+
#if defined(__x86_64__)
592593
sys_set_tls(ARCH_SET_GS, wt->tls);
594+
#endif
593595
return wt->func(wt->arg, tid);
594596
}
595597

libc/runtime/stack.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
/**
77
* Returns preferred size and alignment of thread stack.
88
*/
9+
#ifndef MODE_DBG
910
#define GetStackSize() 81920
11+
#else
12+
#define GetStackSize() 163840
13+
#endif
1014

1115
/**
1216
* Returns preferred stack guard size.

libc/sysv/BUILD.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ o/$(MODE)/libc/sysv/sysret.o: private \
8888

8989
ifeq ($(ARCH),aarch64)
9090
o/$(MODE)/libc/sysv/sysv.o: private \
91-
CFLAGS += \
91+
OVERRIDE_CFLAGS += \
9292
-ffixed-x0 \
9393
-ffixed-x1 \
9494
-ffixed-x2 \

0 commit comments

Comments
 (0)