mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-01 16:58:30 +00:00
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:
parent
0e36cb3ac4
commit
e75ffde09e
4528 changed files with 7776 additions and 11640 deletions
|
@ -67,36 +67,36 @@ __hostos:
|
|||
.quad 0
|
||||
.endobj __hostos,globl,hidden
|
||||
|
||||
/ Performs System Five System Call.
|
||||
/
|
||||
/ Cosmopolitan is designed to delegate all function calls into the
|
||||
/ Linux, FreeBSD, OpenBSD, and XNU kernels via this function, with
|
||||
/ few exceptions. This function should generally only be called by
|
||||
/ generated thunks in the libc/sysv/syscalls/ directory.
|
||||
/
|
||||
/ It's safe to call this function on Windows, where it will always
|
||||
/ return -1 with errno == ENOSYS. Further note that -1 is the only
|
||||
/ return value that means error, a common anti-pattern is to check
|
||||
/ for values less than 0 (which is more problematic on 32-bit).
|
||||
/
|
||||
/ It is important to consider that system calls are one order of a
|
||||
/ magnitude more expensive than normal function calls. For example
|
||||
/ getpid() on Linux usually takes 500ns, and cached i/o calls will
|
||||
/ take 1µs or more. So we don't need to inline them like Chromium.
|
||||
/
|
||||
/ Another thing to consider is that BSDs only loosely follow the
|
||||
/ System Five ABI for the SYSCALL instruction. For example Linux
|
||||
/ always follows the six argument limit but the FreeBSD sendfile
|
||||
/ system call accepts a seventh argument that is passed on stack
|
||||
/ and OpenBSD modifies functions like mmap so that the sixth arg
|
||||
/ is passed on the stack. There's also the carry flag convention
|
||||
/ that XNU, FreeBSD, and OpenBSD inherited from 386BSD aka Jolix
|
||||
/
|
||||
/ @param %rax function ordinal supplied by jump slot
|
||||
/ @param %rdi,%rsi,%rdx,%rcx,%r8,%r9 and rest on stack
|
||||
/ @return %rax:%rdx is result, or -1 w/ errno on error
|
||||
/ @clob %rcx,%r10,%r11
|
||||
/ @see syscalls.sh
|
||||
// Performs System Five System Call.
|
||||
//
|
||||
// Cosmopolitan is designed to delegate all function calls into the
|
||||
// Linux, FreeBSD, OpenBSD, and XNU kernels via this function, with
|
||||
// few exceptions. This function should generally only be called by
|
||||
// generated thunks in the libc/sysv/syscalls/ directory.
|
||||
//
|
||||
// It's safe to call this function on Windows, where it will always
|
||||
// return -1 with errno == ENOSYS. Further note that -1 is the only
|
||||
// return value that means error, a common anti-pattern is to check
|
||||
// for values less than 0 (which is more problematic on 32-bit).
|
||||
//
|
||||
// It is important to consider that system calls are one order of a
|
||||
// magnitude more expensive than normal function calls. For example
|
||||
// getpid() on Linux usually takes 500ns, and cached i/o calls will
|
||||
// take 1µs or more. So we don't need to inline them like Chromium.
|
||||
//
|
||||
// Another thing to consider is that BSDs only loosely follow the
|
||||
// System Five ABI for the SYSCALL instruction. For example Linux
|
||||
// always follows the six argument limit but the FreeBSD sendfile
|
||||
// system call accepts a seventh argument that is passed on stack
|
||||
// and OpenBSD modifies functions like mmap so that the sixth arg
|
||||
// is passed on the stack. There's also the carry flag convention
|
||||
// that XNU, FreeBSD, and OpenBSD inherited from 386BSD aka Jolix
|
||||
//
|
||||
// @param %rax function ordinal supplied by jump slot
|
||||
// @param %rdi,%rsi,%rdx,%rcx,%r8,%r9 and rest on stack
|
||||
// @return %rax:%rdx is result, or -1 w/ errno on error
|
||||
// @clob %rcx,%r10,%r11
|
||||
// @see syscalls.sh
|
||||
__systemfive:
|
||||
.quad 0
|
||||
.endobj __systemfive,globl,hidden
|
||||
|
@ -105,184 +105,184 @@ __systemfive:
|
|||
.privileged
|
||||
.Lanchorpoint:
|
||||
#if SupportsLinux()
|
||||
systemfive.linux:
|
||||
systemfive_linux:
|
||||
and $0xfff,%eax
|
||||
cmp $0xfff,%eax
|
||||
je systemfive.enosys
|
||||
je systemfive_enosys
|
||||
mov %rcx,%r10 # syscall instruction clobbers %rcx
|
||||
push %rbp # linux never reads args from stack
|
||||
mov %rsp,%rbp # having frame will help backtraces
|
||||
syscall # this is known as a context switch
|
||||
pop %rbp # next we check to see if it failed
|
||||
cmp $-4095,%rax # system five nexgen32e abi § A.2.1
|
||||
jae systemfive.error # encodes errno as neg return value
|
||||
jae systemfive_error # encodes errno as neg return value
|
||||
ret
|
||||
.endfn systemfive.linux,globl,hidden
|
||||
systemfive.error:
|
||||
.endfn systemfive_linux,globl,hidden
|
||||
systemfive_error:
|
||||
neg %eax
|
||||
/ 𝑠𝑙𝑖𝑑𝑒
|
||||
.endfn systemfive.error,globl,hidden
|
||||
// 𝑠𝑙𝑖𝑑𝑒
|
||||
.endfn systemfive_error,globl,hidden
|
||||
#endif
|
||||
systemfive.errno:
|
||||
systemfive_errno:
|
||||
mov %eax,errno(%rip) # normalize to c library convention
|
||||
push $-1 # negative one is only error result
|
||||
pop %rax # the push pop is to save code size
|
||||
ret
|
||||
.endfn systemfive.errno,globl,hidden
|
||||
systemfive.enosys:
|
||||
.endfn systemfive_errno,globl,hidden
|
||||
systemfive_enosys:
|
||||
mov ENOSYS(%rip),%eax
|
||||
jmp systemfive.errno
|
||||
.endfn systemfive.enosys,globl,hidden
|
||||
jmp systemfive_errno
|
||||
.endfn systemfive_enosys,globl,hidden
|
||||
#if SupportsNetbsd()
|
||||
systemfive.netbsd:
|
||||
systemfive_netbsd:
|
||||
shr $4*13,%rax
|
||||
jmp systemfive.bsdscrub
|
||||
.endfn systemfive.openbsd,globl,hidden
|
||||
jmp systemfive_bsdscrub
|
||||
.endfn systemfive_openbsd,globl,hidden
|
||||
#endif
|
||||
#if SupportsOpenbsd()
|
||||
systemfive.openbsd:
|
||||
systemfive_openbsd:
|
||||
shr $4*10,%rax
|
||||
jmp systemfive.bsdscrub
|
||||
.endfn systemfive.openbsd,globl,hidden
|
||||
jmp systemfive_bsdscrub
|
||||
.endfn systemfive_openbsd,globl,hidden
|
||||
#endif
|
||||
#if SupportsFreebsd()
|
||||
systemfive.freebsd:
|
||||
systemfive_freebsd:
|
||||
shr $4*7,%rax
|
||||
movzwl %ax,%eax
|
||||
/ 𝑠𝑙𝑖𝑑𝑒
|
||||
.endfn systemfive.freebsd,globl,hidden
|
||||
// 𝑠𝑙𝑖𝑑𝑒
|
||||
.endfn systemfive_freebsd,globl,hidden
|
||||
#endif
|
||||
#if SupportsBsd()
|
||||
systemfive.bsdscrub:
|
||||
systemfive_bsdscrub:
|
||||
and $0xfff,%eax
|
||||
/ 𝑠𝑙𝑖𝑑𝑒
|
||||
.endfn systemfive.bsdscrub,globl,hidden
|
||||
systemfive.bsd:
|
||||
// 𝑠𝑙𝑖𝑑𝑒
|
||||
.endfn systemfive_bsdscrub,globl,hidden
|
||||
systemfive_bsd:
|
||||
cmp $0xfff,%ax
|
||||
je systemfive.enosys
|
||||
je systemfive_enosys
|
||||
mov %rcx,%r10 # note: we do not create a stack frame
|
||||
syscall # bsd will need arg on stack sometimes
|
||||
jc systemfive.errno # bsd sets carry flag if %rax is errno
|
||||
jc systemfive_errno # bsd sets carry flag if %rax is errno
|
||||
ret
|
||||
.endfn systemfive.bsd
|
||||
.endfn systemfive_bsd
|
||||
#endif
|
||||
#if SupportsXnu()
|
||||
systemfive.xnu:
|
||||
/ 0x?????????2153??? # how syscalls.sh encodes xnu ordinals
|
||||
/ │└┴┴┐
|
||||
/ │ ├┬┐
|
||||
/ 0x0000000002000153 # how xnu wants ordinals to be encoded
|
||||
systemfive_xnu:
|
||||
// 0x?????????2153??? # how syscalls.sh encodes xnu ordinals
|
||||
// │└┴┴┐
|
||||
// │ ├┬┐
|
||||
// 0x0000000002000153 # how xnu wants ordinals to be encoded
|
||||
mov %eax,%r11d
|
||||
and $0x0f000000,%r11d
|
||||
shl $8,%eax
|
||||
shr $20,%eax
|
||||
or %r11d,%eax
|
||||
jmp systemfive.bsd
|
||||
.endfn systemfive.xnu,globl,hidden
|
||||
jmp systemfive_bsd
|
||||
.endfn systemfive_xnu,globl,hidden
|
||||
#endif
|
||||
.previous
|
||||
|
||||
/ Initializes System Five system call support.
|
||||
/
|
||||
/ (1) Extracts parameters passed by kernel
|
||||
/ (2) Detects OS without issuing system calls
|
||||
/ (3) Unpacks magnums from libc/sysv/consts.sh
|
||||
/ (4) Replaces stack with one we control
|
||||
/
|
||||
/ @param %r15 is auxv
|
||||
/ @note OpenBSD devs: let us know if you start using auxv
|
||||
// Initializes System Five system call support.
|
||||
//
|
||||
// (1) Extracts parameters passed by kernel
|
||||
// (2) Detects OS without issuing system calls
|
||||
// (3) Unpacks magnums from libc/sysv/consts.sh
|
||||
// (4) Replaces stack with one we control
|
||||
//
|
||||
// @param %r15 is auxv
|
||||
// @note OpenBSD devs: let us know if you start using auxv
|
||||
.init.start 300,_init_systemfive
|
||||
push %rbx
|
||||
push %rsi
|
||||
#if SupportsMetal()
|
||||
testb $METAL,(%rdi) # @see ape/ape.S
|
||||
jnz systemfive.init.metal
|
||||
jnz _init_systemfive_metal
|
||||
#endif
|
||||
#if SupportsXnu()
|
||||
testb $XNU,(%rdi) # @see libc/crt/crt.S
|
||||
jnz systemfive.init.xnu
|
||||
jnz _init_systemfive_xnu
|
||||
#endif
|
||||
#if SupportsFreebsd()
|
||||
testb $FREEBSD,(%rdi) # @see libc/crt/crt.S
|
||||
jnz systemfive.init.freebsd
|
||||
jnz _init_systemfive_freebsd
|
||||
#endif
|
||||
#if SupportsWindows()
|
||||
testb $WINDOWS,(%rdi) # @see libc/runtime/winmain.c
|
||||
jnz systemfive.init.windows
|
||||
jnz _init_systemfive_windows
|
||||
#endif
|
||||
#if SupportsOpenbsd()
|
||||
cmpq $0,(%r15) # OpenBSD doesn't have auxv
|
||||
je systemfive.init.openbsd
|
||||
je _init_systemfive_openbsd
|
||||
#endif
|
||||
#if SupportsNetbsd()
|
||||
xor %eax,%eax
|
||||
0: cmpq $2014,(%r15,%rax,8) # NetBSD's distinctive AT_EXECFN
|
||||
je systemfive.init.netbsd
|
||||
je _init_systemfive_netbsd
|
||||
cmpq $0,(%r15,%rax,8)
|
||||
lea 2(%eax),%eax
|
||||
jnz 0b
|
||||
#endif
|
||||
#if SupportsLinux()
|
||||
systemfive.init.linux:
|
||||
pushb systemfive.linux-.Lanchorpoint
|
||||
_init_systemfive_linux:
|
||||
pushb systemfive_linux-.Lanchorpoint
|
||||
push $LINUX
|
||||
ezlea syscon.linux,si
|
||||
jmp systemfive.init.os
|
||||
ezlea syscon_linux,si
|
||||
jmp _init_systemfive_os
|
||||
#endif
|
||||
#if SupportsMetal()
|
||||
systemfive.init.metal:
|
||||
pushb systemfive.linux-.Lanchorpoint
|
||||
_init_systemfive_metal:
|
||||
pushb systemfive_linux-.Lanchorpoint
|
||||
push $METAL
|
||||
ezlea syscon.linux,si
|
||||
jmp systemfive.init.os
|
||||
ezlea syscon_linux,si
|
||||
jmp _init_systemfive_os
|
||||
#endif
|
||||
#if SupportsWindows()
|
||||
systemfive.init.windows:
|
||||
pushb systemfive.enosys-.Lanchorpoint
|
||||
_init_systemfive_windows:
|
||||
pushb systemfive_enosys-.Lanchorpoint
|
||||
push $WINDOWS
|
||||
ezlea syscon.windows,si
|
||||
jmp systemfive.init.os
|
||||
ezlea syscon_windows,si
|
||||
jmp _init_systemfive_os
|
||||
#endif
|
||||
#if SupportsFreebsd()
|
||||
systemfive.init.freebsd:
|
||||
pushb systemfive.freebsd-.Lanchorpoint
|
||||
_init_systemfive_freebsd:
|
||||
pushb systemfive_freebsd-.Lanchorpoint
|
||||
push $FREEBSD
|
||||
ezlea syscon.freebsd,si
|
||||
jmp systemfive.init.os
|
||||
ezlea syscon_freebsd,si
|
||||
jmp _init_systemfive_os
|
||||
#endif
|
||||
#if SupportsOpenbsd()
|
||||
systemfive.init.openbsd:
|
||||
pushb systemfive.openbsd-.Lanchorpoint
|
||||
_init_systemfive_openbsd:
|
||||
pushb systemfive_openbsd-.Lanchorpoint
|
||||
push $OPENBSD
|
||||
ezlea syscon.openbsd,si
|
||||
jmp systemfive.init.os
|
||||
ezlea syscon_openbsd,si
|
||||
jmp _init_systemfive_os
|
||||
#endif
|
||||
#if SupportsNetbsd()
|
||||
systemfive.init.netbsd:
|
||||
pushb systemfive.netbsd-.Lanchorpoint
|
||||
_init_systemfive_netbsd:
|
||||
pushb systemfive_netbsd-.Lanchorpoint
|
||||
push $NETBSD
|
||||
ezlea syscon.netbsd,si
|
||||
jmp systemfive.init.os
|
||||
ezlea syscon_netbsd,si
|
||||
jmp _init_systemfive_os
|
||||
#endif
|
||||
#if SupportsXnu()
|
||||
systemfive.init.xnu:
|
||||
pushb systemfive.xnu-.Lanchorpoint
|
||||
_init_systemfive_xnu:
|
||||
pushb systemfive_xnu-.Lanchorpoint
|
||||
push $XNU
|
||||
ezlea syscon.xnu,si
|
||||
/ 𝑠𝑙𝑖𝑑𝑒
|
||||
ezlea syscon_xnu,si
|
||||
// 𝑠𝑙𝑖𝑑𝑒
|
||||
#endif
|
||||
systemfive.init.os:
|
||||
_init_systemfive_os:
|
||||
ezlea .Lanchorpoint,cx
|
||||
pop %rax
|
||||
stosq #→ __hostos
|
||||
pop %rax
|
||||
add %rcx,%rax
|
||||
stosq #→ __systemfive
|
||||
/ 𝑠𝑙𝑖𝑑𝑒
|
||||
systemfive.init.magnums:
|
||||
// 𝑠𝑙𝑖𝑑𝑒
|
||||
_init_systemfive_magnums:
|
||||
push %rdi
|
||||
ezlea syscon.start,di
|
||||
ezlea syscon.end,bx
|
||||
ezlea syscon_start,di
|
||||
ezlea syscon_end,bx
|
||||
or $-1,%r9
|
||||
2: cmp %rbx,%rdi
|
||||
jnb 5f
|
||||
|
@ -309,12 +309,12 @@ systemfive.init.magnums:
|
|||
5: pop %rdi
|
||||
pop %rsi
|
||||
pop %rbx
|
||||
/ 𝑠𝑙𝑖𝑑𝑒
|
||||
// 𝑠𝑙𝑖𝑑𝑒
|
||||
#if SupportsSystemv() && !defined(TINY)
|
||||
systemfive.init.stack: # determinism ftw!
|
||||
_init_systemfive_stack: # determinism ftw!
|
||||
#if SupportsWindows()
|
||||
testb IsWindows() # already did this
|
||||
jnz systemfive.init.done
|
||||
jnz _init_systemfive_done
|
||||
#endif
|
||||
push %rdi
|
||||
push %rsi
|
||||
|
@ -364,11 +364,11 @@ systemfive.init.stack: # determinism ftw!
|
|||
push %rcx
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
/ 𝑠𝑙𝑖𝑑𝑒
|
||||
systemfive.init.syscall:
|
||||
// 𝑠𝑙𝑖𝑑𝑒
|
||||
_init_systemfive_syscall:
|
||||
mov __NR_msyscall,%eax # syscall origin protect
|
||||
test %eax,%eax # openbsd is pretty cool
|
||||
js systemfive.init.done
|
||||
js _init_systemfive_done
|
||||
push %rdi
|
||||
push %rsi
|
||||
.weak __privileged_addr
|
||||
|
@ -378,82 +378,82 @@ systemfive.init.syscall:
|
|||
syscall
|
||||
pop %rsi
|
||||
pop %rdi
|
||||
/ 𝑠𝑙𝑖𝑑𝑒
|
||||
// 𝑠𝑙𝑖𝑑𝑒
|
||||
#endif /* TINY */
|
||||
systemfive.init.done:
|
||||
_init_systemfive_done:
|
||||
nop
|
||||
.init.end 300,_init_systemfive,globl,hidden
|
||||
|
||||
/ Sections for varint encoded magic numbers.
|
||||
/
|
||||
/ These sections are all ordered by (group_name, constant_name).
|
||||
/ They're populated by modules simply referencing the symbols.
|
||||
/
|
||||
/ @see libc/sysv/consts.sh
|
||||
/ @see libc/sysv/consts/syscon.h
|
||||
// Sections for varint encoded magic numbers.
|
||||
//
|
||||
// These sections are all ordered by (group_name, constant_name).
|
||||
// They're populated by modules simply referencing the symbols.
|
||||
//
|
||||
// @see libc/sysv/consts.sh
|
||||
// @see libc/sysv/consts/syscon_h
|
||||
.section .piro.bss.sort.syscon.1,"aw",@nobits
|
||||
.align 8
|
||||
syscon.start:/*
|
||||
syscon_start:/*
|
||||
...decentralized quadwords...
|
||||
*/.previous
|
||||
.section .piro.bss.sort.syscon.3,"aw",@nobits
|
||||
syscon.end:
|
||||
syscon_end:
|
||||
.previous
|
||||
.type syscon.start,@object
|
||||
.type syscon.end,@object
|
||||
.globl syscon.start
|
||||
.globl syscon.end
|
||||
.type syscon_start,@object
|
||||
.type syscon_end,@object
|
||||
.globl syscon_start
|
||||
.globl syscon_end
|
||||
#if SupportsLinux()
|
||||
.section .sort.rodata.syscon.linux.1,"a",@progbits
|
||||
.align 1
|
||||
syscon.linux:/*
|
||||
syscon_linux:/*
|
||||
...decentralized leb128...
|
||||
*/.previous
|
||||
.type syscon.linux,@object
|
||||
.globl syscon.linux
|
||||
.type syscon_linux,@object
|
||||
.globl syscon_linux
|
||||
#endif
|
||||
#if SupportsXnu()
|
||||
.section .sort.rodata.syscon.xnu.1,"a",@progbits
|
||||
.align 1
|
||||
syscon.xnu:/*
|
||||
syscon_xnu:/*
|
||||
...decentralized leb128...
|
||||
*/.previous
|
||||
.type syscon.xnu,@object
|
||||
.globl syscon.xnu
|
||||
.type syscon_xnu,@object
|
||||
.globl syscon_xnu
|
||||
#endif
|
||||
#if SupportsFreebsd()
|
||||
.section .sort.rodata.syscon.freebsd.1,"a",@progbits
|
||||
.align 1
|
||||
syscon.freebsd:/*
|
||||
syscon_freebsd:/*
|
||||
...decentralized leb128...
|
||||
*/.previous
|
||||
.type syscon.freebsd,@object
|
||||
.globl syscon.freebsd
|
||||
.type syscon_freebsd,@object
|
||||
.globl syscon_freebsd
|
||||
#endif
|
||||
#if SupportsOpenbsd()
|
||||
.section .sort.rodata.syscon.openbsd.1,"a",@progbits
|
||||
.align 1
|
||||
syscon.openbsd:/*
|
||||
syscon_openbsd:/*
|
||||
...decentralized leb128...
|
||||
*/.previous
|
||||
.type syscon.openbsd,@object
|
||||
.globl syscon.openbsd
|
||||
.type syscon_openbsd,@object
|
||||
.globl syscon_openbsd
|
||||
#endif
|
||||
#if SupportsNetbsd()
|
||||
.section .sort.rodata.syscon.netbsd.1,"a",@progbits
|
||||
.align 1
|
||||
syscon.netbsd:/*
|
||||
syscon_netbsd:/*
|
||||
...decentralized leb128...
|
||||
*/.previous
|
||||
.type syscon.netbsd,@object
|
||||
.globl syscon.netbsd
|
||||
.type syscon_netbsd,@object
|
||||
.globl syscon_netbsd
|
||||
#endif
|
||||
#if SupportsWindows()
|
||||
.section .sort.rodata.syscon.windows.1,"a",@progbits
|
||||
.align 1
|
||||
syscon.windows:/*
|
||||
syscon_windows:/*
|
||||
...decentralized leb128...
|
||||
*/.previous
|
||||
.type syscon.windows,@object
|
||||
.globl syscon.windows
|
||||
.type syscon_windows,@object
|
||||
.globl syscon_windows
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue