rustc_metadata/
creader.rs

1//! Validates all used crates and extern libraries and loads their metadata
2
3use std::error::Error;
4use std::path::Path;
5use std::str::FromStr;
6use std::time::Duration;
7use std::{cmp, env, iter};
8
9use rustc_ast::expand::allocator::{AllocatorKind, alloc_error_handler_name, global_fn_name};
10use rustc_ast::{self as ast, *};
11use rustc_data_structures::fx::FxHashSet;
12use rustc_data_structures::owned_slice::OwnedSlice;
13use rustc_data_structures::svh::Svh;
14use rustc_data_structures::sync::{self, FreezeReadGuard, FreezeWriteGuard};
15use rustc_data_structures::unord::UnordMap;
16use rustc_expand::base::SyntaxExtension;
17use rustc_fs_util::try_canonicalize;
18use rustc_hir as hir;
19use rustc_hir::def_id::{CrateNum, LOCAL_CRATE, LocalDefId, StableCrateId};
20use rustc_hir::definitions::Definitions;
21use rustc_index::IndexVec;
22use rustc_middle::bug;
23use rustc_middle::ty::data_structures::IndexSet;
24use rustc_middle::ty::{TyCtxt, TyCtxtFeed};
25use rustc_proc_macro::bridge::client::ProcMacro;
26use rustc_session::Session;
27use rustc_session::config::{
28    CrateType, ExtendedTargetModifierInfo, ExternLocation, Externs, OptionsTargetModifiers,
29    TargetModifier,
30};
31use rustc_session::cstore::{CrateDepKind, CrateSource, ExternCrate, ExternCrateSource};
32use rustc_session::lint::{self, BuiltinLintDiag};
33use rustc_session::output::validate_crate_name;
34use rustc_session::search_paths::PathKind;
35use rustc_span::edition::Edition;
36use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
37use rustc_target::spec::{PanicStrategy, Target};
38use tracing::{debug, info, trace};
39
40use crate::errors;
41use crate::locator::{CrateError, CrateLocator, CratePaths, CrateRejections};
42use crate::rmeta::{
43    CrateDep, CrateMetadata, CrateNumMap, CrateRoot, MetadataBlob, TargetModifiers,
44};
45
46/// The backend's way to give the crate store access to the metadata in a library.
47/// Note that it returns the raw metadata bytes stored in the library file, whether
48/// it is compressed, uncompressed, some weird mix, etc.
49/// rmeta files are backend independent and not handled here.
50pub trait MetadataLoader {
51    fn get_rlib_metadata(&self, target: &Target, filename: &Path) -> Result<OwnedSlice, String>;
52    fn get_dylib_metadata(&self, target: &Target, filename: &Path) -> Result<OwnedSlice, String>;
53}
54
55pub type MetadataLoaderDyn = dyn MetadataLoader + Send + Sync + sync::DynSend + sync::DynSync;
56
57pub struct CStore {
58    metadata_loader: Box<MetadataLoaderDyn>,
59
60    metas: IndexVec<CrateNum, Option<Box<CrateMetadata>>>,
61    injected_panic_runtime: Option<CrateNum>,
62    /// This crate needs an allocator and either provides it itself, or finds it in a dependency.
63    /// If the above is true, then this field denotes the kind of the found allocator.
64    allocator_kind: Option<AllocatorKind>,
65    /// This crate needs an allocation error handler and either provides it itself, or finds it in a dependency.
66    /// If the above is true, then this field denotes the kind of the found allocator.
67    alloc_error_handler_kind: Option<AllocatorKind>,
68    /// This crate has a `#[global_allocator]` item.
69    has_global_allocator: bool,
70    /// This crate has a `#[alloc_error_handler]` item.
71    has_alloc_error_handler: bool,
72
73    /// Names that were used to load the crates via `extern crate` or paths.
74    resolved_externs: UnordMap<Symbol, CrateNum>,
75
76    /// Unused externs of the crate
77    unused_externs: Vec<Symbol>,
78
79    used_extern_options: FxHashSet<Symbol>,
80}
81
82impl std::fmt::Debug for CStore {
83    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84        f.debug_struct("CStore").finish_non_exhaustive()
85    }
86}
87
88pub enum LoadedMacro {
89    MacroDef {
90        def: MacroDef,
91        ident: Ident,
92        attrs: Vec<hir::Attribute>,
93        span: Span,
94        edition: Edition,
95    },
96    ProcMacro(SyntaxExtension),
97}
98
99pub(crate) struct Library {
100    pub source: CrateSource,
101    pub metadata: MetadataBlob,
102}
103
104enum LoadResult {
105    Previous(CrateNum),
106    Loaded(Library),
107}
108
109/// A reference to `CrateMetadata` that can also give access to whole crate store when necessary.
110#[derive(Clone, Copy)]
111pub(crate) struct CrateMetadataRef<'a> {
112    pub cdata: &'a CrateMetadata,
113    pub cstore: &'a CStore,
114}
115
116impl std::ops::Deref for CrateMetadataRef<'_> {
117    type Target = CrateMetadata;
118
119    fn deref(&self) -> &Self::Target {
120        self.cdata
121    }
122}
123
124struct CrateDump<'a>(&'a CStore);
125
126impl<'a> std::fmt::Debug for CrateDump<'a> {
127    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
128        writeln!(fmt, "resolved crates:")?;
129        for (cnum, data) in self.0.iter_crate_data() {
130            writeln!(fmt, "  name: {}", data.name())?;
131            writeln!(fmt, "  cnum: {cnum}")?;
132            writeln!(fmt, "  hash: {}", data.hash())?;
133            writeln!(fmt, "  reqd: {:?}", data.dep_kind())?;
134            writeln!(fmt, "  priv: {:?}", data.is_private_dep())?;
135            let CrateSource { dylib, rlib, rmeta, sdylib_interface } = data.source();
136            if let Some(dylib) = dylib {
137                writeln!(fmt, "  dylib: {}", dylib.0.display())?;
138            }
139            if let Some(rlib) = rlib {
140                writeln!(fmt, "   rlib: {}", rlib.0.display())?;
141            }
142            if let Some(rmeta) = rmeta {
143                writeln!(fmt, "   rmeta: {}", rmeta.0.display())?;
144            }
145            if let Some(sdylib_interface) = sdylib_interface {
146                writeln!(fmt, "   sdylib interface: {}", sdylib_interface.0.display())?;
147            }
148        }
149        Ok(())
150    }
151}
152
153/// Reason that a crate is being sourced as a dependency.
154#[derive(Clone, Copy)]
155enum CrateOrigin<'a> {
156    /// This crate was a dependency of another crate.
157    IndirectDependency {
158        /// Where this dependency was included from.
159        dep_root: &'a CratePaths,
160        /// True if the parent is private, meaning the dependent should also be private.
161        parent_private: bool,
162        /// Dependency info about this crate.
163        dep: &'a CrateDep,
164    },
165    /// Injected by `rustc`.
166    Injected,
167    /// Provided by `extern crate foo` or as part of the extern prelude.
168    Extern,
169}
170
171impl<'a> CrateOrigin<'a> {
172    /// Return the dependency root, if any.
173    fn dep_root(&self) -> Option<&'a CratePaths> {
174        match self {
175            CrateOrigin::IndirectDependency { dep_root, .. } => Some(dep_root),
176            _ => None,
177        }
178    }
179
180    /// Return dependency information, if any.
181    fn dep(&self) -> Option<&'a CrateDep> {
182        match self {
183            CrateOrigin::IndirectDependency { dep, .. } => Some(dep),
184            _ => None,
185        }
186    }
187
188    /// `Some(true)` if the dependency is private or its parent is private, `Some(false)` if the
189    /// dependency is not private, `None` if it could not be determined.
190    fn private_dep(&self) -> Option<bool> {
191        match self {
192            CrateOrigin::IndirectDependency { parent_private, dep, .. } => {
193                Some(dep.is_private || *parent_private)
194            }
195            _ => None,
196        }
197    }
198}
199
200impl CStore {
201    pub fn from_tcx(tcx: TyCtxt<'_>) -> FreezeReadGuard<'_, CStore> {
202        FreezeReadGuard::map(tcx.untracked().cstore.read(), |cstore| {
203            cstore.as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`")
204        })
205    }
206
207    pub fn from_tcx_mut(tcx: TyCtxt<'_>) -> FreezeWriteGuard<'_, CStore> {
208        FreezeWriteGuard::map(tcx.untracked().cstore.write(), |cstore| {
209            cstore.untracked_as_any().downcast_mut().expect("`tcx.cstore` is not a `CStore`")
210        })
211    }
212
213    fn intern_stable_crate_id<'tcx>(
214        &mut self,
215        tcx: TyCtxt<'tcx>,
216        root: &CrateRoot,
217    ) -> Result<TyCtxtFeed<'tcx, CrateNum>, CrateError> {
218        assert_eq!(self.metas.len(), tcx.untracked().stable_crate_ids.read().len());
219        let num = tcx.create_crate_num(root.stable_crate_id()).map_err(|existing| {
220            // Check for (potential) conflicts with the local crate
221            if existing == LOCAL_CRATE {
222                CrateError::SymbolConflictsCurrent(root.name())
223            } else if let Some(crate_name1) = self.metas[existing].as_ref().map(|data| data.name())
224            {
225                let crate_name0 = root.name();
226                CrateError::StableCrateIdCollision(crate_name0, crate_name1)
227            } else {
228                CrateError::NotFound(root.name())
229            }
230        })?;
231
232        self.metas.push(None);
233        Ok(num)
234    }
235
236    pub fn has_crate_data(&self, cnum: CrateNum) -> bool {
237        self.metas[cnum].is_some()
238    }
239
240    pub(crate) fn get_crate_data(&self, cnum: CrateNum) -> CrateMetadataRef<'_> {
241        let cdata = self.metas[cnum]
242            .as_ref()
243            .unwrap_or_else(|| panic!("Failed to get crate data for {cnum:?}"));
244        CrateMetadataRef { cdata, cstore: self }
245    }
246
247    pub(crate) fn get_crate_data_mut(&mut self, cnum: CrateNum) -> &mut CrateMetadata {
248        self.metas[cnum].as_mut().unwrap_or_else(|| panic!("Failed to get crate data for {cnum:?}"))
249    }
250
251    fn set_crate_data(&mut self, cnum: CrateNum, data: CrateMetadata) {
252        assert!(self.metas[cnum].is_none(), "Overwriting crate metadata entry");
253        self.metas[cnum] = Some(Box::new(data));
254    }
255
256    /// Save the name used to resolve the extern crate in the local crate
257    ///
258    /// The name isn't always the crate's own name, because `sess.opts.externs` can assign it another name.
259    /// It's also not always the same as the `DefId`'s symbol due to renames `extern crate resolved_name as defid_name`.
260    pub(crate) fn set_resolved_extern_crate_name(&mut self, name: Symbol, extern_crate: CrateNum) {
261        self.resolved_externs.insert(name, extern_crate);
262    }
263
264    /// Crate resolved and loaded via the given extern name
265    /// (corresponds to names in `sess.opts.externs`)
266    ///
267    /// May be `None` if the crate wasn't used
268    pub fn resolved_extern_crate(&self, externs_name: Symbol) -> Option<CrateNum> {
269        self.resolved_externs.get(&externs_name).copied()
270    }
271
272    pub(crate) fn iter_crate_data(&self) -> impl Iterator<Item = (CrateNum, &CrateMetadata)> {
273        self.metas
274            .iter_enumerated()
275            .filter_map(|(cnum, data)| data.as_deref().map(|data| (cnum, data)))
276    }
277
278    fn push_dependencies_in_postorder(&self, deps: &mut IndexSet<CrateNum>, cnum: CrateNum) {
279        if !deps.contains(&cnum) {
280            let data = self.get_crate_data(cnum);
281            for dep in data.dependencies() {
282                if dep != cnum {
283                    self.push_dependencies_in_postorder(deps, dep);
284                }
285            }
286
287            deps.insert(cnum);
288        }
289    }
290
291    pub(crate) fn crate_dependencies_in_postorder(&self, cnum: CrateNum) -> IndexSet<CrateNum> {
292        let mut deps = IndexSet::default();
293        if cnum == LOCAL_CRATE {
294            for (cnum, _) in self.iter_crate_data() {
295                self.push_dependencies_in_postorder(&mut deps, cnum);
296            }
297        } else {
298            self.push_dependencies_in_postorder(&mut deps, cnum);
299        }
300        deps
301    }
302
303    pub(crate) fn injected_panic_runtime(&self) -> Option<CrateNum> {
304        self.injected_panic_runtime
305    }
306
307    pub(crate) fn allocator_kind(&self) -> Option<AllocatorKind> {
308        self.allocator_kind
309    }
310
311    pub(crate) fn alloc_error_handler_kind(&self) -> Option<AllocatorKind> {
312        self.alloc_error_handler_kind
313    }
314
315    pub(crate) fn has_global_allocator(&self) -> bool {
316        self.has_global_allocator
317    }
318
319    pub(crate) fn has_alloc_error_handler(&self) -> bool {
320        self.has_alloc_error_handler
321    }
322
323    pub fn report_unused_deps(&self, tcx: TyCtxt<'_>) {
324        let json_unused_externs = tcx.sess.opts.json_unused_externs;
325
326        // We put the check for the option before the lint_level_at_node call
327        // because the call mutates internal state and introducing it
328        // leads to some ui tests failing.
329        if !json_unused_externs.is_enabled() {
330            return;
331        }
332        let level = tcx
333            .lint_level_at_node(lint::builtin::UNUSED_CRATE_DEPENDENCIES, rustc_hir::CRATE_HIR_ID)
334            .level;
335        if level != lint::Level::Allow {
336            let unused_externs =
337                self.unused_externs.iter().map(|ident| ident.to_ident_string()).collect::<Vec<_>>();
338            let unused_externs = unused_externs.iter().map(String::as_str).collect::<Vec<&str>>();
339            tcx.dcx().emit_unused_externs(level, json_unused_externs.is_loud(), &unused_externs);
340        }
341    }
342
343    fn report_target_modifiers_extended(
344        tcx: TyCtxt<'_>,
345        krate: &Crate,
346        mods: &TargetModifiers,
347        dep_mods: &TargetModifiers,
348        data: &CrateMetadata,
349    ) {
350        let span = krate.spans.inner_span.shrink_to_lo();
351        let allowed_flag_mismatches = &tcx.sess.opts.cg.unsafe_allow_abi_mismatch;
352        let local_crate = tcx.crate_name(LOCAL_CRATE);
353        let tmod_extender = |tmod: &TargetModifier| (tmod.extend(), tmod.clone());
354        let report_diff = |prefix: &String,
355                           opt_name: &String,
356                           flag_local_value: Option<&String>,
357                           flag_extern_value: Option<&String>| {
358            if allowed_flag_mismatches.contains(&opt_name) {
359                return;
360            }
361            let extern_crate = data.name();
362            let flag_name = opt_name.clone();
363            let flag_name_prefixed = format!("-{}{}", prefix, opt_name);
364
365            match (flag_local_value, flag_extern_value) {
366                (Some(local_value), Some(extern_value)) => {
367                    tcx.dcx().emit_err(errors::IncompatibleTargetModifiers {
368                        span,
369                        extern_crate,
370                        local_crate,
371                        flag_name,
372                        flag_name_prefixed,
373                        local_value: local_value.to_string(),
374                        extern_value: extern_value.to_string(),
375                    })
376                }
377                (None, Some(extern_value)) => {
378                    tcx.dcx().emit_err(errors::IncompatibleTargetModifiersLMissed {
379                        span,
380                        extern_crate,
381                        local_crate,
382                        flag_name,
383                        flag_name_prefixed,
384                        extern_value: extern_value.to_string(),
385                    })
386                }
387                (Some(local_value), None) => {
388                    tcx.dcx().emit_err(errors::IncompatibleTargetModifiersRMissed {
389                        span,
390                        extern_crate,
391                        local_crate,
392                        flag_name,
393                        flag_name_prefixed,
394                        local_value: local_value.to_string(),
395                    })
396                }
397                (None, None) => panic!("Incorrect target modifiers report_diff(None, None)"),
398            };
399        };
400        let mut it1 = mods.iter().map(tmod_extender);
401        let mut it2 = dep_mods.iter().map(tmod_extender);
402        let mut left_name_val: Option<(ExtendedTargetModifierInfo, TargetModifier)> = None;
403        let mut right_name_val: Option<(ExtendedTargetModifierInfo, TargetModifier)> = None;
404        loop {
405            left_name_val = left_name_val.or_else(|| it1.next());
406            right_name_val = right_name_val.or_else(|| it2.next());
407            match (&left_name_val, &right_name_val) {
408                (Some(l), Some(r)) => match l.1.opt.cmp(&r.1.opt) {
409                    cmp::Ordering::Equal => {
410                        if l.0.tech_value != r.0.tech_value {
411                            report_diff(
412                                &l.0.prefix,
413                                &l.0.name,
414                                Some(&l.1.value_name),
415                                Some(&r.1.value_name),
416                            );
417                        }
418                        left_name_val = None;
419                        right_name_val = None;
420                    }
421                    cmp::Ordering::Greater => {
422                        report_diff(&r.0.prefix, &r.0.name, None, Some(&r.1.value_name));
423                        right_name_val = None;
424                    }
425                    cmp::Ordering::Less => {
426                        report_diff(&l.0.prefix, &l.0.name, Some(&l.1.value_name), None);
427                        left_name_val = None;
428                    }
429                },
430                (Some(l), None) => {
431                    report_diff(&l.0.prefix, &l.0.name, Some(&l.1.value_name), None);
432                    left_name_val = None;
433                }
434                (None, Some(r)) => {
435                    report_diff(&r.0.prefix, &r.0.name, None, Some(&r.1.value_name));
436                    right_name_val = None;
437                }
438                (None, None) => break,
439            }
440        }
441    }
442
443    pub fn report_incompatible_target_modifiers(&self, tcx: TyCtxt<'_>, krate: &Crate) {
444        for flag_name in &tcx.sess.opts.cg.unsafe_allow_abi_mismatch {
445            if !OptionsTargetModifiers::is_target_modifier(flag_name) {
446                tcx.dcx().emit_err(errors::UnknownTargetModifierUnsafeAllowed {
447                    span: krate.spans.inner_span.shrink_to_lo(),
448                    flag_name: flag_name.clone(),
449                });
450            }
451        }
452        let mods = tcx.sess.opts.gather_target_modifiers();
453        for (_cnum, data) in self.iter_crate_data() {
454            if data.is_proc_macro_crate() {
455                continue;
456            }
457            let dep_mods = data.target_modifiers();
458            if mods != dep_mods {
459                Self::report_target_modifiers_extended(tcx, krate, &mods, &dep_mods, data);
460            }
461        }
462    }
463
464    // Report about async drop types in dependency if async drop feature is disabled
465    pub fn report_incompatible_async_drop_feature(&self, tcx: TyCtxt<'_>, krate: &Crate) {
466        if tcx.features().async_drop() {
467            return;
468        }
469        for (_cnum, data) in self.iter_crate_data() {
470            if data.is_proc_macro_crate() {
471                continue;
472            }
473            if data.has_async_drops() {
474                let extern_crate = data.name();
475                let local_crate = tcx.crate_name(LOCAL_CRATE);
476                tcx.dcx().emit_warn(errors::AsyncDropTypesInDependency {
477                    span: krate.spans.inner_span.shrink_to_lo(),
478                    extern_crate,
479                    local_crate,
480                });
481            }
482        }
483    }
484
485    pub fn new(metadata_loader: Box<MetadataLoaderDyn>) -> CStore {
486        CStore {
487            metadata_loader,
488            // We add an empty entry for LOCAL_CRATE (which maps to zero) in
489            // order to make array indices in `metas` match with the
490            // corresponding `CrateNum`. This first entry will always remain
491            // `None`.
492            metas: IndexVec::from_iter(iter::once(None)),
493            injected_panic_runtime: None,
494            allocator_kind: None,
495            alloc_error_handler_kind: None,
496            has_global_allocator: false,
497            has_alloc_error_handler: false,
498            resolved_externs: UnordMap::default(),
499            unused_externs: Vec::new(),
500            used_extern_options: Default::default(),
501        }
502    }
503
504    fn existing_match(
505        &self,
506        externs: &Externs,
507        name: Symbol,
508        hash: Option<Svh>,
509        kind: PathKind,
510    ) -> Option<CrateNum> {
511        for (cnum, data) in self.iter_crate_data() {
512            if data.name() != name {
513                trace!("{} did not match {}", data.name(), name);
514                continue;
515            }
516
517            match hash {
518                Some(hash) if hash == data.hash() => return Some(cnum),
519                Some(hash) => {
520                    debug!("actual hash {} did not match expected {}", hash, data.hash());
521                    continue;
522                }
523                None => {}
524            }
525
526            // When the hash is None we're dealing with a top-level dependency
527            // in which case we may have a specification on the command line for
528            // this library. Even though an upstream library may have loaded
529            // something of the same name, we have to make sure it was loaded
530            // from the exact same location as well.
531            //
532            // We're also sure to compare *paths*, not actual byte slices. The
533            // `source` stores paths which are normalized which may be different
534            // from the strings on the command line.
535            let source = data.source();
536            if let Some(entry) = externs.get(name.as_str()) {
537                // Only use `--extern crate_name=path` here, not `--extern crate_name`.
538                if let Some(mut files) = entry.files() {
539                    if files.any(|l| {
540                        let l = l.canonicalized();
541                        source.dylib.as_ref().map(|(p, _)| p) == Some(l)
542                            || source.rlib.as_ref().map(|(p, _)| p) == Some(l)
543                            || source.rmeta.as_ref().map(|(p, _)| p) == Some(l)
544                    }) {
545                        return Some(cnum);
546                    }
547                }
548                continue;
549            }
550
551            // Alright, so we've gotten this far which means that `data` has the
552            // right name, we don't have a hash, and we don't have a --extern
553            // pointing for ourselves. We're still not quite yet done because we
554            // have to make sure that this crate was found in the crate lookup
555            // path (this is a top-level dependency) as we don't want to
556            // implicitly load anything inside the dependency lookup path.
557            let prev_kind = source
558                .dylib
559                .as_ref()
560                .or(source.rlib.as_ref())
561                .or(source.rmeta.as_ref())
562                .expect("No sources for crate")
563                .1;
564            if kind.matches(prev_kind) {
565                return Some(cnum);
566            } else {
567                debug!(
568                    "failed to load existing crate {}; kind {:?} did not match prev_kind {:?}",
569                    name, kind, prev_kind
570                );
571            }
572        }
573
574        None
575    }
576
577    /// Determine whether a dependency should be considered private.
578    ///
579    /// Dependencies are private if they get extern option specified, e.g. `--extern priv:mycrate`.
580    /// This is stored in metadata, so `private_dep`  can be correctly set during load. A `Some`
581    /// value for `private_dep` indicates that the crate is known to be private or public (note
582    /// that any `None` or `Some(false)` use of the same crate will make it public).
583    ///
584    /// Sometimes the directly dependent crate is not specified by `--extern`, in this case,
585    /// `private-dep` is none during loading. This is equivalent to the scenario where the
586    /// command parameter is set to `public-dependency`
587    fn is_private_dep(
588        &self,
589        externs: &Externs,
590        name: Symbol,
591        private_dep: Option<bool>,
592        origin: CrateOrigin<'_>,
593    ) -> bool {
594        if matches!(origin, CrateOrigin::Injected) {
595            return true;
596        }
597
598        let extern_private = externs.get(name.as_str()).map(|e| e.is_private_dep);
599        match (extern_private, private_dep) {
600            // Explicit non-private via `--extern`, explicit non-private from metadata, or
601            // unspecified with default to public.
602            (Some(false), _) | (_, Some(false)) | (None, None) => false,
603            // Marked private via `--extern priv:mycrate` or in metadata.
604            (Some(true) | None, Some(true) | None) => true,
605        }
606    }
607
608    fn register_crate<'tcx>(
609        &mut self,
610        tcx: TyCtxt<'tcx>,
611        host_lib: Option<Library>,
612        origin: CrateOrigin<'_>,
613        lib: Library,
614        dep_kind: CrateDepKind,
615        name: Symbol,
616        private_dep: Option<bool>,
617    ) -> Result<CrateNum, CrateError> {
618        let _prof_timer =
619            tcx.sess.prof.generic_activity_with_arg("metadata_register_crate", name.as_str());
620
621        let Library { source, metadata } = lib;
622        let crate_root = metadata.get_root();
623        let host_hash = host_lib.as_ref().map(|lib| lib.metadata.get_root().hash());
624        let private_dep = self.is_private_dep(&tcx.sess.opts.externs, name, private_dep, origin);
625
626        // Claim this crate number and cache it
627        let feed = self.intern_stable_crate_id(tcx, &crate_root)?;
628        let cnum = feed.key();
629
630        info!(
631            "register crate `{}` (cnum = {}. private_dep = {})",
632            crate_root.name(),
633            cnum,
634            private_dep
635        );
636
637        // Maintain a reference to the top most crate.
638        // Stash paths for top-most crate locally if necessary.
639        let crate_paths;
640        let dep_root = if let Some(dep_root) = origin.dep_root() {
641            dep_root
642        } else {
643            crate_paths = CratePaths::new(crate_root.name(), source.clone());
644            &crate_paths
645        };
646
647        let cnum_map = self.resolve_crate_deps(
648            tcx,
649            dep_root,
650            &crate_root,
651            &metadata,
652            cnum,
653            dep_kind,
654            private_dep,
655        )?;
656
657        let raw_proc_macros = if crate_root.is_proc_macro_crate() {
658            let temp_root;
659            let (dlsym_source, dlsym_root) = match &host_lib {
660                Some(host_lib) => (&host_lib.source, {
661                    temp_root = host_lib.metadata.get_root();
662                    &temp_root
663                }),
664                None => (&source, &crate_root),
665            };
666            let dlsym_dylib = dlsym_source.dylib.as_ref().expect("no dylib for a proc-macro crate");
667            Some(self.dlsym_proc_macros(tcx.sess, &dlsym_dylib.0, dlsym_root.stable_crate_id())?)
668        } else {
669            None
670        };
671
672        let crate_metadata = CrateMetadata::new(
673            tcx.sess,
674            self,
675            metadata,
676            crate_root,
677            raw_proc_macros,
678            cnum,
679            cnum_map,
680            dep_kind,
681            source,
682            private_dep,
683            host_hash,
684        );
685
686        self.set_crate_data(cnum, crate_metadata);
687
688        Ok(cnum)
689    }
690
691    fn load_proc_macro<'a, 'b>(
692        &self,
693        sess: &'a Session,
694        locator: &mut CrateLocator<'b>,
695        crate_rejections: &mut CrateRejections,
696        path_kind: PathKind,
697        host_hash: Option<Svh>,
698    ) -> Result<Option<(LoadResult, Option<Library>)>, CrateError>
699    where
700        'a: 'b,
701    {
702        if sess.opts.unstable_opts.dual_proc_macros {
703            // Use a new crate locator and crate rejections so trying to load a proc macro doesn't
704            // affect the error message we emit
705            let mut proc_macro_locator = locator.clone();
706
707            // Try to load a proc macro
708            proc_macro_locator.for_target_proc_macro(sess, path_kind);
709
710            // Load the proc macro crate for the target
711            let target_result =
712                match self.load(&mut proc_macro_locator, &mut CrateRejections::default())? {
713                    Some(LoadResult::Previous(cnum)) => {
714                        return Ok(Some((LoadResult::Previous(cnum), None)));
715                    }
716                    Some(LoadResult::Loaded(library)) => Some(LoadResult::Loaded(library)),
717                    None => return Ok(None),
718                };
719
720            // Use the existing crate_rejections as we want the error message to be affected by
721            // loading the host proc macro.
722            *crate_rejections = CrateRejections::default();
723
724            // Load the proc macro crate for the host
725            locator.for_proc_macro(sess, path_kind);
726
727            locator.hash = host_hash;
728
729            let Some(host_result) = self.load(locator, crate_rejections)? else {
730                return Ok(None);
731            };
732
733            let host_result = match host_result {
734                LoadResult::Previous(..) => {
735                    panic!("host and target proc macros must be loaded in lock-step")
736                }
737                LoadResult::Loaded(library) => library,
738            };
739            Ok(Some((target_result.unwrap(), Some(host_result))))
740        } else {
741            // Use a new crate locator and crate rejections so trying to load a proc macro doesn't
742            // affect the error message we emit
743            let mut proc_macro_locator = locator.clone();
744
745            // Load the proc macro crate for the host
746            proc_macro_locator.for_proc_macro(sess, path_kind);
747
748            let Some(host_result) =
749                self.load(&mut proc_macro_locator, &mut CrateRejections::default())?
750            else {
751                return Ok(None);
752            };
753
754            Ok(Some((host_result, None)))
755        }
756    }
757
758    fn resolve_crate<'tcx>(
759        &mut self,
760        tcx: TyCtxt<'tcx>,
761        name: Symbol,
762        span: Span,
763        dep_kind: CrateDepKind,
764        origin: CrateOrigin<'_>,
765    ) -> Option<CrateNum> {
766        self.used_extern_options.insert(name);
767        match self.maybe_resolve_crate(tcx, name, dep_kind, origin) {
768            Ok(cnum) => {
769                self.set_used_recursively(cnum);
770                Some(cnum)
771            }
772            Err(err) => {
773                debug!("failed to resolve crate {} {:?}", name, dep_kind);
774                let missing_core = self
775                    .maybe_resolve_crate(
776                        tcx,
777                        sym::core,
778                        CrateDepKind::Explicit,
779                        CrateOrigin::Extern,
780                    )
781                    .is_err();
782                err.report(tcx.sess, span, missing_core);
783                None
784            }
785        }
786    }
787
788    fn maybe_resolve_crate<'b, 'tcx>(
789        &'b mut self,
790        tcx: TyCtxt<'tcx>,
791        name: Symbol,
792        mut dep_kind: CrateDepKind,
793        origin: CrateOrigin<'b>,
794    ) -> Result<CrateNum, CrateError> {
795        info!("resolving crate `{}`", name);
796        if !name.as_str().is_ascii() {
797            return Err(CrateError::NonAsciiName(name));
798        }
799
800        let dep_root = origin.dep_root();
801        let dep = origin.dep();
802        let hash = dep.map(|d| d.hash);
803        let host_hash = dep.map(|d| d.host_hash).flatten();
804        let extra_filename = dep.map(|d| &d.extra_filename[..]);
805        let path_kind = if dep.is_some() { PathKind::Dependency } else { PathKind::Crate };
806        let private_dep = origin.private_dep();
807
808        let result = if let Some(cnum) =
809            self.existing_match(&tcx.sess.opts.externs, name, hash, path_kind)
810        {
811            (LoadResult::Previous(cnum), None)
812        } else {
813            info!("falling back to a load");
814            let mut locator = CrateLocator::new(
815                tcx.sess,
816                &*self.metadata_loader,
817                name,
818                // The all loop is because `--crate-type=rlib --crate-type=rlib` is
819                // legal and produces both inside this type.
820                tcx.crate_types().iter().all(|c| *c == CrateType::Rlib),
821                hash,
822                extra_filename,
823                path_kind,
824            );
825            let mut crate_rejections = CrateRejections::default();
826
827            match self.load(&mut locator, &mut crate_rejections)? {
828                Some(res) => (res, None),
829                None => {
830                    info!("falling back to loading proc_macro");
831                    dep_kind = CrateDepKind::MacrosOnly;
832                    match self.load_proc_macro(
833                        tcx.sess,
834                        &mut locator,
835                        &mut crate_rejections,
836                        path_kind,
837                        host_hash,
838                    )? {
839                        Some(res) => res,
840                        None => return Err(locator.into_error(crate_rejections, dep_root.cloned())),
841                    }
842                }
843            }
844        };
845
846        match result {
847            (LoadResult::Previous(cnum), None) => {
848                info!("library for `{}` was loaded previously, cnum {cnum}", name);
849                // When `private_dep` is none, it indicates the directly dependent crate. If it is
850                // not specified by `--extern` on command line parameters, it may be
851                // `private-dependency` when `register_crate` is called for the first time. Then it must be updated to
852                // `public-dependency` here.
853                let private_dep =
854                    self.is_private_dep(&tcx.sess.opts.externs, name, private_dep, origin);
855                let data = self.get_crate_data_mut(cnum);
856                if data.is_proc_macro_crate() {
857                    dep_kind = CrateDepKind::MacrosOnly;
858                }
859                data.set_dep_kind(cmp::max(data.dep_kind(), dep_kind));
860                data.update_and_private_dep(private_dep);
861                Ok(cnum)
862            }
863            (LoadResult::Loaded(library), host_library) => {
864                info!("register newly loaded library for `{}`", name);
865                self.register_crate(tcx, host_library, origin, library, dep_kind, name, private_dep)
866            }
867            _ => panic!(),
868        }
869    }
870
871    fn load(
872        &self,
873        locator: &CrateLocator<'_>,
874        crate_rejections: &mut CrateRejections,
875    ) -> Result<Option<LoadResult>, CrateError> {
876        let Some(library) = locator.maybe_load_library_crate(crate_rejections)? else {
877            return Ok(None);
878        };
879
880        // In the case that we're loading a crate, but not matching
881        // against a hash, we could load a crate which has the same hash
882        // as an already loaded crate. If this is the case prevent
883        // duplicates by just using the first crate.
884        let root = library.metadata.get_root();
885        let mut result = LoadResult::Loaded(library);
886        for (cnum, data) in self.iter_crate_data() {
887            if data.name() == root.name() && root.hash() == data.hash() {
888                assert!(locator.hash.is_none());
889                info!("load success, going to previous cnum: {}", cnum);
890                result = LoadResult::Previous(cnum);
891                break;
892            }
893        }
894        Ok(Some(result))
895    }
896
897    /// Go through the crate metadata and load any crates that it references.
898    fn resolve_crate_deps(
899        &mut self,
900        tcx: TyCtxt<'_>,
901        dep_root: &CratePaths,
902        crate_root: &CrateRoot,
903        metadata: &MetadataBlob,
904        krate: CrateNum,
905        dep_kind: CrateDepKind,
906        parent_is_private: bool,
907    ) -> Result<CrateNumMap, CrateError> {
908        debug!(
909            "resolving deps of external crate `{}` with dep root `{}`",
910            crate_root.name(),
911            dep_root.name
912        );
913        if crate_root.is_proc_macro_crate() {
914            return Ok(CrateNumMap::new());
915        }
916
917        // The map from crate numbers in the crate we're resolving to local crate numbers.
918        // We map 0 and all other holes in the map to our parent crate. The "additional"
919        // self-dependencies should be harmless.
920        let deps = crate_root.decode_crate_deps(metadata);
921        let mut crate_num_map = CrateNumMap::with_capacity(1 + deps.len());
922        crate_num_map.push(krate);
923        for dep in deps {
924            info!(
925                "resolving dep `{}`->`{}` hash: `{}` extra filename: `{}` private {}",
926                crate_root.name(),
927                dep.name,
928                dep.hash,
929                dep.extra_filename,
930                dep.is_private,
931            );
932            let dep_kind = match dep_kind {
933                CrateDepKind::MacrosOnly => CrateDepKind::MacrosOnly,
934                _ => dep.kind,
935            };
936            let cnum = self.maybe_resolve_crate(
937                tcx,
938                dep.name,
939                dep_kind,
940                CrateOrigin::IndirectDependency {
941                    dep_root,
942                    parent_private: parent_is_private,
943                    dep: &dep,
944                },
945            )?;
946            crate_num_map.push(cnum);
947        }
948
949        debug!("resolve_crate_deps: cnum_map for {:?} is {:?}", krate, crate_num_map);
950        Ok(crate_num_map)
951    }
952
953    fn dlsym_proc_macros(
954        &self,
955        sess: &Session,
956        path: &Path,
957        stable_crate_id: StableCrateId,
958    ) -> Result<&'static [ProcMacro], CrateError> {
959        let sym_name = sess.generate_proc_macro_decls_symbol(stable_crate_id);
960        debug!("trying to dlsym proc_macros {} for symbol `{}`", path.display(), sym_name);
961
962        unsafe {
963            let result = load_symbol_from_dylib::<*const &[ProcMacro]>(path, &sym_name);
964            match result {
965                Ok(result) => {
966                    debug!("loaded dlsym proc_macros {} for symbol `{}`", path.display(), sym_name);
967                    Ok(*result)
968                }
969                Err(err) => {
970                    debug!(
971                        "failed to dlsym proc_macros {} for symbol `{}`",
972                        path.display(),
973                        sym_name
974                    );
975                    Err(err.into())
976                }
977            }
978        }
979    }
980
981    fn inject_panic_runtime(&mut self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
982        // If we're only compiling an rlib, then there's no need to select a
983        // panic runtime, so we just skip this section entirely.
984        let only_rlib = tcx.crate_types().iter().all(|ct| *ct == CrateType::Rlib);
985        if only_rlib {
986            info!("panic runtime injection skipped, only generating rlib");
987            return;
988        }
989
990        // If we need a panic runtime, we try to find an existing one here. At
991        // the same time we perform some general validation of the DAG we've got
992        // going such as ensuring everything has a compatible panic strategy.
993        let mut needs_panic_runtime = attr::contains_name(&krate.attrs, sym::needs_panic_runtime);
994        for (_cnum, data) in self.iter_crate_data() {
995            needs_panic_runtime |= data.needs_panic_runtime();
996        }
997
998        // If we just don't need a panic runtime at all, then we're done here
999        // and there's nothing else to do.
1000        if !needs_panic_runtime {
1001            return;
1002        }
1003
1004        // By this point we know that we need a panic runtime. Here we just load
1005        // an appropriate default runtime for our panic strategy.
1006        //
1007        // We may resolve to an already loaded crate (as the crate may not have
1008        // been explicitly linked prior to this), but this is fine.
1009        //
1010        // Also note that we have yet to perform validation of the crate graph
1011        // in terms of everyone has a compatible panic runtime format, that's
1012        // performed later as part of the `dependency_format` module.
1013        let desired_strategy = tcx.sess.panic_strategy();
1014        let name = match desired_strategy {
1015            PanicStrategy::Unwind => sym::panic_unwind,
1016            PanicStrategy::Abort => sym::panic_abort,
1017        };
1018        info!("panic runtime not found -- loading {}", name);
1019
1020        let Some(cnum) =
1021            self.resolve_crate(tcx, name, DUMMY_SP, CrateDepKind::Implicit, CrateOrigin::Injected)
1022        else {
1023            return;
1024        };
1025        let data = self.get_crate_data(cnum);
1026
1027        // Sanity check the loaded crate to ensure it is indeed a panic runtime
1028        // and the panic strategy is indeed what we thought it was.
1029        if !data.is_panic_runtime() {
1030            tcx.dcx().emit_err(errors::CrateNotPanicRuntime { crate_name: name });
1031        }
1032        if data.required_panic_strategy() != Some(desired_strategy) {
1033            tcx.dcx()
1034                .emit_err(errors::NoPanicStrategy { crate_name: name, strategy: desired_strategy });
1035        }
1036
1037        self.injected_panic_runtime = Some(cnum);
1038    }
1039
1040    fn inject_profiler_runtime(&mut self, tcx: TyCtxt<'_>) {
1041        let needs_profiler_runtime =
1042            tcx.sess.instrument_coverage() || tcx.sess.opts.cg.profile_generate.enabled();
1043        if !needs_profiler_runtime || tcx.sess.opts.unstable_opts.no_profiler_runtime {
1044            return;
1045        }
1046
1047        info!("loading profiler");
1048
1049        let name = Symbol::intern(&tcx.sess.opts.unstable_opts.profiler_runtime);
1050        let Some(cnum) =
1051            self.resolve_crate(tcx, name, DUMMY_SP, CrateDepKind::Implicit, CrateOrigin::Injected)
1052        else {
1053            return;
1054        };
1055        let data = self.get_crate_data(cnum);
1056
1057        // Sanity check the loaded crate to ensure it is indeed a profiler runtime
1058        if !data.is_profiler_runtime() {
1059            tcx.dcx().emit_err(errors::NotProfilerRuntime { crate_name: name });
1060        }
1061    }
1062
1063    fn inject_allocator_crate(&mut self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
1064        self.has_global_allocator =
1065            match &*fn_spans(krate, Symbol::intern(&global_fn_name(sym::alloc))) {
1066                [span1, span2, ..] => {
1067                    tcx.dcx()
1068                        .emit_err(errors::NoMultipleGlobalAlloc { span2: *span2, span1: *span1 });
1069                    true
1070                }
1071                spans => !spans.is_empty(),
1072            };
1073        self.has_alloc_error_handler = match &*fn_spans(
1074            krate,
1075            Symbol::intern(alloc_error_handler_name(AllocatorKind::Global)),
1076        ) {
1077            [span1, span2, ..] => {
1078                tcx.dcx()
1079                    .emit_err(errors::NoMultipleAllocErrorHandler { span2: *span2, span1: *span1 });
1080                true
1081            }
1082            spans => !spans.is_empty(),
1083        };
1084
1085        // Check to see if we actually need an allocator. This desire comes
1086        // about through the `#![needs_allocator]` attribute and is typically
1087        // written down in liballoc.
1088        if !attr::contains_name(&krate.attrs, sym::needs_allocator)
1089            && !self.iter_crate_data().any(|(_, data)| data.needs_allocator())
1090        {
1091            return;
1092        }
1093
1094        // At this point we've determined that we need an allocator. Let's see
1095        // if our compilation session actually needs an allocator based on what
1096        // we're emitting.
1097        let all_rlib = tcx.crate_types().iter().all(|ct| matches!(*ct, CrateType::Rlib));
1098        if all_rlib {
1099            return;
1100        }
1101
1102        // Ok, we need an allocator. Not only that but we're actually going to
1103        // create an artifact that needs one linked in. Let's go find the one
1104        // that we're going to link in.
1105        //
1106        // First up we check for global allocators. Look at the crate graph here
1107        // and see what's a global allocator, including if we ourselves are a
1108        // global allocator.
1109        #[allow(rustc::symbol_intern_string_literal)]
1110        let this_crate = Symbol::intern("this crate");
1111
1112        let mut global_allocator = self.has_global_allocator.then_some(this_crate);
1113        for (_, data) in self.iter_crate_data() {
1114            if data.has_global_allocator() {
1115                match global_allocator {
1116                    Some(other_crate) => {
1117                        tcx.dcx().emit_err(errors::ConflictingGlobalAlloc {
1118                            crate_name: data.name(),
1119                            other_crate_name: other_crate,
1120                        });
1121                    }
1122                    None => global_allocator = Some(data.name()),
1123                }
1124            }
1125        }
1126        let mut alloc_error_handler = self.has_alloc_error_handler.then_some(this_crate);
1127        for (_, data) in self.iter_crate_data() {
1128            if data.has_alloc_error_handler() {
1129                match alloc_error_handler {
1130                    Some(other_crate) => {
1131                        tcx.dcx().emit_err(errors::ConflictingAllocErrorHandler {
1132                            crate_name: data.name(),
1133                            other_crate_name: other_crate,
1134                        });
1135                    }
1136                    None => alloc_error_handler = Some(data.name()),
1137                }
1138            }
1139        }
1140
1141        if global_allocator.is_some() {
1142            self.allocator_kind = Some(AllocatorKind::Global);
1143        } else {
1144            // Ok we haven't found a global allocator but we still need an
1145            // allocator. At this point our allocator request is typically fulfilled
1146            // by the standard library, denoted by the `#![default_lib_allocator]`
1147            // attribute.
1148            if !attr::contains_name(&krate.attrs, sym::default_lib_allocator)
1149                && !self.iter_crate_data().any(|(_, data)| data.has_default_lib_allocator())
1150            {
1151                tcx.dcx().emit_err(errors::GlobalAllocRequired);
1152            }
1153            self.allocator_kind = Some(AllocatorKind::Default);
1154        }
1155
1156        if alloc_error_handler.is_some() {
1157            self.alloc_error_handler_kind = Some(AllocatorKind::Global);
1158        } else {
1159            // The alloc crate provides a default allocation error handler if
1160            // one isn't specified.
1161            self.alloc_error_handler_kind = Some(AllocatorKind::Default);
1162        }
1163    }
1164
1165    fn inject_forced_externs(&mut self, tcx: TyCtxt<'_>) {
1166        for (name, entry) in tcx.sess.opts.externs.iter() {
1167            if entry.force {
1168                let name_interned = Symbol::intern(name);
1169                if !self.used_extern_options.contains(&name_interned) {
1170                    self.resolve_crate(
1171                        tcx,
1172                        name_interned,
1173                        DUMMY_SP,
1174                        CrateDepKind::Explicit,
1175                        CrateOrigin::Extern,
1176                    );
1177                }
1178            }
1179        }
1180    }
1181
1182    /// Inject the `compiler_builtins` crate if it is not already in the graph.
1183    fn inject_compiler_builtins(&mut self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
1184        // `compiler_builtins` does not get extern builtins, nor do `#![no_core]` crates
1185        if attr::contains_name(&krate.attrs, sym::compiler_builtins)
1186            || attr::contains_name(&krate.attrs, sym::no_core)
1187        {
1188            info!("`compiler_builtins` unneeded");
1189            return;
1190        }
1191
1192        // If a `#![compiler_builtins]` crate already exists, avoid injecting it twice. This is
1193        // the common case since usually it appears as a dependency of `std` or `alloc`.
1194        for (cnum, cmeta) in self.iter_crate_data() {
1195            if cmeta.is_compiler_builtins() {
1196                info!("`compiler_builtins` already exists (cnum = {cnum}); skipping injection");
1197                return;
1198            }
1199        }
1200
1201        // `compiler_builtins` is not yet in the graph; inject it. Error on resolution failure.
1202        let Some(cnum) = self.resolve_crate(
1203            tcx,
1204            sym::compiler_builtins,
1205            krate.spans.inner_span.shrink_to_lo(),
1206            CrateDepKind::Explicit,
1207            CrateOrigin::Injected,
1208        ) else {
1209            info!("`compiler_builtins` not resolved");
1210            return;
1211        };
1212
1213        // Sanity check that the loaded crate is `#![compiler_builtins]`
1214        let cmeta = self.get_crate_data(cnum);
1215        if !cmeta.is_compiler_builtins() {
1216            tcx.dcx().emit_err(errors::CrateNotCompilerBuiltins { crate_name: cmeta.name() });
1217        }
1218    }
1219
1220    fn report_unused_deps_in_crate(&mut self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
1221        // Make a point span rather than covering the whole file
1222        let span = krate.spans.inner_span.shrink_to_lo();
1223        // Complain about anything left over
1224        for (name, entry) in tcx.sess.opts.externs.iter() {
1225            if let ExternLocation::FoundInLibrarySearchDirectories = entry.location {
1226                // Don't worry about pathless `--extern foo` sysroot references
1227                continue;
1228            }
1229            if entry.nounused_dep || entry.force {
1230                // We're not worried about this one
1231                continue;
1232            }
1233            let name_interned = Symbol::intern(name);
1234            if self.used_extern_options.contains(&name_interned) {
1235                continue;
1236            }
1237
1238            // Got a real unused --extern
1239            if tcx.sess.opts.json_unused_externs.is_enabled() {
1240                self.unused_externs.push(name_interned);
1241                continue;
1242            }
1243
1244            tcx.sess.psess.buffer_lint(
1245                lint::builtin::UNUSED_CRATE_DEPENDENCIES,
1246                span,
1247                ast::CRATE_NODE_ID,
1248                BuiltinLintDiag::UnusedCrateDependency {
1249                    extern_crate: name_interned,
1250                    local_crate: tcx.crate_name(LOCAL_CRATE),
1251                },
1252            );
1253        }
1254    }
1255
1256    fn report_future_incompatible_deps(&self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
1257        let name = tcx.crate_name(LOCAL_CRATE);
1258
1259        if name.as_str() == "wasm_bindgen" {
1260            let major = env::var("CARGO_PKG_VERSION_MAJOR")
1261                .ok()
1262                .and_then(|major| u64::from_str(&major).ok());
1263            let minor = env::var("CARGO_PKG_VERSION_MINOR")
1264                .ok()
1265                .and_then(|minor| u64::from_str(&minor).ok());
1266            let patch = env::var("CARGO_PKG_VERSION_PATCH")
1267                .ok()
1268                .and_then(|patch| u64::from_str(&patch).ok());
1269
1270            match (major, minor, patch) {
1271                // v1 or bigger is valid.
1272                (Some(1..), _, _) => return,
1273                // v0.3 or bigger is valid.
1274                (Some(0), Some(3..), _) => return,
1275                // v0.2.88 or bigger is valid.
1276                (Some(0), Some(2), Some(88..)) => return,
1277                // Not using Cargo.
1278                (None, None, None) => return,
1279                _ => (),
1280            }
1281
1282            // Make a point span rather than covering the whole file
1283            let span = krate.spans.inner_span.shrink_to_lo();
1284
1285            tcx.sess.dcx().emit_err(errors::WasmCAbi { span });
1286        }
1287    }
1288
1289    pub fn postprocess(&mut self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
1290        self.inject_compiler_builtins(tcx, krate);
1291        self.inject_forced_externs(tcx);
1292        self.inject_profiler_runtime(tcx);
1293        self.inject_allocator_crate(tcx, krate);
1294        self.inject_panic_runtime(tcx, krate);
1295
1296        self.report_unused_deps_in_crate(tcx, krate);
1297        self.report_future_incompatible_deps(tcx, krate);
1298
1299        info!("{:?}", CrateDump(self));
1300    }
1301
1302    /// Process an `extern crate foo` AST node.
1303    pub fn process_extern_crate(
1304        &mut self,
1305        tcx: TyCtxt<'_>,
1306        item: &ast::Item,
1307        def_id: LocalDefId,
1308        definitions: &Definitions,
1309    ) -> Option<CrateNum> {
1310        match item.kind {
1311            ast::ItemKind::ExternCrate(orig_name, ident) => {
1312                debug!("resolving extern crate stmt. ident: {} orig_name: {:?}", ident, orig_name);
1313                let name = match orig_name {
1314                    Some(orig_name) => {
1315                        validate_crate_name(tcx.sess, orig_name, Some(item.span));
1316                        orig_name
1317                    }
1318                    None => ident.name,
1319                };
1320                let dep_kind = if attr::contains_name(&item.attrs, sym::no_link) {
1321                    CrateDepKind::MacrosOnly
1322                } else {
1323                    CrateDepKind::Explicit
1324                };
1325
1326                let cnum =
1327                    self.resolve_crate(tcx, name, item.span, dep_kind, CrateOrigin::Extern)?;
1328
1329                let path_len = definitions.def_path(def_id).data.len();
1330                self.update_extern_crate(
1331                    cnum,
1332                    name,
1333                    ExternCrate {
1334                        src: ExternCrateSource::Extern(def_id.to_def_id()),
1335                        span: item.span,
1336                        path_len,
1337                        dependency_of: LOCAL_CRATE,
1338                    },
1339                );
1340                Some(cnum)
1341            }
1342            _ => bug!(),
1343        }
1344    }
1345
1346    pub fn process_path_extern(
1347        &mut self,
1348        tcx: TyCtxt<'_>,
1349        name: Symbol,
1350        span: Span,
1351    ) -> Option<CrateNum> {
1352        let cnum =
1353            self.resolve_crate(tcx, name, span, CrateDepKind::Explicit, CrateOrigin::Extern)?;
1354
1355        self.update_extern_crate(
1356            cnum,
1357            name,
1358            ExternCrate {
1359                src: ExternCrateSource::Path,
1360                span,
1361                // to have the least priority in `update_extern_crate`
1362                path_len: usize::MAX,
1363                dependency_of: LOCAL_CRATE,
1364            },
1365        );
1366
1367        Some(cnum)
1368    }
1369
1370    pub fn maybe_process_path_extern(&mut self, tcx: TyCtxt<'_>, name: Symbol) -> Option<CrateNum> {
1371        self.maybe_resolve_crate(tcx, name, CrateDepKind::Explicit, CrateOrigin::Extern).ok()
1372    }
1373}
1374
1375fn fn_spans(krate: &ast::Crate, name: Symbol) -> Vec<Span> {
1376    struct Finder {
1377        name: Symbol,
1378        spans: Vec<Span>,
1379    }
1380    impl<'ast> visit::Visitor<'ast> for Finder {
1381        fn visit_item(&mut self, item: &'ast ast::Item) {
1382            if let Some(ident) = item.kind.ident()
1383                && ident.name == self.name
1384                && attr::contains_name(&item.attrs, sym::rustc_std_internal_symbol)
1385            {
1386                self.spans.push(item.span);
1387            }
1388            visit::walk_item(self, item)
1389        }
1390    }
1391
1392    let mut f = Finder { name, spans: Vec::new() };
1393    visit::walk_crate(&mut f, krate);
1394    f.spans
1395}
1396
1397fn format_dlopen_err(e: &(dyn std::error::Error + 'static)) -> String {
1398    e.sources().map(|e| format!(": {e}")).collect()
1399}
1400
1401fn attempt_load_dylib(path: &Path) -> Result<libloading::Library, libloading::Error> {
1402    #[cfg(target_os = "aix")]
1403    if let Some(ext) = path.extension()
1404        && ext.eq("a")
1405    {
1406        // On AIX, we ship all libraries as .a big_af archive
1407        // the expected format is lib<name>.a(libname.so) for the actual
1408        // dynamic library
1409        let library_name = path.file_stem().expect("expect a library name");
1410        let mut archive_member = std::ffi::OsString::from("a(");
1411        archive_member.push(library_name);
1412        archive_member.push(".so)");
1413        let new_path = path.with_extension(archive_member);
1414
1415        // On AIX, we need RTLD_MEMBER to dlopen an archived shared
1416        let flags = libc::RTLD_LAZY | libc::RTLD_LOCAL | libc::RTLD_MEMBER;
1417        return unsafe { libloading::os::unix::Library::open(Some(&new_path), flags) }
1418            .map(|lib| lib.into());
1419    }
1420
1421    unsafe { libloading::Library::new(&path) }
1422}
1423
1424// On Windows the compiler would sometimes intermittently fail to open the
1425// proc-macro DLL with `Error::LoadLibraryExW`. It is suspected that something in the
1426// system still holds a lock on the file, so we retry a few times before calling it
1427// an error.
1428fn load_dylib(path: &Path, max_attempts: usize) -> Result<libloading::Library, String> {
1429    assert!(max_attempts > 0);
1430
1431    let mut last_error = None;
1432
1433    for attempt in 0..max_attempts {
1434        debug!("Attempt to load proc-macro `{}`.", path.display());
1435        match attempt_load_dylib(path) {
1436            Ok(lib) => {
1437                if attempt > 0 {
1438                    debug!(
1439                        "Loaded proc-macro `{}` after {} attempts.",
1440                        path.display(),
1441                        attempt + 1
1442                    );
1443                }
1444                return Ok(lib);
1445            }
1446            Err(err) => {
1447                // Only try to recover from this specific error.
1448                if !matches!(err, libloading::Error::LoadLibraryExW { .. }) {
1449                    debug!("Failed to load proc-macro `{}`. Not retrying", path.display());
1450                    let err = format_dlopen_err(&err);
1451                    // We include the path of the dylib in the error ourselves, so
1452                    // if it's in the error, we strip it.
1453                    if let Some(err) = err.strip_prefix(&format!(": {}", path.display())) {
1454                        return Err(err.to_string());
1455                    }
1456                    return Err(err);
1457                }
1458
1459                last_error = Some(err);
1460                std::thread::sleep(Duration::from_millis(100));
1461                debug!("Failed to load proc-macro `{}`. Retrying.", path.display());
1462            }
1463        }
1464    }
1465
1466    debug!("Failed to load proc-macro `{}` even after {} attempts.", path.display(), max_attempts);
1467
1468    let last_error = last_error.unwrap();
1469    let message = if let Some(src) = last_error.source() {
1470        format!("{} ({src}) (retried {max_attempts} times)", format_dlopen_err(&last_error))
1471    } else {
1472        format!("{} (retried {max_attempts} times)", format_dlopen_err(&last_error))
1473    };
1474    Err(message)
1475}
1476
1477pub enum DylibError {
1478    DlOpen(String, String),
1479    DlSym(String, String),
1480}
1481
1482impl From<DylibError> for CrateError {
1483    fn from(err: DylibError) -> CrateError {
1484        match err {
1485            DylibError::DlOpen(path, err) => CrateError::DlOpen(path, err),
1486            DylibError::DlSym(path, err) => CrateError::DlSym(path, err),
1487        }
1488    }
1489}
1490
1491pub unsafe fn load_symbol_from_dylib<T: Copy>(
1492    path: &Path,
1493    sym_name: &str,
1494) -> Result<T, DylibError> {
1495    // Make sure the path contains a / or the linker will search for it.
1496    let path = try_canonicalize(path).unwrap();
1497    let lib =
1498        load_dylib(&path, 5).map_err(|err| DylibError::DlOpen(path.display().to_string(), err))?;
1499
1500    let sym = unsafe { lib.get::<T>(sym_name.as_bytes()) }
1501        .map_err(|err| DylibError::DlSym(path.display().to_string(), format_dlopen_err(&err)))?;
1502
1503    // Intentionally leak the dynamic library. We can't ever unload it
1504    // since the library can make things that will live arbitrarily long.
1505    let sym = unsafe { sym.into_raw() };
1506    std::mem::forget(lib);
1507
1508    Ok(*sym)
1509}