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

@ -20,12 +20,12 @@
#include "libc/notice.inc"
.source __FILE__
/ Allocates n * itemsize bytes, initialized to zero.
/
/ @param rdi is number of items (n)
/ @param rsi is size of each item (itemsize)
/ @return rax is memory address, or NULL w/ errno
/ @note overreliance on memalign is a sure way to fragment space
/ @see dlcalloc()
// Allocates n * itemsize bytes, initialized to zero.
//
// @param rdi is number of items (n)
// @param rsi is size of each item (itemsize)
// @return rax is memory address, or NULL w/ errno
// @note overreliance on memalign is a sure way to fragment space
// @see dlcalloc()
calloc: jmp *hook_calloc(%rip)
.endfn calloc,globl

View file

@ -19,67 +19,67 @@
#include "libc/macros.h"
.source __FILE__
/ Frees memory the C++ way.
/
/ \param %rdi is pointer, or NULL for no-op
/ \param %rsi is ignored
/ \param %rdx is ignored
// Frees memory the C++ way.
//
// \param %rdi is pointer, or NULL for no-op
// \param %rsi is ignored
// \param %rdx is ignored
_ZdlPvSt11align_val_tRKSt9nothrow_t:
/ operator delete(void*, std::align_val_t, std::nothrow_t const&)
// operator delete(void*, std::align_val_t, std::nothrow_t const&)
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _ZdlPvSt11align_val_tRKSt9nothrow_t,weak
_ZdaPvSt11align_val_tRKSt9nothrow_t:
/ operator delete[](void*, std::align_val_t, std::nothrow_t const&)
// operator delete[](void*, std::align_val_t, std::nothrow_t const&)
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _ZdaPvSt11align_val_tRKSt9nothrow_t,weak
_ZdlPvRKSt9nothrow_t:
/ operator delete(void*, std::nothrow_t const&)
// operator delete(void*, std::nothrow_t const&)
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _ZdlPvRKSt9nothrow_t,weak
_ZdaPvRKSt9nothrow_t:
/ operator delete[](void*, std::nothrow_t const&)
// operator delete[](void*, std::nothrow_t const&)
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _ZdaPvRKSt9nothrow_t,weak
_ZdlPvmSt11align_val_t:
/ operator delete(void*, unsigned long, std::align_val_t)
// operator delete(void*, unsigned long, std::align_val_t)
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _ZdlPvmSt11align_val_t,weak
_ZdaPvmSt11align_val_t:
/ operator delete[](void*, unsigned long, std::align_val_t)
// operator delete[](void*, unsigned long, std::align_val_t)
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _ZdaPvmSt11align_val_t,weak
_ZdlPvSt11align_val_t:
/ operator delete(void*, std::align_val_t)
// operator delete(void*, std::align_val_t)
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _ZdlPvSt11align_val_t,weak
_ZdaPvSt11align_val_t:
/ operator delete[](void*, std::align_val_t)
// operator delete[](void*, std::align_val_t)
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _ZdaPvSt11align_val_t,weak
_ZdaPvm:
/ operator delete[](void*, unsigned long):
// operator delete[](void*, unsigned long):
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _ZdaPvm,weak
_ZdlPvm:
/ operator delete(void*, unsigned long)
// operator delete(void*, unsigned long)
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _ZdlPvm,weak
_ZdaPv:
/ operator delete[](void*)
// operator delete[](void*)
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _ZdaPv,weak
_ZdlPv:
/ operator delete(void*)
// operator delete(void*)
jmp *hook_free(%rip)
.endfn _ZdlPv,weak

View file

@ -19,28 +19,28 @@
#include "libc/macros.h"
.source __FILE__
/ Allocates memory the C++ way.
/
/ \param %rdi is bytes to allocate
/ \param %rsi is ignored
/ \return new memory or NULL on OOM
// Allocates memory the C++ way.
//
// \param %rdi is bytes to allocate
// \param %rsi is ignored
// \return new memory or NULL on OOM
_ZnamRKSt9nothrow_t:
/ operator new[](unsigned long, std::nothrow_t const&)
// operator new[](unsigned long, std::nothrow_t const&)
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _ZnamRKSt9nothrow_t,weak
_ZnwmRKSt9nothrow_t:
/ operator new(unsigned long, std::nothrow_t const&)
// operator new(unsigned long, std::nothrow_t const&)
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _ZnwmRKSt9nothrow_t,weak
_Znam:
/ operator new[](unsigned long)
// operator new[](unsigned long)
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _Znam,weak
_Znwm:
/ operator new(unsigned long)
// operator new(unsigned long)
test %rdi,%rdi
jne 1f
mov $1,%edi

View file

@ -19,29 +19,29 @@
#include "libc/macros.h"
.source __FILE__
/ Allocates aligned memory the C++ way.
/
/ \param %rdi is bytes to allocate
/ \param %rsi is byte alignment
/ \param %rdx is ignored
/ \return new memory or NULL on OOM
// Allocates aligned memory the C++ way.
//
// \param %rdi is bytes to allocate
// \param %rsi is byte alignment
// \param %rdx is ignored
// \return new memory or NULL on OOM
_ZnamSt11align_val_tRKSt9nothrow_t:
/ operator new[](unsigned long, std::align_val_t, std::nothrow_t const&)
// operator new[](unsigned long, std::align_val_t, std::nothrow_t const&)
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _ZnamSt11align_val_tRKSt9nothrow_t,weak
_ZnwmSt11align_val_tRKSt9nothrow_t:
/ operator new(unsigned long, std::align_val_t, std::nothrow_t const&)
// operator new(unsigned long, std::align_val_t, std::nothrow_t const&)
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _ZnwmSt11align_val_tRKSt9nothrow_t,weak
_ZnwmSt11align_val_t:
/ operator new(unsigned long, std::align_val_t)
// operator new(unsigned long, std::align_val_t)
nop
/ 𝑠𝑙𝑖𝑑𝑒
// 𝑠𝑙𝑖𝑑𝑒
.endfn _ZnwmSt11align_val_t,weak
_ZnamSt11align_val_t:
/ operator new[](unsigned long, std::align_val_t)
// operator new[](unsigned long, std::align_val_t)
test %rdi,%rdi
jnz 1f
mov $1,%eax

65
libc/mem/defer.greg.c Normal file
View file

@ -0,0 +1,65 @@
/*-*- 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/assert.h"
#include "libc/bits/likely.h"
#include "libc/calls/calls.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/gc.internal.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/runtime.h"
forceinline bool PointerNotOwnedByParentStackFrame(struct StackFrame *frame,
struct StackFrame *parent,
void *ptr) {
return !(((intptr_t)ptr > (intptr_t)frame) &&
((intptr_t)ptr < (intptr_t)parent));
}
/**
* Adds destructor to garbage shadow stack.
*
* @param frame is passed automatically by wrapper macro
* @param fn takes one argument
* @param arg is passed to fn(arg)
* @return arg
*/
void __defer(struct StackFrame *frame, void *fn, void *arg) {
size_t n2;
struct Garbage *p2;
struct StackFrame *f2;
if (!arg) return;
f2 = __builtin_frame_address(0);
assert(__garbage.n);
assert(f2->next == frame);
assert(PointerNotOwnedByParentStackFrame(f2, frame, arg));
if (UNLIKELY(__garbage.i == __garbage.n)) {
n2 = __garbage.n + (__garbage.n >> 1);
p2 = malloc(n2 * sizeof(*__garbage.p));
memcpy(p2, __garbage.p, __garbage.n * sizeof(*__garbage.p));
if (__garbage.p != __garbage.initmem) free(__garbage.p);
__garbage.p = p2;
__garbage.n = n2;
}
__garbage.p[__garbage.i].frame = frame->next;
__garbage.p[__garbage.i].fn = (intptr_t)fn;
__garbage.p[__garbage.i].arg = (intptr_t)arg;
__garbage.p[__garbage.i].ret = frame->addr;
__garbage.i++;
frame->addr = (intptr_t)__gc;
}

View file

@ -19,15 +19,15 @@
#include "libc/macros.h"
.source __FILE__
/ Free memory returned by malloc() & co.
/
/ Releases the chunk of memory pointed to by p, that had been
/ previously allocated using malloc or a related routine such as
/ realloc. It has no effect if p is null. If p was not malloced or
/ already freed, free(p) will by default cuase the current program to
/ abort.
/
/ @param rdi is allocation address, which may be NULL
/ @see dlfree()
// Free memory returned by malloc() & co.
//
// Releases the chunk of memory pointed to by p, that had been
// previously allocated using malloc or a related routine such as
// realloc. It has no effect if p is null. If p was not malloced or
// already freed, free(p) will by default cuase the current program to
// abort.
//
// @param rdi is allocation address, which may be NULL
// @see dlfree()
free: jmp *hook_free(%rip)
.endfn free,globl

View file

@ -19,22 +19,22 @@
#include "libc/macros.h"
.source __FILE__
/ Allocates uninitialized memory.
/
/ Returns a pointer to a newly allocated chunk of at least n bytes, or
/ null if no space is available, in which case errno is set to ENOMEM
/ on ANSI C systems.
/
/ If n is zero, malloc returns a minimum-sized chunk. (The minimum size
/ is 32 bytes on 64bit systems.) Note that size_t is an unsigned type,
/ so calls with arguments that would be negative if signed are
/ interpreted as requests for huge amounts of space, which will often
/ fail. The maximum supported value of n differs across systems, but is
/ in all cases less than the maximum representable value of a size_t.
/
/ @param rdi is number of bytes needed
/ @return new memory, or NULL w/ errno
/ @note malloc(0) → malloc(32)
/ @see dlmalloc()
// Allocates uninitialized memory.
//
// Returns a pointer to a newly allocated chunk of at least n bytes, or
// null if no space is available, in which case errno is set to ENOMEM
// on ANSI C systems.
//
// If n is zero, malloc returns a minimum-sized chunk. (The minimum size
// is 32 bytes on 64bit systems.) Note that size_t is an unsigned type,
// so calls with arguments that would be negative if signed are
// interpreted as requests for huge amounts of space, which will often
// fail. The maximum supported value of n differs across systems, but is
// in all cases less than the maximum representable value of a size_t.
//
// @param rdi is number of bytes needed
// @return new memory, or NULL w/ errno
// @note malloc(0) → malloc(32)
// @see dlmalloc()
malloc: jmp *hook_malloc(%rip)
.endfn malloc,globl

View file

@ -18,10 +18,10 @@
*/
#include "libc/macros.h"
/ Releases freed memory back to system.
/
/ @param rdi specifies bytes of memory to leave available
/ @return 1 if it actually released any memory, else 0
// Releases freed memory back to system.
//
// @param rdi specifies bytes of memory to leave available
// @return 1 if it actually released any memory, else 0
malloc_trim:
jmp *hook_malloc_trim(%rip)
.endfn malloc_trim,globl

View file

@ -20,22 +20,22 @@
#include "libc/notice.inc"
.source __FILE__
/ Returns the number of bytes you can actually use in
/ an allocated chunk, which may be more than you requested
/ (although often not) due to alignment and minimum size
/ constraints.
/
/ You can use this many bytes without worrying about overwriting
/ other allocated objects. This is not a particularly great
/ programming practice. malloc_usable_size can be more useful in
/ debugging and assertions, for example:
/
/ p = malloc(n)
/ assert(malloc_usable_size(p) >= 256)
/
/ @param rdi is address of allocation
/ @return rax is total number of bytes
/ @see dlmalloc_usable_size()
// Returns the number of bytes you can actually use in
// an allocated chunk, which may be more than you requested
// (although often not) due to alignment and minimum size
// constraints.
//
// You can use this many bytes without worrying about overwriting
// other allocated objects. This is not a particularly great
// programming practice. malloc_usable_size can be more useful in
// debugging and assertions, for example:
//
// p = malloc(n)
// assert(malloc_usable_size(p) >= 256)
//
// @param rdi is address of allocation
// @return rax is total number of bytes
// @see dlmalloc_usable_size()
malloc_usable_size:
jmp *hook_malloc_usable_size(%rip)
.endfn malloc_usable_size,globl

View file

@ -20,20 +20,20 @@
#include "libc/notice.inc"
.source __FILE__
/ Allocates aligned memory.
/
/ Returns a pointer to a newly allocated chunk of n bytes, aligned in
/ accord with the alignment argument. The alignment argument should be
/ a power of two. If the argument is not a power of two, the nearest
/ greater power is used. 8-byte alignment is guaranteed by normal
/ malloc calls, so don't bother calling memalign with an argument of 8
/ or less.
/
/ @param rdi is alignment in bytes
/ @param rsi (newsize) is number of bytes needed
/ @return rax is memory address, or NULL w/ errno
/ @note overreliance on memalign is a sure way to fragment space
/ @see dlmemalign()
// Allocates aligned memory.
//
// Returns a pointer to a newly allocated chunk of n bytes, aligned in
// accord with the alignment argument. The alignment argument should be
// a power of two. If the argument is not a power of two, the nearest
// greater power is used. 8-byte alignment is guaranteed by normal
// malloc calls, so don't bother calling memalign with an argument of 8
// or less.
//
// @param rdi is alignment in bytes
// @param rsi (newsize) is number of bytes needed
// @return rax is memory address, or NULL w/ errno
// @note overreliance on memalign is a sure way to fragment space
// @see dlmemalign()
memalign:
jmp *hook_memalign(%rip)
.endfn memalign,globl

View file

@ -19,18 +19,18 @@
#include "libc/macros.h"
.source __FILE__
/ Allocates aligned memory the POSIX way.
/
/ Allocates a chunk of n bytes, aligned in accord with the alignment
/ argument. Differs from memalign only in that it (1) assigns the
/ allocated memory to *pp rather than returning it, (2) fails and
/ returns EINVAL if the alignment is not a power of two (3) fails and
/ returns ENOMEM if memory cannot be allocated.
/
/ @param rdi is void **pp
/ @param rsi is size_t align
/ @param rdx is size_t size
/ @return eax
// Allocates aligned memory the POSIX way.
//
// Allocates a chunk of n bytes, aligned in accord with the alignment
// argument. Differs from memalign only in that it (1) assigns the
// allocated memory to *pp rather than returning it, (2) fails and
// returns EINVAL if the alignment is not a power of two (3) fails and
// returns ENOMEM if memory cannot be allocated.
//
// @param rdi is void **pp
// @param rsi is size_t align
// @param rdx is size_t size
// @return eax
posix_memalign:
jmp *hook_posix_memalign(%rip)
.endfn posix_memalign,globl

View file

@ -20,11 +20,11 @@
#include "libc/notice.inc"
.source __FILE__
/ Equivalent to valloc(minimum-page-that-holds(n)), that is,
/ round up n to nearest pagesize.
/
/ @param rdi is number of bytes needed
/ @return rax is memory address, or NULL w/ errno
/ @see dlpvalloc()
// Equivalent to valloc(minimum-page-that-holds(n)), that is,
// round up n to nearest pagesize.
//
// @param rdi is number of bytes needed
// @return rax is memory address, or NULL w/ errno
// @see dlpvalloc()
pvalloc:jmp *hook_pvalloc(%rip)
.endfn pvalloc,globl

View file

@ -20,40 +20,40 @@
#include "libc/notice.inc"
.source __FILE__
/ Allocates / resizes / frees memory, e.g.
/
/ Returns a pointer to a chunk of size n that contains the same data as
/ does chunk p up to the minimum of (n, p's size) bytes, or null if no
/ space is available.
/
/ If p is NULL, realloc is equivalent to malloc.
/ If p is not NULL and n is 0, realloc is equivalent to free.
/
/ The returned pointer may or may not be the same as p. The algorithm
/ prefers extending p in most cases when possible, otherwise it employs
/ the equivalent of a malloc-copy-free sequence.
/
/ Please note that p is NOT free()'d should realloc() fail, thus:
/
/ if ((p2 = realloc(p, n2))) {
/ p = p2;
/ ...
/ } else {
/ ...
/ }
/
/ if n is for fewer bytes than already held by p, the newly unused
/ space is lopped off and freed if possible.
/
/ The old unix realloc convention of allowing the last-free'd chunk to
/ be used as an argument to realloc is not supported.
/
/ @param rdi (p) is address of current allocation or NULL
/ @param rsi (n) is number of bytes needed
/ @return rax is result, or NULL w/ errno w/o free(p)
/ @note realloc(p=0, n=0) → malloc(32)
/ @note realloc(p≠0, n=0) → free(p)
/ @see dlrealloc()
// Allocates / resizes / frees memory, e.g.
//
// Returns a pointer to a chunk of size n that contains the same data as
// does chunk p up to the minimum of (n, p's size) bytes, or null if no
// space is available.
//
// If p is NULL, realloc is equivalent to malloc.
// If p is not NULL and n is 0, realloc is equivalent to free.
//
// The returned pointer may or may not be the same as p. The algorithm
// prefers extending p in most cases when possible, otherwise it employs
// the equivalent of a malloc-copy-free sequence.
//
// Please note that p is NOT free()'d should realloc() fail, thus:
//
// if ((p2 = realloc(p, n2))) {
// p = p2;
// ...
// } else {
// ...
// }
//
// if n is for fewer bytes than already held by p, the newly unused
// space is lopped off and freed if possible.
//
// The old unix realloc convention of allowing the last-free'd chunk to
// be used as an argument to realloc is not supported.
//
// @param rdi (p) is address of current allocation or NULL
// @param rsi (n) is number of bytes needed
// @return rax is result, or NULL w/ errno w/o free(p)
// @note realloc(p=0, n=0) → malloc(32)
// @note realloc(p≠0, n=0) → free(p)
// @see dlrealloc()
realloc:
jmp *hook_realloc(%rip)
.endfn realloc,globl

View file

@ -20,19 +20,19 @@
#include "libc/notice.inc"
.source __FILE__
/ Resizes the space allocated for p to size n, only if this can be
/ done without moving p (i.e., only if there is adjacent space
/ available if n is greater than p's current allocated size, or n
/ is less than or equal to p's size). This may be used instead of
/ plain realloc if an alternative allocation strategy is needed
/ upon failure to expand space, for example, reallocation of a
/ buffer that must be memory-aligned or cleared. You can use
/ realloc_in_place to trigger these alternatives only when needed.
/
/ @param rdi (p) is address of current allocation
/ @param rsi (newsize) is number of bytes needed
/ @return rax is result, or NULL w/ errno
/ @see dlrealloc_in_place()
// Resizes the space allocated for p to size n, only if this can be
// done without moving p (i.e., only if there is adjacent space
// available if n is greater than p's current allocated size, or n
// is less than or equal to p's size). This may be used instead of
// plain realloc if an alternative allocation strategy is needed
// upon failure to expand space, for example, reallocation of a
// buffer that must be memory-aligned or cleared. You can use
// realloc_in_place to trigger these alternatives only when needed.
//
// @param rdi (p) is address of current allocation
// @param rsi (newsize) is number of bytes needed
// @return rax is result, or NULL w/ errno
// @see dlrealloc_in_place()
realloc_in_place:
jmp *hook_realloc_in_place(%rip)
.endfn realloc_in_place,globl

View file

@ -20,10 +20,10 @@
#include "libc/notice.inc"
.source __FILE__
/ Equivalent to memalign(4096, n).
/
/ @param rdi is number of bytes needed
/ @return rax is memory address, or NULL w/ errno
/ @see dlvalloc()
// Equivalent to memalign(4096, n).
//
// @param rdi is number of bytes needed
// @return rax is memory address, or NULL w/ errno
// @see dlvalloc()
valloc: jmp *hook_valloc(%rip)
.endfn valloc,globl