Get codebase completely working with LLVM

You can now build Cosmopolitan with Clang:

    make -j8 MODE=llvm
    o/llvm/examples/hello.com

The assembler and linker code is now friendly to LLVM too.
So it's not needed to configure Clang to use binutils under
the hood. If you love LLVM then you can now use pure LLVM.
This commit is contained in:
Justine Tunney 2021-02-08 09:19:00 -08:00
parent 0e36cb3ac4
commit e75ffde09e
4528 changed files with 7776 additions and 11640 deletions

View file

@ -1,94 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/safemacros.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
int rc;
struct stat st;
const char *path;
int64_t fd, emptyspace, physicalspace;
TEST(fallocate_000, setup) {
mkdir("o", 0755);
mkdir("o/tmp", 0755);
}
TEST(fallocate_010, testBadFileDescriptor) {
if (IsOpenbsd()) return; /* ENOSYS */
if (IsFreebsd()) return; /* TODO: Where's my carry flag FreeBSD? */
close(70); /* just in case */
ASSERT_EQ(-1, fallocate(/*RHEL*/ 70, 0, 0, 1));
if (errno == ENOSYS) exit(0); /* RHEL5 */
EXPECT_EQ(EBADF, errno);
}
TEST(fallocate_020, test) {
if (IsOpenbsd()) return; /* ENOSYS */
path = gc(xasprintf("o/tmp/%s.%d", program_invocation_short_name));
ASSERT_NE(-1, (fd = creat(path, 0755)));
ASSERT_EQ(5, write(fd, "hello", 5));
errno = 31337;
ASSERT_NE(-1, fallocate(fd, 0, 0, 31337));
EXPECT_EQ(31337, errno);
ASSERT_EQ(5, write(fd, "world", 5));
ASSERT_NE(-1, close(fd));
ASSERT_NE(-1, stat(path, &st));
ASSERT_EQ(31337, st.st_size);
ASSERT_BINEQ(u"helloworld", gc(xslurp(path, NULL)));
unlink(path);
}
TEST(fallocate_020, testSparseFile) {
if (IsOpenbsd()) return; /* ENOSYS */
if (IsWindows()) return; /* TODO */
ASSERT_NE(-1, stat("o", &st));
emptyspace = rounddown(6 * 1000 * 1000 * 1000, st.st_blksize);
physicalspace = roundup(4096, st.st_blksize);
path = gc(xasprintf("o/tmp/%s.%d", program_invocation_short_name, getpid()));
ASSERT_NE(-1, (fd = creat(path, 0755)));
rc = fallocate(fd, 0, emptyspace, physicalspace);
if (rc == -1) {
/*
* most important feature is failing w/ enosys if not possible to
* allocate storage like a central banker prints money.
*/
ASSERT_EQ(ENOSYS, errno);
}
ASSERT_EQ(emptyspace, lseek(fd, emptyspace, SEEK_SET));
ASSERT_EQ(5, write(fd, "hello", 5));
ASSERT_NE(-1, fsync(fd));
ASSERT_NE(-1, close(fd));
ASSERT_NE(-1, stat(path, &st));
EXPECT_EQ(emptyspace + physicalspace, st.st_size);
/*
* don't care how much physical space system needs, so long as it's
* transparent and less than 10 percent the fake space
*/
EXPECT_NE(0, st.st_blocks);
EXPECT_LT(st.st_blocks * 512, emptyspace / 10);
unlink(path);
}

View file

@ -26,7 +26,7 @@
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/testlib.h"
bool gotsigint;
volatile bool gotsigint;
void OnSigInt(int sig) {
gotsigint = true;
@ -63,7 +63,7 @@ TEST(sigaction, test) {
EXPECT_EQ(1, WIFEXITED(status));
EXPECT_EQ(0, WEXITSTATUS(status));
EXPECT_EQ(0, WTERMSIG(status));
EXPECT_NE(-1, sigprocmask(SIG_BLOCK, &oldmask, NULL));
EXPECT_NE(-1, sigprocmask(SIG_SETMASK, &oldmask, NULL));
}
TEST(sigaction, raise) {

View file

@ -37,6 +37,7 @@ TEST_LIBC_CALLS_DIRECTDEPS = \
LIBC_STUBS \
LIBC_SYSV \
LIBC_TESTLIB \
LIBC_UNICODE \
LIBC_X
TEST_LIBC_CALLS_DEPS := \

View file

@ -19,6 +19,8 @@
#include "libc/dns/dns.h"
#include "libc/dns/dnsheader.h"
#include "libc/mem/mem.h"
#include "libc/rand/rand.h"
#include "libc/runtime/gc.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"
@ -36,13 +38,12 @@ TEST(serializednsheader, test) {
}
TEST(serializednsheader, fuzzSymmetry) {
uint8_t *buf = malloc(12);
struct DnsHeader *in = malloc(sizeof(struct DnsHeader));
struct DnsHeader *out = malloc(sizeof(struct DnsHeader));
uint8_t *buf;
struct DnsHeader *in, *out;
buf = gc(malloc(12));
in = rngset(gc(malloc(sizeof(struct DnsHeader))), 12, rand64, -1);
out = rngset(gc(malloc(sizeof(struct DnsHeader))), 12, rand64, -1);
ASSERT_EQ(12, serializednsheader(buf, 12, *in));
ASSERT_EQ(12, deserializednsheader(out, buf, 12));
ASSERT_EQ(0, memcmp(in, out, 12));
free(out);
free(in);
free(buf);
ASSERT_EQ(0, memcmp(in, out, 12), "%#.*s\n\t%#.*s", 12, in, 12, buf);
}

View file

@ -32,6 +32,7 @@ TEST_LIBC_DNS_DIRECTDEPS = \
LIBC_RUNTIME \
LIBC_SOCK \
LIBC_STDIO \
LIBC_RAND \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \

View file

@ -3,17 +3,16 @@
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
if CLANG=$(command -v clang); then
$COMPILE $CLANG \
$CLANG \
-o o/$MODE/test/libc/release/smokeclang.com.dbg \
-Os \
-Wall \
-Werror \
-static \
-no-pie \
-fno-pie \
-nostdlib \
-nostdinc \
-mno-red-zone \
-Wl,--gc-sections \
-Wl,-z,max-page-size=0x1000 \
-Wl,-T,o/$MODE/ape/ape.lds \
-include o/cosmopolitan.h \
test/libc/release/smoke.c \
@ -22,3 +21,5 @@ if CLANG=$(command -v clang); then
o/$MODE/cosmopolitan.a || exit
o/$MODE/test/libc/release/smokeclang.com.dbg || exit
fi
touch o/$MODE/test/libc/release/clang.ok

18
test/libc/release/emulate.sh Executable file
View file

@ -0,0 +1,18 @@
#!/bin/sh
# smoke test userspace binary emulation
CMD="o/$MODE/tool/build/blinkenlights.com.dbg o/$MODE/examples/hello.com"
printf '%s\n' "$CMD" >&2
if OUTPUT="$($CMD)"; then
if [ x"$OUTPUT" = x"hello world" ]; then
touch o/$MODE/test/libc/release/emulate.ok
exit 0
else
printf '%s\n' "error: $CMD printed wrong output: $OUTPUT" >&2
exit 1
fi
else
RC=$?
printf '%s\n' "error: $CMD failed: $RC" >&2
exit $RC
fi

18
test/libc/release/metal.sh Executable file
View file

@ -0,0 +1,18 @@
#!/bin/sh
# smoke test booting on bare metal and printing data to serial uart
CMD="o/$MODE/tool/build/blinkenlights.com.dbg -r o/$MODE/examples/hello.com"
printf '%s\n' "$CMD" >&2
if OUTPUT="$($CMD)"; then
if [ x"$OUTPUT" = x"hello world" ]; then
touch o/$MODE/test/libc/release/metal.ok
exit 0
else
printf '%s\n' "error: $CMD printed wrong output: $OUTPUT" >&2
exit 1
fi
else
RC=$?
printf '%s\n' "error: $CMD failed: $RC" >&2
exit $RC
fi

View file

@ -3,11 +3,13 @@ int main(int argc, char *argv[]) {
char *s;
FILE *f;
s = strdup(argv[0]);
s[0] = 'Z';
f = fopen("/dev/null", "w");
fprintf(f, "hello world %d %s\n", argc, s);
fclose(f);
rc = system("exit 42");
CHECK_NE(-1, rc);
CHECK(WIFEXITED(rc));
CHECK_EQ(42, WEXITSTATUS(rc));
free(s);
return 0;

View file

@ -11,7 +11,7 @@ o/$(MODE)/test/libc/release/cosmopolitan.zip: \
o/$(MODE)/test/libc/release/smoke.com: \
o/$(MODE)/test/libc/release/smoke.com.dbg
@objcopy -SO binary $< $@
@$(COMPILE) $(OBJCOPY) -S -O binary $< $@
o/$(MODE)/test/libc/release/smoke.com.dbg: \
test/libc/release/smoke.c \
@ -29,8 +29,6 @@ o/$(MODE)/test/libc/release/smoke.com.dbg: \
-nostdlib \
-nostdinc \
-mno-red-zone \
-Wl,--gc-sections \
-Wl,-z,max-page-size=0x1000 \
-Wl,-T,o/$(MODE)/ape/ape.lds \
-include o/cosmopolitan.h \
test/libc/release/smoke.c \
@ -40,7 +38,7 @@ o/$(MODE)/test/libc/release/smoke.com.dbg: \
o/$(MODE)/test/libc/release/smokecxx.com: \
o/$(MODE)/test/libc/release/smokecxx.com.dbg
@objcopy -SO binary $< $@
@$(COMPILE) $(OBJCOPY) -S -O binary $< $@
o/$(MODE)/test/libc/release/smokecxx.com.dbg: \
test/libc/release/smokecxx.cc \
@ -58,8 +56,6 @@ o/$(MODE)/test/libc/release/smokecxx.com.dbg: \
-nostdlib \
-nostdinc \
-mno-red-zone \
-Wl,--gc-sections \
-Wl,-z,max-page-size=0x1000 \
-Wl,-T,o/$(MODE)/ape/ape.lds \
-include o/cosmopolitan.h \
test/libc/release/smokecxx.cc \
@ -84,8 +80,6 @@ o/$(MODE)/test/libc/release/smokeansi.com.dbg: \
-nostdlib \
-nostdinc \
-mno-red-zone \
-Wl,--gc-sections \
-Wl,-z,max-page-size=0x1000 \
-Wl,-T,o/$(MODE)/ape/ape.lds \
-include o/cosmopolitan.h \
test/libc/release/smoke.c \
@ -93,7 +87,7 @@ o/$(MODE)/test/libc/release/smokeansi.com.dbg: \
o/$(MODE)/ape/ape.o \
o/$(MODE)/cosmopolitan.a
o/$(MODE)/test/libc/release/smokeclang.ok: \
o/$(MODE)/test/libc/release/clang.ok: \
test/libc/release/clang.sh \
test/libc/release/smoke.c \
o/cosmopolitan.h \
@ -103,6 +97,18 @@ o/$(MODE)/test/libc/release/smokeclang.ok: \
o/$(MODE)/cosmopolitan.a
@$<
o/$(MODE)/test/libc/release/metal.ok: \
test/libc/release/metal.sh \
o/$(MODE)/examples/hello.com \
o/$(MODE)/tool/build/blinkenlights.com.dbg
@$<
o/$(MODE)/test/libc/release/emulate.ok: \
test/libc/release/emulate.sh \
o/$(MODE)/examples/hello.com \
o/$(MODE)/tool/build/blinkenlights.com.dbg
@$<
.PHONY: o/$(MODE)/test/libc/release
o/$(MODE)/test/libc/release: \
o/$(MODE)/test/libc/release/smoke.com \
@ -111,4 +117,6 @@ o/$(MODE)/test/libc/release: \
o/$(MODE)/test/libc/release/smokecxx.com.runs \
o/$(MODE)/test/libc/release/smokeansi.com \
o/$(MODE)/test/libc/release/smokeansi.com.runs \
o/$(MODE)/test/libc/release/smokeclang.ok
o/$(MODE)/test/libc/release/clang.ok \
o/$(MODE)/test/libc/release/emulate.ok \
o/$(MODE)/test/libc/release/metal.ok

View file

@ -35,6 +35,8 @@
* @see __addvsi3, __mulvsi3, etc.
*/
#ifndef __llvm__ /* TODO(jart): wut */
volatile bool overflowed_;
void __on_arithmetic_overflow(void) {
@ -351,3 +353,5 @@ TEST(__mulvti3, testOverflow) {
x *= 0xb504f333f9de6d29;
EXPECT_TRUE(overflowed_);
}
#endif /* __llvm__ */

View file

@ -63,7 +63,7 @@ static void CheckMemoryIntervalsAreOk(const struct MemoryIntervals *mm) {
static void RunTrackMemoryIntervalTest(const struct MemoryIntervals t[2], int x,
int y, long h) {
struct MemoryIntervals *mm;
mm = memcpy(memalign(alignof(*t), sizeof(*t)), t, sizeof(*t));
mm = memcpy(malloc(sizeof(*t)), t, sizeof(*t));
CheckMemoryIntervalsAreOk(mm);
CHECK_NE(-1, TrackMemoryInterval(mm, x, y, h, 0, 0));
CheckMemoryIntervalsAreOk(mm);
@ -75,7 +75,7 @@ static int RunReleaseMemoryIntervalsTest(const struct MemoryIntervals t[2],
int x, int y) {
int rc;
struct MemoryIntervals *mm;
mm = memcpy(memalign(alignof(*t), sizeof(*t)), t, sizeof(*t));
mm = memcpy(malloc(sizeof(*t)), t, sizeof(*t));
CheckMemoryIntervalsAreOk(mm);
if ((rc = ReleaseMemoryIntervals(mm, x, y, NULL)) != -1) {
CheckMemoryIntervalsAreOk(mm);

View file

@ -38,6 +38,7 @@ TEST_LIBC_RUNTIME_DIRECTDEPS = \
LIBC_SYSV \
LIBC_TESTLIB \
LIBC_TINYMATH \
LIBC_UNICODE \
LIBC_X \
THIRD_PARTY_XED

View file

@ -36,6 +36,7 @@ TEST_LIBC_STDIO_DIRECTDEPS = \
LIBC_STUBS \
LIBC_SYSV \
LIBC_TESTLIB \
LIBC_UNICODE \
LIBC_X
TEST_LIBC_STDIO_DEPS := \

View file

@ -1,48 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/str/sha256.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/hyperion.h"
#include "libc/testlib/testlib.h"
uint8_t *sha256(const char *s) {
static uint8_t hash[32];
struct Sha256Ctx ctx;
sha256_init(&ctx);
sha256_update(&ctx, (const uint8_t *)s, strlen(s));
sha256_final(&ctx, hash);
return hash;
}
TEST(sha256, testEmpty) {
EXPECT_BINEQ(
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
sha256(""));
}
TEST(sha256, test) {
EXPECT_BINEQ(u",≥M║_░ú♫&Φ;*┼╣Γ€←▬▲\\▼ºB^s♦3bôïÿ$", sha256("hello"));
EXPECT_BINEQ("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b98",
sha256("hello"));
}
TEST(sha256, testNontrivialSize) {
EXPECT_BINEQ(u"╨╒║☺ª↨╨╒ù€»╝∞nfÑ4Æ╒Tn╫╕`eóA¿↑[3╬", sha256(kHyperion));
}

View file

@ -32,6 +32,7 @@ TEST_LIBC_TINYMATH_DIRECTDEPS = \
LIBC_STUBS \
LIBC_TESTLIB \
LIBC_TINYMATH \
LIBC_UNICODE \
LIBC_X
TEST_LIBC_TINYMATH_DEPS := \

View file

@ -33,6 +33,7 @@ TEST_LIBC_X_DIRECTDEPS = \
LIBC_STR \
LIBC_STUBS \
LIBC_TESTLIB \
LIBC_UNICODE \
LIBC_X \
THIRD_PARTY_GDTOA