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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//! Returning to the caller before teardown: the fork the CLI runs under.
//!
//! Unmapping several hundred megabytes of input and freeing a link's heap
//! takes tens of milliseconds, all of it after the image is already renamed
//! into place. A build system waiting on the linker is waiting for nothing
//! during that time. mold and wild both answer this by forking at startup:
//! the parent waits, the child links, and the moment the image is published
//! the child tells the parent to exit. The child then finishes its teardown
//! as an orphan, off the caller's clock.
//!
//! This module is that answer, and the only place the linker calls `fork`.
//! [`fork_child`] runs before any thread is spawned -- forking a threaded
//! process would hand the child a single thread and whatever locks the others
//! held -- and the caller-visible contract is exactly the sequential one:
//! the exit code is the link's, stderr arrives on the caller's stderr, and
//! the output file is complete and renamed into place before the parent
//! returns success. `--no-fork` skips all of it, which is also the right
//! mode for measuring the linker's full cost rather than its latency.
//!
//! The library never arms this on its own: [`published`] is a no-op unless
//! [`fork_child`] stored the pipe, so embedders and tests see plain
//! function-call behaviour.
use ;
/// The write end of the pipe the forked child reports through, or -1.
///
/// An `AtomicI32` rather than a `OnceLock`: the value is claimed back (and
/// reset to -1) by the one [`published`] call, so a second publish -- or one
/// in a process that never forked -- does nothing.
static PUBLISH_FD: AtomicI32 = new;
/// Forks; the parent waits for the link's outcome, the child returns and
/// links.
///
/// In the child this returns and the caller carries on into the link, with
/// the pipe stored for [`published`]. In the parent this never returns: it
/// blocks until the child either reports the image published (exit 0, while
/// the child tears down unobserved) or exits without reporting, in which
/// case the child's own exit status is repeated. If the pipe or the fork
/// cannot be had, the caller simply proceeds unforked; detaching is a
/// latency improvement, never a requirement.
/// Reports the output image renamed into place, releasing the waiting
/// parent. A no-op in a process [`fork_child`] did not fork.
/// The parent's whole life: wait on the pipe, then exit.
///
/// One byte means the image is published and the exit code is 0. End of
/// file means the child exited without publishing -- a failed link, or a
/// crash -- and its status is collected and repeated, including the
/// conventional 128-plus-signal spelling for a killed child.
!
/// The calling thread's `errno`.