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

@ -29,7 +29,6 @@
#include "libc/errno.h"
#include "libc/fmt/itoa.h"
#include "libc/fmt/magnumstrs.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/limits.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
@ -226,8 +225,56 @@ static void CheckPrivilegedCrossReferences(void) {
}
}
// Change AMD code to use %gs:0x30 instead of %fs:0
// We assume -mno-tls-direct-seg-refs has been used
static void ChangeTlsFsToGs(unsigned char *p, size_t n) {
unsigned char *e = p + n - 9;
while (p <= e) {
// we're checking for the following expression:
// 0144 == p[0] && // %fs
// 0110 == (p[1] & 0373) && // rex.w (and ignore rex.r)
// (0213 == p[2] || // mov reg/mem → reg (word-sized)
// 0003 == p[2]) && // add reg/mem → reg (word-sized)
// 0004 == (p[3] & 0307) && // mod/rm (4,reg,0) means sib → reg
// 0045 == p[4] && // sib (5,4,0) → (rbp,rsp,0) → disp32
// 0000 == p[5] && // displacement (von Neumann endian)
// 0000 == p[6] && // displacement
// 0000 == p[7] && // displacement
// 0000 == p[8] // displacement
uint64_t w = READ64LE(p) & READ64LE("\377\373\377\307\377\377\377\377");
if ((w == READ64LE("\144\110\213\004\045\000\000\000") ||
w == READ64LE("\144\110\003\004\045\000\000\000")) &&
!p[8]) {
p[0] = 0145; // change %fs to %gs
p[5] = 0x30; // change 0 to 0x30
p += 9;
} else {
++p;
}
}
}
static void RewriteTlsCodeAmd64(void) {
int i;
uint8_t *p;
Elf64_Shdr *shdr;
for (i = 0; i < elf->e_shnum; ++i) {
if (!(shdr = GetElfSectionHeaderAddress(elf, esize, i))) {
Die("elf header overflow #1");
}
if (shdr->sh_type == SHT_PROGBITS && //
(shdr->sh_flags & SHF_ALLOC) && //
(shdr->sh_flags & SHF_EXECINSTR)) {
if (!(p = GetElfSectionAddress(elf, esize, shdr))) {
Die("elf header overflow #2");
}
ChangeTlsFsToGs(p, shdr->sh_size);
}
}
}
// Modify ARM64 code to use x28 for TLS rather than tpidr_el0.
static void RewriteTlsCode(void) {
static void RewriteTlsCodeArm64(void) {
int i;
Elf64_Shdr *shdr;
uint32_t *p, *pe;
@ -594,10 +641,11 @@ static void FixupObject(void) {
CheckPrivilegedCrossReferences();
if (mode == O_RDWR) {
if (elf->e_machine == EM_NEXGEN32E) {
RewriteTlsCodeAmd64();
OptimizePatchableFunctionEntries();
GenerateIfuncInit();
} else if (elf->e_machine == EM_AARCH64) {
RewriteTlsCode();
RewriteTlsCodeArm64();
if (elf->e_type != ET_REL) {
UseFreebsdOsAbi();
}

View file

@ -9,12 +9,13 @@ reach a broader audience from the platform(s) of your choosing.
## What's Included
This toolchain bundles GCC 11.2.0, Cosmopolitan Libc, LLVM LIBCXX, and
LLVM compiler-rt. Additional libraries were provided by Musl Libc, and
the venerable BSDs OSes. This lets you benefit from the awesome modern
GCC compiler with the strongest GPL barrier possible. The preprocessor
advertises cross compilers as both `__COSMOCC__` and `__COSMOPOLITAN__`
whereas `cosmocc` additionally defines `__FATCOSMOCC__`.
This toolchain bundles GCC 12.3.0, Cosmopolitan Libc, LLVM LIBCXX, LLVM
compiler-rt, and LLVM OpenMP. Additional libraries were provided by Musl
Libc, and the venerable BSDs OSes. This lets you benefit from the
awesome modern GCC compiler with the strongest GPL barrier possible. The
preprocessor advertises cross compilers as both `__COSMOCC__` and
`__COSMOPOLITAN__` whereas `cosmocc` additionally defines
`__FATCOSMOCC__`.
## Getting Started
@ -296,9 +297,9 @@ EINVAL: ... }` in cases where constants like `EINVAL` are linkable
symbols. Your code will be rewritten in such cases to use a series of if
statements instead, so that Cosmopolitan Libc's system constants will
work as expected. Our modifications to GNU GCC are published under the
ISC license at <https://github.com/ahgamut/gcc/tree/portcosmo-11.2>. The
ISC license at <https://github.com/ahgamut/gcc/tree/portcosmo-12.3>. The
binaries you see here were first published at
<https://github.com/ahgamut/superconfigure/releases/tag/z0.0.30> which
<https://github.com/ahgamut/superconfigure/releases/tag/z0.0.32> which
is regularly updated.
## Legal

View file

@ -239,7 +239,7 @@ PLATFORM="-D__COSMOPOLITAN__ -D__COSMOCC__ -D__FATCOSMOCC__"
PREDEF="-include libc/integral/normalize.inc"
CPPFLAGS="-fno-pie -nostdinc -fno-math-errno -isystem $BIN/../include"
CFLAGS="-fportcosmo -fno-dwarf2-cfi-asm -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-semantic-interposition"
LDFLAGS="-static -nostdlib -no-pie -fuse-ld=bfd -Wl,-z,norelro -Wl,--gc-sections"
LDFLAGS="-static -nostdlib -no-pie -fuse-ld=bfd -Wl,-z,noexecstack -Wl,-z,norelro -Wl,--gc-sections"
PRECIOUS="-fno-omit-frame-pointer"
if [ x"$OPT" != x"-Os" ] && [ x"$MODE" != x"tiny" ]; then

View file

@ -49,7 +49,7 @@ PLATFORM="-D__COSMOPOLITAN__ -D__COSMOCC__"
PREDEF="-include libc/integral/normalize.inc"
CFLAGS="-fportcosmo -fno-dwarf2-cfi-asm -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-semantic-interposition"
CPPFLAGS="-fno-pie -nostdinc -fno-math-errno -isystem $BIN/../include"
LDFLAGS="-static -no-pie -nostdlib -fuse-ld=bfd"
LDFLAGS="-static -no-pie -nostdlib -fuse-ld=bfd -Wl,-z,noexecstack"
APEFLAGS="-Wl,--gc-sections"
PRECIOUS="-fno-omit-frame-pointer"

View file

@ -19,6 +19,7 @@ OUTDIR=${1:-cosmocc}
APELINK=o/$(mode)/tool/build/apelink.com
AMD64=${2:-x86_64}
ARM64=${3:-aarch64}
GCCVER=12.3.0
make -j32 m= \
$APELINK
@ -89,10 +90,10 @@ fetch() {
OLD=$PWD
cd "$OUTDIR/"
if [ ! -x bin/x86_64-linux-cosmo-gcc ]; then
fetch https://github.com/ahgamut/superconfigure/releases/download/z0.0.30/aarch64-gcc.zip
fetch https://github.com/ahgamut/superconfigure/releases/download/z0.0.32/aarch64-gcc.zip
unzip aarch64-gcc.zip
rm -f aarch64-gcc.zip
fetch https://github.com/ahgamut/superconfigure/releases/download/z0.0.30/x86_64-gcc.zip
fetch https://github.com/ahgamut/superconfigure/releases/download/z0.0.32/x86_64-gcc.zip
unzip x86_64-gcc.zip
rm -f x86_64-gcc.zip
fi
@ -113,14 +114,14 @@ for arch in aarch64 x86_64; do
ln -sf $arch-linux-cosmo-objdump bin/$arch-unknown-cosmo-objdump
ln -sf $arch-linux-cosmo-readelf bin/$arch-unknown-cosmo-readelf
ln -sf $arch-linux-cosmo-strip bin/$arch-unknown-cosmo-strip
cmp -s libexec/gcc/$arch-linux-cosmo/11.2.0/ld.bfd libexec/gcc/$arch-linux-cosmo/11.2.0/ld
ln -sf ld.bfd libexec/gcc/$arch-linux-cosmo/11.2.0/ld
cmp -s libexec/gcc/$arch-linux-cosmo/11.2.0/ld.bfd bin/$arch-linux-cosmo-ld
ln -sf ../libexec/gcc/$arch-linux-cosmo/11.2.0/ld.bfd bin/$arch-linux-cosmo-ld
cmp -s libexec/gcc/$arch-linux-cosmo/11.2.0/as bin/$arch-linux-cosmo-as
ln -sf ../libexec/gcc/$arch-linux-cosmo/11.2.0/as bin/$arch-linux-cosmo-as
cmp -s libexec/gcc/$arch-linux-cosmo/11.2.0/ld.bfd bin/$arch-linux-cosmo-ld.bfd
ln -sf ../libexec/gcc/$arch-linux-cosmo/11.2.0/ld.bfd bin/$arch-linux-cosmo-ld.bfd
cmp -s libexec/gcc/$arch-linux-cosmo/$GCCVER/ld.bfd libexec/gcc/$arch-linux-cosmo/$GCCVER/ld
ln -sf ld.bfd libexec/gcc/$arch-linux-cosmo/$GCCVER/ld
cmp -s libexec/gcc/$arch-linux-cosmo/$GCCVER/ld.bfd bin/$arch-linux-cosmo-ld
ln -sf ../libexec/gcc/$arch-linux-cosmo/$GCCVER/ld.bfd bin/$arch-linux-cosmo-ld
cmp -s libexec/gcc/$arch-linux-cosmo/$GCCVER/as bin/$arch-linux-cosmo-as
ln -sf ../libexec/gcc/$arch-linux-cosmo/$GCCVER/as bin/$arch-linux-cosmo-as
cmp -s libexec/gcc/$arch-linux-cosmo/$GCCVER/ld.bfd bin/$arch-linux-cosmo-ld.bfd
ln -sf ../libexec/gcc/$arch-linux-cosmo/$GCCVER/ld.bfd bin/$arch-linux-cosmo-ld.bfd
done
cd "$OLD"

View file

@ -121,7 +121,6 @@ Keywords={
"hasatleast",
"nodebuginfo",
"noreturn",
"initarray",
"mayalias",
"dontinstrument",
"interruptfn",

View file

@ -65,6 +65,7 @@
"pltoff"
"gotpcrel"
"progbits"
"note"
"nobits"
"init_array"
"fini_array"

View file

@ -204,6 +204,7 @@
'("DebugBreak"
"__veil"
"__conceal"
"__notice"
"__expropriate"
"__yoink"
"__dll_import"

View file

@ -87,7 +87,6 @@
"nomsan"
"dontubsan"
"nostackprotector"
"initarray"
"mayalias"
"dontinstrument"
"interruptfn"

View file

@ -335,7 +335,6 @@ cosmo_kws = frozenset([
"forcealignargpointer",
"forceinline",
"hasatleast",
"initarray",
"interruptfn",
"mallocesque",
"mayalias",
@ -394,7 +393,6 @@ cosmo_kws = frozenset([
"forcealignargpointer",
"forceinline",
"hasatleast",
"initarray",
"interruptfn",
"mallocesque",
"mayalias",

View file

@ -39,7 +39,7 @@ o/$(MODE)/tool/hello/hello.com.dbg: \
# uses apelink to turn it into an ape executable
# support vector is set to all operating systems
o/$(MODE)/tool/hello/hello.com: \
o/$(MODE)/tool/hello/hello.ape: \
o/$(MODE)/tool/hello/hello.com.dbg \
o/$(MODE)/tool/build/apelink.com \
o/$(MODE)/tool/build/pecheck.com \
@ -49,7 +49,7 @@ o/$(MODE)/tool/hello/hello.com: \
# uses apelink to generate elf-only executable
# support vector = linux/freebsd/openbsd/netbsd/metal
o/$(MODE)/tool/hello/hello-elf.com: \
o/$(MODE)/tool/hello/hello-elf.ape: \
o/$(MODE)/tool/hello/hello.com.dbg \
o/$(MODE)/tool/build/apelink.com \
o/$(MODE)/ape/ape.elf
@ -59,7 +59,7 @@ o/$(MODE)/tool/hello/hello-elf.com: \
# support vector = macos/linux/freebsd/openbsd/netbsd
# - great way to avoid attention from bad virus scanners
# - creates tinier executable by reducing alignment requirement
o/$(MODE)/tool/hello/hello-unix.com: \
o/$(MODE)/tool/hello/hello-unix.ape: \
o/$(MODE)/tool/hello/hello.com.dbg \
o/$(MODE)/tool/build/apelink.com \
o/$(MODE)/ape/ape.elf
@ -71,7 +71,7 @@ o/$(MODE)/tool/hello/hello-unix.com: \
o/$(MODE)/tool/hello/hello-pe.com.dbg: \
o/$(MODE)/tool/hello/hello-pe.o
@$(COMPILE) -ALINK.elf $(LINK) $(LINKARGS) $(OUTPUT_OPTION) -q -e WinMain
o/$(MODE)/tool/hello/hello-pe.com: \
o/$(MODE)/tool/hello/hello-pe.ape: \
o/$(MODE)/tool/hello/hello-pe.com.dbg \
o/$(MODE)/tool/build/elf2pe.com
@$(COMPILE) -AELF2PE o/$(MODE)/tool/build/elf2pe.com -o $@ $<
@ -80,7 +80,7 @@ o/$(MODE)/tool/hello/hello-pe.com: \
o/$(MODE)/tool/hello/life-pe.com.dbg: \
o/$(MODE)/tool/hello/life-pe.o
@$(COMPILE) -ALINK.elf $(LINK) $(LINKARGS) $(OUTPUT_OPTION) -q -e WinMain
o/$(MODE)/tool/hello/life-pe.com: \
o/$(MODE)/tool/hello/life-pe.ape: \
o/$(MODE)/tool/hello/life-pe.com.dbg \
o/$(MODE)/tool/build/elf2pe.com
@$(COMPILE) -AELF2PE o/$(MODE)/tool/build/elf2pe.com -o $@ $<
@ -89,7 +89,7 @@ o/$(MODE)/tool/hello/life-pe.com: \
o/$(MODE)/tool/hello/wait-pe.com.dbg: \
o/$(MODE)/tool/hello/wait-pe.o
@$(COMPILE) -ALINK.elf $(LINK) $(LINKARGS) $(OUTPUT_OPTION) -q -e WinMain
o/$(MODE)/tool/hello/wait-pe.com: \
o/$(MODE)/tool/hello/wait-pe.ape: \
o/$(MODE)/tool/hello/wait-pe.com.dbg \
o/$(MODE)/tool/build/elf2pe.com
@$(COMPILE) -AELF2PE o/$(MODE)/tool/build/elf2pe.com -R 64kb -S 4kb -o $@ $<

View file

@ -32,10 +32,9 @@
#include "third_party/lua/lua.h"
#include "third_party/lua/lualib.h"
asm(".ident\t\"\\n\\n\
largon2 (MIT License)\\n\
Copyright 2016 Thibault Charbonnier\"");
asm(".include \"libc/disclaimer.inc\"");
__notice(largon2_notice, "\
largon2 (MIT License)\n\
Copyright 2016 Thibault Charbonnier");
// clang-format off
/***

View file

@ -38,10 +38,9 @@
#include "third_party/sqlite3/sqlite3.h"
// clang-format off
asm(".ident\t\"\\n\\n\
lsqlite3 (MIT License)\\n\
Copyright 2002-2016 Tiago Dionizio, Doug Currie\"");
asm(".include \"libc/disclaimer.inc\"");
__notice(lsqlite3_notice, "\
lsqlite3 (MIT License)\n\
Copyright 2002-2016 Tiago Dionizio, Doug Currie");
// LOCAL CHANGES
//