rustc_metadata/
locator.rs

1//! Finds crate binaries and loads their metadata
2//!
3//! Might I be the first to welcome you to a world of platform differences,
4//! version requirements, dependency graphs, conflicting desires, and fun! This
5//! is the major guts (along with metadata::creader) of the compiler for loading
6//! crates and resolving dependencies. Let's take a tour!
7//!
8//! # The problem
9//!
10//! Each invocation of the compiler is immediately concerned with one primary
11//! problem, to connect a set of crates to resolved crates on the filesystem.
12//! Concretely speaking, the compiler follows roughly these steps to get here:
13//!
14//! 1. Discover a set of `extern crate` statements.
15//! 2. Transform these directives into crate names. If the directive does not
16//!    have an explicit name, then the identifier is the name.
17//! 3. For each of these crate names, find a corresponding crate on the
18//!    filesystem.
19//!
20//! Sounds easy, right? Let's walk into some of the nuances.
21//!
22//! ## Transitive Dependencies
23//!
24//! Let's say we've got three crates: A, B, and C. A depends on B, and B depends
25//! on C. When we're compiling A, we primarily need to find and locate B, but we
26//! also end up needing to find and locate C as well.
27//!
28//! The reason for this is that any of B's types could be composed of C's types,
29//! any function in B could return a type from C, etc. To be able to guarantee
30//! that we can always type-check/translate any function, we have to have
31//! complete knowledge of the whole ecosystem, not just our immediate
32//! dependencies.
33//!
34//! So now as part of the "find a corresponding crate on the filesystem" step
35//! above, this involves also finding all crates for *all upstream
36//! dependencies*. This includes all dependencies transitively.
37//!
38//! ## Rlibs and Dylibs
39//!
40//! The compiler has two forms of intermediate dependencies. These are dubbed
41//! rlibs and dylibs for the static and dynamic variants, respectively. An rlib
42//! is a rustc-defined file format (currently just an ar archive) while a dylib
43//! is a platform-defined dynamic library. Each library has a metadata somewhere
44//! inside of it.
45//!
46//! A third kind of dependency is an rmeta file. These are metadata files and do
47//! not contain any code, etc. To a first approximation, these are treated in the
48//! same way as rlibs. Where there is both an rlib and an rmeta file, the rlib
49//! gets priority (even if the rmeta file is newer). An rmeta file is only
50//! useful for checking a downstream crate, attempting to link one will cause an
51//! error.
52//!
53//! When translating a crate name to a crate on the filesystem, we all of a
54//! sudden need to take into account both rlibs and dylibs! Linkage later on may
55//! use either one of these files, as each has their pros/cons. The job of crate
56//! loading is to discover what's possible by finding all candidates.
57//!
58//! Most parts of this loading systems keep the dylib/rlib as just separate
59//! variables.
60//!
61//! ## Where to look?
62//!
63//! We can't exactly scan your whole hard drive when looking for dependencies,
64//! so we need to places to look. Currently the compiler will implicitly add the
65//! target lib search path ($prefix/lib/rustlib/$target/lib) to any compilation,
66//! and otherwise all -L flags are added to the search paths.
67//!
68//! ## What criterion to select on?
69//!
70//! This is a pretty tricky area of loading crates. Given a file, how do we know
71//! whether it's the right crate? Currently, the rules look along these lines:
72//!
73//! 1. Does the filename match an rlib/dylib pattern? That is to say, does the
74//!    filename have the right prefix/suffix?
75//! 2. Does the filename have the right prefix for the crate name being queried?
76//!    This is filtering for files like `libfoo*.rlib` and such. If the crate
77//!    we're looking for was originally compiled with -C extra-filename, the
78//!    extra filename will be included in this prefix to reduce reading
79//!    metadata from crates that would otherwise share our prefix.
80//! 3. Is the file an actual rust library? This is done by loading the metadata
81//!    from the library and making sure it's actually there.
82//! 4. Does the name in the metadata agree with the name of the library?
83//! 5. Does the target in the metadata agree with the current target?
84//! 6. Does the SVH match? (more on this later)
85//!
86//! If the file answers `yes` to all these questions, then the file is
87//! considered as being *candidate* for being accepted. It is illegal to have
88//! more than two candidates as the compiler has no method by which to resolve
89//! this conflict. Additionally, rlib/dylib candidates are considered
90//! separately.
91//!
92//! After all this has happened, we have 1 or two files as candidates. These
93//! represent the rlib/dylib file found for a library, and they're returned as
94//! being found.
95//!
96//! ### What about versions?
97//!
98//! A lot of effort has been put forth to remove versioning from the compiler.
99//! There have been forays in the past to have versioning baked in, but it was
100//! largely always deemed insufficient to the point that it was recognized that
101//! it's probably something the compiler shouldn't do anyway due to its
102//! complicated nature and the state of the half-baked solutions.
103//!
104//! With a departure from versioning, the primary criterion for loading crates
105//! is just the name of a crate. If we stopped here, it would imply that you
106//! could never link two crates of the same name from different sources
107//! together, which is clearly a bad state to be in.
108//!
109//! To resolve this problem, we come to the next section!
110//!
111//! # Expert Mode
112//!
113//! A number of flags have been added to the compiler to solve the "version
114//! problem" in the previous section, as well as generally enabling more
115//! powerful usage of the crate loading system of the compiler. The goal of
116//! these flags and options are to enable third-party tools to drive the
117//! compiler with prior knowledge about how the world should look.
118//!
119//! ## The `--extern` flag
120//!
121//! The compiler accepts a flag of this form a number of times:
122//!
123//! ```text
124//! --extern crate-name=path/to/the/crate.rlib
125//! ```
126//!
127//! This flag is basically the following letter to the compiler:
128//!
129//! > Dear rustc,
130//! >
131//! > When you are attempting to load the immediate dependency `crate-name`, I
132//! > would like you to assume that the library is located at
133//! > `path/to/the/crate.rlib`, and look nowhere else. Also, please do not
134//! > assume that the path I specified has the name `crate-name`.
135//!
136//! This flag basically overrides most matching logic except for validating that
137//! the file is indeed a rust library. The same `crate-name` can be specified
138//! twice to specify the rlib/dylib pair.
139//!
140//! ## Enabling "multiple versions"
141//!
142//! This basically boils down to the ability to specify arbitrary packages to
143//! the compiler. For example, if crate A wanted to use Bv1 and Bv2, then it
144//! would look something like:
145//!
146//! ```compile_fail,E0463
147//! extern crate b1;
148//! extern crate b2;
149//!
150//! fn main() {}
151//! ```
152//!
153//! and the compiler would be invoked as:
154//!
155//! ```text
156//! rustc a.rs --extern b1=path/to/libb1.rlib --extern b2=path/to/libb2.rlib
157//! ```
158//!
159//! In this scenario there are two crates named `b` and the compiler must be
160//! manually driven to be informed where each crate is.
161//!
162//! ## Frobbing symbols
163//!
164//! One of the immediate problems with linking the same library together twice
165//! in the same problem is dealing with duplicate symbols. The primary way to
166//! deal with this in rustc is to add hashes to the end of each symbol.
167//!
168//! In order to force hashes to change between versions of a library, if
169//! desired, the compiler exposes an option `-C metadata=foo`, which is used to
170//! initially seed each symbol hash. The string `foo` is prepended to each
171//! string-to-hash to ensure that symbols change over time.
172//!
173//! ## Loading transitive dependencies
174//!
175//! Dealing with same-named-but-distinct crates is not just a local problem, but
176//! one that also needs to be dealt with for transitive dependencies. Note that
177//! in the letter above `--extern` flags only apply to the *local* set of
178//! dependencies, not the upstream transitive dependencies. Consider this
179//! dependency graph:
180//!
181//! ```text
182//! A.1   A.2
183//! |     |
184//! |     |
185//! B     C
186//!  \   /
187//!   \ /
188//!    D
189//! ```
190//!
191//! In this scenario, when we compile `D`, we need to be able to distinctly
192//! resolve `A.1` and `A.2`, but an `--extern` flag cannot apply to these
193//! transitive dependencies.
194//!
195//! Note that the key idea here is that `B` and `C` are both *already compiled*.
196//! That is, they have already resolved their dependencies. Due to unrelated
197//! technical reasons, when a library is compiled, it is only compatible with
198//! the *exact same* version of the upstream libraries it was compiled against.
199//! We use the "Strict Version Hash" to identify the exact copy of an upstream
200//! library.
201//!
202//! With this knowledge, we know that `B` and `C` will depend on `A` with
203//! different SVH values, so we crawl the normal `-L` paths looking for
204//! `liba*.rlib` and filter based on the contained SVH.
205//!
206//! In the end, this ends up not needing `--extern` to specify upstream
207//! transitive dependencies.
208//!
209//! # Wrapping up
210//!
211//! That's the general overview of loading crates in the compiler, but it's by
212//! no means all of the necessary details. Take a look at the rest of
213//! metadata::locator or metadata::creader for all the juicy details!
214
215use std::borrow::Cow;
216use std::io::{Result as IoResult, Write};
217use std::ops::Deref;
218use std::path::{Path, PathBuf};
219use std::{cmp, fmt};
220
221use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
222use rustc_data_structures::memmap::Mmap;
223use rustc_data_structures::owned_slice::slice_owned;
224use rustc_data_structures::svh::Svh;
225use rustc_errors::{DiagArgValue, IntoDiagArg};
226use rustc_fs_util::try_canonicalize;
227use rustc_session::Session;
228use rustc_session::cstore::CrateSource;
229use rustc_session::filesearch::FileSearch;
230use rustc_session::search_paths::PathKind;
231use rustc_session::utils::CanonicalizedPath;
232use rustc_span::{Span, Symbol};
233use rustc_target::spec::{Target, TargetTuple};
234use tracing::{debug, info};
235
236use crate::creader::{Library, MetadataLoader};
237use crate::errors;
238use crate::rmeta::{METADATA_HEADER, MetadataBlob, rustc_version};
239
240#[derive(Clone)]
241pub(crate) struct CrateLocator<'a> {
242    // Immutable per-session configuration.
243    only_needs_metadata: bool,
244    sysroot: &'a Path,
245    metadata_loader: &'a dyn MetadataLoader,
246    cfg_version: &'static str,
247
248    // Immutable per-search configuration.
249    crate_name: Symbol,
250    exact_paths: Vec<CanonicalizedPath>,
251    pub hash: Option<Svh>,
252    extra_filename: Option<&'a str>,
253    pub target: &'a Target,
254    pub tuple: TargetTuple,
255    pub filesearch: &'a FileSearch,
256    pub is_proc_macro: bool,
257
258    pub path_kind: PathKind,
259    // Mutable in-progress state or output.
260    crate_rejections: CrateRejections,
261}
262
263#[derive(Clone)]
264pub(crate) struct CratePaths {
265    pub(crate) name: Symbol,
266    source: CrateSource,
267}
268
269impl CratePaths {
270    pub(crate) fn new(name: Symbol, source: CrateSource) -> CratePaths {
271        CratePaths { name, source }
272    }
273}
274
275#[derive(Copy, Clone, PartialEq)]
276pub(crate) enum CrateFlavor {
277    Rlib,
278    Rmeta,
279    Dylib,
280}
281
282impl fmt::Display for CrateFlavor {
283    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
284        f.write_str(match *self {
285            CrateFlavor::Rlib => "rlib",
286            CrateFlavor::Rmeta => "rmeta",
287            CrateFlavor::Dylib => "dylib",
288        })
289    }
290}
291
292impl IntoDiagArg for CrateFlavor {
293    fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
294        match self {
295            CrateFlavor::Rlib => DiagArgValue::Str(Cow::Borrowed("rlib")),
296            CrateFlavor::Rmeta => DiagArgValue::Str(Cow::Borrowed("rmeta")),
297            CrateFlavor::Dylib => DiagArgValue::Str(Cow::Borrowed("dylib")),
298        }
299    }
300}
301
302impl<'a> CrateLocator<'a> {
303    pub(crate) fn new(
304        sess: &'a Session,
305        metadata_loader: &'a dyn MetadataLoader,
306        crate_name: Symbol,
307        is_rlib: bool,
308        hash: Option<Svh>,
309        extra_filename: Option<&'a str>,
310        path_kind: PathKind,
311    ) -> CrateLocator<'a> {
312        let needs_object_code = sess.opts.output_types.should_codegen();
313        // If we're producing an rlib, then we don't need object code.
314        // Or, if we're not producing object code, then we don't need it either
315        // (e.g., if we're a cdylib but emitting just metadata).
316        let only_needs_metadata = is_rlib || !needs_object_code;
317
318        CrateLocator {
319            only_needs_metadata,
320            sysroot: &sess.sysroot,
321            metadata_loader,
322            cfg_version: sess.cfg_version,
323            crate_name,
324            exact_paths: if hash.is_none() {
325                sess.opts
326                    .externs
327                    .get(crate_name.as_str())
328                    .into_iter()
329                    .filter_map(|entry| entry.files())
330                    .flatten()
331                    .cloned()
332                    .collect()
333            } else {
334                // SVH being specified means this is a transitive dependency,
335                // so `--extern` options do not apply.
336                Vec::new()
337            },
338            hash,
339            extra_filename,
340            target: &sess.target,
341            tuple: sess.opts.target_triple.clone(),
342            filesearch: sess.target_filesearch(),
343            path_kind,
344            is_proc_macro: false,
345            crate_rejections: CrateRejections::default(),
346        }
347    }
348
349    pub(crate) fn reset(&mut self) {
350        self.crate_rejections.via_hash.clear();
351        self.crate_rejections.via_triple.clear();
352        self.crate_rejections.via_kind.clear();
353        self.crate_rejections.via_version.clear();
354        self.crate_rejections.via_filename.clear();
355        self.crate_rejections.via_invalid.clear();
356    }
357
358    pub(crate) fn maybe_load_library_crate(&mut self) -> Result<Option<Library>, CrateError> {
359        if !self.exact_paths.is_empty() {
360            return self.find_commandline_library();
361        }
362        let mut seen_paths = FxHashSet::default();
363        if let Some(extra_filename) = self.extra_filename {
364            if let library @ Some(_) = self.find_library_crate(extra_filename, &mut seen_paths)? {
365                return Ok(library);
366            }
367        }
368        self.find_library_crate("", &mut seen_paths)
369    }
370
371    fn find_library_crate(
372        &mut self,
373        extra_prefix: &str,
374        seen_paths: &mut FxHashSet<PathBuf>,
375    ) -> Result<Option<Library>, CrateError> {
376        let rmeta_prefix = &format!("lib{}{}", self.crate_name, extra_prefix);
377        let rlib_prefix = rmeta_prefix;
378        let dylib_prefix =
379            &format!("{}{}{}", self.target.dll_prefix, self.crate_name, extra_prefix);
380        let staticlib_prefix =
381            &format!("{}{}{}", self.target.staticlib_prefix, self.crate_name, extra_prefix);
382
383        let rmeta_suffix = ".rmeta";
384        let rlib_suffix = ".rlib";
385        let dylib_suffix = &self.target.dll_suffix;
386        let staticlib_suffix = &self.target.staticlib_suffix;
387
388        let mut candidates: FxIndexMap<_, (FxIndexMap<_, _>, FxIndexMap<_, _>, FxIndexMap<_, _>)> =
389            Default::default();
390
391        // First, find all possible candidate rlibs and dylibs purely based on
392        // the name of the files themselves. We're trying to match against an
393        // exact crate name and a possibly an exact hash.
394        //
395        // During this step, we can filter all found libraries based on the
396        // name and id found in the crate id (we ignore the path portion for
397        // filename matching), as well as the exact hash (if specified). If we
398        // end up having many candidates, we must look at the metadata to
399        // perform exact matches against hashes/crate ids. Note that opening up
400        // the metadata is where we do an exact match against the full contents
401        // of the crate id (path/name/id).
402        //
403        // The goal of this step is to look at as little metadata as possible.
404        // Unfortunately, the prefix-based matching sometimes is over-eager.
405        // E.g. if `rlib_suffix` is `libstd` it'll match the file
406        // `libstd_detect-8d6701fb958915ad.rlib` (incorrect) as well as
407        // `libstd-f3ab5b1dea981f17.rlib` (correct). But this is hard to avoid
408        // given that `extra_filename` comes from the `-C extra-filename`
409        // option and thus can be anything, and the incorrect match will be
410        // handled safely in `extract_one`.
411        for search_path in self.filesearch.search_paths(self.path_kind) {
412            debug!("searching {}", search_path.dir.display());
413            let spf = &search_path.files;
414
415            let mut should_check_staticlibs = true;
416            for (prefix, suffix, kind) in [
417                (rlib_prefix.as_str(), rlib_suffix, CrateFlavor::Rlib),
418                (rmeta_prefix.as_str(), rmeta_suffix, CrateFlavor::Rmeta),
419                (dylib_prefix, dylib_suffix, CrateFlavor::Dylib),
420            ] {
421                if prefix == staticlib_prefix && suffix == staticlib_suffix {
422                    should_check_staticlibs = false;
423                }
424                if let Some(matches) = spf.query(prefix, suffix) {
425                    for (hash, spf) in matches {
426                        info!("lib candidate: {}", spf.path.display());
427
428                        let (rlibs, rmetas, dylibs) =
429                            candidates.entry(hash.to_string()).or_default();
430                        let path =
431                            try_canonicalize(&spf.path).unwrap_or_else(|_| spf.path.to_path_buf());
432                        if seen_paths.contains(&path) {
433                            continue;
434                        };
435                        seen_paths.insert(path.clone());
436                        match kind {
437                            CrateFlavor::Rlib => rlibs.insert(path, search_path.kind),
438                            CrateFlavor::Rmeta => rmetas.insert(path, search_path.kind),
439                            CrateFlavor::Dylib => dylibs.insert(path, search_path.kind),
440                        };
441                    }
442                }
443            }
444            if let Some(static_matches) = should_check_staticlibs
445                .then(|| spf.query(staticlib_prefix, staticlib_suffix))
446                .flatten()
447            {
448                for (_, spf) in static_matches {
449                    self.crate_rejections.via_kind.push(CrateMismatch {
450                        path: spf.path.to_path_buf(),
451                        got: "static".to_string(),
452                    });
453                }
454            }
455        }
456
457        // We have now collected all known libraries into a set of candidates
458        // keyed of the filename hash listed. For each filename, we also have a
459        // list of rlibs/dylibs that apply. Here, we map each of these lists
460        // (per hash), to a Library candidate for returning.
461        //
462        // A Library candidate is created if the metadata for the set of
463        // libraries corresponds to the crate id and hash criteria that this
464        // search is being performed for.
465        let mut libraries = FxIndexMap::default();
466        for (_hash, (rlibs, rmetas, dylibs)) in candidates {
467            if let Some((svh, lib)) = self.extract_lib(rlibs, rmetas, dylibs)? {
468                libraries.insert(svh, lib);
469            }
470        }
471
472        // Having now translated all relevant found hashes into libraries, see
473        // what we've got and figure out if we found multiple candidates for
474        // libraries or not.
475        match libraries.len() {
476            0 => Ok(None),
477            1 => Ok(Some(libraries.into_iter().next().unwrap().1)),
478            _ => {
479                let mut libraries: Vec<_> = libraries.into_values().collect();
480
481                libraries.sort_by_cached_key(|lib| lib.source.paths().next().unwrap().clone());
482                let candidates = libraries
483                    .iter()
484                    .map(|lib| lib.source.paths().next().unwrap().clone())
485                    .collect::<Vec<_>>();
486
487                Err(CrateError::MultipleCandidates(
488                    self.crate_name,
489                    // these are the same for all candidates
490                    get_flavor_from_path(candidates.first().unwrap()),
491                    candidates,
492                ))
493            }
494        }
495    }
496
497    fn extract_lib(
498        &mut self,
499        rlibs: FxIndexMap<PathBuf, PathKind>,
500        rmetas: FxIndexMap<PathBuf, PathKind>,
501        dylibs: FxIndexMap<PathBuf, PathKind>,
502    ) -> Result<Option<(Svh, Library)>, CrateError> {
503        let mut slot = None;
504        // Order here matters, rmeta should come first.
505        //
506        // Make sure there's at most one rlib and at most one dylib.
507        //
508        // See comment in `extract_one` below.
509        let source = CrateSource {
510            rmeta: self.extract_one(rmetas, CrateFlavor::Rmeta, &mut slot)?,
511            rlib: self.extract_one(rlibs, CrateFlavor::Rlib, &mut slot)?,
512            dylib: self.extract_one(dylibs, CrateFlavor::Dylib, &mut slot)?,
513        };
514        Ok(slot.map(|(svh, metadata, _)| (svh, Library { source, metadata })))
515    }
516
517    fn needs_crate_flavor(&self, flavor: CrateFlavor) -> bool {
518        if flavor == CrateFlavor::Dylib && self.is_proc_macro {
519            return true;
520        }
521
522        if self.only_needs_metadata {
523            flavor == CrateFlavor::Rmeta
524        } else {
525            // we need all flavors (perhaps not true, but what we do for now)
526            true
527        }
528    }
529
530    // Attempts to extract *one* library from the set `m`. If the set has no
531    // elements, `None` is returned. If the set has more than one element, then
532    // the errors and notes are emitted about the set of libraries.
533    //
534    // With only one library in the set, this function will extract it, and then
535    // read the metadata from it if `*slot` is `None`. If the metadata couldn't
536    // be read, it is assumed that the file isn't a valid rust library (no
537    // errors are emitted).
538    //
539    // The `PathBuf` in `slot` will only be used for diagnostic purposes.
540    fn extract_one(
541        &mut self,
542        m: FxIndexMap<PathBuf, PathKind>,
543        flavor: CrateFlavor,
544        slot: &mut Option<(Svh, MetadataBlob, PathBuf)>,
545    ) -> Result<Option<(PathBuf, PathKind)>, CrateError> {
546        // If we are producing an rlib, and we've already loaded metadata, then
547        // we should not attempt to discover further crate sources (unless we're
548        // locating a proc macro; exact logic is in needs_crate_flavor). This means
549        // that under -Zbinary-dep-depinfo we will not emit a dependency edge on
550        // the *unused* rlib, and by returning `None` here immediately we
551        // guarantee that we do indeed not use it.
552        //
553        // See also #68149 which provides more detail on why emitting the
554        // dependency on the rlib is a bad thing.
555        if slot.is_some() {
556            if m.is_empty() || !self.needs_crate_flavor(flavor) {
557                return Ok(None);
558            }
559        }
560
561        let mut ret: Option<(PathBuf, PathKind)> = None;
562        let mut err_data: Option<Vec<PathBuf>> = None;
563        for (lib, kind) in m {
564            info!("{} reading metadata from: {}", flavor, lib.display());
565            if flavor == CrateFlavor::Rmeta && lib.metadata().is_ok_and(|m| m.len() == 0) {
566                // Empty files will cause get_metadata_section to fail. Rmeta
567                // files can be empty, for example with binaries (which can
568                // often appear with `cargo check` when checking a library as
569                // a unittest). We don't want to emit a user-visible warning
570                // in this case as it is not a real problem.
571                debug!("skipping empty file");
572                continue;
573            }
574            let (hash, metadata) = match get_metadata_section(
575                self.target,
576                flavor,
577                &lib,
578                self.metadata_loader,
579                self.cfg_version,
580            ) {
581                Ok(blob) => {
582                    if let Some(h) = self.crate_matches(&blob, &lib) {
583                        (h, blob)
584                    } else {
585                        info!("metadata mismatch");
586                        continue;
587                    }
588                }
589                Err(MetadataError::VersionMismatch { expected_version, found_version }) => {
590                    // The file was present and created by the same compiler version, but we
591                    // couldn't load it for some reason. Give a hard error instead of silently
592                    // ignoring it, but only if we would have given an error anyway.
593                    info!(
594                        "Rejecting via version: expected {} got {}",
595                        expected_version, found_version
596                    );
597                    self.crate_rejections
598                        .via_version
599                        .push(CrateMismatch { path: lib, got: found_version });
600                    continue;
601                }
602                Err(MetadataError::LoadFailure(err)) => {
603                    info!("no metadata found: {}", err);
604                    // The file was present and created by the same compiler version, but we
605                    // couldn't load it for some reason. Give a hard error instead of silently
606                    // ignoring it, but only if we would have given an error anyway.
607                    self.crate_rejections.via_invalid.push(CrateMismatch { path: lib, got: err });
608                    continue;
609                }
610                Err(err @ MetadataError::NotPresent(_)) => {
611                    info!("no metadata found: {}", err);
612                    continue;
613                }
614            };
615            // If we see multiple hashes, emit an error about duplicate candidates.
616            if slot.as_ref().is_some_and(|s| s.0 != hash) {
617                if let Some(candidates) = err_data {
618                    return Err(CrateError::MultipleCandidates(
619                        self.crate_name,
620                        flavor,
621                        candidates,
622                    ));
623                }
624                err_data = Some(vec![slot.take().unwrap().2]);
625            }
626            if let Some(candidates) = &mut err_data {
627                candidates.push(lib);
628                continue;
629            }
630
631            // Ok so at this point we've determined that `(lib, kind)` above is
632            // a candidate crate to load, and that `slot` is either none (this
633            // is the first crate of its kind) or if some the previous path has
634            // the exact same hash (e.g., it's the exact same crate).
635            //
636            // In principle these two candidate crates are exactly the same so
637            // we can choose either of them to link. As a stupidly gross hack,
638            // however, we favor crate in the sysroot.
639            //
640            // You can find more info in rust-lang/rust#39518 and various linked
641            // issues, but the general gist is that during testing libstd the
642            // compilers has two candidates to choose from: one in the sysroot
643            // and one in the deps folder. These two crates are the exact same
644            // crate but if the compiler chooses the one in the deps folder
645            // it'll cause spurious errors on Windows.
646            //
647            // As a result, we favor the sysroot crate here. Note that the
648            // candidates are all canonicalized, so we canonicalize the sysroot
649            // as well.
650            if let Some((prev, _)) = &ret {
651                let sysroot = self.sysroot;
652                let sysroot = try_canonicalize(sysroot).unwrap_or_else(|_| sysroot.to_path_buf());
653                if prev.starts_with(&sysroot) {
654                    continue;
655                }
656            }
657            *slot = Some((hash, metadata, lib.clone()));
658            ret = Some((lib, kind));
659        }
660
661        if let Some(candidates) = err_data {
662            Err(CrateError::MultipleCandidates(self.crate_name, flavor, candidates))
663        } else {
664            Ok(ret)
665        }
666    }
667
668    fn crate_matches(&mut self, metadata: &MetadataBlob, libpath: &Path) -> Option<Svh> {
669        let header = metadata.get_header();
670        if header.is_proc_macro_crate != self.is_proc_macro {
671            info!(
672                "Rejecting via proc macro: expected {} got {}",
673                self.is_proc_macro, header.is_proc_macro_crate,
674            );
675            return None;
676        }
677
678        if self.exact_paths.is_empty() && self.crate_name != header.name {
679            info!("Rejecting via crate name");
680            return None;
681        }
682
683        if header.triple != self.tuple {
684            info!("Rejecting via crate triple: expected {} got {}", self.tuple, header.triple);
685            self.crate_rejections.via_triple.push(CrateMismatch {
686                path: libpath.to_path_buf(),
687                got: header.triple.to_string(),
688            });
689            return None;
690        }
691
692        let hash = header.hash;
693        if let Some(expected_hash) = self.hash {
694            if hash != expected_hash {
695                info!("Rejecting via hash: expected {} got {}", expected_hash, hash);
696                self.crate_rejections
697                    .via_hash
698                    .push(CrateMismatch { path: libpath.to_path_buf(), got: hash.to_string() });
699                return None;
700            }
701        }
702
703        Some(hash)
704    }
705
706    fn find_commandline_library(&mut self) -> Result<Option<Library>, CrateError> {
707        // First, filter out all libraries that look suspicious. We only accept
708        // files which actually exist that have the correct naming scheme for
709        // rlibs/dylibs.
710        let mut rlibs = FxIndexMap::default();
711        let mut rmetas = FxIndexMap::default();
712        let mut dylibs = FxIndexMap::default();
713        for loc in &self.exact_paths {
714            let loc_canon = loc.canonicalized();
715            let loc_orig = loc.original();
716            if !loc_canon.exists() {
717                return Err(CrateError::ExternLocationNotExist(self.crate_name, loc_orig.clone()));
718            }
719            if !loc_orig.is_file() {
720                return Err(CrateError::ExternLocationNotFile(self.crate_name, loc_orig.clone()));
721            }
722            // Note to take care and match against the non-canonicalized name:
723            // some systems save build artifacts into content-addressed stores
724            // that do not preserve extensions, and then link to them using
725            // e.g. symbolic links. If we canonicalize too early, we resolve
726            // the symlink, the file type is lost and we might treat rlibs and
727            // rmetas as dylibs.
728            let Some(file) = loc_orig.file_name().and_then(|s| s.to_str()) else {
729                return Err(CrateError::ExternLocationNotFile(self.crate_name, loc_orig.clone()));
730            };
731            // FnMut cannot return reference to captured value, so references
732            // must be taken outside the closure.
733            let rlibs = &mut rlibs;
734            let rmetas = &mut rmetas;
735            let dylibs = &mut dylibs;
736            let type_via_filename = (|| {
737                if file.starts_with("lib") {
738                    if file.ends_with(".rlib") {
739                        return Some(rlibs);
740                    }
741                    if file.ends_with(".rmeta") {
742                        return Some(rmetas);
743                    }
744                }
745                let dll_prefix = self.target.dll_prefix.as_ref();
746                let dll_suffix = self.target.dll_suffix.as_ref();
747                if file.starts_with(dll_prefix) && file.ends_with(dll_suffix) {
748                    return Some(dylibs);
749                }
750                None
751            })();
752            match type_via_filename {
753                Some(type_via_filename) => {
754                    type_via_filename.insert(loc_canon.clone(), PathKind::ExternFlag);
755                }
756                None => {
757                    self.crate_rejections
758                        .via_filename
759                        .push(CrateMismatch { path: loc_orig.clone(), got: String::new() });
760                }
761            }
762        }
763
764        // Extract the dylib/rlib/rmeta triple.
765        self.extract_lib(rlibs, rmetas, dylibs).map(|opt| opt.map(|(_, lib)| lib))
766    }
767
768    pub(crate) fn into_error(self, dep_root: Option<CratePaths>) -> CrateError {
769        CrateError::LocatorCombined(Box::new(CombinedLocatorError {
770            crate_name: self.crate_name,
771            dep_root,
772            triple: self.tuple,
773            dll_prefix: self.target.dll_prefix.to_string(),
774            dll_suffix: self.target.dll_suffix.to_string(),
775            crate_rejections: self.crate_rejections,
776        }))
777    }
778}
779
780fn get_metadata_section<'p>(
781    target: &Target,
782    flavor: CrateFlavor,
783    filename: &'p Path,
784    loader: &dyn MetadataLoader,
785    cfg_version: &'static str,
786) -> Result<MetadataBlob, MetadataError<'p>> {
787    if !filename.exists() {
788        return Err(MetadataError::NotPresent(filename));
789    }
790    let raw_bytes = match flavor {
791        CrateFlavor::Rlib => {
792            loader.get_rlib_metadata(target, filename).map_err(MetadataError::LoadFailure)?
793        }
794        CrateFlavor::Dylib => {
795            let buf =
796                loader.get_dylib_metadata(target, filename).map_err(MetadataError::LoadFailure)?;
797            let header_len = METADATA_HEADER.len();
798            // header + u64 length of data
799            let data_start = header_len + 8;
800
801            debug!("checking {} bytes of metadata-version stamp", header_len);
802            let header = &buf[..cmp::min(header_len, buf.len())];
803            if header != METADATA_HEADER {
804                return Err(MetadataError::LoadFailure(format!(
805                    "invalid metadata version found: {}",
806                    filename.display()
807                )));
808            }
809
810            // Length of the metadata - this allows linkers to pad the section if they want
811            let Ok(len_bytes) =
812                <[u8; 8]>::try_from(&buf[header_len..cmp::min(data_start, buf.len())])
813            else {
814                return Err(MetadataError::LoadFailure(
815                    "invalid metadata length found".to_string(),
816                ));
817            };
818            let metadata_len = u64::from_le_bytes(len_bytes) as usize;
819
820            // Header is okay -> inflate the actual metadata
821            buf.slice(|buf| &buf[data_start..(data_start + metadata_len)])
822        }
823        CrateFlavor::Rmeta => {
824            // mmap the file, because only a small fraction of it is read.
825            let file = std::fs::File::open(filename).map_err(|_| {
826                MetadataError::LoadFailure(format!(
827                    "failed to open rmeta metadata: '{}'",
828                    filename.display()
829                ))
830            })?;
831            let mmap = unsafe { Mmap::map(file) };
832            let mmap = mmap.map_err(|_| {
833                MetadataError::LoadFailure(format!(
834                    "failed to mmap rmeta metadata: '{}'",
835                    filename.display()
836                ))
837            })?;
838
839            slice_owned(mmap, Deref::deref)
840        }
841    };
842    let Ok(blob) = MetadataBlob::new(raw_bytes) else {
843        return Err(MetadataError::LoadFailure(format!(
844            "corrupt metadata encountered in {}",
845            filename.display()
846        )));
847    };
848    match blob.check_compatibility(cfg_version) {
849        Ok(()) => {
850            debug!("metadata blob read okay");
851            Ok(blob)
852        }
853        Err(None) => Err(MetadataError::LoadFailure(format!(
854            "invalid metadata version found: {}",
855            filename.display()
856        ))),
857        Err(Some(found_version)) => {
858            return Err(MetadataError::VersionMismatch {
859                expected_version: rustc_version(cfg_version),
860                found_version,
861            });
862        }
863    }
864}
865
866/// A diagnostic function for dumping crate metadata to an output stream.
867pub fn list_file_metadata(
868    target: &Target,
869    path: &Path,
870    metadata_loader: &dyn MetadataLoader,
871    out: &mut dyn Write,
872    ls_kinds: &[String],
873    cfg_version: &'static str,
874) -> IoResult<()> {
875    let flavor = get_flavor_from_path(path);
876    match get_metadata_section(target, flavor, path, metadata_loader, cfg_version) {
877        Ok(metadata) => metadata.list_crate_metadata(out, ls_kinds),
878        Err(msg) => write!(out, "{msg}\n"),
879    }
880}
881
882fn get_flavor_from_path(path: &Path) -> CrateFlavor {
883    let filename = path.file_name().unwrap().to_str().unwrap();
884
885    if filename.ends_with(".rlib") {
886        CrateFlavor::Rlib
887    } else if filename.ends_with(".rmeta") {
888        CrateFlavor::Rmeta
889    } else {
890        CrateFlavor::Dylib
891    }
892}
893
894// ------------------------------------------ Error reporting -------------------------------------
895
896#[derive(Clone)]
897struct CrateMismatch {
898    path: PathBuf,
899    got: String,
900}
901
902#[derive(Clone, Default)]
903struct CrateRejections {
904    via_hash: Vec<CrateMismatch>,
905    via_triple: Vec<CrateMismatch>,
906    via_kind: Vec<CrateMismatch>,
907    via_version: Vec<CrateMismatch>,
908    via_filename: Vec<CrateMismatch>,
909    via_invalid: Vec<CrateMismatch>,
910}
911
912/// Candidate rejection reasons collected during crate search.
913/// If no candidate is accepted, then these reasons are presented to the user,
914/// otherwise they are ignored.
915pub(crate) struct CombinedLocatorError {
916    crate_name: Symbol,
917    dep_root: Option<CratePaths>,
918    triple: TargetTuple,
919    dll_prefix: String,
920    dll_suffix: String,
921    crate_rejections: CrateRejections,
922}
923
924pub(crate) enum CrateError {
925    NonAsciiName(Symbol),
926    ExternLocationNotExist(Symbol, PathBuf),
927    ExternLocationNotFile(Symbol, PathBuf),
928    MultipleCandidates(Symbol, CrateFlavor, Vec<PathBuf>),
929    SymbolConflictsCurrent(Symbol),
930    StableCrateIdCollision(Symbol, Symbol),
931    DlOpen(String, String),
932    DlSym(String, String),
933    LocatorCombined(Box<CombinedLocatorError>),
934    NotFound(Symbol),
935}
936
937enum MetadataError<'a> {
938    /// The file was missing.
939    NotPresent(&'a Path),
940    /// The file was present and invalid.
941    LoadFailure(String),
942    /// The file was present, but compiled with a different rustc version.
943    VersionMismatch { expected_version: String, found_version: String },
944}
945
946impl fmt::Display for MetadataError<'_> {
947    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
948        match self {
949            MetadataError::NotPresent(filename) => {
950                f.write_str(&format!("no such file: '{}'", filename.display()))
951            }
952            MetadataError::LoadFailure(msg) => f.write_str(msg),
953            MetadataError::VersionMismatch { expected_version, found_version } => {
954                f.write_str(&format!(
955                    "rustc version mismatch. expected {}, found {}",
956                    expected_version, found_version,
957                ))
958            }
959        }
960    }
961}
962
963impl CrateError {
964    pub(crate) fn report(self, sess: &Session, span: Span, missing_core: bool) {
965        let dcx = sess.dcx();
966        match self {
967            CrateError::NonAsciiName(crate_name) => {
968                dcx.emit_err(errors::NonAsciiName { span, crate_name });
969            }
970            CrateError::ExternLocationNotExist(crate_name, loc) => {
971                dcx.emit_err(errors::ExternLocationNotExist { span, crate_name, location: &loc });
972            }
973            CrateError::ExternLocationNotFile(crate_name, loc) => {
974                dcx.emit_err(errors::ExternLocationNotFile { span, crate_name, location: &loc });
975            }
976            CrateError::MultipleCandidates(crate_name, flavor, candidates) => {
977                dcx.emit_err(errors::MultipleCandidates { span, crate_name, flavor, candidates });
978            }
979            CrateError::SymbolConflictsCurrent(root_name) => {
980                dcx.emit_err(errors::SymbolConflictsCurrent { span, crate_name: root_name });
981            }
982            CrateError::StableCrateIdCollision(crate_name0, crate_name1) => {
983                dcx.emit_err(errors::StableCrateIdCollision { span, crate_name0, crate_name1 });
984            }
985            CrateError::DlOpen(path, err) | CrateError::DlSym(path, err) => {
986                dcx.emit_err(errors::DlError { span, path, err });
987            }
988            CrateError::LocatorCombined(locator) => {
989                let crate_name = locator.crate_name;
990                let add_info = match &locator.dep_root {
991                    None => String::new(),
992                    Some(r) => format!(" which `{}` depends on", r.name),
993                };
994                if !locator.crate_rejections.via_filename.is_empty() {
995                    let mismatches = locator.crate_rejections.via_filename.iter();
996                    for CrateMismatch { path, .. } in mismatches {
997                        dcx.emit_err(errors::CrateLocationUnknownType { span, path, crate_name });
998                        dcx.emit_err(errors::LibFilenameForm {
999                            span,
1000                            dll_prefix: &locator.dll_prefix,
1001                            dll_suffix: &locator.dll_suffix,
1002                        });
1003                    }
1004                }
1005                let mut found_crates = String::new();
1006                if !locator.crate_rejections.via_hash.is_empty() {
1007                    let mismatches = locator.crate_rejections.via_hash.iter();
1008                    for CrateMismatch { path, .. } in mismatches {
1009                        found_crates.push_str(&format!(
1010                            "\ncrate `{}`: {}",
1011                            crate_name,
1012                            path.display()
1013                        ));
1014                    }
1015                    if let Some(r) = locator.dep_root {
1016                        for path in r.source.paths() {
1017                            found_crates.push_str(&format!(
1018                                "\ncrate `{}`: {}",
1019                                r.name,
1020                                path.display()
1021                            ));
1022                        }
1023                    }
1024                    dcx.emit_err(errors::NewerCrateVersion {
1025                        span,
1026                        crate_name,
1027                        add_info,
1028                        found_crates,
1029                    });
1030                } else if !locator.crate_rejections.via_triple.is_empty() {
1031                    let mismatches = locator.crate_rejections.via_triple.iter();
1032                    for CrateMismatch { path, got } in mismatches {
1033                        found_crates.push_str(&format!(
1034                            "\ncrate `{}`, target triple {}: {}",
1035                            crate_name,
1036                            got,
1037                            path.display(),
1038                        ));
1039                    }
1040                    dcx.emit_err(errors::NoCrateWithTriple {
1041                        span,
1042                        crate_name,
1043                        locator_triple: locator.triple.tuple(),
1044                        add_info,
1045                        found_crates,
1046                    });
1047                } else if !locator.crate_rejections.via_kind.is_empty() {
1048                    let mismatches = locator.crate_rejections.via_kind.iter();
1049                    for CrateMismatch { path, .. } in mismatches {
1050                        found_crates.push_str(&format!(
1051                            "\ncrate `{}`: {}",
1052                            crate_name,
1053                            path.display()
1054                        ));
1055                    }
1056                    dcx.emit_err(errors::FoundStaticlib {
1057                        span,
1058                        crate_name,
1059                        add_info,
1060                        found_crates,
1061                    });
1062                } else if !locator.crate_rejections.via_version.is_empty() {
1063                    let mismatches = locator.crate_rejections.via_version.iter();
1064                    for CrateMismatch { path, got } in mismatches {
1065                        found_crates.push_str(&format!(
1066                            "\ncrate `{}` compiled by {}: {}",
1067                            crate_name,
1068                            got,
1069                            path.display(),
1070                        ));
1071                    }
1072                    dcx.emit_err(errors::IncompatibleRustc {
1073                        span,
1074                        crate_name,
1075                        add_info,
1076                        found_crates,
1077                        rustc_version: rustc_version(sess.cfg_version),
1078                    });
1079                } else if !locator.crate_rejections.via_invalid.is_empty() {
1080                    let mut crate_rejections = Vec::new();
1081                    for CrateMismatch { path: _, got } in locator.crate_rejections.via_invalid {
1082                        crate_rejections.push(got);
1083                    }
1084                    dcx.emit_err(errors::InvalidMetadataFiles {
1085                        span,
1086                        crate_name,
1087                        add_info,
1088                        crate_rejections,
1089                    });
1090                } else {
1091                    let error = errors::CannotFindCrate {
1092                        span,
1093                        crate_name,
1094                        add_info,
1095                        missing_core,
1096                        current_crate: sess
1097                            .opts
1098                            .crate_name
1099                            .clone()
1100                            .unwrap_or("<unknown>".to_string()),
1101                        is_nightly_build: sess.is_nightly_build(),
1102                        profiler_runtime: Symbol::intern(&sess.opts.unstable_opts.profiler_runtime),
1103                        locator_triple: locator.triple,
1104                        is_ui_testing: sess.opts.unstable_opts.ui_testing,
1105                    };
1106                    // The diagnostic for missing core is very good, but it is followed by a lot of
1107                    // other diagnostics that do not add information.
1108                    if missing_core {
1109                        dcx.emit_fatal(error);
1110                    } else {
1111                        dcx.emit_err(error);
1112                    }
1113                }
1114            }
1115            CrateError::NotFound(crate_name) => {
1116                let error = errors::CannotFindCrate {
1117                    span,
1118                    crate_name,
1119                    add_info: String::new(),
1120                    missing_core,
1121                    current_crate: sess.opts.crate_name.clone().unwrap_or("<unknown>".to_string()),
1122                    is_nightly_build: sess.is_nightly_build(),
1123                    profiler_runtime: Symbol::intern(&sess.opts.unstable_opts.profiler_runtime),
1124                    locator_triple: sess.opts.target_triple.clone(),
1125                    is_ui_testing: sess.opts.unstable_opts.ui_testing,
1126                };
1127                // The diagnostic for missing core is very good, but it is followed by a lot of
1128                // other diagnostics that do not add information.
1129                if missing_core {
1130                    dcx.emit_fatal(error);
1131                } else {
1132                    dcx.emit_err(error);
1133                }
1134            }
1135        }
1136    }
1137}