2 * Copyright (c) 2012 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@
26 #include "objc-private.h"
27 #include "objc-cache.h"
30 #include <objc-shared-cache.h>
31 using namespace objc_opt;
32 static const objc_selopt_t *builtins = NULL;
35 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
40 static size_t SelrefCount = 0;
42 static NXMapTable *namedSelectors;
44 static SEL search_builtins(const char *key);
47 /***********************************************************************
49 * Initialize selector tables and register selectors used internally.
50 **********************************************************************/
51 void sel_init(BOOL wantsGC, size_t selrefCount)
53 // save this value for later
54 SelrefCount = selrefCount;
57 builtins = preoptimizedSelectors();
60 // Register selectors used by libobjc
63 // Registering retain/release/autorelease requires GC decision first.
64 // sel_init doesn't actually need the wantsGC parameter, it just
65 // helps enforce the initialization order.
68 #define s(x) SEL_##x = sel_registerNameNoLock(#x, NO)
69 #define t(x,y) SEL_##y = sel_registerNameNoLock(#x, NO)
75 t(resolveInstanceMethod:, resolveInstanceMethod);
76 t(resolveClassMethod:, resolveClassMethod);
77 t(.cxx_construct, cxx_construct);
78 t(.cxx_destruct, cxx_destruct);
84 t(allocWithZone:, allocWithZone);
88 t(forwardInvocation:, forwardInvocation);
97 static SEL sel_alloc(const char *name, bool copy)
99 rwlock_assert_writing(&selLock);
100 return (SEL)(copy ? _strdup_internal(name) : name);
104 const char *sel_getName(SEL sel)
106 if (!sel) return "<null selector>";
107 return (const char *)(const void*)sel;
111 BOOL sel_isMapped(SEL sel)
115 const char *name = (const char *)sel;
117 if (sel == search_builtins(name)) return YES;
119 rwlock_read(&selLock);
120 bool result = (sel == (SEL)NXMapGet(namedSelectors, name));
121 rwlock_unlock_read(&selLock);
127 static SEL search_builtins(const char *name)
130 if (builtins) return (SEL)builtins->get(name);
136 static SEL __sel_registerName(const char *name, int lock, int copy)
140 if (lock) rwlock_assert_unlocked(&selLock);
141 else rwlock_assert_writing(&selLock);
143 if (!name) return (SEL)0;
145 result = search_builtins(name);
146 if (result) return result;
148 if (lock) rwlock_read(&selLock);
149 if (namedSelectors) {
150 result = (SEL)NXMapGet(namedSelectors, name);
152 if (lock) rwlock_unlock_read(&selLock);
153 if (result) return result;
157 if (lock) rwlock_write(&selLock);
159 if (!namedSelectors) {
160 namedSelectors = NXCreateMapTable(NXStrValueMapPrototype,
161 (unsigned)SelrefCount);
164 // Rescan in case it was added while we dropped the lock
165 result = (SEL)NXMapGet(namedSelectors, name);
168 result = sel_alloc(name, copy);
169 // fixme choose a better container (hash not map for starters)
170 NXMapInsert(namedSelectors, sel_getName(result), result);
173 if (lock) rwlock_unlock_write(&selLock);
178 SEL sel_registerName(const char *name) {
179 return __sel_registerName(name, 1, 1); // YES lock, YES copy
182 SEL sel_registerNameNoLock(const char *name, BOOL copy) {
183 return __sel_registerName(name, 0, copy); // NO lock, maybe copy
188 rwlock_write(&selLock);
191 void sel_unlock(void)
193 rwlock_unlock_write(&selLock);
198 // the majority of uses of this function (which used to return NULL if not found)
199 // did not check for NULL, so, in fact, never return NULL
201 SEL sel_getUid(const char *name) {
202 return __sel_registerName(name, 2, 1); // YES lock, YES copy
206 BOOL sel_isEqual(SEL lhs, SEL rhs)
208 return (lhs == rhs) ? YES : NO;
212 /***********************************************************************
213 * sel_preoptimizationValid
214 * Return YES if this image's selector fixups are valid courtesy
215 * of the dyld shared cache.
216 **********************************************************************/
217 BOOL sel_preoptimizationValid(const header_info *hi)
225 // preoptimization disabled for some reason
226 if (!isPreoptimized()) return NO;
228 // image not from shared cache, or not fixed inside shared cache
229 if (!_objcHeaderOptimizedByDyld(hi)) return NO;