3 use crate::context::CodegenCx;
4 use crate::errors::SymbolAlreadyDefined;
6 use crate::type_of::LayoutLlvmExt;
7 use rustc_codegen_ssa::traits::*;
8 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
9 pub use rustc_middle::mir::mono::MonoItem;
10 use rustc_middle::mir::mono::{Linkage, Visibility};
11 use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
12 use rustc_middle::ty::{self, Instance, TypeVisitable};
13 use rustc_session::config::CrateType;
14 use rustc_target::spec::RelocModel;
16 impl<'tcx> PreDefineMethods<'tcx> for CodegenCx<'_, 'tcx> {
21 visibility: Visibility,
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);
28 let g = self.define_global(symbol_name, llty).unwrap_or_else(|| {
30 .emit_fatal(SymbolAlreadyDefined { span: self.tcx.def_span(def_id), symbol_name })
34 llvm::LLVMRustSetLinkage(g, base::linkage_to_llvm(linkage));
35 llvm::LLVMRustSetVisibility(g, base::visibility_to_llvm(visibility));
36 if self.should_assume_dso_local(g, false) {
37 llvm::LLVMRustSetDSOLocal(g, true);
41 self.instances.borrow_mut().insert(instance, g);
46 instance: Instance<'tcx>,
48 visibility: Visibility,
51 assert!(!instance.substs.needs_infer());
53 let fn_abi = self.fn_abi_of_instance(instance, ty::List::empty());
54 let lldecl = self.declare_fn(symbol_name, fn_abi);
55 unsafe { llvm::LLVMRustSetLinkage(lldecl, base::linkage_to_llvm(linkage)) };
56 let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
57 base::set_link_section(lldecl, attrs);
58 if linkage == Linkage::LinkOnceODR || linkage == Linkage::WeakODR {
59 llvm::SetUniqueComdat(self.llmod, lldecl);
62 // If we're compiling the compiler-builtins crate, e.g., the equivalent of
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.
66 if linkage != Linkage::Internal
67 && linkage != Linkage::Private
68 && self.tcx.is_compiler_builtins(LOCAL_CRATE)
71 llvm::LLVMRustSetVisibility(lldecl, llvm::Visibility::Hidden);
75 llvm::LLVMRustSetVisibility(lldecl, base::visibility_to_llvm(visibility));
79 debug!("predefine_fn: instance = {:?}", instance);
81 attributes::from_fn_attrs(self, lldecl, instance);
84 if self.should_assume_dso_local(lldecl, false) {
85 llvm::LLVMRustSetDSOLocal(lldecl, true);
89 self.instances.borrow_mut().insert(instance, lldecl);
93 impl CodegenCx<'_, '_> {
94 /// Whether a definition or declaration can be assumed to be local to a group of
95 /// libraries that form a single DSO or executable.
96 pub(crate) unsafe fn should_assume_dso_local(
101 let linkage = llvm::LLVMRustGetLinkage(llval);
102 let visibility = llvm::LLVMRustGetVisibility(llval);
104 if matches!(linkage, llvm::Linkage::InternalLinkage | llvm::Linkage::PrivateLinkage) {
108 if visibility != llvm::Visibility::Default && linkage != llvm::Linkage::ExternalWeakLinkage
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 {
121 // PowerPC64 prefers TOC indirection to avoid copy relocations.
122 if matches!(&*self.tcx.sess.target.arch, "powerpc64" | "powerpc64le") {
126 // Thread-local variables generally don't support copy relocations.
127 let is_thread_local_var = llvm::LLVMIsAGlobalVariable(llval)
128 .map(|v| llvm::LLVMIsThreadLocal(v) == llvm::True)
130 if is_thread_local_var {
134 // Match clang by only supporting COFF and ELF for now.
135 if self.tcx.sess.target.is_like_osx {
139 // Static relocation model should force copy relocations everywhere.
140 if self.tcx.sess.relocation_model() == RelocModel::Static {
144 // With pie relocation model calls of functions defined in the translation
145 // unit can use copy relocations.
146 self.tcx.sess.relocation_model() == RelocModel::Pie && !is_declaration