]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/mono_item.rs
Rollup merge of #92024 - pcwalton:per-codegen-unit-names, r=davidtwco
[rust.git] / compiler / rustc_codegen_llvm / src / mono_item.rs
1 use crate::attributes;
2 use crate::base;
3 use crate::context::CodegenCx;
4 use crate::llvm;
5 use crate::type_of::LayoutLlvmExt;
6 use rustc_codegen_ssa::traits::*;
7 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
8 pub use rustc_middle::mir::mono::MonoItem;
9 use rustc_middle::mir::mono::{Linkage, Visibility};
10 use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
11 use rustc_middle::ty::{self, Instance, TypeFoldable};
12 use rustc_session::config::CrateType;
13 use rustc_target::spec::RelocModel;
14 use tracing::debug;
15
16 impl<'tcx> PreDefineMethods<'tcx> for CodegenCx<'_, 'tcx> {
17     fn predefine_static(
18         &self,
19         def_id: DefId,
20         linkage: Linkage,
21         visibility: Visibility,
22         symbol_name: &str,
23     ) {
24         let instance = Instance::mono(self.tcx, def_id);
25         let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
26         let llty = self.layout_of(ty).llvm_type(self);
27
28         let g = self.define_global(symbol_name, llty).unwrap_or_else(|| {
29             self.sess().span_fatal(
30                 self.tcx.def_span(def_id),
31                 &format!("symbol `{}` is already defined", symbol_name),
32             )
33         });
34
35         unsafe {
36             llvm::LLVMRustSetLinkage(g, base::linkage_to_llvm(linkage));
37             llvm::LLVMRustSetVisibility(g, base::visibility_to_llvm(visibility));
38             if self.should_assume_dso_local(g, false) {
39                 llvm::LLVMRustSetDSOLocal(g, true);
40             }
41         }
42
43         self.instances.borrow_mut().insert(instance, g);
44     }
45
46     fn predefine_fn(
47         &self,
48         instance: Instance<'tcx>,
49         linkage: Linkage,
50         visibility: Visibility,
51         symbol_name: &str,
52     ) {
53         assert!(!instance.substs.needs_infer());
54
55         let fn_abi = self.fn_abi_of_instance(instance, ty::List::empty());
56         let lldecl = self.declare_fn(symbol_name, fn_abi);
57         unsafe { llvm::LLVMRustSetLinkage(lldecl, base::linkage_to_llvm(linkage)) };
58         let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
59         base::set_link_section(lldecl, attrs);
60         if linkage == Linkage::LinkOnceODR || linkage == Linkage::WeakODR {
61             llvm::SetUniqueComdat(self.llmod, lldecl);
62         }
63
64         // If we're compiling the compiler-builtins crate, e.g., the equivalent of
65         // compiler-rt, then we want to implicitly compile everything with hidden
66         // visibility as we're going to link this object all over the place but
67         // don't want the symbols to get exported.
68         if linkage != Linkage::Internal
69             && linkage != Linkage::Private
70             && self.tcx.is_compiler_builtins(LOCAL_CRATE)
71         {
72             unsafe {
73                 llvm::LLVMRustSetVisibility(lldecl, llvm::Visibility::Hidden);
74             }
75         } else {
76             unsafe {
77                 llvm::LLVMRustSetVisibility(lldecl, base::visibility_to_llvm(visibility));
78             }
79         }
80
81         debug!("predefine_fn: instance = {:?}", instance);
82
83         attributes::from_fn_attrs(self, lldecl, instance);
84
85         unsafe {
86             if self.should_assume_dso_local(lldecl, false) {
87                 llvm::LLVMRustSetDSOLocal(lldecl, true);
88             }
89         }
90
91         self.instances.borrow_mut().insert(instance, lldecl);
92     }
93 }
94
95 impl CodegenCx<'_, '_> {
96     /// Whether a definition or declaration can be assumed to be local to a group of
97     /// libraries that form a single DSO or executable.
98     pub(crate) unsafe fn should_assume_dso_local(
99         &self,
100         llval: &llvm::Value,
101         is_declaration: bool,
102     ) -> bool {
103         let linkage = llvm::LLVMRustGetLinkage(llval);
104         let visibility = llvm::LLVMRustGetVisibility(llval);
105
106         if matches!(linkage, llvm::Linkage::InternalLinkage | llvm::Linkage::PrivateLinkage) {
107             return true;
108         }
109
110         if visibility != llvm::Visibility::Default && linkage != llvm::Linkage::ExternalWeakLinkage
111         {
112             return true;
113         }
114
115         // Symbols from executables can't really be imported any further.
116         let all_exe = self.tcx.sess.crate_types().iter().all(|ty| *ty == CrateType::Executable);
117         let is_declaration_for_linker =
118             is_declaration || linkage == llvm::Linkage::AvailableExternallyLinkage;
119         if all_exe && !is_declaration_for_linker {
120             return true;
121         }
122
123         // PowerPC64 prefers TOC indirection to avoid copy relocations.
124         if matches!(&*self.tcx.sess.target.arch, "powerpc64" | "powerpc64le") {
125             return false;
126         }
127
128         // Thread-local variables generally don't support copy relocations.
129         let is_thread_local_var = llvm::LLVMIsAGlobalVariable(llval)
130             .map(|v| llvm::LLVMIsThreadLocal(v) == llvm::True)
131             .unwrap_or(false);
132         if is_thread_local_var {
133             return false;
134         }
135
136         // Match clang by only supporting COFF and ELF for now.
137         if self.tcx.sess.target.is_like_osx {
138             return false;
139         }
140
141         // Static relocation model should force copy relocations everywhere.
142         if self.tcx.sess.relocation_model() == RelocModel::Static {
143             return true;
144         }
145
146         // With pie relocation model calls of functions defined in the translation
147         // unit can use copy relocations.
148         self.tcx.sess.relocation_model() == RelocModel::Pie && !is_declaration
149     }
150 }