2 use crate::common::CodegenCx;
4 use crate::llvm::{self, True};
5 use crate::type_::Type;
6 use crate::type_of::LayoutLlvmExt;
7 use crate::value::Value;
10 use rustc_codegen_ssa::traits::*;
11 use rustc_hir::def_id::DefId;
12 use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
13 use rustc_middle::mir::interpret::{
14 read_target_uint, Allocation, ErrorHandled, GlobalAlloc, InitChunk, Pointer,
15 Scalar as InterpScalar,
17 use rustc_middle::mir::mono::MonoItem;
18 use rustc_middle::ty::layout::LayoutOf;
19 use rustc_middle::ty::{self, Instance, Ty};
20 use rustc_middle::{bug, span_bug};
21 use rustc_target::abi::{
22 AddressSpace, Align, HasDataLayout, Primitive, Scalar, Size, WrappingRange,
27 pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll Value {
28 let mut llvals = Vec::with_capacity(alloc.relocations().len() + 1);
29 let dl = cx.data_layout();
30 let pointer_size = dl.pointer_size.bytes() as usize;
32 // Note: this function may call `inspect_with_uninit_and_ptr_outside_interpreter`,
33 // so `range` must be within the bounds of `alloc` and not contain or overlap a relocation.
34 fn append_chunks_of_init_and_uninit_bytes<'ll, 'a, 'b>(
35 llvals: &mut Vec<&'ll Value>,
36 cx: &'a CodegenCx<'ll, 'b>,
37 alloc: &'a Allocation,
40 let mut chunks = alloc
42 .range_as_init_chunks(Size::from_bytes(range.start), Size::from_bytes(range.end));
44 let chunk_to_llval = move |chunk| match chunk {
45 InitChunk::Init(range) => {
46 let range = (range.start.bytes() as usize)..(range.end.bytes() as usize);
47 let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(range);
50 InitChunk::Uninit(range) => {
51 let len = range.end.bytes() - range.start.bytes();
52 cx.const_undef(cx.type_array(cx.type_i8(), len))
56 // Generating partially-uninit consts inhibits optimizations, so it is disabled by default.
57 // See https://github.com/rust-lang/rust/issues/84565.
58 let allow_partially_uninit =
59 match cx.sess().opts.debugging_opts.partially_uninit_const_threshold {
60 Some(max) => range.len() <= max,
64 if allow_partially_uninit {
65 llvals.extend(chunks.map(chunk_to_llval));
67 let llval = match (chunks.next(), chunks.next()) {
68 (Some(chunk), None) => {
69 // exactly one chunk, either fully init or fully uninit
73 // partially uninit, codegen as if it was initialized
74 // (using some arbitrary value for uninit bytes)
75 let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(range);
83 let mut next_offset = 0;
84 for &(offset, alloc_id) in alloc.relocations().iter() {
85 let offset = offset.bytes();
86 assert_eq!(offset as usize as u64, offset);
87 let offset = offset as usize;
88 if offset > next_offset {
89 // This `inspect` is okay since we have checked that it is not within a relocation, it
90 // is within the bounds of the allocation, and it doesn't affect interpreter execution
91 // (we inspect the result after interpreter execution).
92 append_chunks_of_init_and_uninit_bytes(&mut llvals, cx, alloc, next_offset..offset);
94 let ptr_offset = read_target_uint(
96 // This `inspect` is okay since it is within the bounds of the allocation, it doesn't
97 // affect interpreter execution (we inspect the result after interpreter execution),
98 // and we properly interpret the relocation as a relocation pointer offset.
99 alloc.inspect_with_uninit_and_ptr_outside_interpreter(offset..(offset + pointer_size)),
101 .expect("const_alloc_to_llvm: could not read relocation pointer")
104 let address_space = match cx.tcx.global_alloc(alloc_id) {
105 GlobalAlloc::Function(..) => cx.data_layout().instruction_address_space,
106 GlobalAlloc::Static(..) | GlobalAlloc::Memory(..) => AddressSpace::DATA,
109 llvals.push(cx.scalar_to_backend(
110 InterpScalar::from_pointer(
111 Pointer::new(alloc_id, Size::from_bytes(ptr_offset)),
114 Scalar { value: Primitive::Pointer, valid_range: WrappingRange { start: 0, end: !0 } },
115 cx.type_i8p_ext(address_space),
117 next_offset = offset + pointer_size;
119 if alloc.len() >= next_offset {
120 let range = next_offset..alloc.len();
121 // This `inspect` is okay since we have check that it is after all relocations, it is
122 // within the bounds of the allocation, and it doesn't affect interpreter execution (we
123 // inspect the result after interpreter execution).
124 append_chunks_of_init_and_uninit_bytes(&mut llvals, cx, alloc, range);
127 cx.const_struct(&llvals, true)
130 pub fn codegen_static_initializer(
131 cx: &CodegenCx<'ll, 'tcx>,
133 ) -> Result<(&'ll Value, &'tcx Allocation), ErrorHandled> {
134 let alloc = cx.tcx.eval_static_initializer(def_id)?;
135 Ok((const_alloc_to_llvm(cx, alloc), alloc))
138 fn set_global_alignment(cx: &CodegenCx<'ll, '_>, gv: &'ll Value, mut align: Align) {
139 // The target may require greater alignment for globals than the type does.
140 // Note: GCC and Clang also allow `__attribute__((aligned))` on variables,
141 // which can force it to be smaller. Rust doesn't support this yet.
142 if let Some(min) = cx.sess().target.min_global_align {
143 match Align::from_bits(min) {
144 Ok(min) => align = align.max(min),
146 cx.sess().err(&format!("invalid minimum global alignment: {}", err));
151 llvm::LLVMSetAlignment(gv, align.bytes() as u32);
155 fn check_and_apply_linkage(
156 cx: &CodegenCx<'ll, 'tcx>,
157 attrs: &CodegenFnAttrs,
162 let llty = cx.layout_of(ty).llvm_type(cx);
163 if let Some(linkage) = attrs.linkage {
164 debug!("get_static: sym={} linkage={:?}", sym, linkage);
166 // If this is a static with a linkage specified, then we need to handle
167 // it a little specially. The typesystem prevents things like &T and
168 // extern "C" fn() from being non-null, so we can't just declare a
169 // static and call it a day. Some linkages (like weak) will make it such
170 // that the static actually has a null value.
171 let llty2 = if let ty::RawPtr(ref mt) = ty.kind() {
172 cx.layout_of(mt.ty).llvm_type(cx)
174 cx.sess().span_fatal(
175 cx.tcx.def_span(span_def_id),
176 "must have type `*const T` or `*mut T` due to `#[linkage]` attribute",
180 // Declare a symbol `foo` with the desired linkage.
181 let g1 = cx.declare_global(sym, llty2);
182 llvm::LLVMRustSetLinkage(g1, base::linkage_to_llvm(linkage));
184 // Declare an internal global `extern_with_linkage_foo` which
185 // is initialized with the address of `foo`. If `foo` is
186 // discarded during linking (for example, if `foo` has weak
187 // linkage and there are no definitions), then
188 // `extern_with_linkage_foo` will instead be initialized to
190 let mut real_name = "_rust_extern_with_linkage_".to_string();
191 real_name.push_str(sym);
192 let g2 = cx.define_global(&real_name, llty).unwrap_or_else(|| {
193 cx.sess().span_fatal(
194 cx.tcx.def_span(span_def_id),
195 &format!("symbol `{}` is already defined", &sym),
198 llvm::LLVMRustSetLinkage(g2, llvm::Linkage::InternalLinkage);
199 llvm::LLVMSetInitializer(g2, g1);
203 // Generate an external declaration.
204 // FIXME(nagisa): investigate whether it can be changed into define_global
205 cx.declare_global(sym, llty)
209 pub fn ptrcast(val: &'ll Value, ty: &'ll Type) -> &'ll Value {
210 unsafe { llvm::LLVMConstPointerCast(val, ty) }
213 impl CodegenCx<'ll, 'tcx> {
214 crate fn const_bitcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
215 unsafe { llvm::LLVMConstBitCast(val, ty) }
218 crate fn static_addr_of_mut(
225 let gv = match kind {
226 Some(kind) if !self.tcx.sess.fewer_names() => {
227 let name = self.generate_local_symbol_name(kind);
228 let gv = self.define_global(&name[..], self.val_ty(cv)).unwrap_or_else(|| {
229 bug!("symbol `{}` is already defined", name);
231 llvm::LLVMRustSetLinkage(gv, llvm::Linkage::PrivateLinkage);
234 _ => self.define_private_global(self.val_ty(cv)),
236 llvm::LLVMSetInitializer(gv, cv);
237 set_global_alignment(self, gv, align);
238 llvm::SetUnnamedAddress(gv, llvm::UnnamedAddr::Global);
243 crate fn get_static(&self, def_id: DefId) -> &'ll Value {
244 let instance = Instance::mono(self.tcx, def_id);
245 if let Some(&g) = self.instances.borrow().get(&instance) {
249 let defined_in_current_codegen_unit =
250 self.codegen_unit.items().contains_key(&MonoItem::Static(def_id));
252 !defined_in_current_codegen_unit,
253 "consts::get_static() should always hit the cache for \
254 statics defined in the same CGU, but did not for `{:?}`",
258 let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
259 let sym = self.tcx.symbol_name(instance).name;
260 let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
262 debug!("get_static: sym={} instance={:?} fn_attrs={:?}", sym, instance, fn_attrs);
264 let g = if def_id.is_local() && !self.tcx.is_foreign_item(def_id) {
265 let llty = self.layout_of(ty).llvm_type(self);
266 if let Some(g) = self.get_declared_value(sym) {
267 if self.val_ty(g) != self.type_ptr_to(llty) {
268 span_bug!(self.tcx.def_span(def_id), "Conflicting types for static");
272 let g = self.declare_global(sym, llty);
274 if !self.tcx.is_reachable_non_generic(def_id) {
276 llvm::LLVMRustSetVisibility(g, llvm::Visibility::Hidden);
282 check_and_apply_linkage(self, fn_attrs, ty, sym, def_id)
285 // Thread-local statics in some other crate need to *always* be linked
286 // against in a thread-local fashion, so we need to be sure to apply the
287 // thread-local attribute locally if it was present remotely. If we
288 // don't do this then linker errors can be generated where the linker
289 // complains that one object files has a thread local version of the
290 // symbol and another one doesn't.
291 if fn_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
292 llvm::set_thread_local_mode(g, self.tls_model);
295 if !def_id.is_local() {
296 let needs_dll_storage_attr = self.use_dll_storage_attrs && !self.tcx.is_foreign_item(def_id) &&
297 // ThinLTO can't handle this workaround in all cases, so we don't
298 // emit the attrs. Instead we make them unnecessary by disallowing
299 // dynamic linking when linker plugin based LTO is enabled.
300 !self.tcx.sess.opts.cg.linker_plugin_lto.enabled();
302 // If this assertion triggers, there's something wrong with commandline
303 // argument validation.
305 !(self.tcx.sess.opts.cg.linker_plugin_lto.enabled()
306 && self.tcx.sess.target.is_like_windows
307 && self.tcx.sess.opts.cg.prefer_dynamic)
310 if needs_dll_storage_attr {
311 // This item is external but not foreign, i.e., it originates from an external Rust
312 // crate. Since we don't know whether this crate will be linked dynamically or
313 // statically in the final application, we always mark such symbols as 'dllimport'.
314 // If final linkage happens to be static, we rely on compiler-emitted __imp_ stubs
315 // to make things work.
317 // However, in some scenarios we defer emission of statics to downstream
318 // crates, so there are cases where a static with an upstream DefId
319 // is actually present in the current crate. We can find out via the
320 // is_codegened_item query.
321 if !self.tcx.is_codegened_item(def_id) {
323 llvm::LLVMSetDLLStorageClass(g, llvm::DLLStorageClass::DllImport);
329 if self.use_dll_storage_attrs && self.tcx.is_dllimport_foreign_item(def_id) {
330 // For foreign (native) libs we know the exact storage type to use.
332 llvm::LLVMSetDLLStorageClass(g, llvm::DLLStorageClass::DllImport);
337 if self.should_assume_dso_local(g, true) {
338 llvm::LLVMRustSetDSOLocal(g, true);
342 self.instances.borrow_mut().insert(instance, g);
347 impl StaticMethods for CodegenCx<'ll, 'tcx> {
348 fn static_addr_of(&self, cv: &'ll Value, align: Align, kind: Option<&str>) -> &'ll Value {
349 if let Some(&gv) = self.const_globals.borrow().get(&cv) {
351 // Upgrade the alignment in cases where the same constant is used with different
352 // alignment requirements
353 let llalign = align.bytes() as u32;
354 if llalign > llvm::LLVMGetAlignment(gv) {
355 llvm::LLVMSetAlignment(gv, llalign);
360 let gv = self.static_addr_of_mut(cv, align, kind);
362 llvm::LLVMSetGlobalConstant(gv, True);
364 self.const_globals.borrow_mut().insert(cv, gv);
368 fn codegen_static(&self, def_id: DefId, is_mutable: bool) {
370 let attrs = self.tcx.codegen_fn_attrs(def_id);
372 let (v, alloc) = match codegen_static_initializer(self, def_id) {
374 // Error has already been reported
378 let g = self.get_static(def_id);
380 // boolean SSA values are i1, but they have to be stored in i8 slots,
381 // otherwise some LLVM optimization passes don't work as expected
382 let mut val_llty = self.val_ty(v);
383 let v = if val_llty == self.type_i1() {
384 val_llty = self.type_i8();
385 llvm::LLVMConstZExt(v, val_llty)
390 let instance = Instance::mono(self.tcx, def_id);
391 let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
392 let llty = self.layout_of(ty).llvm_type(self);
393 let g = if val_llty == llty {
396 // If we created the global with the wrong type,
398 let name = llvm::get_value_name(g).to_vec();
399 llvm::set_value_name(g, b"");
401 let linkage = llvm::LLVMRustGetLinkage(g);
402 let visibility = llvm::LLVMRustGetVisibility(g);
404 let new_g = llvm::LLVMRustGetOrInsertGlobal(
406 name.as_ptr().cast(),
411 llvm::LLVMRustSetLinkage(new_g, linkage);
412 llvm::LLVMRustSetVisibility(new_g, visibility);
414 // To avoid breaking any invariants, we leave around the old
415 // global for the moment; we'll replace all references to it
416 // with the new global later. (See base::codegen_backend.)
417 self.statics_to_rauw.borrow_mut().push((g, new_g));
420 set_global_alignment(self, g, self.align_of(ty));
421 llvm::LLVMSetInitializer(g, v);
423 if self.should_assume_dso_local(g, true) {
424 llvm::LLVMRustSetDSOLocal(g, true);
427 // As an optimization, all shared statics which do not have interior
428 // mutability are placed into read-only memory.
429 if !is_mutable && self.type_is_freeze(ty) {
430 llvm::LLVMSetGlobalConstant(g, llvm::True);
433 debuginfo::create_global_var_metadata(self, def_id, g);
435 if attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
436 llvm::set_thread_local_mode(g, self.tls_model);
438 // Do not allow LLVM to change the alignment of a TLS on macOS.
440 // By default a global's alignment can be freely increased.
441 // This allows LLVM to generate more performant instructions
442 // e.g., using load-aligned into a SIMD register.
444 // However, on macOS 10.10 or below, the dynamic linker does not
445 // respect any alignment given on the TLS (radar 24221680).
446 // This will violate the alignment assumption, and causing segfault at runtime.
448 // This bug is very easy to trigger. In `println!` and `panic!`,
449 // the `LOCAL_STDOUT`/`LOCAL_STDERR` handles are stored in a TLS,
450 // which the values would be `mem::replace`d on initialization.
451 // The implementation of `mem::replace` will use SIMD
452 // whenever the size is 32 bytes or higher. LLVM notices SIMD is used
453 // and tries to align `LOCAL_STDOUT`/`LOCAL_STDERR` to a 32-byte boundary,
454 // which macOS's dyld disregarded and causing crashes
455 // (see issues #51794, #51758, #50867, #48866 and #44056).
457 // To workaround the bug, we trick LLVM into not increasing
458 // the global's alignment by explicitly assigning a section to it
459 // (equivalent to automatically generating a `#[link_section]` attribute).
460 // See the comment in the `GlobalValue::canIncreaseAlignment()` function
461 // of `lib/IR/Globals.cpp` for why this works.
463 // When the alignment is not increased, the optimized `mem::replace`
464 // will use load-unaligned instructions instead, and thus avoiding the crash.
466 // We could remove this hack whenever we decide to drop macOS 10.10 support.
467 if self.tcx.sess.target.is_like_osx {
468 // The `inspect` method is okay here because we checked relocations, and
469 // because we are doing this access to inspect the final interpreter state
470 // (not as part of the interpreter execution).
472 // FIXME: This check requires that the (arbitrary) value of undefined bytes
473 // happens to be zero. Instead, we should only check the value of defined bytes
474 // and set all undefined bytes to zero if this allocation is headed for the
476 let all_bytes_are_zero = alloc.relocations().is_empty()
478 .inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len())
480 .all(|&byte| byte == 0);
482 let sect_name = if all_bytes_are_zero {
483 cstr!("__DATA,__thread_bss")
485 cstr!("__DATA,__thread_data")
487 llvm::LLVMSetSection(g, sect_name.as_ptr());
491 // Wasm statics with custom link sections get special treatment as they
492 // go into custom sections of the wasm executable.
493 if self.tcx.sess.opts.target_triple.triple().starts_with("wasm32") {
494 if let Some(section) = attrs.link_section {
495 let section = llvm::LLVMMDStringInContext(
497 section.as_str().as_ptr().cast(),
498 section.as_str().len() as c_uint,
500 assert!(alloc.relocations().is_empty());
502 // The `inspect` method is okay here because we checked relocations, and
503 // because we are doing this access to inspect the final interpreter state (not
504 // as part of the interpreter execution).
506 alloc.inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len());
507 let alloc = llvm::LLVMMDStringInContext(
509 bytes.as_ptr().cast(),
510 bytes.len() as c_uint,
512 let data = [section, alloc];
513 let meta = llvm::LLVMMDNodeInContext(self.llcx, data.as_ptr(), 2);
514 llvm::LLVMAddNamedMetadataOperand(
516 "wasm.custom_sections\0".as_ptr().cast(),
521 base::set_link_section(g, attrs);
524 if attrs.flags.contains(CodegenFnAttrFlags::USED) {
525 // The semantics of #[used] in Rust only require the symbol to make it into the
526 // object file. It is explicitly allowed for the linker to strip the symbol if it
527 // is dead. As such, use llvm.compiler.used instead of llvm.used.
528 // Additionally, https://reviews.llvm.org/D97448 in LLVM 13 started emitting unique
529 // sections with SHF_GNU_RETAIN flag for llvm.used symbols, which may trigger bugs
530 // in some versions of the gold linker.
531 self.add_compiler_used_global(g);
536 /// Add a global value to a list to be stored in the `llvm.used` variable, an array of i8*.
537 fn add_used_global(&self, global: &'ll Value) {
538 let cast = unsafe { llvm::LLVMConstPointerCast(global, self.type_i8p()) };
539 self.used_statics.borrow_mut().push(cast);
542 /// Add a global value to a list to be stored in the `llvm.compiler.used` variable,
544 fn add_compiler_used_global(&self, global: &'ll Value) {
545 let cast = unsafe { llvm::LLVMConstPointerCast(global, self.type_i8p()) };
546 self.compiler_used_statics.borrow_mut().push(cast);