rustc_codegen_llvm/
consts.rs

1use std::ops::Range;
2
3use rustc_abi::{
4    Align, AlignFromBytesError, HasDataLayout, Primitive, Scalar, Size, WrappingRange,
5};
6use rustc_codegen_ssa::common;
7use rustc_codegen_ssa::traits::*;
8use rustc_hir::def::DefKind;
9use rustc_hir::def_id::DefId;
10use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
11use rustc_middle::mir::interpret::{
12    Allocation, ConstAllocation, ErrorHandled, InitChunk, Pointer, Scalar as InterpScalar,
13    read_target_uint,
14};
15use rustc_middle::mir::mono::MonoItem;
16use rustc_middle::ty::Instance;
17use rustc_middle::ty::layout::{HasTypingEnv, LayoutOf};
18use rustc_middle::{bug, span_bug};
19use rustc_session::config::Lto;
20use tracing::{debug, instrument, trace};
21
22use crate::common::{AsCCharPtr, CodegenCx};
23use crate::errors::{
24    InvalidMinimumAlignmentNotPowerOfTwo, InvalidMinimumAlignmentTooLarge, SymbolAlreadyDefined,
25};
26use crate::llvm::{self, True};
27use crate::type_::Type;
28use crate::type_of::LayoutLlvmExt;
29use crate::value::Value;
30use crate::{base, debuginfo};
31
32pub(crate) fn const_alloc_to_llvm<'ll>(
33    cx: &CodegenCx<'ll, '_>,
34    alloc: ConstAllocation<'_>,
35    is_static: bool,
36) -> &'ll Value {
37    let alloc = alloc.inner();
38    // We expect that callers of const_alloc_to_llvm will instead directly codegen a pointer or
39    // integer for any &ZST where the ZST is a constant (i.e. not a static). We should never be
40    // producing empty LLVM allocations as they're just adding noise to binaries and forcing less
41    // optimal codegen.
42    //
43    // Statics have a guaranteed meaningful address so it's less clear that we want to do
44    // something like this; it's also harder.
45    if !is_static {
46        assert!(alloc.len() != 0);
47    }
48    let mut llvals = Vec::with_capacity(alloc.provenance().ptrs().len() + 1);
49    let dl = cx.data_layout();
50    let pointer_size = dl.pointer_size.bytes() as usize;
51
52    // Note: this function may call `inspect_with_uninit_and_ptr_outside_interpreter`, so `range`
53    // must be within the bounds of `alloc` and not contain or overlap a pointer provenance.
54    fn append_chunks_of_init_and_uninit_bytes<'ll, 'a, 'b>(
55        llvals: &mut Vec<&'ll Value>,
56        cx: &'a CodegenCx<'ll, 'b>,
57        alloc: &'a Allocation,
58        range: Range<usize>,
59    ) {
60        let chunks = alloc.init_mask().range_as_init_chunks(range.clone().into());
61
62        let chunk_to_llval = move |chunk| match chunk {
63            InitChunk::Init(range) => {
64                let range = (range.start.bytes() as usize)..(range.end.bytes() as usize);
65                let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(range);
66                cx.const_bytes(bytes)
67            }
68            InitChunk::Uninit(range) => {
69                let len = range.end.bytes() - range.start.bytes();
70                cx.const_undef(cx.type_array(cx.type_i8(), len))
71            }
72        };
73
74        // Generating partially-uninit consts is limited to small numbers of chunks,
75        // to avoid the cost of generating large complex const expressions.
76        // For example, `[(u32, u8); 1024 * 1024]` contains uninit padding in each element, and
77        // would result in `{ [5 x i8] zeroinitializer, [3 x i8] undef, ...repeat 1M times... }`.
78        let max = cx.sess().opts.unstable_opts.uninit_const_chunk_threshold;
79        let allow_uninit_chunks = chunks.clone().take(max.saturating_add(1)).count() <= max;
80
81        if allow_uninit_chunks {
82            llvals.extend(chunks.map(chunk_to_llval));
83        } else {
84            // If this allocation contains any uninit bytes, codegen as if it was initialized
85            // (using some arbitrary value for uninit bytes).
86            let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(range);
87            llvals.push(cx.const_bytes(bytes));
88        }
89    }
90
91    let mut next_offset = 0;
92    for &(offset, prov) in alloc.provenance().ptrs().iter() {
93        let offset = offset.bytes();
94        assert_eq!(offset as usize as u64, offset);
95        let offset = offset as usize;
96        if offset > next_offset {
97            // This `inspect` is okay since we have checked that there is no provenance, it
98            // is within the bounds of the allocation, and it doesn't affect interpreter execution
99            // (we inspect the result after interpreter execution).
100            append_chunks_of_init_and_uninit_bytes(&mut llvals, cx, alloc, next_offset..offset);
101        }
102        let ptr_offset = read_target_uint(
103            dl.endian,
104            // This `inspect` is okay since it is within the bounds of the allocation, it doesn't
105            // affect interpreter execution (we inspect the result after interpreter execution),
106            // and we properly interpret the provenance as a relocation pointer offset.
107            alloc.inspect_with_uninit_and_ptr_outside_interpreter(offset..(offset + pointer_size)),
108        )
109        .expect("const_alloc_to_llvm: could not read relocation pointer")
110            as u64;
111
112        let address_space = cx.tcx.global_alloc(prov.alloc_id()).address_space(cx);
113
114        llvals.push(cx.scalar_to_backend(
115            InterpScalar::from_pointer(Pointer::new(prov, Size::from_bytes(ptr_offset)), &cx.tcx),
116            Scalar::Initialized {
117                value: Primitive::Pointer(address_space),
118                valid_range: WrappingRange::full(dl.pointer_size),
119            },
120            cx.type_ptr_ext(address_space),
121        ));
122        next_offset = offset + pointer_size;
123    }
124    if alloc.len() >= next_offset {
125        let range = next_offset..alloc.len();
126        // This `inspect` is okay since we have check that it is after all provenance, it is
127        // within the bounds of the allocation, and it doesn't affect interpreter execution (we
128        // inspect the result after interpreter execution).
129        append_chunks_of_init_and_uninit_bytes(&mut llvals, cx, alloc, range);
130    }
131
132    cx.const_struct(&llvals, true)
133}
134
135fn codegen_static_initializer<'ll, 'tcx>(
136    cx: &CodegenCx<'ll, 'tcx>,
137    def_id: DefId,
138) -> Result<(&'ll Value, ConstAllocation<'tcx>), ErrorHandled> {
139    let alloc = cx.tcx.eval_static_initializer(def_id)?;
140    Ok((const_alloc_to_llvm(cx, alloc, /*static*/ true), alloc))
141}
142
143fn set_global_alignment<'ll>(cx: &CodegenCx<'ll, '_>, gv: &'ll Value, mut align: Align) {
144    // The target may require greater alignment for globals than the type does.
145    // Note: GCC and Clang also allow `__attribute__((aligned))` on variables,
146    // which can force it to be smaller. Rust doesn't support this yet.
147    if let Some(min) = cx.sess().target.min_global_align {
148        match Align::from_bits(min) {
149            Ok(min) => align = align.max(min),
150            Err(err) => match err {
151                AlignFromBytesError::NotPowerOfTwo(align) => {
152                    cx.sess().dcx().emit_err(InvalidMinimumAlignmentNotPowerOfTwo { align });
153                }
154                AlignFromBytesError::TooLarge(align) => {
155                    cx.sess().dcx().emit_err(InvalidMinimumAlignmentTooLarge { align });
156                }
157            },
158        }
159    }
160    unsafe {
161        llvm::LLVMSetAlignment(gv, align.bytes() as u32);
162    }
163}
164
165fn check_and_apply_linkage<'ll, 'tcx>(
166    cx: &CodegenCx<'ll, 'tcx>,
167    attrs: &CodegenFnAttrs,
168    llty: &'ll Type,
169    sym: &str,
170    def_id: DefId,
171) -> &'ll Value {
172    if let Some(linkage) = attrs.import_linkage {
173        debug!("get_static: sym={} linkage={:?}", sym, linkage);
174
175        // Declare a symbol `foo` with the desired linkage.
176        let g1 = cx.declare_global(sym, cx.type_i8());
177        llvm::set_linkage(g1, base::linkage_to_llvm(linkage));
178
179        // Declare an internal global `extern_with_linkage_foo` which
180        // is initialized with the address of `foo`. If `foo` is
181        // discarded during linking (for example, if `foo` has weak
182        // linkage and there are no definitions), then
183        // `extern_with_linkage_foo` will instead be initialized to
184        // zero.
185        let mut real_name = "_rust_extern_with_linkage_".to_string();
186        real_name.push_str(sym);
187        let g2 = cx.define_global(&real_name, llty).unwrap_or_else(|| {
188            cx.sess().dcx().emit_fatal(SymbolAlreadyDefined {
189                span: cx.tcx.def_span(def_id),
190                symbol_name: sym,
191            })
192        });
193        llvm::set_linkage(g2, llvm::Linkage::InternalLinkage);
194        llvm::set_initializer(g2, g1);
195        g2
196    } else if cx.tcx.sess.target.arch == "x86"
197        && common::is_mingw_gnu_toolchain(&cx.tcx.sess.target)
198        && let Some(dllimport) = crate::common::get_dllimport(cx.tcx, def_id, sym)
199    {
200        cx.declare_global(&common::i686_decorated_name(dllimport, true, true, false), llty)
201    } else {
202        // Generate an external declaration.
203        // FIXME(nagisa): investigate whether it can be changed into define_global
204        cx.declare_global(sym, llty)
205    }
206}
207
208impl<'ll> CodegenCx<'ll, '_> {
209    pub(crate) fn const_bitcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
210        unsafe { llvm::LLVMConstBitCast(val, ty) }
211    }
212
213    pub(crate) fn const_pointercast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
214        unsafe { llvm::LLVMConstPointerCast(val, ty) }
215    }
216
217    /// Create a global variable.
218    ///
219    /// The returned global variable is a pointer in the default address space for globals.
220    /// Fails if a symbol with the given name already exists.
221    pub(crate) fn static_addr_of_mut(
222        &self,
223        cv: &'ll Value,
224        align: Align,
225        kind: Option<&str>,
226    ) -> &'ll Value {
227        let gv = match kind {
228            Some(kind) if !self.tcx.sess.fewer_names() => {
229                let name = self.generate_local_symbol_name(kind);
230                let gv = self.define_global(&name, self.val_ty(cv)).unwrap_or_else(|| {
231                    bug!("symbol `{}` is already defined", name);
232                });
233                llvm::set_linkage(gv, llvm::Linkage::PrivateLinkage);
234                gv
235            }
236            _ => self.define_private_global(self.val_ty(cv)),
237        };
238        llvm::set_initializer(gv, cv);
239        set_global_alignment(self, gv, align);
240        llvm::SetUnnamedAddress(gv, llvm::UnnamedAddr::Global);
241        gv
242    }
243
244    /// Create a global constant.
245    ///
246    /// The returned global variable is a pointer in the default address space for globals.
247    pub(crate) fn static_addr_of_impl(
248        &self,
249        cv: &'ll Value,
250        align: Align,
251        kind: Option<&str>,
252    ) -> &'ll Value {
253        if let Some(&gv) = self.const_globals.borrow().get(&cv) {
254            unsafe {
255                // Upgrade the alignment in cases where the same constant is used with different
256                // alignment requirements
257                let llalign = align.bytes() as u32;
258                if llalign > llvm::LLVMGetAlignment(gv) {
259                    llvm::LLVMSetAlignment(gv, llalign);
260                }
261            }
262            return gv;
263        }
264        let gv = self.static_addr_of_mut(cv, align, kind);
265        unsafe {
266            llvm::LLVMSetGlobalConstant(gv, True);
267        }
268        self.const_globals.borrow_mut().insert(cv, gv);
269        gv
270    }
271
272    #[instrument(level = "debug", skip(self))]
273    pub(crate) fn get_static(&self, def_id: DefId) -> &'ll Value {
274        let instance = Instance::mono(self.tcx, def_id);
275        trace!(?instance);
276
277        let DefKind::Static { nested, .. } = self.tcx.def_kind(def_id) else { bug!() };
278        // Nested statics do not have a type, so pick a dummy type and let `codegen_static` figure
279        // out the llvm type from the actual evaluated initializer.
280        let llty = if nested {
281            self.type_i8()
282        } else {
283            let ty = instance.ty(self.tcx, self.typing_env());
284            trace!(?ty);
285            self.layout_of(ty).llvm_type(self)
286        };
287        self.get_static_inner(def_id, llty)
288    }
289
290    #[instrument(level = "debug", skip(self, llty))]
291    fn get_static_inner(&self, def_id: DefId, llty: &'ll Type) -> &'ll Value {
292        let instance = Instance::mono(self.tcx, def_id);
293        if let Some(&g) = self.instances.borrow().get(&instance) {
294            trace!("used cached value");
295            return g;
296        }
297
298        let defined_in_current_codegen_unit =
299            self.codegen_unit.items().contains_key(&MonoItem::Static(def_id));
300        assert!(
301            !defined_in_current_codegen_unit,
302            "consts::get_static() should always hit the cache for \
303                 statics defined in the same CGU, but did not for `{def_id:?}`"
304        );
305
306        let sym = self.tcx.symbol_name(instance).name;
307        let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
308
309        debug!(?sym, ?fn_attrs);
310
311        let g = if def_id.is_local() && !self.tcx.is_foreign_item(def_id) {
312            if let Some(g) = self.get_declared_value(sym) {
313                if self.val_ty(g) != self.type_ptr() {
314                    span_bug!(self.tcx.def_span(def_id), "Conflicting types for static");
315                }
316            }
317
318            let g = self.declare_global(sym, llty);
319
320            if !self.tcx.is_reachable_non_generic(def_id) {
321                llvm::set_visibility(g, llvm::Visibility::Hidden);
322            }
323
324            g
325        } else {
326            check_and_apply_linkage(self, fn_attrs, llty, sym, def_id)
327        };
328
329        // Thread-local statics in some other crate need to *always* be linked
330        // against in a thread-local fashion, so we need to be sure to apply the
331        // thread-local attribute locally if it was present remotely. If we
332        // don't do this then linker errors can be generated where the linker
333        // complains that one object files has a thread local version of the
334        // symbol and another one doesn't.
335        if fn_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
336            llvm::set_thread_local_mode(g, self.tls_model);
337        }
338
339        let dso_local = self.should_assume_dso_local(g, true);
340        if dso_local {
341            unsafe {
342                llvm::LLVMRustSetDSOLocal(g, true);
343            }
344        }
345
346        if !def_id.is_local() {
347            let needs_dll_storage_attr = self.use_dll_storage_attrs
348                && !self.tcx.is_foreign_item(def_id)
349                // Local definitions can never be imported, so we must not apply
350                // the DLLImport annotation.
351                && !dso_local
352                // ThinLTO can't handle this workaround in all cases, so we don't
353                // emit the attrs. Instead we make them unnecessary by disallowing
354                // dynamic linking when linker plugin based LTO is enabled.
355                && !self.tcx.sess.opts.cg.linker_plugin_lto.enabled()
356                && self.tcx.sess.lto() != Lto::Thin;
357
358            // If this assertion triggers, there's something wrong with commandline
359            // argument validation.
360            assert!(
361                !(self.tcx.sess.opts.cg.linker_plugin_lto.enabled()
362                    && self.tcx.sess.target.is_like_windows
363                    && self.tcx.sess.opts.cg.prefer_dynamic)
364            );
365
366            if needs_dll_storage_attr {
367                // This item is external but not foreign, i.e., it originates from an external Rust
368                // crate. Since we don't know whether this crate will be linked dynamically or
369                // statically in the final application, we always mark such symbols as 'dllimport'.
370                // If final linkage happens to be static, we rely on compiler-emitted __imp_ stubs
371                // to make things work.
372                //
373                // However, in some scenarios we defer emission of statics to downstream
374                // crates, so there are cases where a static with an upstream DefId
375                // is actually present in the current crate. We can find out via the
376                // is_codegened_item query.
377                if !self.tcx.is_codegened_item(def_id) {
378                    unsafe {
379                        llvm::LLVMSetDLLStorageClass(g, llvm::DLLStorageClass::DllImport);
380                    }
381                }
382            }
383        }
384
385        if self.use_dll_storage_attrs
386            && let Some(library) = self.tcx.native_library(def_id)
387            && library.kind.is_dllimport()
388        {
389            // For foreign (native) libs we know the exact storage type to use.
390            unsafe {
391                llvm::LLVMSetDLLStorageClass(g, llvm::DLLStorageClass::DllImport);
392            }
393        }
394
395        self.instances.borrow_mut().insert(instance, g);
396        g
397    }
398
399    fn codegen_static_item(&self, def_id: DefId) {
400        unsafe {
401            assert!(
402                llvm::LLVMGetInitializer(
403                    self.instances.borrow().get(&Instance::mono(self.tcx, def_id)).unwrap()
404                )
405                .is_none()
406            );
407            let attrs = self.tcx.codegen_fn_attrs(def_id);
408
409            let Ok((v, alloc)) = codegen_static_initializer(self, def_id) else {
410                // Error has already been reported
411                return;
412            };
413            let alloc = alloc.inner();
414
415            let val_llty = self.val_ty(v);
416
417            let g = self.get_static_inner(def_id, val_llty);
418            let llty = llvm::LLVMGlobalGetValueType(g);
419
420            let g = if val_llty == llty {
421                g
422            } else {
423                // codegen_static_initializer creates the global value just from the
424                // `Allocation` data by generating one big struct value that is just
425                // all the bytes and pointers after each other. This will almost never
426                // match the type that the static was declared with. Unfortunately
427                // we can't just LLVMConstBitCast our way out of it because that has very
428                // specific rules on what can be cast. So instead of adding a new way to
429                // generate static initializers that match the static's type, we picked
430                // the easier option and retroactively change the type of the static item itself.
431                let name = llvm::get_value_name(g).to_vec();
432                llvm::set_value_name(g, b"");
433
434                let linkage = llvm::get_linkage(g);
435                let visibility = llvm::get_visibility(g);
436
437                let new_g = llvm::LLVMRustGetOrInsertGlobal(
438                    self.llmod,
439                    name.as_c_char_ptr(),
440                    name.len(),
441                    val_llty,
442                );
443
444                llvm::set_linkage(new_g, linkage);
445                llvm::set_visibility(new_g, visibility);
446
447                // The old global has had its name removed but is returned by
448                // get_static since it is in the instance cache. Provide an
449                // alternative lookup that points to the new global so that
450                // global_asm! can compute the correct mangled symbol name
451                // for the global.
452                self.renamed_statics.borrow_mut().insert(def_id, new_g);
453
454                // To avoid breaking any invariants, we leave around the old
455                // global for the moment; we'll replace all references to it
456                // with the new global later. (See base::codegen_backend.)
457                self.statics_to_rauw.borrow_mut().push((g, new_g));
458                new_g
459            };
460            set_global_alignment(self, g, alloc.align);
461            llvm::set_initializer(g, v);
462
463            if self.should_assume_dso_local(g, true) {
464                llvm::LLVMRustSetDSOLocal(g, true);
465            }
466
467            // Forward the allocation's mutability (picked by the const interner) to LLVM.
468            if alloc.mutability.is_not() {
469                llvm::LLVMSetGlobalConstant(g, llvm::True);
470            }
471
472            debuginfo::build_global_var_di_node(self, def_id, g);
473
474            if attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
475                llvm::set_thread_local_mode(g, self.tls_model);
476            }
477
478            // Wasm statics with custom link sections get special treatment as they
479            // go into custom sections of the wasm executable. The exception to this
480            // is the `.init_array` section which are treated specially by the wasm linker.
481            if self.tcx.sess.target.is_like_wasm
482                && attrs
483                    .link_section
484                    .map(|link_section| !link_section.as_str().starts_with(".init_array"))
485                    .unwrap_or(true)
486            {
487                if let Some(section) = attrs.link_section {
488                    let section = llvm::LLVMMDStringInContext2(
489                        self.llcx,
490                        section.as_str().as_c_char_ptr(),
491                        section.as_str().len(),
492                    );
493                    assert!(alloc.provenance().ptrs().is_empty());
494
495                    // The `inspect` method is okay here because we checked for provenance, and
496                    // because we are doing this access to inspect the final interpreter state (not
497                    // as part of the interpreter execution).
498                    let bytes =
499                        alloc.inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len());
500                    let alloc =
501                        llvm::LLVMMDStringInContext2(self.llcx, bytes.as_c_char_ptr(), bytes.len());
502                    let data = [section, alloc];
503                    let meta = llvm::LLVMMDNodeInContext2(self.llcx, data.as_ptr(), data.len());
504                    let val = llvm::LLVMMetadataAsValue(self.llcx, meta);
505                    llvm::LLVMAddNamedMetadataOperand(
506                        self.llmod,
507                        c"wasm.custom_sections".as_ptr(),
508                        val,
509                    );
510                }
511            } else {
512                base::set_link_section(g, attrs);
513            }
514
515            base::set_variable_sanitizer_attrs(g, attrs);
516
517            if attrs.flags.contains(CodegenFnAttrFlags::USED) {
518                // `USED` and `USED_LINKER` can't be used together.
519                assert!(!attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER));
520
521                // The semantics of #[used] in Rust only require the symbol to make it into the
522                // object file. It is explicitly allowed for the linker to strip the symbol if it
523                // is dead, which means we are allowed to use `llvm.compiler.used` instead of
524                // `llvm.used` here.
525                //
526                // Additionally, https://reviews.llvm.org/D97448 in LLVM 13 started emitting unique
527                // sections with SHF_GNU_RETAIN flag for llvm.used symbols, which may trigger bugs
528                // in the handling of `.init_array` (the static constructor list) in versions of
529                // the gold linker (prior to the one released with binutils 2.36).
530                //
531                // That said, we only ever emit these when compiling for ELF targets, unless
532                // `#[used(compiler)]` is explicitly requested. This is to avoid similar breakage
533                // on other targets, in particular MachO targets have *their* static constructor
534                // lists broken if `llvm.compiler.used` is emitted rather than `llvm.used`. However,
535                // that check happens when assigning the `CodegenFnAttrFlags` in
536                // `rustc_hir_analysis`, so we don't need to take care of it here.
537                self.add_compiler_used_global(g);
538            }
539            if attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER) {
540                // `USED` and `USED_LINKER` can't be used together.
541                assert!(!attrs.flags.contains(CodegenFnAttrFlags::USED));
542
543                self.add_used_global(g);
544            }
545        }
546    }
547}
548
549impl<'ll> StaticCodegenMethods for CodegenCx<'ll, '_> {
550    /// Get a pointer to a global variable.
551    ///
552    /// The pointer will always be in the default address space. If global variables default to a
553    /// different address space, an addrspacecast is inserted.
554    fn static_addr_of(&self, cv: &'ll Value, align: Align, kind: Option<&str>) -> &'ll Value {
555        let gv = self.static_addr_of_impl(cv, align, kind);
556        // static_addr_of_impl returns the bare global variable, which might not be in the default
557        // address space. Cast to the default address space if necessary.
558        self.const_pointercast(gv, self.type_ptr())
559    }
560
561    fn codegen_static(&self, def_id: DefId) {
562        self.codegen_static_item(def_id)
563    }
564
565    /// Add a global value to a list to be stored in the `llvm.used` variable, an array of ptr.
566    fn add_used_global(&self, global: &'ll Value) {
567        self.used_statics.borrow_mut().push(global);
568    }
569
570    /// Add a global value to a list to be stored in the `llvm.compiler.used` variable,
571    /// an array of ptr.
572    fn add_compiler_used_global(&self, global: &'ll Value) {
573        self.compiler_used_statics.borrow_mut().push(global);
574    }
575}