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 Class getPreoptimizedClass(const char *name)
67 Class* copyPreoptimizedClasses(const char *name, int *outCount)
73 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
78 header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
83 void preopt_init(void)
85 disableSharedCacheOptimizations();
88 _objc_inform("PREOPTIMIZATION: is DISABLED "
89 "(not supported on ths platform)");
98 #include <objc-shared-cache.h>
100 using objc_opt::objc_stringhash_offset_t;
101 using objc_opt::objc_protocolopt_t;
102 using objc_opt::objc_clsopt_t;
103 using objc_opt::objc_headeropt_ro_t;
104 using objc_opt::objc_headeropt_rw_t;
105 using objc_opt::objc_opt_t;
109 // preopt: the actual opt used at runtime (nil or &_objc_opt_data)
110 // _objc_opt_data: opt data possibly written by dyld
111 // opt is initialized to ~0 to detect incorrect use before preopt_init()
113 static const objc_opt_t *opt = (objc_opt_t *)~0;
114 static bool preoptimized;
116 extern const objc_opt_t _objc_opt_data; // in __TEXT, __objc_opt_ro
118 /***********************************************************************
119 * Return YES if we have a valid optimized shared cache.
120 **********************************************************************/
121 bool isPreoptimized(void)
127 /***********************************************************************
128 * Return YES if the shared cache does not have any classes with
129 * missing weak superclasses.
130 **********************************************************************/
131 bool noMissingWeakSuperclasses(void)
133 if (!preoptimized) return NO; // might have missing weak superclasses
134 return opt->flags & objc_opt::NoMissingWeakSuperclasses;
138 /***********************************************************************
139 * Return YES if this image's dyld shared cache optimizations are valid.
140 **********************************************************************/
141 bool header_info::isPreoptimized() const
143 // preoptimization disabled for some reason
144 if (!preoptimized) return NO;
146 // image not from shared cache, or not fixed inside shared cache
147 if (!info()->optimizedByDyld()) return NO;
153 objc_selopt_t *preoptimizedSelectors(void)
155 return opt ? opt->selopt() : nil;
159 Protocol *getPreoptimizedProtocol(const char *name)
161 objc_protocolopt_t *protocols = opt ? opt->protocolopt() : nil;
162 if (!protocols) return nil;
164 return (Protocol *)protocols->getProtocol(name);
168 Class getPreoptimizedClass(const char *name)
170 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
171 if (!classes) return nil;
175 uint32_t count = classes->getClassAndHeader(name, cls, hi);
176 if (count == 1 && ((header_info *)hi)->isLoaded()) {
177 // exactly one matching class, and its image is loaded
180 else if (count > 1) {
181 // more than one matching class - find one that is loaded
182 void *clslist[count];
184 classes->getClassesAndHeaders(name, clslist, hilist);
185 for (uint32_t i = 0; i < count; i++) {
186 if (((header_info *)hilist[i])->isLoaded()) {
187 return (Class)clslist[i];
192 // no match that is loaded
197 Class* copyPreoptimizedClasses(const char *name, int *outCount)
201 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
202 if (!classes) return nil;
206 uint32_t count = classes->getClassAndHeader(name, cls, hi);
207 if (count == 0) return nil;
209 Class *result = (Class *)calloc(count, sizeof(Class));
210 if (count == 1 && ((header_info *)hi)->isLoaded()) {
211 // exactly one matching class, and its image is loaded
212 result[(*outCount)++] = (Class)cls;
215 else if (count > 1) {
216 // more than one matching class - find those that are loaded
217 void *clslist[count];
219 classes->getClassesAndHeaders(name, clslist, hilist);
220 for (uint32_t i = 0; i < count; i++) {
221 if (((header_info *)hilist[i])->isLoaded()) {
222 result[(*outCount)++] = (Class)clslist[i];
226 if (*outCount == 0) {
227 // found multiple classes with that name, but none are loaded
234 // no match that is loaded
239 struct objc_headeropt_ro_t {
242 header_info headers[0]; // sorted by mhdr address
244 header_info *get(const headerType *mhdr)
246 assert(entsize == sizeof(header_info));
250 while (start <= end) {
251 int32_t i = (start+end)/2;
252 header_info *hi = headers+i;
253 if (mhdr == hi->mhdr()) return hi;
254 else if (mhdr < hi->mhdr()) end = i-1;
259 for (uint32_t i = 0; i < count; i++) {
260 header_info *hi = headers+i;
261 if (mhdr == hi->mhdr()) {
262 _objc_fatal("failed to find header %p (%d/%d)",
272 struct objc_headeropt_rw_t {
275 header_info_rw headers[0]; // sorted by mhdr address
280 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
283 // fixme old ABI shared cache doesn't prepare these properly
287 objc_headeropt_ro_t *hinfos = opt ? opt->headeropt_ro() : nil;
288 if (hinfos) return hinfos->get(mhdr);
293 header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
296 // fixme old ABI shared cache doesn't prepare these properly
300 objc_headeropt_ro_t *hinfoRO = opt ? opt->headeropt_ro() : nil;
301 objc_headeropt_rw_t *hinfoRW = opt ? opt->headeropt_rw() : nil;
302 if (!hinfoRO || !hinfoRW) {
303 _objc_fatal("preoptimized header_info missing for %s (%p %p %p)",
304 hdr->fname(), hdr, hinfoRO, hinfoRW);
306 int32_t index = (int32_t)(hdr - hinfoRO->headers);
307 assert(hinfoRW->entsize == sizeof(header_info_rw));
308 return &hinfoRW->headers[index];
312 void preopt_init(void)
314 // `opt` not set at compile time in order to detect too-early usage
315 const char *failure = nil;
316 opt = &_objc_opt_data;
319 // OBJC_DISABLE_PREOPTIMIZATION is set
320 // If opt->version != VERSION then you continue at your own risk.
321 failure = "(by OBJC_DISABLE_PREOPTIMIZATION)";
323 else if (opt->version != objc_opt::VERSION) {
324 // This shouldn't happen. You probably forgot to edit objc-sel-table.s.
325 // If dyld really did write the wrong optimization version,
326 // then we must halt because we don't know what bits dyld twiddled.
327 _objc_fatal("bad objc preopt version (want %d, got %d)",
328 objc_opt::VERSION, opt->version);
330 else if (!opt->selopt() || !opt->headeropt_ro()) {
331 // One of the tables is missing.
332 failure = "(dyld shared cache is absent or out of date)";
336 // All preoptimized selector references are invalid.
339 disableSharedCacheOptimizations();
342 _objc_inform("PREOPTIMIZATION: is DISABLED %s", failure);
346 // Valid optimization data written by dyld shared cache
350 _objc_inform("PREOPTIMIZATION: is ENABLED "
351 "(version %d)", opt->version);