initial implementation of the darwin_objc unstable feature

This commit is contained in:
Jo Bates
2025-06-04 19:49:13 -07:00
parent 02c7b1a7ac
commit 1ebf69d1b1
39 changed files with 1381 additions and 11 deletions

View File

@@ -26,7 +26,7 @@ use rustc_session::config::{
BranchProtection, CFGuard, CFProtection, CrateType, DebugInfo, FunctionReturn, PAuthKey, PacRet,
};
use rustc_span::source_map::Spanned;
use rustc_span::{DUMMY_SP, Span};
use rustc_span::{DUMMY_SP, Span, Symbol};
use rustc_symbol_mangling::mangle_internal_symbol;
use rustc_target::spec::{HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel};
use smallvec::SmallVec;
@@ -119,7 +119,7 @@ pub(crate) struct FullCx<'ll, 'tcx> {
/// Statics that will be placed in the llvm.compiler.used variable
/// See <https://llvm.org/docs/LangRef.html#the-llvm-compiler-used-global-variable> for details
pub compiler_used_statics: Vec<&'ll Value>,
pub compiler_used_statics: RefCell<Vec<&'ll Value>>,
/// Mapping of non-scalar types to llvm types.
pub type_lowering: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), &'ll Type>>,
@@ -146,6 +146,15 @@ pub(crate) struct FullCx<'ll, 'tcx> {
/// `global_asm!` needs to be able to find this new global so that it can
/// compute the correct mangled symbol name to insert into the asm.
pub renamed_statics: RefCell<FxHashMap<DefId, &'ll Value>>,
/// Cached Objective-C class type
pub objc_class_t: Cell<Option<&'ll Type>>,
/// Cache of Objective-C class references
pub objc_classrefs: RefCell<FxHashMap<Symbol, &'ll Value>>,
/// Cache of Objective-C selector references
pub objc_selrefs: RefCell<FxHashMap<Symbol, &'ll Value>>,
}
fn to_llvm_tls_model(tls_model: TlsModel) -> llvm::ThreadLocalMode {
@@ -644,7 +653,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
const_globals: Default::default(),
statics_to_rauw: RefCell::new(Vec::new()),
used_statics: Vec::new(),
compiler_used_statics: Vec::new(),
compiler_used_statics: Default::default(),
type_lowering: Default::default(),
scalar_lltypes: Default::default(),
coverage_cx,
@@ -655,6 +664,9 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
intrinsics: Default::default(),
local_gen_sym_counter: Cell::new(0),
renamed_statics: Default::default(),
objc_class_t: Cell::new(None),
objc_classrefs: Default::default(),
objc_selrefs: Default::default(),
},
PhantomData,
)
@@ -679,6 +691,69 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
llvm::set_linkage(g, llvm::Linkage::AppendingLinkage);
llvm::set_section(g, c"llvm.metadata");
}
/// The Objective-C ABI that is used.
///
/// This corresponds to the `-fobjc-abi-version=` flag in Clang / GCC.
pub(crate) fn objc_abi_version(&self) -> u32 {
assert!(self.tcx.sess.target.is_like_darwin);
if self.tcx.sess.target.arch == "x86" && self.tcx.sess.target.os == "macos" {
// 32-bit x86 macOS uses ABI version 1 (a.k.a. the "fragile ABI").
1
} else {
// All other Darwin-like targets we support use ABI version 2
// (a.k.a the "non-fragile ABI").
2
}
}
// We do our best here to match what Clang does when compiling Objective-C natively.
// See Clang's `CGObjCCommonMac::EmitImageInfo`:
// https://github.com/llvm/llvm-project/blob/llvmorg-20.1.8/clang/lib/CodeGen/CGObjCMac.cpp#L5085
pub(crate) fn add_objc_module_flags(&self) {
let abi_version = self.objc_abi_version();
llvm::add_module_flag_u32(
self.llmod,
llvm::ModuleFlagMergeBehavior::Error,
"Objective-C Version",
abi_version,
);
llvm::add_module_flag_u32(
self.llmod,
llvm::ModuleFlagMergeBehavior::Error,
"Objective-C Image Info Version",
0,
);
llvm::add_module_flag_str(
self.llmod,
llvm::ModuleFlagMergeBehavior::Error,
"Objective-C Image Info Section",
match abi_version {
1 => "__OBJC,__image_info,regular",
2 => "__DATA,__objc_imageinfo,regular,no_dead_strip",
_ => unreachable!(),
},
);
if self.tcx.sess.target.env == "sim" {
llvm::add_module_flag_u32(
self.llmod,
llvm::ModuleFlagMergeBehavior::Error,
"Objective-C Is Simulated",
1 << 5,
);
}
llvm::add_module_flag_u32(
self.llmod,
llvm::ModuleFlagMergeBehavior::Error,
"Objective-C Class Properties",
1 << 6,
);
}
}
impl<'ll> SimpleCx<'ll> {
pub(crate) fn get_type_of_global(&self, val: &'ll Value) -> &'ll Type {