rustc_codegen_llvm/
consts.rs

1use std::ops::Range;
2
3use rustc_abi::{Align, HasDataLayout, Primitive, Scalar, Size, WrappingRange};
4use rustc_codegen_ssa::common;
5use rustc_codegen_ssa::traits::*;
6use rustc_hir::LangItem;
7use rustc_hir::attrs::Linkage;
8use rustc_hir::def::DefKind;
9use rustc_hir::def_id::{DefId, LOCAL_CRATE};
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::layout::{HasTypingEnv, LayoutOf};
17use rustc_middle::ty::{self, Instance};
18use rustc_middle::{bug, span_bug};
19use rustc_span::Symbol;
20use tracing::{debug, instrument, trace};
21
22use crate::common::CodegenCx;
23use crate::errors::SymbolAlreadyDefined;
24use crate::llvm::{self, Type, Value};
25use crate::type_of::LayoutLlvmExt;
26use crate::{base, debuginfo};
27
28pub(crate) fn const_alloc_to_llvm<'ll>(
29    cx: &CodegenCx<'ll, '_>,
30    alloc: &Allocation,
31    is_static: bool,
32) -> &'ll Value {
33    // We expect that callers of const_alloc_to_llvm will instead directly codegen a pointer or
34    // integer for any &ZST where the ZST is a constant (i.e. not a static). We should never be
35    // producing empty LLVM allocations as they're just adding noise to binaries and forcing less
36    // optimal codegen.
37    //
38    // Statics have a guaranteed meaningful address so it's less clear that we want to do
39    // something like this; it's also harder.
40    if !is_static {
41        assert!(alloc.len() != 0);
42    }
43    let mut llvals = Vec::with_capacity(alloc.provenance().ptrs().len() + 1);
44    let dl = cx.data_layout();
45    let pointer_size = dl.pointer_size();
46    let pointer_size_bytes = pointer_size.bytes() as usize;
47
48    // Note: this function may call `inspect_with_uninit_and_ptr_outside_interpreter`, so `range`
49    // must be within the bounds of `alloc` and not contain or overlap a pointer provenance.
50    fn append_chunks_of_init_and_uninit_bytes<'ll, 'a, 'b>(
51        llvals: &mut Vec<&'ll Value>,
52        cx: &'a CodegenCx<'ll, 'b>,
53        alloc: &'a Allocation,
54        range: Range<usize>,
55    ) {
56        let chunks = alloc.init_mask().range_as_init_chunks(range.clone().into());
57
58        let chunk_to_llval = move |chunk| match chunk {
59            InitChunk::Init(range) => {
60                let range = (range.start.bytes() as usize)..(range.end.bytes() as usize);
61                let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(range);
62                cx.const_bytes(bytes)
63            }
64            InitChunk::Uninit(range) => {
65                let len = range.end.bytes() - range.start.bytes();
66                cx.const_undef(cx.type_array(cx.type_i8(), len))
67            }
68        };
69
70        // Generating partially-uninit consts is limited to small numbers of chunks,
71        // to avoid the cost of generating large complex const expressions.
72        // For example, `[(u32, u8); 1024 * 1024]` contains uninit padding in each element, and
73        // would result in `{ [5 x i8] zeroinitializer, [3 x i8] undef, ...repeat 1M times... }`.
74        let max = cx.sess().opts.unstable_opts.uninit_const_chunk_threshold;
75        let allow_uninit_chunks = chunks.clone().take(max.saturating_add(1)).count() <= max;
76
77        if allow_uninit_chunks {
78            llvals.extend(chunks.map(chunk_to_llval));
79        } else {
80            // If this allocation contains any uninit bytes, codegen as if it was initialized
81            // (using some arbitrary value for uninit bytes).
82            let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(range);
83            llvals.push(cx.const_bytes(bytes));
84        }
85    }
86
87    let mut next_offset = 0;
88    for &(offset, prov) in alloc.provenance().ptrs().iter() {
89        let offset = offset.bytes();
90        assert_eq!(offset as usize as u64, offset);
91        let offset = offset as usize;
92        if offset > next_offset {
93            // This `inspect` is okay since we have checked that there is no provenance, it
94            // is within the bounds of the allocation, and it doesn't affect interpreter execution
95            // (we inspect the result after interpreter execution).
96            append_chunks_of_init_and_uninit_bytes(&mut llvals, cx, alloc, next_offset..offset);
97        }
98        let ptr_offset = read_target_uint(
99            dl.endian,
100            // This `inspect` is okay since it is within the bounds of the allocation, it doesn't
101            // affect interpreter execution (we inspect the result after interpreter execution),
102            // and we properly interpret the provenance as a relocation pointer offset.
103            alloc.inspect_with_uninit_and_ptr_outside_interpreter(
104                offset..(offset + pointer_size_bytes),
105            ),
106        )
107        .expect("const_alloc_to_llvm: could not read relocation pointer")
108            as u64;
109
110        let address_space = cx.tcx.global_alloc(prov.alloc_id()).address_space(cx);
111
112        llvals.push(cx.scalar_to_backend(
113            InterpScalar::from_pointer(Pointer::new(prov, Size::from_bytes(ptr_offset)), &cx.tcx),
114            Scalar::Initialized {
115                value: Primitive::Pointer(address_space),
116                valid_range: WrappingRange::full(pointer_size),
117            },
118            cx.type_ptr_ext(address_space),
119        ));
120        next_offset = offset + pointer_size_bytes;
121    }
122    if alloc.len() >= next_offset {
123        let range = next_offset..alloc.len();
124        // This `inspect` is okay since we have check that it is after all provenance, it is
125        // within the bounds of the allocation, and it doesn't affect interpreter execution (we
126        // inspect the result after interpreter execution).
127        append_chunks_of_init_and_uninit_bytes(&mut llvals, cx, alloc, range);
128    }
129
130    // Avoid wrapping in a struct if there is only a single value. This ensures
131    // that LLVM is able to perform the string merging optimization if the constant
132    // is a valid C string. LLVM only considers bare arrays for this optimization,
133    // not arrays wrapped in a struct. LLVM handles this at:
134    // https://github.com/rust-lang/llvm-project/blob/acaea3d2bb8f351b740db7ebce7d7a40b9e21488/llvm/lib/Target/TargetLoweringObjectFile.cpp#L249-L280
135    if let &[data] = &*llvals { data } else { cx.const_struct(&llvals, true) }
136}
137
138fn codegen_static_initializer<'ll, 'tcx>(
139    cx: &CodegenCx<'ll, 'tcx>,
140    def_id: DefId,
141) -> Result<(&'ll Value, ConstAllocation<'tcx>), ErrorHandled> {
142    let alloc = cx.tcx.eval_static_initializer(def_id)?;
143    Ok((const_alloc_to_llvm(cx, alloc.inner(), /*static*/ true), alloc))
144}
145
146fn set_global_alignment<'ll>(cx: &CodegenCx<'ll, '_>, gv: &'ll Value, mut align: Align) {
147    // The target may require greater alignment for globals than the type does.
148    // Note: GCC and Clang also allow `__attribute__((aligned))` on variables,
149    // which can force it to be smaller. Rust doesn't support this yet.
150    if let Some(min_global) = cx.sess().target.min_global_align {
151        align = Ord::max(align, min_global);
152    }
153    llvm::set_alignment(gv, align);
154}
155
156fn check_and_apply_linkage<'ll, 'tcx>(
157    cx: &CodegenCx<'ll, 'tcx>,
158    attrs: &CodegenFnAttrs,
159    llty: &'ll Type,
160    sym: &str,
161    def_id: DefId,
162) -> &'ll Value {
163    if let Some(linkage) = attrs.import_linkage {
164        debug!("get_static: sym={} linkage={:?}", sym, linkage);
165
166        // Declare a symbol `foo`. If `foo` is an extern_weak symbol, we declare
167        // an extern_weak function, otherwise a global with the desired linkage.
168        let g1 = if matches!(attrs.import_linkage, Some(Linkage::ExternalWeak)) {
169            // An `extern_weak` function is represented as an `Option<unsafe extern ...>`,
170            // we extract the function signature and declare it as an extern_weak function
171            // instead of an extern_weak i8.
172            let instance = Instance::mono(cx.tcx, def_id);
173            if let ty::Adt(struct_def, args) = instance.ty(cx.tcx, cx.typing_env()).kind()
174                && cx.tcx.is_lang_item(struct_def.did(), LangItem::Option)
175                && let ty::FnPtr(sig, header) = args.type_at(0).kind()
176            {
177                let fn_sig = sig.with(*header);
178
179                let fn_abi = cx.fn_abi_of_fn_ptr(fn_sig, ty::List::empty());
180                cx.declare_fn(sym, &fn_abi, None)
181            } else {
182                cx.declare_global(sym, cx.type_i8())
183            }
184        } else {
185            cx.declare_global(sym, cx.type_i8())
186        };
187        llvm::set_linkage(g1, base::linkage_to_llvm(linkage));
188
189        // Declare an internal global `extern_with_linkage_foo` which
190        // is initialized with the address of `foo`. If `foo` is
191        // discarded during linking (for example, if `foo` has weak
192        // linkage and there are no definitions), then
193        // `extern_with_linkage_foo` will instead be initialized to
194        // zero.
195        let real_name =
196            format!("_rust_extern_with_linkage_{:016x}_{sym}", cx.tcx.stable_crate_id(LOCAL_CRATE));
197        let g2 = cx.define_global(&real_name, llty).unwrap_or_else(|| {
198            cx.sess().dcx().emit_fatal(SymbolAlreadyDefined {
199                span: cx.tcx.def_span(def_id),
200                symbol_name: sym,
201            })
202        });
203        llvm::set_linkage(g2, llvm::Linkage::InternalLinkage);
204        llvm::set_initializer(g2, g1);
205        g2
206    } else if cx.tcx.sess.target.arch == "x86"
207        && common::is_mingw_gnu_toolchain(&cx.tcx.sess.target)
208        && let Some(dllimport) = crate::common::get_dllimport(cx.tcx, def_id, sym)
209    {
210        cx.declare_global(&common::i686_decorated_name(dllimport, true, true, false), llty)
211    } else {
212        // Generate an external declaration.
213        // FIXME(nagisa): investigate whether it can be changed into define_global
214        cx.declare_global(sym, llty)
215    }
216}
217
218impl<'ll> CodegenCx<'ll, '_> {
219    pub(crate) fn const_bitcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
220        unsafe { llvm::LLVMConstBitCast(val, ty) }
221    }
222
223    pub(crate) fn const_pointercast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
224        unsafe { llvm::LLVMConstPointerCast(val, ty) }
225    }
226
227    /// Create a global variable.
228    ///
229    /// The returned global variable is a pointer in the default address space for globals.
230    /// Fails if a symbol with the given name already exists.
231    pub(crate) fn static_addr_of_mut(
232        &self,
233        cv: &'ll Value,
234        align: Align,
235        kind: Option<&str>,
236    ) -> &'ll Value {
237        let gv = match kind {
238            Some(kind) if !self.tcx.sess.fewer_names() => {
239                let name = self.generate_local_symbol_name(kind);
240                let gv = self.define_global(&name, self.val_ty(cv)).unwrap_or_else(|| {
241                    bug!("symbol `{}` is already defined", name);
242                });
243                gv
244            }
245            _ => self.define_global("", self.val_ty(cv)).unwrap_or_else(|| {
246                bug!("anonymous global symbol is already defined");
247            }),
248        };
249        llvm::set_linkage(gv, llvm::Linkage::PrivateLinkage);
250        llvm::set_initializer(gv, cv);
251        set_global_alignment(self, gv, align);
252        llvm::set_unnamed_address(gv, llvm::UnnamedAddr::Global);
253        gv
254    }
255
256    /// Create a global constant.
257    ///
258    /// The returned global variable is a pointer in the default address space for globals.
259    pub(crate) fn static_addr_of_impl(
260        &self,
261        cv: &'ll Value,
262        align: Align,
263        kind: Option<&str>,
264    ) -> &'ll Value {
265        if let Some(&gv) = self.const_globals.borrow().get(&cv) {
266            unsafe {
267                // Upgrade the alignment in cases where the same constant is used with different
268                // alignment requirements
269                let llalign = align.bytes() as u32;
270                if llalign > llvm::LLVMGetAlignment(gv) {
271                    llvm::LLVMSetAlignment(gv, llalign);
272                }
273            }
274            return gv;
275        }
276        let gv = self.static_addr_of_mut(cv, align, kind);
277        llvm::set_global_constant(gv, true);
278
279        self.const_globals.borrow_mut().insert(cv, gv);
280        gv
281    }
282
283    #[instrument(level = "debug", skip(self))]
284    pub(crate) fn get_static(&self, def_id: DefId) -> &'ll Value {
285        let instance = Instance::mono(self.tcx, def_id);
286        trace!(?instance);
287
288        let DefKind::Static { nested, .. } = self.tcx.def_kind(def_id) else { bug!() };
289        // Nested statics do not have a type, so pick a dummy type and let `codegen_static` figure
290        // out the llvm type from the actual evaluated initializer.
291        let llty = if nested {
292            self.type_i8()
293        } else {
294            let ty = instance.ty(self.tcx, self.typing_env());
295            trace!(?ty);
296            self.layout_of(ty).llvm_type(self)
297        };
298        self.get_static_inner(def_id, llty)
299    }
300
301    #[instrument(level = "debug", skip(self, llty))]
302    fn get_static_inner(&self, def_id: DefId, llty: &'ll Type) -> &'ll Value {
303        let instance = Instance::mono(self.tcx, def_id);
304        if let Some(&g) = self.instances.borrow().get(&instance) {
305            trace!("used cached value");
306            return g;
307        }
308
309        let defined_in_current_codegen_unit =
310            self.codegen_unit.items().contains_key(&MonoItem::Static(def_id));
311        assert!(
312            !defined_in_current_codegen_unit,
313            "consts::get_static() should always hit the cache for \
314                 statics defined in the same CGU, but did not for `{def_id:?}`"
315        );
316
317        let sym = self.tcx.symbol_name(instance).name;
318        let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
319
320        debug!(?sym, ?fn_attrs);
321
322        let g = if def_id.is_local() && !self.tcx.is_foreign_item(def_id) {
323            if let Some(g) = self.get_declared_value(sym) {
324                if self.val_ty(g) != self.type_ptr() {
325                    span_bug!(self.tcx.def_span(def_id), "Conflicting types for static");
326                }
327            }
328
329            let g = self.declare_global(sym, llty);
330
331            if !self.tcx.is_reachable_non_generic(def_id) {
332                llvm::set_visibility(g, llvm::Visibility::Hidden);
333            }
334
335            g
336        } else if let Some(classname) = fn_attrs.objc_class {
337            self.get_objc_classref(classname)
338        } else if let Some(methname) = fn_attrs.objc_selector {
339            self.get_objc_selref(methname)
340        } else {
341            check_and_apply_linkage(self, fn_attrs, llty, sym, def_id)
342        };
343
344        // Thread-local statics in some other crate need to *always* be linked
345        // against in a thread-local fashion, so we need to be sure to apply the
346        // thread-local attribute locally if it was present remotely. If we
347        // don't do this then linker errors can be generated where the linker
348        // complains that one object files has a thread local version of the
349        // symbol and another one doesn't.
350        if fn_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
351            llvm::set_thread_local_mode(g, self.tls_model);
352        }
353
354        let dso_local = self.assume_dso_local(g, true);
355
356        if !def_id.is_local() {
357            let needs_dll_storage_attr = self.use_dll_storage_attrs
358                && !self.tcx.is_foreign_item(def_id)
359                // Local definitions can never be imported, so we must not apply
360                // the DLLImport annotation.
361                && !dso_local
362                // Linker plugin ThinLTO doesn't create the self-dllimport Rust uses for rlibs
363                // as the code generation happens out of process. Instead we assume static linkage
364                // and disallow dynamic linking when linker plugin based LTO is enabled.
365                // Regular in-process ThinLTO doesn't need this workaround.
366                && !self.tcx.sess.opts.cg.linker_plugin_lto.enabled();
367
368            // If this assertion triggers, there's something wrong with commandline
369            // argument validation.
370            assert!(
371                !(self.tcx.sess.opts.cg.linker_plugin_lto.enabled()
372                    && self.tcx.sess.target.is_like_windows
373                    && self.tcx.sess.opts.cg.prefer_dynamic)
374            );
375
376            if needs_dll_storage_attr {
377                // This item is external but not foreign, i.e., it originates from an external Rust
378                // crate. Since we don't know whether this crate will be linked dynamically or
379                // statically in the final application, we always mark such symbols as 'dllimport'.
380                // If final linkage happens to be static, we rely on compiler-emitted __imp_ stubs
381                // to make things work.
382                //
383                // However, in some scenarios we defer emission of statics to downstream
384                // crates, so there are cases where a static with an upstream DefId
385                // is actually present in the current crate. We can find out via the
386                // is_codegened_item query.
387                if !self.tcx.is_codegened_item(def_id) {
388                    llvm::set_dllimport_storage_class(g);
389                }
390            }
391        }
392
393        if self.use_dll_storage_attrs
394            && let Some(library) = self.tcx.native_library(def_id)
395            && library.kind.is_dllimport()
396        {
397            // For foreign (native) libs we know the exact storage type to use.
398            llvm::set_dllimport_storage_class(g);
399        }
400
401        self.instances.borrow_mut().insert(instance, g);
402        g
403    }
404
405    fn codegen_static_item(&mut self, def_id: DefId) {
406        assert!(
407            llvm::LLVMGetInitializer(
408                self.instances.borrow().get(&Instance::mono(self.tcx, def_id)).unwrap()
409            )
410            .is_none()
411        );
412        let attrs = self.tcx.codegen_fn_attrs(def_id);
413
414        let Ok((v, alloc)) = codegen_static_initializer(self, def_id) else {
415            // Error has already been reported
416            return;
417        };
418        let alloc = alloc.inner();
419
420        let val_llty = self.val_ty(v);
421
422        let g = self.get_static_inner(def_id, val_llty);
423        let llty = self.get_type_of_global(g);
424
425        let g = if val_llty == llty {
426            g
427        } else {
428            // codegen_static_initializer creates the global value just from the
429            // `Allocation` data by generating one big struct value that is just
430            // all the bytes and pointers after each other. This will almost never
431            // match the type that the static was declared with. Unfortunately
432            // we can't just LLVMConstBitCast our way out of it because that has very
433            // specific rules on what can be cast. So instead of adding a new way to
434            // generate static initializers that match the static's type, we picked
435            // the easier option and retroactively change the type of the static item itself.
436            let name = String::from_utf8(llvm::get_value_name(g))
437                .expect("we declare our statics with a utf8-valid name");
438            llvm::set_value_name(g, b"");
439
440            let linkage = llvm::get_linkage(g);
441            let visibility = llvm::get_visibility(g);
442
443            let new_g = self.declare_global(&name, val_llty);
444
445            llvm::set_linkage(new_g, linkage);
446            llvm::set_visibility(new_g, visibility);
447
448            // The old global has had its name removed but is returned by
449            // get_static since it is in the instance cache. Provide an
450            // alternative lookup that points to the new global so that
451            // global_asm! can compute the correct mangled symbol name
452            // for the global.
453            self.renamed_statics.borrow_mut().insert(def_id, new_g);
454
455            // To avoid breaking any invariants, we leave around the old
456            // global for the moment; we'll replace all references to it
457            // with the new global later. (See base::codegen_backend.)
458            self.statics_to_rauw.borrow_mut().push((g, new_g));
459            new_g
460        };
461
462        // NOTE: Alignment from attributes has already been applied to the allocation.
463        set_global_alignment(self, g, alloc.align);
464        llvm::set_initializer(g, v);
465
466        self.assume_dso_local(g, true);
467
468        // Forward the allocation's mutability (picked by the const interner) to LLVM.
469        if alloc.mutability.is_not() {
470            llvm::set_global_constant(g, true);
471        }
472
473        debuginfo::build_global_var_di_node(self, def_id, g);
474
475        if attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
476            llvm::set_thread_local_mode(g, self.tls_model);
477        }
478
479        // Wasm statics with custom link sections get special treatment as they
480        // go into custom sections of the wasm executable. The exception to this
481        // is the `.init_array` section which are treated specially by the wasm linker.
482        if self.tcx.sess.target.is_like_wasm
483            && attrs
484                .link_section
485                .map(|link_section| !link_section.as_str().starts_with(".init_array"))
486                .unwrap_or(true)
487        {
488            if let Some(section) = attrs.link_section {
489                let section = self.create_metadata(section.as_str().as_bytes());
490                assert!(alloc.provenance().ptrs().is_empty());
491
492                // The `inspect` method is okay here because we checked for provenance, and
493                // because we are doing this access to inspect the final interpreter state (not
494                // as part of the interpreter execution).
495                let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len());
496                let alloc = self.create_metadata(bytes);
497                let data = [section, alloc];
498                self.module_add_named_metadata_node(self.llmod(), c"wasm.custom_sections", &data);
499            }
500        } else {
501            base::set_link_section(g, attrs);
502        }
503
504        base::set_variable_sanitizer_attrs(g, attrs);
505
506        if attrs.flags.contains(CodegenFnAttrFlags::USED_COMPILER) {
507            // `USED` and `USED_LINKER` can't be used together.
508            assert!(!attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER));
509
510            // The semantics of #[used] in Rust only require the symbol to make it into the
511            // object file. It is explicitly allowed for the linker to strip the symbol if it
512            // is dead, which means we are allowed to use `llvm.compiler.used` instead of
513            // `llvm.used` here.
514            //
515            // Additionally, https://reviews.llvm.org/D97448 in LLVM 13 started emitting unique
516            // sections with SHF_GNU_RETAIN flag for llvm.used symbols, which may trigger bugs
517            // in the handling of `.init_array` (the static constructor list) in versions of
518            // the gold linker (prior to the one released with binutils 2.36).
519            //
520            // That said, we only ever emit these when `#[used(compiler)]` is explicitly
521            // requested. This is to avoid similar breakage on other targets, in particular
522            // MachO targets have *their* static constructor lists broken if `llvm.compiler.used`
523            // is emitted rather than `llvm.used`. However, that check happens when assigning
524            // the `CodegenFnAttrFlags` in the `codegen_fn_attrs` query, so we don't need to
525            // take care of it here.
526            self.add_compiler_used_global(g);
527        }
528        if attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER) {
529            // `USED` and `USED_LINKER` can't be used together.
530            assert!(!attrs.flags.contains(CodegenFnAttrFlags::USED_COMPILER));
531
532            self.add_used_global(g);
533        }
534    }
535
536    /// Add a global value to a list to be stored in the `llvm.used` variable, an array of ptr.
537    pub(crate) fn add_used_global(&mut self, global: &'ll Value) {
538        self.used_statics.push(global);
539    }
540
541    /// Add a global value to a list to be stored in the `llvm.compiler.used` variable,
542    /// an array of ptr.
543    pub(crate) fn add_compiler_used_global(&self, global: &'ll Value) {
544        self.compiler_used_statics.borrow_mut().push(global);
545    }
546
547    // We do our best here to match what Clang does when compiling Objective-C natively.
548    // See Clang's `CGObjCCommonMac::CreateCStringLiteral`:
549    // https://github.com/llvm/llvm-project/blob/llvmorg-20.1.8/clang/lib/CodeGen/CGObjCMac.cpp#L4134
550    fn define_objc_classname(&self, classname: &str) -> &'ll Value {
551        assert_eq!(self.objc_abi_version(), 1);
552
553        let llval = self.null_terminate_const_bytes(classname.as_bytes());
554        let llty = self.val_ty(llval);
555        let sym = self.generate_local_symbol_name("OBJC_CLASS_NAME_");
556        let g = self.define_global(&sym, llty).unwrap_or_else(|| {
557            bug!("symbol `{}` is already defined", sym);
558        });
559        set_global_alignment(self, g, self.tcx.data_layout.i8_align);
560        llvm::set_initializer(g, llval);
561        llvm::set_linkage(g, llvm::Linkage::PrivateLinkage);
562        llvm::set_section(g, c"__TEXT,__cstring,cstring_literals");
563        llvm::LLVMSetGlobalConstant(g, llvm::TRUE);
564        llvm::LLVMSetUnnamedAddress(g, llvm::UnnamedAddr::Global);
565        self.add_compiler_used_global(g);
566
567        g
568    }
569
570    // We do our best here to match what Clang does when compiling Objective-C natively.
571    // See Clang's `ObjCNonFragileABITypesHelper`:
572    // https://github.com/llvm/llvm-project/blob/llvmorg-20.1.8/clang/lib/CodeGen/CGObjCMac.cpp#L6052
573    fn get_objc_class_t(&self) -> &'ll Type {
574        if let Some(class_t) = self.objc_class_t.get() {
575            return class_t;
576        }
577
578        assert_eq!(self.objc_abi_version(), 2);
579
580        // struct _class_t {
581        //     struct _class_t* isa;
582        //     struct _class_t* const superclass;
583        //     void* cache;
584        //     IMP* vtable;
585        //     struct class_ro_t* ro;
586        // }
587
588        let class_t = self.type_named_struct("struct._class_t");
589        let els = [self.type_ptr(); 5];
590        let packed = false;
591        self.set_struct_body(class_t, &els, packed);
592
593        self.objc_class_t.set(Some(class_t));
594        class_t
595    }
596
597    // We do our best here to match what Clang does when compiling Objective-C natively. We
598    // deduplicate references within a CGU, but we need a reference definition in each referencing
599    // CGU. All attempts at using external references to a single reference definition result in
600    // linker errors.
601    fn get_objc_classref(&self, classname: Symbol) -> &'ll Value {
602        let mut classrefs = self.objc_classrefs.borrow_mut();
603        if let Some(classref) = classrefs.get(&classname).copied() {
604            return classref;
605        }
606
607        let g = match self.objc_abi_version() {
608            1 => {
609                // See Clang's `CGObjCMac::EmitClassRefFromId`:
610                // https://github.com/llvm/llvm-project/blob/llvmorg-20.1.8/clang/lib/CodeGen/CGObjCMac.cpp#L5205
611                let llval = self.define_objc_classname(classname.as_str());
612                let llty = self.type_ptr();
613                let sym = self.generate_local_symbol_name("OBJC_CLASS_REFERENCES_");
614                let g = self.define_global(&sym, llty).unwrap_or_else(|| {
615                    bug!("symbol `{}` is already defined", sym);
616                });
617                set_global_alignment(self, g, self.tcx.data_layout.pointer_align().abi);
618                llvm::set_initializer(g, llval);
619                llvm::set_linkage(g, llvm::Linkage::PrivateLinkage);
620                llvm::set_section(g, c"__OBJC,__cls_refs,literal_pointers,no_dead_strip");
621                self.add_compiler_used_global(g);
622                g
623            }
624            2 => {
625                // See Clang's `CGObjCNonFragileABIMac::EmitClassRefFromId`:
626                // https://github.com/llvm/llvm-project/blob/llvmorg-20.1.8/clang/lib/CodeGen/CGObjCMac.cpp#L7423
627                let llval = {
628                    let extern_sym = format!("OBJC_CLASS_$_{}", classname.as_str());
629                    let extern_llty = self.get_objc_class_t();
630                    self.declare_global(&extern_sym, extern_llty)
631                };
632                let llty = self.type_ptr();
633                let sym = self.generate_local_symbol_name("OBJC_CLASSLIST_REFERENCES_$_");
634                let g = self.define_global(&sym, llty).unwrap_or_else(|| {
635                    bug!("symbol `{}` is already defined", sym);
636                });
637                set_global_alignment(self, g, self.tcx.data_layout.pointer_align().abi);
638                llvm::set_initializer(g, llval);
639                llvm::set_linkage(g, llvm::Linkage::InternalLinkage);
640                llvm::set_section(g, c"__DATA,__objc_classrefs,regular,no_dead_strip");
641                self.add_compiler_used_global(g);
642                g
643            }
644            _ => unreachable!(),
645        };
646
647        classrefs.insert(classname, g);
648        g
649    }
650
651    // We do our best here to match what Clang does when compiling Objective-C natively. We
652    // deduplicate references within a CGU, but we need a reference definition in each referencing
653    // CGU. All attempts at using external references to a single reference definition result in
654    // linker errors.
655    //
656    // Newer versions of Apple Clang generate calls to `@"objc_msgSend$methname"` selector stub
657    // functions. We don't currently do that. The code we generate is closer to what Apple Clang
658    // generates with the `-fno-objc-msgsend-selector-stubs` option.
659    fn get_objc_selref(&self, methname: Symbol) -> &'ll Value {
660        let mut selrefs = self.objc_selrefs.borrow_mut();
661        if let Some(selref) = selrefs.get(&methname).copied() {
662            return selref;
663        }
664
665        let abi_version = self.objc_abi_version();
666
667        // See Clang's `CGObjCCommonMac::CreateCStringLiteral`:
668        // https://github.com/llvm/llvm-project/blob/llvmorg-20.1.8/clang/lib/CodeGen/CGObjCMac.cpp#L4134
669        let methname_llval = self.null_terminate_const_bytes(methname.as_str().as_bytes());
670        let methname_llty = self.val_ty(methname_llval);
671        let methname_sym = self.generate_local_symbol_name("OBJC_METH_VAR_NAME_");
672        let methname_g = self.define_global(&methname_sym, methname_llty).unwrap_or_else(|| {
673            bug!("symbol `{}` is already defined", methname_sym);
674        });
675        set_global_alignment(self, methname_g, self.tcx.data_layout.i8_align);
676        llvm::set_initializer(methname_g, methname_llval);
677        llvm::set_linkage(methname_g, llvm::Linkage::PrivateLinkage);
678        llvm::set_section(
679            methname_g,
680            match abi_version {
681                1 => c"__TEXT,__cstring,cstring_literals",
682                2 => c"__TEXT,__objc_methname,cstring_literals",
683                _ => unreachable!(),
684            },
685        );
686        llvm::LLVMSetGlobalConstant(methname_g, llvm::TRUE);
687        llvm::LLVMSetUnnamedAddress(methname_g, llvm::UnnamedAddr::Global);
688        self.add_compiler_used_global(methname_g);
689
690        // See Clang's `CGObjCMac::EmitSelectorAddr`:
691        // https://github.com/llvm/llvm-project/blob/llvmorg-20.1.8/clang/lib/CodeGen/CGObjCMac.cpp#L5243
692        // And Clang's `CGObjCNonFragileABIMac::EmitSelectorAddr`:
693        // https://github.com/llvm/llvm-project/blob/llvmorg-20.1.8/clang/lib/CodeGen/CGObjCMac.cpp#L7586
694        let selref_llval = methname_g;
695        let selref_llty = self.type_ptr();
696        let selref_sym = self.generate_local_symbol_name("OBJC_SELECTOR_REFERENCES_");
697        let selref_g = self.define_global(&selref_sym, selref_llty).unwrap_or_else(|| {
698            bug!("symbol `{}` is already defined", selref_sym);
699        });
700        set_global_alignment(self, selref_g, self.tcx.data_layout.pointer_align().abi);
701        llvm::set_initializer(selref_g, selref_llval);
702        llvm::set_externally_initialized(selref_g, true);
703        llvm::set_linkage(
704            selref_g,
705            match abi_version {
706                1 => llvm::Linkage::PrivateLinkage,
707                2 => llvm::Linkage::InternalLinkage,
708                _ => unreachable!(),
709            },
710        );
711        llvm::set_section(
712            selref_g,
713            match abi_version {
714                1 => c"__OBJC,__message_refs,literal_pointers,no_dead_strip",
715                2 => c"__DATA,__objc_selrefs,literal_pointers,no_dead_strip",
716                _ => unreachable!(),
717            },
718        );
719        self.add_compiler_used_global(selref_g);
720
721        selrefs.insert(methname, selref_g);
722        selref_g
723    }
724
725    // We do our best here to match what Clang does when compiling Objective-C natively.
726    // See Clang's `ObjCTypesHelper`:
727    // https://github.com/llvm/llvm-project/blob/llvmorg-20.1.8/clang/lib/CodeGen/CGObjCMac.cpp#L5936
728    // And Clang's `CGObjCMac::EmitModuleInfo`:
729    // https://github.com/llvm/llvm-project/blob/llvmorg-20.1.8/clang/lib/CodeGen/CGObjCMac.cpp#L5151
730    pub(crate) fn define_objc_module_info(&mut self) {
731        assert_eq!(self.objc_abi_version(), 1);
732
733        // struct _objc_module {
734        //     long version;                // Hardcoded to 7 in Clang.
735        //     long size;                   // sizeof(struct _objc_module)
736        //     char* name;                  // Hardcoded to classname "" in Clang.
737        //     struct _objc_symtab* symtab; // Null without class or category definitions.
738        //  }
739
740        let llty = self.type_named_struct("struct._objc_module");
741        let i32_llty = self.type_i32();
742        let ptr_llty = self.type_ptr();
743        let packed = false;
744        self.set_struct_body(llty, &[i32_llty, i32_llty, ptr_llty, ptr_llty], packed);
745
746        let version = self.const_uint(i32_llty, 7);
747        let size = self.const_uint(i32_llty, 16);
748        let name = self.define_objc_classname("");
749        let symtab = self.const_null(ptr_llty);
750        let llval = crate::common::named_struct(llty, &[version, size, name, symtab]);
751
752        let sym = "OBJC_MODULES";
753        let g = self.define_global(&sym, llty).unwrap_or_else(|| {
754            bug!("symbol `{}` is already defined", sym);
755        });
756        set_global_alignment(self, g, self.tcx.data_layout.pointer_align().abi);
757        llvm::set_initializer(g, llval);
758        llvm::set_linkage(g, llvm::Linkage::PrivateLinkage);
759        llvm::set_section(g, c"__OBJC,__module_info,regular,no_dead_strip");
760
761        self.add_compiler_used_global(g);
762    }
763}
764
765impl<'ll> StaticCodegenMethods for CodegenCx<'ll, '_> {
766    /// Get a pointer to a global variable.
767    ///
768    /// The pointer will always be in the default address space. If global variables default to a
769    /// different address space, an addrspacecast is inserted.
770    fn static_addr_of(&self, cv: &'ll Value, align: Align, kind: Option<&str>) -> &'ll Value {
771        let gv = self.static_addr_of_impl(cv, align, kind);
772        // static_addr_of_impl returns the bare global variable, which might not be in the default
773        // address space. Cast to the default address space if necessary.
774        self.const_pointercast(gv, self.type_ptr())
775    }
776
777    fn codegen_static(&mut self, def_id: DefId) {
778        self.codegen_static_item(def_id)
779    }
780}