Skip to main content

rucc_sysroot/
argv.rs

1//! The linker command line, as a function of the target and the sysroot and nothing else.
2//!
3//! Design: `spec/cross-compile/11-linking.md` section 11.3, which is a list of eight things a
4//! system linker gets right by default on the machine it came with and gets wrong when it is asked
5//! to link for another one.
6//!
7//! # Why this is a pure function
8//!
9//! Section 11.3 ends with the shape: `(tuple, sysroot, options) -> argv`, no environment reads, no
10//! filesystem probing. That is not tidiness, it is what makes the highest consequence code in the
11//! driver testable. A link line is the last thing that touches a binary and the first thing that
12//! can quietly ruin it, and a function that reads the machine it runs on can only be tested on the
13//! machine it runs on. This one is tested for every target in the table from any host, and
14//! `tests/link-lines` is what it produced for each of them when it was last changed.
15//!
16//! The mirror of that rule is the one [`crate::search`] enforces for headers: nothing from the host
17//! reaches the line. No `/usr/lib`, no `/lib64`, no `LIBRARY_PATH`, and no start file found by
18//! looking around. Every path here is either under the sysroot or something the user wrote on the
19//! command line themselves, and `nothing_on_the_line_comes_from_the_host` is that as a test.
20//!
21//! # What is not decided here
22//!
23//! Which linker runs. `spec/cross-compile/11-linking.md` section 11.2 picks one per format and the
24//! driver spawns it, and the arguments below are the ones `ld`, `ld.lld` and `mold` all read the
25//! same way. That is a real constraint rather than an aspiration: `-static-pie` is a compiler driver
26//! flag that none of the three linkers has, so the mode that means it is spelled out here as the
27//! three flags a linker does understand.
28//!
29//! # The two formats that have a line
30//!
31//! ELF, and PE in mingw-w64's environment. Both are written in the GNU style, which is the same
32//! syntax for the inputs and a different set of flags, so they share everything below that is about
33//! what has to be linked and differ in what is about the image. The PE line is GNU ld's PE port and
34//! `ld.lld` in its MinGW mode, which read each other's arguments for exactly this reason.
35//!
36//! Mach-O and the MSVC ABI are refused rather than approximated. `ld64` wants a platform version
37//! load command and a `-syslibroot`, `lld-link` wants `/MACHINE:` and a `/DEFAULTLIB:` set out of an
38//! SDK that cannot be redistributed, and neither is a different spelling of what is below.
39//! [`Unsupported`] says which by name, which is a better answer than a line that looks plausible and
40//! produces nothing that runs.
41
42use std::fmt;
43use std::path::{Path, PathBuf};
44
45use rucc_tuple::{Arch, DataModel, Endian, Env, ObjectFormat, TargetTuple};
46
47use crate::layout::Sysroot;
48use crate::link::{BUILTINS, Libc, LinkLine, LinkMode, libc, loader};
49
50/// One input to the link, in the position the user wrote it.
51///
52/// Link order is semantic: an archive is searched for what is undefined at the moment the linker
53/// reaches it, so a library named before the object that needs it contributes nothing. That is why
54/// this is one ordered list rather than a list of objects and a list of libraries, which is a shape
55/// that cannot represent what the user typed.
56#[derive(Debug, Clone, PartialEq, Eq)]
57pub enum Item {
58    /// A file, which is an object this compilation produced or one named on the command line.
59    File(PathBuf),
60    /// `-l<name>`, which the linker resolves against the search path.
61    Library(String),
62    /// One word from `-Wl,` or `-Xlinker`, handed to the linker where the user wrote it.
63    ///
64    /// Here for the reason the other two are. A great many of the linker's options are a bracket
65    /// around the files after them, so an option moved away from what it brackets means something
66    /// else or nothing at all: `--whole-archive` takes every member of every archive after it
67    /// whether anything referenced it or not, `--start-group` searches the archives after it again
68    /// until nothing more comes out, and `-Bstatic` picks which half of a library that ships both
69    /// is wanted.
70    Linker(String),
71}
72
73/// What the driver knows that the line needs, beyond the target and the sysroot.
74///
75/// A struct because most of it is empty in the common case, and because a function with nine
76/// positional parameters of which seven are usually a default is a function somebody calls wrong.
77#[derive(Debug, Clone, Default)]
78pub struct Invocation<'a> {
79    /// The objects and libraries, in the order they were written.
80    pub inputs: &'a [Item],
81    /// `-o`. Empty means the linker's own default, which is what a caller testing a line wants.
82    pub output: Option<&'a Path>,
83    /// How the program is linked, which decides the start file and four of the flags.
84    pub mode: LinkMode,
85    /// `-L`, in the order given. The user's own, and they come before ours, because somebody who
86    /// passed `-L` meant it to win.
87    pub search: &'a [PathBuf],
88    /// `-nostartfiles`, which leaves `crt1.o`, `crti.o` and `crtn.o` off.
89    pub no_startfiles: bool,
90    /// `-nodefaultlibs`, which leaves the libc and our runtime off.
91    pub no_defaultlibs: bool,
92    /// `-fno-builtins-lib`, which leaves our own runtime off and keeps the libc.
93    ///
94    /// It means something narrower here than it does on a native link. There it leaves ours off so
95    /// that the machine's `libgcc` answers for the wide arithmetic instead, and there is no `libgcc`
96    /// in a generated sysroot, so here it leaves those names undefined. Which is what somebody
97    /// passing it with a `-l` of their own is asking for, and the link says so by name if they are
98    /// not.
99    pub no_builtins_lib: bool,
100    /// Our own runtime archive for this target, if it is on the machine.
101    ///
102    /// A path from the caller rather than a name this crate joins onto the sysroot, because it is
103    /// the compiler's own output for the target and not the platform's, and a fetched sysroot will
104    /// never hold it. The driver is what looks for it, in the `-B` prefixes and then beside the
105    /// compiler, and [`None`] is what it says when there is none: the line goes without it and
106    /// whatever wanted a wide divide is undefined. See [`crate::link::BUILTINS`].
107    pub builtins: Option<&'a Path>,
108    /// `-rdynamic`, which puts every symbol in the dynamic table so a program can look itself up.
109    pub export_dynamic: bool,
110    /// `-s`, which drops the symbol table.
111    pub strip: bool,
112}
113
114/// A target, or a combination of a target and a mode, that has no line here.
115///
116/// Four variants and they are different kinds of answer. A format is not supported yet and will be.
117/// The MSVC ABI is waiting on something that is not code. A static glibc link is not a thing this
118/// scheme can produce at all. The distinction matters to somebody reading the message, because only
119/// some of them are worth waiting for.
120#[derive(Debug, Clone, PartialEq, Eq)]
121pub enum Unsupported {
122    /// The target's object format is neither ELF nor PE, and the linker for it wants a different
123    /// line rather than a different spelling of this one.
124    Format {
125        /// The target that was asked for.
126        target: String,
127        /// Its object format, in the spelling `--print-config` uses.
128        format: &'static str,
129    },
130    /// A Windows target in Microsoft's ABI rather than mingw-w64's.
131    ///
132    /// Refused for two reasons and the second one is the one that matters. `lld-link` takes a
133    /// different command line rather than a different set of flags: `/MACHINE:`, `/SUBSYSTEM:`,
134    /// `/DEFAULTLIB:` and a response file, which is its own work. And the import libraries a program
135    /// in that ABI links against come from the Windows SDK and the universal CRT, which
136    /// `spec/cross-compile/08-sysroots.md` section 8.6 says cannot be redistributed, so there is
137    /// nothing to produce on this side and a user has to point at an installed one themselves.
138    MsvcAbi {
139        /// The target that was asked for.
140        target: String,
141    },
142    /// A PE target whose architecture has no machine type among the ones a PE linker writes.
143    ///
144    /// Unreachable through the target table, which has three mingw-w64 rows and an ARM64EC one that
145    /// the MSVC ABI refuses first. It is a variant rather than a panic because the table is data and
146    /// a row added to it should produce a sentence rather than a crash.
147    Machine {
148        /// The target that was asked for.
149        target: String,
150    },
151    /// A static link against a libc that is a stub.
152    ///
153    /// `spec/cross-compile/09-libc-stubs.md` section 9.1 is the reason: a stub carries the names a
154    /// library exports and none of the code behind them, which is everything a dynamic link needs
155    /// and nothing a static one does. glibc's own `libc.a` is several megabytes of objects that
156    /// cannot be synthesized from a description of an interface, so this combination is refused
157    /// here rather than failing later with several thousand undefined symbols.
158    ///
159    /// Every [`crate::link::Libc::Stub`] target, which is glibc and also bionic, the BSDs and
160    /// illumos. musl is the exception rather than the rule here, because musl is the one whose libc
161    /// we build from source.
162    StaticStub {
163        /// The target that was asked for.
164        target: String,
165    },
166}
167
168impl fmt::Display for Unsupported {
169    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170        match self {
171            Unsupported::Format { target, format } => write!(
172                f,
173                "there is no cross link line for {target} yet, because its object format is \
174                 {format} and that linker takes a different line rather than a different spelling \
175                 of this one"
176            ),
177            Unsupported::MsvcAbi { target } => write!(
178                f,
179                "there is no cross link line for {target}, because it is Microsoft's ABI: the \
180                 linker for it takes a different command line and the import libraries a program \
181                 there links against come from the Windows SDK, which cannot be redistributed. \
182                 Build for the mingw-w64 environment instead, which needs nothing installed, or \
183                 pass --sysroot=<dir> naming an SDK you have"
184            ),
185            Unsupported::Machine { target } => write!(
186                f,
187                "there is no PE machine type for {target}, so there is nothing to write after -m \
188                 and a linker would guess the machine from the first object it read"
189            ),
190            Unsupported::StaticStub { target } => write!(
191                f,
192                "{target} cannot be linked statically against a generated sysroot, because its \
193                 libc there is a stub: it carries the names the platform's libc exports and none of \
194                 the code behind them, which is what a dynamic link reads and not what a static one \
195                 needs. Link it dynamically, or use a musl target, which ships a real libc.a"
196            ),
197        }
198    }
199}
200
201impl std::error::Error for Unsupported {}
202
203/// The whole linker command line for this target, not counting the linker itself.
204///
205/// Section 11.3's eight items, in the order a linker wants them:
206///
207/// 1. `-m`, the output format, because a linker built for more than one machine guesses from its
208///    first input otherwise and a link of no objects has nothing to guess from.
209/// 2. `--sysroot`, and every `-L` rooted inside it.
210/// 3. `-dynamic-linker`, the one string on the line that describes the target's filesystem rather
211///    than ours.
212/// 4. The start files, by absolute path, in the order the two nested pairs need.
213/// 5. The default libraries, which are ours rather than the host's.
214/// 6. `librucc_builtins.a` for the target, which [`LinkLine`] puts after the libc.
215/// 7. No host paths at all.
216/// 8. The format's own extras, which on ELF is the hardening and reproducibility set below and on
217///    PE is the subsystem, the address space layout flags and the header timestamp.
218///
219/// Two formats reach a line here and the difference between them is the flags rather than the shape.
220/// Both are written in the GNU style, which is what `ld`, `ld.lld` and `ld.lld` in its MinGW mode all
221/// read, so the inputs and the `-L` directories are assembled once for both rather than twice.
222///
223/// # Errors
224///
225/// [`Unsupported::Format`] for a target whose object format is neither ELF nor PE,
226/// [`Unsupported::MsvcAbi`] for a Windows target in Microsoft's ABI,
227/// [`Unsupported::Machine`] for a PE target with no machine type, and
228/// [`Unsupported::StaticStub`] for a static link against a libc that is a stub.
229pub fn argv(
230    target: TargetTuple,
231    sysroot: &Sysroot,
232    options: &Invocation<'_>,
233) -> Result<Vec<String>, Unsupported> {
234    let format = target.object_format();
235    match format {
236        ObjectFormat::Elf => elf(target, sysroot, options),
237        // mingw-w64 rather than every COFF target, because the MSVC ABI is a different linker with a
238        // different argument syntax and an import library set we are not allowed to ship.
239        ObjectFormat::Coff if target.env() == Env::Gnu => coff(target, sysroot, options),
240        ObjectFormat::Coff => Err(Unsupported::MsvcAbi { target: target.to_canonical_string() }),
241        _ => Err(Unsupported::Format {
242            target: target.to_canonical_string(),
243            format: format.as_str(),
244        }),
245    }
246}
247
248/// The line for an ELF target.
249fn elf(
250    target: TargetTuple,
251    sysroot: &Sysroot,
252    options: &Invocation<'_>,
253) -> Result<Vec<String>, Unsupported> {
254    let statically = matches!(options.mode, LinkMode::Static | LinkMode::StaticPie);
255    if statically && libc(target) == Libc::Stub {
256        return Err(Unsupported::StaticStub { target: target.to_canonical_string() });
257    }
258
259    let mut args = output(options);
260    if let Some(name) = emulation(target) {
261        args.push("-m".to_owned());
262        args.push(name.to_owned());
263    }
264    args.push(sysroot_flag(sysroot));
265
266    args.extend(mode_flags(target, options.mode));
267    args.extend(hardening());
268    if options.export_dynamic {
269        args.push("--export-dynamic".to_owned());
270    }
271    if options.strip {
272        args.push("-s".to_owned());
273    }
274
275    args.extend(body(sysroot, options));
276    Ok(args)
277}
278
279/// The line for a mingw-w64 target.
280///
281/// The same eight items as the ELF line and four of them answered differently. The emulation is a PE
282/// one. There is no dynamic linker, because a PE image names no interpreter: the loader is part of
283/// the operating system and finds a DLL by name at load time rather than by a path written into the
284/// program. There is no `-pie` and no `-no-pie`, because every PE image carries a relocation table
285/// and may be placed anywhere, so the question the two flags answer does not exist here and what is
286/// left of it is whether the loader is asked to use that freedom, which is `--dynamicbase`. And
287/// `-static` says something narrower than it does on ELF, which is the note on
288/// [`crate::link::Libc::Import`].
289///
290/// So the five modes are three lines here, and the recorded file shows two pairs of identical
291/// blocks. That is the answer rather than a gap: a position independent executable and one that is
292/// not are the same image on this format, so a build system that passes `-static-pie` or `-no-pie`
293/// gets what it asked for and loses nothing by the flag having nowhere to go.
294///
295/// The subsystem is named rather than left to the linker. Both linkers default it from the entry
296/// point they find, which means a program with a `WinMain` in it silently becomes a GUI program, and
297/// a cross link deciding anything from what it happens to find in the inputs is the failure mode
298/// section 11.3 is about. A user who wants the other one passes `-Wl,--subsystem,windows`, which
299/// goes on last and wins.
300fn coff(
301    target: TargetTuple,
302    sysroot: &Sysroot,
303    options: &Invocation<'_>,
304) -> Result<Vec<String>, Unsupported> {
305    let Some(machine) = pe_machine(target) else {
306        return Err(Unsupported::Machine { target: target.to_canonical_string() });
307    };
308
309    let mut args = output(options);
310    args.push("-m".to_owned());
311    args.push(machine.to_owned());
312    args.push(sysroot_flag(sysroot));
313
314    if options.mode == LinkMode::Shared {
315        args.push("-shared".to_owned());
316    } else {
317        args.push("--subsystem".to_owned());
318        args.push("console".to_owned());
319    }
320    if matches!(options.mode, LinkMode::Static | LinkMode::StaticPie) {
321        args.push("-static".to_owned());
322    }
323    args.extend(pe_hardening(target));
324    // The PE counterpart of `--export-dynamic`, and a different word rather than a different
325    // default: a Windows image exports what its own export table names, and `-rdynamic` asks for
326    // every symbol to be in there so that a program can look itself up.
327    if options.export_dynamic {
328        args.push("--export-all-symbols".to_owned());
329    }
330    if options.strip {
331        args.push("-s".to_owned());
332    }
333
334    args.extend(body(sysroot, options));
335    Ok(args)
336}
337
338/// `-o`, or nothing, which is what a caller testing a line wants.
339fn output(options: &Invocation<'_>) -> Vec<String> {
340    match options.output {
341        Some(path) => vec!["-o".to_owned(), path.display().to_string()],
342        None => Vec::new(),
343    }
344}
345
346/// `--sysroot`.
347///
348/// Not because anything below needs it, since every path this function writes is absolute and
349/// complete, but because a linker script inside the sysroot resolves the names in it against this.
350/// On a real distribution `libc.so` is such a script, and without this the names in one found under
351/// a sysroot are looked for on the host.
352fn sysroot_flag(sysroot: &Sysroot) -> String {
353    format!("--sysroot={}", sysroot.root().display())
354}
355
356/// Everything after the flags: the start files, the search directories, the inputs, the libraries
357/// and whatever the user told the linker directly.
358///
359/// One function for both formats, because none of this differs between them. What has to be linked
360/// is [`LinkLine`]'s answer and it is already a per target one, and `-L`, `-l` and everything a user
361/// hands the linker directly are spelled the same by every linker that reads a GNU command line.
362///
363/// What the user said goes where the user wrote it, in among the inputs, rather than at the end. An
364/// option that brackets the files after it means nothing once it is moved behind them, which is what
365/// [`Item::Linker`] is about.
366fn body(sysroot: &Sysroot, options: &Invocation<'_>) -> Vec<String> {
367    let mut args = Vec::new();
368    let line = LinkLine::for_target(sysroot, options.mode, options.builtins);
369    if !options.no_startfiles {
370        args.extend(shown(&line.start));
371    }
372
373    // The user's search directories first and ours second, which is the order they are written in a
374    // native link too, so that `-L` in front of a sysroot behaves the way somebody passing it
375    // expects. And then nothing else: step 7 is that there is no host directory here at all.
376    for dir in options.search {
377        args.push(format!("-L{}", dir.display()));
378    }
379    args.push(format!("-L{}", sysroot.lib().display()));
380
381    for input in options.inputs {
382        match input {
383            Item::File(path) => args.push(path.display().to_string()),
384            Item::Library(name) => args.push(format!("-l{name}")),
385            Item::Linker(arg) => args.push(arg.clone()),
386        }
387    }
388
389    if !options.no_defaultlibs {
390        args.extend(shown(&libraries(&line, options)));
391    }
392    if !options.no_startfiles {
393        args.extend(shown(&line.end));
394    }
395
396    args
397}
398
399/// The libraries, with ours left off if that is what was asked for.
400///
401/// By the one name in [`BUILTINS`] rather than by position, because the position is
402/// [`LinkLine`]'s business and a caller that knew it would be a second place to fix the day the
403/// order changes.
404fn libraries(line: &LinkLine, options: &Invocation<'_>) -> Vec<PathBuf> {
405    let mut libraries = line.libraries.clone();
406    if options.no_builtins_lib {
407        libraries.retain(|path| path.file_name().is_none_or(|name| name != BUILTINS));
408    }
409    libraries
410}
411
412/// The flags that say how the result is linked, and the loader when there is one.
413///
414/// `-static-pie` is not among them and that is the point of this function being separate. It is a
415/// compiler driver flag, and the three linkers this line has to suit take three flags instead: the
416/// link is static, the result is position independent, and there is explicitly no interpreter,
417/// because a static binary that names one gets one mapped and then relocates itself twice.
418fn mode_flags(target: TargetTuple, mode: LinkMode) -> Vec<String> {
419    let mut args = Vec::new();
420    match mode {
421        LinkMode::Static => args.push("-static".to_owned()),
422        LinkMode::StaticPie => {
423            args.push("-static".to_owned());
424            args.push("-pie".to_owned());
425            args.push("--no-dynamic-linker".to_owned());
426        }
427        LinkMode::Dynamic => args.push("-pie".to_owned()),
428        LinkMode::DynamicNoPie => args.push("-no-pie".to_owned()),
429        LinkMode::Shared => args.push("-shared".to_owned()),
430    }
431    // A shared object is started by whatever loads it, so it names no interpreter even though it is
432    // linked dynamically. That is the one place `is_dynamic` is not the condition.
433    if matches!(mode, LinkMode::Dynamic | LinkMode::DynamicNoPie) {
434        if let Some(path) = loader(target) {
435            args.push("-dynamic-linker".to_owned());
436            args.push(path.to_owned());
437        }
438    }
439    args
440}
441
442/// The flags that are on every ELF line, whatever the target and whatever the mode.
443///
444/// Five answers to defaults nobody wants. An executable stack is a target default several linkers
445/// still assume when no input object says otherwise. `relro` and `now` make the relocation tables
446/// read only before `main` runs, which is the cheapest hardening there is. The unwind table header
447/// is needed by every crash handler and by `backtrace`, in a C program with no exceptions in it.
448/// The GNU hash table is the one a loader from this century reads.
449///
450/// `--build-id=none` is the reproducibility one and it is the interesting one.
451/// `spec/cross-compile/11-linking.md` section 11.4 wants byte identical output from two hosts, and a
452/// build id computed over the inputs carries their absolute paths into the binary. A deterministic
453/// one would also do, and it is a linker's own idea of deterministic rather than ours, so the
454/// absence of one is the answer that holds on all three linkers.
455fn hardening() -> Vec<String> {
456    [
457        "--eh-frame-hdr",
458        "--hash-style=gnu",
459        "-z",
460        "relro",
461        "-z",
462        "now",
463        "-z",
464        "noexecstack",
465        "--build-id=none",
466    ]
467    .iter()
468    .map(|flag| (*flag).to_owned())
469    .collect()
470}
471
472/// The flags that are on every PE line, whatever the target and whatever the mode.
473///
474/// The same job as [`hardening`] above and a different list, because the two formats protect
475/// themselves with different mechanisms. `--dynamicbase` is the PE counterpart of a position
476/// independent executable: the image carries a relocation table either way, and this is the bit in
477/// the header that tells the loader it may use it rather than placing the image where it asks. It is
478/// not a default in GNU ld's PE port, which is the reason it is written here.
479/// `--high-entropy-va` goes with it on a 64-bit target, where it widens the address space the loader
480/// picks from, and means nothing on a 32-bit one. `--nxcompat` is the `noexecstack` of this format.
481///
482/// `--no-insert-timestamp` is the reproducibility one and it is this format's version of
483/// `--build-id=none`. A PE header carries the time it was linked, `spec/cross-compile/11-linking.md`
484/// section 11.4 names it as one of the four ways byte identical output is lost, and a link that
485/// stamps the current second produces a different file every time it runs on one machine, let alone
486/// on two.
487fn pe_hardening(target: TargetTuple) -> Vec<String> {
488    let mut args = vec!["--dynamicbase".to_owned(), "--nxcompat".to_owned()];
489    if target.pointer_width() == 64 {
490        args.push("--high-entropy-va".to_owned());
491    }
492    args.push("--no-insert-timestamp".to_owned());
493    args
494}
495
496/// Which machine a PE linker is to write for, in the name `-m` knows it by.
497///
498/// A different table from [`emulation`] and a much shorter one, because PE has four machine types
499/// that matter against ELF's dozen formats: there is no byte order to spell, since every Windows port
500/// is little endian, and no data model to spell either, since each machine type fixes one.
501///
502/// The names are GNU ld's PE emulations, which `ld.lld` accepts in its MinGW mode for exactly this
503/// reason. `i386pep` is the 64-bit x86 one and `i386pe` the 32-bit one, and the `p` that tells them
504/// apart is PE32+ rather than anything about the architecture, which is a piece of 1990s naming that
505/// nothing can be done about now.
506///
507/// [`None`] for a Windows target in Microsoft's ABI, which is not a gap. These names are GNU ld's
508/// and `lld-link` has never read one: it takes `/MACHINE:X64`, in an argument syntax where the rest
509/// of the line is different too, so there is nothing for a shared table to hold. ARM64EC is
510/// [`None`] for that reason first and for a second one:
511/// `spec/cross-compile/09-libc-stubs.md` refuses its import libraries as well, because an export in
512/// that ABI is a mangled name and a library written the way the others are written links and then
513/// fails to load.
514#[must_use]
515pub fn pe_machine(target: TargetTuple) -> Option<&'static str> {
516    if target.object_format() != ObjectFormat::Coff || target.env() != Env::Gnu {
517        return None;
518    }
519    Some(match target.arch() {
520        Arch::X86_64 => "i386pep",
521        Arch::X86 => "i386pe",
522        Arch::Aarch64 => "arm64pe",
523        Arch::Arm => "thumb2pe",
524        _ => return None,
525    })
526}
527
528/// Paths as the line carries them.
529fn shown(paths: &[PathBuf]) -> Vec<String> {
530    paths.iter().map(|path| path.display().to_string()).collect()
531}
532
533/// Which of the formats one linker can write is meant, in the name `-m` knows it by.
534///
535/// The same names in `ld`, `ld.lld` and `mold`, which is why this is one table rather than one per
536/// linker. They are not derivable from the architecture: three of them spell the byte order into
537/// the name, two spell the data model, and the narrow modes of a 64-bit architecture are a different
538/// format rather than a flag on one.
539///
540/// [`None`] for a target whose format is not ELF, which is every one of them rather than wasm alone.
541/// An emulation is an ELF idea: `ld64` takes an architecture and a platform version, and the COFF
542/// linkers take a machine, so a Mach-O target that answered `aarch64linux` here would be answering a
543/// question nobody asked it in a word its linker does not know. Nothing reaches this through
544/// [`argv`], which refuses a non-ELF target before asking, and the answer still has to be right for
545/// the recorded files and for anybody calling it directly.
546#[must_use]
547pub fn emulation(target: TargetTuple) -> Option<&'static str> {
548    if target.object_format() != ObjectFormat::Elf {
549        return None;
550    }
551    let narrow = target.data_model() == DataModel::Ilp32On64;
552    let little = target.endian() == Endian::Little;
553    Some(match target.arch() {
554        Arch::X86_64 if narrow => "elf32_x86_64",
555        Arch::X86_64 => "elf_x86_64",
556        Arch::X86 => "elf_i386",
557        Arch::Aarch64 | Arch::Arm64Ec => match (little, narrow) {
558            (true, false) => "aarch64linux",
559            (true, true) => "aarch64linux32",
560            (false, false) => "aarch64linuxb",
561            (false, true) => "aarch64linux32b",
562        },
563        Arch::Arm if little => "armelf_linux_eabi",
564        Arch::Arm => "armelfb_linux_eabi",
565        Arch::Riscv64 if little => "elf64lriscv",
566        Arch::Riscv64 => "elf64briscv",
567        Arch::Riscv32 if little => "elf32lriscv",
568        Arch::Riscv32 => "elf32briscv",
569        // 32-bit z/Architecture is `elf32_s390` and is not a target here, so there is one row.
570        Arch::S390x => "elf64_s390",
571        Arch::PowerPc64 if little => "elf64lppc",
572        Arch::PowerPc64 => "elf64ppc",
573        Arch::LoongArch64 => "elf64loongarch",
574        // Unreachable, because a wasm target's format is wasm and the check above has already
575        // returned. It is here because the match is exhaustive and a wasm emulation name does not
576        // exist to write in it.
577        Arch::Wasm32 => return None,
578    })
579}
580
581#[cfg(test)]
582mod tests {
583    use std::path::{Path, PathBuf};
584
585    use rucc_tuple::TargetTuple;
586
587    use super::{Invocation, Item, Unsupported, argv, emulation, pe_machine};
588    use crate::layout::Sysroot;
589    use crate::link::LinkMode;
590
591    fn target(spelling: &str) -> TargetTuple {
592        spelling.parse().expect("a tuple the table knows")
593    }
594
595    fn sysroot(spelling: &str) -> Sysroot {
596        Sysroot::in_cache(Path::new("/cache"), target(spelling))
597    }
598
599    /// Where our own runtime is, which is beside the compiler on a real machine and therefore
600    /// nowhere near the sysroot. The driver finds it and hands the path in.
601    fn builtins() -> PathBuf {
602        PathBuf::from("/beside/the/compiler/librucc_builtins.a")
603    }
604
605    fn line(spelling: &str, mode: LinkMode) -> Vec<String> {
606        let one = [Item::File(Path::new("main.o").to_path_buf())];
607        let ours = builtins();
608        let options = Invocation {
609            inputs: &one,
610            output: Some(Path::new("main")),
611            mode,
612            builtins: Some(&ours),
613            ..Invocation::default()
614        };
615        argv(target(spelling), &sysroot(spelling), &options).expect("a line")
616    }
617
618    #[test]
619    fn nothing_on_the_line_comes_from_the_host() {
620        // The mirror of the header search rule, and the property `spec/cross-compile/02-the-goal.md`
621        // claim 5 rests on. Every path is under the sysroot or is what the caller wrote.
622        for spelling in ["aarch64-linux-musl", "x86_64-linux-gnu", "riscv64-linux-musl"] {
623            for mode in [LinkMode::Dynamic, LinkMode::DynamicNoPie, LinkMode::Shared] {
624                for arg in line(spelling, mode) {
625                    let host = ["/usr/lib", "/usr/local", "/lib64/", "/lib/x86_64"]
626                        .iter()
627                        .any(|bad| arg.starts_with(bad));
628                    // The loader is the one absolute path that is not a path on this machine. It is
629                    // read by the kernel on the target, which is why it is written in full.
630                    let is_loader = arg.contains("ld-musl") || arg.contains("ld-linux");
631                    assert!(!host || is_loader, "{spelling} {mode:?} {arg}");
632                }
633            }
634        }
635    }
636
637    #[test]
638    fn every_file_of_ours_is_under_the_sysroot_except_the_runtime_the_caller_named() {
639        let spelling = "aarch64-linux-musl";
640        // The prefix as this host spells it rather than as a literal, because the question is which
641        // directory these files are in and a Windows separator is a backslash.
642        let root = sysroot(spelling).root().display().to_string();
643        let ours = builtins().display().to_string();
644        for arg in line(spelling, LinkMode::Static) {
645            // The caller's own `main.o` is relative and is theirs. Our runtime is absolute and is
646            // also theirs, because it is the compiler's output for the target and the caller is
647            // what knows where it put it. Everything else absolute is under the sysroot.
648            let named = arg.starts_with('/') && (arg.ends_with(".o") || arg.ends_with(".a"));
649            assert!(!named || arg == ours || arg.starts_with(&root), "{arg}");
650        }
651    }
652
653    #[test]
654    fn the_static_line_names_no_loader_because_nothing_will_start_it() {
655        let args = line("aarch64-linux-musl", LinkMode::Static);
656        assert!(args.contains(&"-static".to_owned()), "{args:?}");
657        assert!(!args.contains(&"-dynamic-linker".to_owned()), "{args:?}");
658    }
659
660    #[test]
661    fn a_static_position_independent_link_is_three_flags_and_not_the_driver_one() {
662        // `-static-pie` is a gcc flag and none of the three linkers has it, which is the whole
663        // reason the mode is spelled out rather than passed through.
664        let args = line("x86_64-linux-musl", LinkMode::StaticPie);
665        assert!(!args.iter().any(|arg| arg == "-static-pie"), "{args:?}");
666        for flag in ["-static", "-pie", "--no-dynamic-linker"] {
667            assert!(args.contains(&flag.to_owned()), "{flag} missing from {args:?}");
668        }
669    }
670
671    #[test]
672    fn a_dynamic_program_names_the_loader_that_will_start_it_and_a_shared_object_does_not() {
673        let program = line("x86_64-linux-gnu", LinkMode::Dynamic);
674        let at = program.iter().position(|arg| arg == "-dynamic-linker").expect("the flag");
675        assert_eq!(program[at + 1], "/lib64/ld-linux-x86-64.so.2");
676        let library = line("x86_64-linux-gnu", LinkMode::Shared);
677        assert!(!library.contains(&"-dynamic-linker".to_owned()), "{library:?}");
678        assert!(library.contains(&"-shared".to_owned()), "{library:?}");
679    }
680
681    #[test]
682    fn the_start_file_of_a_program_that_moves_is_not_the_one_of_a_program_that_does_not() {
683        let named = |mode| {
684            line("x86_64-linux-gnu", mode)
685                .iter()
686                .filter_map(|arg| {
687                    Path::new(arg).file_name().map(|n| n.to_string_lossy().into_owned())
688                })
689                .find(|name| name.ends_with("crt1.o"))
690        };
691        assert_eq!(named(LinkMode::Dynamic).as_deref(), Some("Scrt1.o"));
692        assert_eq!(named(LinkMode::DynamicNoPie).as_deref(), Some("crt1.o"));
693        assert_eq!(named(LinkMode::Shared), None);
694    }
695
696    #[test]
697    fn the_library_comes_after_the_objects_that_need_it() {
698        let inputs = [Item::File(Path::new("main.o").to_path_buf()), Item::Library("m".to_owned())];
699        let options =
700            Invocation { inputs: &inputs, mode: LinkMode::Static, ..Invocation::default() };
701        let args = argv(target("x86_64-linux-musl"), &sysroot("x86_64-linux-musl"), &options)
702            .expect("a line");
703        let object = args.iter().position(|arg| arg == "main.o").expect("the object");
704        let asked = args.iter().position(|arg| arg == "-lm").expect("the library");
705        let libc = args.iter().position(|arg| arg.ends_with("libc.a")).expect("the libc");
706        let end = args.iter().position(|arg| arg.ends_with("crtn.o")).expect("the end file");
707        assert!(object < asked && asked < libc && libc < end, "{args:?}");
708    }
709
710    #[test]
711    fn a_static_glibc_link_is_refused_by_name_rather_than_attempted() {
712        // A stub has no code in it, so there is nothing for a static link to take. Saying that is
713        // the whole value here: the alternative is a line that produces several thousand undefined
714        // symbols and a user reading the first forty of them.
715        let options = Invocation { mode: LinkMode::Static, ..Invocation::default() };
716        let error = argv(target("x86_64-linux-gnu"), &sysroot("x86_64-linux-gnu"), &options)
717            .expect_err("refused");
718        assert!(matches!(error, Unsupported::StaticStub { .. }), "{error:?}");
719        assert!(error.to_string().contains("musl"), "the way out is not in the message");
720        // And a musl target links statically, which is the exit criterion of #618.
721        assert!(argv(target("x86_64-linux-musl"), &sysroot("x86_64-linux-musl"), &options).is_ok());
722    }
723
724    #[test]
725    fn a_format_with_no_line_of_its_own_is_refused_by_name_rather_than_approximated() {
726        for spelling in ["aarch64-macos", "wasm32-wasi"] {
727            let options = Invocation { mode: LinkMode::Dynamic, ..Invocation::default() };
728            let error =
729                argv(target(spelling), &sysroot(spelling), &options).expect_err("no line for it");
730            assert!(matches!(error, Unsupported::Format { .. }), "{spelling} {error:?}");
731        }
732    }
733
734    #[test]
735    fn the_msvc_abi_is_refused_on_its_own_grounds_and_the_way_out_is_in_the_message() {
736        // Not the format, because mingw-w64 has a line and is the same format. What is missing is an
737        // SDK nobody may redistribute and a linker with a different command line, and the two are
738        // different kinds of missing, so the message names the environment that needs neither.
739        for spelling in ["x86_64-windows-msvc", "aarch64-windows-msvc", "arm64ec-windows-msvc"] {
740            let options = Invocation { mode: LinkMode::Dynamic, ..Invocation::default() };
741            let error = argv(target(spelling), &sysroot(spelling), &options).expect_err("refused");
742            assert!(matches!(error, Unsupported::MsvcAbi { .. }), "{spelling} {error:?}");
743            assert!(error.to_string().contains("mingw-w64"), "{spelling} {error}");
744        }
745    }
746
747    #[test]
748    fn what_the_user_told_the_linker_stays_where_the_user_wrote_it() {
749        // The pair libtool writes around a set of convenience archives. Both words bracket the files
750        // between them, so a line that collects them and appends them to the end has two options
751        // that say nothing and an archive that went in empty. Written in the middle here for that
752        // reason: what is checked is the position rather than the presence.
753        let inputs = [
754            Item::File(Path::new("main.o").to_path_buf()),
755            Item::Linker("--whole-archive".to_owned()),
756            Item::File(Path::new("libaesni.a").to_path_buf()),
757            Item::Linker("--no-whole-archive".to_owned()),
758            Item::Library("m".to_owned()),
759        ];
760        let options =
761            Invocation { mode: LinkMode::Dynamic, inputs: &inputs, ..Invocation::default() };
762        let args = argv(target("x86_64-linux-gnu"), &sysroot("x86_64-linux-gnu"), &options)
763            .expect("a line");
764        let at = |what: &str| args.iter().position(|arg| arg == what).expect(what);
765        assert!(at("main.o") < at("--whole-archive"), "{args:?}");
766        assert!(at("--whole-archive") < at("libaesni.a"), "{args:?}");
767        assert!(at("libaesni.a") < at("--no-whole-archive"), "{args:?}");
768        assert!(at("--no-whole-archive") < at("-lm"), "{args:?}");
769        // And still in front of the libc and the end start files, which are ours and go after every
770        // input whatever kind each one turned out to be.
771        assert!(args.iter().position(|arg| arg.ends_with("crtn.o")).expect("crtn") > at("-lm"));
772    }
773
774    #[test]
775    fn asking_for_no_start_files_leaves_out_both_ends_of_them() {
776        let options =
777            Invocation { mode: LinkMode::Dynamic, no_startfiles: true, ..Invocation::default() };
778        let args = argv(target("x86_64-linux-gnu"), &sysroot("x86_64-linux-gnu"), &options)
779            .expect("a line");
780        assert!(!args.iter().any(|arg| arg.ends_with("crt1.o")), "{args:?}");
781        assert!(!args.iter().any(|arg| arg.ends_with("crtn.o")), "{args:?}");
782        // And still links against the libc, because that is the other flag.
783        assert!(args.iter().any(|arg| arg.ends_with("libc.so")), "{args:?}");
784    }
785
786    #[test]
787    fn a_narrow_mode_of_a_wide_architecture_is_a_different_output_format() {
788        // The row that proves the data model belongs in the tuple. Linking x32 as `elf_x86_64`
789        // produces 64-bit pointers for a target whose pointers are 32 bits.
790        assert_eq!(emulation(target("x86_64-linux-gnux32")), Some("elf32_x86_64"));
791        assert_eq!(emulation(target("x86_64-linux-gnu")), Some("elf_x86_64"));
792    }
793
794    #[test]
795    fn byte_order_is_in_the_output_format_name() {
796        assert_eq!(emulation(target("s390x-linux-gnu")), Some("elf64_s390"));
797        assert_eq!(emulation(target("powerpc64le-linux-gnu")), Some("elf64lppc"));
798        assert_eq!(emulation(target("riscv64-linux-musl")), Some("elf64lriscv"));
799    }
800
801    /// The two flags that are about the line rather than about the target.
802    ///
803    /// `-rdynamic` is a flag the linker has and `-fno-builtins-lib` is one it does not, so one of
804    /// them appears and the other one takes a path away, and both are here because a flag the cross
805    /// line ignored would be a flag that works natively and stops working the moment the target is
806    /// somebody else's.
807    #[test]
808    fn rdynamic_reaches_the_linker_and_no_builtins_lib_takes_our_runtime_off() {
809        let one = [Item::File(Path::new("main.o").to_path_buf())];
810        let ours = builtins();
811        let both = Invocation {
812            inputs: &one,
813            output: Some(Path::new("main")),
814            mode: LinkMode::Dynamic,
815            export_dynamic: true,
816            no_builtins_lib: true,
817            // Found on the machine and still left off, which is what the flag is. A line built
818            // with no runtime to name would pass this test without the flag doing anything.
819            builtins: Some(&ours),
820            ..Invocation::default()
821        };
822        let spelling = "x86_64-linux-musl";
823        let args = argv(target(spelling), &sysroot(spelling), &both).expect("a line");
824        assert!(args.contains(&"--export-dynamic".to_owned()), "{args:?}");
825        assert!(!args.iter().any(|arg| arg.ends_with("librucc_builtins.a")), "{args:?}");
826        // And the libc it was asked to keep is still there, because that is the other flag.
827        assert!(args.iter().any(|arg| arg.ends_with("libc.a")), "{args:?}");
828    }
829
830    #[test]
831    fn a_mingw_line_names_the_pe_machine_and_the_subsystem_and_no_loader() {
832        let args = line("x86_64-windows-gnu", LinkMode::Dynamic);
833        let at = args.iter().position(|arg| arg == "-m").expect("the machine flag");
834        assert_eq!(args[at + 1], "i386pep");
835        let at = args.iter().position(|arg| arg == "--subsystem").expect("the subsystem flag");
836        assert_eq!(args[at + 1], "console");
837        // A PE image names no interpreter and carries a relocation table whatever it is linked as,
838        // so the two flags that answer those questions on ELF have nothing to say here.
839        for absent in ["-dynamic-linker", "-pie", "-no-pie", "--eh-frame-hdr"] {
840            assert!(!args.contains(&absent.to_owned()), "{absent} in {args:?}");
841        }
842    }
843
844    #[test]
845    fn a_mingw_line_carries_the_crt_and_the_win32_libraries_in_single_pass_order() {
846        let args = line("x86_64-windows-gnu", LinkMode::Dynamic);
847        let at = |name: &str| {
848            args.iter().position(|arg| arg.ends_with(name)).unwrap_or_else(|| panic!("{name}"))
849        };
850        // One start file and no end file, because PE has no `.init` and `.fini` for a pair of them
851        // to open and close.
852        assert!(at("crt2.o") < at("main.o"), "{args:?}");
853        assert!(!args.iter().any(|arg| arg.ends_with("crtn.o")), "{args:?}");
854        // Then a library after everything that calls into it, which is what GNU ld's PE port needs
855        // and what lld's COFF linker does not care about.
856        assert!(at("main.o") < at("libmingw32.a"), "{args:?}");
857        assert!(at("libmingwex.a") < at("libmsvcrt.a"), "{args:?}");
858        assert!(at("libmsvcrt.a") < at("libkernel32.a"), "{args:?}");
859        assert!(at("libkernel32.a") < at("librucc_builtins.a"), "{args:?}");
860    }
861
862    #[test]
863    fn a_dll_takes_the_other_start_file_and_no_subsystem() {
864        let args = line("x86_64-windows-gnu", LinkMode::Shared);
865        assert!(args.contains(&"-shared".to_owned()), "{args:?}");
866        // By file name rather than by suffix, since `dllcrt2.o` ends with the other one's name.
867        let named = |name: &str| {
868            args.iter().any(|arg| Path::new(arg).file_name().is_some_and(|file| file == name))
869        };
870        assert!(named("dllcrt2.o"), "{args:?}");
871        assert!(!named("crt2.o"), "{args:?}");
872        assert!(!args.contains(&"--subsystem".to_owned()), "{args:?}");
873    }
874
875    #[test]
876    fn a_static_windows_link_is_not_refused_because_the_crt_there_is_a_dll_on_every_machine() {
877        // The difference between an import library and a stub shared object that shows up on the
878        // line. `-static` on Windows is a statement about our libraries rather than about the CRT,
879        // and the program it produces runs, which is why the refusal is about `Libc::Stub` by name.
880        let args = line("x86_64-windows-gnu", LinkMode::Static);
881        assert!(args.contains(&"-static".to_owned()), "{args:?}");
882        assert!(args.iter().any(|arg| arg.ends_with("libmsvcrt.a")), "{args:?}");
883    }
884
885    #[test]
886    fn the_pe_header_carries_no_timestamp_so_that_two_links_produce_one_file() {
887        // Section 11.4's second cause of a host reaching a binary, and the PE counterpart of
888        // `--build-id=none`. A stamped header differs between two runs on one machine.
889        for spelling in ["x86_64-windows-gnu", "i686-windows-gnu", "aarch64-windows-gnu"] {
890            let args = line(spelling, LinkMode::Dynamic);
891            assert!(args.contains(&"--no-insert-timestamp".to_owned()), "{spelling} {args:?}");
892            assert!(args.contains(&"--dynamicbase".to_owned()), "{spelling} {args:?}");
893            // The wide address space is a 64-bit idea and i686 has no room for it.
894            let wide = args.contains(&"--high-entropy-va".to_owned());
895            assert_eq!(wide, spelling != "i686-windows-gnu", "{spelling} {args:?}");
896        }
897    }
898
899    #[test]
900    fn the_pe_machine_is_the_one_the_linker_knows_and_not_the_one_the_architecture_is_called() {
901        assert_eq!(pe_machine(target("x86_64-windows-gnu")), Some("i386pep"));
902        assert_eq!(pe_machine(target("i686-windows-gnu")), Some("i386pe"));
903        assert_eq!(pe_machine(target("aarch64-windows-gnu")), Some("arm64pe"));
904        // An ELF target has no PE machine, the same way a PE target has no ELF emulation. And
905        // neither has the MSVC ABI, whose linker takes `/MACHINE:X64` and reads none of these names.
906        assert_eq!(pe_machine(target("x86_64-linux-gnu")), None);
907        assert_eq!(pe_machine(target("x86_64-windows-msvc")), None);
908        assert_eq!(emulation(target("x86_64-windows-gnu")), None);
909    }
910
911    #[test]
912    fn a_format_with_no_emulation_names_none_rather_than_its_architecture_s() {
913        // An emulation is an ELF idea. A Mach-O target whose architecture is also an ELF one would
914        // otherwise answer `aarch64linux` here, which is a word `ld64` has never heard and exactly
915        // the almost-right answer `spec/cross-compile/06-abis.md` opens by warning about.
916        for spelling in
917            ["aarch64-macos", "x86_64-windows-gnu", "x86_64-windows-msvc", "wasm32-wasi"]
918        {
919            assert_eq!(emulation(target(spelling)), None, "{spelling}");
920        }
921    }
922
923    #[test]
924    fn a_freestanding_link_has_no_libc_and_no_start_files_and_still_has_our_runtime() {
925        // Section 8.2's first row is nine headers and no link inputs, so there is no `crt1.o` to
926        // name and no `libc.a` either. The builtins stay, because a 32-bit target doing 64-bit
927        // arithmetic reaches them whether a libc exists or not.
928        let args = line("armv7m-none-eabi", LinkMode::Static);
929        assert!(!args.iter().any(|arg| arg.ends_with("crt1.o")), "{args:?}");
930        assert!(!args.iter().any(|arg| arg.ends_with("crti.o")), "{args:?}");
931        assert!(!args.iter().any(|arg| arg.ends_with("crtn.o")), "{args:?}");
932        assert!(!args.iter().any(|arg| arg.ends_with("libc.a")), "{args:?}");
933        assert!(args.iter().any(|arg| arg.ends_with("librucc_builtins.a")), "{args:?}");
934        // And it is a static link with nothing to interpret it, which is what a bare metal target is.
935        assert!(args.contains(&"-static".to_owned()), "{args:?}");
936        assert!(!args.contains(&"-dynamic-linker".to_owned()), "{args:?}");
937    }
938
939    #[test]
940    fn the_platforms_whose_libc_we_stub_refuse_a_static_link_too_and_not_only_glibc() {
941        // The refusal follows from the sysroot holding a stub rather than from the target being a
942        // glibc one. bionic and the BSDs are in the same position for the same reason, and a line
943        // that pretended otherwise would fail in the linker instead of here.
944        for spelling in ["aarch64-linux-android", "x86_64-freebsd", "x86_64-illumos"] {
945            let options = Invocation { mode: LinkMode::Static, ..Invocation::default() };
946            let error = argv(target(spelling), &sysroot(spelling), &options).expect_err("refused");
947            assert!(matches!(error, Unsupported::StaticStub { .. }), "{spelling} {error:?}");
948        }
949    }
950
951    #[test]
952    fn the_same_line_comes_out_every_time_it_is_asked_for() {
953        // Claim 5 in the smallest form it has: the function reads nothing but its arguments, so
954        // two calls agree and so do two hosts.
955        for mode in [LinkMode::Dynamic, LinkMode::Shared] {
956            assert_eq!(line("aarch64-linux-gnu", mode), line("aarch64-linux-gnu", mode));
957        }
958    }
959}