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