2 use crate::common::{self, CodegenCx};
4 use crate::llvm::{self, True};
6 use crate::type_::Type;
7 use crate::type_of::LayoutLlvmExt;
8 use crate::value::Value;
11 use rustc_codegen_ssa::traits::*;
12 use rustc_hir::def_id::DefId;
13 use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
14 use rustc_middle::mir::interpret::{
15 read_target_uint, Allocation, ConstAllocation, ErrorHandled, GlobalAlloc, InitChunk, Pointer,
16 Scalar as InterpScalar,
18 use rustc_middle::mir::mono::MonoItem;
19 use rustc_middle::ty::layout::LayoutOf;
20 use rustc_middle::ty::{self, Instance, Ty};
21 use rustc_middle::{bug, span_bug};
22 use rustc_target::abi::{
23 AddressSpace, Align, HasDataLayout, Primitive, Scalar, Size, WrappingRange,
27 pub fn const_alloc_to_llvm<'ll>(cx: &CodegenCx<'ll, '_>, alloc: ConstAllocation<'_>) -> &'ll Value {
28 let alloc = alloc.inner();
29 let mut llvals = Vec::with_capacity(alloc.provenance().len() + 1);
30 let dl = cx.data_layout();
31 let pointer_size = dl.pointer_size.bytes() as usize;
33 // Note: this function may call `inspect_with_uninit_and_ptr_outside_interpreter`, so `range`
34 // must be within the bounds of `alloc` and not contain or overlap a pointer provenance.
35 fn append_chunks_of_init_and_uninit_bytes<'ll, 'a, 'b>(
36 llvals: &mut Vec<&'ll Value>,
37 cx: &'a CodegenCx<'ll, 'b>,
38 alloc: &'a Allocation,
43 .range_as_init_chunks(Size::from_bytes(range.start), Size::from_bytes(range.end));
45 let chunk_to_llval = move |chunk| match chunk {
46 InitChunk::Init(range) => {
47 let range = (range.start.bytes() as usize)..(range.end.bytes() as usize);
48 let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(range);
51 InitChunk::Uninit(range) => {
52 let len = range.end.bytes() - range.start.bytes();
53 cx.const_undef(cx.type_array(cx.type_i8(), len))
57 // Generating partially-uninit consts is limited to small numbers of chunks,
58 // to avoid the cost of generating large complex const expressions.
59 // For example, `[(u32, u8); 1024 * 1024]` contains uninit padding in each element,
60 // and would result in `{ [5 x i8] zeroinitializer, [3 x i8] undef, ...repeat 1M times... }`.
61 let max = if llvm_util::get_version() < (14, 0, 0) {
62 // Generating partially-uninit consts inhibits optimizations in LLVM < 14.
63 // See https://github.com/rust-lang/rust/issues/84565.
66 cx.sess().opts.unstable_opts.uninit_const_chunk_threshold
68 let allow_uninit_chunks = chunks.clone().take(max.saturating_add(1)).count() <= max;
70 if allow_uninit_chunks {
71 llvals.extend(chunks.map(chunk_to_llval));
73 // If this allocation contains any uninit bytes, 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);
76 llvals.push(cx.const_bytes(bytes));
80 let mut next_offset = 0;
81 for &(offset, alloc_id) in alloc.provenance().iter() {
82 let offset = offset.bytes();
83 assert_eq!(offset as usize as u64, offset);
84 let offset = offset as usize;
85 if offset > next_offset {
86 // This `inspect` is okay since we have checked that there is no provenance, it
87 // is within the bounds of the allocation, and it doesn't affect interpreter execution
88 // (we inspect the result after interpreter execution).
89 append_chunks_of_init_and_uninit_bytes(&mut llvals, cx, alloc, next_offset..offset);
91 let ptr_offset = read_target_uint(
93 // This `inspect` is okay since it is within the bounds of the allocation, it doesn't
94 // affect interpreter execution (we inspect the result after interpreter execution),
95 // and we properly interpret the provenance as a relocation pointer offset.
96 alloc.inspect_with_uninit_and_ptr_outside_interpreter(offset..(offset + pointer_size)),
98 .expect("const_alloc_to_llvm: could not read relocation pointer")
101 let address_space = match cx.tcx.global_alloc(alloc_id) {
102 GlobalAlloc::Function(..) => cx.data_layout().instruction_address_space,
103 GlobalAlloc::Static(..) | GlobalAlloc::Memory(..) | GlobalAlloc::VTable(..) => {
108 llvals.push(cx.scalar_to_backend(
109 InterpScalar::from_pointer(
110 Pointer::new(alloc_id, Size::from_bytes(ptr_offset)),
113 Scalar::Initialized {
114 value: Primitive::Pointer,
115 valid_range: WrappingRange::full(dl.pointer_size),
117 cx.type_i8p_ext(address_space),
119 next_offset = offset + pointer_size;
121 if alloc.len() >= next_offset {
122 let range = next_offset..alloc.len();
123 // This `inspect` is okay since we have check that it is after all provenance, it is
124 // within the bounds of the allocation, and it doesn't affect interpreter execution (we
125 // inspect the result after interpreter execution).
126 append_chunks_of_init_and_uninit_bytes(&mut llvals, cx, alloc, range);
129 cx.const_struct(&llvals, true)
132 pub fn codegen_static_initializer<'ll, 'tcx>(
133 cx: &CodegenCx<'ll, 'tcx>,
135 ) -> Result<(&'ll Value, ConstAllocation<'tcx>), ErrorHandled> {
136 let alloc = cx.tcx.eval_static_initializer(def_id)?;
137 Ok((const_alloc_to_llvm(cx, alloc), alloc))
140 fn set_global_alignment<'ll>(cx: &CodegenCx<'ll, '_>, gv: &'ll Value, mut align: Align) {
141 // The target may require greater alignment for globals than the type does.
142 // Note: GCC and Clang also allow `__attribute__((aligned))` on variables,
143 // which can force it to be smaller. Rust doesn't support this yet.
144 if let Some(min) = cx.sess().target.min_global_align {
145 match Align::from_bits(min) {
146 Ok(min) => align = align.max(min),
148 cx.sess().err(&format!("invalid minimum global alignment: {}", err));
153 llvm::LLVMSetAlignment(gv, align.bytes() as u32);
157 fn check_and_apply_linkage<'ll, 'tcx>(
158 cx: &CodegenCx<'ll, 'tcx>,
159 attrs: &CodegenFnAttrs,
164 let llty = cx.layout_of(ty).llvm_type(cx);
165 if let Some(linkage) = attrs.linkage {
166 debug!("get_static: sym={} linkage={:?}", sym, linkage);
168 // If this is a static with a linkage specified, then we need to handle
169 // it a little specially. The typesystem prevents things like &T and
170 // extern "C" fn() from being non-null, so we can't just declare a
171 // static and call it a day. Some linkages (like weak) will make it such
172 // that the static actually has a null value.
173 let llty2 = if let ty::RawPtr(ref mt) = ty.kind() {
174 cx.layout_of(mt.ty).llvm_type(cx)
176 cx.sess().span_fatal(
177 cx.tcx.def_span(def_id),
178 "must have type `*const T` or `*mut T` due to `#[linkage]` attribute",
182 // Declare a symbol `foo` with the desired linkage.
183 let g1 = cx.declare_global(sym, llty2);
184 llvm::LLVMRustSetLinkage(g1, base::linkage_to_llvm(linkage));
186 // Declare an internal global `extern_with_linkage_foo` which
187 // is initialized with the address of `foo`. If `foo` is
188 // discarded during linking (for example, if `foo` has weak
189 // linkage and there are no definitions), then
190 // `extern_with_linkage_foo` will instead be initialized to
192 let mut real_name = "_rust_extern_with_linkage_".to_string();
193 real_name.push_str(sym);
194 let g2 = cx.define_global(&real_name, llty).unwrap_or_else(|| {
195 cx.sess().span_fatal(
196 cx.tcx.def_span(def_id),
197 &format!("symbol `{}` is already defined", &sym),
200 llvm::LLVMRustSetLinkage(g2, llvm::Linkage::InternalLinkage);
201 llvm::LLVMSetInitializer(g2, g1);
204 } else if cx.tcx.sess.target.arch == "x86" &&
205 let Some(dllimport) = common::get_dllimport(cx.tcx, def_id, sym)
207 cx.declare_global(&common::i686_decorated_name(&dllimport, common::is_mingw_gnu_toolchain(&cx.tcx.sess.target), true), llty)
209 // Generate an external declaration.
210 // FIXME(nagisa): investigate whether it can be changed into define_global
211 cx.declare_global(sym, llty)
215 pub fn ptrcast<'ll>(val: &'ll Value, ty: &'ll Type) -> &'ll Value {
216 unsafe { llvm::LLVMConstPointerCast(val, ty) }
219 impl<'ll> CodegenCx<'ll, '_> {
220 pub(crate) fn const_bitcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
221 unsafe { llvm::LLVMConstBitCast(val, ty) }
224 pub(crate) fn static_addr_of_mut(
231 let gv = match kind {
232 Some(kind) if !self.tcx.sess.fewer_names() => {
233 let name = self.generate_local_symbol_name(kind);
234 let gv = self.define_global(&name, self.val_ty(cv)).unwrap_or_else(|| {
235 bug!("symbol `{}` is already defined", name);
237 llvm::LLVMRustSetLinkage(gv, llvm::Linkage::PrivateLinkage);
240 _ => self.define_private_global(self.val_ty(cv)),
242 llvm::LLVMSetInitializer(gv, cv);
243 set_global_alignment(self, gv, align);
244 llvm::SetUnnamedAddress(gv, llvm::UnnamedAddr::Global);
249 pub(crate) fn get_static(&self, def_id: DefId) -> &'ll Value {
250 let instance = Instance::mono(self.tcx, def_id);
251 if let Some(&g) = self.instances.borrow().get(&instance) {
255 let defined_in_current_codegen_unit =
256 self.codegen_unit.items().contains_key(&MonoItem::Static(def_id));
258 !defined_in_current_codegen_unit,
259 "consts::get_static() should always hit the cache for \
260 statics defined in the same CGU, but did not for `{:?}`",
264 let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
265 let sym = self.tcx.symbol_name(instance).name;
266 let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
268 debug!("get_static: sym={} instance={:?} fn_attrs={:?}", sym, instance, fn_attrs);
270 let g = if def_id.is_local() && !self.tcx.is_foreign_item(def_id) {
271 let llty = self.layout_of(ty).llvm_type(self);
272 if let Some(g) = self.get_declared_value(sym) {
273 if self.val_ty(g) != self.type_ptr_to(llty) {
274 span_bug!(self.tcx.def_span(def_id), "Conflicting types for static");
278 let g = self.declare_global(sym, llty);
280 if !self.tcx.is_reachable_non_generic(def_id) {
282 llvm::LLVMRustSetVisibility(g, llvm::Visibility::Hidden);
288 check_and_apply_linkage(self, fn_attrs, ty, sym, def_id)
291 // Thread-local statics in some other crate need to *always* be linked
292 // against in a thread-local fashion, so we need to be sure to apply the
293 // thread-local attribute locally if it was present remotely. If we
294 // don't do this then linker errors can be generated where the linker
295 // complains that one object files has a thread local version of the
296 // symbol and another one doesn't.
297 if fn_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
298 llvm::set_thread_local_mode(g, self.tls_model);
301 if !def_id.is_local() {
302 let needs_dll_storage_attr = self.use_dll_storage_attrs && !self.tcx.is_foreign_item(def_id) &&
303 // ThinLTO can't handle this workaround in all cases, so we don't
304 // emit the attrs. Instead we make them unnecessary by disallowing
305 // dynamic linking when linker plugin based LTO is enabled.
306 !self.tcx.sess.opts.cg.linker_plugin_lto.enabled();
308 // If this assertion triggers, there's something wrong with commandline
309 // argument validation.
311 !(self.tcx.sess.opts.cg.linker_plugin_lto.enabled()
312 && self.tcx.sess.target.is_like_windows
313 && self.tcx.sess.opts.cg.prefer_dynamic)
316 if needs_dll_storage_attr {
317 // This item is external but not foreign, i.e., it originates from an external Rust
318 // crate. Since we don't know whether this crate will be linked dynamically or
319 // statically in the final application, we always mark such symbols as 'dllimport'.
320 // If final linkage happens to be static, we rely on compiler-emitted __imp_ stubs
321 // to make things work.
323 // However, in some scenarios we defer emission of statics to downstream
324 // crates, so there are cases where a static with an upstream DefId
325 // is actually present in the current crate. We can find out via the
326 // is_codegened_item query.
327 if !self.tcx.is_codegened_item(def_id) {
329 llvm::LLVMSetDLLStorageClass(g, llvm::DLLStorageClass::DllImport);
335 if self.use_dll_storage_attrs
336 && let Some(library) = self.tcx.native_library(def_id)
337 && library.kind.is_dllimport()
339 // For foreign (native) libs we know the exact storage type to use.
341 llvm::LLVMSetDLLStorageClass(g, llvm::DLLStorageClass::DllImport);
346 if self.should_assume_dso_local(g, true) {
347 llvm::LLVMRustSetDSOLocal(g, true);
351 self.instances.borrow_mut().insert(instance, g);
356 impl<'ll> StaticMethods for CodegenCx<'ll, '_> {
357 fn static_addr_of(&self, cv: &'ll Value, align: Align, kind: Option<&str>) -> &'ll Value {
358 if let Some(&gv) = self.const_globals.borrow().get(&cv) {
360 // Upgrade the alignment in cases where the same constant is used with different
361 // alignment requirements
362 let llalign = align.bytes() as u32;
363 if llalign > llvm::LLVMGetAlignment(gv) {
364 llvm::LLVMSetAlignment(gv, llalign);
369 let gv = self.static_addr_of_mut(cv, align, kind);
371 llvm::LLVMSetGlobalConstant(gv, True);
373 self.const_globals.borrow_mut().insert(cv, gv);
377 fn codegen_static(&self, def_id: DefId, is_mutable: bool) {
379 let attrs = self.tcx.codegen_fn_attrs(def_id);
381 let Ok((v, alloc)) = codegen_static_initializer(self, def_id) else {
382 // Error has already been reported
385 let alloc = alloc.inner();
387 let g = self.get_static(def_id);
389 // boolean SSA values are i1, but they have to be stored in i8 slots,
390 // otherwise some LLVM optimization passes don't work as expected
391 let mut val_llty = self.val_ty(v);
392 let v = if val_llty == self.type_i1() {
393 val_llty = self.type_i8();
394 llvm::LLVMConstZExt(v, val_llty)
399 let instance = Instance::mono(self.tcx, def_id);
400 let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
401 let llty = self.layout_of(ty).llvm_type(self);
402 let g = if val_llty == llty {
405 // If we created the global with the wrong type,
407 let name = llvm::get_value_name(g).to_vec();
408 llvm::set_value_name(g, b"");
410 let linkage = llvm::LLVMRustGetLinkage(g);
411 let visibility = llvm::LLVMRustGetVisibility(g);
413 let new_g = llvm::LLVMRustGetOrInsertGlobal(
415 name.as_ptr().cast(),
420 llvm::LLVMRustSetLinkage(new_g, linkage);
421 llvm::LLVMRustSetVisibility(new_g, visibility);
423 // The old global has had its name removed but is returned by
424 // get_static since it is in the instance cache. Provide an
425 // alternative lookup that points to the new global so that
426 // global_asm! can compute the correct mangled symbol name
428 self.renamed_statics.borrow_mut().insert(def_id, new_g);
430 // To avoid breaking any invariants, we leave around the old
431 // global for the moment; we'll replace all references to it
432 // with the new global later. (See base::codegen_backend.)
433 self.statics_to_rauw.borrow_mut().push((g, new_g));
436 set_global_alignment(self, g, self.align_of(ty));
437 llvm::LLVMSetInitializer(g, v);
439 if self.should_assume_dso_local(g, true) {
440 llvm::LLVMRustSetDSOLocal(g, true);
443 // As an optimization, all shared statics which do not have interior
444 // mutability are placed into read-only memory.
445 if !is_mutable && self.type_is_freeze(ty) {
446 llvm::LLVMSetGlobalConstant(g, llvm::True);
449 debuginfo::build_global_var_di_node(self, def_id, g);
451 if attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
452 llvm::set_thread_local_mode(g, self.tls_model);
454 // Do not allow LLVM to change the alignment of a TLS on macOS.
456 // By default a global's alignment can be freely increased.
457 // This allows LLVM to generate more performant instructions
458 // e.g., using load-aligned into a SIMD register.
460 // However, on macOS 10.10 or below, the dynamic linker does not
461 // respect any alignment given on the TLS (radar 24221680).
462 // This will violate the alignment assumption, and causing segfault at runtime.
464 // This bug is very easy to trigger. In `println!` and `panic!`,
465 // the `LOCAL_STDOUT`/`LOCAL_STDERR` handles are stored in a TLS,
466 // which the values would be `mem::replace`d on initialization.
467 // The implementation of `mem::replace` will use SIMD
468 // whenever the size is 32 bytes or higher. LLVM notices SIMD is used
469 // and tries to align `LOCAL_STDOUT`/`LOCAL_STDERR` to a 32-byte boundary,
470 // which macOS's dyld disregarded and causing crashes
471 // (see issues #51794, #51758, #50867, #48866 and #44056).
473 // To workaround the bug, we trick LLVM into not increasing
474 // the global's alignment by explicitly assigning a section to it
475 // (equivalent to automatically generating a `#[link_section]` attribute).
476 // See the comment in the `GlobalValue::canIncreaseAlignment()` function
477 // of `lib/IR/Globals.cpp` for why this works.
479 // When the alignment is not increased, the optimized `mem::replace`
480 // will use load-unaligned instructions instead, and thus avoiding the crash.
482 // We could remove this hack whenever we decide to drop macOS 10.10 support.
483 if self.tcx.sess.target.is_like_osx {
484 // The `inspect` method is okay here because we checked for provenance, and
485 // because we are doing this access to inspect the final interpreter state
486 // (not as part of the interpreter execution).
488 // FIXME: This check requires that the (arbitrary) value of undefined bytes
489 // happens to be zero. Instead, we should only check the value of defined bytes
490 // and set all undefined bytes to zero if this allocation is headed for the
492 let all_bytes_are_zero = alloc.provenance().is_empty()
494 .inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len())
496 .all(|&byte| byte == 0);
498 let sect_name = if all_bytes_are_zero {
499 cstr!("__DATA,__thread_bss")
501 cstr!("__DATA,__thread_data")
503 llvm::LLVMSetSection(g, sect_name.as_ptr());
507 // Wasm statics with custom link sections get special treatment as they
508 // go into custom sections of the wasm executable.
509 if self.tcx.sess.target.is_like_wasm {
510 if let Some(section) = attrs.link_section {
511 let section = llvm::LLVMMDStringInContext(
513 section.as_str().as_ptr().cast(),
514 section.as_str().len() as c_uint,
516 assert!(alloc.provenance().is_empty());
518 // The `inspect` method is okay here because we checked for provenance, and
519 // because we are doing this access to inspect the final interpreter state (not
520 // as part of the interpreter execution).
522 alloc.inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len());
523 let alloc = llvm::LLVMMDStringInContext(
525 bytes.as_ptr().cast(),
526 bytes.len() as c_uint,
528 let data = [section, alloc];
529 let meta = llvm::LLVMMDNodeInContext(self.llcx, data.as_ptr(), 2);
530 llvm::LLVMAddNamedMetadataOperand(
532 "wasm.custom_sections\0".as_ptr().cast(),
537 base::set_link_section(g, attrs);
540 if attrs.flags.contains(CodegenFnAttrFlags::USED) {
541 // `USED` and `USED_LINKER` can't be used together.
542 assert!(!attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER));
544 // The semantics of #[used] in Rust only require the symbol to make it into the
545 // object file. It is explicitly allowed for the linker to strip the symbol if it
546 // is dead, which means we are allowed use `llvm.compiler.used` instead of
549 // Additionally, https://reviews.llvm.org/D97448 in LLVM 13 started emitting unique
550 // sections with SHF_GNU_RETAIN flag for llvm.used symbols, which may trigger bugs
551 // in the handling of `.init_array` (the static constructor list) in versions of
552 // the gold linker (prior to the one released with binutils 2.36).
554 // That said, we only ever emit these when compiling for ELF targets, unless
555 // `#[used(compiler)]` is explicitly requested. This is to avoid similar breakage
556 // on other targets, in particular MachO targets have *their* static constructor
557 // lists broken if `llvm.compiler.used` is emitted rather than llvm.used. However,
558 // that check happens when assigning the `CodegenFnAttrFlags` in `rustc_hir_analysis`,
559 // so we don't need to take care of it here.
560 self.add_compiler_used_global(g);
562 if attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER) {
563 // `USED` and `USED_LINKER` can't be used together.
564 assert!(!attrs.flags.contains(CodegenFnAttrFlags::USED));
566 self.add_used_global(g);
571 /// Add a global value to a list to be stored in the `llvm.used` variable, an array of i8*.
572 fn add_used_global(&self, global: &'ll Value) {
573 let cast = unsafe { llvm::LLVMConstPointerCast(global, self.type_i8p()) };
574 self.used_statics.borrow_mut().push(cast);
577 /// Add a global value to a list to be stored in the `llvm.compiler.used` variable,
579 fn add_compiler_used_global(&self, global: &'ll Value) {
580 let cast = unsafe { llvm::LLVMConstPointerCast(global, self.type_i8p()) };
581 self.compiler_used_statics.borrow_mut().push(cast);