Skip to main content

rustc_metadata/
native_libs.rs

1use std::ops::ControlFlow;
2use std::path::{Path, PathBuf};
3
4use rustc_abi::ExternAbi;
5use rustc_attr_parsing::eval_config_entry;
6use rustc_data_structures::fx::FxHashSet;
7use rustc_hir::attrs::{NativeLibKind, PeImportNameType};
8use rustc_hir::def::DefKind;
9use rustc_hir::find_attr;
10use rustc_middle::bug;
11use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
12use rustc_middle::query::LocalCrate;
13use rustc_middle::ty::{self, List, Ty, TyCtxt};
14use rustc_session::Session;
15use rustc_session::config::CrateType;
16use rustc_session::cstore::{
17    DllCallingConvention, DllImport, DllImportSymbolType, ForeignModule, NativeLib,
18};
19use rustc_session::search_paths::PathKind;
20use rustc_span::Symbol;
21use rustc_span::def_id::{DefId, LOCAL_CRATE};
22use rustc_target::spec::{Arch, BinaryFormat, CfgAbi, Env, LinkSelfContainedComponents, Os};
23
24use crate::errors;
25
26/// The fallback directories are passed to linker, but not used when rustc does the search,
27/// because in the latter case the set of fallback directories cannot always be determined
28/// consistently at the moment.
29pub struct NativeLibSearchFallback<'a> {
30    pub self_contained_components: LinkSelfContainedComponents,
31    pub apple_sdk_root: Option<&'a Path>,
32}
33
34pub fn walk_native_lib_search_dirs<R>(
35    sess: &Session,
36    fallback: Option<NativeLibSearchFallback<'_>>,
37    mut f: impl FnMut(&Path, bool /*is_framework*/) -> ControlFlow<R>,
38) -> ControlFlow<R> {
39    // Library search paths explicitly supplied by user (`-L` on the command line).
40    for search_path in sess.target_filesearch().cli_search_paths(PathKind::Native) {
41        f(&search_path.dir, false)?;
42    }
43    for search_path in sess.target_filesearch().cli_search_paths(PathKind::Framework) {
44        // Frameworks are looked up strictly in framework-specific paths.
45        if search_path.kind != PathKind::All {
46            f(&search_path.dir, true)?;
47        }
48    }
49
50    let Some(NativeLibSearchFallback { self_contained_components, apple_sdk_root }) = fallback
51    else {
52        return ControlFlow::Continue(());
53    };
54
55    // The toolchain ships some native library components and self-contained linking was enabled.
56    // Add the self-contained library directory to search paths.
57    if self_contained_components.intersects(
58        LinkSelfContainedComponents::LIBC
59            | LinkSelfContainedComponents::UNWIND
60            | LinkSelfContainedComponents::MINGW,
61    ) {
62        f(&sess.target_tlib_path.dir.join("self-contained"), false)?;
63    }
64
65    // Toolchains for some targets may ship `libunwind.a`, but place it into the main sysroot
66    // library directory instead of the self-contained directories.
67    // Sanitizer libraries have the same issue and are also linked by name on Apple targets.
68    // The targets here should be in sync with `copy_third_party_objects` in bootstrap.
69    // Finally there is shared LLVM library, which unlike compiler libraries, is linked by the name,
70    // therefore requiring the search path for the linker.
71    // FIXME: implement `-Clink-self-contained=+/-unwind,+/-sanitizers`, move the shipped libunwind
72    // and sanitizers to self-contained directory, and stop adding this search path.
73    // FIXME: On AIX this also has the side-effect of making the list of library search paths
74    // non-empty, which is needed or the linker may decide to record the LIBPATH env, if
75    // defined, as the search path instead of appending the default search paths.
76    if sess.target.cfg_abi == CfgAbi::Fortanix
77        || sess.target.os == Os::Linux
78        || sess.target.os == Os::Fuchsia
79        || sess.target.is_like_aix
80        || sess.target.is_like_darwin && !sess.sanitizers().is_empty()
81        || sess.target.os == Os::Windows
82            && sess.target.env == Env::Gnu
83            && sess.target.cfg_abi == CfgAbi::Llvm
84    {
85        f(&sess.target_tlib_path.dir, false)?;
86    }
87
88    // Mac Catalyst uses the macOS SDK, but to link to iOS-specific frameworks
89    // we must have the support library stubs in the library search path (#121430).
90    if let Some(sdk_root) = apple_sdk_root
91        && sess.target.env == Env::MacAbi
92    {
93        f(&sdk_root.join("System/iOSSupport/usr/lib"), false)?;
94        f(&sdk_root.join("System/iOSSupport/System/Library/Frameworks"), true)?;
95    }
96
97    ControlFlow::Continue(())
98}
99
100pub fn try_find_native_static_library(
101    sess: &Session,
102    name: &str,
103    verbatim: bool,
104) -> Option<PathBuf> {
105    let default = sess.staticlib_components(verbatim);
106    let formats = if verbatim {
107        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [default]))vec![default]
108    } else {
109        // On Windows, static libraries sometimes show up as libfoo.a and other
110        // times show up as foo.lib
111        let unix = ("lib", ".a");
112        if default == unix { ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [default]))vec![default] } else { ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [default, unix]))vec![default, unix] }
113    };
114
115    walk_native_lib_search_dirs(sess, None, |dir, is_framework| {
116        if !is_framework {
117            for (prefix, suffix) in &formats {
118                let test = dir.join(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1}{2}", prefix, name, suffix))
    })format!("{prefix}{name}{suffix}"));
119                if test.exists() {
120                    return ControlFlow::Break(test);
121                }
122            }
123        }
124        ControlFlow::Continue(())
125    })
126    .break_value()
127}
128
129pub fn try_find_native_dynamic_library(
130    sess: &Session,
131    name: &str,
132    verbatim: bool,
133) -> Option<PathBuf> {
134    let default = sess.staticlib_components(verbatim);
135    let formats = if verbatim {
136        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [default]))vec![default]
137    } else {
138        // While the official naming convention for MSVC import libraries
139        // is foo.lib, Meson follows the libfoo.dll.a convention to
140        // disambiguate .a for static libraries
141        let meson = ("lib", ".dll.a");
142        // and MinGW uses .a altogether
143        let mingw = ("lib", ".a");
144        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [default, meson, mingw]))vec![default, meson, mingw]
145    };
146
147    walk_native_lib_search_dirs(sess, None, |dir, is_framework| {
148        if !is_framework {
149            for (prefix, suffix) in &formats {
150                let test = dir.join(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1}{2}", prefix, name, suffix))
    })format!("{prefix}{name}{suffix}"));
151                if test.exists() {
152                    return ControlFlow::Break(test);
153                }
154            }
155        }
156        ControlFlow::Continue(())
157    })
158    .break_value()
159}
160
161pub fn find_native_static_library(name: &str, verbatim: bool, sess: &Session) -> PathBuf {
162    try_find_native_static_library(sess, name, verbatim)
163        .unwrap_or_else(|| sess.dcx().emit_fatal(errors::MissingNativeLibrary::new(name, verbatim)))
164}
165
166fn find_bundled_library(
167    name: Symbol,
168    verbatim: Option<bool>,
169    kind: NativeLibKind,
170    has_cfg: bool,
171    tcx: TyCtxt<'_>,
172) -> Option<Symbol> {
173    let sess = tcx.sess;
174    if let NativeLibKind::Static { bundle: Some(true) | None, whole_archive, .. } = kind
175        && tcx.crate_types().iter().any(|t| #[allow(non_exhaustive_omitted_patterns)] match t {
    &CrateType::Rlib | CrateType::StaticLib => true,
    _ => false,
}matches!(t, &CrateType::Rlib | CrateType::StaticLib))
176        && (sess.opts.unstable_opts.packed_bundled_libs || has_cfg || whole_archive == Some(true))
177    {
178        let verbatim = verbatim.unwrap_or(false);
179        return find_native_static_library(name.as_str(), verbatim, sess)
180            .file_name()
181            .and_then(|s| s.to_str())
182            .map(Symbol::intern);
183    }
184    None
185}
186
187pub(crate) fn collect(tcx: TyCtxt<'_>, LocalCrate: LocalCrate) -> Vec<NativeLib> {
188    let mut collector = Collector { tcx, libs: Vec::new() };
189    if tcx.sess.opts.unstable_opts.link_directives {
190        for module in tcx.foreign_modules(LOCAL_CRATE).values() {
191            collector.process_module(module);
192        }
193    }
194    collector.process_command_line();
195    collector.libs
196}
197
198pub(crate) fn relevant_lib(sess: &Session, lib: &NativeLib) -> bool {
199    match lib.cfg {
200        Some(ref cfg) => eval_config_entry(sess, cfg).as_bool(),
201        None => true,
202    }
203}
204
205struct Collector<'tcx> {
206    tcx: TyCtxt<'tcx>,
207    libs: Vec<NativeLib>,
208}
209
210impl<'tcx> Collector<'tcx> {
211    fn process_module(&mut self, module: &ForeignModule) {
212        let ForeignModule { def_id, abi, ref foreign_items } = *module;
213        let def_id = def_id.expect_local();
214
215        let sess = self.tcx.sess;
216
217        if #[allow(non_exhaustive_omitted_patterns)] match abi {
    ExternAbi::Rust => true,
    _ => false,
}matches!(abi, ExternAbi::Rust) {
218            return;
219        }
220
221        for attr in
222            {
    {
        'done:
            {
            for i in
                ::rustc_hir::attrs::HasAttrs::get_attrs(def_id, &self.tcx) {
                #[allow(unused_imports)]
                use rustc_hir::attrs::AttributeKind::*;
                let i: &rustc_hir::Attribute = i;
                match i {
                    rustc_hir::Attribute::Parsed(Link(links, _)) => {
                        break 'done Some(links);
                    }
                    rustc_hir::Attribute::Unparsed(..) =>
                        {}
                        #[deny(unreachable_patterns)]
                        _ => {}
                }
            }
            None
        }
    }
}find_attr!(self.tcx, def_id, Link(links, _) => links).iter().map(|v| v.iter()).flatten()
223        {
224            let dll_imports = match attr.kind {
225                NativeLibKind::RawDylib { .. } => foreign_items
226                    .iter()
227                    .filter_map(|&child_item| {
228                        self.build_dll_import(
229                            abi,
230                            attr.import_name_type.map(|(import_name_type, _)| import_name_type),
231                            child_item,
232                        )
233                    })
234                    .collect(),
235                _ => {
236                    for &child_item in foreign_items {
237                        if let Some(span) =
238                            {
    {
        'done:
            {
            for i in
                ::rustc_hir::attrs::HasAttrs::get_attrs(child_item, &self.tcx)
                {
                #[allow(unused_imports)]
                use rustc_hir::attrs::AttributeKind::*;
                let i: &rustc_hir::Attribute = i;
                match i {
                    rustc_hir::Attribute::Parsed(LinkOrdinal { span, .. }) => {
                        break 'done Some(*span);
                    }
                    rustc_hir::Attribute::Unparsed(..) =>
                        {}
                        #[deny(unreachable_patterns)]
                        _ => {}
                }
            }
            None
        }
    }
}find_attr!(self.tcx, child_item, LinkOrdinal {span, ..} => *span)
239                        {
240                            sess.dcx().emit_err(errors::LinkOrdinalRawDylib { span });
241                        }
242                    }
243
244                    Vec::new()
245                }
246            };
247
248            let filename = find_bundled_library(
249                attr.name,
250                attr.verbatim,
251                attr.kind,
252                attr.cfg.is_some(),
253                self.tcx,
254            );
255            self.libs.push(NativeLib {
256                name: attr.name,
257                filename,
258                kind: attr.kind,
259                cfg: attr.cfg.clone(),
260                foreign_module: Some(def_id.to_def_id()),
261                verbatim: attr.verbatim,
262                dll_imports,
263            });
264        }
265    }
266
267    // Process libs passed on the command line
268    fn process_command_line(&mut self) {
269        // First, check for errors
270        let mut renames = FxHashSet::default();
271        for lib in &self.tcx.sess.opts.libs {
272            if let NativeLibKind::Framework { .. } = lib.kind
273                && !self.tcx.sess.target.is_like_darwin
274            {
275                // Cannot check this when parsing options because the target is not yet available.
276                self.tcx.dcx().emit_err(errors::LibFrameworkApple);
277            }
278            if let Some(ref new_name) = lib.new_name {
279                let any_duplicate = self.libs.iter().any(|n| n.name.as_str() == lib.name);
280                if new_name.is_empty() {
281                    self.tcx.dcx().emit_err(errors::EmptyRenamingTarget { lib_name: &lib.name });
282                } else if !any_duplicate {
283                    self.tcx.dcx().emit_err(errors::RenamingNoLink { lib_name: &lib.name });
284                } else if !renames.insert(&lib.name) {
285                    self.tcx.dcx().emit_err(errors::MultipleRenamings { lib_name: &lib.name });
286                }
287            }
288        }
289
290        // Update kind and, optionally, the name of all native libraries
291        // (there may be more than one) with the specified name. If any
292        // library is mentioned more than once, keep the latest mention
293        // of it, so that any possible dependent libraries appear before
294        // it. (This ensures that the linker is able to see symbols from
295        // all possible dependent libraries before linking in the library
296        // in question.)
297        for passed_lib in &self.tcx.sess.opts.libs {
298            // If we've already added any native libraries with the same
299            // name, they will be pulled out into `existing`, so that we
300            // can move them to the end of the list below.
301            let mut existing = self
302                .libs
303                .extract_if(.., |lib| {
304                    if lib.name.as_str() == passed_lib.name {
305                        // FIXME: This whole logic is questionable, whether modifiers are
306                        // involved or not, library reordering and kind overriding without
307                        // explicit `:rename` in particular.
308                        if lib.has_modifiers() || passed_lib.has_modifiers() {
309                            match lib.foreign_module {
310                                Some(def_id) => {
311                                    self.tcx.dcx().emit_err(errors::NoLinkModOverride {
312                                        span: Some(self.tcx.def_span(def_id)),
313                                    })
314                                }
315                                None => self
316                                    .tcx
317                                    .dcx()
318                                    .emit_err(errors::NoLinkModOverride { span: None }),
319                            };
320                        }
321                        if passed_lib.kind != NativeLibKind::Unspecified {
322                            lib.kind = passed_lib.kind;
323                        }
324                        if let Some(new_name) = &passed_lib.new_name {
325                            lib.name = Symbol::intern(new_name);
326                        }
327                        lib.verbatim = passed_lib.verbatim;
328                        return true;
329                    }
330                    false
331                })
332                .collect::<Vec<_>>();
333            if existing.is_empty() {
334                // Add if not found
335                let new_name: Option<&str> = passed_lib.new_name.as_deref();
336                let name = Symbol::intern(new_name.unwrap_or(&passed_lib.name));
337                let filename = find_bundled_library(
338                    name,
339                    passed_lib.verbatim,
340                    passed_lib.kind,
341                    false,
342                    self.tcx,
343                );
344                self.libs.push(NativeLib {
345                    name,
346                    filename,
347                    kind: passed_lib.kind,
348                    cfg: None,
349                    foreign_module: None,
350                    verbatim: passed_lib.verbatim,
351                    dll_imports: Vec::new(),
352                });
353            } else {
354                // Move all existing libraries with the same name to the
355                // end of the command line.
356                self.libs.append(&mut existing);
357            }
358        }
359    }
360
361    fn i686_arg_list_size(&self, item: DefId) -> usize {
362        let argument_types: &List<Ty<'_>> = self.tcx.instantiate_bound_regions_with_erased(
363            self.tcx
364                .type_of(item)
365                .instantiate_identity()
366                .skip_norm_wip()
367                .fn_sig(self.tcx)
368                .inputs()
369                .map_bound(|slice| self.tcx.mk_type_list(slice)),
370        );
371
372        argument_types
373            .iter()
374            .map(|ty| {
375                let layout = self
376                    .tcx
377                    .layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty))
378                    .expect("layout")
379                    .layout;
380                // In both stdcall and fastcall, we always round up the argument size to the
381                // nearest multiple of 4 bytes.
382                (layout.size().bytes_usize() + 3) & !3
383            })
384            .sum()
385    }
386
387    fn build_dll_import(
388        &self,
389        abi: ExternAbi,
390        import_name_type: Option<PeImportNameType>,
391        item: DefId,
392    ) -> Option<DllImport> {
393        let span = self.tcx.def_span(item);
394
395        // This `extern` block should have been checked for general ABI support before, but let's
396        // double-check that.
397        if !self.tcx.sess.target.is_abi_supported(abi) {
    ::core::panicking::panic("assertion failed: self.tcx.sess.target.is_abi_supported(abi)")
};assert!(self.tcx.sess.target.is_abi_supported(abi));
398
399        // This logic is similar to `AbiMap::canonize_abi` (in rustc_target/src/spec/abi_map.rs) but
400        // we need more detail than those adjustments, and we can't support all ABIs that are
401        // generally supported.
402        let calling_convention = if self.tcx.sess.target.arch == Arch::X86 {
403            match abi {
404                ExternAbi::C { .. } | ExternAbi::Cdecl { .. } => DllCallingConvention::C,
405                ExternAbi::Stdcall { .. } => {
406                    DllCallingConvention::Stdcall(self.i686_arg_list_size(item))
407                }
408                // On Windows, `extern "system"` behaves like msvc's `__stdcall`.
409                // `__stdcall` only applies on x86 and on non-variadic functions:
410                // https://learn.microsoft.com/en-us/cpp/cpp/stdcall?view=msvc-170
411                ExternAbi::System { .. } => {
412                    let c_variadic = self
413                        .tcx
414                        .type_of(item)
415                        .instantiate_identity()
416                        .skip_norm_wip()
417                        .fn_sig(self.tcx)
418                        .c_variadic();
419
420                    if c_variadic {
421                        DllCallingConvention::C
422                    } else {
423                        DllCallingConvention::Stdcall(self.i686_arg_list_size(item))
424                    }
425                }
426                ExternAbi::Fastcall { .. } => {
427                    DllCallingConvention::Fastcall(self.i686_arg_list_size(item))
428                }
429                ExternAbi::Vectorcall { .. } => {
430                    DllCallingConvention::Vectorcall(self.i686_arg_list_size(item))
431                }
432                _ => {
433                    self.tcx.dcx().emit_fatal(errors::RawDylibUnsupportedAbi { span });
434                }
435            }
436        } else {
437            match abi {
438                ExternAbi::C { .. } | ExternAbi::Win64 { .. } | ExternAbi::System { .. } => {
439                    DllCallingConvention::C
440                }
441                _ => {
442                    self.tcx.dcx().emit_fatal(errors::RawDylibUnsupportedAbi { span });
443                }
444            }
445        };
446
447        let codegen_fn_attrs = self.tcx.codegen_fn_attrs(item);
448        let import_name_type = codegen_fn_attrs
449            .link_ordinal
450            .map_or(import_name_type, |ord| Some(PeImportNameType::Ordinal(ord)));
451
452        let name = codegen_fn_attrs.symbol_name.unwrap_or_else(|| self.tcx.item_name(item));
453
454        if self.tcx.sess.target.binary_format == BinaryFormat::Elf {
455            let name = name.as_str();
456            if name.contains('\0') {
457                self.tcx.dcx().emit_err(errors::RawDylibMalformed { span });
458            } else if let Some((left, right)) = name.split_once('@')
459                && (left.is_empty() || right.is_empty() || right.contains('@'))
460            {
461                self.tcx.dcx().emit_err(errors::RawDylibMalformed { span });
462            }
463        }
464
465        let def_kind = self.tcx.def_kind(item);
466        let symbol_type = if def_kind.is_fn_like() {
467            DllImportSymbolType::Function
468        } else if #[allow(non_exhaustive_omitted_patterns)] match def_kind {
    DefKind::Static { .. } => true,
    _ => false,
}matches!(def_kind, DefKind::Static { .. }) {
469            if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
470                DllImportSymbolType::ThreadLocal
471            } else {
472                DllImportSymbolType::Static
473            }
474        } else if def_kind == DefKind::ForeignTy {
475            return None;
476        } else {
477            ::rustc_middle::util::bug::bug_fmt(format_args!("Unexpected type for raw-dylib: {0}",
        def_kind.descr(item)));bug!("Unexpected type for raw-dylib: {}", def_kind.descr(item));
478        };
479
480        let size = match symbol_type {
481            // We cannot determine the size of a function at compile time, but it shouldn't matter anyway.
482            DllImportSymbolType::Function => rustc_abi::Size::ZERO,
483            DllImportSymbolType::Static | DllImportSymbolType::ThreadLocal => {
484                let ty = self.tcx.type_of(item).instantiate_identity().skip_norm_wip();
485                self.tcx
486                    .layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty))
487                    .ok()
488                    .map(|layout| layout.size)
489                    .unwrap_or_else(|| ::rustc_middle::util::bug::bug_fmt(format_args!("Non-function symbols must have a size"))bug!("Non-function symbols must have a size"))
490            }
491        };
492
493        Some(DllImport { name, import_name_type, calling_convention, span, symbol_type, size })
494    }
495}