Skip to content

Commit c6d3802

Browse files
committed
Add more fixes for new cosmocc toolchain
We now have an `#include <cxxabi.h>` header which defines all the APIs Cosmopolitan's implemented so far. The `cosmocc` README.md file is now greatly expanded with documentation.
1 parent 95124ca commit c6d3802

32 files changed

+256
-69
lines changed

Makefile

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,14 @@ COSMOPOLITAN_OBJECTS = \
403403
LIBC_NT_KERNEL32 \
404404
LIBC_NEXGEN32E
405405

406-
COSMOPOLITAN_HEADERS = \
406+
COSMOPOLITAN_H_PKGS = \
407407
APE \
408408
LIBC \
409409
LIBC_CALLS \
410410
LIBC_DNS \
411411
LIBC_ELF \
412412
LIBC_FMT \
413+
LIBC_DLOPEN \
413414
LIBC_INTRIN \
414415
LIBC_LOG \
415416
LIBC_MEM \
@@ -436,26 +437,34 @@ COSMOPOLITAN_HEADERS = \
436437
THIRD_PARTY_MUSL \
437438
THIRD_PARTY_REGEX
438439

439-
COSMOCC_HEADERS = \
440+
COSMOCC_PKGS = \
441+
$(COSMOPOLITAN_H_PKGS) \
440442
THIRD_PARTY_AARCH64 \
441443
THIRD_PARTY_LIBCXX \
442444
THIRD_PARTY_INTEL
443445

444446
o/$(MODE)/cosmopolitan.a: \
445447
$(foreach x,$(COSMOPOLITAN_OBJECTS),$($(x)_A_OBJS))
446448

447-
o/cosmocc.h.txt: $(foreach x,$(COSMOCC_HEADERS),$($(x)_HDRS))
448-
$(file >$@, $^)
449+
COSMOCC_HDRS = \
450+
$(wildcard libc/integral/*) \
451+
$(foreach x,$(COSMOCC_PKGS),$($(x)_HDRS)) \
452+
$(foreach x,$(COSMOCC_PKGS),$($(x)_INCS))
453+
454+
o/cosmocc.h.txt: Makefile
455+
$(file >$@, $(call uniq,$(COSMOCC_HDRS)))
456+
457+
COSMOPOLITAN_H_ROOT_HDRS = \
458+
libc/integral/normalize.inc \
459+
$(foreach x,$(COSMOPOLITAN_H_PKGS),$($(x)_HDRS))
449460

450-
o/cosmopolitan.h.txt: \
451-
libc/integral/normalize.inc \
452-
$(foreach x,$(COSMOPOLITAN_HEADERS),$($(x)_HDRS))
453-
$(file >$@, $^)
461+
o/cosmopolitan.h.txt: Makefile
462+
$(file >$@, $(call uniq,$(COSMOPOLITAN_H_ROOT_HDRS)))
454463

455464
o/cosmopolitan.h: o/cosmopolitan.h.txt \
456-
libc/integral/normalize.inc \
457-
$(foreach x,$(COSMOPOLITAN_HEADERS),$($(x)_HDRS)) \
458-
$(foreach x,$(COSMOPOLITAN_HEADERS),$($(x)_INCS))
465+
$(wildcard libc/integral/*) \
466+
$(foreach x,$(COSMOPOLITAN_H_PKGS),$($(x)_HDRS)) \
467+
$(foreach x,$(COSMOPOLITAN_H_PKGS),$($(x)_INCS))
459468
@$(ECHO) '#ifndef __STRICT_ANSI__' >$@
460469
@$(ECHO) '#define _COSMO_SOURCE' >>$@
461470
@$(ECHO) '#endif' >>$@

ape/ape-m1.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ struct Syslib {
6262
/* v2 (2023-09-10) */
6363
pthread_t (*pthread_self)(void);
6464
void (*dispatch_release)(dispatch_semaphore_t);
65-
int (*raise)(int);
65+
long (*raise)(int);
6666
int (*pthread_join)(pthread_t, void **);
6767
void (*pthread_yield_np)(void);
6868
int pthread_stack_min;
@@ -441,6 +441,10 @@ static long sys_close(int fd) {
441441
return sysret(close(fd));
442442
}
443443

444+
static long sys_raise(int sig) {
445+
return sysret(raise(sig));
446+
}
447+
444448
static long sys_pipe(int pfds[2]) {
445449
return sysret(pipe(pfds));
446450
}
@@ -888,7 +892,7 @@ int main(int argc, char **argv, char **envp) {
888892
M->lib.dispatch_walltime = dispatch_walltime;
889893
M->lib.pthread_self = pthread_self;
890894
M->lib.dispatch_release = dispatch_release;
891-
M->lib.raise = raise;
895+
M->lib.raise = sys_raise;
892896
M->lib.pthread_join = pthread_join;
893897
M->lib.pthread_yield_np = pthread_yield_np;
894898
M->lib.pthread_stack_min = PTHREAD_STACK_MIN;

libc/calls/raise.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,19 @@
3434
*
3535
* Note `SIG_DFL` still results in process death for most signals.
3636
*
37+
* POSIX defines raise() errors as returning non-zero and makes setting
38+
* `errno` optional. Every platform we've tested in our support vector
39+
* returns -1 with `errno` on error (like a normal system call).
40+
*
3741
* @param sig can be SIGALRM, SIGINT, SIGTERM, SIGKILL, etc.
38-
* @return 0 on success, or nonzero on failure
42+
* @return 0 on success, or -1 w/ errno
3943
* @raise EINVAL if `sig` is invalid
4044
* @asyncsignalsafe
4145
*/
4246
int raise(int sig) {
4347
int rc;
4448
if (IsXnuSilicon()) {
45-
rc = __syslib->__raise(sig);
49+
rc = _sysret(__syslib->__raise(sig));
4650
} else if (IsWindows()) {
4751
if (0 <= sig && sig <= 64) {
4852
__sig_raise(sig, SI_TKILL);

libc/cxxabi.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef _CXXABI_H
2+
#define _CXXABI_H
3+
COSMOPOLITAN_C_START_
4+
5+
union CxaGuardValue;
6+
7+
char *__cxa_demangle(const char *, char *, size_t *, int *);
8+
int __cxa_atexit(void (*)(void *), void *, void *) paramsnonnull((1)) dontthrow;
9+
int __cxa_guard_acquire(union CxaGuardValue *);
10+
int __cxa_thread_atexit(void *, void *, void *) dontthrow;
11+
void __cxa_finalize(void *);
12+
void __cxa_guard_abort(union CxaGuardValue *) dontthrow;
13+
void __cxa_guard_release(union CxaGuardValue *) dontthrow;
14+
void __cxa_pure_virtual(void) wontreturn;
15+
16+
COSMOPOLITAN_C_END_
17+
#endif /* _CXXABI_H */

libc/dns/gethoststxt.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/calls/calls.h"
20+
#include "libc/cxxabi.h"
2021
#include "libc/dce.h"
2122
#include "libc/dns/hoststxt.h"
2223
#include "libc/dns/servicestxt.h"
@@ -64,7 +65,7 @@ const struct HostsTxt *GetHostsTxt(void) {
6465
init->ht.entries.p = init->entries;
6566
init->ht.strings.n = pushpop(ARRAYLEN(init->strings));
6667
init->ht.strings.p = init->strings;
67-
__cxa_atexit(FreeHostsTxt, &g_hoststxt, NULL);
68+
__cxa_atexit((void *)FreeHostsTxt, &g_hoststxt, NULL);
6869
if ((f = fopen(GetHostsTxtPath(pathbuf, sizeof(pathbuf)), "r"))) {
6970
if (ParseHostsTxt(g_hoststxt, f) == -1) {
7071
/* TODO(jart): Elevate robustness. */

libc/dns/getresolvconf.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/cxxabi.h"
1920
#include "libc/dce.h"
2021
#include "libc/dns/resolvconf.h"
2122
#include "libc/fmt/fmt.h"
@@ -48,7 +49,7 @@ const struct ResolvConf *GetResolvConf(void) {
4849
g_resolvconf = &init->rv;
4950
pushmov(&init->rv.nameservers.n, ARRAYLEN(init->nameservers));
5051
init->rv.nameservers.p = init->nameservers;
51-
__cxa_atexit(FreeResolvConf, &g_resolvconf, NULL);
52+
__cxa_atexit((void *)FreeResolvConf, &g_resolvconf, NULL);
5253
if (!IsWindows()) {
5354
if ((f = fopen("/etc/resolv.conf", "r"))) {
5455
rc = ParseResolvConf(g_resolvconf, f);

libc/intrin/atexit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/cxxabi.h"
1920
#include "libc/runtime/runtime.h"
2021

2122
/**
@@ -29,5 +30,5 @@
2930
* @return 0 on success or nonzero if out of space
3031
*/
3132
int atexit(void f(void)) {
32-
return __cxa_atexit(f, 0, 0);
33+
return __cxa_atexit((void *)f, 0, 0);
3334
}

libc/intrin/cxaatexit.internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ void __cxa_lock(void);
2323
void __cxa_unlock(void);
2424
void __cxa_thread_finalize(void);
2525
void __cxa_printexits(FILE *, void *);
26-
int __cxa_thread_atexit(void *, void *, void *);
2726
int __cxa_thread_atexit_impl(void *, void *, void *);
2827

2928
COSMOPOLITAN_C_END_

libc/intrin/describebacktrace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "libc/log/libfatal.internal.h"
2121
#include "libc/nexgen32e/stackframe.h"
2222

23-
#define N 100
23+
#define N 160
2424

2525
dontinstrument const char *(DescribeBacktrace)(char buf[N],
2626
struct StackFrame *fr) {

libc/intrin/describebacktrace.internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#if !(__ASSEMBLER__ + __LINKER__ + 0)
66
COSMOPOLITAN_C_START_
77

8-
const char *DescribeBacktrace(char[100], struct StackFrame *);
9-
#define DescribeBacktrace(x) DescribeBacktrace(alloca(100), x)
8+
const char *DescribeBacktrace(char[160], struct StackFrame *);
9+
#define DescribeBacktrace(x) DescribeBacktrace(alloca(160), x)
1010

1111
COSMOPOLITAN_C_END_
1212
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

0 commit comments

Comments
 (0)