Skip to content

Commit dd97696

Browse files
committed
Support macOS 32-bit with unstable-static-class
1 parent a7b3ff6 commit dd97696

File tree

9 files changed

+438
-209
lines changed

9 files changed

+438
-209
lines changed

helper-scripts/test-local.fish

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ rm -d .cargo
3434
export SDKROOT=(pwd)/ideas/MacOSX10.13.sdk
3535
export CARGO_BUILD_TARGET=i686-apple-darwin
3636
cargo +nightly test -Zbuild-std
37-
cargo +nightly test -Zbuild-std --features malloc,block,exception,catch-all,verify_message
37+
cargo +nightly test -Zbuild-std --features malloc,block,exception,catch-all,verify_message,unstable-static-class,unstable-static-sel
3838
cargo +nightly test -Zbuild-std --release

objc2/src/macros.rs

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@ macro_rules! __sel_inner {
208208
#[doc(hidden)]
209209
#[macro_export]
210210
macro_rules! __inner_statics_apple_generic {
211+
{
212+
@string_to_known_length_bytes;
213+
$x:ident;
214+
} => {{
215+
// Convert the `&[u8]` slice to an array with known length, so
216+
// that we can place that directly in a static.
217+
let mut res: [u8; $x.len()] = [0; $x.len()];
218+
let mut i = 0;
219+
while i < $x.len() {
220+
res[i] = $x[i];
221+
i += 1;
222+
}
223+
res
224+
}};
211225
{
212226
@image_info;
213227
$image_info_section:literal;
@@ -248,16 +262,9 @@ macro_rules! __inner_statics_apple_generic {
248262
"\x01L_OBJC_METH_VAR_NAME_",
249263
$crate::__macro_helpers::__hash_idents!($($idents)+),
250264
)]
251-
static NAME_DATA: [u8; X.len()] = {
252-
// Convert the `&[u8]` slice to an array with known length, so
253-
// that we can place that directly in a static.
254-
let mut res: [u8; X.len()] = [0; X.len()];
255-
let mut i = 0;
256-
while i < X.len() {
257-
res[i] = X[i];
258-
i += 1;
259-
}
260-
res
265+
static NAME_DATA: [u8; X.len()] = $crate::__inner_statics_apple_generic! {
266+
@string_to_known_length_bytes;
267+
X;
261268
};
262269

263270
/// Place the constant value in the correct section.
@@ -273,10 +280,6 @@ macro_rules! __inner_statics_apple_generic {
273280
/// `static mut` is used so that we don't need to wrap the
274281
/// `UnsafeCell` in something that implements `Sync`.
275282
///
276-
/// Clang uses `no_dead_strip` in the link section for some reason,
277-
/// which other tools (notably some LLVM tools) now assume is present,
278-
/// so we have to add it as well.
279-
///
280283
///
281284
/// # Safety
282285
///
@@ -298,7 +301,6 @@ macro_rules! __inner_statics_apple_generic {
298301
};
299302
{
300303
@class;
301-
$class_ref_section:literal;
302304
$name:ident
303305
} => {
304306
use $crate::__macro_helpers::UnsafeCell;
@@ -330,8 +332,8 @@ macro_rules! __inner_statics_apple_generic {
330332
static CLASS: Class;
331333
}
332334

333-
/// SAFETY: Same as `REF` above.
334-
#[link_section = $class_ref_section]
335+
/// SAFETY: Same as `REF` above in `@sel`.
336+
#[link_section = "__DATA,__objc_classrefs,regular,no_dead_strip"]
335337
#[export_name = $crate::__macro_helpers::concat!(
336338
"\x01L_OBJC_CLASSLIST_REFERENCES_$_",
337339
$crate::__macro_helpers::__hash_idents!($name),
@@ -340,6 +342,39 @@ macro_rules! __inner_statics_apple_generic {
340342
UnsafeCell::new(&CLASS)
341343
};
342344
};
345+
{
346+
@class_old;
347+
$name:ident
348+
} => {
349+
use $crate::__macro_helpers::{u8, UnsafeCell};
350+
use $crate::runtime::Class;
351+
352+
const X: &[u8] = $crate::__macro_helpers::stringify!($name).as_bytes();
353+
354+
/// Similar to NAME_DATA above in `@sel`.
355+
#[link_section = "__TEXT,__cstring,cstring_literals"]
356+
#[export_name = $crate::__macro_helpers::concat!(
357+
"\x01L_OBJC_CLASS_NAME_",
358+
$crate::__macro_helpers::__hash_idents!($name),
359+
)]
360+
static NAME_DATA: [u8; X.len()] = $crate::__inner_statics_apple_generic! {
361+
@string_to_known_length_bytes;
362+
X;
363+
};
364+
365+
/// SAFETY: Same as `REF` above in `@sel`.
366+
#[link_section = "__OBJC,__cls_refs,literal_pointers,no_dead_strip"]
367+
#[export_name = $crate::__macro_helpers::concat!(
368+
"\x01L_OBJC_CLASS_REFERENCES_",
369+
$crate::__macro_helpers::__hash_idents!($name),
370+
)]
371+
static mut REF: UnsafeCell<&Class> = unsafe {
372+
let ptr: *const Class = NAME_DATA.as_ptr().cast();
373+
UnsafeCell::new(&*ptr)
374+
};
375+
376+
// TODO: module info?
377+
}
343378
}
344379

345380
// These sections are found by reading clang/LLVM sources
@@ -358,14 +393,16 @@ macro_rules! __inner_statics {
358393
$crate::__inner_statics_apple_generic! {
359394
@sel;
360395
"__TEXT,__objc_methname,cstring_literals";
396+
// Clang uses `no_dead_strip` in the link section for some reason,
397+
// which other tools (notably some LLVM tools) now assume is
398+
// present, so we have to add it as well.
361399
"__DATA,__objc_selrefs,literal_pointers,no_dead_strip";
362400
$($args)*
363401
}
364402
};
365403
(@class $($args:tt)*) => {
366404
$crate::__inner_statics_apple_generic! {
367405
@class;
368-
"__DATA,__objc_classrefs,regular,no_dead_strip";
369406
$($args)*
370407
}
371408
};
@@ -391,11 +428,10 @@ macro_rules! __inner_statics {
391428
}
392429
};
393430
(@class $($args:tt)*) => {
394-
// TODO
395-
$crate::__macro_helpers::compile_error!(
396-
"The `\"unstable-static-class\"` feature is not yet supported on 32bit macOS!"
397-
)
398-
// TODO: module info
431+
$crate::__inner_statics_apple_generic! {
432+
@class_old;
433+
$($args)*
434+
}
399435
};
400436
}
401437

test-assembly/crates/test_static_class/expected/apple-aarch64.s

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@
33
.p2align 2
44
_get_class:
55
Lloh0:
6-
adrp x8, L_OBJC_CLASSLIST_REFERENCES_$_e0796f2f86586929@PAGE
6+
adrp x8, L_OBJC_CLASSLIST_REFERENCES_$_9657804a2a54ab6f@PAGE
77
Lloh1:
8-
ldr x0, [x8, L_OBJC_CLASSLIST_REFERENCES_$_e0796f2f86586929@PAGEOFF]
8+
ldr x0, [x8, L_OBJC_CLASSLIST_REFERENCES_$_9657804a2a54ab6f@PAGEOFF]
99
ret
1010
.loh AdrpLdr Lloh0, Lloh1
1111

1212
.globl _get_same_class
1313
.p2align 2
1414
_get_same_class:
1515
Lloh2:
16-
adrp x8, L_OBJC_CLASSLIST_REFERENCES_$_1121b6b8519ae283@PAGE
16+
adrp x8, L_OBJC_CLASSLIST_REFERENCES_$_e897a41b218dcf79@PAGE
1717
Lloh3:
18-
ldr x0, [x8, L_OBJC_CLASSLIST_REFERENCES_$_1121b6b8519ae283@PAGEOFF]
18+
ldr x0, [x8, L_OBJC_CLASSLIST_REFERENCES_$_e897a41b218dcf79@PAGEOFF]
1919
ret
2020
.loh AdrpLdr Lloh2, Lloh3
2121

2222
.globl _get_different_class
2323
.p2align 2
2424
_get_different_class:
2525
Lloh4:
26-
adrp x8, L_OBJC_CLASSLIST_REFERENCES_$_7314246f93b118b1@PAGE
26+
adrp x8, L_OBJC_CLASSLIST_REFERENCES_$_3b7780b4dcfcb9d4@PAGE
2727
Lloh5:
28-
ldr x0, [x8, L_OBJC_CLASSLIST_REFERENCES_$_7314246f93b118b1@PAGEOFF]
28+
ldr x0, [x8, L_OBJC_CLASSLIST_REFERENCES_$_3b7780b4dcfcb9d4@PAGEOFF]
2929
ret
3030
.loh AdrpLdr Lloh4, Lloh5
3131

@@ -38,21 +38,21 @@ _unused_sel:
3838
.p2align 2
3939
_use_fns:
4040
Lloh6:
41-
adrp x9, L_OBJC_CLASSLIST_REFERENCES_$_e0796f2f86586929@PAGE
41+
adrp x9, L_OBJC_CLASSLIST_REFERENCES_$_9657804a2a54ab6f@PAGE
4242
Lloh7:
43-
ldr x9, [x9, L_OBJC_CLASSLIST_REFERENCES_$_e0796f2f86586929@PAGEOFF]
43+
ldr x9, [x9, L_OBJC_CLASSLIST_REFERENCES_$_9657804a2a54ab6f@PAGEOFF]
4444
Lloh8:
45-
adrp x10, L_OBJC_CLASSLIST_REFERENCES_$_1121b6b8519ae283@PAGE
45+
adrp x10, L_OBJC_CLASSLIST_REFERENCES_$_e897a41b218dcf79@PAGE
4646
Lloh9:
47-
ldr x10, [x10, L_OBJC_CLASSLIST_REFERENCES_$_1121b6b8519ae283@PAGEOFF]
47+
ldr x10, [x10, L_OBJC_CLASSLIST_REFERENCES_$_e897a41b218dcf79@PAGEOFF]
4848
Lloh10:
49-
adrp x11, L_OBJC_CLASSLIST_REFERENCES_$_7314246f93b118b1@PAGE
49+
adrp x11, L_OBJC_CLASSLIST_REFERENCES_$_3b7780b4dcfcb9d4@PAGE
5050
Lloh11:
51-
ldr x11, [x11, L_OBJC_CLASSLIST_REFERENCES_$_7314246f93b118b1@PAGEOFF]
51+
ldr x11, [x11, L_OBJC_CLASSLIST_REFERENCES_$_3b7780b4dcfcb9d4@PAGEOFF]
5252
Lloh12:
53-
adrp x12, L_OBJC_CLASSLIST_REFERENCES_$_16e72be3e810c687@PAGE
53+
adrp x12, L_OBJC_CLASSLIST_REFERENCES_$_2f45d8445f72bd9b@PAGE
5454
Lloh13:
55-
ldr x12, [x12, L_OBJC_CLASSLIST_REFERENCES_$_16e72be3e810c687@PAGEOFF]
55+
ldr x12, [x12, L_OBJC_CLASSLIST_REFERENCES_$_2f45d8445f72bd9b@PAGEOFF]
5656
stp x9, x10, [x8]
5757
stp x11, x12, [x8, #16]
5858
ret
@@ -65,9 +65,9 @@ Lloh13:
6565
.p2align 2
6666
_use_same_twice:
6767
Lloh14:
68-
adrp x9, L_OBJC_CLASSLIST_REFERENCES_$_e0796f2f86586929@PAGE
68+
adrp x9, L_OBJC_CLASSLIST_REFERENCES_$_9657804a2a54ab6f@PAGE
6969
Lloh15:
70-
ldr x9, [x9, L_OBJC_CLASSLIST_REFERENCES_$_e0796f2f86586929@PAGEOFF]
70+
ldr x9, [x9, L_OBJC_CLASSLIST_REFERENCES_$_9657804a2a54ab6f@PAGEOFF]
7171
stp x9, x9, [x8]
7272
ret
7373
.loh AdrpLdr Lloh14, Lloh15
@@ -78,75 +78,75 @@ _use_in_loop:
7878
ret
7979

8080
.section __DATA,__objc_imageinfo,regular,no_dead_strip
81-
.globl L_OBJC_IMAGE_INFO_e0796f2f86586929
81+
.globl L_OBJC_IMAGE_INFO_9657804a2a54ab6f
8282
.p2align 2
83-
L_OBJC_IMAGE_INFO_e0796f2f86586929:
83+
L_OBJC_IMAGE_INFO_9657804a2a54ab6f:
8484
.asciz "\000\000\000\000@\000\000"
8585

8686
.section __DATA,__objc_classrefs,regular,no_dead_strip
87-
.globl L_OBJC_CLASSLIST_REFERENCES_$_e0796f2f86586929
87+
.globl L_OBJC_CLASSLIST_REFERENCES_$_9657804a2a54ab6f
8888
.p2align 3
89-
L_OBJC_CLASSLIST_REFERENCES_$_e0796f2f86586929:
89+
L_OBJC_CLASSLIST_REFERENCES_$_9657804a2a54ab6f:
9090
.quad _OBJC_CLASS_$_NSObject
9191

9292
.section __DATA,__objc_imageinfo,regular,no_dead_strip
93-
.globl L_OBJC_IMAGE_INFO_1121b6b8519ae283
93+
.globl L_OBJC_IMAGE_INFO_e897a41b218dcf79
9494
.p2align 2
95-
L_OBJC_IMAGE_INFO_1121b6b8519ae283:
95+
L_OBJC_IMAGE_INFO_e897a41b218dcf79:
9696
.asciz "\000\000\000\000@\000\000"
9797

9898
.section __DATA,__objc_classrefs,regular,no_dead_strip
99-
.globl L_OBJC_CLASSLIST_REFERENCES_$_1121b6b8519ae283
99+
.globl L_OBJC_CLASSLIST_REFERENCES_$_e897a41b218dcf79
100100
.p2align 3
101-
L_OBJC_CLASSLIST_REFERENCES_$_1121b6b8519ae283:
101+
L_OBJC_CLASSLIST_REFERENCES_$_e897a41b218dcf79:
102102
.quad _OBJC_CLASS_$_NSObject
103103

104104
.section __DATA,__objc_imageinfo,regular,no_dead_strip
105-
.globl L_OBJC_IMAGE_INFO_7314246f93b118b1
105+
.globl L_OBJC_IMAGE_INFO_3b7780b4dcfcb9d4
106106
.p2align 2
107-
L_OBJC_IMAGE_INFO_7314246f93b118b1:
107+
L_OBJC_IMAGE_INFO_3b7780b4dcfcb9d4:
108108
.asciz "\000\000\000\000@\000\000"
109109

110110
.section __DATA,__objc_classrefs,regular,no_dead_strip
111-
.globl L_OBJC_CLASSLIST_REFERENCES_$_7314246f93b118b1
111+
.globl L_OBJC_CLASSLIST_REFERENCES_$_3b7780b4dcfcb9d4
112112
.p2align 3
113-
L_OBJC_CLASSLIST_REFERENCES_$_7314246f93b118b1:
113+
L_OBJC_CLASSLIST_REFERENCES_$_3b7780b4dcfcb9d4:
114114
.quad _OBJC_CLASS_$_NSString
115115

116116
.section __DATA,__objc_imageinfo,regular,no_dead_strip
117-
.globl L_OBJC_IMAGE_INFO_f5b45e093b02d6c9
117+
.globl L_OBJC_IMAGE_INFO_f2fb7c579d3c0a74
118118
.p2align 2
119-
L_OBJC_IMAGE_INFO_f5b45e093b02d6c9:
119+
L_OBJC_IMAGE_INFO_f2fb7c579d3c0a74:
120120
.asciz "\000\000\000\000@\000\000"
121121

122122
.section __DATA,__objc_classrefs,regular,no_dead_strip
123-
.globl L_OBJC_CLASSLIST_REFERENCES_$_f5b45e093b02d6c9
123+
.globl L_OBJC_CLASSLIST_REFERENCES_$_f2fb7c579d3c0a74
124124
.p2align 3
125-
L_OBJC_CLASSLIST_REFERENCES_$_f5b45e093b02d6c9:
125+
L_OBJC_CLASSLIST_REFERENCES_$_f2fb7c579d3c0a74:
126126
.quad _OBJC_CLASS_$_NSData
127127

128128
.section __DATA,__objc_imageinfo,regular,no_dead_strip
129-
.globl L_OBJC_IMAGE_INFO_16e72be3e810c687
129+
.globl L_OBJC_IMAGE_INFO_2f45d8445f72bd9b
130130
.p2align 2
131-
L_OBJC_IMAGE_INFO_16e72be3e810c687:
131+
L_OBJC_IMAGE_INFO_2f45d8445f72bd9b:
132132
.asciz "\000\000\000\000@\000\000"
133133

134134
.section __DATA,__objc_classrefs,regular,no_dead_strip
135-
.globl L_OBJC_CLASSLIST_REFERENCES_$_16e72be3e810c687
135+
.globl L_OBJC_CLASSLIST_REFERENCES_$_2f45d8445f72bd9b
136136
.p2align 3
137-
L_OBJC_CLASSLIST_REFERENCES_$_16e72be3e810c687:
137+
L_OBJC_CLASSLIST_REFERENCES_$_2f45d8445f72bd9b:
138138
.quad _OBJC_CLASS_$_NSException
139139

140140
.section __DATA,__objc_imageinfo,regular,no_dead_strip
141-
.globl L_OBJC_IMAGE_INFO_4465b4c2c67ae674
141+
.globl L_OBJC_IMAGE_INFO_3bf610c78df2b6bb
142142
.p2align 2
143-
L_OBJC_IMAGE_INFO_4465b4c2c67ae674:
143+
L_OBJC_IMAGE_INFO_3bf610c78df2b6bb:
144144
.asciz "\000\000\000\000@\000\000"
145145

146146
.section __DATA,__objc_classrefs,regular,no_dead_strip
147-
.globl L_OBJC_CLASSLIST_REFERENCES_$_4465b4c2c67ae674
147+
.globl L_OBJC_CLASSLIST_REFERENCES_$_3bf610c78df2b6bb
148148
.p2align 3
149-
L_OBJC_CLASSLIST_REFERENCES_$_4465b4c2c67ae674:
149+
L_OBJC_CLASSLIST_REFERENCES_$_3bf610c78df2b6bb:
150150
.quad _OBJC_CLASS_$_NSLock
151151

152152
.subsections_via_symbols

0 commit comments

Comments
 (0)