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