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 objc_selopt_t *preoptimizedSelectors(void)
57 Protocol *getPreoptimizedProtocol(const char *name)
62 unsigned int getPreoptimizedClassUnreasonableCount()
67 Class getPreoptimizedClass(const char *name)
72 Class* copyPreoptimizedClasses(const char *name, int *outCount)
78 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
83 header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
88 void preopt_init(void)
90 disableSharedCacheOptimizations();
93 _objc_inform("PREOPTIMIZATION: is DISABLED "
94 "(not supported on ths platform)");
103 #include <objc-shared-cache.h>
105 using objc_opt::objc_stringhash_offset_t;
106 using objc_opt::objc_protocolopt_t;
107 using objc_opt::objc_clsopt_t;
108 using objc_opt::objc_headeropt_ro_t;
109 using objc_opt::objc_headeropt_rw_t;
110 using objc_opt::objc_opt_t;
114 // preopt: the actual opt used at runtime (nil or &_objc_opt_data)
115 // _objc_opt_data: opt data possibly written by dyld
116 // opt is initialized to ~0 to detect incorrect use before preopt_init()
118 static const objc_opt_t *opt = (objc_opt_t *)~0;
119 static bool preoptimized;
121 extern const objc_opt_t _objc_opt_data; // in __TEXT, __objc_opt_ro
123 /***********************************************************************
124 * Return YES if we have a valid optimized shared cache.
125 **********************************************************************/
126 bool isPreoptimized(void)
132 /***********************************************************************
133 * Return YES if the shared cache does not have any classes with
134 * missing weak superclasses.
135 **********************************************************************/
136 bool noMissingWeakSuperclasses(void)
138 if (!preoptimized) return NO; // might have missing weak superclasses
139 return opt->flags & objc_opt::NoMissingWeakSuperclasses;
143 /***********************************************************************
144 * Return YES if this image's dyld shared cache optimizations are valid.
145 **********************************************************************/
146 bool header_info::isPreoptimized() const
148 // preoptimization disabled for some reason
149 if (!preoptimized) return NO;
151 // image not from shared cache, or not fixed inside shared cache
152 if (!info()->optimizedByDyld()) return NO;
158 objc_selopt_t *preoptimizedSelectors(void)
160 return opt ? opt->selopt() : nil;
164 Protocol *getPreoptimizedProtocol(const char *name)
166 objc_protocolopt_t *protocols = opt ? opt->protocolopt() : nil;
167 if (!protocols) return nil;
169 return (Protocol *)protocols->getProtocol(name);
173 unsigned int getPreoptimizedClassUnreasonableCount()
175 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
176 if (!classes) return 0;
178 // This is an overestimate: each set of duplicates
179 // gets double-counted in `capacity` as well.
180 return classes->capacity + classes->duplicateCount();
184 Class getPreoptimizedClass(const char *name)
186 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
187 if (!classes) return nil;
191 uint32_t count = classes->getClassAndHeader(name, cls, hi);
192 if (count == 1 && ((header_info *)hi)->isLoaded()) {
193 // exactly one matching class, and its image is loaded
196 else if (count > 1) {
197 // more than one matching class - find one that is loaded
198 void *clslist[count];
200 classes->getClassesAndHeaders(name, clslist, hilist);
201 for (uint32_t i = 0; i < count; i++) {
202 if (((header_info *)hilist[i])->isLoaded()) {
203 return (Class)clslist[i];
208 // no match that is loaded
213 Class* copyPreoptimizedClasses(const char *name, int *outCount)
217 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
218 if (!classes) return nil;
222 uint32_t count = classes->getClassAndHeader(name, cls, hi);
223 if (count == 0) return nil;
225 Class *result = (Class *)calloc(count, sizeof(Class));
226 if (count == 1 && ((header_info *)hi)->isLoaded()) {
227 // exactly one matching class, and its image is loaded
228 result[(*outCount)++] = (Class)cls;
231 else if (count > 1) {
232 // more than one matching class - find those that are loaded
233 void *clslist[count];
235 classes->getClassesAndHeaders(name, clslist, hilist);
236 for (uint32_t i = 0; i < count; i++) {
237 if (((header_info *)hilist[i])->isLoaded()) {
238 result[(*outCount)++] = (Class)clslist[i];
242 if (*outCount == 0) {
243 // found multiple classes with that name, but none are loaded
250 // no match that is loaded
255 struct objc_headeropt_ro_t {
258 header_info headers[0]; // sorted by mhdr address
260 header_info *get(const headerType *mhdr)
262 assert(entsize == sizeof(header_info));
266 while (start <= end) {
267 int32_t i = (start+end)/2;
268 header_info *hi = headers+i;
269 if (mhdr == hi->mhdr()) return hi;
270 else if (mhdr < hi->mhdr()) end = i-1;
275 for (uint32_t i = 0; i < count; i++) {
276 header_info *hi = headers+i;
277 if (mhdr == hi->mhdr()) {
278 _objc_fatal("failed to find header %p (%d/%d)",
288 struct objc_headeropt_rw_t {
291 header_info_rw headers[0]; // sorted by mhdr address
296 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
299 // fixme old ABI shared cache doesn't prepare these properly
303 objc_headeropt_ro_t *hinfos = opt ? opt->headeropt_ro() : nil;
304 if (hinfos) return hinfos->get(mhdr);
309 header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
312 // fixme old ABI shared cache doesn't prepare these properly
316 objc_headeropt_ro_t *hinfoRO = opt ? opt->headeropt_ro() : nil;
317 objc_headeropt_rw_t *hinfoRW = opt ? opt->headeropt_rw() : nil;
318 if (!hinfoRO || !hinfoRW) {
319 _objc_fatal("preoptimized header_info missing for %s (%p %p %p)",
320 hdr->fname(), hdr, hinfoRO, hinfoRW);
322 int32_t index = (int32_t)(hdr - hinfoRO->headers);
323 assert(hinfoRW->entsize == sizeof(header_info_rw));
324 return &hinfoRW->headers[index];
328 void preopt_init(void)
330 // `opt` not set at compile time in order to detect too-early usage
331 const char *failure = nil;
332 opt = &_objc_opt_data;
335 // OBJC_DISABLE_PREOPTIMIZATION is set
336 // If opt->version != VERSION then you continue at your own risk.
337 failure = "(by OBJC_DISABLE_PREOPTIMIZATION)";
339 else if (opt->version != objc_opt::VERSION) {
340 // This shouldn't happen. You probably forgot to edit objc-sel-table.s.
341 // If dyld really did write the wrong optimization version,
342 // then we must halt because we don't know what bits dyld twiddled.
343 _objc_fatal("bad objc preopt version (want %d, got %d)",
344 objc_opt::VERSION, opt->version);
346 else if (!opt->selopt() || !opt->headeropt_ro()) {
347 // One of the tables is missing.
348 failure = "(dyld shared cache is absent or out of date)";
352 // All preoptimized selector references are invalid.
355 disableSharedCacheOptimizations();
358 _objc_inform("PREOPTIMIZATION: is DISABLED %s", failure);
362 // Valid optimization data written by dyld shared cache
366 _objc_inform("PREOPTIMIZATION: is ENABLED "
367 "(version %d)", opt->version);