From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) by sourceware.org (Postfix) with ESMTPS id E6E1C386102D for ; Wed, 3 Mar 2021 19:19:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org E6E1C386102D Received: by mail-lj1-x22f.google.com with SMTP id u18so16540848ljd.3 for ; Wed, 03 Mar 2021 11:19:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=rb0m3DRNNZ3W7dkXmtB+vh3KgT60Mq0q/nkEdt9Ls60=; b=mTOzZVvnz4NDa3mJ/Rgnn60SWlhDlrkJ7Ts7xSE8p9LMdGRh4MXSHeFmusFYFlxpfe xtCwh6pZ/VQMasTWttFMslQ6Ka0vB1q/JDcLU96dECIf7DSXAieuQrLC4A3gdnYZuRbn mDj8g42S+p3XMYZ2pRGAJ1PS8OFIo5JDF9eCm6o9tyA/BZnVm58/bYxYQgao5RbDjcMB +nDgZqoV+IBBqWXqvE+C9+AiUkiZXEDwBjJGUSQWGFm17eyBMl8EXKIFpNiRXHLW2PbK z/XIJCYY8x0SOub0C7rrilw740iB/W+RhipnZSozzAP2sn4PcFZpaTqQgsSqqXPOuU20 yJkg== X-Gm-Message-State: AOAM532g88Ik2YM3wDAXdqsNAfcDH+HYFRMUZP6l89axjb0Sx/TwB5KA Crx6Gr1cj0bDZgJ70An2yBCqCf3rhNzjF0TQ+blwwUvi X-Google-Smtp-Source: ABdhPJzd7xW8T+7hCxorTScnyWXKatkZJYUsN3cU6B811y6MQIcP3T+q/jrm64+ftsv+Hi0BfaqaAatF8RLzaJ3McJ0= X-Received: by 2002:a2e:9e11:: with SMTP id e17mr249076ljk.330.1614799179768; Wed, 03 Mar 2021 11:19:39 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Evan Bowman Date: Wed, 3 Mar 2021 14:19:15 -0500 Message-ID: Subject: Re: Question about selectors in the Objective-C ABI To: gcc-help@gcc.gnu.org X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 03 Mar 2021 19:19:43 -0000 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 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 > > @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. >