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