Improve cosmocc toolchain

- Decouple zlib from libc
- Add some underscores to mostly internal names
This commit is contained in:
Justine Tunney 2023-03-05 23:52:17 -08:00
parent 713d4424c6
commit ba180e754d
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
25 changed files with 128 additions and 42 deletions

View file

@ -121,6 +121,7 @@ include libc/tinymath/tinymath.mk # │ You can issue raw system calls
include third_party/compiler_rt/compiler_rt.mk # │
include libc/str/str.mk # │
include third_party/xed/xed.mk # │
include third_party/puff/puff.mk # │
include third_party/zlib/zlib.mk # │
include third_party/double-conversion/dc.mk # │
include libc/elf/elf.mk # │
@ -309,7 +310,6 @@ COSMOPOLITAN_OBJECTS = \
LIBC_LOG \
LIBC_TIME \
LIBC_ZIPOS \
THIRD_PARTY_ZLIB \
THIRD_PARTY_MUSL \
LIBC_STDIO \
THIRD_PARTY_GDTOA \
@ -335,6 +335,7 @@ COSMOPOLITAN_OBJECTS = \
LIBC_NT_ADVAPI32 \
LIBC_NT_SYNCHRONIZATION \
LIBC_FMT \
THIRD_PARTY_PUFF \
THIRD_PARTY_COMPILER_RT \
LIBC_TINYMATH \
THIRD_PARTY_XED \

21
examples/getdomainname.c Normal file
View file

@ -0,0 +1,21 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/calls/calls.h"
#include "libc/stdio/stdio.h"
int main(int argc, char *argv[]) {
char buf[256];
if (!getdomainname(buf, sizeof(buf))) {
printf("%s\n", buf);
return 0;
} else {
return 1;
}
}

4
libc/isystem/sys/vfs.h Normal file
View file

@ -0,0 +1,4 @@
#ifndef COSMOPOLITAN_LIBC_ISYSTEM_SYS_VFS_H_
#define COSMOPOLITAN_LIBC_ISYSTEM_SYS_VFS_H_
#include "libc/calls/struct/statfs.h"
#endif /* COSMOPOLITAN_LIBC_ISYSTEM_SYS_VFS_H_ */

View file

@ -1,4 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_ISYSTEM_ZLIB_H_
#define COSMOPOLITAN_LIBC_ISYSTEM_ZLIB_H_
#include "third_party/zlib/zlib.h"
#endif /* COSMOPOLITAN_LIBC_ISYSTEM_ZLIB_H_ */

View file

@ -26,12 +26,12 @@
/**
* Returns maximum number of open files.
*/
long GetMaxFd(void) {
long _GetMaxFd(void) {
int rc;
if (IsNetbsd()) {
if ((rc = __sys_fcntl(0, F_MAXFD, 0)) != -1) {
return rc;
}
}
return GetResourceLimit(RLIMIT_NOFILE);
return _GetResourceLimit(RLIMIT_NOFILE);
}

View file

@ -21,7 +21,7 @@
#include "libc/macros.internal.h"
#include "libc/sysv/consts/rlim.h"
long GetResourceLimit(int resource) {
long _GetResourceLimit(int resource) {
struct rlimit rl;
getrlimit(resource, &rl);
if (rl.rlim_cur == RLIM_INFINITY) return -1;

View file

@ -30,7 +30,7 @@
#include "libc/x/x.h"
#include "libc/zip.h"
#include "libc/zipos/zipos.internal.h"
#include "third_party/zlib/puff.h"
#include "third_party/puff/puff.h"
static pthread_spinlock_t g_lock;
_Hide struct SymbolTable *__symtab; // for kprintf

View file

@ -20,7 +20,7 @@
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/runtime/runtime.h"
#include "third_party/zlib/puff.h"
#include "third_party/puff/puff.h"
#include "third_party/zlib/zlib.h"
/**
@ -54,7 +54,7 @@ int __inflate(void *out, size_t outsize, const void *in, size_t insize) {
rc = rc;
}
} else {
rc = puff(out, &outsize, in, &insize);
rc = _puff(out, &outsize, in, &insize);
}
STRACE("inflate([%#.*hhs%s], %'zu, %#.*hhs%s, %'zu) → %d", MIN(40, outsize),
out, outsize > 40 ? "..." : "", outsize, MIN(40, insize), in,

View file

@ -44,9 +44,9 @@ int GetDosEnviron(const char16_t *, char *, size_t, char **, size_t);
bool __intercept_flag(int *, char *[], const char *);
int sys_mprotect_nt(void *, size_t, int) _Hide;
int __inflate(void *, size_t, const void *, size_t);
noasan void *Mmap(void *addr, size_t size, int prot, int flags, int fd,
noasan void *_Mmap(void *addr, size_t size, int prot, int flags, int fd,
int64_t off) _Hide;
noasan int Munmap(char *, size_t) _Hide;
noasan int _Munmap(char *, size_t) _Hide;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -33,7 +33,7 @@
/**
* Returns true if ELF executable uses dynamic loading magic.
*/
bool IsDynamicExecutable(const char *prog) {
bool _IsDynamicExecutable(const char *prog) {
bool res;
Elf64_Ehdr *e;
Elf64_Phdr *p;

View file

@ -238,7 +238,7 @@ static textwindows dontinline noasan void *MapMemories(char *addr, size_t size,
return addr;
}
noasan inline void *Mmap(void *addr, size_t size, int prot, int flags, int fd,
noasan inline void *_Mmap(void *addr, size_t size, int prot, int flags, int fd,
int64_t off) {
char *p = addr;
struct DirectMap dm;
@ -494,7 +494,7 @@ void *mmap(void *addr, size_t size, int prot, int flags, int fd, int64_t off) {
#endif
__mmi_lock();
if (!__isfdkind(fd, kFdZip)) {
res = Mmap(addr, size, prot, flags, fd, off);
res = _Mmap(addr, size, prot, flags, fd, off);
} else {
res = _weaken(__zipos_Mmap)(
addr, size, prot, flags,

View file

@ -53,7 +53,7 @@ static noasan void MunmapShadow(char *p, size_t n) {
// to be >1mb since we can only unmap it if it's aligned, and
// as such we poison the edges if there are any.
__repstosb((void *)a, kAsanUnmapped, x - a);
Munmap((void *)x, y - x);
_Munmap((void *)x, y - x);
__repstosb((void *)y, kAsanUnmapped, b - y);
} else {
// otherwise just poison and assume reuse
@ -113,7 +113,7 @@ static noasan void MunmapImpl(char *p, size_t n) {
}
}
noasan int Munmap(char *p, size_t n) {
noasan int _Munmap(char *p, size_t n) {
unsigned i;
char poison;
intptr_t a, b, x, y;
@ -157,7 +157,7 @@ int munmap(void *p, size_t n) {
int rc;
size_t toto;
__mmi_lock();
rc = Munmap(p, n);
rc = _Munmap(p, n);
#if SYSDEBUG
toto = __strace > 0 ? GetMemtrackSize(&_mmi) : 0;
#endif

View file

@ -30,7 +30,7 @@
// @return file descriptor
// @note only works on .com binary (not .com.dbg)
// @note only supports linux, freebsd, openbsd, and netbsd
OpenExecutable:
_OpenExecutable:
push %rbp
mov %rsp,%rbp
pushq __NR_open(%rip) # -0x08(%rbp)
@ -198,7 +198,7 @@ OpenExecutable:
pop %rbx
leave
ret
9: .endfn OpenExecutable,globl
9: .endfn _OpenExecutable,globl
.weak ape_rom_vaddr
.weak ape_rom_filesz

View file

@ -94,12 +94,12 @@ void _peekall(void);
void _savexmm(void *);
void _weakfree(void *);
void free_s(void *) paramsnonnull() libcesque;
int OpenExecutable(void);
int _OpenExecutable(void);
int ftrace_install(void);
int ftrace_enabled(int);
int strace_enabled(int);
long GetResourceLimit(int);
long GetMaxFd(void);
long _GetResourceLimit(int);
long _GetMaxFd(void);
char *GetProgramExecutableName(void);
char *GetInterpreterExecutableName(char *, size_t);
void __printargs(const char *);
@ -108,7 +108,7 @@ int __arg_max(void);
void __print_maps(void);
void __warn_if_powersave(void);
const char *__describe_os(void);
bool IsDynamicExecutable(const char *);
bool _IsDynamicExecutable(const char *);
void _restorewintty(void);
const char *GetCpuidOs(void);
const char *GetCpuidEmulator(void);

View file

@ -45,7 +45,7 @@ LIBC_RUNTIME_A_DIRECTDEPS = \
LIBC_STUBS \
LIBC_SYSV \
LIBC_SYSV_CALLS \
THIRD_PARTY_ZLIB \
THIRD_PARTY_PUFF \
THIRD_PARTY_XED
LIBC_RUNTIME_A_DEPS := \

View file

@ -41,11 +41,11 @@ long sysconf(int name) {
case _SC_ARG_MAX:
return _ARG_MAX;
case _SC_CHILD_MAX:
return GetResourceLimit(RLIMIT_NPROC);
return _GetResourceLimit(RLIMIT_NPROC);
case _SC_CLK_TCK:
return CLK_TCK;
case _SC_OPEN_MAX:
return GetMaxFd();
return _GetMaxFd();
case _SC_PAGESIZE:
return FRAMESIZE;
case _SC_NPROCESSORS_ONLN:

View file

@ -64,7 +64,7 @@ noasan void *__zipos_Mmap(void *addr, size_t size, int prot, int flags,
const int tempProt = !IsXnu() ? prot | PROT_WRITE : PROT_WRITE;
void *outAddr =
Mmap(addr, size, tempProt, (flags & (~MAP_FILE)) | MAP_ANONYMOUS, -1, 0);
_Mmap(addr, size, tempProt, (flags & (~MAP_FILE)) | MAP_ANONYMOUS, -1, 0);
if (outAddr == MAP_FAILED) {
return MAP_FAILED;
}
@ -82,7 +82,7 @@ noasan void *__zipos_Mmap(void *addr, size_t size, int prot, int flags,
return outAddr;
} while (0);
const int e = errno;
Munmap(outAddr, size);
_Munmap(outAddr, size);
errno = e;
strace_enabled(+1);
return MAP_FAILED;

View file

@ -40,7 +40,7 @@ LIBC_ZIPOS_A_DIRECTDEPS = \
LIBC_STUBS \
LIBC_SYSV_CALLS \
LIBC_NT_KERNEL32 \
THIRD_PARTY_ZLIB
THIRD_PARTY_PUFF
LIBC_ZIPOS_A_DEPS := \
$(call uniq,$(foreach zipos,$(LIBC_ZIPOS_A_DIRECTDEPS),$($(zipos))))

18
third_party/puff/README.cosmo vendored Normal file
View file

@ -0,0 +1,18 @@
DESCRIPTION
puff is a tiny implementation of deflate decompression
LICENSE
zlib license
ORIGIN
Origin: git@github.com:madler/zlib.git
Commit: 03614c56ad299f9b238c75aa1e66f0c08fc4fc8b
Author: Mark Adler <madler@alumni.caltech.edu>
Date: Sun Oct 30 08:36:13 2016 -0700
LOCAL CHANGES
None.

View file

@ -23,7 +23,7 @@
Mark Adler madler@alumni.caltech.edu
*/
#include "libc/runtime/runtime.h"
#include "third_party/zlib/puff.h"
#include "third_party/puff/puff.h"
// clang-format off
asm(".ident\t\"\\n\\n\
@ -825,7 +825,7 @@ local int dynamic(struct state *s)
* block (if it was a fixed or dynamic block) are undefined and have no
* expected values to check.
*/
int puff(unsigned char *dest, /* pointer to destination pointer */
int _puff(unsigned char *dest, /* pointer to destination pointer */
unsigned long *destlen, /* amount of output space */
const unsigned char *source, /* pointer to source data pointer */
unsigned long *sourcelen) /* amount of input available */

View file

@ -7,7 +7,7 @@ COSMOPOLITAN_C_START_
#define NIL ((unsigned char *)0) /* for no output option */
#endif
int puff(unsigned char *dest, unsigned long *destlen,
int _puff(unsigned char *dest, unsigned long *destlen,
const unsigned char *source, unsigned long *sourcelen);
COSMOPOLITAN_C_END_

45
third_party/puff/puff.mk vendored Normal file
View file

@ -0,0 +1,45 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
PKGS += THIRD_PARTY_PUFF
THIRD_PARTY_PUFF_ARTIFACTS += THIRD_PARTY_PUFF_A
THIRD_PARTY_PUFF = $(THIRD_PARTY_PUFF_A_DEPS) $(THIRD_PARTY_PUFF_A)
THIRD_PARTY_PUFF_A = o/$(MODE)/third_party/puff/puff.a
THIRD_PARTY_PUFF_A_FILES := $(wildcard third_party/puff/*)
THIRD_PARTY_PUFF_A_HDRS = $(filter %.h,$(THIRD_PARTY_PUFF_A_FILES))
THIRD_PARTY_PUFF_A_SRCS = $(filter %.c,$(THIRD_PARTY_PUFF_A_FILES))
THIRD_PARTY_PUFF_A_OBJS = \
$(THIRD_PARTY_PUFF_A_SRCS:%.c=o/$(MODE)/%.o)
THIRD_PARTY_PUFF_A_CHECKS = \
$(THIRD_PARTY_PUFF_A).pkg \
$(THIRD_PARTY_PUFF_A_HDRS:%=o/$(MODE)/%.ok)
THIRD_PARTY_PUFF_A_DIRECTDEPS = \
LIBC_INTRIN \
LIBC_NEXGEN32E \
LIBC_STUBS
THIRD_PARTY_PUFF_A_DEPS := \
$(call uniq,$(foreach x,$(THIRD_PARTY_PUFF_A_DIRECTDEPS),$($(x))))
$(THIRD_PARTY_PUFF_A): \
third_party/puff/ \
$(THIRD_PARTY_PUFF_A).pkg \
$(THIRD_PARTY_PUFF_A_OBJS)
$(THIRD_PARTY_PUFF_A).pkg: \
$(THIRD_PARTY_PUFF_A_OBJS) \
$(foreach x,$(THIRD_PARTY_PUFF_A_DIRECTDEPS),$($(x)_A).pkg)
THIRD_PARTY_PUFF_LIBS = $(foreach x,$(THIRD_PARTY_PUFF_ARTIFACTS),$($(x)))
THIRD_PARTY_PUFF_SRCS = $(foreach x,$(THIRD_PARTY_PUFF_ARTIFACTS),$($(x)_SRCS))
THIRD_PARTY_PUFF_HDRS = $(foreach x,$(THIRD_PARTY_PUFF_ARTIFACTS),$($(x)_HDRS))
THIRD_PARTY_PUFF_CHECKS = $(foreach x,$(THIRD_PARTY_PUFF_ARTIFACTS),$($(x)_CHECKS))
THIRD_PARTY_PUFF_OBJS = $(foreach x,$(THIRD_PARTY_PUFF_ARTIFACTS),$($(x)_OBJS))
.PHONY: o/$(MODE)/third_party/puff
o/$(MODE)/third_party/puff: \
$(THIRD_PARTY_PUFF_CHECKS)

View file

@ -23,6 +23,7 @@ o/$(MODE)/third_party: \
o/$(MODE)/third_party/mbedtls \
o/$(MODE)/third_party/musl \
o/$(MODE)/third_party/nsync \
o/$(MODE)/third_party/puff \
o/$(MODE)/third_party/python \
o/$(MODE)/third_party/quickjs \
o/$(MODE)/third_party/regex \

View file

@ -673,7 +673,7 @@ int main(int argc, char *argv[]) {
}
// figure out where we want the dso
if (IsDynamicExecutable(prog)) {
if (_IsDynamicExecutable(prog)) {
isdynamic = true;
if ((s = getenv("TMPDIR")) || //
(s = getenv("HOME")) || //

View file

@ -6893,7 +6893,7 @@ static void MakeExecutableModifiable(void) {
if (_endswith(zpath, ".com.dbg")) return;
close(zfd);
ft = ftrace_enabled(0);
if ((zfd = OpenExecutable()) == -1) {
if ((zfd = _OpenExecutable()) == -1) {
WARNF("(srvr) can't open executable for modification: %m");
}
if (ft > 0) {