2 * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Utilities for registering and looking up selectors. The sole
26 * purpose of the selector tables is a registry whereby there is
27 * exactly one address (selector) associated with a given string
32 #include "objc-private.h"
33 #include "objc-auto.h"
34 #include "objc-sel-set.h"
37 #include "objc-selopt.h"
43 // opt: the actual opt used at runtime
44 // builtins: the actual selector table used at runtime
45 // _objc_opt_data: opt data possibly written by dyld
46 // empty_opt_data: empty data to use if dyld didn't cooperate or DisablePreopt
47 using namespace objc_opt;
48 static const objc_selopt_t *builtins = NULL;
49 static const objc_opt_t *opt = NULL;
50 static BOOL preoptimized;
52 extern const objc_opt_t _objc_opt_data; // in __TEXT, __objc_selopt
53 static const uint32_t empty_opt_data[] = OPT_INITIALIZER;
57 #define NUM_NONBUILTIN_SELS 3500
58 // objc_sel_set grows at 3571, 5778, 9349.
59 // Most apps use 2000..7000 extra sels. Most apps will grow zero to two times.
61 static const char *_objc_empty_selector = "";
62 static struct __objc_sel_set *_objc_selectors = NULL;
66 PRIVATE_EXTERN void dump_builtins(void)
68 uint32_t occupied = builtins->occupied;
69 uint32_t capacity = builtins->capacity;
71 _objc_inform("BUILTIN SELECTORS: %d selectors", occupied);
72 _objc_inform("BUILTIN SELECTORS: %d/%d (%d%%) hash table occupancy",
73 occupied, capacity, (int)(occupied/(double)capacity * 100));
74 _objc_inform("BUILTIN SELECTORS: using __TEXT,__objc_selopt at %p",
76 _objc_inform("BUILTIN SELECTORS: capacity: %u", builtins->capacity);
77 _objc_inform("BUILTIN SELECTORS: occupied: %u", builtins->occupied);
78 _objc_inform("BUILTIN SELECTORS: shift: %u", builtins->shift);
79 _objc_inform("BUILTIN SELECTORS: mask: 0x%x", builtins->mask);
80 _objc_inform("BUILTIN SELECTORS: zero: %u", builtins->zero);
81 _objc_inform("BUILTIN SELECTORS: salt: 0x%llx", builtins->salt);
83 const int32_t *offsets = builtins->offsets();
85 for (i = 0; i < capacity; i++) {
86 if (offsets[i] != offsetof(objc_selopt_t, zero)) {
87 const char *str = (const char *)builtins + offsets[i];
88 _objc_inform("BUILTIN SELECTORS: %6d: %+8d %s",
91 _objc_inform("BUILTIN SELECTORS: %6d: ", i);
98 static SEL _objc_search_builtins(const char *key)
100 #if defined(DUMP_SELECTORS)
101 if (NULL != key) printf("\t\"%s\",\n", key);
104 if (!key) return (SEL)0;
105 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
106 if ((uintptr_t)key == kIgnore) return (SEL)kIgnore;
107 if (ignoreSelectorNamed(key)) return (SEL)kIgnore;
109 if ('\0' == *key) return (SEL)_objc_empty_selector;
112 return (SEL)builtins->get(key);
119 const char *sel_getName(SEL sel) {
120 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
121 if ((uintptr_t)sel == kIgnore) return "<ignored selector>";
123 return sel ? (const char *)sel : "<null selector>";
127 BOOL sel_isMapped(SEL name)
131 if (!name) return NO;
132 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
133 if ((uintptr_t)name == kIgnore) return YES;
136 result = _objc_search_builtins((const char *)name);
137 if (result) return YES;
139 rwlock_read(&selLock);
140 if (_objc_selectors) {
141 result = __objc_sel_set_get(_objc_selectors, name);
143 rwlock_unlock_read(&selLock);
144 return result ? YES : NO;
147 static SEL __sel_registerName(const char *name, int lock, int copy)
151 if (lock) rwlock_assert_unlocked(&selLock);
152 else rwlock_assert_writing(&selLock);
154 if (!name) return (SEL)0;
155 result = _objc_search_builtins(name);
156 if (result) return result;
158 if (lock) rwlock_read(&selLock);
159 if (_objc_selectors) {
160 result = __objc_sel_set_get(_objc_selectors, (SEL)name);
162 if (lock) rwlock_unlock_read(&selLock);
163 if (result) return result;
167 if (lock) rwlock_write(&selLock);
169 if (!_objc_selectors) {
170 _objc_selectors = __objc_sel_set_create(NUM_NONBUILTIN_SELS);
173 // Rescan in case it was added while we dropped the lock
174 result = __objc_sel_set_get(_objc_selectors, (SEL)name);
177 result = (SEL)(copy ? _strdup_internal(name) : name);
178 __objc_sel_set_add(_objc_selectors, result);
179 #if defined(DUMP_UNKNOWN_SELECTORS)
180 printf("\t\"%s\",\n", name);
184 if (lock) rwlock_unlock_write(&selLock);
189 SEL sel_registerName(const char *name) {
190 return __sel_registerName(name, 1, 1); // YES lock, YES copy
193 PRIVATE_EXTERN SEL sel_registerNameNoLock(const char *name, BOOL copy) {
194 return __sel_registerName(name, 0, copy); // NO lock, maybe copy
197 PRIVATE_EXTERN void sel_lock(void)
199 rwlock_write(&selLock);
202 PRIVATE_EXTERN void sel_unlock(void)
204 rwlock_unlock_write(&selLock);
209 // the majority of uses of this function (which used to return NULL if not found)
210 // did not check for NULL, so, in fact, never return NULL
212 SEL sel_getUid(const char *name) {
213 return __sel_registerName(name, 2, 1); // YES lock, YES copy
217 BOOL sel_isEqual(SEL lhs, SEL rhs)
219 return (lhs == rhs) ? YES : NO;
223 /***********************************************************************
224 * sel_preoptimizationValid
225 * Return YES if this image's selector fixups are valid courtesy
226 * of the dyld shared cache.
227 **********************************************************************/
228 PRIVATE_EXTERN BOOL sel_preoptimizationValid(const header_info *hi)
230 #if !SUPPORT_BUILTINS
236 # if SUPPORT_IGNORED_SELECTOR_CONSTANT
237 // shared cache can't fix constant ignored selectors
238 if (UseGC) return NO;
241 // preoptimization disabled for some reason
242 if (!preoptimized) return NO;
244 // image not from shared cache, or not fixed inside shared cache
245 if (!_objcHeaderOptimizedByDyld(hi)) return NO;
253 /***********************************************************************
255 * Initialize selector tables and register selectors used internally.
256 **********************************************************************/
257 PRIVATE_EXTERN void sel_init(BOOL wantsGC)
259 #if !SUPPORT_BUILTINS
261 disableSharedCacheOptimizations();
264 // not set at compile time in order to detect too-early selector operations
265 const char *failure = NULL;
266 opt = &_objc_opt_data;
269 // OBJC_DISABLE_PREOPTIMIZATION is set
270 // If opt->version != VERSION then you continue at your own risk.
271 failure = "(by OBJC_DISABLE_PREOPTIMIZATION)";
273 else if (opt->version != objc_opt::VERSION) {
274 // This shouldn't happen. You probably forgot to
275 // change OPT_INITIALIZER and objc-sel-table.s.
276 // If dyld really did write the wrong optimization version,
277 // then we must halt because we don't know what bits dyld twiddled.
278 _objc_fatal("bad objc opt version (want %d, got %d)",
279 objc_opt::VERSION, opt->version);
281 else if (!opt->selopt()) {
282 // No selector table. dyld must not have written one.
283 failure = "(dyld shared cache is absent or out of date)";
285 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
287 // GC is on, which renames some selectors
288 // Non-selector optimizations are still valid, but we don't have
290 failure = "(GC is on)";
295 // All preoptimized selector references are invalid.
297 opt = (objc_opt_t *)empty_opt_data;
298 builtins = opt->selopt();
299 disableSharedCacheOptimizations();
302 _objc_inform("PREOPTIMIZATION: is DISABLED %s", failure);
306 // Valid optimization data written by dyld shared cache
308 builtins = opt->selopt();
311 _objc_inform("PREOPTIMIZATION: is ENABLED "
312 "(version %d)", opt->version);
318 // Register selectors used by libobjc
321 // Registering retain/release/autorelease requires GC decision first.
322 // sel_init doesn't actually need the wantsGC parameter, it just
323 // helps enforce the initialization order.
326 #define s(x) SEL_##x = sel_registerNameNoLock(#x, NO)
327 #define t(x,y) SEL_##y = sel_registerNameNoLock(#x, NO)
333 t(resolveInstanceMethod:, resolveInstanceMethod);
334 t(resolveClassMethod:, resolveClassMethod);
335 t(.cxx_construct, cxx_construct);
336 t(.cxx_destruct, cxx_destruct);
345 t(forwardInvocation:, forwardInvocation);