2020-06-16 13:38:43 +00:00
|
|
|
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
|
2023-12-13 07:28:11 +00:00
|
|
|
│ vi: set noet ft=asm ts=8 sw=8 fenc=utf-8 :vi │
|
2020-06-15 14:18:57 +00:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2024-08-04 19:52:25 +00:00
|
|
|
#include "libc/macros.h"
|
2020-09-28 08:13:56 +00:00
|
|
|
#include "libc/sysv/consts/prot.h"
|
|
|
|
#include "libc/sysv/consts/map.h"
|
2024-07-20 02:36:25 +00:00
|
|
|
#include "libc/intrin/strace.h"
|
2022-05-12 13:43:59 +00:00
|
|
|
#include "libc/sysv/consts/map.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/dce.h"
|
2023-05-09 08:56:56 +00:00
|
|
|
#ifdef __x86_64__
|
2020-06-15 14:18:57 +00:00
|
|
|
.text.startup
|
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
// Cosmopolitan runtime.
|
|
|
|
//
|
|
|
|
// @param edi is argc
|
|
|
|
// @param rsi is argv
|
|
|
|
// @param rdx is environ
|
|
|
|
// @param rcx is auxv
|
|
|
|
// @noreturn
|
2021-02-01 11:33:13 +00:00
|
|
|
cosmo: push %rbp
|
2020-08-25 11:23:25 +00:00
|
|
|
mov %rsp,%rbp
|
|
|
|
mov %edi,%r12d
|
|
|
|
mov %rsi,%r13
|
|
|
|
mov %rdx,%r14
|
|
|
|
mov %rcx,%r15
|
2022-08-14 08:19:58 +00:00
|
|
|
|
2023-11-29 11:45:54 +00:00
|
|
|
#if SYSDEBUG
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
call __strace_init
|
|
|
|
mov %eax,%r12d
|
|
|
|
#endif /* SYSDEBUG */
|
2022-08-14 08:19:58 +00:00
|
|
|
|
2023-06-15 20:50:42 +00:00
|
|
|
#ifndef NOX87
|
2024-07-22 03:45:27 +00:00
|
|
|
// Windows always initializes FPU to double precision.
|
2022-11-02 09:17:14 +00:00
|
|
|
// WSL breaks Linux ABI by initializing FPU to double precision.
|
|
|
|
// This code makes long double long again.
|
|
|
|
//
|
|
|
|
// @see System V Application Binary Interface NexGen32e Architecture
|
|
|
|
// Processor Supplement, Version 1.0, December 5th, 2018
|
|
|
|
// Section 3.4.1: Initial Stack and Register State
|
|
|
|
fldcw 1f(%rip)
|
|
|
|
.rodata
|
2023-05-02 02:43:59 +00:00
|
|
|
.balign 2
|
2022-11-02 09:17:14 +00:00
|
|
|
// 8087 FPU Control Word
|
|
|
|
// IM: Invalid Operation ───────────────┐
|
|
|
|
// DM: Denormal Operand ───────────────┐│
|
|
|
|
// ZM: Zero Divide ───────────────────┐││
|
|
|
|
// OM: Overflow ─────────────────────┐│││
|
|
|
|
// UM: Underflow ───────────────────┐││││
|
|
|
|
// PM: Precision ──────────────────┐│││││
|
|
|
|
// PC: Precision Control ───────┐ ││││││
|
|
|
|
// {float,∅,double,long double}│ ││││││
|
|
|
|
// RC: Rounding Control ──────┐ │ ││││││
|
|
|
|
// {even, →-∞, →+∞, →0} │┌┤ ││││││
|
|
|
|
// ┌┤││ ││││││
|
|
|
|
// d││││rr││││││
|
|
|
|
1: .short 0b00000000000000000001101111111
|
|
|
|
.previous
|
2023-06-15 20:50:42 +00:00
|
|
|
#endif
|
2022-11-02 09:17:14 +00:00
|
|
|
|
2021-02-01 11:33:13 +00:00
|
|
|
#ifdef __FAST_MATH__
|
2021-02-08 12:04:42 +00:00
|
|
|
push %rax
|
|
|
|
stmxcsr (%rsp)
|
2024-04-30 20:32:23 +00:00
|
|
|
//
|
|
|
|
// Enable hardware optimizations in violation of the IEEE standard.
|
|
|
|
//
|
|
|
|
// - 0x0040 enables "DAZ: Denormals Are Zeros" in MXCSR. This causes the
|
|
|
|
// processor to turn denormal inputs into zero, before computing them.
|
|
|
|
// See Intel Manual Vol. 1 §10.2.3.4
|
|
|
|
//
|
|
|
|
// - 0x8000 enables "FTZ: Flush To Zero" in MXCSR. This means a floating
|
|
|
|
// point operation that results in underflow will be set to zero, with
|
|
|
|
// the same sign, rather than producing a denormalized output. It will
|
|
|
|
// happen only if underflow trapping hasnt been enabled. See the Intel
|
|
|
|
// Manual Vol. 1 §10.2.3.3.
|
|
|
|
//
|
2021-02-08 12:04:42 +00:00
|
|
|
orl $0x8040,(%rsp)
|
|
|
|
ldmxcsr (%rsp)
|
|
|
|
pop %rax
|
2021-02-01 11:33:13 +00:00
|
|
|
#endif
|
2022-08-14 08:19:58 +00:00
|
|
|
|
|
|
|
// run assembly init
|
2021-02-01 11:33:13 +00:00
|
|
|
call _init
|
2022-08-14 08:19:58 +00:00
|
|
|
|
|
|
|
// call constructors
|
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.
2024-02-20 19:12:09 +00:00
|
|
|
.weak __init_array_end
|
|
|
|
.weak __init_array_start
|
|
|
|
mov $__init_array_start,%eax
|
|
|
|
1: cmp $__init_array_end,%eax
|
2021-02-08 12:04:42 +00:00
|
|
|
je 2f
|
|
|
|
push %rax
|
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.
2024-02-20 19:12:09 +00:00
|
|
|
push %rax
|
2021-02-08 12:04:42 +00:00
|
|
|
call .Largs
|
|
|
|
call *(%rax)
|
|
|
|
pop %rax
|
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.
2024-02-20 19:12:09 +00:00
|
|
|
pop %rax
|
|
|
|
add $8,%eax
|
2021-02-08 12:04:42 +00:00
|
|
|
jmp 1b
|
2022-08-14 08:19:58 +00:00
|
|
|
|
|
|
|
// call main()
|
2021-09-28 05:58:51 +00:00
|
|
|
2: call .Largs
|
2020-06-15 14:18:57 +00:00
|
|
|
.weak main
|
2020-11-09 23:41:11 +00:00
|
|
|
call main
|
|
|
|
xchg %eax,%edi
|
2020-06-15 14:18:57 +00:00
|
|
|
call exit
|
2022-08-14 08:19:58 +00:00
|
|
|
|
2021-02-08 12:04:42 +00:00
|
|
|
.Largs: mov %r12d,%edi
|
|
|
|
mov %r13,%rsi
|
|
|
|
mov %r14,%rdx
|
|
|
|
mov %r15,%rcx
|
|
|
|
ret
|
|
|
|
.endfn cosmo,weak
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-07-19 05:26:11 +00:00
|
|
|
// Enables Thread Local Storage.
|
2022-07-10 15:27:50 +00:00
|
|
|
.init.start 304,_init_tls
|
|
|
|
push %rdi
|
|
|
|
push %rsi
|
|
|
|
call __enable_tls
|
|
|
|
pop %rsi
|
|
|
|
pop %rdi
|
2022-07-19 05:26:11 +00:00
|
|
|
.init.end 304,_init_tls
|
2022-07-10 15:27:50 +00:00
|
|
|
|
2023-06-06 06:35:31 +00:00
|
|
|
#ifdef FTRACE
|
2022-06-13 05:20:59 +00:00
|
|
|
.init.start 306,_init_ftrace
|
2022-05-21 01:51:41 +00:00
|
|
|
push %rdi
|
|
|
|
push %rsi
|
2022-06-13 05:20:59 +00:00
|
|
|
call ftrace_init
|
|
|
|
mov %eax,%r12d
|
2022-05-21 01:51:41 +00:00
|
|
|
pop %rsi
|
|
|
|
pop %rdi
|
2022-06-13 05:20:59 +00:00
|
|
|
.init.end 306,_init_ftrace
|
2022-05-21 01:51:41 +00:00
|
|
|
#endif
|
|
|
|
|
2023-05-09 08:56:56 +00:00
|
|
|
#endif /* __x86_64__ */
|