Fix bugs in cosmocc toolchain

This change integrates e58abc1110b335a3341e8ad5821ad8e3880d9bb2 from
https://github.com/ahgamut/musl-cross-make/ which fixes the issues we
were having with our C language extension for symbolic constants. This
change also performs some code cleanup and bug fixes to getaddrinfo().
It's now possible to compile projects like ncurses, readline and python
without needing to patch anything upstream, except maybe a line or two.
Pretty soon it should be possible to build a Linux distro on Cosmo.
This commit is contained in:
Justine Tunney 2023-06-08 23:44:03 -07:00
parent 22f81a8d50
commit 23e235b7a5
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
272 changed files with 3491 additions and 4350 deletions

View file

@ -650,7 +650,7 @@ static char *Tokenize(void) {
break;
default:
unreachable;
__builtin_unreachable();
}
}
}

View file

@ -144,7 +144,7 @@ textstartup void cosmo(long *sp, struct Syslib *m1) {
#endif
// initialize file system
InitializeFileDescriptors();
__init_fds();
// set helpful globals
__argc = argc;

View file

@ -258,7 +258,7 @@ __msabi noasan EFI_STATUS EfiMain(EFI_HANDLE ImageHandle,
* Switches to copied image and launches program.
*/
_EfiPostboot(mm, pml4t, Args, ArgBlock->Args);
unreachable;
__builtin_unreachable();
}
#endif /* __x86_64__ */

View file

@ -26,10 +26,8 @@
#include "libc/runtime/runtime.h"
#include "libc/thread/tls.h"
extern int __threadcalls_end[];
extern int __threadcalls_start[];
#pragma weak __threadcalls_start
#pragma weak __threadcalls_end
extern int __threadcalls_end[] __attribute__((__weak__));
extern int __threadcalls_start[] __attribute__((__weak__));
static privileged dontinline void FixupLockNops(void) {
sigset_t mask;

View file

@ -0,0 +1,34 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/log/log.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
/**
* Returns name of symbol at address.
*/
noasan char *GetSymbolByAddr(int64_t addr) {
/* asan runtime depends on this function */
int i;
struct SymbolTable *st;
st = GetSymbolTable();
i = __get_symbol(st, addr);
if (i == -1) i = __get_symbol(st, addr - 1);
return __get_symbol_name(st, i);
}

View file

@ -117,7 +117,7 @@ privileged noinstrument noasan int __hook(void *dest, struct SymbolTable *st) {
for (i = 0; i < st->count; ++i) {
if (st->symbols[i].x < 9) continue;
if (st->addr_base + st->symbols[i].x < lowest) continue;
if (st->addr_base + st->symbols[i].y >= (intptr_t)__privileged_addr) break;
if (st->addr_base + st->symbols[i].y >= (intptr_t)__privileged_start) break;
p = (code_t *)((char *)st->addr_base + st->symbols[i].x);
pe = (code_t *)((char *)st->addr_base + st->symbols[i].y);
if (pe - p < 2) continue;

View file

@ -47,8 +47,8 @@ int sys_mprotect_nt(void *, size_t, int) _Hide;
int __inflate(void *, size_t, const void *, size_t);
noasan void *_Mmap(void *, size_t, int, int, int, int64_t) _Hide;
noasan int _Munmap(char *, size_t) _Hide;
void InitializeFileDescriptors(void);
void __on_arithmetic_overflow(void);
void __init_fds(void);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -118,7 +118,7 @@ privileged void __morph_begin(sigset_t *save) {
#else
#error "unsupported architecture"
#endif
__morph_mprotect(__executable_start, __privileged_addr - __executable_start,
__morph_mprotect(__executable_start, __privileged_start - __executable_start,
PROT_READ | PROT_WRITE, kNtPageWritecopy);
}
@ -129,7 +129,7 @@ privileged void __morph_end(sigset_t *save) {
int ax;
long dx;
bool cf;
__morph_mprotect(__executable_start, __privileged_addr - __executable_start,
__morph_mprotect(__executable_start, __privileged_start - __executable_start,
PROT_READ | PROT_EXEC, kNtPageExecuteRead);
#ifdef __x86_64__
if (IsOpenbsd()) {

View file

@ -92,7 +92,7 @@ static noasan void MunmapImpl(char *p, size_t n) {
beg = MAX(_mmi.p[i].x, l);
end = _mmi.p[i].y;
} else {
unreachable;
__builtin_unreachable();
}
// openbsd even requires that if we mapped, for instance a 5 byte
// file, that we be sure to call munmap(file, 5). let's abstract!

View file

@ -0,0 +1,58 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/fmt.h"
#include "libc/intrin/kprintf.h"
#include "libc/log/log.h"
#include "libc/nexgen32e/gc.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/thread/tls.h"
// clang-format off
/**
* Prints list of deferred operations on shadow stack.
*/
void PrintGarbage(void) {
size_t i;
char name[19];
const char *symbol;
struct Garbages *g;
kprintf("\n");
kprintf(" SHADOW STACK %P @ %p\n", __builtin_frame_address(0));
kprintf("garbage ent. parent frame original ret callback arg \n");
kprintf("------------ ------------ ------------------ ------------------ ------------------\n");
if ((g = __tls_enabled ? __get_tls()->tib_garbages:0) && g->i) {
for (i = g->n; i--;) {
symbol = GetSymbolByAddr(g->p[i].ret);
if (symbol) {
ksnprintf(name, sizeof(name), "%s", symbol);
} else {
ksnprintf(name, sizeof(name), "%#014lx", g->p[i].ret);
}
kprintf("%12lx %12lx %18s %18s %#18lx\n",
g->p + i,
g->p[i].frame,
name,
GetSymbolByAddr(g->p[i].fn),
g->p[i].arg);
}
} else {
kprintf("%12s %12s %18s %18s %18s\n","empty","-","-","-","-");
}
kprintf("\n");
}

View file

@ -1,14 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_RUNTIME_SYMBOLIC_H_
#define COSMOPOLITAN_LIBC_RUNTIME_SYMBOLIC_H_
#ifdef __ASSEMBLER__
/* clang-format off */
#define SYMBOLIC(NAME) NAME(%rip)
#define LITERALLY(NAME) $NAME
/* clang-format on */
#else
#define SYMBOLIC(NAME) NAME
#define LITERALLY(NAME) NAME
#endif
#endif /* COSMOPOLITAN_LIBC_RUNTIME_SYMBOLIC_H_ */