Skip to main content

rucc_driver/
link.rs

1//! Finding a linker and telling it what to link.
2//!
3//! Design: `spec/04-driver-and-cli.md` section 4.9. There is no linker of our own before 1.0, so
4//! this finds one on the machine and builds the command line it wants.
5//!
6//! The linker is invoked directly rather than through the system compiler driver. Going through
7//! `cc` would be shorter to write and would borrow that compiler's idea of where everything is,
8//! and it would also mean this compiler cannot link on a machine that has no other compiler on
9//! it, which is most of the machines a compiler ends up on. It would also make `-###` output a
10//! line that does not say what happens, since the interesting half would be inside the program
11//! being spawned.
12//!
13//! # What is not decided here
14//!
15//! The startup files and the library directories are looked for rather than configured, for the
16//! same reason `library` looks for the headers: gcc settles this when it is built because a gcc
17//! is built for the machine it will run on, and this is one binary that runs wherever it is
18//! copied. So the shape of the answer is a list of candidates per platform of which the ones
19//! that exist are taken, and a cross build says where the rest is with `--sysroot`.
20//!
21//! # The compiler's own runtime
22//!
23//! `crtbegin`, `crtend` and the runtime libraries are found the same way, on the machine rather
24//! than by configuration. Ours is `librucc_builtins.a`, looked for beside the compiler, and the
25//! machine's `libgcc` goes on after it for the parts we have not written, which today is the
26//! unwinder and its personality routine. The C library goes in front of both, so that on a target
27//! that has one its `memcpy` is the one that answers rather than ours. `-fno-builtins-lib` leaves
28//! ours off, for somebody who wants libgcc to answer for everything.
29//!
30//! On a static link the three archives go inside `--start-group`, because `libc.a` refers to the
31//! unwinder and the unwinder refers back to `libc.a`, and a linker walking a list once resolves
32//! whichever of the two it reaches first and leaves the other undefined. That circularity is the
33//! whole reason `-static` failed before this, and it is issue #277.
34//!
35//! # Linking for a machine that is not this one
36//!
37//! Everything above describes a link against the machine running the compiler, and it is what runs
38//! when the target is that machine. A target that is not is a different problem: there is no
39//! `crt1.o` for it in `/usr/lib`, the `libc.so` there is the wrong architecture, and a line built
40//! out of what is lying around either fails at the first input or, worse, links. So a cross link
41//! does not look at this machine at all. It is built by [`rucc_sysroot::argv`] out of the target
42//! and a sysroot under the cache directory, and `spec/cross-compile/11-linking.md` section 11.3 is
43//! the design. [`cross_sysroot`] is the one place that decides which of the two it is.
44//!
45//! Two conditions keep that out of the way of everything that works today. The target has to differ
46//! from the host, and `--sysroot` must not have been given: somebody who assembled a tree and named
47//! it is asking for the line above with their own root in front of every path, which is what a
48//! cross compile with a real distribution tree in it has always been.
49//!
50//! That second condition is also the escape hatch for a machine which has a distribution's own cross
51//! files installed, where `/usr/lib/aarch64-linux-gnu` really does hold an AArch64 `crt1.o`.
52//! `--sysroot=/` takes the line above, and then every directory it decides is that machine's again.
53//!
54//! # What is not here yet
55//!
56//! Darwin, and Windows in Microsoft's ABI. `ld64` wants a platform version load command and a
57//! different set of default libraries, and `lld-link` wants a `/`-style command line and an import
58//! library set out of an SDK nobody may redistribute. Each arrives with the target that needs it,
59//! and a cross link to either is refused by name rather than approximated. A mingw-w64 target does
60//! have a line, because PE in that environment is written in the GNU style and the import libraries
61//! for it are ours to produce.
62//!
63//! The headers are the other half of a cross compile and [`crate::library::header_dirs`] is where
64//! they are decided. It asks [`cross_sysroot`] the same question this file asks it, which is the
65//! point: a compile that took its libc from the sysroot and its declarations from this machine would
66//! be wrong in the quietest way available, and one function answering for both is what stops that
67//! being possible.
68
69use std::ffi::OsString;
70use std::fs;
71use std::path::{Path, PathBuf};
72use std::process::Command;
73
74use rucc_sysroot::layout::{Kernel, Sysroot};
75use rucc_sysroot::{LinkMode, argv};
76use rucc_target::{Arch, Env, Os, Triple};
77use rucc_tuple::TargetTuple;
78
79/// What the command line said about linking.
80///
81/// Kept apart from `Options` because none of it reaches the compilation. A flag here changes what
82/// the linker is told and changes nothing about the object files handed to it, which is why `-lm`
83/// on a `-c` line is a note rather than an error.
84#[derive(Debug, Default, Clone, PartialEq, Eq)]
85pub struct LinkOptions {
86    /// `-fuse-ld=<name>`, which names a linker rather than a path to one.
87    pub use_ld: Option<String>,
88    /// `-L<dir>`, in order, because the linker takes the first library it finds.
89    pub search: Vec<PathBuf>,
90    /// `-B<prefix>`, which is where to look for the linker before looking on the path.
91    pub prefixes: Vec<PathBuf>,
92    /// `--sysroot=<dir>`, which prefixes the directories this looks in.
93    pub sysroot: Option<PathBuf>,
94    /// Where the generated sysroots are, which is [`crate::cache::dir`] on a real command line.
95    ///
96    /// [`None`] is a caller that was not given one, which outside a test is nothing, and then there
97    /// is no cross link line and a foreign target is refused the way it was before there was one.
98    /// It is a field rather than a call inside this module because a link line that read the
99    /// environment could only be tested on a machine whose environment said the right thing.
100    pub cache: Option<PathBuf>,
101    /// `-static`.
102    pub is_static: bool,
103    /// `-shared`.
104    pub shared: bool,
105    /// `-pie` or `-no-pie`, and the platform's default when neither was written.
106    pub pie: Option<bool>,
107    /// `-nostdlib`, which is `-nostartfiles` and `-nodefaultlibs` together.
108    pub no_stdlib: bool,
109    /// `-nostartfiles`.
110    pub no_startfiles: bool,
111    /// `-nodefaultlibs`.
112    pub no_defaultlibs: bool,
113    /// `-rdynamic`, which puts every symbol in the dynamic table so a program can look itself up.
114    pub export_dynamic: bool,
115    /// `-s`, which drops the symbol table.
116    pub strip: bool,
117    /// `-fno-builtins-lib`, which leaves our own runtime off the line so that the machine's
118    /// libgcc answers for everything instead.
119    pub no_builtins_lib: bool,
120    /// The whole ten field target when `--target=` spelled one, which is where a pinned libc
121    /// release is.
122    ///
123    /// [`None`] is a command line that named no target at all, and then there is nothing pinned and
124    /// this machine is the target. A `Triple` has room for an architecture, an OS and an
125    /// environment and nowhere to put a release, so the release arrives here instead of there, and
126    /// [`cross_sysroot`] reads it for both of the things it decides: whether this is a cross link
127    /// and which directory under the cache it is against.
128    pub pinned: Option<TargetTuple>,
129    /// `-pg`, which changes the link as well as the code.
130    ///
131    /// The counts a profiled program keeps have to be started before `main` runs and written out
132    /// after it returns, and what does both is a start file of its own. So a build that compiles
133    /// with the flag and links without it produces a program that calls the hook on every function
134    /// and never writes a profile.
135    pub profile: bool,
136}
137
138impl LinkOptions {
139    /// Whether the startup files go on the line.
140    fn wants_startfiles(&self) -> bool {
141        !self.no_stdlib && !self.no_startfiles
142    }
143
144    /// Whether the library the program was written against goes on the line.
145    fn wants_defaultlibs(&self) -> bool {
146        !self.no_stdlib && !self.no_defaultlibs
147    }
148
149    /// Whether the compiler's own runtime goes on the line.
150    ///
151    /// The same switch as the C library, because `-nodefaultlibs` in GCC means the compiler's
152    /// runtime too, and a link that keeps `libgcc` while dropping `libc` is not a thing anyone
153    /// asks for on purpose.
154    fn wants_runtime(&self) -> bool {
155        !self.no_stdlib && !self.no_defaultlibs
156    }
157}
158
159/// One item on the link line, in the order it was written, because link order is semantic.
160///
161/// A library named before the object that needs it is not found on a static link, which is the
162/// oldest surprise in the toolchain and the reason this is one ordered list rather than a list of
163/// files and a list of libraries.
164#[derive(Debug, Clone, PartialEq, Eq)]
165pub enum Item {
166    /// A file: an object this compilation produced, or one named on the command line.
167    File(String),
168    /// `-l<name>`, which the linker resolves against its search path.
169    Library(String),
170    /// One word from `-Wl,` or `-Xlinker`, handed to the linker where the user wrote it.
171    ///
172    /// Here rather than in a list of its own because a great many of the linker's options are a
173    /// bracket around the files after them, and an option moved away from what it brackets means
174    /// something else or nothing at all. `--whole-archive` says that every member of every archive
175    /// named after it goes in whether anything referenced it or not, `--start-group` says that the
176    /// archives after it are searched again until nothing more comes out, and `-Bstatic` says which
177    /// half of a library that ships both is wanted. Collecting them and appending them to the end
178    /// leaves each of those pointing at nothing.
179    Linker(String),
180}
181
182impl std::fmt::Display for Item {
183    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
184        match self {
185            Item::File(path) => f.write_str(path),
186            Item::Library(name) => write!(f, "-l{name}"),
187            Item::Linker(arg) => write!(f, "-Wl,{arg}"),
188        }
189    }
190}
191
192/// Why a link could not be run.
193#[derive(Debug, Clone, PartialEq, Eq)]
194pub enum Error {
195    /// No linker was found, after looking everywhere there was to look.
196    NoLinker {
197        /// The names that were tried, in the order they were tried.
198        tried: Vec<String>,
199    },
200    /// `-fuse-ld=` named one that is not on this machine.
201    Named {
202        /// What it named.
203        name: String,
204    },
205    /// A target this does not know how to build a link line for.
206    Target {
207        /// The triple that was asked for.
208        triple: String,
209    },
210    /// A cross link this scheme cannot produce, which [`rucc_sysroot::argv`] has explained.
211    ///
212    /// The reason is carried as a sentence rather than as a variant per cause, because the causes
213    /// live in `rucc-sysroot` and a second enumeration here would be a second thing to keep in step
214    /// with them. What this adds is that the sentence came from a link rather than from a
215    /// compilation.
216    Cross {
217        /// Why, in full, ready to print.
218        why: String,
219    },
220    /// The sysroot a cross link needs is not on this machine.
221    Sysroot {
222        /// The target that was asked for.
223        target: String,
224        /// Where its sysroot would be.
225        dir: String,
226        /// Whether this release pins an artifact for that target, which decides whether the message
227        /// can name a command that would fix it.
228        pinned: bool,
229    },
230    /// The linker was found and cannot do this target's link.
231    ///
232    /// Separate from [`Error::NoLinker`] because the linker is there and runs, and separate from
233    /// [`Error::Refused`] because the refusal is ours rather than its own: this is the case the
234    /// linker would not complain about at all.
235    TooOld {
236        /// What it was found as, which is what to look for when replacing it.
237        name: String,
238        /// The major version it reported.
239        found: u32,
240        /// The target whose link it cannot do.
241        target: String,
242    },
243    /// The linker was found and could not be started.
244    Spawn {
245        /// Where it was.
246        path: String,
247        /// What the operating system said.
248        why: String,
249    },
250    /// The linker ran and said no.
251    Refused {
252        /// What it exited with, or a description when it was killed instead.
253        status: String,
254    },
255}
256
257impl std::fmt::Display for Error {
258    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
259        match self {
260            Error::NoLinker { tried } => {
261                write!(f, "no linker was found; tried {}", tried.join(", "))
262            }
263            Error::Named { name } => {
264                write!(f, "-fuse-ld={name} asks for a linker that is not on this machine")
265            }
266            Error::Target { triple } => {
267                write!(f, "there is no link line for {triple} in this compiler yet")
268            }
269            Error::Cross { why } => f.write_str(why),
270            // Two sentences and the second one changes, because a person whose link just failed
271            // wants the command that fixes it and there is only a command to name when this release
272            // pins an artifact for that target. Section 13.8's rule is that a compile which is
273            // missing a sysroot says what to run rather than running it, and this is where it says
274            // it.
275            Error::Sysroot { target, dir, pinned: true } => write!(
276                f,
277                "there is no sysroot for {target} at {dir}, so there is nothing to link it \
278                 against. `rucc --fetch {target}` gets the one this release pins, or pass \
279                 --sysroot=<dir> to name a tree you have already"
280            ),
281            Error::Sysroot { target, dir, pinned: false } => write!(
282                f,
283                "there is no sysroot for {target} at {dir}, so there is nothing to link it \
284                 against, and this release pins none for it to fetch. Pass --sysroot=<dir> to name \
285                 a tree you have already, or see spec/cross-compile/13-distribution.md section \
286                 13.2 for the cache that will hold one"
287            ),
288            // The whole message, because the person reading it has a linker that works, a link that
289            // succeeded on their last try, and no reason to suspect the thing that is wrong.
290            Error::TooOld { name, found, target } => write!(
291                f,
292                "{name} is lld {found} and cannot link for {target}. mingw-w64 writes a few hundred \
293                 of its aliases, `_crt_atexit == atexit` among them, as IMPORT_NAME_EXPORTAS \
294                 records in its import libraries, which lld learned to read in {LLD_EXPORTAS}. An \
295                 older one neither reads them nor says so: it writes an import by ordinal zero, the \
296                 link succeeds, and the program dies at startup. Install lld {LLD_EXPORTAS} or \
297                 newer, or name one with -fuse-ld="
298            ),
299            Error::Spawn { path, why } => write!(f, "could not run the linker at {path}: {why}"),
300            Error::Refused { status } => write!(f, "the linker {status}"),
301        }
302    }
303}
304
305impl std::error::Error for Error {}
306
307/// A linker, found.
308#[derive(Debug, Clone, PartialEq, Eq)]
309pub struct Linker {
310    /// The name it is known by, which is what `--print-config` reports.
311    pub name: String,
312    /// Where it is, which is what gets spawned.
313    pub path: PathBuf,
314}
315
316/// The names to look for, in the order section 4.9 gives.
317///
318/// `mold` first because it is dramatically faster, and a compiler that is twice the speed of
319/// another one while the link takes twelve seconds has not helped anybody. Then `lld`, then the
320/// platform's own. Each is looked for under both the bare name and the `ld.` prefix, because a
321/// distribution installs `mold` under its own name and `ld.mold` for exactly this lookup.
322#[must_use]
323pub fn order(target: Triple, opts: &LinkOptions) -> Vec<String> {
324    if let Some(named) = &opts.use_ld {
325        // A name rather than a path, so `-fuse-ld=mold` finds a `mold` that is not `ld.mold`.
326        return vec![format!("ld.{named}"), named.clone()];
327    }
328    if cross_sysroot(target, opts).is_some() {
329        return cross_order(target);
330    }
331    match target.os {
332        Os::Windows => vec!["lld-link".to_owned(), "link.exe".to_owned()],
333        _ => vec![
334            "ld.mold".to_owned(),
335            "mold".to_owned(),
336            "ld.lld".to_owned(),
337            "lld".to_owned(),
338            "ld".to_owned(),
339        ],
340    }
341}
342
343/// The names to look for when the target is not this machine.
344///
345/// A shorter list than the one above and a different one, because most of that list cannot do this.
346/// `spec/cross-compile/11-linking.md` section 11.2 settles it: `ld.lld` is the ELF cross linker,
347/// since one binary of it links for every architecture it was built with and that is all of them.
348/// mold is off the list because it links for the host and `wild` likewise, which is why section 11.2
349/// has them as `-fuse-ld=` choices for a native link rather than as defaults. The platform's own
350/// `ld` is off it for the same reason: a distribution's `/usr/bin/ld` is built for one architecture,
351/// and `-fuse-ld=` is still there for somebody whose is not.
352///
353/// A cross binutils under its prefixed name is last, because a machine that has
354/// `aarch64-linux-gnu-ld` installed has it on purpose. The prefix is a distribution convention and
355/// there are two of them: a Linux target is filed under its multiarch name and a mingw-w64 one under
356/// `<arch>-w64-mingw32`, which is what every distribution's mingw packages install. `ld.lld` is the
357/// same binary for both, because its MinGW mode is a mode of the one linker rather than a second one.
358fn cross_order(target: Triple) -> Vec<String> {
359    let mut names = vec!["ld.lld".to_owned(), "lld".to_owned()];
360    match (target.os, target.env) {
361        (Os::Linux, _) => names.push(format!("{}-ld", multiarch(target))),
362        (Os::Windows, Env::Gnu) => names.push(format!("{}-w64-mingw32-ld", target.arch.as_str())),
363        _ => {}
364    }
365    names
366}
367
368/// The sysroot a cross link would use, or [`None`] for a link against this machine.
369///
370/// The one place the two paths are told apart, so that the linker that is looked for and the line it
371/// is handed cannot disagree about which kind of link this is.
372///
373/// Three conditions, and two of them are about leaving working configurations alone. A target that
374/// is this machine is linked against this machine, which is what every native compile has always
375/// done and what the directories under `/usr/lib` are for. A `--sysroot` the user wrote is taken as
376/// the root of a tree they assembled, and the line above prefixes every path it decides with it,
377/// which is what cross compiling against a real distribution tree has always meant here. The third
378/// is that there has to be a cache directory to look in, which on a real command line there always
379/// is.
380///
381/// An unknown host counts as different from every target. A machine this compiler cannot name is a
382/// machine whose `/usr/lib` it should not be guessing at.
383///
384/// # A pinned release is a cross compile
385///
386/// The first of those three conditions is about the machine and not about the triple, and a target
387/// that names a libc release is not this machine even when it is this architecture. Somebody on a
388/// 2.44 box writing `--target=x86_64-linux-gnu.2.28` is asking for a binary that runs on a 2.28
389/// machine, and handing them their own headers and their own libc gives them a binary that does not.
390/// So the condition is the triple being the host *and* no release named, and what it costs is that a
391/// pin equal to this machine's own release also stops using this machine's libc. That is not a loss:
392/// the two should be the same text, and if they are not then this machine's copy is patched and the
393/// bundled tree is the one the pin asked for. tamnd/rucc#956.
394#[must_use]
395pub fn cross_sysroot(target: Triple, opts: &LinkOptions) -> Option<Sysroot> {
396    cross_for(target, opts, Triple::host())
397}
398
399/// The same answer with the host as a parameter, so that both branches are testable on one machine.
400fn cross_for(target: Triple, opts: &LinkOptions, host: Option<Triple>) -> Option<Sysroot> {
401    if opts.sysroot.is_some() {
402        return None;
403    }
404    let tuple = target_tuple(target, opts);
405    if host == Some(target) && tuple.env_version().is_none() {
406        return None;
407    }
408    let cache = opts.cache.as_deref()?;
409    Some(Sysroot::in_cache(cache, tuple))
410}
411
412/// The target as the model that has room for a release, which is what names the cache directory.
413///
414/// The pinned spelling when there is one, because `x86_64-linux-gnu` and `x86_64-linux-gnu.2.28` are
415/// two sysroots and not one: the release is in the tuple for the reason
416/// `spec/cross-compile/03-target-model.md` section 3.2 admits a field at all, which is that it
417/// changes what is compiled. A command line that named no target, or one whose spelling the ten
418/// field parser did not take, falls back to what the three field one did.
419fn target_tuple(target: Triple, opts: &LinkOptions) -> TargetTuple {
420    opts.pinned.unwrap_or_else(|| target.tuple())
421}
422
423/// The kernel headers that go with [`cross_sysroot`], for the targets that have any.
424///
425/// The same three conditions, asked through the same function, because the two halves of one
426/// target's system headers have to be decided together or a compile could read glibc's `sys/stat.h`
427/// against this machine's `asm/stat.h`. A `None` here on a Linux target where the sysroot is `Some`
428/// means only one thing, which is that the cache has no kernel tree for that architecture, and the
429/// directory is still named for the reason [`crate::library::header_dirs`] gives.
430///
431/// Not under the sysroot, because `linux/` and `asm-generic/` are the same nine megabytes for every
432/// target that shares an architecture, and a copy per target is eight copies of one thing.
433#[must_use]
434pub fn cross_kernel(target: Triple, opts: &LinkOptions) -> Option<Kernel> {
435    kernel_for(target, opts, Triple::host())
436}
437
438/// The same answer with the host as a parameter, for the same reason as [`cross_for`].
439fn kernel_for(target: Triple, opts: &LinkOptions, host: Option<Triple>) -> Option<Kernel> {
440    cross_for(target, opts, host)?;
441    Kernel::for_target(opts.cache.as_deref()?, target.tuple())
442}
443
444/// How the result is linked, as the five cases a sysroot link line is written over.
445///
446/// Four booleans reach here and five cases leave, because static and position independent are not
447/// independent of each other and the start file differs in four of the five. The default for `pie`
448/// is the one the native line above uses, so that a command line that says neither gets the same
449/// answer whichever path it takes.
450fn mode(opts: &LinkOptions) -> LinkMode {
451    let pie = opts.pie.unwrap_or(!opts.is_static && !opts.shared);
452    if opts.shared {
453        LinkMode::Shared
454    } else if opts.is_static {
455        if pie { LinkMode::StaticPie } else { LinkMode::Static }
456    } else if pie {
457        LinkMode::Dynamic
458    } else {
459        LinkMode::DynamicNoPie
460    }
461}
462
463/// The line for a machine that is not this one, from the target and the sysroot and nothing else.
464///
465/// Everything this knows is already in `opts`, and all it does is say it in the shape
466/// [`rucc_sysroot::argv`] is written over. There is deliberately no decision here: a second place
467/// that decided what goes on a cross link line would be a second place to get it wrong, and the
468/// recorded lines under `tests/link-lines` would stop describing what this compiler does.
469fn cross_line(
470    target: Triple,
471    opts: &LinkOptions,
472    items: &[Item],
473    output: &str,
474    sysroot: &Sysroot,
475) -> Result<Vec<String>, Error> {
476    if opts.profile {
477        // `gcrt1.o` is a compiled object out of the C library's own sources, and a generated sysroot
478        // has the names a libc exports rather than the bodies behind them. Said here rather than
479        // left to the linker, because what the linker would say is that `main` is undefined.
480        return Err(Error::Cross {
481            why: format!(
482                "-pg needs gcrt1.o, or gcrt2.o on Windows, the startup file that starts and stops \
483                 the counting, and a generated sysroot for {target} does not have one. Profile on \
484                 the host, or pass --sysroot=<dir> naming a tree that has it"
485            ),
486        });
487    }
488    let inputs: Vec<argv::Item> = items
489        .iter()
490        .map(|item| match item {
491            Item::File(path) => argv::Item::File(PathBuf::from(path)),
492            Item::Library(name) => argv::Item::Library(name.clone()),
493            Item::Linker(arg) => argv::Item::Linker(arg.clone()),
494        })
495        .collect();
496    let output = PathBuf::from(output);
497    // Ours, from beside the compiler, because that is where `cargo xtask builtins` writes it and a
498    // fetched sysroot will never hold it. The cross line used to name it inside the sysroot, which
499    // is a file nothing puts there, so every cross link either failed at the linker or quietly ran
500    // against somebody else's `libgcc` copied in under the name. tamnd/rucc#1514.
501    let ours = builtins_archive(target, &opts.prefixes);
502    if ours.is_none() && opts.wants_runtime() && !opts.no_builtins_lib {
503        // Said here rather than left to the linker, which on a Windows target says `___chkstk_ms`
504        // is undefined and names mingw-w64's objects as the callers, and on a musl one says
505        // `__udivti3` is. Neither of those is a person's first guess at a missing archive.
506        let tuple = target.tuple().to_canonical_string();
507        return Err(Error::Cross {
508            why: format!(
509                "a cross link ends with librucc_builtins.a, this compiler's own runtime for \
510                 {tuple}, and there is none beside the compiler or under a -B prefix. A sysroot \
511                 does not carry it, because it is our output rather than the platform's. Build it \
512                 with `cargo xtask builtins --target={tuple}`, or pass -fno-builtins-lib to link \
513                 without it"
514            ),
515        });
516    }
517    let invocation = argv::Invocation {
518        inputs: &inputs,
519        output: Some(&output),
520        mode: mode(opts),
521        search: &opts.search,
522        no_startfiles: !opts.wants_startfiles(),
523        no_defaultlibs: !opts.wants_defaultlibs(),
524        no_builtins_lib: opts.no_builtins_lib,
525        builtins: ours.as_deref(),
526        export_dynamic: opts.export_dynamic,
527        strip: opts.strip,
528    };
529    argv::argv(target.tuple(), sysroot, &invocation)
530        .map_err(|why| Error::Cross { why: why.to_string() })
531}
532
533/// Whether this link can be run at all, asked before anything is compiled.
534///
535/// Two questions that have answers before the first object exists: whether there is a line for this
536/// target and mode at all, and whether the sysroot it would read is on the machine. Both are worth a
537/// second at the start rather than a message after a minute of compiling, which is the same reason
538/// the linker itself is looked for first.
539///
540/// The line is built rather than inspected, with no inputs and a name nothing will be written to,
541/// because the refusals belong to the one function that builds it. A link against this machine has
542/// nothing to answer here: its directories are looked for as the line is built and a missing one is
543/// simply a directory that is not offered.
544///
545/// # Errors
546///
547/// [`Error::Cross`] for a target or a mode that has no line, and [`Error::Sysroot`] when the sysroot
548/// it would be linked against is not there.
549pub fn preflight(target: Triple, opts: &LinkOptions) -> Result<(), Error> {
550    let Some(sysroot) = cross_sysroot(target, opts) else { return Ok(()) };
551    // Whether there is a line for this target and mode at all, asked with our own runtime left off
552    // it. Otherwise a target nothing here can link and a machine where nobody built the runtime
553    // report the same thing, and the archive is the smaller of the two problems by a long way.
554    let shape = LinkOptions { no_builtins_lib: true, ..opts.clone() };
555    cross_line(target, &shape, &[], "a.out", &sysroot)?;
556    // The library directory rather than the root, because the root of a cache directory that has
557    // been created and never populated is there and holds nothing. Section 11.6's rule is that
558    // suitable is checked and not assumed, and this is the cheapest form of that.
559    if !sysroot.lib().is_dir() {
560        let tuple = target_tuple(target, opts).to_canonical_string();
561        return Err(Error::Sysroot {
562            dir: sysroot.root().display().to_string(),
563            pinned: rucc_sysroot::pinned_for(&tuple).is_some(),
564            target: tuple,
565        });
566    }
567    // And now the whole line, which is the sysroot's files plus ours, so that a missing runtime is
568    // said here rather than by the linker after everything has been compiled.
569    cross_line(target, opts, &[], "a.out", &sysroot)?;
570    Ok(())
571}
572
573/// The linker to use, looked for where a linker is.
574///
575/// `-B` prefixes first, since the point of one is to put a toolchain in front of the machine's,
576/// then the path. A name that contains a separator is a path and is taken as one, which is what
577/// gcc does with `-fuse-ld=/usr/bin/ld.gold` and what a build system relying on that expects.
578///
579/// # Errors
580///
581/// [`Error::Named`] when `-fuse-ld=` asked for one that is not here, and [`Error::NoLinker`] when
582/// nothing was, which name the candidates so that the message says what was looked for.
583pub fn find(target: Triple, opts: &LinkOptions) -> Result<Linker, Error> {
584    let tried = order(target, opts);
585    for name in &tried {
586        if name.contains(std::path::MAIN_SEPARATOR) || name.contains('/') {
587            let path = PathBuf::from(name);
588            if path.is_file() {
589                return Ok(Linker { name: name.clone(), path });
590            }
591            continue;
592        }
593        for dir in &opts.prefixes {
594            let path = dir.join(name);
595            if path.is_file() {
596                return Ok(Linker { name: name.clone(), path });
597            }
598        }
599        if let Some(path) = on_path(name) {
600            return Ok(Linker { name: name.clone(), path });
601        }
602    }
603    match &opts.use_ld {
604        Some(name) => Err(Error::Named { name: name.clone() }),
605        None => Err(Error::NoLinker { tried }),
606    }
607}
608
609/// The first lld that reads `IMPORT_NAME_EXPORTAS`, which is what a windows-gnu link needs.
610///
611/// 18 does not read it and does not say so, so the number is not a convenience: below it the
612/// answer is wrong rather than absent. tamnd/rucc#1515.
613pub const LLD_EXPORTAS: u32 = 19;
614
615/// Whether a found linker can do this target's link, asked before it is handed anything.
616///
617/// Section 11.6's rule is that suitable is checked and not assumed, and this is the one check that
618/// cannot be made by looking at a file. A windows-gnu link reads import libraries that mingw-w64's
619/// `==` aliases compiled into `IMPORT_NAME_EXPORTAS` records, which lld reads from
620/// [`LLD_EXPORTAS`] on. An older lld writes an import by ordinal zero instead, without a warning
621/// and with a successful exit, so nothing later in the toolchain has anything to notice: the
622/// program is wrong at startup and the link that made it said nothing. Ubuntu 24.04 is the current
623/// LTS and ships 18, so the machine this happens on is an ordinary one.
624///
625/// Every other target is left alone, and so is anything that is not an lld, because this is the one
626/// version of the one linker that is known to answer wrongly rather than not at all.
627///
628/// A linker that will not run or whose version cannot be read is allowed through. What the check
629/// can establish is that a specific old lld is here, and it should not turn every unusual linker
630/// into a refusal on the strength of failing to recognise it.
631///
632/// # Errors
633///
634/// [`Error::TooOld`] when the linker is an lld older than [`LLD_EXPORTAS`] and the target is
635/// windows-gnu.
636pub fn suitable(target: Triple, linker: &Linker) -> Result<(), Error> {
637    if (target.os, target.env) != (Os::Windows, Env::Gnu) {
638        return Ok(());
639    }
640    let Some(found) = lld_major(&reported_version(&linker.path)) else { return Ok(()) };
641    if found >= LLD_EXPORTAS {
642        return Ok(());
643    }
644    Err(Error::TooOld {
645        name: linker.name.clone(),
646        found,
647        target: target.tuple().to_canonical_string(),
648    })
649}
650
651/// What `<linker> --version` prints, or an empty string when it will not say.
652///
653/// A linker that cannot be started is not this function's problem to report, because the link is
654/// about to start it again and say so properly. What this returns for such a one is nothing to
655/// read, which is the same as a linker that ran and said something unrecognisable.
656fn reported_version(path: &Path) -> String {
657    let Ok(out) = Command::new(path).arg("--version").output() else { return String::new() };
658    String::from_utf8_lossy(&out.stdout).into_owned()
659}
660
661/// The major version in an lld's `--version`, when the program that printed it was an lld.
662///
663/// What lld prints is `LLD 18.1.8 (compatible with GNU linkers)`, with a distribution's own prefix
664/// in front of it often enough that the word is looked for rather than the line starting with it:
665/// Ubuntu's says `Ubuntu LLD 18.1.3`. Binutils prints `GNU ld (GNU Binutils for Ubuntu) 2.42` and
666/// mold prints its own name, and neither has the word, so both come back as [`None`] and are left
667/// alone.
668fn lld_major(text: &str) -> Option<u32> {
669    let mut words = text.split_whitespace();
670    words.find(|word| *word == "LLD")?;
671    words.next()?.split('.').next()?.parse().ok()
672}
673
674/// The first executable of that name on `PATH`.
675///
676/// Executability is checked rather than assumed, because a directory of that name on `PATH` is
677/// not a thing to try to run and neither is a file nobody may execute.
678fn on_path(name: &str) -> Option<PathBuf> {
679    let path = std::env::var_os("PATH")?;
680    std::env::split_paths(&path).map(|dir| dir.join(name)).find(|p| executable(p))
681}
682
683/// Whether a path is a file this process could run.
684#[cfg(unix)]
685fn executable(path: &Path) -> bool {
686    use std::os::unix::fs::PermissionsExt as _;
687    path.metadata().is_ok_and(|m| m.is_file() && m.permissions().mode() & 0o111 != 0)
688}
689
690/// Whether a path is a file this process could run.
691///
692/// Windows has no executable bit and decides by extension, and the names looked for above carry
693/// theirs, so being a file is the whole of the question here.
694#[cfg(not(unix))]
695fn executable(path: &Path) -> bool {
696    path.is_file()
697}
698
699/// What the linker is told, in order, not counting the linker itself.
700///
701/// Two lines and [`cross_sysroot`] picks which: the one above for this machine, and
702/// [`rucc_sysroot::argv`]'s for any other. Nothing about the machine is read on the second path, so
703/// `-###` prints the same line on every host and prints it whether the sysroot has been built or
704/// not, which is what makes it worth printing.
705///
706/// # Errors
707///
708/// [`Error::Target`] for a platform there is no native line for yet, which is every one but Linux,
709/// and [`Error::Cross`] for a cross link that cannot be produced at all.
710pub fn line(
711    target: Triple,
712    opts: &LinkOptions,
713    items: &[Item],
714    output: &str,
715) -> Result<Vec<String>, Error> {
716    if let Some(sysroot) = cross_sysroot(target, opts) {
717        return cross_line(target, opts, items, output, &sysroot);
718    }
719    if target.os != Os::Linux {
720        return Err(Error::Target { triple: target.to_string() });
721    }
722    let machine = emulation(target);
723    let root = opts.sysroot.as_deref();
724    let dirs = library_dirs(target, root);
725    // Where a gcc on this machine keeps its own runtime, which is a different place from where
726    // the C library keeps its own, and where our runtime is if it was built for this target.
727    let runtime = runtime_dirs(target, root);
728    let ours = if opts.no_builtins_lib { None } else { builtins_archive(target, &opts.prefixes) };
729    let mut args = vec![
730        "-o".to_owned(),
731        output.to_owned(),
732        // Which of the several formats one `ld` can write is meant. A linker built for more than
733        // one machine guesses from its first input otherwise, and a link of no objects at all has
734        // nothing to guess from.
735        "-m".to_owned(),
736        machine.to_owned(),
737        // The table a program unwinds through, which a C program with no exceptions in it still
738        // needs because `backtrace` and every crash handler read it.
739        "--eh-frame-hdr".to_owned(),
740        // The symbol hash a dynamic loader from this century reads. The old one is still written
741        // alongside by default on some distributions, and asking for this one is what stops a link
742        // from carrying a table nothing has needed since 2006.
743        "--hash-style=gnu".to_owned(),
744    ];
745
746    let pie = opts.pie.unwrap_or(!opts.is_static && !opts.shared);
747    if opts.shared {
748        args.push("-shared".to_owned());
749    } else if opts.is_static {
750        args.push("-static".to_owned());
751    } else if pie {
752        args.push("-pie".to_owned());
753    } else {
754        args.push("-no-pie".to_owned());
755    }
756    if !opts.is_static && !opts.shared {
757        args.push("-dynamic-linker".to_owned());
758        args.push(target_path(root, loader(target)));
759    }
760    if opts.export_dynamic {
761        args.push("--export-dynamic".to_owned());
762    }
763    if opts.strip {
764        args.push("-s".to_owned());
765    }
766
767    if opts.wants_startfiles() {
768        for name in startfile(opts, pie).into_iter().chain(["crti.o"]) {
769            if let Some(path) = find_file(&dirs, name) {
770                args.push(path.display().to_string());
771            }
772        }
773        // The compiler's own startup file, which runs the static constructors. Three spellings
774        // of the same thing, and which one is right is about how the code in it refers to
775        // itself: `S` for a position independent result, `T` for a static one, plain for the
776        // rest. Skipped when there is no gcc on the machine to take it from, because a program
777        // with no constructor in it does not miss it.
778        let begin = if opts.shared || pie {
779            "crtbeginS.o"
780        } else if opts.is_static {
781            "crtbeginT.o"
782        } else {
783            "crtbegin.o"
784        };
785        if let Some(path) = find_file(&runtime, begin).or_else(|| find_file(&runtime, "crtbegin.o"))
786        {
787            args.push(path.display().to_string());
788        }
789    }
790
791    for dir in &opts.search {
792        args.push(format!("-L{}", dir.display()));
793    }
794    for dir in &dirs {
795        args.push(format!("-L{}", dir.display()));
796    }
797    // Where `libgcc.a` and `libgcc_eh.a` are, which is not where the C library is. Nothing is
798    // added when there is no gcc on the machine, and then the `-l` names below are left off too.
799    for dir in &runtime {
800        args.push(format!("-L{}", dir.display()));
801    }
802
803    for item in items {
804        match item {
805            Item::File(path) => args.push(path.clone()),
806            Item::Library(name) => args.push(format!("-l{name}")),
807            Item::Linker(arg) => args.push(arg.clone()),
808        }
809    }
810    // After the objects, because a static archive is searched for what is undefined at the point
811    // it is reached and a library named before the object that needs it contributes nothing.
812    args.extend(runtime_items(opts, &runtime, ours.as_deref()));
813
814    if opts.wants_startfiles() {
815        // The other end of `crtbegin`, and it goes before `crtn.o` for the same reason `crti.o`
816        // goes before `crtbegin`: the four are two nested pairs and not four separate files.
817        let end = if opts.shared || pie { "crtendS.o" } else { "crtend.o" };
818        if let Some(path) = find_file(&runtime, end).or_else(|| find_file(&runtime, "crtend.o")) {
819            args.push(path.display().to_string());
820        }
821        if let Some(path) = find_file(&dirs, "crtn.o") {
822            args.push(path.display().to_string());
823        }
824    }
825
826    Ok(args)
827}
828
829/// The startup file the C library brings, or `None` for a link that calls nothing.
830///
831/// This is what calls `main` and what passes it the arguments, so a shared object takes none of
832/// them: nothing starts one and it has no `main` to be started at. `Scrt1.o` rather than `crt1.o`
833/// when the result moves, because the two differ in whether the reference to `main` in them is one
834/// a loader may relocate.
835///
836/// A profiled program gets a different one again, which does all of that and starts and stops the
837/// counting around it. There are two of those rather than three: the one that relocates itself is
838/// only needed by a static position independent link, and every other link takes the plain one,
839/// which is what gcc does with the same flag.
840fn startfile(opts: &LinkOptions, pie: bool) -> Option<&'static str> {
841    if opts.shared {
842        None
843    } else if opts.profile {
844        Some(if pie && opts.is_static { "grcrt1.o" } else { "gcrt1.o" })
845    } else if pie {
846        Some("Scrt1.o")
847    } else {
848        Some("crt1.o")
849    }
850}
851
852/// The libraries the compiler's own runtime contributes, in the order the linker wants them.
853///
854/// The C library first, then ours, then the machine's `libgcc`. Order inside this list is not
855/// about whether a symbol resolves, it is about which archive supplies one that more than one of
856/// them defines, and the two places that happens both have a right answer.
857///
858/// `memcpy` and its three neighbours are in the C library on a hosted target and in ours only for
859/// a freestanding one, which is what `spec/12-abi-and-runtime.md` section 12.8 says they are for.
860/// glibc's are written in assembly per microarchitecture and ours is a word at a time loop, so a
861/// link that took ours over glibc's would be slower at the one routine every program reaches.
862///
863/// The wide arithmetic is in ours and in `libgcc` both, and the two are ABI-identical on purpose,
864/// so which one answers is not a correctness question. Ours comes first because it is ours, and
865/// `-fno-builtins-lib` leaves it off for somebody who would rather it were not.
866///
867/// A static link puts the whole list inside `--start-group`. `libc.a` refers to `_Unwind_Resume`,
868/// and the unwinder refers back into `libc.a`, so a linker walking the list once resolves
869/// whichever it reaches first and reports the other as undefined. That is exactly the failure
870/// issue #277 describes and the group is the fix for it.
871///
872/// A dynamic link needs no group, because the shared `libc` resolves its own references inside
873/// itself. `libgcc_s` is asked for `--as-needed` there, the way gcc asks for it, so a program that
874/// never unwinds does not acquire a dependency on it.
875fn runtime_items(opts: &LinkOptions, runtime: &[PathBuf], ours: Option<&Path>) -> Vec<String> {
876    let mut args = Vec::new();
877    if !opts.wants_defaultlibs() && !opts.wants_runtime() {
878        return args;
879    }
880    // Only when there is a gcc to take them from. On a machine without one the names would be an
881    // error about a library that was never going to be there, and a program that needs neither
882    // the unwinder nor a wide divide links and runs without them.
883    let has_gcc = find_file(runtime, "libgcc.a").is_some();
884
885    if opts.is_static {
886        args.push("--start-group".to_owned());
887    }
888    if opts.wants_defaultlibs() {
889        args.push("-lc".to_owned());
890    }
891    if opts.wants_runtime() {
892        if let Some(path) = ours {
893            args.push(path.display().to_string());
894        }
895        if has_gcc {
896            args.push("-lgcc".to_owned());
897            if opts.is_static {
898                args.push("-lgcc_eh".to_owned());
899            }
900        }
901    }
902    if opts.is_static {
903        args.push("--end-group".to_owned());
904    } else if opts.wants_runtime() && has_gcc {
905        // The shared half, and only if something still wants it after everything above.
906        args.push("--as-needed".to_owned());
907        args.push("-lgcc_s".to_owned());
908        args.push("--no-as-needed".to_owned());
909    }
910    args
911}
912
913/// Where a gcc on this machine keeps `crtbegin.o`, `crtend.o` and `libgcc.a`, newest first.
914///
915/// This is not where the C library's files are. A distribution puts them under a directory named
916/// for the gcc version, and there may be several, so the answer is every one that exists with the
917/// highest version in front. Newest first because a newer `libgcc` is a superset of an older one
918/// and because that is the one the C library on the same machine was built against.
919#[must_use]
920pub fn runtime_dirs(target: Triple, sysroot: Option<&Path>) -> Vec<PathBuf> {
921    let libc = match target.env {
922        Env::Musl => "musl",
923        Env::None | Env::Gnu | Env::Msvc => "gnu",
924    };
925    let arch = target.arch.as_str();
926    // The spellings the distributions use for the same triple. Debian and Ubuntu drop the vendor
927    // field, the source builds and Arch keep `pc`, and Red Hat and SUSE write their own name in
928    // it, so all of them are looked for and the ones that are there are taken.
929    let names = [
930        format!("{arch}-linux-{libc}"),
931        format!("{arch}-pc-linux-{libc}"),
932        format!("{arch}-redhat-linux"),
933        format!("{arch}-suse-linux"),
934        format!("{arch}-alpine-linux-{libc}"),
935    ];
936    let mut found = Vec::new();
937    for base in ["/usr/lib/gcc", "/usr/lib64/gcc", "/usr/local/lib/gcc"] {
938        for name in &names {
939            let dir = under(sysroot, &format!("{base}/{name}"));
940            let Ok(entries) = fs::read_dir(&dir) else { continue };
941            let mut versions: Vec<(Vec<u64>, PathBuf)> = entries
942                .flatten()
943                .map(|e| e.path())
944                .filter(|p| p.is_dir())
945                .map(|p| (version_key(&p), p))
946                .collect();
947            // Descending, so the highest version is the first place `find_file` looks. Ties keep
948            // the order the directory gave, which is arbitrary and does not matter because two
949            // directories that sort the same hold the same version.
950            versions.sort_by(|a, b| b.0.cmp(&a.0));
951            found.extend(versions.into_iter().map(|(_, path)| path));
952        }
953    }
954    found
955}
956
957/// A directory name read as a version, so that `13` sorts above `9` and `10.2` above `10`.
958///
959/// A name that is not a version at all sorts below every name that is, rather than being left
960/// out, because a directory holding a `libgcc.a` is worth looking in whatever it is called.
961fn version_key(dir: &Path) -> Vec<u64> {
962    let name = dir.file_name().unwrap_or_default().to_string_lossy();
963    name.split('.').map(|part| part.parse::<u64>().unwrap_or(0)).collect()
964}
965
966/// Our own runtime library for this target, if it was built.
967///
968/// Looked for beside the compiler rather than at a path decided when the compiler was built, for
969/// the same reason everything else here is looked for: one binary runs wherever it is copied. A
970/// `-B` prefix is asked first, because that is what a `-B` prefix is for.
971#[must_use]
972pub fn builtins_archive(target: Triple, prefixes: &[PathBuf]) -> Option<PathBuf> {
973    // The name from the crate that puts it on a line, rather than a second spelling of it here,
974    // which is what that constant asks of anybody who needs the name.
975    const NAME: &str = rucc_sysroot::link::BUILTINS;
976    let triple = target.to_string();
977    let mut places: Vec<PathBuf> = Vec::new();
978    for prefix in prefixes {
979        places.push(prefix.join(&triple).join(NAME));
980        places.push(prefix.join(NAME));
981    }
982    if let Some(dir) =
983        std::env::current_exe().ok().and_then(|exe| exe.parent().map(Path::to_path_buf))
984    {
985        // An install: the compiler in `bin` and its runtime in `lib/rucc/<triple>`.
986        if let Some(up) = dir.parent() {
987            places.push(up.join("lib").join("rucc").join(&triple).join(NAME));
988            // A build tree: the compiler in `target/release` and the runtime, which is built for
989            // the target and not the host, in `target/<triple>/release`.
990            for profile in ["release", "debug"] {
991                places.push(up.join(&triple).join(profile).join(NAME));
992            }
993        }
994        places.push(dir.join(NAME));
995    }
996    places.into_iter().find(|path| path.is_file())
997}
998
999/// Which output format this `ld` should write, in the name `ld` knows it by.
1000fn emulation(target: Triple) -> &'static str {
1001    match target.arch {
1002        Arch::X86_64 => "elf_x86_64",
1003        Arch::Aarch64 => "aarch64linux",
1004        Arch::Riscv64 => "elf64lriscv",
1005    }
1006}
1007
1008/// The program that starts a dynamically linked program, whose path is part of the file.
1009///
1010/// It is a per-target constant rather than something to look for, because the name is fixed by
1011/// the platform's ABI and a program naming a different one does not start.
1012fn loader(target: Triple) -> &'static str {
1013    match (target.arch, target.env) {
1014        (Arch::X86_64, Env::Musl) => "/lib/ld-musl-x86_64.so.1",
1015        (Arch::X86_64, _) => "/lib64/ld-linux-x86-64.so.2",
1016        (Arch::Aarch64, Env::Musl) => "/lib/ld-musl-aarch64.so.1",
1017        (Arch::Aarch64, _) => "/lib/ld-linux-aarch64.so.1",
1018        (Arch::Riscv64, Env::Musl) => "/lib/ld-musl-riscv64.so.1",
1019        (Arch::Riscv64, _) => "/lib/ld-linux-riscv64-lp64d.so.1",
1020    }
1021}
1022
1023/// Where the library's own files might be, in search order.
1024///
1025/// The multiarch directory first for the reason it comes first in the header search: it is where
1026/// a distribution that can hold two architectures at once puts the one being asked for, and a
1027/// distribution that cannot simply does not have it. `lib64` after it, which is what the
1028/// distributions that split by word size use instead, and `lib` last, which is every other one.
1029#[must_use]
1030pub fn candidates(target: Triple, sysroot: Option<&Path>) -> Vec<PathBuf> {
1031    let multiarch = multiarch(target);
1032    [
1033        format!("/usr/lib/{multiarch}"),
1034        format!("/lib/{multiarch}"),
1035        "/usr/lib64".to_owned(),
1036        "/lib64".to_owned(),
1037        "/usr/lib".to_owned(),
1038        "/lib".to_owned(),
1039    ]
1040    .into_iter()
1041    .map(|dir| under(sysroot, &dir))
1042    .collect()
1043}
1044
1045/// The name a distribution that holds two architectures at once files this target under.
1046///
1047/// `x86_64-linux-gnu` and its friends, which is what `gcc -print-multiarch` prints and what a
1048/// build system pastes into a path when it is looking for a library itself.
1049#[must_use]
1050pub fn multiarch(target: Triple) -> String {
1051    let libc = match target.env {
1052        Env::Musl => "musl",
1053        Env::None | Env::Gnu | Env::Msvc => "gnu",
1054    };
1055    format!("{}-linux-{libc}", target.arch.as_str())
1056}
1057
1058/// The candidates that are there.
1059fn library_dirs(target: Triple, sysroot: Option<&Path>) -> Vec<PathBuf> {
1060    candidates(target, sysroot).into_iter().filter(|dir| dir.is_dir()).collect()
1061}
1062
1063/// Where a library is looked for, in the order it is looked for in.
1064///
1065/// The command line first and the target's own after it, which is the order the linker is handed
1066/// and therefore the order `-print-search-dirs` has to print.
1067#[must_use]
1068pub fn search_dirs(link: &LinkOptions, target: Triple) -> Vec<PathBuf> {
1069    let mut dirs = link.search.clone();
1070    // A cross link searches one directory and it is the sysroot's, so this is that and not the
1071    // machine's. What `-print-search-dirs` says is what a build system pastes into a link line of its
1072    // own, and an answer that named `/usr/lib` for a target whose link line never goes near it would
1073    // be worse than no answer at all.
1074    if let Some(sysroot) = cross_sysroot(target, link) {
1075        dirs.push(sysroot.lib());
1076        return dirs;
1077    }
1078    dirs.extend(candidates(target, link.sysroot.as_deref()));
1079    dirs
1080}
1081
1082/// The full path of a file with that name, when one of the search directories holds it.
1083///
1084/// What `-print-file-name=` answers. GCC prints the name back unchanged when it finds nothing,
1085/// which is what makes the flag safe to paste into a link line either way.
1086#[must_use]
1087pub fn find_in_search(link: &LinkOptions, target: Triple, name: &str) -> Option<PathBuf> {
1088    find_file(&search_dirs(link, target), name)
1089}
1090
1091/// The first of those directories holding a file of that name.
1092fn find_file(dirs: &[PathBuf], name: &str) -> Option<PathBuf> {
1093    dirs.iter().map(|dir| dir.join(name)).find(|path| path.is_file())
1094}
1095
1096/// A path under the sysroot, when there is one.
1097fn under(sysroot: Option<&Path>, path: &str) -> PathBuf {
1098    match sysroot {
1099        // `strip_prefix` because joining an absolute path replaces the root rather than extending
1100        // it, which would make every entry the unprefixed one.
1101        Some(root) => root.join(path.strip_prefix('/').unwrap_or(path)),
1102        None => PathBuf::from(path),
1103    }
1104}
1105
1106/// A path on the machine that will run the program, rather than on the one compiling it.
1107///
1108/// Written with the separator of the target and not of the host, which matters for the one path
1109/// that is not looked at here but stored in the file and read by something else later: the loader
1110/// a dynamic program names. A Windows host joining it would put a backslash in the middle of a
1111/// name that a Linux loader has to find, and the program would not start.
1112fn target_path(sysroot: Option<&Path>, path: &str) -> String {
1113    match sysroot {
1114        Some(root) => {
1115            let root = root.display().to_string();
1116            format!("{}/{}", root.trim_end_matches(['/', '\\']), path.trim_start_matches('/'))
1117        }
1118        None => path.to_owned(),
1119    }
1120}
1121
1122/// The whole invocation as one line, quoted the way `-###` prints it.
1123#[must_use]
1124pub fn render(linker: &Linker, args: &[String]) -> String {
1125    let mut out = linker.path.display().to_string();
1126    for arg in args {
1127        out.push(' ');
1128        if arg.is_empty() || arg.contains(char::is_whitespace) {
1129            out.push('"');
1130            out.push_str(arg);
1131            out.push('"');
1132        } else {
1133            out.push_str(arg);
1134        }
1135    }
1136    out
1137}
1138
1139/// Runs the linker and waits for it.
1140///
1141/// # Errors
1142///
1143/// [`Error::Spawn`] when it could not be started, which is a machine problem, and
1144/// [`Error::Refused`] when it ran and said no, which is a program problem and one the linker has
1145/// already explained on its own error output.
1146pub fn run(linker: &Linker, args: &[String]) -> Result<(), Error> {
1147    let args: Vec<OsString> = args.iter().map(OsString::from).collect();
1148    let status = Command::new(&linker.path).args(&args).status().map_err(|why| Error::Spawn {
1149        path: linker.path.display().to_string(),
1150        why: why.to_string(),
1151    })?;
1152    if status.success() {
1153        return Ok(());
1154    }
1155    // Nothing is added to what the linker printed. It has already named the symbol or the file,
1156    // and a second message from here saying that linking failed would only push the first one
1157    // further up the screen.
1158    Err(Error::Refused {
1159        status: match status.code() {
1160            Some(code) => format!("exited with status {code}"),
1161            None => "was killed before it finished".to_owned(),
1162        },
1163    })
1164}
1165
1166#[cfg(test)]
1167mod tests {
1168    use super::*;
1169
1170    fn linux() -> Triple {
1171        Triple::new(Arch::X86_64, Os::Linux, Env::Gnu)
1172    }
1173
1174    fn one(name: &str) -> Vec<Item> {
1175        vec![Item::File(name.to_owned())]
1176    }
1177
1178    #[test]
1179    fn the_fast_one_is_looked_for_first_and_the_platforms_own_last() {
1180        let names = order(linux(), &LinkOptions::default());
1181        assert_eq!(names.first().map(String::as_str), Some("ld.mold"));
1182        assert_eq!(names.last().map(String::as_str), Some("ld"));
1183    }
1184
1185    #[test]
1186    fn naming_one_is_the_whole_of_the_order() {
1187        let opts = LinkOptions { use_ld: Some("gold".to_owned()), ..LinkOptions::default() };
1188        assert_eq!(order(linux(), &opts), ["ld.gold", "gold"]);
1189    }
1190
1191    #[test]
1192    fn a_dynamic_program_names_the_loader_that_will_start_it() {
1193        let args = line(linux(), &LinkOptions::default(), &one("a.o"), "a.out").expect("a line");
1194        let at = args.iter().position(|a| a == "-dynamic-linker").expect("the flag");
1195        assert!(args[at + 1].ends_with("/lib64/ld-linux-x86-64.so.2"), "{args:?}");
1196    }
1197
1198    #[test]
1199    fn a_static_program_names_no_loader_because_nothing_will_start_it() {
1200        let opts = LinkOptions { is_static: true, ..LinkOptions::default() };
1201        let args = line(linux(), &opts, &one("a.o"), "a.out").expect("a line");
1202        assert!(args.contains(&"-static".to_owned()), "{args:?}");
1203        assert!(!args.contains(&"-dynamic-linker".to_owned()), "{args:?}");
1204    }
1205
1206    #[test]
1207    fn the_startup_file_of_a_program_that_moves_is_not_the_one_of_a_program_that_does_not() {
1208        let moving = LinkOptions { pie: Some(true), ..LinkOptions::default() };
1209        let fixed = LinkOptions { pie: Some(false), ..LinkOptions::default() };
1210        let named = |opts: &LinkOptions| {
1211            line(linux(), opts, &one("a.o"), "a.out")
1212                .expect("a line")
1213                .iter()
1214                .filter_map(|a| Path::new(a).file_name().map(|n| n.to_string_lossy().into_owned()))
1215                .find(|n| n.ends_with("crt1.o"))
1216        };
1217        // Only when the machine running this has them, which is what makes this two assertions
1218        // rather than one: a machine with no glibc development files has neither to find.
1219        if let Some(name) = named(&moving) {
1220            assert_eq!(name, "Scrt1.o");
1221            assert_eq!(named(&fixed).as_deref(), Some("crt1.o"));
1222        }
1223    }
1224
1225    /// A profiled program is started by a startup file of its own.
1226    ///
1227    /// The counts it keeps have to be started before `main` runs and written out after it returns,
1228    /// and what does both is this file rather than anything the compiler wrote. So a build that
1229    /// compiles with the flag and links without it produces a program that calls the hook on every
1230    /// function and never writes a profile, which is the failure this is here to keep out.
1231    ///
1232    /// A shared object takes none of them either way, since nothing starts one.
1233    #[test]
1234    fn a_profiled_program_is_started_by_the_startup_file_that_counts() {
1235        let profile = LinkOptions { profile: true, ..LinkOptions::default() };
1236        assert_eq!(startfile(&profile, false), Some("gcrt1.o"));
1237        assert_eq!(startfile(&profile, true), Some("gcrt1.o"));
1238        let still = LinkOptions { is_static: true, ..profile.clone() };
1239        assert_eq!(startfile(&still, true), Some("grcrt1.o"));
1240        assert_eq!(startfile(&still, false), Some("gcrt1.o"));
1241        let shared = LinkOptions { shared: true, ..profile };
1242        assert_eq!(startfile(&shared, false), None);
1243    }
1244
1245    /// And a program that is not profiled is started by the one it always was.
1246    #[test]
1247    fn a_program_that_is_not_profiled_is_started_by_the_usual_one() {
1248        let plain = LinkOptions::default();
1249        assert_eq!(startfile(&plain, false), Some("crt1.o"));
1250        assert_eq!(startfile(&plain, true), Some("Scrt1.o"));
1251    }
1252
1253    #[test]
1254    fn asking_for_no_startup_files_leaves_out_both_ends_of_them() {
1255        let opts = LinkOptions { no_startfiles: true, ..LinkOptions::default() };
1256        let args = line(linux(), &opts, &one("a.o"), "a.out").expect("a line");
1257        assert!(!args.iter().any(|a| a.ends_with("crt1.o")), "{args:?}");
1258        assert!(!args.iter().any(|a| a.ends_with("crtn.o")), "{args:?}");
1259        // And still links against the library, because that is the other flag.
1260        assert!(args.contains(&"-lc".to_owned()), "{args:?}");
1261    }
1262
1263    #[test]
1264    fn asking_for_no_library_at_all_leaves_out_the_startup_files_too() {
1265        let opts = LinkOptions { no_stdlib: true, ..LinkOptions::default() };
1266        let args = line(linux(), &opts, &one("a.o"), "a.out").expect("a line");
1267        assert!(!args.contains(&"-lc".to_owned()), "{args:?}");
1268        assert!(!args.iter().any(|a| a.ends_with("crt1.o")), "{args:?}");
1269    }
1270
1271    #[test]
1272    fn the_library_comes_after_the_objects_that_need_it() {
1273        let items = vec![Item::File("a.o".to_owned()), Item::Library("m".to_owned())];
1274        let args = line(linux(), &LinkOptions::default(), &items, "a.out").expect("a line");
1275        let obj = args.iter().position(|a| a == "a.o").expect("the object");
1276        let m = args.iter().position(|a| a == "-lm").expect("the library");
1277        let c = args.iter().position(|a| a == "-lc").expect("the library");
1278        assert!(obj < m && m < c, "{args:?}");
1279    }
1280
1281    #[test]
1282    fn what_the_user_told_the_linker_stays_where_the_user_wrote_it() {
1283        // The pair libtool writes around a set of convenience archives, which is what found this.
1284        // Both words are about the files between them, so a line that collects them and puts them
1285        // at the end has two options that do nothing and an archive whose members were all dropped.
1286        let items = vec![
1287            Item::File("a.o".to_owned()),
1288            Item::Linker("--whole-archive".to_owned()),
1289            Item::File("libaesni.a".to_owned()),
1290            Item::Linker("--no-whole-archive".to_owned()),
1291            Item::Library("m".to_owned()),
1292        ];
1293        let args = line(linux(), &LinkOptions::default(), &items, "a.out").expect("a line");
1294        let at = |what: &str| args.iter().position(|a| a == what).expect(what);
1295        assert!(at("a.o") < at("--whole-archive"), "{args:?}");
1296        assert!(at("--whole-archive") < at("libaesni.a"), "{args:?}");
1297        assert!(at("libaesni.a") < at("--no-whole-archive"), "{args:?}");
1298        assert!(at("--no-whole-archive") < at("-lm"), "{args:?}");
1299        assert!(at("-lm") < at("-lc"), "{args:?}");
1300    }
1301
1302    #[test]
1303    fn a_sysroot_moves_every_path_this_decided_and_none_the_user_wrote() {
1304        let opts = LinkOptions {
1305            sysroot: Some(PathBuf::from("/nowhere-at-all")),
1306            search: vec![PathBuf::from("/opt/mine")],
1307            ..LinkOptions::default()
1308        };
1309        let args = line(linux(), &opts, &one("a.o"), "a.out").expect("a line");
1310        let at = args.iter().position(|a| a == "-dynamic-linker").expect("the flag");
1311        assert_eq!(args[at + 1], "/nowhere-at-all/lib64/ld-linux-x86-64.so.2");
1312        assert!(args.contains(&"-L/opt/mine".to_owned()), "{args:?}");
1313    }
1314
1315    #[test]
1316    fn a_platform_with_no_link_line_is_said_so_rather_than_linked_wrongly() {
1317        for triple in [
1318            Triple::new(Arch::X86_64, Os::Darwin, Env::Gnu),
1319            Triple::new(Arch::X86_64, Os::Windows, Env::Msvc),
1320        ] {
1321            let error = line(triple, &LinkOptions::default(), &one("a.o"), "a.out")
1322                .expect_err("no line for it");
1323            assert!(matches!(error, Error::Target { .. }), "{error:?}");
1324        }
1325    }
1326
1327    #[test]
1328    fn the_line_is_printed_the_way_it_would_be_typed() {
1329        let linker = Linker { name: "ld".to_owned(), path: PathBuf::from("/usr/bin/ld") };
1330        let args = ["-o".to_owned(), "a b".to_owned()];
1331        assert_eq!(render(&linker, &args), "/usr/bin/ld -o \"a b\"");
1332    }
1333
1334    #[test]
1335    fn a_linker_that_is_not_there_is_said_by_name() {
1336        let opts = LinkOptions {
1337            use_ld: Some("a-linker-nobody-has".to_owned()),
1338            ..LinkOptions::default()
1339        };
1340        let error = find(linux(), &opts).expect_err("not on this machine");
1341        assert_eq!(error, Error::Named { name: "a-linker-nobody-has".to_owned() });
1342    }
1343    /// A directory with a `libgcc.a` in it, so a test can say what a machine with a gcc on it
1344    /// looks like without needing one.
1345    fn a_gcc_dir(name: &str) -> PathBuf {
1346        let dir = std::env::temp_dir().join(format!("rucc-link-{name}-{}", std::process::id()));
1347        fs::create_dir_all(&dir).expect("a temporary directory");
1348        fs::write(dir.join("libgcc.a"), b"not really an archive").expect("a file in it");
1349        dir
1350    }
1351
1352    #[test]
1353    fn the_c_library_supplies_the_block_routines_and_our_runtime_does_not_displace_them() {
1354        let gcc = a_gcc_dir("order");
1355        let ours = PathBuf::from("/somewhere/librucc_builtins.a");
1356        let args = runtime_items(&LinkOptions::default(), &[gcc], Some(&ours));
1357        let at_libc = args.iter().position(|a| a == "-lc").expect("libc");
1358        let at_ours = args.iter().position(|a| a.ends_with("librucc_builtins.a")).expect("ours");
1359        // glibc's `memcpy` is assembly per microarchitecture and ours is a word at a time loop,
1360        // so on a target that has one, its is the one that should answer.
1361        assert!(at_libc < at_ours, "{args:?}");
1362    }
1363
1364    #[test]
1365    fn a_static_link_puts_them_in_a_group_because_two_of_them_refer_to_each_other() {
1366        let gcc = a_gcc_dir("group");
1367        let opts = LinkOptions { is_static: true, ..LinkOptions::default() };
1368        let args = runtime_items(&opts, &[gcc], None);
1369        assert_eq!(args.first().map(String::as_str), Some("--start-group"), "{args:?}");
1370        assert_eq!(args.last().map(String::as_str), Some("--end-group"), "{args:?}");
1371        // The unwinder, which is what `libc.a` refers to and what a static link fails on without
1372        // it. Issue #277.
1373        assert!(args.contains(&"-lgcc_eh".to_owned()), "{args:?}");
1374    }
1375
1376    #[test]
1377    fn a_dynamic_link_needs_no_group_and_asks_for_the_shared_half_only_if_something_wants_it() {
1378        let gcc = a_gcc_dir("dynamic");
1379        let args = runtime_items(&LinkOptions::default(), &[gcc], None);
1380        assert!(!args.contains(&"--start-group".to_owned()), "{args:?}");
1381        assert!(!args.contains(&"-lgcc_eh".to_owned()), "{args:?}");
1382        let at = args.iter().position(|a| a == "-lgcc_s").expect("the shared half");
1383        assert_eq!(args[at - 1], "--as-needed", "{args:?}");
1384        assert_eq!(args[at + 1], "--no-as-needed", "{args:?}");
1385    }
1386
1387    #[test]
1388    fn our_own_runtime_comes_before_the_machines_because_the_two_are_interchangeable() {
1389        let gcc = a_gcc_dir("ours");
1390        let ours = PathBuf::from("/somewhere/librucc_builtins.a");
1391        let args = runtime_items(&LinkOptions::default(), &[gcc], Some(&ours));
1392        let at_ours = args.iter().position(|a| a.ends_with("librucc_builtins.a")).expect("ours");
1393        let at_gcc = args.iter().position(|a| a == "-lgcc").expect("libgcc");
1394        assert!(at_ours < at_gcc, "{args:?}");
1395    }
1396
1397    #[test]
1398    fn no_builtins_lib_leaves_ours_off_and_keeps_the_machines() {
1399        let gcc = a_gcc_dir("theirs");
1400        let opts = LinkOptions { no_builtins_lib: true, ..LinkOptions::default() };
1401        let args = line(linux(), &opts, &one("a.o"), "a.out").expect("a line");
1402        assert!(!args.iter().any(|a| a.ends_with("librucc_builtins.a")), "{args:?}");
1403        // And the machine's half is still decided the same way it was, from the directories
1404        // that are there, which on the machine running this test may be none.
1405        assert!(runtime_items(&opts, &[gcc], None).contains(&"-lgcc".to_owned()));
1406    }
1407
1408    #[test]
1409    fn nodefaultlibs_leaves_the_whole_runtime_off_and_not_only_the_c_library() {
1410        let gcc = a_gcc_dir("none");
1411        let opts = LinkOptions { no_defaultlibs: true, ..LinkOptions::default() };
1412        assert!(runtime_items(&opts, &[gcc], None).is_empty());
1413    }
1414
1415    #[test]
1416    fn a_machine_with_no_gcc_on_it_gets_no_names_for_libraries_that_are_not_there() {
1417        let empty = std::env::temp_dir().join("rucc-link-empty-not-a-gcc");
1418        let args = runtime_items(&LinkOptions::default(), &[empty], None);
1419        assert_eq!(args, ["-lc"], "{args:?}");
1420    }
1421
1422    #[test]
1423    fn a_gcc_version_directory_is_read_as_a_version_and_not_as_a_word() {
1424        assert!(version_key(Path::new("/usr/lib/gcc/x/13")) > version_key(Path::new("/x/9")));
1425        assert!(version_key(Path::new("/x/10.2")) > version_key(Path::new("/x/10")));
1426        // Something that is not a version at all still sorts, and sorts below one that is.
1427        assert!(version_key(Path::new("/x/snapshot")) < version_key(Path::new("/x/1")));
1428    }
1429
1430    /// A command line that has a cache to find generated sysroots in, which a real one always has.
1431    ///
1432    /// And a `-B` prefix with our runtime in it, because a cross link refuses without one and
1433    /// every machine that does this for real has the archive `cargo xtask builtins` wrote. What
1434    /// happens when it is missing is its own test below.
1435    fn cached() -> LinkOptions {
1436        LinkOptions {
1437            cache: Some(PathBuf::from("/cache")),
1438            prefixes: vec![a_builtins_dir()],
1439            ..LinkOptions::default()
1440        }
1441    }
1442
1443    /// A directory with our runtime archive in it, so that a test can say what a machine where the
1444    /// runtime was built looks like without building one.
1445    ///
1446    /// One directory for every test rather than one each, since none of them writes to it and the
1447    /// name of the file is the whole of what they read.
1448    fn a_builtins_dir() -> PathBuf {
1449        let dir = std::env::temp_dir().join(format!("rucc-link-ours-{}", std::process::id()));
1450        fs::create_dir_all(&dir).expect("a temporary directory");
1451        fs::write(dir.join("librucc_builtins.a"), b"not really an archive").expect("a file in it");
1452        dir
1453    }
1454
1455    /// Where that cache would keep this target's sysroot.
1456    fn a_sysroot(target: Triple) -> Sysroot {
1457        Sysroot::in_cache(Path::new("/cache"), target.tuple())
1458    }
1459
1460    /// A target that is not the machine running this test, whatever machine that is.
1461    ///
1462    /// A freestanding one, because [`Triple::host`] answers Linux, Darwin or Windows and never
1463    /// `Os::None`. Every other triple is somebody's host, so a test that wants the cross path out of
1464    /// [`line`] itself has to use this one and the rest go through [`cross_line`].
1465    fn foreign() -> Triple {
1466        Triple::new(Arch::X86_64, Os::None, Env::None)
1467    }
1468
1469    #[test]
1470    fn a_cross_link_reads_the_targets_own_sysroot_and_nothing_of_this_machine() {
1471        let target = Triple::new(Arch::Aarch64, Os::Linux, Env::Musl);
1472        let sysroot = a_sysroot(target);
1473        // The paths as this host spells them, because what is being checked is which directory the
1474        // files are in and a Windows separator is a backslash.
1475        let root = sysroot.root().display().to_string();
1476        let lib = sysroot.lib();
1477        let args = cross_line(target, &cached(), &one("a.o"), "a.out", &sysroot).expect("a line");
1478        assert!(args.contains(&format!("--sysroot={root}")), "{args:?}");
1479        assert!(args.contains(&format!("-L{}", lib.display())), "{args:?}");
1480        assert!(args.contains(&lib.join("libc.a").display().to_string()), "{args:?}");
1481        let at = args.iter().position(|a| a == "-dynamic-linker").expect("the loader");
1482        assert_eq!(args[at + 1], "/lib/ld-musl-aarch64.so.1", "{args:?}");
1483        // The whole point of the other path not being taken: not one directory of this machine is
1484        // on the line, so the line is the same on every host and the recorded ones describe it.
1485        for arg in &args {
1486            assert!(!arg.contains("/usr/lib"), "{arg} in {args:?}");
1487            assert!(!arg.contains("/lib64"), "{arg} in {args:?}");
1488        }
1489    }
1490
1491    #[test]
1492    fn a_freestanding_target_links_against_our_runtime_instead_of_being_refused() {
1493        let args = line(foreign(), &cached(), &one("a.o"), "a.out").expect("a line");
1494        assert!(args.iter().any(|a| a.ends_with("librucc_builtins.a")), "{args:?}");
1495        // No libc, because there is not one, and no start file either: what runs before `main` on a
1496        // freestanding target comes from whatever is being built.
1497        assert!(!args.iter().any(|a| a.ends_with("libc.a")), "{args:?}");
1498        assert!(!args.contains(&"-lc".to_owned()), "{args:?}");
1499        assert!(!args.iter().any(|a| a.ends_with("crt1.o")), "{args:?}");
1500    }
1501
1502    /// And with nothing to find sysroots in it is refused, which is what it was before this.
1503    #[test]
1504    fn a_driver_with_no_cache_to_look_in_says_so_rather_than_guessing() {
1505        let error = line(foreign(), &LinkOptions::default(), &one("a.o"), "a.out")
1506            .expect_err("no line for it");
1507        assert!(matches!(error, Error::Target { .. }), "{error:?}");
1508    }
1509
1510    #[test]
1511    fn a_static_link_against_a_libc_that_is_a_stub_is_refused_rather_than_attempted() {
1512        let target = Triple::new(Arch::X86_64, Os::Linux, Env::Gnu);
1513        let opts = LinkOptions { is_static: true, ..cached() };
1514        let error = cross_line(target, &opts, &one("a.o"), "a.out", &a_sysroot(target))
1515            .expect_err("there is no libc.a in a stub sysroot");
1516        let Error::Cross { why } = &error else { panic!("{error:?}") };
1517        // Because a stub carries the names a library exports and none of the bodies, which is
1518        // everything a dynamic link reads and nothing a static one does.
1519        assert!(why.contains("stub"), "{why}");
1520    }
1521
1522    #[test]
1523    fn a_target_whose_linker_wants_a_different_line_is_refused_by_name() {
1524        for target in [
1525            Triple::new(Arch::Aarch64, Os::Darwin, Env::None),
1526            Triple::new(Arch::X86_64, Os::Windows, Env::Msvc),
1527        ] {
1528            let error = cross_line(target, &cached(), &one("a.o"), "a.out", &a_sysroot(target))
1529                .expect_err("no line for that format");
1530            let Error::Cross { why } = &error else { panic!("{error:?}") };
1531            assert!(why.contains(&target.tuple().to_canonical_string()), "{why}");
1532        }
1533    }
1534
1535    #[test]
1536    fn a_mingw_target_links_and_looks_for_a_linker_that_can_write_a_pe_image() {
1537        let target = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
1538        let args = cross_line(target, &cached(), &one("a.o"), "a.exe", &a_sysroot(target))
1539            .expect("a line for mingw-w64");
1540        let at = |flag: &str| args.iter().position(|arg| arg == flag).expect(flag);
1541        assert_eq!(args[at("-m") + 1], "i386pep");
1542        assert_eq!(args[at("--subsystem") + 1], "console");
1543        assert!(args.iter().any(|arg| arg.ends_with("libmsvcrt.a")), "{args:?}");
1544        // And the prefixed name a distribution files its mingw binutils under, which is not the
1545        // multiarch one.
1546        let names = cross_order(target);
1547        assert_eq!(names.first().map(String::as_str), Some("ld.lld"));
1548        assert!(names.contains(&"x86_64-w64-mingw32-ld".to_owned()), "{names:?}");
1549    }
1550
1551    #[test]
1552    fn our_runtime_comes_from_beside_the_compiler_rather_than_from_inside_the_sysroot() {
1553        // The two halves of tamnd/rucc#1514. The line used to name it under the sysroot's `lib`,
1554        // where nothing ever put it: it is this compiler's output for the target and a sysroot
1555        // fetched from a release holds the platform's files and not ours. So the path on the line
1556        // is the one the driver found, and the only `librucc_builtins.a` on the line is that one.
1557        let target = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
1558        let sysroot = a_sysroot(target);
1559        let opts = cached();
1560        let args = cross_line(target, &opts, &one("a.o"), "a.exe", &sysroot).expect("a line");
1561        let ours: Vec<&String> =
1562            args.iter().filter(|arg| arg.ends_with("librucc_builtins.a")).collect();
1563        assert_eq!(ours.len(), 1, "{args:?}");
1564        assert_eq!(ours[0], &opts.prefixes[0].join("librucc_builtins.a").display().to_string());
1565        assert!(!ours[0].starts_with(&sysroot.lib().display().to_string()), "{args:?}");
1566        // And it is still last, after everything that calls into it.
1567        assert_eq!(args.last(), Some(ours[0]), "{args:?}");
1568    }
1569
1570    #[test]
1571    fn a_cross_link_with_no_runtime_to_find_says_which_command_writes_one() {
1572        // What the linker would say instead is that `___chkstk_ms` is undefined, referenced from
1573        // mingw-w64's own objects, which is tamnd/rucc#1513 and is nobody's first guess at a
1574        // missing archive.
1575        let target = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
1576        let opts = LinkOptions { prefixes: Vec::new(), ..cached() };
1577        let error = cross_line(target, &opts, &one("a.o"), "a.exe", &a_sysroot(target))
1578            .expect_err("there is no runtime for it to find");
1579        let Error::Cross { why } = &error else { panic!("{error:?}") };
1580        assert!(why.contains("cargo xtask builtins"), "{why}");
1581        assert!(why.contains("-fno-builtins-lib"), "{why}");
1582
1583        // And that flag is the way through it, for somebody who meant to link without ours.
1584        let without = LinkOptions { no_builtins_lib: true, ..opts };
1585        let args = cross_line(target, &without, &one("a.o"), "a.exe", &a_sysroot(target))
1586            .expect("a line without ours on it");
1587        assert!(!args.iter().any(|arg| arg.ends_with("librucc_builtins.a")), "{args:?}");
1588    }
1589
1590    #[test]
1591    fn the_version_an_lld_prints_is_read_and_nothing_elses_is() {
1592        // What each of these programs actually prints, because the word being in the line is the
1593        // whole of how one is told from another.
1594        assert_eq!(lld_major("LLD 18.1.8 (compatible with GNU linkers)\n"), Some(18));
1595        assert_eq!(lld_major("Ubuntu LLD 18.1.3 (compatible with GNU linkers)\n"), Some(18));
1596        assert_eq!(lld_major("LLD 20.1.2 (compatible with GNU linkers)\n"), Some(20));
1597
1598        // Binutils and mold do not have it, and neither of them has this problem, so the answer
1599        // for both is that this check has nothing to say about them.
1600        assert_eq!(lld_major("GNU ld (GNU Binutils for Ubuntu) 2.42\n"), None);
1601        assert_eq!(lld_major("mold 2.4.1 (compatible with GNU ld)\n"), None);
1602        assert_eq!(lld_major(""), None);
1603    }
1604
1605    /// A program that prints `text` and exits, which is as much of a linker as this check reads.
1606    ///
1607    /// Named after what it says, so that two of them in one test are two files.
1608    #[cfg(unix)]
1609    fn a_linker_that_says(tag: &str, text: &str) -> Linker {
1610        use std::os::unix::fs::PermissionsExt as _;
1611        let dir = std::env::temp_dir().join(format!("rucc-link-ld-{}", std::process::id()));
1612        fs::create_dir_all(&dir).expect("a temporary directory");
1613        let path = dir.join(format!("ld.lld-{tag}"));
1614        fs::write(&path, format!("#!/bin/sh\necho '{text}'\n")).expect("a script");
1615        fs::set_permissions(&path, fs::Permissions::from_mode(0o755)).expect("an executable one");
1616        Linker { name: "ld.lld".to_owned(), path }
1617    }
1618
1619    #[test]
1620    #[cfg(unix)]
1621    fn an_lld_too_old_to_read_exportas_is_refused_for_windows_gnu_and_nowhere_else() {
1622        // The failure this replaces has no diagnostic at all: 18 writes an import by ordinal zero,
1623        // exits successfully, and the program dies at startup under wine. tamnd/rucc#1515.
1624        let windows = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
1625        let old = a_linker_that_says("18", "LLD 18.1.8 (compatible with GNU linkers)");
1626        let error = suitable(windows, &old).expect_err("18 cannot link this");
1627        let Error::TooOld { name, found, target } = &error else { panic!("{error:?}") };
1628        assert_eq!((name.as_str(), *found, target.as_str()), ("ld.lld", 18, "x86_64-windows-gnu"));
1629        assert!(error.to_string().contains("IMPORT_NAME_EXPORTAS"), "{error}");
1630
1631        // The same linker for a target whose import libraries have no such records in them, which
1632        // is every other target, since this is one encoding in one format.
1633        let linux = Triple::new(Arch::X86_64, Os::Linux, Env::Musl);
1634        assert_eq!(suitable(linux, &old), Ok(()));
1635
1636        // And the first one that reads them.
1637        let new = a_linker_that_says("19", "LLD 19.1.0 (compatible with GNU linkers)");
1638        assert_eq!(suitable(windows, &new), Ok(()));
1639    }
1640
1641    #[test]
1642    #[cfg(unix)]
1643    fn a_linker_that_will_not_say_what_it_is_is_left_alone() {
1644        // Every linker that is not an lld reaches this check too, and what it can establish is
1645        // that a specific old lld is here rather than that anything else is fit. Turning "I did
1646        // not recognise this" into a refusal would break machines this problem never touched.
1647        let windows = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
1648        let quiet = a_linker_that_says("gnu", "GNU ld (GNU Binutils for Ubuntu) 2.42");
1649        assert_eq!(suitable(windows, &quiet), Ok(()));
1650
1651        let missing = Linker { name: "ld.lld".to_owned(), path: PathBuf::from("/no/such/linker") };
1652        assert_eq!(suitable(windows, &missing), Ok(()));
1653    }
1654
1655    #[test]
1656    fn profiling_a_cross_link_is_refused_because_the_startup_file_is_compiled_code() {
1657        let target = Triple::new(Arch::X86_64, Os::Linux, Env::Musl);
1658        let opts = LinkOptions { profile: true, ..cached() };
1659        let error = cross_line(target, &opts, &one("a.o"), "a.out", &a_sysroot(target))
1660            .expect_err("there is no gcrt1.o in a generated sysroot");
1661        let Error::Cross { why } = &error else { panic!("{error:?}") };
1662        assert!(why.contains("gcrt1.o"), "{why}");
1663    }
1664
1665    #[test]
1666    fn the_host_takes_the_host_line_and_a_tree_the_user_named_takes_it_too() {
1667        let host = Triple::new(Arch::X86_64, Os::Linux, Env::Gnu);
1668        let other = Triple::new(Arch::Riscv64, Os::Linux, Env::Musl);
1669        assert!(cross_for(host, &cached(), Some(host)).is_none());
1670        assert!(cross_for(other, &cached(), Some(host)).is_some());
1671        // A tree somebody assembled and named is what `--sysroot` has always meant here, and the
1672        // native line prefixes every path it decides with it.
1673        let named = LinkOptions { sysroot: Some(PathBuf::from("/opt/root")), ..cached() };
1674        assert!(cross_for(other, &named, Some(host)).is_none());
1675        // A host this compiler cannot name is a host whose directories it should not be guessing at.
1676        assert!(cross_for(other, &cached(), None).is_some());
1677    }
1678
1679    #[test]
1680    fn a_pinned_release_on_this_machines_own_target_is_a_cross_compile() {
1681        // The case that used to be dropped on the floor. `--target=x86_64-linux-gnu.2.28` on an
1682        // x86-64 glibc machine read that machine's headers and linked that machine's libc, and the
1683        // release reached nothing, so what came out was a binary for whatever release the build
1684        // machine happened to have. A pin is the one thing a person writes to say otherwise.
1685        let host = Triple::new(Arch::X86_64, Os::Linux, Env::Gnu);
1686        let pinned = LinkOptions {
1687            pinned: Some(
1688                "x86_64-linux-gnu.2.28".parse::<TargetTuple>().expect("a spelling with a release"),
1689            ),
1690            ..cached()
1691        };
1692        let at = cross_for(host, &pinned, Some(host)).expect("a pin is a cross compile");
1693        // And against the release's own directory, because the release is in the cache key: a tree
1694        // produced for 2.28 and a tree produced for 2.44 are two trees and the path has to say which.
1695        assert!(at.root().ends_with("x86_64-linux-gnu.2.28"), "{:?}", at.root());
1696        // The release is the whole of the difference. The same command line without it is this
1697        // machine, which is what every native compile has always been.
1698        let bare = LinkOptions { pinned: None, ..cached() };
1699        assert!(cross_for(host, &bare, Some(host)).is_none());
1700    }
1701
1702    #[test]
1703    fn what_a_cross_link_searches_is_the_sysroot_and_not_this_machine() {
1704        let dirs = search_dirs(&cached(), foreign());
1705        // One directory, because that is what the line has, and the same one the line has, because
1706        // `-print-search-dirs` is what a build system reads to write a link line of its own.
1707        assert_eq!(dirs.len(), 1, "{dirs:?}");
1708        assert!(dirs[0].starts_with("/cache"), "{dirs:?}");
1709        assert!(dirs[0].ends_with("lib"), "{dirs:?}");
1710        // And what the user wrote still comes first, the way it does on the line itself.
1711        let mine = LinkOptions { search: vec![PathBuf::from("/opt/mine")], ..cached() };
1712        assert_eq!(search_dirs(&mine, foreign())[0], PathBuf::from("/opt/mine"));
1713    }
1714
1715    #[test]
1716    fn the_linker_looked_for_on_a_cross_link_is_one_that_can_cross() {
1717        let names = cross_order(Triple::new(Arch::Aarch64, Os::Linux, Env::Gnu));
1718        assert_eq!(names.first().map(String::as_str), Some("ld.lld"));
1719        assert!(names.contains(&"aarch64-linux-gnu-ld".to_owned()), "{names:?}");
1720        // mold links for the machine it is running on, and so does a distribution's own `ld`, so
1721        // neither is a default here. `-fuse-ld=` is still there for somebody whose is different.
1722        assert!(!names.iter().any(|name| name.contains("mold")), "{names:?}");
1723        assert!(!names.contains(&"ld".to_owned()), "{names:?}");
1724        // And the lookup the driver really does for a target that is not this machine.
1725        assert_eq!(order(foreign(), &cached()), ["ld.lld", "lld"]);
1726    }
1727
1728    #[test]
1729    fn the_four_flags_become_the_five_modes_they_describe() {
1730        let plain = LinkOptions::default();
1731        assert_eq!(mode(&plain), LinkMode::Dynamic);
1732        assert_eq!(
1733            mode(&LinkOptions { pie: Some(false), ..plain.clone() }),
1734            LinkMode::DynamicNoPie
1735        );
1736        assert_eq!(mode(&LinkOptions { is_static: true, ..plain.clone() }), LinkMode::Static);
1737        let both = LinkOptions { is_static: true, pie: Some(true), ..plain.clone() };
1738        assert_eq!(mode(&both), LinkMode::StaticPie);
1739        assert_eq!(mode(&LinkOptions { shared: true, ..plain }), LinkMode::Shared);
1740    }
1741
1742    #[test]
1743    fn a_sysroot_that_has_not_been_built_is_named_before_anything_is_compiled() {
1744        let opts = LinkOptions {
1745            cache: Some(std::env::temp_dir().join("rucc-a-cache-nobody-filled")),
1746            ..LinkOptions::default()
1747        };
1748        let error = preflight(foreign(), &opts).expect_err("nothing has built one");
1749        let Error::Sysroot { dir, pinned, .. } = &error else { panic!("{error:?}") };
1750        assert!(dir.ends_with("x86_64-none"), "{dir}");
1751        // Nothing is pinned for that target, or for any target yet, so the message says that rather
1752        // than naming a command that would not work.
1753        assert!(!pinned, "nothing should be pinned for a bare metal target");
1754        let said = error.to_string();
1755        assert!(said.contains("pins none for it to fetch"), "{said}");
1756    }
1757
1758    /// The other half of the same message, which is what a target this release does pin an artifact
1759    /// for is told. Built by hand rather than through `preflight`, because what is being checked is
1760    /// the message and not which targets `rucc_sysroot::artifact` happens to pin this release.
1761    #[test]
1762    fn a_sysroot_that_could_be_fetched_is_told_what_to_run() {
1763        let said = Error::Sysroot {
1764            target: "x86_64-linux-musl".to_owned(),
1765            dir: "/somewhere/sysroots/x86_64-linux-musl".to_owned(),
1766            pinned: true,
1767        }
1768        .to_string();
1769        assert!(said.contains("`rucc --fetch x86_64-linux-musl`"), "{said}");
1770        // And the other way out of it, because a person who has a tree already does not want a
1771        // download.
1772        assert!(said.contains("--sysroot=<dir>"), "{said}");
1773    }
1774
1775    #[test]
1776    fn a_link_against_this_machine_has_nothing_to_check_before_it_starts() {
1777        // Its directories are looked for as the line is built, and one that is not there is simply
1778        // one that is not offered, so there is no question to answer early.
1779        assert!(preflight(linux(), &LinkOptions::default()).is_ok());
1780    }
1781
1782    #[test]
1783    fn a_runtime_directory_that_is_not_on_this_machine_is_not_offered() {
1784        let dirs = runtime_dirs(linux(), Some(Path::new("/definitely/not/a/sysroot")));
1785        assert!(dirs.is_empty(), "{dirs:?}");
1786    }
1787}