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