1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! Shims for places where cosmopolitan's libc and Rust's std disagree.
//!
//! Link this into any cosmo target and the fixes apply themselves -- each is a
//! `#[no_mangle]` definition that the linker prefers over the one in
//! libcosmo.a, because an archive member is only pulled in to satisfy a symbol
//! that is still undefined.
// no_std so one shim serves both worlds: cosmo-ld wraps 123 libc symbols on
// every cosmo link, so even a no_std binary has to supply the translators, and
// nothing here needs more than core.
use ;
// Stage-2 normalization (docs/DESIGN.md): translate OS constants at the libc
// boundary so a binary compiled with Linux numbering runs on the host cosmo
// landed it on. `gen` is generated by tools/gen-shim.py; `shim` holds the
// hand-written __wrap_* functions cosmo-ld routes calls into.
/// Keep the two plain overrides alive through `lto = "fat"`.
///
/// Nothing in the Rust call graph references them -- std reaches both through
/// an `extern "C"` declaration LTO cannot see -- so fat LTO internalizes them
/// away and the libc bugs they exist to fix come back, silently and only in
/// release builds. (The `__wrap_*` functions do not need this: `--wrap` makes
/// the linker demand them, so losing one is a link error rather than a quiet
/// regression.) `#[used]` emits `llvm.used`, which LTO honours.
static KEEP_SHIMS: = ;
extern "C"
/// POSIX XSI `strerror_r`, which is what Rust's std actually calls.
///
/// std links this symbol and requires the XSI contract: return an `int`, zero
/// on success. Cosmopolitan declares it `char *__xpg_strerror_r(int, char *,
/// size_t)` -- the GNU contract -- so std reinterprets the returned pointer as
/// an int, finds the sign bit set often enough to matter, and panics with
/// "strerror_r failure". The effect is that formatting *any* io::Error blows
/// up, which takes out ordinary error paths well before anything exotic.
///
/// Copying out of cosmo's `strerror` and returning 0 restores the contract.
pub unsafe extern "C"
extern "C"
/// `waitid`, which cosmopolitan does not provide but std's `Process::wait`
/// references unconditionally (its pidfd path, `P_PIDFD`, is compiled in for
/// every linux target). Linking is the requirement; the semantics only matter
/// if the function is ever reached, and on cosmo std never creates a pidfd, so
/// it is not. Implemented over `waitpid` for the `P_PID`/`P_ALL` shapes anyway:
/// `siginfo_t.si_status` receives the raw wait status and `si_pid` the pid, at
/// the Linux offsets std reads (`si_pid` at byte 16, `si_status` at 24).
pub unsafe extern "C"
/// Built standalone by cargo-cosmo (tools/cargo-cosmo, `ensure_inject`): a
/// staticlib needs a panic handler of its own. The shim never panics on
/// purpose; if it does, abort rather than unwind through C.
!