public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Question about selectors in the Objective-C ABI
@ 2021-03-03 17:05 Evan Bowman
  2021-03-03 19:19 ` Evan Bowman
  0 siblings, 1 reply; 3+ messages in thread
From: Evan Bowman @ 2021-03-03 17:05 UTC (permalink / raw)
  To: gcc-help

Hello,

Yesterday, I was playing around with objective-c and GCC, and I have a
question about module loading and selectors.

Given this minimal sample code:
```
#import "Object.h"
#include <stdio.h>

@interface HelloWorld:Object {
}
+(void) hello: (const char*) param;
+(void) world: (const char*) param;
@end

@implementation HelloWorld
+(void) hello: (const char*) param {
    printf("hello %s\n", param);
}

+(void) world: (const char*) param {
    printf("world %s\n", param);
}
@end

int main()
{
    [HelloWorld hello:"some text"];
}
```

I am able to get the code to compile and link with my own implementations
of the objective-c runtime ABI functions, but I'm confused about how to
determine the string name of a selector attached to a method definition.
For example, I have this C code, which I run upon sending the hello:
message to class HelloWorld, in the above example:

void debug_walk_class(struct objc_class_gsv1* class, int depth)
{
    printf("name: %s, info: %zu, inst_size: %zu, abi: %zu\n",
           class->name,
           class->info,
           class->instance_size,
           class->abi_version);

    if (class->methods) {
        struct objc_method_list_gcc* methods = class->methods;

        for (int i = 0; i < methods->count; ++i) {
            struct objc_method_gcc* method = &methods->methods[i];
            printf("method: %s %zu\n",
                   method->types ? method->types : "no types",
                   method->selector->index);
        }
    }

    puts("");

    if (class->isa && depth < 2) {
        debug_walk_class((struct objc_class_gsv1*)class->isa, depth + 1);
    }
}

id objc_msg_lookup(id receiver, SEL selector)
{
    debug_walk_class((struct objc_class_gsv1*)receiver, 0);
}

The output, when walking the class tree, looks like this:
name: (null), info: 0, inst_size: 94483748880412, abi: 1

name: HelloWorld, info: 1, inst_size: 8, abi: 0

name: HelloWorld, info: 2, inst_size: 104, abi: 0
method: v24@0:8r*16 5692614131986886519
method: v24@0:8r*16 8574917940748248424

So, I see the two methods in the metaclass, but when I try to print the
selector->name string rather than selector->index, the code segfaults, so I
guess the selector in this case holds an index rather than a string? Does
anyone know how to determine the string name of the selector attached to an
objc_method_gcc struct? If not, what does the index represent? The selector
passed to objc_msg_lookup, on the other hand, does seem to be a string, I
can print it just fine.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Question about selectors in the Objective-C ABI
  2021-03-03 17:05 Question about selectors in the Objective-C ABI Evan Bowman
@ 2021-03-03 19:19 ` Evan Bowman
  2021-03-03 19:25   ` Evan Bowman
  0 siblings, 1 reply; 3+ messages in thread
From: Evan Bowman @ 2021-03-03 19:19 UTC (permalink / raw)
  To: gcc-help

Never mind! I answered my own question. It seems that for short names, gcc
is doing a small size optimization, by storing string literals in-place
within the selectors, rather than as a pointer to the string.

On Wed, Mar 3, 2021 at 12:05 PM Evan Bowman <evan.a.bowman@gmail.com> wrote:

> Hello,
>
> Yesterday, I was playing around with objective-c and GCC, and I have a
> question about module loading and selectors.
>
> Given this minimal sample code:
> ```
> #import "Object.h"
> #include <stdio.h>
>
> @interface HelloWorld:Object {
> }
> +(void) hello: (const char*) param;
> +(void) world: (const char*) param;
> @end
>
> @implementation HelloWorld
> +(void) hello: (const char*) param {
>     printf("hello %s\n", param);
> }
>
> +(void) world: (const char*) param {
>     printf("world %s\n", param);
> }
> @end
>
> int main()
> {
>     [HelloWorld hello:"some text"];
> }
> ```
>
> I am able to get the code to compile and link with my own implementations
> of the objective-c runtime ABI functions, but I'm confused about how to
> determine the string name of a selector attached to a method definition.
> For example, I have this C code, which I run upon sending the hello:
> message to class HelloWorld, in the above example:
>
> void debug_walk_class(struct objc_class_gsv1* class, int depth)
> {
>     printf("name: %s, info: %zu, inst_size: %zu, abi: %zu\n",
>            class->name,
>            class->info,
>            class->instance_size,
>            class->abi_version);
>
>     if (class->methods) {
>         struct objc_method_list_gcc* methods = class->methods;
>
>         for (int i = 0; i < methods->count; ++i) {
>             struct objc_method_gcc* method = &methods->methods[i];
>             printf("method: %s %zu\n",
>                    method->types ? method->types : "no types",
>                    method->selector->index);
>         }
>     }
>
>     puts("");
>
>     if (class->isa && depth < 2) {
>         debug_walk_class((struct objc_class_gsv1*)class->isa, depth + 1);
>     }
> }
>
> id objc_msg_lookup(id receiver, SEL selector)
> {
>     debug_walk_class((struct objc_class_gsv1*)receiver, 0);
> }
>
> The output, when walking the class tree, looks like this:
> name: (null), info: 0, inst_size: 94483748880412, abi: 1
>
> name: HelloWorld, info: 1, inst_size: 8, abi: 0
>
> name: HelloWorld, info: 2, inst_size: 104, abi: 0
> method: v24@0:8r*16 5692614131986886519
> method: v24@0:8r*16 8574917940748248424
>
> So, I see the two methods in the metaclass, but when I try to print the
> selector->name string rather than selector->index, the code segfaults, so I
> guess the selector in this case holds an index rather than a string? Does
> anyone know how to determine the string name of the selector attached to an
> objc_method_gcc struct? If not, what does the index represent? The selector
> passed to objc_msg_lookup, on the other hand, does seem to be a string, I
> can print it just fine.
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Question about selectors in the Objective-C ABI
  2021-03-03 19:19 ` Evan Bowman
@ 2021-03-03 19:25   ` Evan Bowman
  0 siblings, 0 replies; 3+ messages in thread
From: Evan Bowman @ 2021-03-03 19:25 UTC (permalink / raw)
  To: gcc-help

Oh, perhaps not, maybe I'm still confused.

On Wed, Mar 3, 2021 at 2:19 PM Evan Bowman <evan.a.bowman@gmail.com> wrote:

> Never mind! I answered my own question. It seems that for short names, gcc
> is doing a small size optimization, by storing string literals in-place
> within the selectors, rather than as a pointer to the string.
>
> On Wed, Mar 3, 2021 at 12:05 PM Evan Bowman <evan.a.bowman@gmail.com>
> wrote:
>
>> Hello,
>>
>> Yesterday, I was playing around with objective-c and GCC, and I have a
>> question about module loading and selectors.
>>
>> Given this minimal sample code:
>> ```
>> #import "Object.h"
>> #include <stdio.h>
>>
>> @interface HelloWorld:Object {
>> }
>> +(void) hello: (const char*) param;
>> +(void) world: (const char*) param;
>> @end
>>
>> @implementation HelloWorld
>> +(void) hello: (const char*) param {
>>     printf("hello %s\n", param);
>> }
>>
>> +(void) world: (const char*) param {
>>     printf("world %s\n", param);
>> }
>> @end
>>
>> int main()
>> {
>>     [HelloWorld hello:"some text"];
>> }
>> ```
>>
>> I am able to get the code to compile and link with my own implementations
>> of the objective-c runtime ABI functions, but I'm confused about how to
>> determine the string name of a selector attached to a method definition.
>> For example, I have this C code, which I run upon sending the hello:
>> message to class HelloWorld, in the above example:
>>
>> void debug_walk_class(struct objc_class_gsv1* class, int depth)
>> {
>>     printf("name: %s, info: %zu, inst_size: %zu, abi: %zu\n",
>>            class->name,
>>            class->info,
>>            class->instance_size,
>>            class->abi_version);
>>
>>     if (class->methods) {
>>         struct objc_method_list_gcc* methods = class->methods;
>>
>>         for (int i = 0; i < methods->count; ++i) {
>>             struct objc_method_gcc* method = &methods->methods[i];
>>             printf("method: %s %zu\n",
>>                    method->types ? method->types : "no types",
>>                    method->selector->index);
>>         }
>>     }
>>
>>     puts("");
>>
>>     if (class->isa && depth < 2) {
>>         debug_walk_class((struct objc_class_gsv1*)class->isa, depth + 1);
>>     }
>> }
>>
>> id objc_msg_lookup(id receiver, SEL selector)
>> {
>>     debug_walk_class((struct objc_class_gsv1*)receiver, 0);
>> }
>>
>> The output, when walking the class tree, looks like this:
>> name: (null), info: 0, inst_size: 94483748880412, abi: 1
>>
>> name: HelloWorld, info: 1, inst_size: 8, abi: 0
>>
>> name: HelloWorld, info: 2, inst_size: 104, abi: 0
>> method: v24@0:8r*16 5692614131986886519
>> method: v24@0:8r*16 8574917940748248424
>>
>> So, I see the two methods in the metaclass, but when I try to print the
>> selector->name string rather than selector->index, the code segfaults, so I
>> guess the selector in this case holds an index rather than a string? Does
>> anyone know how to determine the string name of the selector attached to an
>> objc_method_gcc struct? If not, what does the index represent? The selector
>> passed to objc_msg_lookup, on the other hand, does seem to be a string, I
>> can print it just fine.
>>
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-03-03 19:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-03 17:05 Question about selectors in the Objective-C ABI Evan Bowman
2021-03-03 19:19 ` Evan Bowman
2021-03-03 19:25   ` Evan Bowman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).