Get Cosmopolitan into releasable state

A new rollup tool now exists for flattening out the headers in a way
that works better for our purposes than cpp. A lot of the API clutter
has been removed. APIs that aren't a sure thing in terms of general
recommendation are now marked internal.

There's now a smoke test for the amalgamation archive and gigantic
header file. So we can now guarantee you can use this project on the
easiest difficulty setting without the gigantic repository.

A website is being created, which is currently a work in progress:
https://justine.storage.googleapis.com/cosmopolitan/index.html
This commit is contained in:
Justine Tunney 2020-11-25 08:19:00 -08:00
parent dba7552c1e
commit ea0b5d9d1c
775 changed files with 6864 additions and 3963 deletions

View file

@ -165,7 +165,7 @@ int arch_prctl(int code, int64_t addr) {
if (g_fsgs_once == 2) {
return arch_prctl$fsgsbase(code, addr);
}
switch (hostos) {
switch (__hostos) {
case METAL:
return arch_prctl$msr(code, addr);
case FREEBSD:

View file

@ -48,7 +48,7 @@ relegated void __assert_fail(const char *expr, const char *file, int line) {
p = mempcpy(p, expr, exprlen);
p = stpcpy(p, "\r\n");
write(STDERR_FILENO, msg, p - msg);
if (weaken(die)) weaken(die)();
if (weaken(__die)) weaken(__die)();
}
abort();
unreachable;

View file

@ -19,7 +19,8 @@
*/
#include "libc/calls/calls.h"
#include "libc/runtime/ezmap.h"
#include "libc/runtime/symbols.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
/**
* Frees symbol table.
@ -32,7 +33,7 @@ int CloseSymbolTable(struct SymbolTable **table) {
if (*table && *table != MAP_FAILED) {
t = *table;
*table = NULL;
rc |= unmapfile(&t->mf);
rc |= UnmapFile(&t->mf);
rc |= munmap(t, t->scratch);
}
return rc;

View file

@ -17,12 +17,12 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/arraylist.h"
#include "libc/alg/arraylist.internal.h"
#include "libc/assert.h"
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/gc.h"
#include "libc/nexgen32e/gc.internal.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/runtime.h"
@ -53,10 +53,10 @@ void __defer(struct StackFrame *frame, void *fn, void *arg) {
frame2 = __builtin_frame_address(0);
assert(frame2->next == frame);
assert(PointerNotOwnedByParentStackFrame(frame2, frame, arg));
if (append(&g_garbage,
if (append(&__garbage,
(&(const struct Garbage){frame->next, (intptr_t)fn, (intptr_t)arg,
frame->addr})) != -1) {
atomic_store(&frame->addr, (intptr_t)&CollectGarbage);
atomic_store(&frame->addr, (intptr_t)&__gc);
} else {
abort();
}

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/limits.h"
#include "libc/runtime/ezmap.h"
@ -30,7 +30,7 @@
* Memory-maps file for reading.
* An internal veneer for a common mmap() use-case.
*/
int mapfileread(const char *filename, struct MappedFile *mf) {
int MapFileRead(const char *filename, struct MappedFile *mf) {
mf->addr = MAP_FAILED;
if ((mf->fd = open(filename, O_RDONLY)) != -1 &&
(mf->size = getfiledescriptorsize(mf->fd)) < INT_MAX &&
@ -39,15 +39,15 @@ int mapfileread(const char *filename, struct MappedFile *mf) {
: NULL) != MAP_FAILED) {
return 0;
} else {
unmapfile(mf);
UnmapFile(mf);
return -1;
}
}
/**
* Releases resource returned by mapfileread().
* Releases resource returned by MapFileRead().
*/
int unmapfile(struct MappedFile *mf) {
int UnmapFile(struct MappedFile *mf) {
int rc;
rc = 0;
if (mf->addr && mf->addr != MAP_FAILED) {

View file

@ -1,22 +1,3 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=8 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#ifndef COSMOPOLITAN_LIBC_EZMAP_H_
#define COSMOPOLITAN_LIBC_EZMAP_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
@ -28,8 +9,8 @@ struct MappedFile {
size_t size;
};
int mapfileread(const char *, struct MappedFile *) hidden;
int unmapfile(struct MappedFile *) hidden;
int MapFileRead(const char *, struct MappedFile *) hidden;
int UnmapFile(struct MappedFile *) hidden;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -22,7 +22,7 @@
#include "libc/errno.h"
#include "libc/macros.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/auxv.h"

View file

@ -17,9 +17,9 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/bisectcarleft.h"
#include "libc/alg/bisectcarleft.internal.h"
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/sigset.h"
@ -31,9 +31,9 @@
#include "libc/nt/runtime.h"
#include "libc/nt/thunk/msabi.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/missioncritical.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/nr.h"
#include "libc/sysv/consts/prot.h"
@ -71,7 +71,6 @@ privileged interruptfn void ftrace_hook(void) {
size_t i, j, nesting;
const char *symbol;
struct StackFrame *frame;
LOAD_DEFAULT_RBX();
if (g_symbols) {
frame = __builtin_frame_address(0);
symbol =
@ -98,7 +97,6 @@ privileged interruptfn void ftrace_hook(void) {
}
g_lastsymbol = symbol;
}
RESTORE_RBX();
}
/**

View file

@ -20,7 +20,7 @@
#include "libc/assert.h"
#include "libc/bits/bits.h"
#include "libc/bits/pushpop.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/runtime/internal.h"
#include "libc/str/appendchar.h"
#include "libc/str/str.h"

View file

@ -1,73 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=8 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#ifndef COSMOPOLITAN_LIBC_DOSENVIRON_H_
#define COSMOPOLITAN_LIBC_DOSENVIRON_H_
#ifndef __STRICT_ANSI__
#include "libc/bits/safemacros.h"
#include "libc/str/appendchar.h"
#include "libc/str/str.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
/**
* Transcodes NT environment variable block from UTF-16 to UTF-8.
*
* @param env is a double NUL-terminated block of key=values
* @param buf is the new environment
* @param size is the byte capacity of buf
* @param envp stores NULL-terminated string pointer list
* @param max is the pointer count capacity of envp
* @return number of variables decoded, excluding NULL-terminator
*/
static inline int GetDosEnviron(const char16_t *env, char *buf, size_t size,
char **envp, size_t max) {
wint_t wc;
size_t envc;
char *p, *pe;
bool endstring;
const char16_t *s;
s = env;
envc = 0;
if (size) {
p = buf;
pe = buf + size - 1;
if (p < pe) {
wc = DecodeNtsUtf16(&s);
while (wc) {
if (++envc < max) {
envp[envc - 1] = p < pe ? p : NULL;
}
do {
AppendChar(&p, pe, wc);
endstring = !wc;
wc = DecodeNtsUtf16(&s);
} while (!endstring);
buf[min(p - buf, size - 2)] = u'\0';
}
}
AppendChar(&p, pe, '\0');
buf[min(p - buf, size - 1)] = u'\0';
}
if (max) envp[min(envc, max - 1)] = NULL;
return envc;
}
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* !ANSI */
#endif /* COSMOPOLITAN_LIBC_DOSENVIRON_H_ */

View file

@ -0,0 +1,55 @@
#ifndef COSMOPOLITAN_LIBC_DOSENVIRON_H_
#define COSMOPOLITAN_LIBC_DOSENVIRON_H_
#ifndef __STRICT_ANSI__
#include "libc/bits/safemacros.internal.h"
#include "libc/str/appendchar.h"
#include "libc/str/str.h"
#include "libc/str/utf16.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
/**
* Transcodes NT environment variable block from UTF-16 to UTF-8.
*
* @param env is a double NUL-terminated block of key=values
* @param buf is the new environment
* @param size is the byte capacity of buf
* @param envp stores NULL-terminated string pointer list
* @param max is the pointer count capacity of envp
* @return number of variables decoded, excluding NULL-terminator
*/
static inline int GetDosEnviron(const char16_t *env, char *buf, size_t size,
char **envp, size_t max) {
wint_t wc;
size_t envc;
char *p, *pe;
bool endstring;
const char16_t *s;
s = env;
envc = 0;
if (size) {
p = buf;
pe = buf + size - 1;
if (p < pe) {
wc = DecodeNtsUtf16(&s);
while (wc) {
if (++envc < max) {
envp[envc - 1] = p < pe ? p : NULL;
}
do {
AppendChar(&p, pe, wc);
endstring = !wc;
wc = DecodeNtsUtf16(&s);
} while (!endstring);
buf[min(p - buf, size - 2)] = u'\0';
}
}
AppendChar(&p, pe, '\0');
buf[min(p - buf, size - 1)] = u'\0';
}
if (max) envp[min(envc, max - 1)] = NULL;
return envc;
}
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* !ANSI */
#endif /* COSMOPOLITAN_LIBC_DOSENVIRON_H_ */

View file

@ -18,7 +18,7 @@
02110-1301 USA
*/
#include "libc/conv/itoa.h"
#include "libc/runtime/missioncritical.h"
#include "libc/runtime/runtime.h"
/**
* Debugs super low-level stuff, e.g.

View file

@ -56,7 +56,7 @@
* @note tiny programs might need to explicitly YOINK(realloc)
* @see test/libc/runtime/grow_test.c
*/
bool grow(void *pp, size_t *capacity, size_t itemsize, size_t extra) {
bool __grow(void *pp, size_t *capacity, size_t itemsize, size_t extra) {
void **p, *p1, *p2;
size_t n1, n2; /* item counts */
size_t t1, t2; /* byte counts */

View file

@ -22,7 +22,7 @@
#include "libc/calls/internal.h"
#include "libc/calls/struct/sigset.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/sysv/consts/prot.h"
/**

View file

@ -2,6 +2,8 @@
#define COSMOPOLITAN_LIBC_RUNTIME_INTERNAL_H_
#ifndef __STRICT_ANSI__
#include "libc/dce.h"
#include "libc/elf/struct/ehdr.h"
#include "libc/runtime/ezmap.h"
#include "libc/runtime/runtime.h"
#define STACK_CEIL 0x700000000000ul
@ -23,6 +25,7 @@ void __stack_chk_fail_local(void) noreturn relegated hidden;
void _jmpstack(void *, void *, ...) hidden noreturn;
long _setstack(void *, void *, ...) hidden;
int GetDosArgv(const char16_t *, char *, size_t, char **, size_t) hidden;
Elf64_Ehdr *MapElfRead(const char *, struct MappedFile *) hidden;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -17,8 +17,8 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/progn.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/progn.internal.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/interruptiblecall.h"
#include "libc/runtime/runtime.h"

View file

@ -20,12 +20,13 @@
#include "libc/elf/def.h"
#include "libc/elf/elf.h"
#include "libc/runtime/ezmap.h"
#include "libc/runtime/internal.h"
Elf64_Ehdr *mapelfread(const char *filename, struct MappedFile *mf) {
if (mapfileread(filename, mf) != -1 && IsElf64Binary(mf->addr, mf->size)) {
Elf64_Ehdr *MapElfRead(const char *filename, struct MappedFile *mf) {
if (MapFileRead(filename, mf) != -1 && IsElf64Binary(mf->addr, mf->size)) {
return mf->addr;
} else {
unmapfile(mf);
UnmapFile(mf);
return NULL;
}
}

View file

@ -1,147 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_INTERNAL_MISSIONCRITICAL_H_
#define COSMOPOLITAN_LIBC_INTERNAL_MISSIONCRITICAL_H_
#ifndef __STRICT_ANSI__
#include "libc/bits/bits.h"
#include "libc/calls/internal.h"
#include "libc/dce.h"
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/nexgen32e/tinystrlen.h"
#include "libc/nt/console.h"
#include "libc/nt/enum/version.h"
#include "libc/nt/ntdll.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/teb.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/nr.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/errfuns.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
/**
* @fileoverview Mission critical system calls.
*/
#define PRINT(STR) __print(STR, tinystrlen(STR))
#define NT_HAVE_IMPORT(SLOT) \
((void *)*SLOT && *SLOT != (void *)&missingno /* see libc/crt/crt.S */)
#define _EXIT(rc) \
({ \
int ExitAx; \
asm volatile("syscall" \
: "=a"(ExitAx) \
: "0"(__NR_exit), "D"(rc) \
: "rcx", "r11", "cc", "memory"); \
ExitAx; \
})
#define GETPID() \
({ \
int Res; \
asm("syscall" : "=a"(Res) : "0"(__NR_getpid) : "rcx", "r11", "cc"); \
Res; \
})
#define KILL(pid, sig) \
({ \
int KillAx; \
unsigned char Cf; \
asm volatile(CFLAG_ASM("clc\n\t" \
"syscall") \
: CFLAG_CONSTRAINT(Cf), "=a"(KillAx) \
: "1"(__NR_kill), "D"(pid), "S"(sig) \
: "rcx", "r11", "cc", "memory"); \
Cf ? -KillAx : KillAx; \
})
#define RAISE(SIG) \
({ \
int RaiseAx = -1; \
int Sig = (SIG); \
if (Sig == SIGTRAP) { \
DebugBreak(); \
} else if (!IsWindows()) { \
RaiseAx = KILL(GETPID(), Sig); \
} else { \
switch (Sig) { \
case SIGINT: \
GenerateConsoleCtrlEvent(kNtCtrlCEvent, 0); \
break; \
case SIGHUP: \
GenerateConsoleCtrlEvent(kNtCtrlCloseEvent, 0); \
break; \
case SIGQUIT: \
GenerateConsoleCtrlEvent(kNtCtrlBreakEvent, 0); \
break; \
default: \
for (;;) TerminateProcess(GetCurrentProcess(), 128 + Sig); \
} \
} \
RaiseAx; \
})
#define SCHED_YIELD() \
({ \
int64_t SyAx; \
if (!IsWindows()) { \
asm volatile("syscall" \
: "=a"(SyAx) \
: "0"(__NR_sched_yield) \
: "rcx", "r11", "cc", "memory"); \
} else { \
NtYieldExecution(); \
} \
0; \
})
#define WAIT4(PID, OPT_OUT_WSTATUS, OPTIONS, OPT_OUT_RUSAGE) \
({ \
int64_t WaAx; \
if (!IsWindows()) { \
register void *Reg10 asm("r10") = (OPT_OUT_RUSAGE); \
asm volatile("syscall" \
: "=a"(WaAx) \
: "0"(__NR_wait4), "D"(PID), "S"(OPT_OUT_WSTATUS), \
"d"(OPTIONS), "r"(Reg10) \
: "rcx", "r11", "cc", "memory"); \
} else { \
WaAx = wait4$nt(PID, OPT_OUT_WSTATUS, OPTIONS, OPT_OUT_RUSAGE); \
} \
WaAx; \
})
#if 0
/**
* Exits on Windows the hard way.
*/
#endif
#define NT_TERMINATE_PROCESS() \
do \
if (NtGetVersion() < kNtVersionFuture) { \
int64_t ax, cx; \
do \
asm volatile( \
"syscall" /* hook THIS system call */ \
: "=a"(ax), "=c"(cx) \
: "0"(NtGetVersion() < kNtVersionWindows8 \
? 0x0029 \
: NtGetVersion() < kNtVersionWindows81 \
? 0x002a \
: NtGetVersion() < kNtVersionWindows10 ? 0x002b \
: 0x002c), \
"1"(-1L /* GetCurrentProcess() */), "d"(42) \
: "r11", "cc", "memory"); \
while (!ax); \
} \
while (0)
void __print(const void *, size_t);
void __print_string(const char *);
#define LOAD_DEFAULT_RBX() /* disabled for now b/c clang */
#define RESTORE_RBX() /* disabled for now b/c clang */
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* !ANSI */
#endif /* COSMOPOLITAN_LIBC_INTERNAL_MISSIONCRITICAL_H_ */

View file

@ -22,7 +22,7 @@
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/dce.h"
#include "libc/log/asan.h"
#include "libc/log/asan.internal.h"
#include "libc/macros.h"
#include "libc/rand/rand.h"
#include "libc/runtime/directmap.h"

View file

@ -22,8 +22,9 @@
#include "libc/elf/def.h"
#include "libc/elf/elf.h"
#include "libc/runtime/carsort.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
@ -38,15 +39,15 @@ struct SymbolTable *OpenSymbolTable(const char *filename) {
const Elf64_Sym *symtab, *sym;
t = MAP_FAILED;
if (filename && (t = mapanon(BIGPAGESIZE)) != MAP_FAILED &&
mapelfread(filename, &t->mf) &&
MapElfRead(filename, &t->mf) &&
(t->name_base = GetElfStringTable(t->elf, t->elfsize)) != NULL &&
(symtab = GetElfSymbolTable(t->elf, t->elfsize, &t->count)) &&
sizeof(struct SymbolTable) + sizeof(struct Symbol) * t->count <
(t->scratch = BIGPAGESIZE)) {
getelfvirtualaddressrange(t->elf, t->elfsize, &t->addr_base, &t->addr_end);
GetElfVirtualAddressRange(t->elf, t->elfsize, &t->addr_base, &t->addr_end);
for (j = i = 0; i < t->count; ++i) {
sym = &symtab[i];
if (iselfsymbolcontent(sym) &&
if (IsElfSymbolContent(sym) &&
(sym->st_value >= t->addr_base && sym->st_value <= t->addr_end)) {
t->symbols[j].addr_rva = (unsigned)(sym->st_value - t->addr_base);
t->symbols[j].name_rva = sym->st_name;

View file

@ -33,7 +33,7 @@
αcτµαlly pδrταblε εxεcµταblε § post-initialization read-only
*/
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/bits/weaken.h"
#include "libc/calls/calls.h"
#include "libc/runtime/internal.h"

View file

@ -21,12 +21,14 @@
#include "libc/dce.h"
#include "libc/nt/files.h"
#include "libc/nt/runtime.h"
#include "libc/runtime/missioncritical.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/nr.h"
#define WasImported(SLOT) \
((void *)*SLOT && *SLOT != (void *)&missingno /* see libc/crt/crt.S */)
static privileged void __print$nt(const void *data, size_t len) {
int64_t hand;
uint32_t wrote;
@ -47,11 +49,10 @@ static privileged void __print$nt(const void *data, size_t len) {
*
* @param len can be computed w/ tinystrlen()
* @clob nothing except flags
* @see PRINT()
*/
privileged void __print(const void *data, size_t len) {
int64_t ax, ordinal;
if (NT_HAVE_IMPORT(__imp_WriteFile)) {
if (WasImported(__imp_WriteFile)) {
__print$nt(data, len);
} else {
ordinal = __NR_write > 0 ? __NR_write : IsXnu() ? 0x2000004 : 4;
@ -59,14 +60,13 @@ privileged void __print(const void *data, size_t len) {
: "=a"(ax)
: "0"(ordinal), "D"(STDERR_FILENO), "S"(data), "d"(len)
: "rcx", "r11", "memory", "cc");
if (ax == -1 && !hostos && !__NR_write) {
if (ax == -1 && !__hostos && !__NR_write) {
asm volatile("syscall"
: "=a"(ax)
: "0"(ordinal), "D"(STDERR_FILENO), "S"(data), "d"(len)
: "rcx", "r11", "memory", "cc");
}
}
RESTORE_RBX();
}
privileged void __print_string(const char *s) {

View file

@ -55,7 +55,6 @@ int putenv(char *) paramsnonnull();
int setenv(const char *, const char *, int) paramsnonnull();
int unsetenv(const char *);
int clearenv(void);
void __fast_math(void);
void fpreset(void);
void savexmm(void *);
void loadxmm(void *);
@ -68,6 +67,9 @@ void *mremap(void *, uint64_t, uint64_t, int32_t, void *);
int munmap(void *, uint64_t);
int mprotect(void *, uint64_t, int) privileged;
int msync(void *, size_t, int);
void __print(const void *, size_t);
void __print_string(const char *);
void __fast_math(void);
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § runtime » optimizations

View file

@ -17,12 +17,11 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "ape/config.h"
#include "libc/bits/bits.h"
#include "libc/bits/pushpop.h"
#include "libc/nt/process.h"
#include "libc/nt/enum/version.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/teb.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/missioncritical.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/nr.h"
@ -32,11 +31,13 @@
* Aborts program under enemy fire to avoid being taken alive.
*/
void __stack_chk_fail(void) {
size_t len;
const char *msg;
int64_t ax, cx, si;
if (!IsWindows()) {
const char *const msg = STACK_SMASH_MESSAGE;
const size_t len = pushpop(sizeof(STACK_SMASH_MESSAGE) - 1);
msg = STACK_SMASH_MESSAGE;
len = pushpop(sizeof(STACK_SMASH_MESSAGE) - 1);
if (!IsMetal()) {
unsigned ax;
asm volatile("syscall"
: "=a"(ax)
: "0"(__NR_write), "D"(pushpop(STDERR_FILENO)), "S"(msg),
@ -44,18 +45,31 @@ void __stack_chk_fail(void) {
: "rcx", "r11", "cc", "memory");
asm volatile("syscall"
: "=a"(ax)
: "0"(__NR_exit), "D"(pushpop(88))
: "0"(__NR_exit), "D"(pushpop(23))
: "rcx", "r11", "cc", "memory");
}
short(*ttys)[4] = (short(*)[4])XLM(BIOS_DATA_AREA);
unsigned long si;
unsigned cx;
asm volatile("rep outsb"
: "=S"(si), "=c"(cx)
: "0"(msg), "1"(len), "d"((*ttys)[1 /*COM2*/])
: "0"(msg), "1"(len), "d"(0x3F8 /* COM1 */)
: "memory");
triplf();
}
NT_TERMINATE_PROCESS();
for (;;) TerminateProcess(GetCurrentProcess(), 42);
if (NtGetVersion() < kNtVersionFuture) {
do {
asm volatile(
"syscall"
: "=a"(ax), "=c"(cx)
: "0"(NtGetVersion() < kNtVersionWindows8
? 0x0029
: NtGetVersion() < kNtVersionWindows81
? 0x002a
: NtGetVersion() < kNtVersionWindows10 ? 0x002b
: 0x002c),
"1"(pushpop(-1L)), "d"(42)
: "r11", "cc", "memory");
} while (!ax);
}
for (;;) {
TerminateProcess(GetCurrentProcess(), 42);
}
}

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"

View file

@ -26,21 +26,21 @@
#include "libc/macros.h"
#include "libc/nt/console.h"
#include "libc/nt/enum/consolemodeflags.h"
#include "libc/nt/enum/filemapflags.h"
#include "libc/nt/enum/filetype.h"
#include "libc/nt/enum/loadlibrarysearch.h"
#include "libc/nt/enum/pageflags.h"
#include "libc/nt/enum/version.h"
#include "libc/nt/files.h"
#include "libc/nt/memory.h"
#include "libc/nt/pedef.h"
#include "libc/nt/pedef.internal.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/teb.h"
#include "libc/runtime/getdosenviron.h"
#include "libc/runtime/getdosenviron.internal.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/memtrack.h"
#include "libc/runtime/missioncritical.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/winmain.h"
#include "libc/sock/internal.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
@ -62,8 +62,6 @@ static struct CmdExe {
} oldin, oldout;
} g_cmdexe;
struct WinMain g_winmain;
static textwindows void RestoreCmdExe(void) {
if (g_cmdexe.oldin.handle) {
SetConsoleCP(g_cmdexe.oldin.codepage);
@ -122,21 +120,15 @@ static textwindows char *AllocateMemory(void *addr, size_t size, int64_t *h) {
kNtNumaNoPreferredNode);
}
static textwindows noreturn void WinMainNew(int64_t hInstance,
int64_t hPrevInstance,
const char *lpCmdLine,
int nCmdShow) {
static textwindows noreturn void WinMainNew(void) {
int64_t h;
size_t size;
int i, count;
uint64_t data;
struct WinArgs *wa;
const char16_t *env16;
g_winmain.nCmdShow = nCmdShow;
g_winmain.hInstance = hInstance;
g_winmain.hPrevInstance = hPrevInstance;
NormalizeCmdExe();
*(/*unconst*/ int *)&hostos = WINDOWS;
*(/*unconst*/ int *)&__hostos = WINDOWS;
size = ROUNDUP(STACKSIZE + sizeof(struct WinArgs), FRAMESIZE);
data = 0x777000000000;
data = (intptr_t)AllocateMemory((char *)data, size, &_mmi.p[0].h);
@ -191,11 +183,12 @@ static textwindows noreturn void WinMainNew(int64_t hInstance,
* from having fork() so we pass pipe handles in an environment
* variable literally copy all the memory.
*
* @param hInstance call GetModuleHandle(NULL) from main if you need it
*/
textwindows int64_t WinMain(int64_t hInstance, int64_t hPrevInstance,
const char *lpCmdLine, int nCmdShow) {
SetDefaultDllDirectories(kNtLoadLibrarySearchSearchSystem32);
if (weaken(winsockinit)) weaken(winsockinit)();
if (weaken(WinMainForked)) weaken(WinMainForked)();
WinMainNew(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
WinMainNew();
}

View file

@ -1,16 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_RUNTIME_WINMAIN_H_
#define COSMOPOLITAN_LIBC_RUNTIME_WINMAIN_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct WinMain {
int nCmdShow;
int64_t hInstance;
int64_t hPrevInstance;
};
extern struct WinMain g_winmain;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_RUNTIME_WINMAIN_H_ */