Skip to content

Commit

Permalink
Add module info on macOS 32-bit
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jul 27, 2022
1 parent 776f43e commit 251912c
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 2 deletions.
38 changes: 38 additions & 0 deletions objc2/src/__macro_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,35 @@ pub const fn in_selector_family(mut selector: &[u8], mut family: &[u8]) -> bool
}
}

/// Helper struct for emitting the module info that macOS 32-bit requires.
///
/// <https://github.com/llvm/llvm-project/blob/release/13.x/clang/lib/CodeGen/CGObjCMac.cpp#L5211-L5234>
#[repr(C)]
pub struct ModuleInfo {
version: usize,
size: usize,
name: *const u8,
symtab: *const (),
}

// SAFETY: ModuleInfo is immutable.
unsafe impl Sync for ModuleInfo {}

impl ModuleInfo {
/// This is hardcoded in clang as 7.
const VERSION: usize = 7;

pub const fn new(name: *const u8) -> Self {
Self {
version: Self::VERSION,
size: core::mem::size_of::<Self>(),
name,
// We don't expose any symbols
symtab: core::ptr::null(),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -441,4 +470,13 @@ mod tests {
// to be different!
assert_eq!(ident1, ident2);
}

#[test]
#[cfg_attr(
not(all(feature = "apple", target_os = "macos", target_arch = "x86")),
ignore = "Only relevant on macOS 32-bit"
)]
fn ensure_size_of_module_info() {
assert_eq!(core::mem::size_of::<ModuleInfo>(), 16);
}
}
32 changes: 30 additions & 2 deletions objc2/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,32 @@ macro_rules! __inner_statics_apple_generic {
#[used] // Make sure this reaches the linker
static _IMAGE_INFO: $crate::ffi::__ImageInfo = $crate::ffi::__ImageInfo::system();
};
{
@module_info;
$($idents:ident)+
} => {
#[link_section = "__TEXT,__cstring,cstring_literals"]
#[export_name = $crate::__macro_helpers::concat!(
"\x01L_OBJC_CLASS_NAME_",
$crate::__macro_helpers::__hash_idents!($($idents)+),
"_MODULE_INFO"
)]
static MODULE_INFO_NAME: [$crate::__macro_helpers::u8; 1] = [0];

/// Emit module info.
///
/// This is similar to image info, and must be present in the final
/// binary on macOS 32-bit.
#[link_section = "__OBJC,__module_info,regular,no_dead_strip"]
#[export_name = $crate::__macro_helpers::concat!(
"\x01L_OBJC_MODULES_",
$crate::__macro_helpers::__hash_idents!($($idents)+)
)]
#[used] // Make sure this reaches the linker
static _MODULE_INFO: $crate::__macro_helpers::ModuleInfo = $crate::__macro_helpers::ModuleInfo::new(
MODULE_INFO_NAME.as_ptr()
);
};
{
@sel;
$var_name_section:literal;
Expand Down Expand Up @@ -369,8 +395,6 @@ macro_rules! __inner_statics_apple_generic {
let ptr: *const Class = NAME_DATA.as_ptr().cast();
UnsafeCell::new(&*ptr)
};

// TODO: module info?
}
}

Expand Down Expand Up @@ -429,6 +453,10 @@ macro_rules! __inner_statics {
@class_old;
$($args)*
}
$crate::__inner_statics_apple_generic! {
@module_info;
$($args)*
}
};
}

Expand Down
78 changes: 78 additions & 0 deletions test-assembly/crates/test_static_class/expected/apple-old-x86.s
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ L_OBJC_CLASS_NAME_9657804a2a54ab6f:
L_OBJC_CLASS_REFERENCES_9657804a2a54ab6f:
.long L_OBJC_CLASS_NAME_9657804a2a54ab6f

.section __TEXT,__cstring,cstring_literals
.globl L_OBJC_CLASS_NAME_9657804a2a54ab6f_MODULE_INFO
L_OBJC_CLASS_NAME_9657804a2a54ab6f_MODULE_INFO:
.space 1

.section __OBJC,__module_info,regular,no_dead_strip
.globl L_OBJC_MODULES_9657804a2a54ab6f
.p2align 2
L_OBJC_MODULES_9657804a2a54ab6f:
.asciz "\007\000\000\000\020\000\000"
.long L_OBJC_CLASS_NAME_9657804a2a54ab6f_MODULE_INFO
.space 4

.section __OBJC,__image_info
.globl L_OBJC_IMAGE_INFO_e897a41b218dcf79
.p2align 2
Expand All @@ -125,6 +138,19 @@ L_OBJC_CLASS_NAME_e897a41b218dcf79:
L_OBJC_CLASS_REFERENCES_e897a41b218dcf79:
.long L_OBJC_CLASS_NAME_e897a41b218dcf79

.section __TEXT,__cstring,cstring_literals
.globl L_OBJC_CLASS_NAME_e897a41b218dcf79_MODULE_INFO
L_OBJC_CLASS_NAME_e897a41b218dcf79_MODULE_INFO:
.space 1

.section __OBJC,__module_info,regular,no_dead_strip
.globl L_OBJC_MODULES_e897a41b218dcf79
.p2align 2
L_OBJC_MODULES_e897a41b218dcf79:
.asciz "\007\000\000\000\020\000\000"
.long L_OBJC_CLASS_NAME_e897a41b218dcf79_MODULE_INFO
.space 4

.section __OBJC,__image_info
.globl L_OBJC_IMAGE_INFO_3b7780b4dcfcb9d4
.p2align 2
Expand All @@ -142,6 +168,19 @@ L_OBJC_CLASS_NAME_3b7780b4dcfcb9d4:
L_OBJC_CLASS_REFERENCES_3b7780b4dcfcb9d4:
.long L_OBJC_CLASS_NAME_3b7780b4dcfcb9d4

.section __TEXT,__cstring,cstring_literals
.globl L_OBJC_CLASS_NAME_3b7780b4dcfcb9d4_MODULE_INFO
L_OBJC_CLASS_NAME_3b7780b4dcfcb9d4_MODULE_INFO:
.space 1

.section __OBJC,__module_info,regular,no_dead_strip
.globl L_OBJC_MODULES_3b7780b4dcfcb9d4
.p2align 2
L_OBJC_MODULES_3b7780b4dcfcb9d4:
.asciz "\007\000\000\000\020\000\000"
.long L_OBJC_CLASS_NAME_3b7780b4dcfcb9d4_MODULE_INFO
.space 4

.section __OBJC,__image_info
.globl L_OBJC_IMAGE_INFO_f2fb7c579d3c0a74
.p2align 2
Expand All @@ -159,6 +198,19 @@ L_OBJC_CLASS_NAME_f2fb7c579d3c0a74:
L_OBJC_CLASS_REFERENCES_f2fb7c579d3c0a74:
.long L_OBJC_CLASS_NAME_f2fb7c579d3c0a74

.section __TEXT,__cstring,cstring_literals
.globl L_OBJC_CLASS_NAME_f2fb7c579d3c0a74_MODULE_INFO
L_OBJC_CLASS_NAME_f2fb7c579d3c0a74_MODULE_INFO:
.space 1

.section __OBJC,__module_info,regular,no_dead_strip
.globl L_OBJC_MODULES_f2fb7c579d3c0a74
.p2align 2
L_OBJC_MODULES_f2fb7c579d3c0a74:
.asciz "\007\000\000\000\020\000\000"
.long L_OBJC_CLASS_NAME_f2fb7c579d3c0a74_MODULE_INFO
.space 4

.section __OBJC,__image_info
.globl L_OBJC_IMAGE_INFO_2f45d8445f72bd9b
.p2align 2
Expand All @@ -176,6 +228,19 @@ L_OBJC_CLASS_NAME_2f45d8445f72bd9b:
L_OBJC_CLASS_REFERENCES_2f45d8445f72bd9b:
.long L_OBJC_CLASS_NAME_2f45d8445f72bd9b

.section __TEXT,__cstring,cstring_literals
.globl L_OBJC_CLASS_NAME_2f45d8445f72bd9b_MODULE_INFO
L_OBJC_CLASS_NAME_2f45d8445f72bd9b_MODULE_INFO:
.space 1

.section __OBJC,__module_info,regular,no_dead_strip
.globl L_OBJC_MODULES_2f45d8445f72bd9b
.p2align 2
L_OBJC_MODULES_2f45d8445f72bd9b:
.asciz "\007\000\000\000\020\000\000"
.long L_OBJC_CLASS_NAME_2f45d8445f72bd9b_MODULE_INFO
.space 4

.section __OBJC,__image_info
.globl L_OBJC_IMAGE_INFO_3bf610c78df2b6bb
.p2align 2
Expand All @@ -193,4 +258,17 @@ L_OBJC_CLASS_NAME_3bf610c78df2b6bb:
L_OBJC_CLASS_REFERENCES_3bf610c78df2b6bb:
.long L_OBJC_CLASS_NAME_3bf610c78df2b6bb

.section __TEXT,__cstring,cstring_literals
.globl L_OBJC_CLASS_NAME_3bf610c78df2b6bb_MODULE_INFO
L_OBJC_CLASS_NAME_3bf610c78df2b6bb_MODULE_INFO:
.space 1

.section __OBJC,__module_info,regular,no_dead_strip
.globl L_OBJC_MODULES_3bf610c78df2b6bb
.p2align 2
L_OBJC_MODULES_3bf610c78df2b6bb:
.asciz "\007\000\000\000\020\000\000"
.long L_OBJC_CLASS_NAME_3bf610c78df2b6bb_MODULE_INFO
.space 4

.subsections_via_symbols

0 comments on commit 251912c

Please sign in to comment.