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, FxIndexSet};
222use rustc_data_structures::memmap::Mmap;
223use rustc_data_structures::owned_slice::{OwnedSlice, slice_owned};
224use rustc_data_structures::svh::Svh;
225use rustc_errors::{DiagArgValue, IntoDiagArg};
226use rustc_fs_util::try_canonicalize;
227use rustc_session::cstore::CrateSource;
228use rustc_session::filesearch::FileSearch;
229use rustc_session::search_paths::PathKind;
230use rustc_session::utils::CanonicalizedPath;
231use rustc_session::{Session, config};
232use rustc_span::{Span, Symbol};
233use rustc_target::spec::{Target, TargetTuple};
234use tempfile::Builder as TempFileBuilder;
235use tracing::{debug, info};
236
237use crate::creader::{Library, MetadataLoader};
238use crate::errors;
239use crate::rmeta::{METADATA_HEADER, MetadataBlob, rustc_version};
240
241#[derive(Clone)]
242pub(crate) struct CrateLocator<'a> {
243    // Immutable per-session configuration.
244    only_needs_metadata: bool,
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    target: &'a Target,
254    tuple: TargetTuple,
255    filesearch: &'a FileSearch,
256    is_proc_macro: bool,
257    path_kind: PathKind,
258}
259
260#[derive(Clone, Debug)]
261pub(crate) struct CratePaths {
262    pub(crate) name: Symbol,
263    source: CrateSource,
264}
265
266impl CratePaths {
267    pub(crate) fn new(name: Symbol, source: CrateSource) -> CratePaths {
268        CratePaths { name, source }
269    }
270}
271
272#[derive(Copy, Clone, Debug, PartialEq)]
273pub(crate) enum CrateFlavor {
274    Rlib,
275    Rmeta,
276    Dylib,
277    SDylib,
278}
279
280impl fmt::Display for CrateFlavor {
281    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
282        f.write_str(match *self {
283            CrateFlavor::Rlib => "rlib",
284            CrateFlavor::Rmeta => "rmeta",
285            CrateFlavor::Dylib => "dylib",
286            CrateFlavor::SDylib => "sdylib",
287        })
288    }
289}
290
291impl IntoDiagArg for CrateFlavor {
292    fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
293        match self {
294            CrateFlavor::Rlib => DiagArgValue::Str(Cow::Borrowed("rlib")),
295            CrateFlavor::Rmeta => DiagArgValue::Str(Cow::Borrowed("rmeta")),
296            CrateFlavor::Dylib => DiagArgValue::Str(Cow::Borrowed("dylib")),
297            CrateFlavor::SDylib => DiagArgValue::Str(Cow::Borrowed("sdylib")),
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            metadata_loader,
321            cfg_version: sess.cfg_version,
322            crate_name,
323            exact_paths: if hash.is_none() {
324                sess.opts
325                    .externs
326                    .get(crate_name.as_str())
327                    .into_iter()
328                    .filter_map(|entry| entry.files())
329                    .flatten()
330                    .cloned()
331                    .collect()
332            } else {
333                // SVH being specified means this is a transitive dependency,
334                // so `--extern` options do not apply.
335                Vec::new()
336            },
337            hash,
338            extra_filename,
339            target: &sess.target,
340            tuple: sess.opts.target_triple.clone(),
341            filesearch: sess.target_filesearch(),
342            path_kind,
343            is_proc_macro: false,
344        }
345    }
346
347    pub(crate) fn for_proc_macro(&mut self, sess: &'a Session, path_kind: PathKind) {
348        self.is_proc_macro = true;
349        self.target = &sess.host;
350        self.tuple = TargetTuple::from_tuple(config::host_tuple());
351        self.filesearch = sess.host_filesearch();
352        self.path_kind = path_kind;
353    }
354
355    pub(crate) fn for_target_proc_macro(&mut self, sess: &'a Session, path_kind: PathKind) {
356        self.is_proc_macro = true;
357        self.target = &sess.target;
358        self.tuple = sess.opts.target_triple.clone();
359        self.filesearch = sess.target_filesearch();
360        self.path_kind = path_kind;
361    }
362
363    pub(crate) fn maybe_load_library_crate(
364        &self,
365        crate_rejections: &mut CrateRejections,
366    ) -> Result<Option<Library>, CrateError> {
367        if !self.exact_paths.is_empty() {
368            return self.find_commandline_library(crate_rejections);
369        }
370        let mut seen_paths = FxHashSet::default();
371        if let Some(extra_filename) = self.extra_filename
372            && let library @ Some(_) =
373                self.find_library_crate(crate_rejections, extra_filename, &mut seen_paths)?
374        {
375            return Ok(library);
376        }
377        self.find_library_crate(crate_rejections, "", &mut seen_paths)
378    }
379
380    fn find_library_crate(
381        &self,
382        crate_rejections: &mut CrateRejections,
383        extra_prefix: &str,
384        seen_paths: &mut FxHashSet<PathBuf>,
385    ) -> Result<Option<Library>, CrateError> {
386        let rmeta_prefix = &format!("lib{}{}", self.crate_name, extra_prefix);
387        let rlib_prefix = rmeta_prefix;
388        let dylib_prefix =
389            &format!("{}{}{}", self.target.dll_prefix, self.crate_name, extra_prefix);
390        let staticlib_prefix =
391            &format!("{}{}{}", self.target.staticlib_prefix, self.crate_name, extra_prefix);
392        let interface_prefix = rmeta_prefix;
393
394        let rmeta_suffix = ".rmeta";
395        let rlib_suffix = ".rlib";
396        let dylib_suffix = &self.target.dll_suffix;
397        let staticlib_suffix = &self.target.staticlib_suffix;
398        let interface_suffix = ".rs";
399
400        let mut candidates: FxIndexMap<
401            _,
402            (FxIndexSet<_>, FxIndexSet<_>, FxIndexSet<_>, FxIndexSet<_>),
403        > = Default::default();
404
405        // First, find all possible candidate rlibs and dylibs purely based on
406        // the name of the files themselves. We're trying to match against an
407        // exact crate name and a possibly an exact hash.
408        //
409        // During this step, we can filter all found libraries based on the
410        // name and id found in the crate id (we ignore the path portion for
411        // filename matching), as well as the exact hash (if specified). If we
412        // end up having many candidates, we must look at the metadata to
413        // perform exact matches against hashes/crate ids. Note that opening up
414        // the metadata is where we do an exact match against the full contents
415        // of the crate id (path/name/id).
416        //
417        // The goal of this step is to look at as little metadata as possible.
418        // Unfortunately, the prefix-based matching sometimes is over-eager.
419        // E.g. if `rlib_suffix` is `libstd` it'll match the file
420        // `libstd_detect-8d6701fb958915ad.rlib` (incorrect) as well as
421        // `libstd-f3ab5b1dea981f17.rlib` (correct). But this is hard to avoid
422        // given that `extra_filename` comes from the `-C extra-filename`
423        // option and thus can be anything, and the incorrect match will be
424        // handled safely in `extract_one`.
425        for search_path in self.filesearch.search_paths(self.path_kind) {
426            debug!("searching {}", search_path.dir.display());
427            let spf = &search_path.files;
428
429            let mut should_check_staticlibs = true;
430            for (prefix, suffix, kind) in [
431                (rlib_prefix.as_str(), rlib_suffix, CrateFlavor::Rlib),
432                (rmeta_prefix.as_str(), rmeta_suffix, CrateFlavor::Rmeta),
433                (dylib_prefix, dylib_suffix, CrateFlavor::Dylib),
434                (interface_prefix, interface_suffix, CrateFlavor::SDylib),
435            ] {
436                if prefix == staticlib_prefix && suffix == staticlib_suffix {
437                    should_check_staticlibs = false;
438                }
439                if let Some(matches) = spf.query(prefix, suffix) {
440                    for (hash, spf) in matches {
441                        info!("lib candidate: {}", spf.path.display());
442
443                        let (rlibs, rmetas, dylibs, interfaces) =
444                            candidates.entry(hash).or_default();
445                        {
446                            // As a performance optimisation we canonicalize the path and skip
447                            // ones we've already seen. This allows us to ignore crates
448                            // we know are exactual equal to ones we've already found.
449                            // Going to the same crate through different symlinks does not change the result.
450                            let path = try_canonicalize(&spf.path)
451                                .unwrap_or_else(|_| spf.path.to_path_buf());
452                            if seen_paths.contains(&path) {
453                                continue;
454                            };
455                            seen_paths.insert(path);
456                        }
457                        // Use the original path (potentially with unresolved symlinks),
458                        // filesystem code should not care, but this is nicer for diagnostics.
459                        let path = spf.path.to_path_buf();
460                        match kind {
461                            CrateFlavor::Rlib => rlibs.insert(path),
462                            CrateFlavor::Rmeta => rmetas.insert(path),
463                            CrateFlavor::Dylib => dylibs.insert(path),
464                            CrateFlavor::SDylib => interfaces.insert(path),
465                        };
466                    }
467                }
468            }
469            if let Some(static_matches) = should_check_staticlibs
470                .then(|| spf.query(staticlib_prefix, staticlib_suffix))
471                .flatten()
472            {
473                for (_, spf) in static_matches {
474                    crate_rejections.via_kind.push(CrateMismatch {
475                        path: spf.path.to_path_buf(),
476                        got: "static".to_string(),
477                    });
478                }
479            }
480        }
481
482        // We have now collected all known libraries into a set of candidates
483        // keyed of the filename hash listed. For each filename, we also have a
484        // list of rlibs/dylibs that apply. Here, we map each of these lists
485        // (per hash), to a Library candidate for returning.
486        //
487        // A Library candidate is created if the metadata for the set of
488        // libraries corresponds to the crate id and hash criteria that this
489        // search is being performed for.
490        let mut libraries = FxIndexMap::default();
491        for (_hash, (rlibs, rmetas, dylibs, interfaces)) in candidates {
492            if let Some((svh, lib)) =
493                self.extract_lib(crate_rejections, rlibs, rmetas, dylibs, interfaces)?
494            {
495                libraries.insert(svh, lib);
496            }
497        }
498
499        // Having now translated all relevant found hashes into libraries, see
500        // what we've got and figure out if we found multiple candidates for
501        // libraries or not.
502        match libraries.len() {
503            0 => Ok(None),
504            1 => Ok(Some(libraries.into_iter().next().unwrap().1)),
505            _ => {
506                let mut candidates: Vec<PathBuf> = libraries
507                    .into_values()
508                    .map(|lib| lib.source.paths().next().unwrap().clone())
509                    .collect();
510                candidates.sort();
511
512                Err(CrateError::MultipleCandidates(
513                    self.crate_name,
514                    // these are the same for all candidates
515                    get_flavor_from_path(candidates.first().unwrap()),
516                    candidates,
517                ))
518            }
519        }
520    }
521
522    fn extract_lib(
523        &self,
524        crate_rejections: &mut CrateRejections,
525        rlibs: FxIndexSet<PathBuf>,
526        rmetas: FxIndexSet<PathBuf>,
527        dylibs: FxIndexSet<PathBuf>,
528        interfaces: FxIndexSet<PathBuf>,
529    ) -> Result<Option<(Svh, Library)>, CrateError> {
530        let mut slot = None;
531        // Order here matters, rmeta should come first.
532        //
533        // Make sure there's at most one rlib and at most one dylib.
534        //
535        // See comment in `extract_one` below.
536        let rmeta = self.extract_one(crate_rejections, rmetas, CrateFlavor::Rmeta, &mut slot)?;
537        let rlib = self.extract_one(crate_rejections, rlibs, CrateFlavor::Rlib, &mut slot)?;
538        let sdylib_interface =
539            self.extract_one(crate_rejections, interfaces, CrateFlavor::SDylib, &mut slot)?;
540        let dylib = self.extract_one(crate_rejections, dylibs, CrateFlavor::Dylib, &mut slot)?;
541
542        if sdylib_interface.is_some() && dylib.is_none() {
543            return Err(CrateError::FullMetadataNotFound(self.crate_name, CrateFlavor::SDylib));
544        }
545
546        let source = CrateSource { rmeta, rlib, dylib, sdylib_interface };
547        Ok(slot.map(|(svh, metadata, _, _)| (svh, Library { source, metadata })))
548    }
549
550    fn needs_crate_flavor(&self, flavor: CrateFlavor) -> bool {
551        if flavor == CrateFlavor::Dylib && self.is_proc_macro {
552            return true;
553        }
554
555        if self.only_needs_metadata {
556            flavor == CrateFlavor::Rmeta
557        } else {
558            // we need all flavors (perhaps not true, but what we do for now)
559            true
560        }
561    }
562
563    // Attempts to extract *one* library from the set `m`. If the set has no
564    // elements, `None` is returned. If the set has more than one element, then
565    // the errors and notes are emitted about the set of libraries.
566    //
567    // With only one library in the set, this function will extract it, and then
568    // read the metadata from it if `*slot` is `None`. If the metadata couldn't
569    // be read, it is assumed that the file isn't a valid rust library (no
570    // errors are emitted).
571    //
572    // The `PathBuf` in `slot` will only be used for diagnostic purposes.
573    fn extract_one(
574        &self,
575        crate_rejections: &mut CrateRejections,
576        m: FxIndexSet<PathBuf>,
577        flavor: CrateFlavor,
578        slot: &mut Option<(Svh, MetadataBlob, PathBuf, CrateFlavor)>,
579    ) -> Result<Option<PathBuf>, CrateError> {
580        // If we are producing an rlib, and we've already loaded metadata, then
581        // we should not attempt to discover further crate sources (unless we're
582        // locating a proc macro; exact logic is in needs_crate_flavor). This means
583        // that under -Zbinary-dep-depinfo we will not emit a dependency edge on
584        // the *unused* rlib, and by returning `None` here immediately we
585        // guarantee that we do indeed not use it.
586        //
587        // See also #68149 which provides more detail on why emitting the
588        // dependency on the rlib is a bad thing.
589        if slot.is_some() {
590            if m.is_empty() || !self.needs_crate_flavor(flavor) {
591                return Ok(None);
592            }
593        }
594
595        let mut ret: Option<PathBuf> = None;
596        let mut err_data: Option<Vec<PathBuf>> = None;
597        for lib in m {
598            info!("{} reading metadata from: {}", flavor, lib.display());
599            if flavor == CrateFlavor::Rmeta && lib.metadata().is_ok_and(|m| m.len() == 0) {
600                // Empty files will cause get_metadata_section to fail. Rmeta
601                // files can be empty, for example with binaries (which can
602                // often appear with `cargo check` when checking a library as
603                // a unittest). We don't want to emit a user-visible warning
604                // in this case as it is not a real problem.
605                debug!("skipping empty file");
606                continue;
607            }
608            let (hash, metadata) = match get_metadata_section(
609                self.target,
610                flavor,
611                &lib,
612                self.metadata_loader,
613                self.cfg_version,
614                Some(self.crate_name),
615            ) {
616                Ok(blob) => {
617                    if let Some(h) = self.crate_matches(crate_rejections, &blob, &lib) {
618                        (h, blob)
619                    } else {
620                        info!("metadata mismatch");
621                        continue;
622                    }
623                }
624                Err(MetadataError::VersionMismatch { expected_version, found_version }) => {
625                    // The file was present and created by the same compiler version, but we
626                    // couldn't load it for some reason. Give a hard error instead of silently
627                    // ignoring it, but only if we would have given an error anyway.
628                    info!(
629                        "Rejecting via version: expected {} got {}",
630                        expected_version, found_version
631                    );
632                    crate_rejections
633                        .via_version
634                        .push(CrateMismatch { path: lib, got: found_version });
635                    continue;
636                }
637                Err(MetadataError::LoadFailure(err)) => {
638                    info!("no metadata found: {}", err);
639                    // Metadata was loaded from interface file earlier.
640                    if let Some((.., CrateFlavor::SDylib)) = slot {
641                        ret = Some(lib);
642                        continue;
643                    }
644                    // The file was present and created by the same compiler version, but we
645                    // couldn't load it for some reason. Give a hard error instead of silently
646                    // ignoring it, but only if we would have given an error anyway.
647                    crate_rejections.via_invalid.push(CrateMismatch { path: lib, got: err });
648                    continue;
649                }
650                Err(err @ MetadataError::NotPresent(_)) => {
651                    info!("no metadata found: {}", err);
652                    continue;
653                }
654            };
655            // If we see multiple hashes, emit an error about duplicate candidates.
656            if slot.as_ref().is_some_and(|s| s.0 != hash) {
657                if let Some(candidates) = err_data {
658                    return Err(CrateError::MultipleCandidates(
659                        self.crate_name,
660                        flavor,
661                        candidates,
662                    ));
663                }
664                err_data = Some(vec![slot.take().unwrap().2]);
665            }
666            if let Some(candidates) = &mut err_data {
667                candidates.push(lib);
668                continue;
669            }
670
671            // We error eagerly here. If we're locating a rlib, then in theory the full metadata
672            // could still be in a (later resolved) dylib. In practice, if the rlib and dylib
673            // were produced in a way where one has full metadata and the other hasn't, it would
674            // mean that they were compiled using different compiler flags and probably also have
675            // a different SVH value.
676            if metadata.get_header().is_stub {
677                // `is_stub` should never be true for .rmeta files.
678                assert_ne!(flavor, CrateFlavor::Rmeta);
679
680                // Because rmeta files are resolved before rlib/dylib files, if this is a stub and
681                // we haven't found a slot already, it means that the full metadata is missing.
682                if slot.is_none() {
683                    return Err(CrateError::FullMetadataNotFound(self.crate_name, flavor));
684                }
685            } else {
686                *slot = Some((hash, metadata, lib.clone(), flavor));
687            }
688            ret = Some(lib);
689        }
690
691        if let Some(candidates) = err_data {
692            Err(CrateError::MultipleCandidates(self.crate_name, flavor, candidates))
693        } else {
694            Ok(ret)
695        }
696    }
697
698    fn crate_matches(
699        &self,
700        crate_rejections: &mut CrateRejections,
701        metadata: &MetadataBlob,
702        libpath: &Path,
703    ) -> Option<Svh> {
704        let header = metadata.get_header();
705        if header.is_proc_macro_crate != self.is_proc_macro {
706            info!(
707                "Rejecting via proc macro: expected {} got {}",
708                self.is_proc_macro, header.is_proc_macro_crate,
709            );
710            return None;
711        }
712
713        if self.exact_paths.is_empty() && self.crate_name != header.name {
714            info!("Rejecting via crate name");
715            return None;
716        }
717
718        if header.triple != self.tuple {
719            info!("Rejecting via crate triple: expected {} got {}", self.tuple, header.triple);
720            crate_rejections.via_triple.push(CrateMismatch {
721                path: libpath.to_path_buf(),
722                got: header.triple.to_string(),
723            });
724            return None;
725        }
726
727        let hash = header.hash;
728        if let Some(expected_hash) = self.hash {
729            if hash != expected_hash {
730                info!("Rejecting via hash: expected {} got {}", expected_hash, hash);
731                crate_rejections
732                    .via_hash
733                    .push(CrateMismatch { path: libpath.to_path_buf(), got: hash.to_string() });
734                return None;
735            }
736        }
737
738        Some(hash)
739    }
740
741    fn find_commandline_library(
742        &self,
743        crate_rejections: &mut CrateRejections,
744    ) -> Result<Option<Library>, CrateError> {
745        // First, filter out all libraries that look suspicious. We only accept
746        // files which actually exist that have the correct naming scheme for
747        // rlibs/dylibs.
748        let mut rlibs = FxIndexSet::default();
749        let mut rmetas = FxIndexSet::default();
750        let mut dylibs = FxIndexSet::default();
751        let mut sdylib_interfaces = FxIndexSet::default();
752        for loc in &self.exact_paths {
753            let loc_canon = loc.canonicalized();
754            let loc_orig = loc.original();
755            if !loc_canon.exists() {
756                return Err(CrateError::ExternLocationNotExist(self.crate_name, loc_orig.clone()));
757            }
758            if !loc_orig.is_file() {
759                return Err(CrateError::ExternLocationNotFile(self.crate_name, loc_orig.clone()));
760            }
761            // Note to take care and match against the non-canonicalized name:
762            // some systems save build artifacts into content-addressed stores
763            // that do not preserve extensions, and then link to them using
764            // e.g. symbolic links. If we canonicalize too early, we resolve
765            // the symlink, the file type is lost and we might treat rlibs and
766            // rmetas as dylibs.
767            let Some(file) = loc_orig.file_name().and_then(|s| s.to_str()) else {
768                return Err(CrateError::ExternLocationNotFile(self.crate_name, loc_orig.clone()));
769            };
770            if file.starts_with("lib") {
771                if file.ends_with(".rlib") {
772                    rlibs.insert(loc_canon.clone());
773                    continue;
774                }
775                if file.ends_with(".rmeta") {
776                    rmetas.insert(loc_canon.clone());
777                    continue;
778                }
779                if file.ends_with(".rs") {
780                    sdylib_interfaces.insert(loc_canon.clone());
781                }
782            }
783            let dll_prefix = self.target.dll_prefix.as_ref();
784            let dll_suffix = self.target.dll_suffix.as_ref();
785            if file.starts_with(dll_prefix) && file.ends_with(dll_suffix) {
786                dylibs.insert(loc_canon.clone());
787                continue;
788            }
789            crate_rejections
790                .via_filename
791                .push(CrateMismatch { path: loc_orig.clone(), got: String::new() });
792        }
793
794        // Extract the dylib/rlib/rmeta triple.
795        self.extract_lib(crate_rejections, rlibs, rmetas, dylibs, sdylib_interfaces)
796            .map(|opt| opt.map(|(_, lib)| lib))
797    }
798
799    pub(crate) fn into_error(
800        self,
801        crate_rejections: CrateRejections,
802        dep_root: Option<CratePaths>,
803    ) -> CrateError {
804        CrateError::LocatorCombined(Box::new(CombinedLocatorError {
805            crate_name: self.crate_name,
806            dep_root,
807            triple: self.tuple,
808            dll_prefix: self.target.dll_prefix.to_string(),
809            dll_suffix: self.target.dll_suffix.to_string(),
810            crate_rejections,
811        }))
812    }
813}
814
815fn get_metadata_section<'p>(
816    target: &Target,
817    flavor: CrateFlavor,
818    filename: &'p Path,
819    loader: &dyn MetadataLoader,
820    cfg_version: &'static str,
821    crate_name: Option<Symbol>,
822) -> Result<MetadataBlob, MetadataError<'p>> {
823    if !filename.exists() {
824        return Err(MetadataError::NotPresent(filename));
825    }
826    let raw_bytes = match flavor {
827        CrateFlavor::Rlib => {
828            loader.get_rlib_metadata(target, filename).map_err(MetadataError::LoadFailure)?
829        }
830        CrateFlavor::SDylib => {
831            let compiler = std::env::current_exe().map_err(|_err| {
832                MetadataError::LoadFailure(
833                    "couldn't obtain current compiler binary when loading sdylib interface"
834                        .to_string(),
835                )
836            })?;
837
838            let tmp_path = match TempFileBuilder::new().prefix("rustc").tempdir() {
839                Ok(tmp_path) => tmp_path,
840                Err(error) => {
841                    return Err(MetadataError::LoadFailure(format!(
842                        "couldn't create a temp dir: {}",
843                        error
844                    )));
845                }
846            };
847
848            let crate_name = crate_name.unwrap();
849            debug!("compiling {}", filename.display());
850            // FIXME: This will need to be done either within the current compiler session or
851            // as a separate compiler session in the same process.
852            let res = std::process::Command::new(compiler)
853                .arg(&filename)
854                .arg("--emit=metadata")
855                .arg(format!("--crate-name={}", crate_name))
856                .arg(format!("--out-dir={}", tmp_path.path().display()))
857                .arg("-Zbuild-sdylib-interface")
858                .output()
859                .map_err(|err| {
860                    MetadataError::LoadFailure(format!("couldn't compile interface: {}", err))
861                })?;
862
863            if !res.status.success() {
864                return Err(MetadataError::LoadFailure(format!(
865                    "couldn't compile interface: {}",
866                    std::str::from_utf8(&res.stderr).unwrap_or_default()
867                )));
868            }
869
870            // Load interface metadata instead of crate metadata.
871            let interface_metadata_name = format!("lib{}.rmeta", crate_name);
872            let rmeta_file = tmp_path.path().join(interface_metadata_name);
873            debug!("loading interface metadata from {}", rmeta_file.display());
874            let rmeta = get_rmeta_metadata_section(&rmeta_file)?;
875            let _ = std::fs::remove_file(rmeta_file);
876
877            rmeta
878        }
879        CrateFlavor::Dylib => {
880            let buf =
881                loader.get_dylib_metadata(target, filename).map_err(MetadataError::LoadFailure)?;
882            let header_len = METADATA_HEADER.len();
883            // header + u64 length of data
884            let data_start = header_len + 8;
885
886            debug!("checking {} bytes of metadata-version stamp", header_len);
887            let header = &buf[..cmp::min(header_len, buf.len())];
888            if header != METADATA_HEADER {
889                return Err(MetadataError::LoadFailure(format!(
890                    "invalid metadata version found: {}",
891                    filename.display()
892                )));
893            }
894
895            // Length of the metadata - this allows linkers to pad the section if they want
896            let Ok(len_bytes) =
897                <[u8; 8]>::try_from(&buf[header_len..cmp::min(data_start, buf.len())])
898            else {
899                return Err(MetadataError::LoadFailure(
900                    "invalid metadata length found".to_string(),
901                ));
902            };
903            let metadata_len = u64::from_le_bytes(len_bytes) as usize;
904
905            // Header is okay -> inflate the actual metadata
906            buf.slice(|buf| &buf[data_start..(data_start + metadata_len)])
907        }
908        CrateFlavor::Rmeta => get_rmeta_metadata_section(filename)?,
909    };
910    let Ok(blob) = MetadataBlob::new(raw_bytes) else {
911        return Err(MetadataError::LoadFailure(format!(
912            "corrupt metadata encountered in {}",
913            filename.display()
914        )));
915    };
916    match blob.check_compatibility(cfg_version) {
917        Ok(()) => {
918            debug!("metadata blob read okay");
919            Ok(blob)
920        }
921        Err(None) => Err(MetadataError::LoadFailure(format!(
922            "invalid metadata version found: {}",
923            filename.display()
924        ))),
925        Err(Some(found_version)) => {
926            return Err(MetadataError::VersionMismatch {
927                expected_version: rustc_version(cfg_version),
928                found_version,
929            });
930        }
931    }
932}
933
934fn get_rmeta_metadata_section<'a, 'p>(filename: &'p Path) -> Result<OwnedSlice, MetadataError<'a>> {
935    // mmap the file, because only a small fraction of it is read.
936    let file = std::fs::File::open(filename).map_err(|_| {
937        MetadataError::LoadFailure(format!(
938            "failed to open rmeta metadata: '{}'",
939            filename.display()
940        ))
941    })?;
942    let mmap = unsafe { Mmap::map(file) };
943    let mmap = mmap.map_err(|_| {
944        MetadataError::LoadFailure(format!(
945            "failed to mmap rmeta metadata: '{}'",
946            filename.display()
947        ))
948    })?;
949
950    Ok(slice_owned(mmap, Deref::deref))
951}
952
953/// A diagnostic function for dumping crate metadata to an output stream.
954pub fn list_file_metadata(
955    target: &Target,
956    path: &Path,
957    metadata_loader: &dyn MetadataLoader,
958    out: &mut dyn Write,
959    ls_kinds: &[String],
960    cfg_version: &'static str,
961) -> IoResult<()> {
962    let flavor = get_flavor_from_path(path);
963    match get_metadata_section(target, flavor, path, metadata_loader, cfg_version, None) {
964        Ok(metadata) => metadata.list_crate_metadata(out, ls_kinds),
965        Err(msg) => write!(out, "{msg}\n"),
966    }
967}
968
969fn get_flavor_from_path(path: &Path) -> CrateFlavor {
970    let filename = path.file_name().unwrap().to_str().unwrap();
971
972    if filename.ends_with(".rlib") {
973        CrateFlavor::Rlib
974    } else if filename.ends_with(".rmeta") {
975        CrateFlavor::Rmeta
976    } else {
977        CrateFlavor::Dylib
978    }
979}
980
981// ------------------------------------------ Error reporting -------------------------------------
982
983#[derive(Clone, Debug)]
984struct CrateMismatch {
985    path: PathBuf,
986    got: String,
987}
988
989#[derive(Clone, Debug, Default)]
990pub(crate) struct CrateRejections {
991    via_hash: Vec<CrateMismatch>,
992    via_triple: Vec<CrateMismatch>,
993    via_kind: Vec<CrateMismatch>,
994    via_version: Vec<CrateMismatch>,
995    via_filename: Vec<CrateMismatch>,
996    via_invalid: Vec<CrateMismatch>,
997}
998
999/// Candidate rejection reasons collected during crate search.
1000/// If no candidate is accepted, then these reasons are presented to the user,
1001/// otherwise they are ignored.
1002#[derive(Debug)]
1003pub(crate) struct CombinedLocatorError {
1004    crate_name: Symbol,
1005    dep_root: Option<CratePaths>,
1006    triple: TargetTuple,
1007    dll_prefix: String,
1008    dll_suffix: String,
1009    crate_rejections: CrateRejections,
1010}
1011
1012#[derive(Debug)]
1013pub(crate) enum CrateError {
1014    NonAsciiName(Symbol),
1015    ExternLocationNotExist(Symbol, PathBuf),
1016    ExternLocationNotFile(Symbol, PathBuf),
1017    MultipleCandidates(Symbol, CrateFlavor, Vec<PathBuf>),
1018    FullMetadataNotFound(Symbol, CrateFlavor),
1019    SymbolConflictsCurrent(Symbol),
1020    StableCrateIdCollision(Symbol, Symbol),
1021    DlOpen(String, String),
1022    DlSym(String, String),
1023    LocatorCombined(Box<CombinedLocatorError>),
1024    NotFound(Symbol),
1025}
1026
1027enum MetadataError<'a> {
1028    /// The file was missing.
1029    NotPresent(&'a Path),
1030    /// The file was present and invalid.
1031    LoadFailure(String),
1032    /// The file was present, but compiled with a different rustc version.
1033    VersionMismatch { expected_version: String, found_version: String },
1034}
1035
1036impl fmt::Display for MetadataError<'_> {
1037    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1038        match self {
1039            MetadataError::NotPresent(filename) => {
1040                f.write_str(&format!("no such file: '{}'", filename.display()))
1041            }
1042            MetadataError::LoadFailure(msg) => f.write_str(msg),
1043            MetadataError::VersionMismatch { expected_version, found_version } => {
1044                f.write_str(&format!(
1045                    "rustc version mismatch. expected {}, found {}",
1046                    expected_version, found_version,
1047                ))
1048            }
1049        }
1050    }
1051}
1052
1053impl CrateError {
1054    pub(crate) fn report(self, sess: &Session, span: Span, missing_core: bool) {
1055        let dcx = sess.dcx();
1056        match self {
1057            CrateError::NonAsciiName(crate_name) => {
1058                dcx.emit_err(errors::NonAsciiName { span, crate_name });
1059            }
1060            CrateError::ExternLocationNotExist(crate_name, loc) => {
1061                dcx.emit_err(errors::ExternLocationNotExist { span, crate_name, location: &loc });
1062            }
1063            CrateError::ExternLocationNotFile(crate_name, loc) => {
1064                dcx.emit_err(errors::ExternLocationNotFile { span, crate_name, location: &loc });
1065            }
1066            CrateError::MultipleCandidates(crate_name, flavor, candidates) => {
1067                dcx.emit_err(errors::MultipleCandidates { span, crate_name, flavor, candidates });
1068            }
1069            CrateError::FullMetadataNotFound(crate_name, flavor) => {
1070                dcx.emit_err(errors::FullMetadataNotFound { span, crate_name, flavor });
1071            }
1072            CrateError::SymbolConflictsCurrent(root_name) => {
1073                dcx.emit_err(errors::SymbolConflictsCurrent { span, crate_name: root_name });
1074            }
1075            CrateError::StableCrateIdCollision(crate_name0, crate_name1) => {
1076                dcx.emit_err(errors::StableCrateIdCollision { span, crate_name0, crate_name1 });
1077            }
1078            CrateError::DlOpen(path, err) | CrateError::DlSym(path, err) => {
1079                dcx.emit_err(errors::DlError { span, path, err });
1080            }
1081            CrateError::LocatorCombined(locator) => {
1082                let crate_name = locator.crate_name;
1083                let add_info = match &locator.dep_root {
1084                    None => String::new(),
1085                    Some(r) => format!(" which `{}` depends on", r.name),
1086                };
1087                if !locator.crate_rejections.via_filename.is_empty() {
1088                    let mismatches = locator.crate_rejections.via_filename.iter();
1089                    for CrateMismatch { path, .. } in mismatches {
1090                        dcx.emit_err(errors::CrateLocationUnknownType { span, path, crate_name });
1091                        dcx.emit_err(errors::LibFilenameForm {
1092                            span,
1093                            dll_prefix: &locator.dll_prefix,
1094                            dll_suffix: &locator.dll_suffix,
1095                        });
1096                    }
1097                }
1098                let mut found_crates = String::new();
1099                if !locator.crate_rejections.via_hash.is_empty() {
1100                    let mismatches = locator.crate_rejections.via_hash.iter();
1101                    for CrateMismatch { path, .. } in mismatches {
1102                        found_crates.push_str(&format!(
1103                            "\ncrate `{}`: {}",
1104                            crate_name,
1105                            path.display()
1106                        ));
1107                    }
1108                    if let Some(r) = locator.dep_root {
1109                        for path in r.source.paths() {
1110                            found_crates.push_str(&format!(
1111                                "\ncrate `{}`: {}",
1112                                r.name,
1113                                path.display()
1114                            ));
1115                        }
1116                    }
1117                    dcx.emit_err(errors::NewerCrateVersion {
1118                        span,
1119                        crate_name,
1120                        add_info,
1121                        found_crates,
1122                    });
1123                } else if !locator.crate_rejections.via_triple.is_empty() {
1124                    let mismatches = locator.crate_rejections.via_triple.iter();
1125                    for CrateMismatch { path, got } in mismatches {
1126                        found_crates.push_str(&format!(
1127                            "\ncrate `{}`, target triple {}: {}",
1128                            crate_name,
1129                            got,
1130                            path.display(),
1131                        ));
1132                    }
1133                    dcx.emit_err(errors::NoCrateWithTriple {
1134                        span,
1135                        crate_name,
1136                        locator_triple: locator.triple.tuple(),
1137                        add_info,
1138                        found_crates,
1139                    });
1140                } else if !locator.crate_rejections.via_kind.is_empty() {
1141                    let mismatches = locator.crate_rejections.via_kind.iter();
1142                    for CrateMismatch { path, .. } in mismatches {
1143                        found_crates.push_str(&format!(
1144                            "\ncrate `{}`: {}",
1145                            crate_name,
1146                            path.display()
1147                        ));
1148                    }
1149                    dcx.emit_err(errors::FoundStaticlib {
1150                        span,
1151                        crate_name,
1152                        add_info,
1153                        found_crates,
1154                    });
1155                } else if !locator.crate_rejections.via_version.is_empty() {
1156                    let mismatches = locator.crate_rejections.via_version.iter();
1157                    for CrateMismatch { path, got } in mismatches {
1158                        found_crates.push_str(&format!(
1159                            "\ncrate `{}` compiled by {}: {}",
1160                            crate_name,
1161                            got,
1162                            path.display(),
1163                        ));
1164                    }
1165                    dcx.emit_err(errors::IncompatibleRustc {
1166                        span,
1167                        crate_name,
1168                        add_info,
1169                        found_crates,
1170                        rustc_version: rustc_version(sess.cfg_version),
1171                    });
1172                } else if !locator.crate_rejections.via_invalid.is_empty() {
1173                    let mut crate_rejections = Vec::new();
1174                    for CrateMismatch { path: _, got } in locator.crate_rejections.via_invalid {
1175                        crate_rejections.push(got);
1176                    }
1177                    dcx.emit_err(errors::InvalidMetadataFiles {
1178                        span,
1179                        crate_name,
1180                        add_info,
1181                        crate_rejections,
1182                    });
1183                } else {
1184                    let error = errors::CannotFindCrate {
1185                        span,
1186                        crate_name,
1187                        add_info,
1188                        missing_core,
1189                        current_crate: sess
1190                            .opts
1191                            .crate_name
1192                            .clone()
1193                            .unwrap_or_else(|| "<unknown>".to_string()),
1194                        is_nightly_build: sess.is_nightly_build(),
1195                        profiler_runtime: Symbol::intern(&sess.opts.unstable_opts.profiler_runtime),
1196                        locator_triple: locator.triple,
1197                        is_ui_testing: sess.opts.unstable_opts.ui_testing,
1198                        is_tier_3: sess.target.metadata.tier == Some(3),
1199                    };
1200                    // The diagnostic for missing core is very good, but it is followed by a lot of
1201                    // other diagnostics that do not add information.
1202                    if missing_core {
1203                        dcx.emit_fatal(error);
1204                    } else {
1205                        dcx.emit_err(error);
1206                    }
1207                }
1208            }
1209            CrateError::NotFound(crate_name) => {
1210                let error = errors::CannotFindCrate {
1211                    span,
1212                    crate_name,
1213                    add_info: String::new(),
1214                    missing_core,
1215                    current_crate: sess
1216                        .opts
1217                        .crate_name
1218                        .clone()
1219                        .unwrap_or_else(|| "<unknown>".to_string()),
1220                    is_nightly_build: sess.is_nightly_build(),
1221                    profiler_runtime: Symbol::intern(&sess.opts.unstable_opts.profiler_runtime),
1222                    locator_triple: sess.opts.target_triple.clone(),
1223                    is_ui_testing: sess.opts.unstable_opts.ui_testing,
1224                    is_tier_3: sess.target.metadata.tier == Some(3),
1225                };
1226                // The diagnostic for missing core is very good, but it is followed by a lot of
1227                // other diagnostics that do not add information.
1228                if missing_core {
1229                    dcx.emit_fatal(error);
1230                } else {
1231                    dcx.emit_err(error);
1232                }
1233            }
1234        }
1235    }
1236}