2019-02-18 03:58:58 +09:00
|
|
|
use crate::llvm::{self, SetUnnamedAddr, True};
|
|
|
|
use crate::debuginfo;
|
|
|
|
use crate::monomorphize::MonoItem;
|
|
|
|
use crate::common::CodegenCx;
|
|
|
|
use crate::base;
|
|
|
|
use crate::type_::Type;
|
|
|
|
use crate::type_of::LayoutLlvmExt;
|
|
|
|
use crate::value::Value;
|
2018-07-13 11:30:47 -07:00
|
|
|
use libc::c_uint;
|
2016-03-29 12:54:26 +03:00
|
|
|
use rustc::hir::def_id::DefId;
|
2018-10-03 16:56:24 +02:00
|
|
|
use rustc::mir::interpret::{ConstValue, Allocation, read_target_uint,
|
|
|
|
Pointer, ErrorHandled, GlobalId};
|
2018-08-25 15:56:16 +01:00
|
|
|
use rustc::hir::Node;
|
2018-07-23 08:43:22 -04:00
|
|
|
use syntax_pos::Span;
|
2018-10-03 16:56:24 +02:00
|
|
|
use rustc_target::abi::HasDataLayout;
|
2019-05-08 13:21:18 +10:00
|
|
|
use syntax::symbol::sym;
|
2018-07-23 08:43:22 -04:00
|
|
|
use syntax_pos::symbol::LocalInternedString;
|
2019-05-23 12:15:48 -05:00
|
|
|
use rustc::ty::{self, Ty, Instance};
|
2018-11-16 13:45:28 +02:00
|
|
|
use rustc_codegen_ssa::traits::*;
|
2018-07-10 13:28:39 +03:00
|
|
|
|
2018-09-09 01:16:45 +03:00
|
|
|
use rustc::ty::layout::{self, Size, Align, LayoutOf};
|
2012-12-13 13:05:22 -08:00
|
|
|
|
2018-07-23 08:43:22 -04:00
|
|
|
use rustc::hir::{self, CodegenFnAttrs, CodegenFnAttrFlags};
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2015-06-28 10:36:46 -07:00
|
|
|
use std::ffi::{CStr, CString};
|
2012-07-31 18:34:36 -07:00
|
|
|
|
2018-10-03 13:49:57 +02:00
|
|
|
pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll Value {
|
|
|
|
let mut llvals = Vec::with_capacity(alloc.relocations.len() + 1);
|
|
|
|
let dl = cx.data_layout();
|
|
|
|
let pointer_size = dl.pointer_size.bytes() as usize;
|
|
|
|
|
|
|
|
let mut next_offset = 0;
|
|
|
|
for &(offset, ((), alloc_id)) in alloc.relocations.iter() {
|
|
|
|
let offset = offset.bytes();
|
|
|
|
assert_eq!(offset as usize as u64, offset);
|
|
|
|
let offset = offset as usize;
|
|
|
|
if offset > next_offset {
|
|
|
|
llvals.push(cx.const_bytes(&alloc.bytes[next_offset..offset]));
|
|
|
|
}
|
|
|
|
let ptr_offset = read_target_uint(
|
|
|
|
dl.endian,
|
|
|
|
&alloc.bytes[offset..(offset + pointer_size)],
|
|
|
|
).expect("const_alloc_to_llvm: could not read relocation pointer") as u64;
|
|
|
|
llvals.push(cx.scalar_to_backend(
|
|
|
|
Pointer::new(alloc_id, Size::from_bytes(ptr_offset)).into(),
|
|
|
|
&layout::Scalar {
|
|
|
|
value: layout::Primitive::Pointer,
|
|
|
|
valid_range: 0..=!0
|
|
|
|
},
|
|
|
|
cx.type_i8p()
|
|
|
|
));
|
|
|
|
next_offset = offset + pointer_size;
|
|
|
|
}
|
|
|
|
if alloc.bytes.len() >= next_offset {
|
|
|
|
llvals.push(cx.const_bytes(&alloc.bytes[next_offset ..]));
|
|
|
|
}
|
|
|
|
|
|
|
|
cx.const_struct(&llvals, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn codegen_static_initializer(
|
|
|
|
cx: &CodegenCx<'ll, 'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
) -> Result<(&'ll Value, &'tcx Allocation), ErrorHandled> {
|
|
|
|
let instance = ty::Instance::mono(cx.tcx, def_id);
|
|
|
|
let cid = GlobalId {
|
|
|
|
instance,
|
|
|
|
promoted: None,
|
|
|
|
};
|
|
|
|
let param_env = ty::ParamEnv::reveal_all();
|
|
|
|
let static_ = cx.tcx.const_eval(param_env.and(cid))?;
|
|
|
|
|
|
|
|
let alloc = match static_.val {
|
2019-02-16 14:29:27 +01:00
|
|
|
ConstValue::ByRef(ptr, alloc) if ptr.offset.bytes() == 0 => alloc,
|
2018-10-03 13:49:57 +02:00
|
|
|
_ => bug!("static const eval returned {:#?}", static_),
|
|
|
|
};
|
|
|
|
Ok((const_alloc_to_llvm(cx, alloc), alloc))
|
|
|
|
}
|
2017-07-30 20:43:53 +03:00
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
fn set_global_alignment(cx: &CodegenCx<'ll, '_>,
|
|
|
|
gv: &'ll Value,
|
2018-09-09 01:16:45 +03:00
|
|
|
mut align: Align) {
|
2017-09-08 14:49:51 -07:00
|
|
|
// The target may require greater alignment for globals than the type does.
|
|
|
|
// Note: GCC and Clang also allow `__attribute__((aligned))` on variables,
|
|
|
|
// which can force it to be smaller. Rust doesn't support this yet.
|
2018-01-05 07:04:08 +02:00
|
|
|
if let Some(min) = cx.sess().target.target.options.min_global_align {
|
2018-09-09 00:22:22 +03:00
|
|
|
match Align::from_bits(min) {
|
2018-09-09 01:16:45 +03:00
|
|
|
Ok(min) => align = align.max(min),
|
2017-09-08 14:49:51 -07:00
|
|
|
Err(err) => {
|
2018-01-05 07:04:08 +02:00
|
|
|
cx.sess().err(&format!("invalid minimum global alignment: {}", err));
|
2017-09-08 14:49:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe {
|
2018-09-09 01:16:45 +03:00
|
|
|
llvm::LLVMSetAlignment(gv, align.bytes() as u32);
|
2017-09-08 14:49:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
fn check_and_apply_linkage(
|
|
|
|
cx: &CodegenCx<'ll, 'tcx>,
|
2018-07-23 08:43:22 -04:00
|
|
|
attrs: &CodegenFnAttrs,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
sym: LocalInternedString,
|
2019-05-24 14:11:39 +02:00
|
|
|
span: Span
|
2018-07-10 13:28:39 +03:00
|
|
|
) -> &'ll Value {
|
2018-07-23 08:43:22 -04:00
|
|
|
let llty = cx.layout_of(ty).llvm_type(cx);
|
|
|
|
if let Some(linkage) = attrs.linkage {
|
|
|
|
debug!("get_static: sym={} linkage={:?}", sym, linkage);
|
|
|
|
|
|
|
|
// If this is a static with a linkage specified, then we need to handle
|
|
|
|
// it a little specially. The typesystem prevents things like &T and
|
|
|
|
// extern "C" fn() from being non-null, so we can't just declare a
|
|
|
|
// static and call it a day. Some linkages (like weak) will make it such
|
|
|
|
// that the static actually has a null value.
|
2018-10-08 16:58:26 +02:00
|
|
|
let llty2 = if let ty::RawPtr(ref mt) = ty.sty {
|
|
|
|
cx.layout_of(mt.ty).llvm_type(cx)
|
|
|
|
} else {
|
2019-05-24 14:55:15 +02:00
|
|
|
cx.sess().span_fatal(
|
|
|
|
span, "must have type `*const T` or `*mut T` due to `#[linkage]` attribute")
|
2018-07-23 08:43:22 -04:00
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
// Declare a symbol `foo` with the desired linkage.
|
2018-09-20 15:47:22 +02:00
|
|
|
let g1 = cx.declare_global(&sym, llty2);
|
2018-07-23 08:43:22 -04:00
|
|
|
llvm::LLVMRustSetLinkage(g1, base::linkage_to_llvm(linkage));
|
|
|
|
|
|
|
|
// Declare an internal global `extern_with_linkage_foo` which
|
|
|
|
// is initialized with the address of `foo`. If `foo` is
|
|
|
|
// discarded during linking (for example, if `foo` has weak
|
|
|
|
// linkage and there are no definitions), then
|
|
|
|
// `extern_with_linkage_foo` will instead be initialized to
|
|
|
|
// zero.
|
|
|
|
let mut real_name = "_rust_extern_with_linkage_".to_string();
|
|
|
|
real_name.push_str(&sym);
|
2018-09-20 15:47:22 +02:00
|
|
|
let g2 = cx.define_global(&real_name, llty).unwrap_or_else(||{
|
2019-05-24 14:11:39 +02:00
|
|
|
cx.sess().span_fatal(span, &format!("symbol `{}` is already defined", &sym))
|
2018-07-23 08:43:22 -04:00
|
|
|
});
|
|
|
|
llvm::LLVMRustSetLinkage(g2, llvm::Linkage::InternalLinkage);
|
|
|
|
llvm::LLVMSetInitializer(g2, g1);
|
|
|
|
g2
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Generate an external declaration.
|
|
|
|
// FIXME(nagisa): investigate whether it can be changed into define_global
|
2018-09-20 15:47:22 +02:00
|
|
|
cx.declare_global(&sym, llty)
|
2018-07-23 08:43:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 16:28:47 +02:00
|
|
|
pub fn ptrcast(val: &'ll Value, ty: &'ll Type) -> &'ll Value {
|
2013-01-10 21:23:07 -08:00
|
|
|
unsafe {
|
2018-09-10 16:28:47 +02:00
|
|
|
llvm::LLVMConstPointerCast(val, ty)
|
|
|
|
}
|
|
|
|
}
|
2015-06-28 10:36:46 -07:00
|
|
|
|
2018-11-24 17:11:59 +01:00
|
|
|
impl CodegenCx<'ll, 'tcx> {
|
2018-11-24 17:45:05 +01:00
|
|
|
crate fn const_bitcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
|
2018-09-10 16:28:47 +02:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMConstBitCast(val, ty)
|
|
|
|
}
|
|
|
|
}
|
2018-11-24 17:11:59 +01:00
|
|
|
|
2018-11-24 17:30:48 +01:00
|
|
|
crate fn static_addr_of_mut(
|
2018-09-10 16:28:47 +02:00
|
|
|
&self,
|
|
|
|
cv: &'ll Value,
|
2018-09-09 01:16:45 +03:00
|
|
|
align: Align,
|
2018-09-10 16:28:47 +02:00
|
|
|
kind: Option<&str>,
|
|
|
|
) -> &'ll Value {
|
|
|
|
unsafe {
|
|
|
|
let gv = match kind {
|
2018-11-07 12:08:41 +02:00
|
|
|
Some(kind) if !self.tcx.sess.fewer_names() => {
|
|
|
|
let name = self.generate_local_symbol_name(kind);
|
2018-09-20 15:47:22 +02:00
|
|
|
let gv = self.define_global(&name[..],
|
2018-11-07 12:08:41 +02:00
|
|
|
self.val_ty(cv)).unwrap_or_else(||{
|
2018-09-10 16:28:47 +02:00
|
|
|
bug!("symbol `{}` is already defined", name);
|
|
|
|
});
|
|
|
|
llvm::LLVMRustSetLinkage(gv, llvm::Linkage::PrivateLinkage);
|
|
|
|
gv
|
|
|
|
},
|
2018-09-20 15:47:22 +02:00
|
|
|
_ => self.define_private_global(self.val_ty(cv)),
|
2018-09-10 16:28:47 +02:00
|
|
|
};
|
|
|
|
llvm::LLVMSetInitializer(gv, cv);
|
|
|
|
set_global_alignment(&self, gv, align);
|
|
|
|
SetUnnamedAddr(gv, true);
|
|
|
|
gv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 18:36:58 +01:00
|
|
|
crate fn get_static(&self, def_id: DefId) -> &'ll Value {
|
2018-09-10 16:28:47 +02:00
|
|
|
let instance = Instance::mono(self.tcx, def_id);
|
2018-11-07 12:08:41 +02:00
|
|
|
if let Some(&g) = self.instances.borrow().get(&instance) {
|
2018-09-10 16:28:47 +02:00
|
|
|
return g;
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:08:41 +02:00
|
|
|
let defined_in_current_codegen_unit = self.codegen_unit
|
2018-09-10 16:28:47 +02:00
|
|
|
.items()
|
|
|
|
.contains_key(&MonoItem::Static(def_id));
|
|
|
|
assert!(!defined_in_current_codegen_unit,
|
|
|
|
"consts::get_static() should always hit the cache for \
|
|
|
|
statics defined in the same CGU, but did not for `{:?}`",
|
|
|
|
def_id);
|
|
|
|
|
|
|
|
let ty = instance.ty(self.tcx);
|
|
|
|
let sym = self.tcx.symbol_name(instance).as_str();
|
|
|
|
|
|
|
|
debug!("get_static: sym={} instance={:?}", sym, instance);
|
|
|
|
|
2019-03-04 09:00:30 +01:00
|
|
|
let g = if let Some(id) = self.tcx.hir().as_local_hir_id(def_id) {
|
2018-09-10 16:28:47 +02:00
|
|
|
|
2018-09-13 14:58:19 +02:00
|
|
|
let llty = self.layout_of(ty).llvm_type(self);
|
2019-03-04 09:00:30 +01:00
|
|
|
let (g, attrs) = match self.tcx.hir().get_by_hir_id(id) {
|
2018-09-10 16:28:47 +02:00
|
|
|
Node::Item(&hir::Item {
|
|
|
|
ref attrs, span, node: hir::ItemKind::Static(..), ..
|
|
|
|
}) => {
|
2018-09-20 15:47:22 +02:00
|
|
|
if self.get_declared_value(&sym[..]).is_some() {
|
2018-09-10 16:28:47 +02:00
|
|
|
span_bug!(span, "Conflicting symbol names for static?");
|
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
let g = self.define_global(&sym[..], llty).unwrap();
|
2018-09-10 16:28:47 +02:00
|
|
|
|
2018-11-07 12:08:41 +02:00
|
|
|
if !self.tcx.is_reachable_non_generic(def_id) {
|
2018-09-10 16:28:47 +02:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMRustSetVisibility(g, llvm::Visibility::Hidden);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(g, attrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
Node::ForeignItem(&hir::ForeignItem {
|
|
|
|
ref attrs, span, node: hir::ForeignItemKind::Static(..), ..
|
|
|
|
}) => {
|
2018-11-07 12:08:41 +02:00
|
|
|
let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
|
2019-05-24 14:11:39 +02:00
|
|
|
(check_and_apply_linkage(&self, &fn_attrs, ty, sym, span), attrs)
|
2018-09-10 16:28:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
item => bug!("get_static: expected static, found {:?}", item)
|
|
|
|
};
|
|
|
|
|
|
|
|
debug!("get_static: sym={} attrs={:?}", sym, attrs);
|
|
|
|
|
|
|
|
for attr in attrs {
|
2019-05-08 13:21:18 +10:00
|
|
|
if attr.check_name(sym::thread_local) {
|
2018-09-10 16:28:47 +02:00
|
|
|
llvm::set_thread_local_mode(g, self.tls_model);
|
|
|
|
}
|
|
|
|
}
|
2015-06-28 10:36:46 -07:00
|
|
|
|
2016-08-16 17:41:38 +03:00
|
|
|
g
|
2015-06-28 10:36:46 -07:00
|
|
|
} else {
|
2018-09-10 16:28:47 +02:00
|
|
|
// FIXME(nagisa): perhaps the map of externs could be offloaded to llvm somehow?
|
2018-11-07 12:08:41 +02:00
|
|
|
debug!("get_static: sym={} item_attr={:?}", sym, self.tcx.item_attrs(def_id));
|
2018-09-10 16:28:47 +02:00
|
|
|
|
2018-11-07 12:08:41 +02:00
|
|
|
let attrs = self.tcx.codegen_fn_attrs(def_id);
|
2019-05-24 14:11:39 +02:00
|
|
|
let span = self.tcx.def_span(def_id);
|
|
|
|
let g = check_and_apply_linkage(&self, &attrs, ty, sym, span);
|
2018-09-10 16:28:47 +02:00
|
|
|
|
|
|
|
// Thread-local statics in some other crate need to *always* be linked
|
|
|
|
// against in a thread-local fashion, so we need to be sure to apply the
|
|
|
|
// thread-local attribute locally if it was present remotely. If we
|
|
|
|
// don't do this then linker errors can be generated where the linker
|
|
|
|
// complains that one object files has a thread local version of the
|
|
|
|
// symbol and another one doesn't.
|
|
|
|
if attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
|
|
|
|
llvm::set_thread_local_mode(g, self.tls_model);
|
|
|
|
}
|
|
|
|
|
|
|
|
let needs_dll_storage_attr =
|
2018-11-07 12:08:41 +02:00
|
|
|
self.use_dll_storage_attrs && !self.tcx.is_foreign_item(def_id) &&
|
2018-09-10 16:28:47 +02:00
|
|
|
// ThinLTO can't handle this workaround in all cases, so we don't
|
|
|
|
// emit the attrs. Instead we make them unnecessary by disallowing
|
2019-02-01 15:15:43 +01:00
|
|
|
// dynamic linking when linker plugin based LTO is enabled.
|
|
|
|
!self.tcx.sess.opts.cg.linker_plugin_lto.enabled();
|
2018-09-10 16:28:47 +02:00
|
|
|
|
|
|
|
// If this assertion triggers, there's something wrong with commandline
|
|
|
|
// argument validation.
|
2019-02-01 15:15:43 +01:00
|
|
|
debug_assert!(!(self.tcx.sess.opts.cg.linker_plugin_lto.enabled() &&
|
2018-09-10 16:28:47 +02:00
|
|
|
self.tcx.sess.target.target.options.is_like_msvc &&
|
|
|
|
self.tcx.sess.opts.cg.prefer_dynamic));
|
|
|
|
|
|
|
|
if needs_dll_storage_attr {
|
2018-11-27 02:59:49 +00:00
|
|
|
// This item is external but not foreign, i.e., it originates from an external Rust
|
2018-09-10 16:28:47 +02:00
|
|
|
// crate. Since we don't know whether this crate will be linked dynamically or
|
|
|
|
// statically in the final application, we always mark such symbols as 'dllimport'.
|
|
|
|
// If final linkage happens to be static, we rely on compiler-emitted __imp_ stubs
|
|
|
|
// to make things work.
|
|
|
|
//
|
|
|
|
// However, in some scenarios we defer emission of statics to downstream
|
|
|
|
// crates, so there are cases where a static with an upstream DefId
|
|
|
|
// is actually present in the current crate. We can find out via the
|
|
|
|
// is_codegened_item query.
|
2018-11-07 12:08:41 +02:00
|
|
|
if !self.tcx.is_codegened_item(def_id) {
|
2018-09-10 16:28:47 +02:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMSetDLLStorageClass(g, llvm::DLLStorageClass::DllImport);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g
|
2015-06-28 10:36:46 -07:00
|
|
|
};
|
2018-09-10 16:28:47 +02:00
|
|
|
|
|
|
|
if self.use_dll_storage_attrs && self.tcx.is_dllimport_foreign_item(def_id) {
|
|
|
|
// For foreign (native) libs we know the exact storage type to use.
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMSetDLLStorageClass(g, llvm::DLLStorageClass::DllImport);
|
rustc: Add `const` globals to the language
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
[rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 08:17:01 -07:00
|
|
|
}
|
2013-06-21 18:46:34 -07:00
|
|
|
}
|
2015-06-28 10:36:46 -07:00
|
|
|
|
2018-11-07 12:08:41 +02:00
|
|
|
self.instances.borrow_mut().insert(instance, g);
|
2018-09-10 16:28:47 +02:00
|
|
|
g
|
|
|
|
}
|
2018-11-26 18:36:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StaticMethods for CodegenCx<'ll, 'tcx> {
|
|
|
|
fn static_addr_of(
|
|
|
|
&self,
|
|
|
|
cv: &'ll Value,
|
|
|
|
align: Align,
|
|
|
|
kind: Option<&str>,
|
|
|
|
) -> &'ll Value {
|
|
|
|
if let Some(&gv) = self.const_globals.borrow().get(&cv) {
|
|
|
|
unsafe {
|
|
|
|
// Upgrade the alignment in cases where the same constant is used with different
|
|
|
|
// alignment requirements
|
|
|
|
let llalign = align.bytes() as u32;
|
|
|
|
if llalign > llvm::LLVMGetAlignment(gv) {
|
|
|
|
llvm::LLVMSetAlignment(gv, llalign);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gv;
|
|
|
|
}
|
|
|
|
let gv = self.static_addr_of_mut(cv, align, kind);
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMSetGlobalConstant(gv, True);
|
|
|
|
}
|
|
|
|
self.const_globals.borrow_mut().insert(cv, gv);
|
|
|
|
gv
|
|
|
|
}
|
2018-09-10 16:28:47 +02:00
|
|
|
|
|
|
|
fn codegen_static(
|
|
|
|
&self,
|
|
|
|
def_id: DefId,
|
|
|
|
is_mutable: bool,
|
|
|
|
) {
|
|
|
|
unsafe {
|
2018-11-07 12:08:41 +02:00
|
|
|
let attrs = self.tcx.codegen_fn_attrs(def_id);
|
2018-09-10 16:28:47 +02:00
|
|
|
|
2018-10-03 16:56:24 +02:00
|
|
|
let (v, alloc) = match codegen_static_initializer(&self, def_id) {
|
2018-09-10 16:28:47 +02:00
|
|
|
Ok(v) => v,
|
|
|
|
// Error has already been reported
|
|
|
|
Err(_) => return,
|
|
|
|
};
|
|
|
|
|
2018-11-07 12:08:41 +02:00
|
|
|
let g = self.get_static(def_id);
|
2018-09-10 16:28:47 +02:00
|
|
|
|
|
|
|
// boolean SSA values are i1, but they have to be stored in i8 slots,
|
|
|
|
// otherwise some LLVM optimization passes don't work as expected
|
|
|
|
let mut val_llty = self.val_ty(v);
|
|
|
|
let v = if val_llty == self.type_i1() {
|
|
|
|
val_llty = self.type_i8();
|
|
|
|
llvm::LLVMConstZExt(v, val_llty)
|
|
|
|
} else {
|
|
|
|
v
|
|
|
|
};
|
|
|
|
|
|
|
|
let instance = Instance::mono(self.tcx, def_id);
|
|
|
|
let ty = instance.ty(self.tcx);
|
2018-09-13 14:58:19 +02:00
|
|
|
let llty = self.layout_of(ty).llvm_type(self);
|
2018-09-10 16:28:47 +02:00
|
|
|
let g = if val_llty == llty {
|
|
|
|
g
|
|
|
|
} else {
|
|
|
|
// If we created the global with the wrong type,
|
|
|
|
// correct the type.
|
|
|
|
let empty_string = const_cstr!("");
|
|
|
|
let name_str_ref = CStr::from_ptr(llvm::LLVMGetValueName(g));
|
|
|
|
let name_string = CString::new(name_str_ref.to_bytes()).unwrap();
|
|
|
|
llvm::LLVMSetValueName(g, empty_string.as_ptr());
|
|
|
|
|
|
|
|
let linkage = llvm::LLVMRustGetLinkage(g);
|
|
|
|
let visibility = llvm::LLVMRustGetVisibility(g);
|
|
|
|
|
|
|
|
let new_g = llvm::LLVMRustGetOrInsertGlobal(
|
2018-11-07 12:08:41 +02:00
|
|
|
self.llmod, name_string.as_ptr(), val_llty);
|
2018-09-10 16:28:47 +02:00
|
|
|
|
|
|
|
llvm::LLVMRustSetLinkage(new_g, linkage);
|
|
|
|
llvm::LLVMRustSetVisibility(new_g, visibility);
|
|
|
|
|
|
|
|
// To avoid breaking any invariants, we leave around the old
|
|
|
|
// global for the moment; we'll replace all references to it
|
|
|
|
// with the new global later. (See base::codegen_backend.)
|
2018-11-07 12:08:41 +02:00
|
|
|
self.statics_to_rauw.borrow_mut().push((g, new_g));
|
2018-09-10 16:28:47 +02:00
|
|
|
new_g
|
|
|
|
};
|
|
|
|
set_global_alignment(&self, g, self.align_of(ty));
|
|
|
|
llvm::LLVMSetInitializer(g, v);
|
|
|
|
|
|
|
|
// As an optimization, all shared statics which do not have interior
|
|
|
|
// mutability are placed into read-only memory.
|
|
|
|
if !is_mutable {
|
|
|
|
if self.type_is_freeze(ty) {
|
|
|
|
llvm::LLVMSetGlobalConstant(g, llvm::True);
|
|
|
|
}
|
2018-06-28 06:24:09 +08:00
|
|
|
}
|
2016-05-06 20:02:09 -04:00
|
|
|
|
2018-09-10 16:28:47 +02:00
|
|
|
debuginfo::create_global_var_metadata(&self, def_id, g);
|
|
|
|
|
|
|
|
if attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
|
|
|
|
llvm::set_thread_local_mode(g, self.tls_model);
|
|
|
|
|
|
|
|
// Do not allow LLVM to change the alignment of a TLS on macOS.
|
|
|
|
//
|
|
|
|
// By default a global's alignment can be freely increased.
|
|
|
|
// This allows LLVM to generate more performant instructions
|
2018-11-27 02:59:49 +00:00
|
|
|
// e.g., using load-aligned into a SIMD register.
|
2018-09-10 16:28:47 +02:00
|
|
|
//
|
|
|
|
// However, on macOS 10.10 or below, the dynamic linker does not
|
|
|
|
// respect any alignment given on the TLS (radar 24221680).
|
|
|
|
// This will violate the alignment assumption, and causing segfault at runtime.
|
|
|
|
//
|
|
|
|
// This bug is very easy to trigger. In `println!` and `panic!`,
|
|
|
|
// the `LOCAL_STDOUT`/`LOCAL_STDERR` handles are stored in a TLS,
|
|
|
|
// which the values would be `mem::replace`d on initialization.
|
|
|
|
// The implementation of `mem::replace` will use SIMD
|
|
|
|
// whenever the size is 32 bytes or higher. LLVM notices SIMD is used
|
|
|
|
// and tries to align `LOCAL_STDOUT`/`LOCAL_STDERR` to a 32-byte boundary,
|
|
|
|
// which macOS's dyld disregarded and causing crashes
|
|
|
|
// (see issues #51794, #51758, #50867, #48866 and #44056).
|
|
|
|
//
|
|
|
|
// To workaround the bug, we trick LLVM into not increasing
|
|
|
|
// the global's alignment by explicitly assigning a section to it
|
|
|
|
// (equivalent to automatically generating a `#[link_section]` attribute).
|
|
|
|
// See the comment in the `GlobalValue::canIncreaseAlignment()` function
|
|
|
|
// of `lib/IR/Globals.cpp` for why this works.
|
|
|
|
//
|
|
|
|
// When the alignment is not increased, the optimized `mem::replace`
|
|
|
|
// will use load-unaligned instructions instead, and thus avoiding the crash.
|
|
|
|
//
|
|
|
|
// We could remove this hack whenever we decide to drop macOS 10.10 support.
|
|
|
|
if self.tcx.sess.target.target.options.is_like_osx {
|
|
|
|
let sect_name = if alloc.bytes.iter().all(|b| *b == 0) {
|
|
|
|
CStr::from_bytes_with_nul_unchecked(b"__DATA,__thread_bss\0")
|
|
|
|
} else {
|
|
|
|
CStr::from_bytes_with_nul_unchecked(b"__DATA,__thread_data\0")
|
|
|
|
};
|
|
|
|
llvm::LLVMSetSection(g, sect_name.as_ptr());
|
|
|
|
}
|
|
|
|
}
|
2016-05-06 20:02:09 -04:00
|
|
|
|
2018-09-10 16:28:47 +02:00
|
|
|
|
|
|
|
// Wasm statics with custom link sections get special treatment as they
|
|
|
|
// go into custom sections of the wasm executable.
|
|
|
|
if self.tcx.sess.opts.target_triple.triple().starts_with("wasm32") {
|
|
|
|
if let Some(section) = attrs.link_section {
|
|
|
|
let section = llvm::LLVMMDStringInContext(
|
2018-11-07 12:08:41 +02:00
|
|
|
self.llcx,
|
2018-09-10 16:28:47 +02:00
|
|
|
section.as_str().as_ptr() as *const _,
|
|
|
|
section.as_str().len() as c_uint,
|
|
|
|
);
|
|
|
|
let alloc = llvm::LLVMMDStringInContext(
|
2018-11-07 12:08:41 +02:00
|
|
|
self.llcx,
|
2018-09-10 16:28:47 +02:00
|
|
|
alloc.bytes.as_ptr() as *const _,
|
|
|
|
alloc.bytes.len() as c_uint,
|
|
|
|
);
|
|
|
|
let data = [section, alloc];
|
2018-11-07 12:08:41 +02:00
|
|
|
let meta = llvm::LLVMMDNodeInContext(self.llcx, data.as_ptr(), 2);
|
2018-09-10 16:28:47 +02:00
|
|
|
llvm::LLVMAddNamedMetadataOperand(
|
2018-11-07 12:08:41 +02:00
|
|
|
self.llmod,
|
2018-09-10 16:28:47 +02:00
|
|
|
"wasm.custom_sections\0".as_ptr() as *const _,
|
|
|
|
meta,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
base::set_link_section(g, &attrs);
|
2018-07-13 11:30:47 -07:00
|
|
|
}
|
|
|
|
|
2018-09-10 16:28:47 +02:00
|
|
|
if attrs.flags.contains(CodegenFnAttrFlags::USED) {
|
|
|
|
// This static will be stored in the llvm.used variable which is an array of i8*
|
2018-11-07 12:08:41 +02:00
|
|
|
let cast = llvm::LLVMConstPointerCast(g, self.type_i8p());
|
|
|
|
self.used_statics.borrow_mut().push(cast);
|
2018-09-10 16:28:47 +02:00
|
|
|
}
|
2017-02-20 14:42:47 -05:00
|
|
|
}
|
2013-01-10 21:23:07 -08:00
|
|
|
}
|
2012-07-31 18:34:36 -07:00
|
|
|
}
|