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 bool sharedRegionContains(const void *ptr)
83 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
88 header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
93 void preopt_init(void)
95 disableSharedCacheOptimizations();
98 _objc_inform("PREOPTIMIZATION: is DISABLED "
99 "(not supported on ths platform)");
108 #include <objc-shared-cache.h>
110 using objc_opt::objc_stringhash_offset_t;
111 using objc_opt::objc_protocolopt_t;
112 using objc_opt::objc_clsopt_t;
113 using objc_opt::objc_headeropt_ro_t;
114 using objc_opt::objc_headeropt_rw_t;
115 using objc_opt::objc_opt_t;
119 // preopt: the actual opt used at runtime (nil or &_objc_opt_data)
120 // _objc_opt_data: opt data possibly written by dyld
121 // opt is initialized to ~0 to detect incorrect use before preopt_init()
123 static const objc_opt_t *opt = (objc_opt_t *)~0;
124 static uintptr_t shared_cache_start;
125 static uintptr_t shared_cache_end;
126 static bool preoptimized;
128 extern const objc_opt_t _objc_opt_data; // in __TEXT, __objc_opt_ro
130 /***********************************************************************
131 * Return YES if we have a valid optimized shared cache.
132 **********************************************************************/
133 bool isPreoptimized(void)
139 /***********************************************************************
140 * Return YES if the shared cache does not have any classes with
141 * missing weak superclasses.
142 **********************************************************************/
143 bool noMissingWeakSuperclasses(void)
145 if (!preoptimized) return NO; // might have missing weak superclasses
146 return opt->flags & objc_opt::NoMissingWeakSuperclasses;
150 /***********************************************************************
151 * Return YES if this image's dyld shared cache optimizations are valid.
152 **********************************************************************/
153 bool header_info::isPreoptimized() const
155 // preoptimization disabled for some reason
156 if (!preoptimized) return NO;
158 // image not from shared cache, or not fixed inside shared cache
159 if (!info()->optimizedByDyld()) return NO;
165 objc_selopt_t *preoptimizedSelectors(void)
167 return opt ? opt->selopt() : nil;
171 Protocol *getPreoptimizedProtocol(const char *name)
173 objc_protocolopt_t *protocols = opt ? opt->protocolopt() : nil;
174 if (!protocols) return nil;
176 return (Protocol *)protocols->getProtocol(name);
180 unsigned int getPreoptimizedClassUnreasonableCount()
182 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
183 if (!classes) return 0;
185 // This is an overestimate: each set of duplicates
186 // gets double-counted in `capacity` as well.
187 return classes->capacity + classes->duplicateCount();
191 Class getPreoptimizedClass(const char *name)
193 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
194 if (!classes) return nil;
198 uint32_t count = classes->getClassAndHeader(name, cls, hi);
199 if (count == 1 && ((header_info *)hi)->isLoaded()) {
200 // exactly one matching class, and its image is loaded
203 else if (count > 1) {
204 // more than one matching class - find one that is loaded
205 void *clslist[count];
207 classes->getClassesAndHeaders(name, clslist, hilist);
208 for (uint32_t i = 0; i < count; i++) {
209 if (((header_info *)hilist[i])->isLoaded()) {
210 return (Class)clslist[i];
215 // no match that is loaded
220 Class* copyPreoptimizedClasses(const char *name, int *outCount)
224 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
225 if (!classes) return nil;
229 uint32_t count = classes->getClassAndHeader(name, cls, hi);
230 if (count == 0) return nil;
232 Class *result = (Class *)calloc(count, sizeof(Class));
233 if (count == 1 && ((header_info *)hi)->isLoaded()) {
234 // exactly one matching class, and its image is loaded
235 result[(*outCount)++] = (Class)cls;
238 else if (count > 1) {
239 // more than one matching class - find those that are loaded
240 void *clslist[count];
242 classes->getClassesAndHeaders(name, clslist, hilist);
243 for (uint32_t i = 0; i < count; i++) {
244 if (((header_info *)hilist[i])->isLoaded()) {
245 result[(*outCount)++] = (Class)clslist[i];
249 if (*outCount == 0) {
250 // found multiple classes with that name, but none are loaded
257 // no match that is loaded
261 /***********************************************************************
262 * Return YES if the given pointer lies within the shared cache.
263 * If the shared cache is not set up or is not valid,
264 **********************************************************************/
265 bool sharedRegionContains(const void *ptr)
267 uintptr_t address = (uintptr_t)ptr;
268 return shared_cache_start <= address && address < shared_cache_end;
272 struct objc_headeropt_ro_t {
275 header_info headers[0]; // sorted by mhdr address
277 header_info *get(const headerType *mhdr)
279 assert(entsize == sizeof(header_info));
283 while (start <= end) {
284 int32_t i = (start+end)/2;
285 header_info *hi = headers+i;
286 if (mhdr == hi->mhdr()) return hi;
287 else if (mhdr < hi->mhdr()) end = i-1;
292 for (uint32_t i = 0; i < count; i++) {
293 header_info *hi = headers+i;
294 if (mhdr == hi->mhdr()) {
295 _objc_fatal("failed to find header %p (%d/%d)",
305 struct objc_headeropt_rw_t {
308 header_info_rw headers[0]; // sorted by mhdr address
313 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
316 // fixme old ABI shared cache doesn't prepare these properly
320 objc_headeropt_ro_t *hinfos = opt ? opt->headeropt_ro() : nil;
321 if (hinfos) return hinfos->get(mhdr);
326 header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
329 // fixme old ABI shared cache doesn't prepare these properly
333 objc_headeropt_ro_t *hinfoRO = opt ? opt->headeropt_ro() : nil;
334 objc_headeropt_rw_t *hinfoRW = opt ? opt->headeropt_rw() : nil;
335 if (!hinfoRO || !hinfoRW) {
336 _objc_fatal("preoptimized header_info missing for %s (%p %p %p)",
337 hdr->fname(), hdr, hinfoRO, hinfoRW);
339 int32_t index = (int32_t)(hdr - hinfoRO->headers);
340 assert(hinfoRW->entsize == sizeof(header_info_rw));
341 return &hinfoRW->headers[index];
345 void preopt_init(void)
347 // Get the memory region occupied by the shared cache.
349 const void *start = _dyld_get_shared_cache_range(&length);
351 shared_cache_start = (uintptr_t)start;
352 shared_cache_end = shared_cache_start + length;
354 shared_cache_start = shared_cache_end = 0;
357 // `opt` not set at compile time in order to detect too-early usage
358 const char *failure = nil;
359 opt = &_objc_opt_data;
362 // OBJC_DISABLE_PREOPTIMIZATION is set
363 // If opt->version != VERSION then you continue at your own risk.
364 failure = "(by OBJC_DISABLE_PREOPTIMIZATION)";
366 else if (opt->version != objc_opt::VERSION) {
367 // This shouldn't happen. You probably forgot to edit objc-sel-table.s.
368 // If dyld really did write the wrong optimization version,
369 // then we must halt because we don't know what bits dyld twiddled.
370 _objc_fatal("bad objc preopt version (want %d, got %d)",
371 objc_opt::VERSION, opt->version);
373 else if (!opt->selopt() || !opt->headeropt_ro()) {
374 // One of the tables is missing.
375 failure = "(dyld shared cache is absent or out of date)";
379 // All preoptimized selector references are invalid.
382 disableSharedCacheOptimizations();
385 _objc_inform("PREOPTIMIZATION: is DISABLED %s", failure);
389 // Valid optimization data written by dyld shared cache
393 _objc_inform("PREOPTIMIZATION: is ENABLED "
394 "(version %d)", opt->version);