2019-10-29 16:34:50 +00:00
|
|
|
use crate::abi::FnAbi;
|
2019-02-17 18:58:58 +00:00
|
|
|
use crate::attributes;
|
|
|
|
use crate::base;
|
|
|
|
use crate::context::CodegenCx;
|
|
|
|
use crate::llvm;
|
|
|
|
use crate::type_of::LayoutLlvmExt;
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_codegen_ssa::traits::*;
|
|
|
|
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
|
2020-03-31 16:16:47 +00:00
|
|
|
pub use rustc_middle::mir::mono::MonoItem;
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::mir::mono::{Linkage, Visibility};
|
2020-03-31 16:16:47 +00:00
|
|
|
use rustc_middle::ty::layout::FnAbiExt;
|
2020-06-22 12:57:03 +00:00
|
|
|
use rustc_middle::ty::{self, Instance, TypeFoldable};
|
2021-03-27 22:11:24 +00:00
|
|
|
use rustc_session::config::CrateType;
|
2020-03-31 16:16:47 +00:00
|
|
|
use rustc_target::abi::LayoutOf;
|
2021-03-27 22:11:24 +00:00
|
|
|
use rustc_target::spec::RelocModel;
|
2020-08-05 11:35:53 +00:00
|
|
|
use tracing::debug;
|
2016-05-09 17:37:14 +00:00
|
|
|
|
2018-09-21 14:13:15 +00:00
|
|
|
impl PreDefineMethods<'tcx> for CodegenCx<'ll, 'tcx> {
|
2019-12-22 22:42:04 +00:00
|
|
|
fn predefine_static(
|
|
|
|
&self,
|
|
|
|
def_id: DefId,
|
|
|
|
linkage: Linkage,
|
|
|
|
visibility: Visibility,
|
|
|
|
symbol_name: &str,
|
|
|
|
) {
|
2018-09-21 14:13:15 +00:00
|
|
|
let instance = Instance::mono(self.tcx, def_id);
|
2020-06-22 12:57:03 +00:00
|
|
|
let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
|
2018-09-21 14:13:15 +00:00
|
|
|
let llty = self.layout_of(ty).llvm_type(self);
|
2017-09-12 18:04:46 +00:00
|
|
|
|
2018-09-21 14:13:15 +00:00
|
|
|
let g = self.define_global(symbol_name, llty).unwrap_or_else(|| {
|
2019-12-22 22:42:04 +00:00
|
|
|
self.sess().span_fatal(
|
|
|
|
self.tcx.def_span(def_id),
|
|
|
|
&format!("symbol `{}` is already defined", symbol_name),
|
|
|
|
)
|
2018-09-21 14:13:15 +00:00
|
|
|
});
|
2017-09-12 18:04:46 +00:00
|
|
|
|
2018-09-21 14:13:15 +00:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMRustSetLinkage(g, base::linkage_to_llvm(linkage));
|
|
|
|
llvm::LLVMRustSetVisibility(g, base::visibility_to_llvm(visibility));
|
2021-03-27 22:11:24 +00:00
|
|
|
if self.should_assume_dso_local(linkage, visibility) {
|
|
|
|
llvm::LLVMRustSetDSOLocal(g, true);
|
|
|
|
}
|
2018-09-21 14:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.instances.borrow_mut().insert(instance, g);
|
2017-09-12 18:04:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
fn predefine_fn(
|
|
|
|
&self,
|
|
|
|
instance: Instance<'tcx>,
|
|
|
|
linkage: Linkage,
|
|
|
|
visibility: Visibility,
|
|
|
|
symbol_name: &str,
|
|
|
|
) {
|
2020-06-22 12:57:03 +00:00
|
|
|
assert!(!instance.substs.needs_infer());
|
2018-09-21 14:13:15 +00:00
|
|
|
|
2019-10-29 19:46:25 +00:00
|
|
|
let fn_abi = FnAbi::of_instance(self, instance, &[]);
|
2019-10-29 16:34:50 +00:00
|
|
|
let lldecl = self.declare_fn(symbol_name, &fn_abi);
|
2018-09-21 14:13:15 +00:00
|
|
|
unsafe { llvm::LLVMRustSetLinkage(lldecl, base::linkage_to_llvm(linkage)) };
|
2019-10-29 16:34:50 +00:00
|
|
|
let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
|
2018-09-21 14:13:15 +00:00
|
|
|
base::set_link_section(lldecl, &attrs);
|
2019-12-22 22:42:04 +00:00
|
|
|
if linkage == Linkage::LinkOnceODR || linkage == Linkage::WeakODR {
|
2018-09-21 14:13:15 +00:00
|
|
|
llvm::SetUniqueComdat(self.llmod, lldecl);
|
2017-09-12 18:04:46 +00:00
|
|
|
}
|
2018-09-21 14:13:15 +00:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// If we're compiling the compiler-builtins crate, e.g., the equivalent of
|
2018-09-21 14:13:15 +00:00
|
|
|
// compiler-rt, then we want to implicitly compile everything with hidden
|
|
|
|
// visibility as we're going to link this object all over the place but
|
|
|
|
// don't want the symbols to get exported.
|
2019-12-22 22:42:04 +00:00
|
|
|
if linkage != Linkage::Internal
|
|
|
|
&& linkage != Linkage::Private
|
|
|
|
&& self.tcx.is_compiler_builtins(LOCAL_CRATE)
|
|
|
|
{
|
2018-09-21 14:13:15 +00:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMRustSetVisibility(lldecl, llvm::Visibility::Hidden);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMRustSetVisibility(lldecl, base::visibility_to_llvm(visibility));
|
|
|
|
}
|
2017-09-12 18:04:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:01:31 +00:00
|
|
|
debug!("predefine_fn: instance = {:?}", instance);
|
2019-11-27 10:53:19 +00:00
|
|
|
|
2020-03-31 12:27:09 +00:00
|
|
|
attributes::from_fn_attrs(self, lldecl, instance);
|
2017-09-12 18:04:46 +00:00
|
|
|
|
2021-03-27 22:11:24 +00:00
|
|
|
unsafe {
|
|
|
|
if self.should_assume_dso_local(linkage, visibility) {
|
|
|
|
llvm::LLVMRustSetDSOLocal(lldecl, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 14:13:15 +00:00
|
|
|
self.instances.borrow_mut().insert(instance, lldecl);
|
|
|
|
}
|
2017-09-12 18:04:46 +00:00
|
|
|
}
|
2021-03-27 22:11:24 +00:00
|
|
|
|
|
|
|
impl CodegenCx<'ll, 'tcx> {
|
|
|
|
/// Whether a definition (NB: not declaration!) can be assumed to be local to a group of
|
|
|
|
/// libraries that form a single DSO or executable.
|
|
|
|
pub(crate) unsafe fn should_assume_dso_local(
|
|
|
|
&self,
|
|
|
|
linkage: Linkage,
|
|
|
|
visibility: Visibility,
|
|
|
|
) -> bool {
|
|
|
|
if matches!(linkage, Linkage::Internal | Linkage::Private) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if visibility != Visibility::Default && linkage != Linkage::ExternalWeak {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Static relocation model should force copy relocations everywhere.
|
|
|
|
if self.tcx.sess.relocation_model() == RelocModel::Static {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Symbols from executables can't really be imported any further.
|
|
|
|
if self.tcx.sess.crate_types().iter().all(|ty| *ty == CrateType::Executable) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|