Skip to main content

rustc_codegen_llvm/
mono_item.rs

1use std::borrow::Cow;
2use std::ffi::CString;
3
4use rustc_abi::AddressSpace;
5use rustc_attr_ir::Linkage;
6use rustc_codegen_ssa::traits::*;
7use rustc_hir::def::DefKind;
8use rustc_hir::def_id::{DefId, LOCAL_CRATE};
9use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
10use rustc_middle::mono::Visibility;
11use rustc_middle::ty::layout::{FnAbiOf, HasTypingEnv, LayoutOf};
12use rustc_middle::ty::{self, Instance, Ty, TypeVisitableExt};
13use rustc_span::bug;
14use rustc_structures::CrateType;
15use rustc_target::callconv::{FnAbi, PassMode};
16use rustc_target::spec::{Arch, RelocModel};
17use tracing::debug;
18
19use crate::abi::FnAbiLlvmExt;
20use crate::builder::Builder;
21use crate::context::CodegenCx;
22use crate::diagnostics::SymbolAlreadyDefined;
23use crate::{base, llvm};
24
25impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
26    fn predefine_static(
27        &mut self,
28        def_id: DefId,
29        linkage: Linkage,
30        visibility: Visibility,
31        symbol_name: &str,
32    ) {
33        let instance = Instance::mono(self.tcx, def_id);
34        let DefKind::Static { nested, .. } = self.tcx.def_kind(def_id) else { ::rustc_span::macros::bug_impl(None, format_args!("impossible case reached"),
    Location::caller())bug!() };
35        // Nested statics do not have a type, so pick a dummy type and let `codegen_static` figure
36        // out the llvm type from the actual evaluated initializer.
37        let ty =
38            if nested { self.tcx.types.unit } else { instance.ty(self.tcx, self.typing_env()) };
39        let llty = self.type_array(self.type_i8(), self.layout_of(ty).size.bytes());
40
41        let g = self.define_global(symbol_name, llty).unwrap_or_else(|| {
42            self.sess()
43                .dcx()
44                .emit_fatal(SymbolAlreadyDefined { span: self.tcx.def_span(def_id), symbol_name })
45        });
46
47        llvm::set_linkage(g, base::linkage_to_llvm(linkage));
48        self.set_visibility(g, linkage, visibility);
49
50        self.assume_dso_local(g, false);
51
52        let attrs = self.tcx.codegen_instance_attrs(instance.def);
53        self.add_static_aliases(g, &attrs.foreign_item_symbol_aliases);
54
55        self.instances.borrow_mut().insert(instance, g);
56    }
57
58    fn predefine_fn(
59        &mut self,
60        instance: Instance<'tcx>,
61        linkage: Linkage,
62        visibility: Visibility,
63        symbol_name: &str,
64    ) {
65        if !!instance.args.has_infer() {
    ::core::panicking::panic("assertion failed: !instance.args.has_infer()")
};assert!(!instance.args.has_infer());
66
67        let attrs = self.tcx.codegen_instance_attrs(instance.def);
68
69        let lldecl =
70            self.predefine_without_aliases(instance, &attrs, linkage, visibility, symbol_name);
71        self.add_function_aliases(instance, lldecl, &attrs, &attrs.foreign_item_symbol_aliases);
72
73        self.instances.borrow_mut().insert(instance, lldecl);
74    }
75}
76
77impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
78    fn predefine_without_aliases(
79        &self,
80        instance: Instance<'tcx>,
81        attrs: &Cow<'_, CodegenFnAttrs>,
82        linkage: Linkage,
83        visibility: Visibility,
84        symbol_name: &str,
85    ) -> &'ll llvm::Value {
86        let fn_abi: &FnAbi<'tcx, Ty<'tcx>> = self.fn_abi_of_instance(instance, ty::List::empty());
87        let lldecl = self.declare_fn(symbol_name, fn_abi, Some(instance));
88        llvm::set_linkage(lldecl, base::linkage_to_llvm(linkage));
89        base::set_link_section(lldecl, attrs);
90        if (linkage == Linkage::LinkOnceODR || linkage == Linkage::WeakODR)
91            && self.tcx.sess.target.supports_comdat()
92        {
93            llvm::SetUniqueComdat(self.llmod, lldecl);
94        }
95        self.set_visibility(lldecl, linkage, visibility);
96
97        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/mono_item.rs:97",
                        "rustc_codegen_llvm::mono_item", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/mono_item.rs"),
                        ::tracing_core::__macro_support::Option::Some(97u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::mono_item"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("predefine_fn: instance = {0:?}",
                                                    instance) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("predefine_fn: instance = {:?}", instance);
98
99        self.assume_dso_local(lldecl, false);
100
101        lldecl
102    }
103
104    /// LLVM has the concept of an `alias`.
105    /// We need this for the "externally implementable items" feature,
106    /// though it's generally useful.
107    ///
108    /// On macos, though this might be a more general problem, function symbols
109    /// have a fixed target architecture. This is necessary, since macos binaries
110    /// may contain code for both ARM and x86 macs.
111    ///
112    /// LLVM *can* add attributes for target architecture to function symbols,
113    /// cannot do so for statics, but importantly, also cannot for aliases
114    /// *even* when aliases may refer to a function symbol.
115    ///
116    /// This is not a problem: instead of using LLVM aliases, we can just generate
117    /// a new function symbol (with target architecture!) which effectively comes down to:
118    ///
119    /// ```ignore (illustrative example)
120    /// fn alias_name(...args) {
121    ///     original_name(...args)
122    /// }
123    /// ```
124    ///
125    /// That's also an alias.
126    ///
127    /// This does mean that the alias symbol has a different address than the original symbol
128    /// (assuming no optimizations by LLVM occur). This is unacceptable for statics.
129    /// So for statics we do want to use LLVM aliases, which is fine,
130    /// since for those we don't care about target architecture anyway.
131    ///
132    /// So, this function is for static aliases. See [`add_function_aliases`](Self::add_function_aliases) for the alternative.
133    fn add_static_aliases(
134        &self,
135        aliasee: &'ll llvm::Value,
136        aliases: &[(DefId, Linkage, Visibility)],
137    ) {
138        let ty = self.get_type_of_global(aliasee);
139
140        for (alias, linkage, visibility) in aliases {
141            let instance = Instance::mono(self.tcx, *alias);
142            let symbol_name = self.tcx.symbol_name(instance);
143            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/mono_item.rs:143",
                        "rustc_codegen_llvm::mono_item", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/mono_item.rs"),
                        ::tracing_core::__macro_support::Option::Some(143u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::mono_item"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("STATIC ALIAS: {0:?} {1:?} {2:?}",
                                                    alias, linkage, visibility) as
                                            &dyn ::tracing::field::Value))])
            });
    } else { ; }
};tracing::debug!("STATIC ALIAS: {alias:?} {linkage:?} {visibility:?}");
144
145            let lldecl = llvm::add_alias(
146                self.llmod,
147                ty,
148                AddressSpace::ZERO,
149                aliasee,
150                &CString::new(symbol_name.name).unwrap(),
151            );
152            // Add the alias name to the set of cached items, so there is no duplicate
153            // instance added to it during the normal `external static` codegen
154            let prev_entry = self.instances.borrow_mut().insert(instance, lldecl);
155
156            // If there already was a previous entry, then `add_static_aliases` was called multiple times for the same `alias`
157            // which would result in incorrect codegen
158            if !prev_entry.is_none() {
    {
        ::core::panicking::panic_fmt(format_args!("An instance was already present for {0:?}",
                instance));
    }
};assert!(prev_entry.is_none(), "An instance was already present for {instance:?}");
159
160            llvm::set_visibility(lldecl, base::visibility_to_llvm(*visibility));
161            llvm::set_linkage(lldecl, base::linkage_to_llvm(*linkage));
162        }
163    }
164
165    /// See [`add_static_aliases`](Self::add_static_aliases) for docs.
166    fn add_function_aliases(
167        &self,
168        aliasee_instance: Instance<'tcx>,
169        aliasee: &'ll llvm::Value,
170        attrs: &Cow<'_, CodegenFnAttrs>,
171        aliases: &[(DefId, Linkage, Visibility)],
172    ) {
173        for (alias, linkage, visibility) in aliases {
174            let symbol_name = self.tcx.symbol_name(Instance::mono(self.tcx, *alias));
175            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/mono_item.rs:175",
                        "rustc_codegen_llvm::mono_item", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/mono_item.rs"),
                        ::tracing_core::__macro_support::Option::Some(175u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::mono_item"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("FUNCTION ALIAS: generating fn {0} that calls {1:?} ({2:?} {3:?} {4:?})",
                                                    symbol_name.name, aliasee_instance, alias, linkage,
                                                    visibility) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};tracing::debug!(
176                "FUNCTION ALIAS: generating fn {} that calls {aliasee_instance:?} ({alias:?} {linkage:?} {visibility:?})",
177                symbol_name.name
178            );
179
180            // predefine another copy of the original instance
181            // with a new symbol name
182            let alias_lldecl = self.predefine_without_aliases(
183                aliasee_instance,
184                attrs,
185                *linkage,
186                *visibility,
187                symbol_name.name,
188            );
189
190            let fn_abi: &FnAbi<'tcx, Ty<'tcx>> =
191                self.fn_abi_of_instance(aliasee_instance, ty::List::empty());
192
193            // both the alias and the aliasee have the same ty
194            let fn_ty = fn_abi.llvm_type(self);
195            let start_llbb = Builder::append_block(self, alias_lldecl, "start");
196            let mut start_bx = Builder::build(self, start_llbb);
197
198            let num_params = llvm::count_params(alias_lldecl);
199            let mut args = Vec::with_capacity(num_params as usize);
200            for index in 0..num_params {
201                args.push(llvm::get_param(alias_lldecl, index));
202            }
203
204            // For an indirect return, the alias's own first parameter is the
205            // caller-provided return slot: forward it to the aliasee as such.
206            let (return_slot, args) = if fn_abi.ret.is_indirect() {
207                let (sret_ptr, rest) = args.split_first().unwrap();
208                (ReturnSlot::Indirect(*sret_ptr), rest)
209            } else {
210                (ReturnSlot::Direct, &args[..])
211            };
212            let call = start_bx.call(
213                fn_ty,
214                Some(attrs),
215                Some(fn_abi),
216                aliasee,
217                return_slot,
218                args,
219                None,
220                Some(aliasee_instance),
221            );
222
223            match &fn_abi.ret.mode {
224                PassMode::Ignore | PassMode::Indirect { .. } => start_bx.ret_void(),
225                PassMode::Direct(_) | PassMode::Pair { .. } | PassMode::Cast { .. } => {
226                    start_bx.ret(call)
227                }
228                PassMode::IndirectUnsized { .. } => ::rustc_span::macros::bug_impl(None,
    format_args!("unsized returns are not supported"), Location::caller())bug!("unsized returns are not supported"),
229            }
230        }
231    }
232
233    /// A definition or declaration can be assumed to be local to a group of
234    /// libraries that form a single DSO or executable.
235    /// Marks the local as DSO if so.
236    pub(crate) fn assume_dso_local(&self, llval: &llvm::Value, is_declaration: bool) -> bool {
237        let assume = self.should_assume_dso_local(llval, is_declaration);
238        if assume {
239            llvm::set_dso_local(llval);
240        }
241        assume
242    }
243
244    fn set_visibility(&self, lldecl: &llvm::Value, linkage: Linkage, visibility: Visibility) {
245        // If we're compiling the compiler-builtins crate, i.e., the equivalent of
246        // compiler-rt, then we want to implicitly compile everything with hidden
247        // visibility as we're going to link this object all over the place but
248        // don't want the symbols to get exported.
249        if linkage != Linkage::Internal && self.tcx.is_compiler_builtins(LOCAL_CRATE) {
250            llvm::set_visibility(lldecl, llvm::Visibility::Hidden);
251        } else {
252            llvm::set_visibility(lldecl, base::visibility_to_llvm(visibility));
253        }
254    }
255
256    fn should_assume_dso_local(&self, llval: &llvm::Value, is_declaration: bool) -> bool {
257        let linkage = llvm::get_linkage(llval);
258        let visibility = llvm::get_visibility(llval);
259
260        if #[allow(non_exhaustive_omitted_patterns)] match linkage {
    llvm::Linkage::InternalLinkage | llvm::Linkage::PrivateLinkage => true,
    _ => false,
}matches!(linkage, llvm::Linkage::InternalLinkage | llvm::Linkage::PrivateLinkage) {
261            return true;
262        }
263
264        if visibility != llvm::Visibility::Default && linkage != llvm::Linkage::ExternalWeakLinkage
265        {
266            return true;
267        }
268
269        // Symbols from executables can't really be imported any further.
270        let all_exe = self.tcx.crate_types().iter().all(|ty| *ty == CrateType::Executable);
271        let is_declaration_for_linker =
272            is_declaration || linkage == llvm::Linkage::AvailableExternallyLinkage;
273        if all_exe && !is_declaration_for_linker {
274            return true;
275        }
276
277        // PowerPC64 prefers TOC indirection to avoid copy relocations.
278        if self.tcx.sess.target.arch == Arch::PowerPC64 {
279            return false;
280        }
281
282        // Match clang by only supporting COFF and ELF for now.
283        if self.tcx.sess.target.is_like_darwin {
284            return false;
285        }
286
287        // With pie relocation model, calls of functions defined in the translation
288        // unit can use copy relocations.
289        if self.tcx.sess.relocation_model() == RelocModel::Pie && !is_declaration {
290            return true;
291        }
292
293        // Thread-local variables generally don't support copy relocations.
294        let is_thread_local_var = llvm::LLVMIsAGlobalVariable(llval)
295            .is_some_and(|v| llvm::LLVMIsThreadLocal(v).is_true());
296        if is_thread_local_var {
297            return false;
298        }
299
300        // Respect the direct-access-external-data to override default behavior if present.
301        if let Some(direct) = self.tcx.sess.direct_access_external_data() {
302            return direct;
303        }
304
305        // Static relocation model should force copy relocations everywhere.
306        self.tcx.sess.relocation_model() == RelocModel::Static
307    }
308}