Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How does your message sending work? #1

Closed
madsmtm opened this issue May 2, 2023 · 6 comments
Closed

Question: How does your message sending work? #1

madsmtm opened this issue May 2, 2023 · 6 comments
Labels
question Further information is requested

Comments

@madsmtm
Copy link
Collaborator

madsmtm commented May 2, 2023

I see that you're emitting a call to a objc_msgSend function, but you somehow move the selector into the link name. How does that work? I assume it's dyld magic, but do you have some documentation on it, and some information on since when it has been supported?

extern \"C\" {{
#[link_name = \"objc_msgSend${extern_name}\"]
fn msg_send();
}}
unsafe {{
let fn_ptr = msg_send as *const std::ffi::c_void;
let sig: extern \"C\" fn{fn_args} {ret} = std::mem::transmute(fn_ptr);
{call_args}
}}

@madsmtm madsmtm added the question Further information is requested label May 2, 2023
@yury
Copy link
Owner

yury commented May 2, 2023

Starting with Xcode 14 Apple changed how they generate code for selectors. More info at WWDC video 3:10.

I noticed that change, tested and rewrote my old msg_send to new one.

// clang -ObjC -S test.h
#import <Foundation/Foundation.h>

void test(void) {
  NSNumber * num = [NSNumber numberWithBool:YES];
}
        .section        __TEXT,__text,regular,pure_instructions
        .build_version macos, 13, 0     sdk_version 13, 3
        .globl  _test                           ; -- Begin function test
        .p2align        2
_test:                                  ; @test
        .cfi_startproc
; %bb.0:
        sub     sp, sp, #32
        .cfi_def_cfa_offset 32
        stp     x29, x30, [sp, #16]             ; 16-byte Folded Spill
        add     x29, sp, #16
        .cfi_def_cfa w29, 16
        .cfi_offset w30, -8
        .cfi_offset w29, -16
                                        ; implicit-def: $x1
        adrp    x8, _OBJC_CLASSLIST_REFERENCES_$_@PAGE
        ldr     x0, [x8, _OBJC_CLASSLIST_REFERENCES_$_@PAGEOFF]
        mov     w8, #1
        and     w2, w8, #0x1
        bl      "_objc_msgSend$numberWithBool:"
        str     x0, [sp, #8]
        ldp     x29, x30, [sp, #16]             ; 16-byte Folded Reload
        add     sp, sp, #32
        ret
        .cfi_endproc
                                        ; -- End function
        .section        __DATA,__objc_classrefs,regular,no_dead_strip
        .p2align        3                               ; @"OBJC_CLASSLIST_REFERENCES_$_"
_OBJC_CLASSLIST_REFERENCES_$_:
        .quad   _OBJC_CLASS_$_NSNumber

        .section        __DATA,__objc_imageinfo,regular,no_dead_strip
L_OBJC_IMAGE_INFO:
        .long   0
        .long   64

.subsections_via_symbols

One downside - they turn it as optimization. So I have to add opt-level = 1

Another - this is very platform specific, but I'm ok with that if we get zero cost.

@yury
Copy link
Owner

yury commented May 3, 2023

Also implement objc_release optimization.

@madsmtm
Copy link
Collaborator Author

madsmtm commented May 5, 2023

Oh that's very, very cool! Thanks for the video link, I'll probably incorporate something similar into objc2 soon, behind an experimental feature-flag.

Do you know if it only works on Aarch64, or are these also available for x86_64?

@yury
Copy link
Owner

yury commented May 5, 2023

Not sure about 86_64.

@madsmtm
Copy link
Collaborator Author

madsmtm commented Jun 16, 2023

I've implemented parts of this in madsmtm/objc2#462, the message sending changes will take a bit more to do.

yury added a commit that referenced this issue Jun 17, 2023
@yury
Copy link
Owner

yury commented May 16, 2024

Closing this issue. Feel free to reopen if needed.

@yury yury closed this as completed May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants