Release Cosmopolitan v3.3

This change upgrades to GCC 12.3 and GNU binutils 2.42. The GNU linker
appears to have changed things so that only a single de-duplicated str
table is present in the binary, and it gets placed wherever the linker
wants, regardless of what the linker script says. To cope with that we
need to stop using .ident to embed licenses. As such, this change does
significant work to revamp how third party licenses are defined in the
codebase, using `.section .notice,"aR",@progbits`.

This new GCC 12.3 toolchain has support for GNU indirect functions. It
lets us support __target_clones__ for the first time. This is used for
optimizing the performance of libc string functions such as strlen and
friends so far on x86, by ensuring AVX systems favor a second codepath
that uses VEX encoding. It shaves some latency off certain operations.
It's a useful feature to have for scientific computing for the reasons
explained by the test/libcxx/openmp_test.cc example which compiles for
fifteen different microarchitectures. Thanks to the upgrades, it's now
also possible to use newer instruction sets, such as AVX512FP16, VNNI.

Cosmo now uses the %gs register on x86 by default for TLS. Doing it is
helpful for any program that links `cosmo_dlopen()`. Such programs had
to recompile their binaries at startup to change the TLS instructions.
That's not great, since it means every page in the executable needs to
be faulted. The work of rewriting TLS-related x86 opcodes, is moved to
fixupobj.com instead. This is great news for MacOS x86 users, since we
previously needed to morph the binary every time for that platform but
now that's no longer necessary. The only platforms where we need fixup
of TLS x86 opcodes at runtime are now Windows, OpenBSD, and NetBSD. On
Windows we morph TLS to point deeper into the TIB, based on a TlsAlloc
assignment, and on OpenBSD/NetBSD we morph %gs back into %fs since the
kernels do not allow us to specify a value for the %gs register.

OpenBSD users are now required to use APE Loader to run Cosmo binaries
and assimilation is no longer possible. OpenBSD kernel needs to change
to allow programs to specify a value for the %gs register, or it needs
to stop marking executable pages loaded by the kernel as mimmutable().

This release fixes __constructor__, .ctor, .init_array, and lastly the
.preinit_array so they behave the exact same way as glibc.

We no longer use hex constants to define math.h symbols like M_PI.
This commit is contained in:
Justine Tunney 2024-02-20 11:12:09 -08:00
parent d3ff48c63f
commit 957c61cbbf
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
736 changed files with 13726 additions and 9445 deletions

View file

@ -52,7 +52,6 @@
"relegated=",
"hidden=",
"textstartup=",
"initarray=",
"returnsnonnull=",
"returnspointerwithnoaliases=",
"printfesque(x)=",

View file

@ -133,10 +133,10 @@ endif
ifneq ($(findstring aarch64,$(MODE)),)
ARCH = aarch64
HOSTS ?= pi pi5 studio freebsdarm
HOSTS ?= pi studio freebsdarm
else
ARCH = x86_64
HOSTS ?= freebsd rhel7 xnu win10 openbsd netbsd meatball nightmare
HOSTS ?= freebsd rhel7 xnu openbsd netbsd win10
endif
ZIPOBJ_FLAGS += -a$(ARCH)
@ -149,9 +149,9 @@ export MODE
export SOURCE_DATE_EPOCH
export TMPDIR
COSMOCC = .cosmocc/3.2
COSMOCC = .cosmocc/3.3
TOOLCHAIN = $(COSMOCC)/bin/$(ARCH)-linux-cosmo-
DOWNLOAD := $(shell build/download-cosmocc.sh $(COSMOCC) 3.2 28b48682595f0f46b45ab381118cdffdabc8fcfa29aa54e301fe6ffe35269f5e)
DOWNLOAD := $(shell build/download-cosmocc.sh $(COSMOCC) 3.3 d26ec8f4e48f6db004fc6a9677c7ff3b50c3b21e936e9393158aa2ed51b0b549)
AS = $(TOOLCHAIN)as
CC = $(TOOLCHAIN)gcc
@ -206,8 +206,7 @@ endif
.UNVEIL += \
libc/integral \
libc/stdbool.h \
libc/disclaimer.inc \
rwc:/dev/shm \
rwc:/dev/shm \
rx:.cosmocc \
rx:build/bootstrap \
r:build/portcosmo.h \

View file

@ -157,7 +157,6 @@ o/$(MODE)/ape/ape-no-modify-self.o: \
ape/ape.S \
ape/ape.h \
ape/macros.internal.h \
ape/notice.inc \
ape/relocations.h \
ape/ape.internal.h \
libc/dce.h \
@ -185,7 +184,6 @@ o/$(MODE)/ape/ape-copy-self.o: \
ape/ape.S \
ape/ape.h \
ape/macros.internal.h \
ape/notice.inc \
ape/relocations.h \
ape/ape.internal.h \
libc/dce.h \

View file

@ -89,10 +89,12 @@ SECTIONS {
*(.ubsan.data)
}
.comment : {
__comment_start = .;
KEEP(*(.comment))
.notice : {
__notices = .;
KEEP(*(.notice))
BYTE(0);
BYTE(10);
BYTE(10);
}
.eh_frame_hdr : {
@ -157,8 +159,11 @@ SECTIONS {
.init_array : {
__init_array_start = .;
KEEP(*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
KEEP(*(.init_array EXCLUDE_FILE(*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
KEEP(*(.preinit_array))
KEEP(*(SORT_BY_INIT_PRIORITY(.init_array.*)
SORT_BY_INIT_PRIORITY(.ctors.*)))
KEEP(*(.init_array))
KEEP(*(.ctors))
__init_array_end = .;
}

View file

@ -33,7 +33,6 @@
αcτµαlly pδrταblε εxεcµταblε § program header
*/
#include "ape/macros.internal.h"
#include "ape/notice.inc"
#include "ape/relocations.h"
#include "libc/calls/metalfile.internal.h"
#include "libc/dce.h"
@ -1772,49 +1771,31 @@ kernel: movabs $ape_stack_vaddr,%rsp
.type ape_text_nops,@object
.type __test_end,@object
.section .commentprologue,"a",@progbits
.globl __comment_start
.type __comment_start,@object
.hidden __comment_start
__comment_start:/*
...
decentralized content
...
*/.previous
.section .commentepilogue,"a",@progbits
.byte 0
.previous
.section .ape.pad.head,"a",@progbits
.type ape_pad_head,@object
.hidden ape_pad_head
ape_pad_head:
.previous
.section .ape.pad.text,"a",@progbits
.type ape_pad_text,@object
.hidden ape_pad_text
ape_pad_text:
.previous
.section .ape.pad.privileged,"a",@progbits
.type ape_pad_privileged,@object
.hidden ape_pad_privileged
ape_pad_privileged:
.previous
.section .ape.pad.data,"a",@progbits
.type ape_pad_data,@object
.hidden ape_pad_data
ape_pad_data:
.previous
#if SupportsWindows()
.section .idata.ro,"a",@progbits
.type ape_idata_ro,@object
.hidden ape_idata_ro
ape_idata_ro:
.previous
#endif /* SupportsWindows() */
.section .dataprologue,"aw",@progbits
@ -1822,32 +1803,45 @@ ape_idata_ro:
.globl __data_start
.hidden __data_start
__data_start:
.previous
.section .dataepilogue,"aw",@progbits
.type __data_end,@object
.globl __data_end
.hidden __data_end
__data_end:
.previous
.section .bssprologue,"aw",@nobits
.type __bss_start,@object
.globl __bss_start
.hidden __bss_start
__bss_start:
.previous
.section .bssepilogue,"aw",@nobits
.type __bss_end,@object
.globl __bss_end
.hidden __bss_end
__bss_end:
.previous
.section .fstls,"awT",@nobits
.align TLS_ALIGNMENT
.previous
.section .notice,"aR",@progbits
.asciz "\n\n\
Cosmopolitan\n\
Copyright 2024 Justine Alexandra Roberts Tunney\n\
\n\
Permission to use, copy, modify, and/or distribute this software for\n\
any purpose with or without fee is hereby granted, provided that the\n\
above copyright notice and this permission notice appear in all copies.\n\
\n\
THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL\n\
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED\n\
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE\n\
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\n\
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\n\
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER\n\
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\n\
PERFORMANCE OF THIS SOFTWARE."
.end


View file

@ -330,11 +330,11 @@ SECTIONS {
*(.ubsan.data)
/* Legal Notices */
#if !defined(IM_FEELING_NAUGHTY) || defined(EMBED_NOTICES)
KEEP(*(.commentprologue))
KEEP(*(.comment))
KEEP(*(.commentepilogue))
#endif
__notices = .;
KEEP(*(.notice))
BYTE(0);
BYTE(10);
BYTE(10);
/*BEGIN: read-only data that's only needed for initialization */
@ -390,6 +390,9 @@ SECTIONS {
/*BEGIN: NT FORK COPYING */
KEEP(*(.dataprologue))
*(.data .data.*)
*(.gnu_extab)
*(.gcc_except_table .gcc_except_table.*)
*(.exception_ranges*)
*(.PyRuntime) /* for python */
*(.subrs) /* for emacs */
KEEP(*(SORT_BY_NAME(.sort.data.*)))
@ -404,11 +407,11 @@ SECTIONS {
. = ALIGN(. != 0 ? __SIZEOF_POINTER__ : 0);
__init_array_start = .;
KEEP(*(.preinit_array))
KEEP(*(SORT_BY_INIT_PRIORITY(.init_array.*)
SORT_BY_INIT_PRIORITY(.ctors.*)))
KEEP(*(.ctors))
KEEP(*(.init_array))
KEEP(*(.preinit_array))
KEEP(*(.ctors))
__init_array_end = .;
. = ALIGN(. != 0 ? __SIZEOF_POINTER__ : 0);
@ -425,8 +428,8 @@ SECTIONS {
. = ALIGN(. != 0 ? __SIZEOF_POINTER__ : 0);
KEEP(*(SORT_BY_NAME(.piro.data.sort.*)))
KEEP(*(.piro.pad.data))
KEEP(*(.dataepilogue))
*(.igot.plt)
KEEP(*(.dataepilogue))
. = ALIGN(. != 0 ? CONSTANT(COMMONPAGESIZE) : 0);
/*END: NT FORK COPYING */

View file

@ -2,7 +2,7 @@
#define COSMOPOLITAN_APE_SECTIONS_INTERNAL_H_
COSMOPOLITAN_C_START_
extern const char __comment_start[] __attribute__((__weak__));
extern const char __notices[] __attribute__((__weak__));
extern unsigned char __executable_start[] __attribute__((__weak__));
extern unsigned char __privileged_start[] __attribute__((__weak__));
extern unsigned char _ehead[] __attribute__((__weak__));

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -138,6 +138,7 @@ MATHEMATICAL = \
DEFAULT_CPPFLAGS += \
-D_COSMO_SOURCE \
-DMODE='"$(MODE)"' \
-Wno-prio-ctor-dtor \
-Wno-unknown-pragmas \
-nostdinc \
-iquote. \
@ -164,6 +165,7 @@ DEFAULT_LDFLAGS = \
-nostdlib \
-znorelro \
--gc-sections \
-z noexecstack \
--build-id=none \
--no-dynamic-linker

View file

@ -6,14 +6,14 @@ if [ -n "$OBJDUMP" ]; then
fi
find_objdump() {
if [ -x .cosmocc/3.2/bin/$1-linux-cosmo-objdump ]; then
OBJDUMP=.cosmocc/3.2/bin/$1-linux-cosmo-objdump
elif [ -x .cosmocc/3.2/bin/$1-linux-musl-objdump ]; then
OBJDUMP=.cosmocc/3.2/bin/$1-linux-musl-objdump
elif [ -x "$COSMO/.cosmocc/3.2/bin/$1-linux-cosmo-objdump" ]; then
OBJDUMP="$COSMO/.cosmocc/3.2/bin/$1-linux-cosmo-objdump"
elif [ -x "$COSMO/.cosmocc/3.2/bin/$1-linux-musl-objdump" ]; then
OBJDUMP="$COSMO/.cosmocc/3.2/bin/$1-linux-musl-objdump"
if [ -x .cosmocc/3.3/bin/$1-linux-cosmo-objdump ]; then
OBJDUMP=.cosmocc/3.3/bin/$1-linux-cosmo-objdump
elif [ -x .cosmocc/3.3/bin/$1-linux-musl-objdump ]; then
OBJDUMP=.cosmocc/3.3/bin/$1-linux-musl-objdump
elif [ -x "$COSMO/.cosmocc/3.3/bin/$1-linux-cosmo-objdump" ]; then
OBJDUMP="$COSMO/.cosmocc/3.3/bin/$1-linux-cosmo-objdump"
elif [ -x "$COSMO/.cosmocc/3.3/bin/$1-linux-musl-objdump" ]; then
OBJDUMP="$COSMO/.cosmocc/3.3/bin/$1-linux-musl-objdump"
else
echo "error: toolchain not found (try running 'cosmocc --update' or 'make' in the cosmo monorepo)" >&2
exit 1

View file

@ -35,12 +35,7 @@
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/madv.h"
asm(".ident\t\"\\n\\n\
PL_MPEG (MIT License)\\n\
Copyright(c) 2019 Dominic Szablewski\\n\
https://phoboslab.org\"");
asm(".include \"libc/disclaimer.inc\"");
__static_yoink("pl_mpeg_notice");
/* clang-format off */
// -----------------------------------------------------------------------------

View file

@ -27,17 +27,12 @@
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "dsp/mpeg/buffer.h"
#include "dsp/mpeg/demux.h"
#include "dsp/mpeg/buffer.h"
#include "dsp/mpeg/mpeg.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"
asm(".ident\t\"\\n\\n\
PL_MPEG (MIT License)\\n\
Copyright(c) 2019 Dominic Szablewski\\n\
https://phoboslab.org\"");
asm(".include \"libc/disclaimer.inc\"");
__static_yoink("pl_mpeg_notice");
/* clang-format off */
// ----------------------------------------------------------------------------

View file

@ -28,12 +28,7 @@
SOFTWARE.
*/
#include "dsp/core/half.h"
asm(".ident\t\"\\n\\n\
PL_MPEG (MIT License)\\n\
Copyright(c) 2019 Dominic Szablewski\\n\
https://phoboslab.org\"");
asm(".include \"libc/disclaimer.inc\"");
__static_yoink("pl_mpeg_notice");
/**
* Computes Fixed-Point 8x8 Inverse Discrete Cosine Transform.

View file

@ -33,12 +33,6 @@
#include "libc/mem/mem.h"
#include "libc/str/str.h"
asm(".ident\t\"\\n\\n\
PL_MPEG (MIT License)\\n\
Copyright(c) 2019 Dominic Szablewski\\n\
https://phoboslab.org\"");
asm(".include \"libc/disclaimer.inc\"");
/* clang-format off */
// -----------------------------------------------------------------------------
// plm_audio implementation

View file

@ -41,12 +41,7 @@
#include "libc/str/str.h"
#include "libc/time/time.h"
#include "libc/x/x.h"
asm(".ident\t\"\\n\\n\
PL_MPEG (MIT License)\\n\
Copyright(c) 2019 Dominic Szablewski\\n\
https://phoboslab.org\"");
asm(".include \"libc/disclaimer.inc\"");
__static_yoink("pl_mpeg_notice");
// -----------------------------------------------------------------------------
// plm_video implementation
@ -1104,7 +1099,7 @@ plm_video_t *plm_video_create_with_buffer(plm_buffer_t *buffer,
return self;
}
static textstartup void plm_video_init(void) {
__attribute__((__constructor__)) static textstartup void plm_video_init(void) {
PLM_VIDEO_MACROBLOCK_TYPE[0] = NULL;
PLM_VIDEO_MACROBLOCK_TYPE[1] = (void *)PLM_VIDEO_MACROBLOCK_TYPE_INTRA;
PLM_VIDEO_MACROBLOCK_TYPE[2] = (void *)PLM_VIDEO_MACROBLOCK_TYPE_PREDICTIVE;
@ -1113,5 +1108,3 @@ static textstartup void plm_video_init(void) {
PLM_VIDEO_DCT_SIZE[1] = (void *)PLM_VIDEO_DCT_SIZE_CHROMINANCE;
PLM_VIDEO_DCT_SIZE[2] = (void *)PLM_VIDEO_DCT_SIZE_CHROMINANCE;
}
const void *const plm_video_init_ctor[] initarray = {plm_video_init};

4
dsp/mpeg/notice.c Normal file
View file

@ -0,0 +1,4 @@
__notice(pl_mpeg_notice, "\
PL_MPEG (MIT License)\n\
Copyright(c) 2019 Dominic Szablewski\n\
https://phoboslab.org");

View file

@ -32,12 +32,7 @@
#include "libc/mem/mem.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
asm(".ident\t\"\\n\\n\
PL_MPEG (MIT License)\\n\
Copyright(c) 2019 Dominic Szablewski\\n\
https://phoboslab.org\"");
asm(".include \"libc/disclaimer.inc\"");
__static_yoink("pl_mpeg_notice");
/* clang-format off */
// -----------------------------------------------------------------------------

View file

@ -29,12 +29,7 @@
*/
#include "dsp/mpeg/mpeg.h"
#include "libc/macros.internal.h"
asm(".ident\t\"\\n\\n\
PL_MPEG (MIT License)\\n\
Copyright(c) 2019 Dominic Szablewski\\n\
https://phoboslab.org\"");
asm(".include \"libc/disclaimer.inc\"");
__static_yoink("pl_mpeg_notice");
/**
* @see YCbCr2RGB() in tool/viz/lib/ycbcr2rgb.c

View file

@ -121,8 +121,7 @@ void *MagikarpY(long dys, long dxs, unsigned char d[restrict dys][dxs],
return d;
}
static textstartup void g_magikarp_init() {
__attribute__((__constructor__)) static textstartup void g_magikarp_init() {
memcpy(g_magkern, kMagkern[0], sizeof(g_magkern));
memcpy(g_magikarp, kMagikarp[0], sizeof(g_magikarp));
}
const void *const g_magikarp_ctor[] initarray = {g_magikarp_init};

View file

@ -21,7 +21,7 @@
struct Itoa8 kItoa8;
static textstartup void itoa8_init(void) {
__attribute__((__constructor__)) static textstartup void itoa8_init(void) {
int i;
uint8_t z;
uint32_t w;
@ -41,5 +41,3 @@ static textstartup void itoa8_init(void) {
kItoa8.data[i] = w;
}
}
const void *const itoa8_init_ctor[] initarray = {itoa8_init};

View file

@ -97,7 +97,7 @@ static int uncube(int x) {
return x < 48 ? 0 : x < 115 ? 1 : (x - 35) / 40;
}
static textstartup void rgb2ansi_init(void) {
__attribute__((__constructor__)) static textstartup void rgb2ansi_init(void) {
uint8_t c;
uint32_t i;
memcpy(g_ansi2rgb_, &kCgaPalette, sizeof(kCgaPalette));
@ -114,5 +114,3 @@ static textstartup void rgb2ansi_init(void) {
g_ansi2rgb_[i].xt = i;
}
}
const void *const rgb2ansi_init_ctor[] initarray = {rgb2ansi_init};

View file

@ -73,8 +73,6 @@ textstartup void ttyquantsetup(enum TtyQuantizationAlgorithm alg,
TTYQUANT()->blocks = blocks;
}
textstartup void ttyquant_init(void) {
__attribute__((__constructor__)) textstartup void ttyquant_init(void) {
ttyquantsetup(kTtyQuantXterm256, kTtyQuantRgb, kTtyBlocksUnicode);
}
const void *const ttyquant_init_ctor[] initarray = {ttyquant_init};

View file

@ -33,11 +33,10 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
asm(".ident \"\n\
__notice(kilo_notice, "\
Kilo A very simple editor (BSD-2)\n\
Copyright 2016 Salvatore Sanfilippo\n\
Contact: antirez@gmail.com\"\n\
.include \"libc/disclaimer.inc\"");
Contact: antirez@gmail.com");
/*
* This software has been modified by Justine Tunney to:

View file

@ -67,13 +67,10 @@
* @see https://asciinema.org/
*/
asm(".ident\t\"\\n\\n\
FreeBSD Script (BSD-3 License)\\n\
Copyright (c) 2010, 2012 David E. O'Brien\\n\
Copyright (c) 1980, 1992, 1993\\n\
\tThe Regents of the University of California.\\n\
\tAll rights reserved.\"");
asm(".include \"libc/disclaimer.inc\"");
__notice(freebsd_script_notice, "\
FreeBSD Script (BSD-3 License)\n\
Copyright (c) 2010, 2012 David E. O'Brien\n\
Copyright (c) 1980, 1992, 1993 The Regents of the University of California");
#define DEF_BUF 65536

View file

@ -48,12 +48,9 @@
#include "third_party/musl/netdb.h"
// clang-format off
asm(".ident\t\"\\n\\n\
FreeBSD Whois (BSD-3 License)\\n\
Copyright (c) 1980, 1993\\n\
\tThe Regents of the University of California.\\n\
\tAll rights reserved.\"");
asm(".include \"libc/disclaimer.inc\"");
__notice(freebsd_whois_notice, "\
FreeBSD Whois (BSD-3 License)\n\
Copyright (c) 1980, 1993 The Regents of the University of California");
#define ABUSEHOST "whois.abuse.net"
#define ANICHOST "whois.arin.net"

View file

@ -73,12 +73,10 @@ $(LIBC_CALLS_A_OBJS): private \
-Wframe-larger-than=4096 \
-Walloca-larger-than=4096
ifneq ($(ARCH), aarch64)
# we always want -O3 because:
# it makes the code size smaller too
# we need -mstringop-strategy=loop because:
# privileged code might generate memcpy call
o/$(MODE)/libc/calls/termios2host.o \
o/$(MODE)/libc/calls/siginfo2cosmo.o \
o/$(MODE)/libc/calls/sigenter-freebsd.o \
o/$(MODE)/libc/calls/sigenter-netbsd.o \
o/$(MODE)/libc/calls/sigenter-openbsd.o \
@ -86,6 +84,19 @@ o/$(MODE)/libc/calls/sigenter-xnu.o \
o/$(MODE)/libc/calls/ntcontext2linux.o: private \
COPTS += \
-O3 \
-ffreestanding
ifeq ($(ARCH), x86_64)
# we need -mstringop-strategy=loop because:
# privileged code might generate memcpy call
o/$(MODE)/libc/calls/termios2host.o \
o/$(MODE)/libc/calls/siginfo2cosmo.o \
o/$(MODE)/libc/calls/sigenter-freebsd.o \
o/$(MODE)/libc/calls/sigenter-netbsd.o \
o/$(MODE)/libc/calls/sigenter-openbsd.o \
o/$(MODE)/libc/calls/sigenter-xnu.o \
o/$(MODE)/libc/calls/ntcontext2linux.o: private \
COPTS += \
-mstringop-strategy=loop
endif

View file

@ -87,13 +87,9 @@ textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) {
}
}
static textstartup void winclock_init() {
__attribute__((__constructor__(40))) static textstartup void winclock_init() {
if (IsWindows()) {
QueryPerformanceCounter(&g_winclock.base);
QueryPerformanceFrequency(&g_winclock.freq);
}
}
const void *const winclock_ctor[] initarray = {
winclock_init,
};

View file

@ -32,11 +32,8 @@
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/s.h"
#include "libc/sysv/errfuns.h"
__static_yoink("musl_libc_notice");
asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
// clang-format off
static void __procfdname(char *buf, unsigned fd) {

View file

@ -27,6 +27,7 @@
#include "libc/elf/tinyelf.internal.h"
#include "libc/errno.h"
#include "libc/intrin/directmap.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/nt/memory.h"
#include "libc/nt/runtime.h"
#include "libc/runtime/runtime.h"

View file

@ -59,7 +59,7 @@ textwindows int sys_getloadavg_nt(double *a, int n) {
return rc;
}
static textstartup void sys_getloadavg_nt_init(void) {
__attribute__((__constructor__(40))) static textstartup void ntinitload(void) {
if (IsWindows()) {
load = 1;
cpus = __get_cpu_count() / 2;
@ -67,7 +67,3 @@ static textstartup void sys_getloadavg_nt_init(void) {
GetSystemTimes(&idle1, &kern1, &user1);
}
}
const void *const sys_getloadavg_nt_ctor[] initarray = {
sys_getloadavg_nt_init,
};

View file

@ -190,7 +190,8 @@ ssize_t getrandom(void *p, size_t n, unsigned f) {
return rc;
}
__attribute__((__constructor__)) static textstartup void getrandom_init(void) {
__attribute__((__constructor__(30))) static textstartup void getrandom_init(
void) {
int e, rc;
if (IsWindows() || IsMetal()) return;
BLOCK_CANCELATION;

View file

@ -21,7 +21,7 @@
char *program_invocation_short_name;
__attribute__((__constructor__)) static void //
__attribute__((__constructor__(10))) static textstartup void
program_invocation_short_name_init(void) {
char *p, *r;
if (!__argc) return;

View file

@ -22,8 +22,8 @@
#include "libc/nexgen32e/x86info.h"
#include "libc/stdio/rand.h"
textstartup void rdrand_init(int argc, char **argv, char **envp,
intptr_t *auxv) {
__attribute__((__constructor__(2))) textstartup void rdrand_init(
int argc, char **argv, char **envp, intptr_t *auxv) {
extern unsigned kMutableCpuids[KCPUIDS_LEN][4] asm("kCpuids");
/*
* Clear RDRAND on AMD models before Zen and then some
@ -39,5 +39,3 @@ textstartup void rdrand_init(int argc, char **argv, char **envp,
kMutableCpuids[KCPUIDS_7H][KCPUIDS_EBX] &= ~(1u << 18);
}
}
const void *const g_rdrand_init[] initarray = {rdrand_init};

View file

@ -32,6 +32,7 @@
#include "libc/intrin/bsf.h"
#include "libc/intrin/describebacktrace.internal.h"
#include "libc/intrin/dll.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/strace.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/nt/console.h"
@ -517,6 +518,15 @@ static int __sig_crash_sig(struct NtExceptionPointers *ep, int *code) {
}
}
static char *__sig_stpcpy(char *d, const char *s) {
size_t i;
for (i = 0;; ++i) {
if (!(d[i] = s[i])) {
return d + i;
}
}
}
static void __sig_unmaskable(struct NtExceptionPointers *ep, int code, int sig,
struct CosmoTib *tib) {
@ -540,9 +550,10 @@ static void __sig_unmaskable(struct NtExceptionPointers *ep, int code, int sig,
intptr_t hStderr;
char sigbuf[21], s[128], *p;
hStderr = GetStdHandle(kNtStdErrorHandle);
p = stpcpy(s, "Terminating on uncaught ");
p = stpcpy(p, strsignal_r(sig, sigbuf));
p = stpcpy(p, ". Pass --strace and/or ShowCrashReports() for details.\n");
p = __sig_stpcpy(s, "Terminating on uncaught ");
p = __sig_stpcpy(p, strsignal_r(sig, sigbuf));
p = __sig_stpcpy(
p, ". Pass --strace and/or ShowCrashReports() for details.\n");
WriteFile(hStderr, s, p - s, 0, 0);
#endif
__sig_terminate(sig);
@ -656,12 +667,10 @@ textwindows int __sig_check(void) {
}
}
textstartup void __sig_init(void) {
__attribute__((__constructor__(10))) textstartup void __sig_init(void) {
if (!IsWindows()) return;
AddVectoredExceptionHandler(true, (void *)__sig_crash);
SetConsoleCtrlHandler((void *)__sig_console, true);
}
const void *const __sig_ctor[] initarray = {__sig_init};
#endif /* __x86_64__ */

View file

@ -100,7 +100,7 @@ static const struct sock_filter kUnveilBlacklistLatestAbi[] = {
static int landlock_abi_version;
static int landlock_abi_errno;
__attribute__((__constructor__)) void init_landlock_version() {
__attribute__((__constructor__(40))) textstartup void init_landlock_version() {
int e = errno;
landlock_abi_version =
landlock_create_ruleset(0, 0, LANDLOCK_CREATE_RULESET_VERSION);

View file

@ -18,7 +18,6 @@
*/
#include "libc/dce.h"
#include "libc/macros.internal.h"
#include "libc/notice.inc"
.section .start,"ax",@progbits
#if SupportsXnu() && defined(__x86_64__)

View file

View file

@ -920,17 +920,3 @@ char *cosmo_dlerror(void) {
STRACE("dlerror() → %#s", res);
return res;
}
#ifdef __x86_64__
static textstartup void dlopen_init() {
if (IsLinux() || IsFreebsd()) {
// switch from %fs to %gs for tls
struct CosmoTib *tib = __get_tls();
__morph_tls();
__set_tls(tib);
}
}
const void *const dlopen_ctor[] initarray = {
dlopen_init,
};
#endif

View file

@ -55,7 +55,7 @@ foreign_tramp:
mov %rax,-0xc0(%rbp)
// switch to foreign tls
mov %fs:0,%rax
mov %gs:0x30,%rax
mov %rax,-0xc8(%rbp)
mov __foreign+8(%rip),%rdi
call __set_tls

View file

@ -639,12 +639,6 @@ void abort(void) wontreturn;
#define _Section(s) __attribute__((__section__(s)))
#endif
#if defined(__x86_64__) && !defined(__llvm__)
#define initarray _Section(".init_array,\"a\",@init_array #")
#else
#define initarray _Section(".init_array")
#endif
#ifndef __llvm__
#pragma GCC diagnostic ignored "-Wformat=0" /* todo: patch gcc */
#pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch"
@ -758,17 +752,21 @@ void abort(void) wontreturn;
#endif
#if defined(__x86_64__) && (defined(__GNUC__) || defined(__llvm__)) && \
!defined(__chibicc__) && 0 /* TODO: enable with toolchain upgrade */
!defined(__chibicc__) && defined(__OPTIMIZE__)
#define __target_clones(x) __attribute__((__target_clones__(x ",default")))
#else
#define __target_clones(x)
#endif
#if !defined(TINY) && !defined(__AVX__) && 0
#if !defined(TINY) && !defined(__AVX__)
#define __vex __target_clones("avx")
#else
#define __vex
#endif
#define __notice(sym, str) \
__attribute__((__section__(".notice"), __aligned__(1))) const char sym[] = \
"\n\n" str
#define MACHINE_CODE_ANALYSIS_BEGIN_
#define MACHINE_CODE_ANALYSIS_END_

View file

@ -3,8 +3,8 @@
#endif
#define __COSMOPOLITAN_MAJOR__ 3
#define __COSMOPOLITAN_MINOR__ 2
#define __COSMOPOLITAN_PATCH__ 4
#define __COSMOPOLITAN_MINOR__ 3
#define __COSMOPOLITAN_PATCH__ 0
#define __COSMOPOLITAN__ \
(100000000 * __COSMOPOLITAN_MAJOR__ + 1000000 * __COSMOPOLITAN_MINOR__ + \
__COSMOPOLITAN_PATCH__)

View file

@ -74,11 +74,15 @@ o//libc/intrin/memmove.o: private \
-fno-toplevel-reorder
o//libc/intrin/bzero.o \
o//libc/intrin/strlen.o \
o//libc/intrin/strchr.o \
o//libc/intrin/memchr.o \
o//libc/intrin/memrchr.o \
o//libc/intrin/memcmp.o \
o//libc/intrin/memset.o \
o//libc/intrin/memmove.o: private \
CFLAGS += \
-O2 -finline
-O2 -finline -foptimize-sibling-calls
o/$(MODE)/libc/intrin/bzero.o \
o/$(MODE)/libc/intrin/memcmp.o \
@ -89,7 +93,9 @@ o/$(MODE)/libc/intrin/memmove.o: private \
o/$(MODE)/libc/intrin/x86.o: private \
CFLAGS += \
-ffreestanding \
-fno-jump-tables
-fno-jump-tables \
-fpatchable-function-entry=0 \
-Os
# these assembly files are safe to build on aarch64
o/$(MODE)/libc/intrin/aarch64/%.o: libc/intrin/aarch64/%.S

View file

@ -26,14 +26,10 @@
*/
#include "libc/intrin/aarch64/asmdefs.internal.h"
.yoink arm_optimized_routines_notice
#define __memchr_aarch64 memchr
.ident "\n\n\
Optimized Routines (MIT License)\n\
Copyright 2022 ARM Limited\n"
.include "libc/disclaimer.inc"
/* Assumptions:
*
* ARMv8-a, AArch64

View file

@ -26,15 +26,11 @@
*/
#include "libc/intrin/aarch64/asmdefs.internal.h"
.yoink arm_optimized_routines_notice
#define __memcpy_aarch64_simd memcpy
#define __memmove_aarch64_simd memmove
.ident "\n\n\
Optimized Routines (MIT License)\n\
Copyright 2022 ARM Limited\n"
.include "libc/disclaimer.inc"
/* Assumptions:
*
* ARMv8-a, AArch64, Advanced SIMD, unaligned accesses.

View file

@ -26,14 +26,10 @@
*/
#include "libc/intrin/aarch64/asmdefs.internal.h"
.yoink arm_optimized_routines_notice
#define __memrchr_aarch64 memrchr
.ident "\n\n\
Optimized Routines (MIT License)\n\
Copyright 2022 ARM Limited\n"
.include "libc/disclaimer.inc"
/* Assumptions:
*
* ARMv8-a, AArch64, Advanced SIMD.

View file

@ -26,14 +26,10 @@
*/
#include "libc/intrin/aarch64/asmdefs.internal.h"
.yoink arm_optimized_routines_notice
#define __memset_aarch64 memset
.ident "\n\n\
Optimized Routines (MIT License)\n\
Copyright 2022 ARM Limited\n"
.include "libc/disclaimer.inc"
/* Assumptions:
*
* ARMv8-a, AArch64, Advanced SIMD, unaligned accesses.

View file

@ -26,14 +26,10 @@
*/
#include "libc/intrin/aarch64/asmdefs.internal.h"
.yoink arm_optimized_routines_notice
#define __stpcpy_aarch64 stpcpy
.ident "\n\n\
Optimized Routines (MIT License)\n\
Copyright 2022 ARM Limited\n"
.include "libc/disclaimer.inc"
/* Assumptions:
*
* ARMv8-a, AArch64, Advanced SIMD.

View file

@ -26,15 +26,11 @@
*/
#include "libc/intrin/aarch64/asmdefs.internal.h"
.yoink arm_optimized_routines_notice
#define __strchr_aarch64 strchr
#define __index_aarch64 index
.ident "\n\n\
Optimized Routines (MIT License)\n\
Copyright 2022 ARM Limited\n"
.include "libc/disclaimer.inc"
/* Assumptions:
*
* ARMv8-a, AArch64

View file

@ -26,14 +26,10 @@
*/
#include "libc/intrin/aarch64/asmdefs.internal.h"
.yoink arm_optimized_routines_notice
#define __strchrnul_aarch64 strchrnul
.ident "\n\n\
Optimized Routines (MIT License)\n\
Copyright 2022 ARM Limited\n"
.include "libc/disclaimer.inc"
/* Assumptions:
*
* ARMv8-a, AArch64

View file

@ -26,14 +26,10 @@
*/
#include "libc/intrin/aarch64/asmdefs.internal.h"
.yoink arm_optimized_routines_notice
#define __strcmp_aarch64 strcmp
.ident "\n\n\
Optimized Routines (MIT License)\n\
Copyright 2022 ARM Limited\n"
.include "libc/disclaimer.inc"
/* Assumptions:
*
* ARMv8-a, AArch64.

View file

@ -26,14 +26,10 @@
*/
#include "libc/intrin/aarch64/asmdefs.internal.h"
.yoink arm_optimized_routines_notice
#define __strcpy_aarch64 strcpy
.ident "\n\n\
Optimized Routines (MIT License)\n\
Copyright 2022 ARM Limited\n"
.include "libc/disclaimer.inc"
/* Assumptions:
*
* ARMv8-a, AArch64, Advanced SIMD.

View file

@ -26,14 +26,10 @@
*/
#include "libc/intrin/aarch64/asmdefs.internal.h"
.yoink arm_optimized_routines_notice
#define __strlen_aarch64 strlen
.ident "\n\n\
Optimized Routines (MIT License)\n\
Copyright 2022 ARM Limited\n"
.include "libc/disclaimer.inc"
/* Assumptions:
*
* ARMv8-a, AArch64, Advanced SIMD, unaligned accesses.

View file

@ -26,14 +26,10 @@
*/
#include "libc/intrin/aarch64/asmdefs.internal.h"
.yoink arm_optimized_routines_notice
#define __strncmp_aarch64 strncmp
.ident "\n\n\
Optimized Routines (MIT License)\n\
Copyright 2022 ARM Limited\n"
.include "libc/disclaimer.inc"
/* Assumptions:
*
* ARMv8-a, AArch64.

View file

@ -26,14 +26,10 @@
*/
#include "libc/intrin/aarch64/asmdefs.internal.h"
.yoink arm_optimized_routines_notice
#define __strnlen_aarch64 strnlen
.ident "\n\n\
Optimized Routines (MIT License)\n\
Copyright 2022 ARM Limited\n"
.include "libc/disclaimer.inc"
/* Assumptions:
*
* ARMv8-a, AArch64, Advanced SIMD.

View file

@ -26,15 +26,11 @@
*/
#include "libc/intrin/aarch64/asmdefs.internal.h"
.yoink arm_optimized_routines_notice
#define __strrchr_aarch64 strrchr
#define __rindex_aarch64 rindex
.ident "\n\n\
Optimized Routines (MIT License)\n\
Copyright 2022 ARM Limited\n"
.include "libc/disclaimer.inc"
/* Assumptions:
*
* ARMv8-a, AArch64

3
libc/intrin/armopt.c Normal file
View file

@ -0,0 +1,3 @@
__notice(arm_optimized_routines_notice, "\
Optimized Routines (MIT License)\n\
Copyright 2022 ARM Limited");

3
libc/intrin/chromium.c Normal file
View file

@ -0,0 +1,3 @@
__notice(chromium_notice, "\
Chromium (BSD-3 License)\n\
Copyright 2017 The Chromium Authors");

View file

@ -33,10 +33,6 @@ void __cxa_unlock(void) {
pthread_mutex_unlock(&__cxa_lock_obj);
}
static textstartup void __cxa_init() {
__attribute__((__constructor__(60))) static textstartup void __cxa_init() {
pthread_atfork(__cxa_lock, __cxa_unlock, __cxa_wipe);
}
const void *const __cxa_ctor[] initarray = {
__cxa_init,
};

4
libc/intrin/fblibm.c Normal file
View file

@ -0,0 +1,4 @@
__notice(freebsd_libm_notice, "FreeBSD libm (BSD-2 License)\n\
Copyright (c) 1992-2024 The FreeBSD Project.\n\
Copyright (c) 2005-2011 Bruce D. Evans, Steven G. Kargl, David Schultz\n\
Copyright (c) 2009-2013 Steven G. Kargl");

2
libc/intrin/fdlibm.c Normal file
View file

@ -0,0 +1,2 @@
__notice(fdlibm_notice, "fdlibm (fdlibm license)\n\
Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.");

2
libc/intrin/folibm.c Normal file
View file

@ -0,0 +1,2 @@
__notice(openbsd_libm_notice, "OpenBSD libm (ISC License)\n\
Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>");

View file

@ -30,10 +30,6 @@ bool32 IsRunningUnderMake(void) {
return g_isrunningundermake;
}
textstartup void g_isrunningundermake_init(void) {
__attribute__((__constructor__(30))) textstartup void onmake_init(void) {
g_isrunningundermake = !!getenv("MAKEFLAGS");
}
const void *const g_isrunningundermake_ctor[] initarray = {
g_isrunningundermake_init,
};

View file

@ -36,8 +36,8 @@ static inline const unsigned char *memchr_pure(const unsigned char *s,
}
#if defined(__x86_64__) && !defined(__chibicc__)
static inline const unsigned char *memchr_sse(const unsigned char *s,
unsigned char c, size_t n) {
static __vex const unsigned char *memchr_sse(const unsigned char *s,
unsigned char c, size_t n) {
size_t i;
unsigned m;
xmm_t v, t = {c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c};
@ -67,11 +67,9 @@ static inline const unsigned char *memchr_sse(const unsigned char *s,
* @return is pointer to first instance of c or NULL if not found
* @asyncsignalsafe
*/
__vex void *memchr(const void *s, int c, size_t n) {
void *memchr(const void *s, int c, size_t n) {
#if defined(__x86_64__) && !defined(__chibicc__)
const void *r;
r = memchr_sse(s, c, n);
return (void *)r;
return (void *)memchr_sse(s, c, n);
#else
return (void *)memchr_pure(s, c, n);
#endif

View file

@ -27,66 +27,7 @@
typedef long long xmm_t __attribute__((__vector_size__(16), __aligned__(1)));
typedef long long xmm_a __attribute__((__vector_size__(16), __aligned__(16)));
/**
* Copies memory.
*
* memmove n=0 661 picoseconds
* memmove n=1 661 ps/byte 1,476 mb/s
* memmove n=2 330 ps/byte 2,952 mb/s
* memmove n=3 330 ps/byte 2,952 mb/s
* memmove n=4 165 ps/byte 5,904 mb/s
* memmove n=7 141 ps/byte 6,888 mb/s
* memmove n=8 82 ps/byte 11 GB/s
* memmove n=15 44 ps/byte 21 GB/s
* memmove n=16 41 ps/byte 23 GB/s
* memmove n=31 32 ps/byte 29 GB/s
* memmove n=32 31 ps/byte 30 GB/s
* memmove n=63 21 ps/byte 45 GB/s
* memmove n=64 15 ps/byte 61 GB/s
* memmove n=127 13 ps/byte 73 GB/s
* memmove n=128 31 ps/byte 30 GB/s
* memmove n=255 20 ps/byte 45 GB/s
* memmove n=256 19 ps/byte 49 GB/s
* memmove n=511 16 ps/byte 56 GB/s
* memmove n=512 17 ps/byte 54 GB/s
* memmove n=1023 18 ps/byte 52 GB/s
* memmove n=1024 13 ps/byte 72 GB/s
* memmove n=2047 9 ps/byte 96 GB/s
* memmove n=2048 9 ps/byte 98 GB/s
* memmove n=4095 8 ps/byte 112 GB/s
* memmove n=4096 8 ps/byte 109 GB/s
* memmove n=8191 7 ps/byte 124 GB/s
* memmove n=8192 7 ps/byte 125 GB/s
* memmove n=16383 7 ps/byte 134 GB/s
* memmove n=16384 7 ps/byte 134 GB/s
* memmove n=32767 13 ps/byte 72 GB/s
* memmove n=32768 13 ps/byte 72 GB/s
* memmove n=65535 13 ps/byte 68 GB/s
* memmove n=65536 14 ps/byte 67 GB/s
* memmove n=131071 14 ps/byte 65 GB/s
* memmove n=131072 14 ps/byte 64 GB/s
* memmove n=262143 15 ps/byte 63 GB/s
* memmove n=262144 15 ps/byte 63 GB/s
* memmove n=524287 15 ps/byte 61 GB/s
* memmove n=524288 15 ps/byte 61 GB/s
* memmove n=1048575 15 ps/byte 61 GB/s
* memmove n=1048576 15 ps/byte 61 GB/s
* memmove n=2097151 19 ps/byte 48 GB/s
* memmove n=2097152 27 ps/byte 35 GB/s
* memmove n=4194303 28 ps/byte 33 GB/s
* memmove n=4194304 28 ps/byte 33 GB/s
* memmove n=8388607 28 ps/byte 33 GB/s
* memmove n=8388608 28 ps/byte 33 GB/s
*
* DST and SRC may overlap.
*
* @param dst is destination
* @param src is memory to copy
* @param n is number of bytes to copy
* @return dst
* @asyncsignalsafe
*/
__vex void *memmove(void *dst, const void *src, size_t n) {
static __vex void *__memmove(void *dst, const void *src, size_t n) {
char *d;
size_t i;
const char *s;
@ -338,6 +279,69 @@ __vex void *memmove(void *dst, const void *src, size_t n) {
}
}
/**
* Copies memory.
*
* memmove n=0 661 picoseconds
* memmove n=1 661 ps/byte 1,476 mb/s
* memmove n=2 330 ps/byte 2,952 mb/s
* memmove n=3 330 ps/byte 2,952 mb/s
* memmove n=4 165 ps/byte 5,904 mb/s
* memmove n=7 141 ps/byte 6,888 mb/s
* memmove n=8 82 ps/byte 11 GB/s
* memmove n=15 44 ps/byte 21 GB/s
* memmove n=16 41 ps/byte 23 GB/s
* memmove n=31 32 ps/byte 29 GB/s
* memmove n=32 31 ps/byte 30 GB/s
* memmove n=63 21 ps/byte 45 GB/s
* memmove n=64 15 ps/byte 61 GB/s
* memmove n=127 13 ps/byte 73 GB/s
* memmove n=128 31 ps/byte 30 GB/s
* memmove n=255 20 ps/byte 45 GB/s
* memmove n=256 19 ps/byte 49 GB/s
* memmove n=511 16 ps/byte 56 GB/s
* memmove n=512 17 ps/byte 54 GB/s
* memmove n=1023 18 ps/byte 52 GB/s
* memmove n=1024 13 ps/byte 72 GB/s
* memmove n=2047 9 ps/byte 96 GB/s
* memmove n=2048 9 ps/byte 98 GB/s
* memmove n=4095 8 ps/byte 112 GB/s
* memmove n=4096 8 ps/byte 109 GB/s
* memmove n=8191 7 ps/byte 124 GB/s
* memmove n=8192 7 ps/byte 125 GB/s
* memmove n=16383 7 ps/byte 134 GB/s
* memmove n=16384 7 ps/byte 134 GB/s
* memmove n=32767 13 ps/byte 72 GB/s
* memmove n=32768 13 ps/byte 72 GB/s
* memmove n=65535 13 ps/byte 68 GB/s
* memmove n=65536 14 ps/byte 67 GB/s
* memmove n=131071 14 ps/byte 65 GB/s
* memmove n=131072 14 ps/byte 64 GB/s
* memmove n=262143 15 ps/byte 63 GB/s
* memmove n=262144 15 ps/byte 63 GB/s
* memmove n=524287 15 ps/byte 61 GB/s
* memmove n=524288 15 ps/byte 61 GB/s
* memmove n=1048575 15 ps/byte 61 GB/s
* memmove n=1048576 15 ps/byte 61 GB/s
* memmove n=2097151 19 ps/byte 48 GB/s
* memmove n=2097152 27 ps/byte 35 GB/s
* memmove n=4194303 28 ps/byte 33 GB/s
* memmove n=4194304 28 ps/byte 33 GB/s
* memmove n=8388607 28 ps/byte 33 GB/s
* memmove n=8388608 28 ps/byte 33 GB/s
*
* DST and SRC may overlap.
*
* @param dst is destination
* @param src is memory to copy
* @param n is number of bytes to copy
* @return dst
* @asyncsignalsafe
*/
void *memmove(void *dst, const void *src, size_t n) {
return __memmove(dst, src, n);
}
__weak_reference(memmove, memcpy);
#endif /* __aarch64__ */

View file

@ -36,8 +36,8 @@ static inline const unsigned char *memrchr_pure(const unsigned char *s,
}
#if defined(__x86_64__) && !defined(__chibicc__)
static inline const unsigned char *memrchr_sse(const unsigned char *s,
unsigned char c, size_t n) {
static __vex const unsigned char *memrchr_sse(const unsigned char *s,
unsigned char c, size_t n) {
size_t i;
unsigned m;
xmm_t v, t = {c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c};
@ -67,11 +67,9 @@ static inline const unsigned char *memrchr_sse(const unsigned char *s,
* @return is pointer to first instance of c or NULL if not found
* @asyncsignalsafe
*/
__vex void *memrchr(const void *s, int c, size_t n) {
void *memrchr(const void *s, int c, size_t n) {
#if defined(__x86_64__) && !defined(__chibicc__)
const void *r;
r = memrchr_sse(s, c, n);
return (void *)r;
return (void *)memrchr_sse(s, c, n);
#else
return (void *)memrchr_pure(s, c, n);
#endif

3
libc/intrin/musl.c Normal file
View file

@ -0,0 +1,3 @@
__notice(musl_libc_notice, "\
Musl libc (MIT License)\n\
Copyright 2005-2014 Rich Felker, et. al.");

View file

@ -48,13 +48,9 @@
*/
bool __nocolor;
optimizesize textstartup void __nocolor_init(int argc, char **argv, char **envp,
intptr_t *auxv) {
__attribute__((__constructor__(20))) optimizesize textstartup void
__nocolor_init(int argc, char **argv, char **envp, intptr_t *auxv) {
char *s;
__nocolor = (IsWindows() && !IsAtLeastWindows10()) ||
((s = getenv("TERM")) && IsDumb(s));
}
const void *const __nocolor_ctor[] initarray = {
__nocolor_init,
};

View file

@ -28,11 +28,8 @@
#include "libc/limits.h"
#include "libc/math.h"
#if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024)
__static_yoink("musl_libc_notice");
asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
// clang-format off
long double scalblnl(long double x, long n) {

View file

@ -26,11 +26,8 @@
*/
#include "libc/math.h"
__static_yoink("musl_libc_notice");
asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
// clang-format off
/**

View file

@ -26,11 +26,8 @@
*/
#include "libc/math.h"
__static_yoink("musl_libc_notice");
asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
// clang-format off
/**

View file

@ -28,11 +28,8 @@
#include "libc/math.h"
#include "libc/tinymath/ldshape.internal.h"
#if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024)
__static_yoink("musl_libc_notice");
asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
// clang-format off
/**

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/notice.inc"
// Canary for -fstack-protector.
//

View file

@ -31,7 +31,7 @@ static inline const char *strchr_pure(const char *s, int c) {
#if defined(__x86_64__) && !defined(__chibicc__)
typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
static inline const char *strchr_sse(const char *s, unsigned char c) {
static __vex const char *strchr_sse(const char *s, unsigned char c) {
unsigned k;
unsigned m;
const xmm_t *p;
@ -94,7 +94,7 @@ static inline const char *strchr_x64(const char *p, uint64_t c) {
* @asyncsignalsafe
* @vforksafe
*/
__vex char *strchr(const char *s, int c) {
char *strchr(const char *s, int c) {
#if defined(__x86_64__) && !defined(__chibicc__)
const char *r;
if (X86_HAVE(SSE)) {
@ -102,7 +102,6 @@ __vex char *strchr(const char *s, int c) {
} else {
r = strchr_pure(s, c);
}
unassert(!r || *r || !(c & 255));
return (char *)r;
#else
const char *r;
@ -110,9 +109,7 @@ __vex char *strchr(const char *s, int c) {
if ((*s & 255) == c) return (char *)s;
if (!*s) return NULL;
}
r = strchr_x64(s, c);
unassert(!r || *r || !c);
return (char *)r;
return (char *)strchr_x64(s, c);
#endif
}

View file

@ -20,14 +20,7 @@
#include "libc/str/str.h"
#ifndef __aarch64__
/**
* Returns length of NUL-terminated string.
*
* @param s is non-null NUL-terminated string pointer
* @return number of bytes (excluding NUL)
* @asyncsignalsafe
*/
size_t strlen(const char *s) {
static __vex size_t __strlen(const char *s) {
#if defined(__x86_64__) && !defined(__chibicc__)
typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
xmm_t z = {0};
@ -56,4 +49,16 @@ size_t strlen(const char *s) {
return (const char *)p + (__builtin_ctzl(w) >> 3) - s;
#endif
}
/**
* Returns length of NUL-terminated string.
*
* @param s is non-null NUL-terminated string pointer
* @return number of bytes (excluding NUL)
* @asyncsignalsafe
*/
size_t strlen(const char *s) {
return __strlen(s);
}
#endif /* __aarch64__ */

View file

@ -627,7 +627,7 @@ void *__ubsan_get_current_report_data(void) {
return 0;
}
static textstartup void ubsan_init() {
__attribute__((__constructor__(90))) static textstartup void ubsan_init() {
STRACE(" _ _ ____ ____ _ _ _");
STRACE("| | | | __ ) ___| / \\ | \\ | |");
STRACE("| | | | _ \\___ \\ / _ \\ | \\| |");
@ -635,7 +635,3 @@ static textstartup void ubsan_init() {
STRACE(" \\___/|____/____/_/ \\_\\_| \\_|");
STRACE("cosmopolitan behavior module initialized");
}
const void *const ubsan_ctor[] initarray = {
ubsan_init,
};

View file

@ -12,8 +12,6 @@
//
//===----------------------------------------------------------------------===//
#if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__))
#include "libc/intrin/strace.internal.h"
#include "third_party/compiler_rt/cpu_model.h"
enum VendorSignatures {
SIG_INTEL = 0x756e6547, // Genu
@ -740,8 +738,6 @@ static void getAvailableFeatures(unsigned ECX, unsigned EDX, unsigned MaxLeaf,
#undef setFeature
}
int __cpu_indicator_init(void) CONSTRUCTOR_ATTRIBUTE;
struct __processor_model {
unsigned int __cpu_vendor;
unsigned int __cpu_type;
@ -757,7 +753,7 @@ unsigned __cpu_features2[(CPU_FEATURE_MAX - 1) / 32];
// the priority set. However, it still runs after ifunc initializers and
// needs to be called explicitly there.
int CONSTRUCTOR_ATTRIBUTE __cpu_indicator_init(void) {
__attribute__((__constructor__(1))) textstartup int __cpu_indicator_init(void) {
unsigned EAX, EBX, ECX, EDX;
unsigned MaxLeaf = 5;
unsigned Vendor;

View file

@ -1,4 +1,4 @@
#ifndef COSMOPOLITAN_LIBC_ISYSTEM_SYSCALL_H_
#define COSMOPOLITAN_LIBC_ISYSTEM_SYSCALL_H_
#ifndef COSMOPOLITAN_LIBC_ISYSTEM_SYS_SYSCALL_H_
#define COSMOPOLITAN_LIBC_ISYSTEM_SYS_SYSCALL_H_
#include "libc/stdio/syscall.h"
#endif /* COSMOPOLITAN_LIBC_ISYSTEM_SYSCALL_H_ */
#endif /* COSMOPOLITAN_LIBC_ISYSTEM_SYS_SYSCALL_H_ */

4
libc/isystem/syscall.h Normal file
View file

@ -0,0 +1,4 @@
#ifndef COSMOPOLITAN_LIBC_ISYSTEM_SYSCALL_H_
#define COSMOPOLITAN_LIBC_ISYSTEM_SYSCALL_H_
#include "libc/stdio/syscall.h"
#endif /* COSMOPOLITAN_LIBC_ISYSTEM_SYSCALL_H_ */

View file

@ -96,12 +96,9 @@ void countbranch_report(void) {
}
}
static textstartup void countbranch_init() {
__attribute__((__constructor__(90))) static textstartup void
countbranch_init() {
atexit(countbranch_report);
}
const void *const countbranch_ctor[] initarray = {
countbranch_init,
};
#endif /* __x86_64__ */

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/notice.inc"
.yoink countexpr_report

View file

@ -76,12 +76,8 @@ void countexpr_report(void) {
}
}
static textstartup void countexpr_init() {
__attribute__((__constructor__(90))) static textstartup void countexpr_init() {
atexit(countexpr_report);
}
const void *const countexpr_ctor[] initarray = {
countexpr_init,
};
#endif /* __x86_64__ */

View file

@ -36,12 +36,9 @@
#include "libc/str/str.h"
// clang-format off
asm(".ident\t\"\\n\\n\
FreeBSD Err (BSD-3 License)\\n\
Copyright (c) 1993\\n\
\tThe Regents of the University of California.\\n\
\tAll rights reserved.\"");
asm(".include \"libc/disclaimer.inc\"");
__notice(freebsd_err_notice, "\
FreeBSD Error Library (BSD-3 License)\n\
Copyright 1993 The Regents of the University of California");
static FILE *err_file; /* file to use for error output */
static void (*err_exit)(int);

View file

@ -21,6 +21,6 @@
FILE *__log_file;
__attribute__((__constructor__)) static void init(void) {
__attribute__((__constructor__(60))) static void loginit(void) {
__log_file = stderr;
}

View file

@ -66,7 +66,7 @@ static struct Memlog {
struct Alloc {
void *addr;
long size;
} * p;
} *p;
} allocs;
atomic_long usage;
} __memlog;
@ -270,7 +270,9 @@ static textexit void __memlog_destroy(void) {
__memlog_unlock();
}
static textstartup void __memlog_init(void) {
__attribute__((__constructor__(90))) //
static textstartup void
__memlog_init(void) {
GetSymbolTable();
__memlog_lock();
__memlog.free = hook_free;
@ -290,7 +292,3 @@ static textstartup void __memlog_init(void) {
atexit(__memlog_destroy);
__memlog_unlock();
}
const void *const enable_memory_log[] initarray = {
__memlog_init,
};

View file

@ -4,59 +4,69 @@
cosmopolitan § mathematics
*/
#define M_E 2.7182818284590452354 /* 𝑒 */
#define M_LOG2_10 0xd.49a784bcd1b8afep-2 /* log₂10 ≈ 3.3219280948873623478 */
#define M_LOG2E 0xb.8aa3b295c17f0bcp-3 /* log₂𝑒 ≈ 1.4426950408889634074 */
#define M_LOG10E 0.43429448190325182765 /* log₁₀𝑒 */
#define M_LN2 0xb.17217f7d1cf79acp-4 /* logₑ2 ≈ */
#define M_LN10 2.30258509299404568402 /* logₑ10 */
#define M_TAU 0x1.921fb54442d1846ap+2 /* τ = 2π */
#define M_PI 0x1.921fb54442d1846ap+1 /* π ≈ 3.14159265358979323846 */
#define M_PI_2 1.57079632679489661923 /* π/2 */
#define M_PI_4 0.78539816339744830962 /* π/4 */
#define M_1_PI 0.31830988618379067154 /* 1/π */
#define M_2_PI 0.63661977236758134308 /* 2/π */
#define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrtπ */
#define M_SQRT2 1.41421356237309504880 /* sqrt2 */
#define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt2 */
#define M_E 2.7182818284590452354 /* 𝑒 */
#define M_LOG2E 1.4426950408889634074 /* log₂𝑒 */
#define M_LOG10E 0.43429448190325182765 /* log₁₀𝑒 */
#define M_LN2 0.69314718055994530942 /* logₑ2 */
#define M_LN10 2.30258509299404568402 /* logₑ10 */
#define M_PI 3.14159265358979323846 /* pi */
#define M_PI_2 1.57079632679489661923 /* pi/2 */
#define M_PI_4 0.78539816339744830962 /* pi/4 */
#define M_1_PI 0.31830988618379067154 /* 1/pi */
#define M_2_PI 0.63661977236758134308 /* 2/pi */
#define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
#define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
#define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
#define DBL_DECIMAL_DIG __DBL_DECIMAL_DIG__
#define DBL_DIG __DBL_DIG__
#define DBL_EPSILON __DBL_EPSILON__
#define DBL_MANT_DIG __DBL_MANT_DIG__
#define DBL_MANT_DIG __DBL_MANT_DIG__
#define DBL_MAX __DBL_MAX__
#define DBL_MAX_10_EXP __DBL_MAX_10_EXP__
#define DBL_MAX_EXP __DBL_MAX_EXP__
#define DBL_MIN __DBL_MIN__ /* 2.23e308 ↔ 1.79e308 */
#define DBL_MIN_10_EXP __DBL_MIN_10_EXP__
#define DBL_MIN_EXP __DBL_MIN_EXP__
#define DECIMAL_DIG __LDBL_DECIMAL_DIG__
#define FLT_DECIMAL_DIG __FLT_DECIMAL_DIG__
#define FLT_RADIX __FLT_RADIX__
#define FLT_DIG __FLT_DIG__
#define FLT_EPSILON __FLT_EPSILON__
#define FLT_MANT_DIG __FLT_MANT_DIG__
#define FLT_MANT_DIG __FLT_MANT_DIG__
#define FLT_MAX __FLT_MAX__
#define FLT_MAX_10_EXP __FLT_MAX_10_EXP__
#define FLT_MAX_EXP __FLT_MAX_EXP__
#define FLT_MIN __FLT_MIN__ /* 1.18e38 ↔ 3.40e38 */
#define FLT_MIN_10_EXP __FLT_MIN_10_EXP__
#define FLT_MIN_EXP __FLT_MIN_EXP__
#define HLF_MAX 6.50e4f
#define HLF_MIN 3.10e-5f
#define LDBL_DECIMAL_DIG __LDBL_DECIMAL_DIG__
#define LDBL_DIG __LDBL_DIG__
#define LDBL_EPSILON __LDBL_EPSILON__
#define LDBL_MANT_DIG __LDBL_MANT_DIG__
#define LDBL_MANT_DIG __LDBL_MANT_DIG__
#define LDBL_MAX __LDBL_MAX__
#define LDBL_MAX_10_EXP __LDBL_MAX_10_EXP__
#define LDBL_MAX_EXP __LDBL_MAX_EXP__
#define LDBL_MIN __LDBL_MIN__ /* 3.37e4932 ↔ 1.18e4932 */
#define LDBL_MIN_10_EXP __LDBL_MIN_10_EXP__
#define LDBL_MIN_EXP __LDBL_MIN_EXP__
#define DBL_DECIMAL_DIG __DBL_DECIMAL_DIG__
#define DBL_DIG __DBL_DIG__
#define DBL_EPSILON __DBL_EPSILON__
#define DBL_HAS_SUBNORM __DBL_HAS_DENORM__
#define DBL_IS_IEC_60559 __DBL_IS_IEC_60559__
#define DBL_MANT_DIG __DBL_MANT_DIG__
#define DBL_MANT_DIG __DBL_MANT_DIG__
#define DBL_MAX __DBL_MAX__
#define DBL_MAX_10_EXP __DBL_MAX_10_EXP__
#define DBL_MAX_EXP __DBL_MAX_EXP__
#define DBL_MIN __DBL_MIN__ /* 2.23e308 ↔ 1.79e308 */
#define DBL_MIN_10_EXP __DBL_MIN_10_EXP__
#define DBL_MIN_EXP __DBL_MIN_EXP__
#define DBL_NORM_MAX __DBL_NORM_MAX__
#define DBL_TRUE_MIN __DBL_DENORM_MIN__
#define DECIMAL_DIG __LDBL_DECIMAL_DIG__
#define FLT_DECIMAL_DIG __FLT_DECIMAL_DIG__
#define FLT_DIG __FLT_DIG__
#define FLT_EPSILON __FLT_EPSILON__
#define FLT_HAS_SUBNORM __FLT_HAS_DENORM__
#define FLT_IS_IEC_60559 __FLT_IS_IEC_60559__
#define FLT_MANT_DIG __FLT_MANT_DIG__
#define FLT_MANT_DIG __FLT_MANT_DIG__
#define FLT_MAX __FLT_MAX__
#define FLT_MAX_10_EXP __FLT_MAX_10_EXP__
#define FLT_MAX_EXP __FLT_MAX_EXP__
#define FLT_MIN __FLT_MIN__ /* 1.18e38 ↔ 3.40e38 */
#define FLT_MIN_10_EXP __FLT_MIN_10_EXP__
#define FLT_MIN_EXP __FLT_MIN_EXP__
#define FLT_NORM_MAX __FLT_NORM_MAX__
#define FLT_RADIX __FLT_RADIX__
#define FLT_TRUE_MIN __FLT_DENORM_MIN__
#define HLF_MAX 6.50e4f
#define HLF_MIN 3.10e-5f
#define LDBL_DECIMAL_DIG __LDBL_DECIMAL_DIG__
#define LDBL_DIG __LDBL_DIG__
#define LDBL_EPSILON __LDBL_EPSILON__
#define LDBL_HAS_SUBNORM __LDBL_HAS_DENORM__
#define LDBL_IS_IEC_60559 __LDBL_IS_IEC_60559__
#define LDBL_MANT_DIG __LDBL_MANT_DIG__
#define LDBL_MANT_DIG __LDBL_MANT_DIG__
#define LDBL_MAX __LDBL_MAX__
#define LDBL_MAX_10_EXP __LDBL_MAX_10_EXP__
#define LDBL_MAX_EXP __LDBL_MAX_EXP__
#define LDBL_MIN __LDBL_MIN__ /* 3.37e4932 ↔ 1.18e4932 */
#define LDBL_MIN_10_EXP __LDBL_MIN_10_EXP__
#define LDBL_MIN_EXP __LDBL_MIN_EXP__
#define LDBL_NORM_MAX __LDBL_NORM_MAX__
#define LDBL_TRUE_MIN __LDBL_DENORM_MIN__
#define FP_NAN 0
#define FP_INFINITE 1

View file

@ -51,7 +51,7 @@ static void TeardownGc(void) {
}
}
__attribute__((__constructor__)) static void InitializeGc(void) {
__attribute__((__constructor__(51))) static textstartup void InitGc(void) {
atexit(TeardownGc);
}

View file

@ -31,6 +31,7 @@
#include "libc/mem/alg.h"
#include "libc/mem/mem.h"
#include "libc/sysv/errfuns.h"
__static_yoink("openbsd_sorting_notice");
// clang-format off
/*

View file

@ -33,13 +33,9 @@
#include "libc/mem/mem.h"
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"
__static_yoink("openbsd_sorting_notice");
// clang-format off
asm(".ident\t\"\\n\\n\
OpenBSD Sorting (BSD-3)\\n\
Copyright 1993 The Regents of the University of California\"");
asm(".include \"libc/disclaimer.inc\"");
/*
* Hybrid exponential search/linear search merge sort with hybrid
* natural/pairwise first pass. Requires about .3% more comparisons

2
libc/mem/opensort.c Normal file
View file

@ -0,0 +1,2 @@
__notice(openbsd_sorting_notice, "OpenBSD Sorting (BSD-3)\n\
Copyright 1993 The Regents of the University of California");

View file

@ -31,13 +31,9 @@
#include "libc/macros.internal.h"
#include "libc/mem/alg.h"
#include "libc/str/str.h"
__static_yoink("openbsd_sorting_notice");
// clang-format off
asm(".ident\t\"\\n\\n\
OpenBSD Sorting (BSD-3)\\n\
Copyright 1993 The Regents of the University of California\"");
asm(".include \"libc/disclaimer.inc\"");
#define SWAPTYPE_BYTEV 1
#define SWAPTYPE_INTV 2
#define SWAPTYPE_LONGV 3

View file

@ -28,20 +28,17 @@
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/serialize.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/limits.h"
#include "libc/log/backtrace.internal.h"
#include "libc/mem/mem.h"
#include "libc/serialize.h"
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"
__static_yoink("musl_libc_notice");
#define SYMLOOP_MAX 40
asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
// clang-format off
static size_t GetSlashLen(const char *s)

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/notice.inc"
.initbss 300,_init_argc
// Global variable holding _start(argc) parameter.

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/notice.inc"
.initbss 300,_init_argv
// Global variable holding _start(argv) parameter.

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/notice.inc"
.initbss 300,_init_auxv
// Global variable holding _start(auxv) parameter.

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/notice.inc"
// Environment variable pointer list.
.bss

View file

@ -18,7 +18,6 @@
*/
#include "libc/macros.internal.h"
#include "libc/dce.h"
#include "libc/notice.inc"
nop
@ -37,7 +36,7 @@ __gc: .ftrace2
#ifdef __x86_64__
mov %fs:0,%rcx // __get_tls()
mov %gs:0x30,%rcx // __get_tls()
mov 0x18(%rcx),%rcx // tls::garbages
decl (%rcx) // --g->i
mov (%rcx),%r8d // r8 = g->i

View file

@ -35,7 +35,7 @@ gclongjmp:
#ifdef __x86_64__
push %rbp
mov %rsp,%rbp
mov %fs:0,%r12 // __get_tls()
mov %gs:0x30,%r12 // __get_tls()
mov 0x18(%r12),%r12 // Tls::garbages
test %r12,%r12
jz 0f

Some files were not shown because too many files have changed in this diff Show more