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