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_target(target_tuple(target, opts)).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/// Writes the stub libraries a glibc cross link reads, into [`Sysroot::stubs`].
574///
575/// `spec/cross-compile/09-libc-stubs.md` section 9.1: the stubs are generated on demand rather than
576/// shipped, out of the description `rucc-stub` carries, cut at the release the tuple names or at
577/// the bundled one when it names none. So there is nothing to fetch for them and nothing to go
578/// stale, and a pin is a different directory rather than a different download.
579///
580/// A file is written only when its bytes differ from what is there, and then through a temporary
581/// name and a rename, because two builds for one target run side by side all the time and a
582/// linker must never read a half written `libc.so`. The bytes are the same on every host, so two
583/// processes racing to write them race to write the same thing.
584///
585/// Nothing for a link against this machine, a `--sysroot` the user named, or a target that is not
586/// glibc. A glibc release newer than the bundled tree was already refused when the headers were
587/// chosen, so it is quietly nothing here too.
588///
589/// # Errors
590///
591/// [`Error::Cross`] when the stubs cannot be generated for this target or cannot be written.
592pub fn write_stubs(target: Triple, opts: &LinkOptions) -> Result<(), Error> {
593 let Some(sysroot) = cross_sysroot(target, opts) else { return Ok(()) };
594 let tuple = sysroot.target();
595 if rucc_stub::glibc::architecture(tuple).is_none() {
596 return Ok(());
597 }
598 let Ok(Some(minor)) = rucc_sysroot::bundled_glibc_minor(tuple) else { return Ok(()) };
599 let files = rucc_stub::glibc::stubs(tuple, minor).map_err(|why| Error::Cross {
600 why: format!("the glibc stubs for {}: {why}", tuple.to_canonical_string()),
601 })?;
602 let dir = sysroot.stubs();
603 let failed = |path: &Path, why: std::io::Error| Error::Cross {
604 why: format!("{} cannot be written: {why}", path.display()),
605 };
606 fs::create_dir_all(dir).map_err(|why| failed(dir, why))?;
607 for file in files {
608 let path = dir.join(&file.name);
609 if fs::read(&path).is_ok_and(|there| there == file.bytes) {
610 continue;
611 }
612 let temporary = dir.join(format!(".{}.{}", file.name, std::process::id()));
613 fs::write(&temporary, &file.bytes).map_err(|why| failed(&temporary, why))?;
614 fs::rename(&temporary, &path).map_err(|why| {
615 let _ = fs::remove_file(&temporary);
616 failed(&path, why)
617 })?;
618 }
619 Ok(())
620}
621
622/// The linker to use, looked for where a linker is.
623///
624/// `-B` prefixes first, since the point of one is to put a toolchain in front of the machine's,
625/// then the path. A name that contains a separator is a path and is taken as one, which is what
626/// gcc does with `-fuse-ld=/usr/bin/ld.gold` and what a build system relying on that expects.
627///
628/// # Errors
629///
630/// [`Error::Named`] when `-fuse-ld=` asked for one that is not here, and [`Error::NoLinker`] when
631/// nothing was, which name the candidates so that the message says what was looked for.
632pub fn find(target: Triple, opts: &LinkOptions) -> Result<Linker, Error> {
633 let tried = order(target, opts);
634 for name in &tried {
635 if name.contains(std::path::MAIN_SEPARATOR) || name.contains('/') {
636 let path = PathBuf::from(name);
637 if path.is_file() {
638 return Ok(Linker { name: name.clone(), path });
639 }
640 continue;
641 }
642 for dir in &opts.prefixes {
643 let path = dir.join(name);
644 if path.is_file() {
645 return Ok(Linker { name: name.clone(), path });
646 }
647 }
648 if let Some(path) = on_path(name) {
649 return Ok(Linker { name: name.clone(), path });
650 }
651 }
652 match &opts.use_ld {
653 Some(name) => Err(Error::Named { name: name.clone() }),
654 None => Err(Error::NoLinker { tried }),
655 }
656}
657
658/// The first lld that reads `IMPORT_NAME_EXPORTAS`, which is what a windows-gnu link needs.
659///
660/// 18 does not read it and does not say so, so the number is not a convenience: below it the
661/// answer is wrong rather than absent. tamnd/rucc#1515.
662pub const LLD_EXPORTAS: u32 = 19;
663
664/// Whether a found linker can do this target's link, asked before it is handed anything.
665///
666/// Section 11.6's rule is that suitable is checked and not assumed, and this is the one check that
667/// cannot be made by looking at a file. A windows-gnu link reads import libraries that mingw-w64's
668/// `==` aliases compiled into `IMPORT_NAME_EXPORTAS` records, which lld reads from
669/// [`LLD_EXPORTAS`] on. An older lld writes an import by ordinal zero instead, without a warning
670/// and with a successful exit, so nothing later in the toolchain has anything to notice: the
671/// program is wrong at startup and the link that made it said nothing. Ubuntu 24.04 is the current
672/// LTS and ships 18, so the machine this happens on is an ordinary one.
673///
674/// Every other target is left alone, and so is anything that is not an lld, because this is the one
675/// version of the one linker that is known to answer wrongly rather than not at all.
676///
677/// A linker that will not run or whose version cannot be read is allowed through. What the check
678/// can establish is that a specific old lld is here, and it should not turn every unusual linker
679/// into a refusal on the strength of failing to recognise it.
680///
681/// # Errors
682///
683/// [`Error::TooOld`] when the linker is an lld older than [`LLD_EXPORTAS`] and the target is
684/// windows-gnu.
685pub fn suitable(target: Triple, linker: &Linker) -> Result<(), Error> {
686 if (target.os, target.env) != (Os::Windows, Env::Gnu) {
687 return Ok(());
688 }
689 let Some(found) = lld_major(&reported_version(&linker.path)) else { return Ok(()) };
690 if found >= LLD_EXPORTAS {
691 return Ok(());
692 }
693 Err(Error::TooOld {
694 name: linker.name.clone(),
695 found,
696 target: target.tuple().to_canonical_string(),
697 })
698}
699
700/// What `<linker> --version` prints, or an empty string when it will not say.
701///
702/// A linker that cannot be started is not this function's problem to report, because the link is
703/// about to start it again and say so properly. What this returns for such a one is nothing to
704/// read, which is the same as a linker that ran and said something unrecognisable.
705fn reported_version(path: &Path) -> String {
706 let Ok(out) = Command::new(path).arg("--version").output() else { return String::new() };
707 String::from_utf8_lossy(&out.stdout).into_owned()
708}
709
710/// The major version in an lld's `--version`, when the program that printed it was an lld.
711///
712/// What lld prints is `LLD 18.1.8 (compatible with GNU linkers)`, with a distribution's own prefix
713/// in front of it often enough that the word is looked for rather than the line starting with it:
714/// Ubuntu's says `Ubuntu LLD 18.1.3`. Binutils prints `GNU ld (GNU Binutils for Ubuntu) 2.42` and
715/// mold prints its own name, and neither has the word, so both come back as [`None`] and are left
716/// alone.
717fn lld_major(text: &str) -> Option<u32> {
718 let mut words = text.split_whitespace();
719 words.find(|word| *word == "LLD")?;
720 words.next()?.split('.').next()?.parse().ok()
721}
722
723/// The first executable of that name on `PATH`.
724///
725/// Executability is checked rather than assumed, because a directory of that name on `PATH` is
726/// not a thing to try to run and neither is a file nobody may execute.
727fn on_path(name: &str) -> Option<PathBuf> {
728 let path = std::env::var_os("PATH")?;
729 std::env::split_paths(&path).map(|dir| dir.join(name)).find(|p| executable(p))
730}
731
732/// Whether a path is a file this process could run.
733#[cfg(unix)]
734fn executable(path: &Path) -> bool {
735 use std::os::unix::fs::PermissionsExt as _;
736 path.metadata().is_ok_and(|m| m.is_file() && m.permissions().mode() & 0o111 != 0)
737}
738
739/// Whether a path is a file this process could run.
740///
741/// Windows has no executable bit and decides by extension, and the names looked for above carry
742/// theirs, so being a file is the whole of the question here.
743#[cfg(not(unix))]
744fn executable(path: &Path) -> bool {
745 path.is_file()
746}
747
748/// What the linker is told, in order, not counting the linker itself.
749///
750/// Two lines and [`cross_sysroot`] picks which: the one above for this machine, and
751/// [`rucc_sysroot::argv`]'s for any other. Nothing about the machine is read on the second path, so
752/// `-###` prints the same line on every host and prints it whether the sysroot has been built or
753/// not, which is what makes it worth printing.
754///
755/// # Errors
756///
757/// [`Error::Target`] for a platform there is no native line for yet, which is every one but Linux,
758/// and [`Error::Cross`] for a cross link that cannot be produced at all.
759pub fn line(
760 target: Triple,
761 opts: &LinkOptions,
762 items: &[Item],
763 output: &str,
764) -> Result<Vec<String>, Error> {
765 if let Some(sysroot) = cross_sysroot(target, opts) {
766 return cross_line(target, opts, items, output, &sysroot);
767 }
768 if target.os != Os::Linux {
769 return Err(Error::Target { triple: target.to_string() });
770 }
771 let machine = emulation(target);
772 let root = opts.sysroot.as_deref();
773 let dirs = library_dirs(target, root);
774 // Where a gcc on this machine keeps its own runtime, which is a different place from where
775 // the C library keeps its own, and where our runtime is if it was built for this target.
776 let runtime = runtime_dirs(target, root);
777 let ours = if opts.no_builtins_lib { None } else { builtins_archive(target, &opts.prefixes) };
778 let mut args = vec![
779 "-o".to_owned(),
780 output.to_owned(),
781 // Which of the several formats one `ld` can write is meant. A linker built for more than
782 // one machine guesses from its first input otherwise, and a link of no objects at all has
783 // nothing to guess from.
784 "-m".to_owned(),
785 machine.to_owned(),
786 // The table a program unwinds through, which a C program with no exceptions in it still
787 // needs because `backtrace` and every crash handler read it.
788 "--eh-frame-hdr".to_owned(),
789 // The symbol hash a dynamic loader from this century reads. The old one is still written
790 // alongside by default on some distributions, and asking for this one is what stops a link
791 // from carrying a table nothing has needed since 2006.
792 "--hash-style=gnu".to_owned(),
793 ];
794
795 let pie = opts.pie.unwrap_or(!opts.is_static && !opts.shared);
796 if opts.shared {
797 args.push("-shared".to_owned());
798 } else if opts.is_static {
799 args.push("-static".to_owned());
800 } else if pie {
801 args.push("-pie".to_owned());
802 } else {
803 args.push("-no-pie".to_owned());
804 }
805 if !opts.is_static && !opts.shared {
806 args.push("-dynamic-linker".to_owned());
807 args.push(target_path(root, loader(target)));
808 }
809 if opts.export_dynamic {
810 args.push("--export-dynamic".to_owned());
811 }
812 if opts.strip {
813 args.push("-s".to_owned());
814 }
815
816 if opts.wants_startfiles() {
817 for name in startfile(opts, pie).into_iter().chain(["crti.o"]) {
818 if let Some(path) = find_file(&dirs, name) {
819 args.push(path.display().to_string());
820 }
821 }
822 // The compiler's own startup file, which runs the static constructors. Three spellings
823 // of the same thing, and which one is right is about how the code in it refers to
824 // itself: `S` for a position independent result, `T` for a static one, plain for the
825 // rest. Skipped when there is no gcc on the machine to take it from, because a program
826 // with no constructor in it does not miss it.
827 let begin = if opts.shared || pie {
828 "crtbeginS.o"
829 } else if opts.is_static {
830 "crtbeginT.o"
831 } else {
832 "crtbegin.o"
833 };
834 if let Some(path) = find_file(&runtime, begin).or_else(|| find_file(&runtime, "crtbegin.o"))
835 {
836 args.push(path.display().to_string());
837 }
838 }
839
840 for dir in &opts.search {
841 args.push(format!("-L{}", dir.display()));
842 }
843 for dir in &dirs {
844 args.push(format!("-L{}", dir.display()));
845 }
846 // Where `libgcc.a` and `libgcc_eh.a` are, which is not where the C library is. Nothing is
847 // added when there is no gcc on the machine, and then the `-l` names below are left off too.
848 for dir in &runtime {
849 args.push(format!("-L{}", dir.display()));
850 }
851
852 for item in items {
853 match item {
854 Item::File(path) => args.push(path.clone()),
855 Item::Library(name) => args.push(format!("-l{name}")),
856 Item::Linker(arg) => args.push(arg.clone()),
857 }
858 }
859 // After the objects, because a static archive is searched for what is undefined at the point
860 // it is reached and a library named before the object that needs it contributes nothing.
861 args.extend(runtime_items(opts, &runtime, ours.as_deref()));
862
863 if opts.wants_startfiles() {
864 // The other end of `crtbegin`, and it goes before `crtn.o` for the same reason `crti.o`
865 // goes before `crtbegin`: the four are two nested pairs and not four separate files.
866 let end = if opts.shared || pie { "crtendS.o" } else { "crtend.o" };
867 if let Some(path) = find_file(&runtime, end).or_else(|| find_file(&runtime, "crtend.o")) {
868 args.push(path.display().to_string());
869 }
870 if let Some(path) = find_file(&dirs, "crtn.o") {
871 args.push(path.display().to_string());
872 }
873 }
874
875 Ok(args)
876}
877
878/// The startup file the C library brings, or `None` for a link that calls nothing.
879///
880/// This is what calls `main` and what passes it the arguments, so a shared object takes none of
881/// them: nothing starts one and it has no `main` to be started at. `Scrt1.o` rather than `crt1.o`
882/// when the result moves, because the two differ in whether the reference to `main` in them is one
883/// a loader may relocate.
884///
885/// A profiled program gets a different one again, which does all of that and starts and stops the
886/// counting around it. There are two of those rather than three: the one that relocates itself is
887/// only needed by a static position independent link, and every other link takes the plain one,
888/// which is what gcc does with the same flag.
889fn startfile(opts: &LinkOptions, pie: bool) -> Option<&'static str> {
890 if opts.shared {
891 None
892 } else if opts.profile {
893 Some(if pie && opts.is_static { "grcrt1.o" } else { "gcrt1.o" })
894 } else if pie {
895 Some("Scrt1.o")
896 } else {
897 Some("crt1.o")
898 }
899}
900
901/// The libraries the compiler's own runtime contributes, in the order the linker wants them.
902///
903/// The C library first, then ours, then the machine's `libgcc`. Order inside this list is not
904/// about whether a symbol resolves, it is about which archive supplies one that more than one of
905/// them defines, and the two places that happens both have a right answer.
906///
907/// `memcpy` and its three neighbours are in the C library on a hosted target and in ours only for
908/// a freestanding one, which is what `spec/12-abi-and-runtime.md` section 12.8 says they are for.
909/// glibc's are written in assembly per microarchitecture and ours is a word at a time loop, so a
910/// link that took ours over glibc's would be slower at the one routine every program reaches.
911///
912/// The wide arithmetic is in ours and in `libgcc` both, and the two are ABI-identical on purpose,
913/// so which one answers is not a correctness question. Ours comes first because it is ours, and
914/// `-fno-builtins-lib` leaves it off for somebody who would rather it were not.
915///
916/// A static link puts the whole list inside `--start-group`. `libc.a` refers to `_Unwind_Resume`,
917/// and the unwinder refers back into `libc.a`, so a linker walking the list once resolves
918/// whichever it reaches first and reports the other as undefined. That is exactly the failure
919/// issue #277 describes and the group is the fix for it.
920///
921/// A dynamic link needs no group, because the shared `libc` resolves its own references inside
922/// itself. `libgcc_s` is asked for `--as-needed` there, the way gcc asks for it, so a program that
923/// never unwinds does not acquire a dependency on it.
924fn runtime_items(opts: &LinkOptions, runtime: &[PathBuf], ours: Option<&Path>) -> Vec<String> {
925 let mut args = Vec::new();
926 if !opts.wants_defaultlibs() && !opts.wants_runtime() {
927 return args;
928 }
929 // Only when there is a gcc to take them from. On a machine without one the names would be an
930 // error about a library that was never going to be there, and a program that needs neither
931 // the unwinder nor a wide divide links and runs without them.
932 let has_gcc = find_file(runtime, "libgcc.a").is_some();
933
934 if opts.is_static {
935 args.push("--start-group".to_owned());
936 }
937 if opts.wants_defaultlibs() {
938 args.push("-lc".to_owned());
939 }
940 if opts.wants_runtime() {
941 if let Some(path) = ours {
942 args.push(path.display().to_string());
943 }
944 if has_gcc {
945 args.push("-lgcc".to_owned());
946 if opts.is_static {
947 args.push("-lgcc_eh".to_owned());
948 }
949 }
950 }
951 if opts.is_static {
952 args.push("--end-group".to_owned());
953 } else if opts.wants_runtime() && has_gcc {
954 // The shared half, and only if something still wants it after everything above.
955 args.push("--as-needed".to_owned());
956 args.push("-lgcc_s".to_owned());
957 args.push("--no-as-needed".to_owned());
958 }
959 args
960}
961
962/// Where a gcc on this machine keeps `crtbegin.o`, `crtend.o` and `libgcc.a`, newest first.
963///
964/// This is not where the C library's files are. A distribution puts them under a directory named
965/// for the gcc version, and there may be several, so the answer is every one that exists with the
966/// highest version in front. Newest first because a newer `libgcc` is a superset of an older one
967/// and because that is the one the C library on the same machine was built against.
968#[must_use]
969pub fn runtime_dirs(target: Triple, sysroot: Option<&Path>) -> Vec<PathBuf> {
970 let libc = match target.env {
971 Env::Musl => "musl",
972 Env::None | Env::Gnu | Env::Msvc => "gnu",
973 };
974 let arch = target.arch.as_str();
975 // The spellings the distributions use for the same triple. Debian and Ubuntu drop the vendor
976 // field, the source builds and Arch keep `pc`, and Red Hat and SUSE write their own name in
977 // it, so all of them are looked for and the ones that are there are taken.
978 let names = [
979 format!("{arch}-linux-{libc}"),
980 format!("{arch}-pc-linux-{libc}"),
981 format!("{arch}-redhat-linux"),
982 format!("{arch}-suse-linux"),
983 format!("{arch}-alpine-linux-{libc}"),
984 ];
985 let mut found = Vec::new();
986 for base in ["/usr/lib/gcc", "/usr/lib64/gcc", "/usr/local/lib/gcc"] {
987 for name in &names {
988 let dir = under(sysroot, &format!("{base}/{name}"));
989 let Ok(entries) = fs::read_dir(&dir) else { continue };
990 let mut versions: Vec<(Vec<u64>, PathBuf)> = entries
991 .flatten()
992 .map(|e| e.path())
993 .filter(|p| p.is_dir())
994 .map(|p| (version_key(&p), p))
995 .collect();
996 // Descending, so the highest version is the first place `find_file` looks. Ties keep
997 // the order the directory gave, which is arbitrary and does not matter because two
998 // directories that sort the same hold the same version.
999 versions.sort_by(|a, b| b.0.cmp(&a.0));
1000 found.extend(versions.into_iter().map(|(_, path)| path));
1001 }
1002 }
1003 found
1004}
1005
1006/// A directory name read as a version, so that `13` sorts above `9` and `10.2` above `10`.
1007///
1008/// A name that is not a version at all sorts below every name that is, rather than being left
1009/// out, because a directory holding a `libgcc.a` is worth looking in whatever it is called.
1010fn version_key(dir: &Path) -> Vec<u64> {
1011 let name = dir.file_name().unwrap_or_default().to_string_lossy();
1012 name.split('.').map(|part| part.parse::<u64>().unwrap_or(0)).collect()
1013}
1014
1015/// Our own runtime library for this target, if it was built.
1016///
1017/// Looked for beside the compiler rather than at a path decided when the compiler was built, for
1018/// the same reason everything else here is looked for: one binary runs wherever it is copied. A
1019/// `-B` prefix is asked first, because that is what a `-B` prefix is for.
1020#[must_use]
1021pub fn builtins_archive(target: Triple, prefixes: &[PathBuf]) -> Option<PathBuf> {
1022 // The name from the crate that puts it on a line, rather than a second spelling of it here,
1023 // which is what that constant asks of anybody who needs the name.
1024 const NAME: &str = rucc_sysroot::link::BUILTINS;
1025 // The four field triple first and then the tuple the sysroots are named by, because `cargo
1026 // xtask builtins --target=aarch64-linux-musl` names its directory after what it was given, and
1027 // that shorter spelling is the one people type.
1028 let spellings = [target.to_string(), target.tuple().to_string()];
1029 let mut places: Vec<PathBuf> = Vec::new();
1030 for prefix in prefixes {
1031 for triple in &spellings {
1032 places.push(prefix.join(triple).join(NAME));
1033 }
1034 places.push(prefix.join(NAME));
1035 }
1036 if let Some(dir) =
1037 std::env::current_exe().ok().and_then(|exe| exe.parent().map(Path::to_path_buf))
1038 {
1039 if let Some(up) = dir.parent() {
1040 for triple in &spellings {
1041 // An install: the compiler in `bin` and its runtime in `lib/rucc/<triple>`.
1042 places.push(up.join("lib").join("rucc").join(triple).join(NAME));
1043 // A build tree: the compiler in `target/release` and the runtime, which is built
1044 // for the target and not the host, in `target/<triple>/release`.
1045 for profile in ["release", "debug"] {
1046 places.push(up.join(triple).join(profile).join(NAME));
1047 }
1048 }
1049 }
1050 places.push(dir.join(NAME));
1051 }
1052 places.into_iter().find(|path| path.is_file())
1053}
1054
1055/// Which output format this `ld` should write, in the name `ld` knows it by.
1056fn emulation(target: Triple) -> &'static str {
1057 match target.arch {
1058 Arch::X86_64 => "elf_x86_64",
1059 Arch::Aarch64 => "aarch64linux",
1060 Arch::Riscv64 => "elf64lriscv",
1061 }
1062}
1063
1064/// The program that starts a dynamically linked program, whose path is part of the file.
1065///
1066/// It is a per-target constant rather than something to look for, because the name is fixed by
1067/// the platform's ABI and a program naming a different one does not start.
1068fn loader(target: Triple) -> &'static str {
1069 match (target.arch, target.env) {
1070 (Arch::X86_64, Env::Musl) => "/lib/ld-musl-x86_64.so.1",
1071 (Arch::X86_64, _) => "/lib64/ld-linux-x86-64.so.2",
1072 (Arch::Aarch64, Env::Musl) => "/lib/ld-musl-aarch64.so.1",
1073 (Arch::Aarch64, _) => "/lib/ld-linux-aarch64.so.1",
1074 (Arch::Riscv64, Env::Musl) => "/lib/ld-musl-riscv64.so.1",
1075 (Arch::Riscv64, _) => "/lib/ld-linux-riscv64-lp64d.so.1",
1076 }
1077}
1078
1079/// Where the library's own files might be, in search order.
1080///
1081/// The multiarch directory first for the reason it comes first in the header search: it is where
1082/// a distribution that can hold two architectures at once puts the one being asked for, and a
1083/// distribution that cannot simply does not have it. `lib64` after it, which is what the
1084/// distributions that split by word size use instead, and `lib` last, which is every other one.
1085#[must_use]
1086pub fn candidates(target: Triple, sysroot: Option<&Path>) -> Vec<PathBuf> {
1087 let multiarch = multiarch(target);
1088 [
1089 format!("/usr/lib/{multiarch}"),
1090 format!("/lib/{multiarch}"),
1091 "/usr/lib64".to_owned(),
1092 "/lib64".to_owned(),
1093 "/usr/lib".to_owned(),
1094 "/lib".to_owned(),
1095 ]
1096 .into_iter()
1097 .map(|dir| under(sysroot, &dir))
1098 .collect()
1099}
1100
1101/// The name a distribution that holds two architectures at once files this target under.
1102///
1103/// `x86_64-linux-gnu` and its friends, which is what `gcc -print-multiarch` prints and what a
1104/// build system pastes into a path when it is looking for a library itself.
1105#[must_use]
1106pub fn multiarch(target: Triple) -> String {
1107 let libc = match target.env {
1108 Env::Musl => "musl",
1109 Env::None | Env::Gnu | Env::Msvc => "gnu",
1110 };
1111 format!("{}-linux-{libc}", target.arch.as_str())
1112}
1113
1114/// The candidates that are there.
1115fn library_dirs(target: Triple, sysroot: Option<&Path>) -> Vec<PathBuf> {
1116 candidates(target, sysroot).into_iter().filter(|dir| dir.is_dir()).collect()
1117}
1118
1119/// Where a library is looked for, in the order it is looked for in.
1120///
1121/// The command line first and the target's own after it, which is the order the linker is handed
1122/// and therefore the order `-print-search-dirs` has to print.
1123#[must_use]
1124pub fn search_dirs(link: &LinkOptions, target: Triple) -> Vec<PathBuf> {
1125 let mut dirs = link.search.clone();
1126 // A cross link searches the sysroot's directory and, for a libc that is a stub, the one its
1127 // stubs are written to, so this is those and not the machine's. What `-print-search-dirs` says is what a build
1128 // system pastes into a link line of its own, and an answer that named `/usr/lib` for a target
1129 // whose link line never goes near it would be worse than no answer at all.
1130 if let Some(sysroot) = cross_sysroot(target, link) {
1131 dirs.push(sysroot.lib());
1132 if rucc_sysroot::link::libc(sysroot.target()) == rucc_sysroot::link::Libc::Stub {
1133 dirs.push(sysroot.stubs().to_path_buf());
1134 }
1135 return dirs;
1136 }
1137 dirs.extend(candidates(target, link.sysroot.as_deref()));
1138 dirs
1139}
1140
1141/// The full path of a file with that name, when one of the search directories holds it.
1142///
1143/// What `-print-file-name=` answers. GCC prints the name back unchanged when it finds nothing,
1144/// which is what makes the flag safe to paste into a link line either way.
1145#[must_use]
1146pub fn find_in_search(link: &LinkOptions, target: Triple, name: &str) -> Option<PathBuf> {
1147 find_file(&search_dirs(link, target), name)
1148}
1149
1150/// The first of those directories holding a file of that name.
1151fn find_file(dirs: &[PathBuf], name: &str) -> Option<PathBuf> {
1152 dirs.iter().map(|dir| dir.join(name)).find(|path| path.is_file())
1153}
1154
1155/// A path under the sysroot, when there is one.
1156fn under(sysroot: Option<&Path>, path: &str) -> PathBuf {
1157 match sysroot {
1158 // `strip_prefix` because joining an absolute path replaces the root rather than extending
1159 // it, which would make every entry the unprefixed one.
1160 Some(root) => root.join(path.strip_prefix('/').unwrap_or(path)),
1161 None => PathBuf::from(path),
1162 }
1163}
1164
1165/// A path on the machine that will run the program, rather than on the one compiling it.
1166///
1167/// Written with the separator of the target and not of the host, which matters for the one path
1168/// that is not looked at here but stored in the file and read by something else later: the loader
1169/// a dynamic program names. A Windows host joining it would put a backslash in the middle of a
1170/// name that a Linux loader has to find, and the program would not start.
1171fn target_path(sysroot: Option<&Path>, path: &str) -> String {
1172 match sysroot {
1173 Some(root) => {
1174 let root = root.display().to_string();
1175 format!("{}/{}", root.trim_end_matches(['/', '\\']), path.trim_start_matches('/'))
1176 }
1177 None => path.to_owned(),
1178 }
1179}
1180
1181/// The whole invocation as one line, quoted the way `-###` prints it.
1182#[must_use]
1183pub fn render(linker: &Linker, args: &[String]) -> String {
1184 let mut out = linker.path.display().to_string();
1185 for arg in args {
1186 out.push(' ');
1187 if arg.is_empty() || arg.contains(char::is_whitespace) {
1188 out.push('"');
1189 out.push_str(arg);
1190 out.push('"');
1191 } else {
1192 out.push_str(arg);
1193 }
1194 }
1195 out
1196}
1197
1198/// Runs the linker and waits for it.
1199///
1200/// # Errors
1201///
1202/// [`Error::Spawn`] when it could not be started, which is a machine problem, and
1203/// [`Error::Refused`] when it ran and said no, which is a program problem and one the linker has
1204/// already explained on its own error output.
1205pub fn run(linker: &Linker, args: &[String]) -> Result<(), Error> {
1206 let args: Vec<OsString> = args.iter().map(OsString::from).collect();
1207 let status = Command::new(&linker.path).args(&args).status().map_err(|why| Error::Spawn {
1208 path: linker.path.display().to_string(),
1209 why: why.to_string(),
1210 })?;
1211 if status.success() {
1212 return Ok(());
1213 }
1214 // Nothing is added to what the linker printed. It has already named the symbol or the file,
1215 // and a second message from here saying that linking failed would only push the first one
1216 // further up the screen.
1217 Err(Error::Refused {
1218 status: match status.code() {
1219 Some(code) => format!("exited with status {code}"),
1220 None => "was killed before it finished".to_owned(),
1221 },
1222 })
1223}
1224
1225#[cfg(test)]
1226mod tests {
1227 use super::*;
1228
1229 fn linux() -> Triple {
1230 Triple::new(Arch::X86_64, Os::Linux, Env::Gnu)
1231 }
1232
1233 fn one(name: &str) -> Vec<Item> {
1234 vec![Item::File(name.to_owned())]
1235 }
1236
1237 #[test]
1238 fn the_fast_one_is_looked_for_first_and_the_platforms_own_last() {
1239 let names = order(linux(), &LinkOptions::default());
1240 assert_eq!(names.first().map(String::as_str), Some("ld.mold"));
1241 assert_eq!(names.last().map(String::as_str), Some("ld"));
1242 }
1243
1244 #[test]
1245 fn naming_one_is_the_whole_of_the_order() {
1246 let opts = LinkOptions { use_ld: Some("gold".to_owned()), ..LinkOptions::default() };
1247 assert_eq!(order(linux(), &opts), ["ld.gold", "gold"]);
1248 }
1249
1250 #[test]
1251 fn a_dynamic_program_names_the_loader_that_will_start_it() {
1252 let args = line(linux(), &LinkOptions::default(), &one("a.o"), "a.out").expect("a line");
1253 let at = args.iter().position(|a| a == "-dynamic-linker").expect("the flag");
1254 assert!(args[at + 1].ends_with("/lib64/ld-linux-x86-64.so.2"), "{args:?}");
1255 }
1256
1257 #[test]
1258 fn a_static_program_names_no_loader_because_nothing_will_start_it() {
1259 let opts = LinkOptions { is_static: true, ..LinkOptions::default() };
1260 let args = line(linux(), &opts, &one("a.o"), "a.out").expect("a line");
1261 assert!(args.contains(&"-static".to_owned()), "{args:?}");
1262 assert!(!args.contains(&"-dynamic-linker".to_owned()), "{args:?}");
1263 }
1264
1265 #[test]
1266 fn the_startup_file_of_a_program_that_moves_is_not_the_one_of_a_program_that_does_not() {
1267 let moving = LinkOptions { pie: Some(true), ..LinkOptions::default() };
1268 let fixed = LinkOptions { pie: Some(false), ..LinkOptions::default() };
1269 let named = |opts: &LinkOptions| {
1270 line(linux(), opts, &one("a.o"), "a.out")
1271 .expect("a line")
1272 .iter()
1273 .filter_map(|a| Path::new(a).file_name().map(|n| n.to_string_lossy().into_owned()))
1274 .find(|n| n.ends_with("crt1.o"))
1275 };
1276 // Only when the machine running this has them, which is what makes this two assertions
1277 // rather than one: a machine with no glibc development files has neither to find.
1278 if let Some(name) = named(&moving) {
1279 assert_eq!(name, "Scrt1.o");
1280 assert_eq!(named(&fixed).as_deref(), Some("crt1.o"));
1281 }
1282 }
1283
1284 /// A profiled program is started by a startup file of its own.
1285 ///
1286 /// The counts it keeps have to be started before `main` runs and written out after it returns,
1287 /// and what does both is this file rather than anything the compiler wrote. So a build that
1288 /// compiles with the flag and links without it produces a program that calls the hook on every
1289 /// function and never writes a profile, which is the failure this is here to keep out.
1290 ///
1291 /// A shared object takes none of them either way, since nothing starts one.
1292 #[test]
1293 fn a_profiled_program_is_started_by_the_startup_file_that_counts() {
1294 let profile = LinkOptions { profile: true, ..LinkOptions::default() };
1295 assert_eq!(startfile(&profile, false), Some("gcrt1.o"));
1296 assert_eq!(startfile(&profile, true), Some("gcrt1.o"));
1297 let still = LinkOptions { is_static: true, ..profile.clone() };
1298 assert_eq!(startfile(&still, true), Some("grcrt1.o"));
1299 assert_eq!(startfile(&still, false), Some("gcrt1.o"));
1300 let shared = LinkOptions { shared: true, ..profile };
1301 assert_eq!(startfile(&shared, false), None);
1302 }
1303
1304 /// And a program that is not profiled is started by the one it always was.
1305 #[test]
1306 fn a_program_that_is_not_profiled_is_started_by_the_usual_one() {
1307 let plain = LinkOptions::default();
1308 assert_eq!(startfile(&plain, false), Some("crt1.o"));
1309 assert_eq!(startfile(&plain, true), Some("Scrt1.o"));
1310 }
1311
1312 #[test]
1313 fn asking_for_no_startup_files_leaves_out_both_ends_of_them() {
1314 let opts = LinkOptions { no_startfiles: true, ..LinkOptions::default() };
1315 let args = line(linux(), &opts, &one("a.o"), "a.out").expect("a line");
1316 assert!(!args.iter().any(|a| a.ends_with("crt1.o")), "{args:?}");
1317 assert!(!args.iter().any(|a| a.ends_with("crtn.o")), "{args:?}");
1318 // And still links against the library, because that is the other flag.
1319 assert!(args.contains(&"-lc".to_owned()), "{args:?}");
1320 }
1321
1322 #[test]
1323 fn asking_for_no_library_at_all_leaves_out_the_startup_files_too() {
1324 let opts = LinkOptions { no_stdlib: true, ..LinkOptions::default() };
1325 let args = line(linux(), &opts, &one("a.o"), "a.out").expect("a line");
1326 assert!(!args.contains(&"-lc".to_owned()), "{args:?}");
1327 assert!(!args.iter().any(|a| a.ends_with("crt1.o")), "{args:?}");
1328 }
1329
1330 #[test]
1331 fn the_library_comes_after_the_objects_that_need_it() {
1332 let items = vec![Item::File("a.o".to_owned()), Item::Library("m".to_owned())];
1333 let args = line(linux(), &LinkOptions::default(), &items, "a.out").expect("a line");
1334 let obj = args.iter().position(|a| a == "a.o").expect("the object");
1335 let m = args.iter().position(|a| a == "-lm").expect("the library");
1336 let c = args.iter().position(|a| a == "-lc").expect("the library");
1337 assert!(obj < m && m < c, "{args:?}");
1338 }
1339
1340 #[test]
1341 fn what_the_user_told_the_linker_stays_where_the_user_wrote_it() {
1342 // The pair libtool writes around a set of convenience archives, which is what found this.
1343 // Both words are about the files between them, so a line that collects them and puts them
1344 // at the end has two options that do nothing and an archive whose members were all dropped.
1345 let items = vec![
1346 Item::File("a.o".to_owned()),
1347 Item::Linker("--whole-archive".to_owned()),
1348 Item::File("libaesni.a".to_owned()),
1349 Item::Linker("--no-whole-archive".to_owned()),
1350 Item::Library("m".to_owned()),
1351 ];
1352 let args = line(linux(), &LinkOptions::default(), &items, "a.out").expect("a line");
1353 let at = |what: &str| args.iter().position(|a| a == what).expect(what);
1354 assert!(at("a.o") < at("--whole-archive"), "{args:?}");
1355 assert!(at("--whole-archive") < at("libaesni.a"), "{args:?}");
1356 assert!(at("libaesni.a") < at("--no-whole-archive"), "{args:?}");
1357 assert!(at("--no-whole-archive") < at("-lm"), "{args:?}");
1358 assert!(at("-lm") < at("-lc"), "{args:?}");
1359 }
1360
1361 #[test]
1362 fn a_sysroot_moves_every_path_this_decided_and_none_the_user_wrote() {
1363 let opts = LinkOptions {
1364 sysroot: Some(PathBuf::from("/nowhere-at-all")),
1365 search: vec![PathBuf::from("/opt/mine")],
1366 ..LinkOptions::default()
1367 };
1368 let args = line(linux(), &opts, &one("a.o"), "a.out").expect("a line");
1369 let at = args.iter().position(|a| a == "-dynamic-linker").expect("the flag");
1370 assert_eq!(args[at + 1], "/nowhere-at-all/lib64/ld-linux-x86-64.so.2");
1371 assert!(args.contains(&"-L/opt/mine".to_owned()), "{args:?}");
1372 }
1373
1374 #[test]
1375 fn a_platform_with_no_link_line_is_said_so_rather_than_linked_wrongly() {
1376 for triple in [
1377 Triple::new(Arch::X86_64, Os::Darwin, Env::Gnu),
1378 Triple::new(Arch::X86_64, Os::Windows, Env::Msvc),
1379 ] {
1380 let error = line(triple, &LinkOptions::default(), &one("a.o"), "a.out")
1381 .expect_err("no line for it");
1382 assert!(matches!(error, Error::Target { .. }), "{error:?}");
1383 }
1384 }
1385
1386 #[test]
1387 fn the_line_is_printed_the_way_it_would_be_typed() {
1388 let linker = Linker { name: "ld".to_owned(), path: PathBuf::from("/usr/bin/ld") };
1389 let args = ["-o".to_owned(), "a b".to_owned()];
1390 assert_eq!(render(&linker, &args), "/usr/bin/ld -o \"a b\"");
1391 }
1392
1393 #[test]
1394 fn a_linker_that_is_not_there_is_said_by_name() {
1395 let opts = LinkOptions {
1396 use_ld: Some("a-linker-nobody-has".to_owned()),
1397 ..LinkOptions::default()
1398 };
1399 let error = find(linux(), &opts).expect_err("not on this machine");
1400 assert_eq!(error, Error::Named { name: "a-linker-nobody-has".to_owned() });
1401 }
1402 /// A directory with a `libgcc.a` in it, so a test can say what a machine with a gcc on it
1403 /// looks like without needing one.
1404 fn a_gcc_dir(name: &str) -> PathBuf {
1405 let dir = std::env::temp_dir().join(format!("rucc-link-{name}-{}", std::process::id()));
1406 fs::create_dir_all(&dir).expect("a temporary directory");
1407 fs::write(dir.join("libgcc.a"), b"not really an archive").expect("a file in it");
1408 dir
1409 }
1410
1411 #[test]
1412 fn the_c_library_supplies_the_block_routines_and_our_runtime_does_not_displace_them() {
1413 let gcc = a_gcc_dir("order");
1414 let ours = PathBuf::from("/somewhere/librucc_builtins.a");
1415 let args = runtime_items(&LinkOptions::default(), &[gcc], Some(&ours));
1416 let at_libc = args.iter().position(|a| a == "-lc").expect("libc");
1417 let at_ours = args.iter().position(|a| a.ends_with("librucc_builtins.a")).expect("ours");
1418 // glibc's `memcpy` is assembly per microarchitecture and ours is a word at a time loop,
1419 // so on a target that has one, its is the one that should answer.
1420 assert!(at_libc < at_ours, "{args:?}");
1421 }
1422
1423 #[test]
1424 fn a_static_link_puts_them_in_a_group_because_two_of_them_refer_to_each_other() {
1425 let gcc = a_gcc_dir("group");
1426 let opts = LinkOptions { is_static: true, ..LinkOptions::default() };
1427 let args = runtime_items(&opts, &[gcc], None);
1428 assert_eq!(args.first().map(String::as_str), Some("--start-group"), "{args:?}");
1429 assert_eq!(args.last().map(String::as_str), Some("--end-group"), "{args:?}");
1430 // The unwinder, which is what `libc.a` refers to and what a static link fails on without
1431 // it. Issue #277.
1432 assert!(args.contains(&"-lgcc_eh".to_owned()), "{args:?}");
1433 }
1434
1435 #[test]
1436 fn a_dynamic_link_needs_no_group_and_asks_for_the_shared_half_only_if_something_wants_it() {
1437 let gcc = a_gcc_dir("dynamic");
1438 let args = runtime_items(&LinkOptions::default(), &[gcc], None);
1439 assert!(!args.contains(&"--start-group".to_owned()), "{args:?}");
1440 assert!(!args.contains(&"-lgcc_eh".to_owned()), "{args:?}");
1441 let at = args.iter().position(|a| a == "-lgcc_s").expect("the shared half");
1442 assert_eq!(args[at - 1], "--as-needed", "{args:?}");
1443 assert_eq!(args[at + 1], "--no-as-needed", "{args:?}");
1444 }
1445
1446 #[test]
1447 fn our_own_runtime_comes_before_the_machines_because_the_two_are_interchangeable() {
1448 let gcc = a_gcc_dir("ours");
1449 let ours = PathBuf::from("/somewhere/librucc_builtins.a");
1450 let args = runtime_items(&LinkOptions::default(), &[gcc], Some(&ours));
1451 let at_ours = args.iter().position(|a| a.ends_with("librucc_builtins.a")).expect("ours");
1452 let at_gcc = args.iter().position(|a| a == "-lgcc").expect("libgcc");
1453 assert!(at_ours < at_gcc, "{args:?}");
1454 }
1455
1456 #[test]
1457 fn no_builtins_lib_leaves_ours_off_and_keeps_the_machines() {
1458 let gcc = a_gcc_dir("theirs");
1459 let opts = LinkOptions { no_builtins_lib: true, ..LinkOptions::default() };
1460 let args = line(linux(), &opts, &one("a.o"), "a.out").expect("a line");
1461 assert!(!args.iter().any(|a| a.ends_with("librucc_builtins.a")), "{args:?}");
1462 // And the machine's half is still decided the same way it was, from the directories
1463 // that are there, which on the machine running this test may be none.
1464 assert!(runtime_items(&opts, &[gcc], None).contains(&"-lgcc".to_owned()));
1465 }
1466
1467 #[test]
1468 fn nodefaultlibs_leaves_the_whole_runtime_off_and_not_only_the_c_library() {
1469 let gcc = a_gcc_dir("none");
1470 let opts = LinkOptions { no_defaultlibs: true, ..LinkOptions::default() };
1471 assert!(runtime_items(&opts, &[gcc], None).is_empty());
1472 }
1473
1474 #[test]
1475 fn a_machine_with_no_gcc_on_it_gets_no_names_for_libraries_that_are_not_there() {
1476 let empty = std::env::temp_dir().join("rucc-link-empty-not-a-gcc");
1477 let args = runtime_items(&LinkOptions::default(), &[empty], None);
1478 assert_eq!(args, ["-lc"], "{args:?}");
1479 }
1480
1481 #[test]
1482 fn a_gcc_version_directory_is_read_as_a_version_and_not_as_a_word() {
1483 assert!(version_key(Path::new("/usr/lib/gcc/x/13")) > version_key(Path::new("/x/9")));
1484 assert!(version_key(Path::new("/x/10.2")) > version_key(Path::new("/x/10")));
1485 // Something that is not a version at all still sorts, and sorts below one that is.
1486 assert!(version_key(Path::new("/x/snapshot")) < version_key(Path::new("/x/1")));
1487 }
1488
1489 /// A command line that has a cache to find generated sysroots in, which a real one always has.
1490 ///
1491 /// And a `-B` prefix with our runtime in it, because a cross link refuses without one and
1492 /// every machine that does this for real has the archive `cargo xtask builtins` wrote. What
1493 /// happens when it is missing is its own test below.
1494 fn cached() -> LinkOptions {
1495 LinkOptions {
1496 cache: Some(PathBuf::from("/cache")),
1497 prefixes: vec![a_builtins_dir()],
1498 ..LinkOptions::default()
1499 }
1500 }
1501
1502 /// A directory with our runtime archive in it, so that a test can say what a machine where the
1503 /// runtime was built looks like without building one.
1504 ///
1505 /// One directory for every test rather than one each, since none of them writes to it and the
1506 /// name of the file is the whole of what they read.
1507 fn a_builtins_dir() -> PathBuf {
1508 let dir = std::env::temp_dir().join(format!("rucc-link-ours-{}", std::process::id()));
1509 fs::create_dir_all(&dir).expect("a temporary directory");
1510 fs::write(dir.join("librucc_builtins.a"), b"not really an archive").expect("a file in it");
1511 dir
1512 }
1513
1514 /// An archive in a directory named by the short tuple is found, which is where `cargo xtask
1515 /// builtins --target=aarch64-linux-musl` puts it.
1516 #[test]
1517 fn the_runtime_is_found_under_the_tuple_as_well_as_the_triple() {
1518 let dir = std::env::temp_dir().join(format!("rucc-link-tuple-{}", std::process::id()));
1519 let target: Triple = "aarch64-linux-musl".parse().expect("a triple");
1520 let under = dir.join(target.tuple().to_string());
1521 fs::create_dir_all(&under).expect("a temporary directory");
1522 fs::write(under.join("librucc_builtins.a"), b"not really an archive")
1523 .expect("a file in it");
1524 let found = builtins_archive(target, std::slice::from_ref(&dir));
1525 assert_eq!(found, Some(under.join("librucc_builtins.a")));
1526 let _ = fs::remove_dir_all(&dir);
1527 }
1528
1529 /// Where that cache would keep this target's sysroot.
1530 fn a_sysroot(target: Triple) -> Sysroot {
1531 Sysroot::in_cache(Path::new("/cache"), target.tuple())
1532 }
1533
1534 /// A target that is not the machine running this test, whatever machine that is.
1535 ///
1536 /// A freestanding one, because [`Triple::host`] answers Linux, Darwin or Windows and never
1537 /// `Os::None`. Every other triple is somebody's host, so a test that wants the cross path out of
1538 /// [`line`] itself has to use this one and the rest go through [`cross_line`].
1539 fn foreign() -> Triple {
1540 Triple::new(Arch::X86_64, Os::None, Env::None)
1541 }
1542
1543 #[test]
1544 fn a_cross_link_reads_the_targets_own_sysroot_and_nothing_of_this_machine() {
1545 let target = Triple::new(Arch::Aarch64, Os::Linux, Env::Musl);
1546 let sysroot = a_sysroot(target);
1547 // The paths as this host spells them, because what is being checked is which directory the
1548 // files are in and a Windows separator is a backslash.
1549 let root = sysroot.root().display().to_string();
1550 let lib = sysroot.lib();
1551 let args = cross_line(target, &cached(), &one("a.o"), "a.out", &sysroot).expect("a line");
1552 assert!(args.contains(&format!("--sysroot={root}")), "{args:?}");
1553 assert!(args.contains(&format!("-L{}", lib.display())), "{args:?}");
1554 assert!(args.contains(&lib.join("libc.a").display().to_string()), "{args:?}");
1555 let at = args.iter().position(|a| a == "-dynamic-linker").expect("the loader");
1556 assert_eq!(args[at + 1], "/lib/ld-musl-aarch64.so.1", "{args:?}");
1557 // The whole point of the other path not being taken: not one directory of this machine is
1558 // on the line, so the line is the same on every host and the recorded ones describe it.
1559 for arg in &args {
1560 assert!(!arg.contains("/usr/lib"), "{arg} in {args:?}");
1561 assert!(!arg.contains("/lib64"), "{arg} in {args:?}");
1562 }
1563 }
1564
1565 #[test]
1566 fn a_freestanding_target_links_against_our_runtime_instead_of_being_refused() {
1567 let args = line(foreign(), &cached(), &one("a.o"), "a.out").expect("a line");
1568 assert!(args.iter().any(|a| a.ends_with("librucc_builtins.a")), "{args:?}");
1569 // No libc, because there is not one, and no start file either: what runs before `main` on a
1570 // freestanding target comes from whatever is being built.
1571 assert!(!args.iter().any(|a| a.ends_with("libc.a")), "{args:?}");
1572 assert!(!args.contains(&"-lc".to_owned()), "{args:?}");
1573 assert!(!args.iter().any(|a| a.ends_with("crt1.o")), "{args:?}");
1574 }
1575
1576 /// And with nothing to find sysroots in it is refused, which is what it was before this.
1577 #[test]
1578 fn a_driver_with_no_cache_to_look_in_says_so_rather_than_guessing() {
1579 let error = line(foreign(), &LinkOptions::default(), &one("a.o"), "a.out")
1580 .expect_err("no line for it");
1581 assert!(matches!(error, Error::Target { .. }), "{error:?}");
1582 }
1583
1584 #[test]
1585 fn a_static_link_against_a_libc_that_is_a_stub_is_refused_rather_than_attempted() {
1586 let target = Triple::new(Arch::X86_64, Os::Linux, Env::Gnu);
1587 let opts = LinkOptions { is_static: true, ..cached() };
1588 let error = cross_line(target, &opts, &one("a.o"), "a.out", &a_sysroot(target))
1589 .expect_err("there is no libc.a in a stub sysroot");
1590 let Error::Cross { why } = &error else { panic!("{error:?}") };
1591 // Because a stub carries the names a library exports and none of the bodies, which is
1592 // everything a dynamic link reads and nothing a static one does.
1593 assert!(why.contains("stub"), "{why}");
1594 }
1595
1596 #[test]
1597 fn a_target_whose_linker_wants_a_different_line_is_refused_by_name() {
1598 for target in [
1599 Triple::new(Arch::Aarch64, Os::Darwin, Env::None),
1600 Triple::new(Arch::X86_64, Os::Windows, Env::Msvc),
1601 ] {
1602 let error = cross_line(target, &cached(), &one("a.o"), "a.out", &a_sysroot(target))
1603 .expect_err("no line for that format");
1604 let Error::Cross { why } = &error else { panic!("{error:?}") };
1605 assert!(why.contains(&target.tuple().to_canonical_string()), "{why}");
1606 }
1607 }
1608
1609 #[test]
1610 fn a_mingw_target_links_and_looks_for_a_linker_that_can_write_a_pe_image() {
1611 let target = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
1612 let args = cross_line(target, &cached(), &one("a.o"), "a.exe", &a_sysroot(target))
1613 .expect("a line for mingw-w64");
1614 let at = |flag: &str| args.iter().position(|arg| arg == flag).expect(flag);
1615 assert_eq!(args[at("-m") + 1], "i386pep");
1616 assert_eq!(args[at("--subsystem") + 1], "console");
1617 assert!(args.iter().any(|arg| arg.ends_with("libmsvcrt.a")), "{args:?}");
1618 // And the prefixed name a distribution files its mingw binutils under, which is not the
1619 // multiarch one.
1620 let names = cross_order(target);
1621 assert_eq!(names.first().map(String::as_str), Some("ld.lld"));
1622 assert!(names.contains(&"x86_64-w64-mingw32-ld".to_owned()), "{names:?}");
1623 }
1624
1625 #[test]
1626 fn our_runtime_comes_from_beside_the_compiler_rather_than_from_inside_the_sysroot() {
1627 // The two halves of tamnd/rucc#1514. The line used to name it under the sysroot's `lib`,
1628 // where nothing ever put it: it is this compiler's output for the target and a sysroot
1629 // fetched from a release holds the platform's files and not ours. So the path on the line
1630 // is the one the driver found, and the only `librucc_builtins.a` on the line is that one.
1631 let target = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
1632 let sysroot = a_sysroot(target);
1633 let opts = cached();
1634 let args = cross_line(target, &opts, &one("a.o"), "a.exe", &sysroot).expect("a line");
1635 let ours: Vec<&String> =
1636 args.iter().filter(|arg| arg.ends_with("librucc_builtins.a")).collect();
1637 assert_eq!(ours.len(), 1, "{args:?}");
1638 assert_eq!(ours[0], &opts.prefixes[0].join("librucc_builtins.a").display().to_string());
1639 assert!(!ours[0].starts_with(&sysroot.lib().display().to_string()), "{args:?}");
1640 // And it is still last, after everything that calls into it.
1641 assert_eq!(args.last(), Some(ours[0]), "{args:?}");
1642 }
1643
1644 #[test]
1645 fn a_cross_link_with_no_runtime_to_find_says_which_command_writes_one() {
1646 // What the linker would say instead is that `___chkstk_ms` is undefined, referenced from
1647 // mingw-w64's own objects, which is tamnd/rucc#1513 and is nobody's first guess at a
1648 // missing archive.
1649 let target = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
1650 let opts = LinkOptions { prefixes: Vec::new(), ..cached() };
1651 let error = cross_line(target, &opts, &one("a.o"), "a.exe", &a_sysroot(target))
1652 .expect_err("there is no runtime for it to find");
1653 let Error::Cross { why } = &error else { panic!("{error:?}") };
1654 assert!(why.contains("cargo xtask builtins"), "{why}");
1655 assert!(why.contains("-fno-builtins-lib"), "{why}");
1656
1657 // And that flag is the way through it, for somebody who meant to link without ours.
1658 let without = LinkOptions { no_builtins_lib: true, ..opts };
1659 let args = cross_line(target, &without, &one("a.o"), "a.exe", &a_sysroot(target))
1660 .expect("a line without ours on it");
1661 assert!(!args.iter().any(|arg| arg.ends_with("librucc_builtins.a")), "{args:?}");
1662 }
1663
1664 #[test]
1665 fn the_version_an_lld_prints_is_read_and_nothing_elses_is() {
1666 // What each of these programs actually prints, because the word being in the line is the
1667 // whole of how one is told from another.
1668 assert_eq!(lld_major("LLD 18.1.8 (compatible with GNU linkers)\n"), Some(18));
1669 assert_eq!(lld_major("Ubuntu LLD 18.1.3 (compatible with GNU linkers)\n"), Some(18));
1670 assert_eq!(lld_major("LLD 20.1.2 (compatible with GNU linkers)\n"), Some(20));
1671
1672 // Binutils and mold do not have it, and neither of them has this problem, so the answer
1673 // for both is that this check has nothing to say about them.
1674 assert_eq!(lld_major("GNU ld (GNU Binutils for Ubuntu) 2.42\n"), None);
1675 assert_eq!(lld_major("mold 2.4.1 (compatible with GNU ld)\n"), None);
1676 assert_eq!(lld_major(""), None);
1677 }
1678
1679 /// A program that prints `text` and exits, which is as much of a linker as this check reads.
1680 ///
1681 /// Named after what it says, so that two of them in one test are two files.
1682 #[cfg(unix)]
1683 fn a_linker_that_says(tag: &str, text: &str) -> Linker {
1684 use std::os::unix::fs::PermissionsExt as _;
1685 let dir = std::env::temp_dir().join(format!("rucc-link-ld-{}", std::process::id()));
1686 fs::create_dir_all(&dir).expect("a temporary directory");
1687 let path = dir.join(format!("ld.lld-{tag}"));
1688 fs::write(&path, format!("#!/bin/sh\necho '{text}'\n")).expect("a script");
1689 fs::set_permissions(&path, fs::Permissions::from_mode(0o755)).expect("an executable one");
1690 Linker { name: "ld.lld".to_owned(), path }
1691 }
1692
1693 #[test]
1694 #[cfg(unix)]
1695 fn an_lld_too_old_to_read_exportas_is_refused_for_windows_gnu_and_nowhere_else() {
1696 // The failure this replaces has no diagnostic at all: 18 writes an import by ordinal zero,
1697 // exits successfully, and the program dies at startup under wine. tamnd/rucc#1515.
1698 let windows = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
1699 let old = a_linker_that_says("18", "LLD 18.1.8 (compatible with GNU linkers)");
1700 let error = suitable(windows, &old).expect_err("18 cannot link this");
1701 let Error::TooOld { name, found, target } = &error else { panic!("{error:?}") };
1702 assert_eq!((name.as_str(), *found, target.as_str()), ("ld.lld", 18, "x86_64-windows-gnu"));
1703 assert!(error.to_string().contains("IMPORT_NAME_EXPORTAS"), "{error}");
1704
1705 // The same linker for a target whose import libraries have no such records in them, which
1706 // is every other target, since this is one encoding in one format.
1707 let linux = Triple::new(Arch::X86_64, Os::Linux, Env::Musl);
1708 assert_eq!(suitable(linux, &old), Ok(()));
1709
1710 // And the first one that reads them.
1711 let new = a_linker_that_says("19", "LLD 19.1.0 (compatible with GNU linkers)");
1712 assert_eq!(suitable(windows, &new), Ok(()));
1713 }
1714
1715 #[test]
1716 #[cfg(unix)]
1717 fn a_linker_that_will_not_say_what_it_is_is_left_alone() {
1718 // Every linker that is not an lld reaches this check too, and what it can establish is
1719 // that a specific old lld is here rather than that anything else is fit. Turning "I did
1720 // not recognise this" into a refusal would break machines this problem never touched.
1721 let windows = Triple::new(Arch::X86_64, Os::Windows, Env::Gnu);
1722 let quiet = a_linker_that_says("gnu", "GNU ld (GNU Binutils for Ubuntu) 2.42");
1723 assert_eq!(suitable(windows, &quiet), Ok(()));
1724
1725 let missing = Linker { name: "ld.lld".to_owned(), path: PathBuf::from("/no/such/linker") };
1726 assert_eq!(suitable(windows, &missing), Ok(()));
1727 }
1728
1729 #[test]
1730 fn profiling_a_cross_link_is_refused_because_the_startup_file_is_compiled_code() {
1731 let target = Triple::new(Arch::X86_64, Os::Linux, Env::Musl);
1732 let opts = LinkOptions { profile: true, ..cached() };
1733 let error = cross_line(target, &opts, &one("a.o"), "a.out", &a_sysroot(target))
1734 .expect_err("there is no gcrt1.o in a generated sysroot");
1735 let Error::Cross { why } = &error else { panic!("{error:?}") };
1736 assert!(why.contains("gcrt1.o"), "{why}");
1737 }
1738
1739 #[test]
1740 fn the_host_takes_the_host_line_and_a_tree_the_user_named_takes_it_too() {
1741 let host = Triple::new(Arch::X86_64, Os::Linux, Env::Gnu);
1742 let other = Triple::new(Arch::Riscv64, Os::Linux, Env::Musl);
1743 assert!(cross_for(host, &cached(), Some(host)).is_none());
1744 assert!(cross_for(other, &cached(), Some(host)).is_some());
1745 // A tree somebody assembled and named is what `--sysroot` has always meant here, and the
1746 // native line prefixes every path it decides with it.
1747 let named = LinkOptions { sysroot: Some(PathBuf::from("/opt/root")), ..cached() };
1748 assert!(cross_for(other, &named, Some(host)).is_none());
1749 // A host this compiler cannot name is a host whose directories it should not be guessing at.
1750 assert!(cross_for(other, &cached(), None).is_some());
1751 }
1752
1753 #[test]
1754 fn a_pinned_release_on_this_machines_own_target_is_a_cross_compile() {
1755 // The case that used to be dropped on the floor. `--target=x86_64-linux-gnu.2.28` on an
1756 // x86-64 glibc machine read that machine's headers and linked that machine's libc, and the
1757 // release reached nothing, so what came out was a binary for whatever release the build
1758 // machine happened to have. A pin is the one thing a person writes to say otherwise.
1759 let host = Triple::new(Arch::X86_64, Os::Linux, Env::Gnu);
1760 let pinned = LinkOptions {
1761 pinned: Some(
1762 "x86_64-linux-gnu.2.28".parse::<TargetTuple>().expect("a spelling with a release"),
1763 ),
1764 ..cached()
1765 };
1766 let at = cross_for(host, &pinned, Some(host)).expect("a pin is a cross compile");
1767 // And against the release's own directory, because the release is in the cache key: a tree
1768 // produced for 2.28 and a tree produced for 2.44 are two trees and the path has to say which.
1769 assert!(at.root().ends_with("x86_64-linux-gnu.2.28"), "{:?}", at.root());
1770 // The release is the whole of the difference. The same command line without it is this
1771 // machine, which is what every native compile has always been.
1772 let bare = LinkOptions { pinned: None, ..cached() };
1773 assert!(cross_for(host, &bare, Some(host)).is_none());
1774 }
1775
1776 #[test]
1777 fn a_glibc_cross_link_writes_its_stubs_beside_the_sysroot_once() {
1778 let cache = std::env::temp_dir().join(format!("rucc-link-stubs-{}", std::process::id()));
1779 let _ = fs::remove_dir_all(&cache);
1780 let target = Triple::new(Arch::X86_64, Os::Linux, Env::Gnu);
1781 let pinned = LinkOptions {
1782 cache: Some(cache.clone()),
1783 pinned: Some("x86_64-linux-gnu.2.28".parse().expect("a spelling with a release")),
1784 ..LinkOptions::default()
1785 };
1786 write_stubs(target, &pinned).expect("x86_64 glibc has a description");
1787 let dir = cache.join("stubs").join("x86_64-linux-gnu.2.28");
1788 let libc = dir.join("libc.so");
1789 let bytes = fs::read(&libc).expect("libc.so was written");
1790 assert!(bytes.starts_with(b"\x7fELF"));
1791 assert!(dir.join("libm.so").is_file());
1792 // 2.28 is before the release that emptied libpthread, so there is no empty one to write.
1793 assert!(!dir.join("libpthread.so").exists());
1794 // The second time finds the same bytes and leaves the file alone, which is what keeps a
1795 // linker in another build from ever reading one that is being replaced.
1796 let before = fs::metadata(&libc).and_then(|m| m.modified()).expect("a time");
1797 write_stubs(target, &pinned).expect("again");
1798 let after = fs::metadata(&libc).and_then(|m| m.modified()).expect("a time");
1799 assert_eq!(before, after);
1800 // And nothing for a libc that is not glibc, whose sysroot has a real one in it.
1801 let musl = LinkOptions {
1802 cache: Some(cache.clone()),
1803 pinned: Some("x86_64-linux-musl".parse().expect("musl")),
1804 ..LinkOptions::default()
1805 };
1806 write_stubs(Triple::new(Arch::X86_64, Os::Linux, Env::Musl), &musl).expect("nothing");
1807 assert!(!cache.join("stubs").join("x86_64-linux-musl").exists());
1808 let _ = fs::remove_dir_all(&cache);
1809 }
1810
1811 #[test]
1812 fn what_a_cross_link_searches_is_the_sysroot_and_not_this_machine() {
1813 let dirs = search_dirs(&cached(), foreign());
1814 // One directory, because that is what the line has, and the same one the line has, because
1815 // `-print-search-dirs` is what a build system reads to write a link line of its own.
1816 assert_eq!(dirs.len(), 1, "{dirs:?}");
1817 assert!(dirs[0].starts_with("/cache"), "{dirs:?}");
1818 assert!(dirs[0].ends_with("lib"), "{dirs:?}");
1819 // And what the user wrote still comes first, the way it does on the line itself.
1820 let mine = LinkOptions { search: vec![PathBuf::from("/opt/mine")], ..cached() };
1821 assert_eq!(search_dirs(&mine, foreign())[0], PathBuf::from("/opt/mine"));
1822 }
1823
1824 #[test]
1825 fn the_linker_looked_for_on_a_cross_link_is_one_that_can_cross() {
1826 let names = cross_order(Triple::new(Arch::Aarch64, Os::Linux, Env::Gnu));
1827 assert_eq!(names.first().map(String::as_str), Some("ld.lld"));
1828 assert!(names.contains(&"aarch64-linux-gnu-ld".to_owned()), "{names:?}");
1829 // mold links for the machine it is running on, and so does a distribution's own `ld`, so
1830 // neither is a default here. `-fuse-ld=` is still there for somebody whose is different.
1831 assert!(!names.iter().any(|name| name.contains("mold")), "{names:?}");
1832 assert!(!names.contains(&"ld".to_owned()), "{names:?}");
1833 // And the lookup the driver really does for a target that is not this machine.
1834 assert_eq!(order(foreign(), &cached()), ["ld.lld", "lld"]);
1835 }
1836
1837 #[test]
1838 fn the_four_flags_become_the_five_modes_they_describe() {
1839 let plain = LinkOptions::default();
1840 assert_eq!(mode(&plain), LinkMode::Dynamic);
1841 assert_eq!(
1842 mode(&LinkOptions { pie: Some(false), ..plain.clone() }),
1843 LinkMode::DynamicNoPie
1844 );
1845 assert_eq!(mode(&LinkOptions { is_static: true, ..plain.clone() }), LinkMode::Static);
1846 let both = LinkOptions { is_static: true, pie: Some(true), ..plain.clone() };
1847 assert_eq!(mode(&both), LinkMode::StaticPie);
1848 assert_eq!(mode(&LinkOptions { shared: true, ..plain }), LinkMode::Shared);
1849 }
1850
1851 #[test]
1852 fn a_sysroot_that_has_not_been_built_is_named_before_anything_is_compiled() {
1853 let opts = LinkOptions {
1854 cache: Some(std::env::temp_dir().join("rucc-a-cache-nobody-filled")),
1855 ..LinkOptions::default()
1856 };
1857 let error = preflight(foreign(), &opts).expect_err("nothing has built one");
1858 let Error::Sysroot { dir, pinned, .. } = &error else { panic!("{error:?}") };
1859 assert!(dir.ends_with("x86_64-none"), "{dir}");
1860 // Nothing is pinned for that target, or for any target yet, so the message says that rather
1861 // than naming a command that would not work.
1862 assert!(!pinned, "nothing should be pinned for a bare metal target");
1863 let said = error.to_string();
1864 assert!(said.contains("pins none for it to fetch"), "{said}");
1865 }
1866
1867 /// The other half of the same message, which is what a target this release does pin an artifact
1868 /// for is told. Built by hand rather than through `preflight`, because what is being checked is
1869 /// the message and not which targets `rucc_sysroot::artifact` happens to pin this release.
1870 #[test]
1871 fn a_sysroot_that_could_be_fetched_is_told_what_to_run() {
1872 let said = Error::Sysroot {
1873 target: "x86_64-linux-musl".to_owned(),
1874 dir: "/somewhere/sysroots/x86_64-linux-musl".to_owned(),
1875 pinned: true,
1876 }
1877 .to_string();
1878 assert!(said.contains("`rucc --fetch x86_64-linux-musl`"), "{said}");
1879 // And the other way out of it, because a person who has a tree already does not want a
1880 // download.
1881 assert!(said.contains("--sysroot=<dir>"), "{said}");
1882 }
1883
1884 #[test]
1885 fn a_link_against_this_machine_has_nothing_to_check_before_it_starts() {
1886 // Its directories are looked for as the line is built, and one that is not there is simply
1887 // one that is not offered, so there is no question to answer early.
1888 assert!(preflight(linux(), &LinkOptions::default()).is_ok());
1889 }
1890
1891 #[test]
1892 fn a_runtime_directory_that_is_not_on_this_machine_is_not_offered() {
1893 let dirs = runtime_dirs(linux(), Some(Path::new("/definitely/not/a/sysroot")));
1894 assert!(dirs.is_empty(), "{dirs:?}");
1895 }
1896}