2019-02-17 18:58:58 +00:00
|
|
|
use crate::attributes;
|
|
|
|
use crate::base;
|
|
|
|
use crate::context::CodegenCx;
|
2022-08-25 21:47:11 +00:00
|
|
|
use crate::errors::SymbolAlreadyDefined;
|
2019-02-17 18:58:58 +00:00
|
|
|
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};
|
2021-09-01 21:09:34 +00:00
|
|
|
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf};
|
2023-02-22 02:18:40 +00:00
|
|
|
use rustc_middle::ty::{self, Instance, TypeVisitableExt};
|
2021-03-27 22:11:24 +00:00
|
|
|
use rustc_session::config::CrateType;
|
|
|
|
use rustc_target::spec::RelocModel;
|
2016-05-09 17:37:14 +00:00
|
|
|
|
2021-12-14 18:49:49 +00:00
|
|
|
impl<'tcx> PreDefineMethods<'tcx> for CodegenCx<'_, 'tcx> {
|
2018-09-21 14:13:15 +00:00
|
|
|
fn predefine_static(
|
|
|
|
&self,
|
|
|
|
def_id: DefId,
|
|
|
|
linkage: Linkage,
|
|
|
|
visibility: Visibility,
|
|
|
|
symbol_name: &str,
|
|
|
|
) {
|
|
|
|
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(|| {
|
2022-08-25 21:47:11 +00:00
|
|
|
self.sess()
|
|
|
|
.emit_fatal(SymbolAlreadyDefined { span: self.tcx.def_span(def_id), 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-05-25 21:54:07 +00:00
|
|
|
if self.should_assume_dso_local(g, false) {
|
2021-03-27 22:11:24 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-21 14:13:15 +00:00
|
|
|
fn predefine_fn(
|
|
|
|
&self,
|
2018-11-30 15:53:44 +00:00
|
|
|
instance: Instance<'tcx>,
|
|
|
|
linkage: Linkage,
|
|
|
|
visibility: Visibility,
|
|
|
|
symbol_name: &str,
|
|
|
|
) {
|
2023-07-11 21:35:29 +00:00
|
|
|
assert!(!instance.args.has_infer());
|
2018-09-21 14:13:15 +00:00
|
|
|
|
2021-09-01 21:29:15 +00:00
|
|
|
let fn_abi = self.fn_abi_of_instance(instance, ty::List::empty());
|
2023-05-03 22:22:24 +00:00
|
|
|
let lldecl = self.declare_fn(symbol_name, fn_abi, Some(instance));
|
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());
|
2021-09-30 17:38:50 +00:00
|
|
|
base::set_link_section(lldecl, attrs);
|
2018-09-21 14:13:15 +00:00
|
|
|
if linkage == Linkage::LinkOnceODR || linkage == Linkage::WeakODR {
|
|
|
|
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.
|
|
|
|
if linkage != Linkage::Internal
|
|
|
|
&& linkage != Linkage::Private
|
|
|
|
&& self.tcx.is_compiler_builtins(LOCAL_CRATE)
|
|
|
|
{
|
|
|
|
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 {
|
2021-05-25 21:54:07 +00:00
|
|
|
if self.should_assume_dso_local(lldecl, false) {
|
2021-03-27 22:11:24 +00:00
|
|
|
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
|
|
|
|
2021-12-14 18:49:49 +00:00
|
|
|
impl CodegenCx<'_, '_> {
|
2021-05-25 21:54:07 +00:00
|
|
|
/// Whether a definition or declaration can be assumed to be local to a group of
|
2021-03-27 22:11:24 +00:00
|
|
|
/// libraries that form a single DSO or executable.
|
|
|
|
pub(crate) unsafe fn should_assume_dso_local(
|
|
|
|
&self,
|
2021-05-25 21:54:07 +00:00
|
|
|
llval: &llvm::Value,
|
|
|
|
is_declaration: bool,
|
2021-03-27 22:11:24 +00:00
|
|
|
) -> bool {
|
2021-05-25 21:54:07 +00:00
|
|
|
let linkage = llvm::LLVMRustGetLinkage(llval);
|
|
|
|
let visibility = llvm::LLVMRustGetVisibility(llval);
|
|
|
|
|
|
|
|
if matches!(linkage, llvm::Linkage::InternalLinkage | llvm::Linkage::PrivateLinkage) {
|
2021-03-27 22:11:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-25 21:54:07 +00:00
|
|
|
if visibility != llvm::Visibility::Default && linkage != llvm::Linkage::ExternalWeakLinkage
|
|
|
|
{
|
2021-03-27 22:11:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-25 21:54:07 +00:00
|
|
|
// Symbols from executables can't really be imported any further.
|
|
|
|
let all_exe = self.tcx.sess.crate_types().iter().all(|ty| *ty == CrateType::Executable);
|
|
|
|
let is_declaration_for_linker =
|
|
|
|
is_declaration || linkage == llvm::Linkage::AvailableExternallyLinkage;
|
|
|
|
if all_exe && !is_declaration_for_linker {
|
2021-03-27 22:11:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-25 21:54:07 +00:00
|
|
|
// PowerPC64 prefers TOC indirection to avoid copy relocations.
|
|
|
|
if matches!(&*self.tcx.sess.target.arch, "powerpc64" | "powerpc64le") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Thread-local variables generally don't support copy relocations.
|
|
|
|
let is_thread_local_var = llvm::LLVMIsAGlobalVariable(llval)
|
2023-05-24 14:33:43 +00:00
|
|
|
.is_some_and(|v| llvm::LLVMIsThreadLocal(v) == llvm::True);
|
2021-05-25 21:54:07 +00:00
|
|
|
if is_thread_local_var {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
Stop emitting the `dso_local` LLVM attribute for external symbols under the static relocation model on macOS.
This matches Clang's behavior:
https://github.com/llvm/llvm-project/blob/973cb2c326be9f256da0897c4d2ef117dc22761d/clang/lib/CodeGen/CodeGenModule.cpp#L1038-L1040
Even if `dso_local` were properly supported in this way on macOS, it seems
incorrect to add this annotation as liberally as we did. The `dso_local`
annotation is for symbols that ultimately end up in the same linkage unit, but
we were adding this annotation even for `static` values inside `extern` blocks
marked with `#[link(type="framework")]`, which should be considered dynamically
linked. Note that Clang likewise avoids emitting `dso_local` for `dllimport`
symbols:
https://github.com/llvm/llvm-project/blob/973cb2c326be9f256da0897c4d2ef117dc22761d/clang/lib/CodeGen/CodeGenModule.cpp#L1005-L1007
This issue caused breakage in the `ring` crate, which links to a symbol defined
in `Security.framework` that ultimately resolves to address `0x0`:
https://github.com/briansmith/ring/blob/b94d61e044b42827fefd71d5f61e8c58a7659870/src/rand.rs#L390
For this symbol, the use of `dso_local` causes LLVM to emit a relocation of
type `X86_64_RELOC_SIGNED`, which is a 32-bit signed PC-relative offset. If the
binary is large enough, `0x0` might be out of range, and the link will fail.
Avoiding `dso_local` causes LLVM to use the GOT instead, emitting a relocation
of type `X86_64_RELOC_GOT_LOAD`, which will properly handle the large offset
and cause the link to succeed.
As a side note, the static relocation model is effectively deprecated for
security reasons on macOS, as it prohibits PIE. It's also completely
unsupported on Apple Silicon, so I don't think it's worth going to the effort
of properly supporting this model on that platform.
2021-08-20 23:42:45 +00:00
|
|
|
// Match clang by only supporting COFF and ELF for now.
|
|
|
|
if self.tcx.sess.target.is_like_osx {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-25 21:54:07 +00:00
|
|
|
// Static relocation model should force copy relocations everywhere.
|
|
|
|
if self.tcx.sess.relocation_model() == RelocModel::Static {
|
2021-03-27 22:11:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-10 13:11:56 +00:00
|
|
|
// With pie relocation model calls of functions defined in the translation
|
|
|
|
// unit can use copy relocations.
|
2021-09-30 17:38:50 +00:00
|
|
|
self.tcx.sess.relocation_model() == RelocModel::Pie && !is_declaration
|
2021-03-27 22:11:24 +00:00
|
|
|
}
|
|
|
|
}
|