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 Management of optimizations in the dyld shared cache
29 #include "objc-private.h"
33 // Preoptimization not supported on this platform.
37 bool isPreoptimized(void)
42 bool noMissingWeakSuperclasses(void)
47 bool header_info::isPreoptimized() const
52 bool header_info::hasPreoptimizedSelectors() const
57 bool header_info::hasPreoptimizedClasses() const
62 bool header_info::hasPreoptimizedProtocols() const
67 objc_selopt_t *preoptimizedSelectors(void)
72 bool sharedCacheSupportsProtocolRoots(void)
77 Protocol *getPreoptimizedProtocol(const char *name)
82 unsigned int getPreoptimizedClassUnreasonableCount()
87 Class getPreoptimizedClass(const char *name)
92 Class* copyPreoptimizedClasses(const char *name, int *outCount)
98 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
103 header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
108 void preopt_init(void)
110 disableSharedCacheOptimizations();
113 _objc_inform("PREOPTIMIZATION: is DISABLED "
114 "(not supported on ths platform)");
123 #include <objc-shared-cache.h>
125 using objc_opt::objc_stringhash_offset_t;
126 using objc_opt::objc_protocolopt_t;
127 using objc_opt::objc_protocolopt2_t;
128 using objc_opt::objc_clsopt_t;
129 using objc_opt::objc_headeropt_ro_t;
130 using objc_opt::objc_headeropt_rw_t;
131 using objc_opt::objc_opt_t;
135 // preopt: the actual opt used at runtime (nil or &_objc_opt_data)
136 // _objc_opt_data: opt data possibly written by dyld
137 // opt is initialized to ~0 to detect incorrect use before preopt_init()
139 static const objc_opt_t *opt = (objc_opt_t *)~0;
140 static bool preoptimized;
142 extern const objc_opt_t _objc_opt_data; // in __TEXT, __objc_opt_ro
144 /***********************************************************************
145 * Return YES if we have a valid optimized shared cache.
146 **********************************************************************/
147 bool isPreoptimized(void)
153 /***********************************************************************
154 * Return YES if the shared cache does not have any classes with
155 * missing weak superclasses.
156 **********************************************************************/
157 bool noMissingWeakSuperclasses(void)
159 if (!preoptimized) return NO; // might have missing weak superclasses
160 return opt->flags & objc_opt::NoMissingWeakSuperclasses;
164 /***********************************************************************
165 * Return YES if this image's dyld shared cache optimizations are valid.
166 **********************************************************************/
167 bool header_info::isPreoptimized() const
169 // preoptimization disabled for some reason
170 if (!preoptimized) return NO;
172 // image not from shared cache, or not fixed inside shared cache
173 if (!info()->optimizedByDyld()) return NO;
178 bool header_info::hasPreoptimizedSelectors() const
180 // preoptimization disabled for some reason
181 if (!preoptimized) return NO;
183 return info()->optimizedByDyld() || info()->optimizedByDyldClosure();
186 bool header_info::hasPreoptimizedClasses() const
188 // preoptimization disabled for some reason
189 if (!preoptimized) return NO;
191 return info()->optimizedByDyld() || info()->optimizedByDyldClosure();
194 bool header_info::hasPreoptimizedProtocols() const
196 // preoptimization disabled for some reason
197 if (!preoptimized) return NO;
199 return info()->optimizedByDyld() || info()->optimizedByDyldClosure();
203 objc_selopt_t *preoptimizedSelectors(void)
205 return opt ? opt->selopt() : nil;
208 bool sharedCacheSupportsProtocolRoots(void)
210 return (opt != nil) && (opt->protocolopt2() != nil);
214 Protocol *getSharedCachePreoptimizedProtocol(const char *name)
216 // Look in the new table if we have it
217 if (objc_protocolopt2_t *protocols2 = opt ? opt->protocolopt2() : nil) {
218 // Note, we have to pass the lambda directly here as otherwise we would try
219 // message copy and autorelease.
220 return (Protocol *)protocols2->getProtocol(name, [](const void* hi) -> bool {
221 return ((header_info *)hi)->isLoaded();
225 objc_protocolopt_t *protocols = opt ? opt->protocolopt() : nil;
226 if (!protocols) return nil;
228 return (Protocol *)protocols->getProtocol(name);
232 Protocol *getPreoptimizedProtocol(const char *name)
234 // Try table from dyld closure first. It was built to ignore the dupes it
235 // knows will come from the cache, so anything left in here was there when
237 Protocol *result = nil;
238 // Note, we have to pass the lambda directly here as otherwise we would try
239 // message copy and autorelease.
240 _dyld_for_each_objc_protocol(name, [&result](void* protocolPtr, bool isLoaded, bool* stop) {
241 // Skip images which aren't loaded. This supports the case where dyld
242 // might soft link an image from the main binary so its possibly not
247 // Found a loaded image with this class name, so stop the search
248 result = (Protocol *)protocolPtr;
251 if (result) return result;
253 return getSharedCachePreoptimizedProtocol(name);
257 unsigned int getPreoptimizedClassUnreasonableCount()
259 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
260 if (!classes) return 0;
262 // This is an overestimate: each set of duplicates
263 // gets double-counted in `capacity` as well.
264 return classes->capacity + classes->duplicateCount();
268 Class getPreoptimizedClass(const char *name)
270 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
271 if (!classes) return nil;
273 // Try table from dyld closure first. It was built to ignore the dupes it
274 // knows will come from the cache, so anything left in here was there when
277 // Note, we have to pass the lambda directly here as otherwise we would try
278 // message copy and autorelease.
279 _dyld_for_each_objc_class(name, [&result](void* classPtr, bool isLoaded, bool* stop) {
280 // Skip images which aren't loaded. This supports the case where dyld
281 // might soft link an image from the main binary so its possibly not
286 // Found a loaded image with this class name, so stop the search
287 result = (Class)classPtr;
290 if (result) return result;
294 uint32_t count = classes->getClassAndHeader(name, cls, hi);
295 if (count == 1 && ((header_info *)hi)->isLoaded()) {
296 // exactly one matching class, and its image is loaded
299 else if (count > 1) {
300 // more than one matching class - find one that is loaded
301 void *clslist[count];
303 classes->getClassesAndHeaders(name, clslist, hilist);
304 for (uint32_t i = 0; i < count; i++) {
305 if (((header_info *)hilist[i])->isLoaded()) {
306 return (Class)clslist[i];
311 // no match that is loaded
316 Class* copyPreoptimizedClasses(const char *name, int *outCount)
320 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
321 if (!classes) return nil;
325 uint32_t count = classes->getClassAndHeader(name, cls, hi);
326 if (count == 0) return nil;
328 Class *result = (Class *)calloc(count, sizeof(Class));
329 if (count == 1 && ((header_info *)hi)->isLoaded()) {
330 // exactly one matching class, and its image is loaded
331 result[(*outCount)++] = (Class)cls;
334 else if (count > 1) {
335 // more than one matching class - find those that are loaded
336 void *clslist[count];
338 classes->getClassesAndHeaders(name, clslist, hilist);
339 for (uint32_t i = 0; i < count; i++) {
340 if (((header_info *)hilist[i])->isLoaded()) {
341 result[(*outCount)++] = (Class)clslist[i];
345 if (*outCount == 0) {
346 // found multiple classes with that name, but none are loaded
353 // no match that is loaded
358 struct objc_headeropt_ro_t {
361 header_info headers[0]; // sorted by mhdr address
363 header_info *get(const headerType *mhdr)
365 ASSERT(entsize == sizeof(header_info));
369 while (start <= end) {
370 int32_t i = (start+end)/2;
371 header_info *hi = headers+i;
372 if (mhdr == hi->mhdr()) return hi;
373 else if (mhdr < hi->mhdr()) end = i-1;
378 for (uint32_t i = 0; i < count; i++) {
379 header_info *hi = headers+i;
380 if (mhdr == hi->mhdr()) {
381 _objc_fatal("failed to find header %p (%d/%d)",
391 struct objc_headeropt_rw_t {
394 header_info_rw headers[0]; // sorted by mhdr address
399 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
402 // fixme old ABI shared cache doesn't prepare these properly
406 objc_headeropt_ro_t *hinfos = opt ? opt->headeropt_ro() : nil;
407 if (hinfos) return hinfos->get(mhdr);
412 header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
415 // fixme old ABI shared cache doesn't prepare these properly
419 objc_headeropt_ro_t *hinfoRO = opt ? opt->headeropt_ro() : nil;
420 objc_headeropt_rw_t *hinfoRW = opt ? opt->headeropt_rw() : nil;
421 if (!hinfoRO || !hinfoRW) {
422 _objc_fatal("preoptimized header_info missing for %s (%p %p %p)",
423 hdr->fname(), hdr, hinfoRO, hinfoRW);
425 int32_t index = (int32_t)(hdr - hinfoRO->headers);
426 ASSERT(hinfoRW->entsize == sizeof(header_info_rw));
427 return &hinfoRW->headers[index];
431 void preopt_init(void)
433 // Get the memory region occupied by the shared cache.
435 const uintptr_t start = (uintptr_t)_dyld_get_shared_cache_range(&length);
438 objc::dataSegmentsRanges.add(start, start + length);
441 // `opt` not set at compile time in order to detect too-early usage
442 const char *failure = nil;
443 opt = &_objc_opt_data;
446 // OBJC_DISABLE_PREOPTIMIZATION is set
447 // If opt->version != VERSION then you continue at your own risk.
448 failure = "(by OBJC_DISABLE_PREOPTIMIZATION)";
450 else if (opt->version != objc_opt::VERSION) {
451 // This shouldn't happen. You probably forgot to edit objc-sel-table.s.
452 // If dyld really did write the wrong optimization version,
453 // then we must halt because we don't know what bits dyld twiddled.
454 _objc_fatal("bad objc preopt version (want %d, got %d)",
455 objc_opt::VERSION, opt->version);
457 else if (!opt->selopt() || !opt->headeropt_ro()) {
458 // One of the tables is missing.
459 failure = "(dyld shared cache is absent or out of date)";
463 // All preoptimized selector references are invalid.
466 disableSharedCacheOptimizations();
469 _objc_inform("PREOPTIMIZATION: is DISABLED %s", failure);
473 // Valid optimization data written by dyld shared cache
477 _objc_inform("PREOPTIMIZATION: is ENABLED "
478 "(version %d)", opt->version);