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 header_info::isPreoptimized() const
47 objc_selopt_t *preoptimizedSelectors(void)
52 Protocol *getPreoptimizedProtocol(const char *name)
57 Class getPreoptimizedClass(const char *name)
62 Class* copyPreoptimizedClasses(const char *name, int *outCount)
68 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
73 void preopt_init(void)
75 disableSharedCacheOptimizations();
78 _objc_inform("PREOPTIMIZATION: is DISABLED "
79 "(not supported on ths platform)");
88 #include <objc-shared-cache.h>
90 using objc_opt::objc_stringhash_offset_t;
91 using objc_opt::objc_protocolopt_t;
92 using objc_opt::objc_clsopt_t;
93 using objc_opt::objc_headeropt_t;
94 using objc_opt::objc_opt_t;
98 // preopt: the actual opt used at runtime (nil or &_objc_opt_data)
99 // _objc_opt_data: opt data possibly written by dyld
100 // opt is initialized to ~0 to detect incorrect use before preopt_init()
102 static const objc_opt_t *opt = (objc_opt_t *)~0;
103 static bool preoptimized;
105 extern const objc_opt_t _objc_opt_data; // in __TEXT, __objc_opt_ro
107 bool isPreoptimized(void)
113 /***********************************************************************
114 * Return YES if this image's dyld shared cache optimizations are valid.
115 **********************************************************************/
116 bool header_info::isPreoptimized() const
118 // preoptimization disabled for some reason
119 if (!preoptimized) return NO;
121 // image not from shared cache, or not fixed inside shared cache
122 if (!_objcHeaderOptimizedByDyld(this)) return NO;
128 objc_selopt_t *preoptimizedSelectors(void)
130 return opt ? opt->selopt() : nil;
134 Protocol *getPreoptimizedProtocol(const char *name)
136 objc_protocolopt_t *protocols = opt ? opt->protocolopt() : nil;
137 if (!protocols) return nil;
139 return (Protocol *)protocols->getProtocol(name);
143 Class getPreoptimizedClass(const char *name)
145 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
146 if (!classes) return nil;
150 uint32_t count = classes->getClassAndHeader(name, cls, hi);
151 if (count == 1 && ((header_info *)hi)->isLoaded()) {
152 // exactly one matching class, and its image is loaded
155 else if (count > 1) {
156 // more than one matching class - find one that is loaded
157 void *clslist[count];
159 classes->getClassesAndHeaders(name, clslist, hilist);
160 for (uint32_t i = 0; i < count; i++) {
161 if (((header_info *)hilist[i])->isLoaded()) {
162 return (Class)clslist[i];
167 // no match that is loaded
172 Class* copyPreoptimizedClasses(const char *name, int *outCount)
176 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
177 if (!classes) return nil;
181 uint32_t count = classes->getClassAndHeader(name, cls, hi);
182 if (count == 0) return nil;
184 Class *result = (Class *)calloc(count, sizeof(Class));
185 if (count == 1 && ((header_info *)hi)->isLoaded()) {
186 // exactly one matching class, and its image is loaded
187 result[(*outCount)++] = (Class)cls;
190 else if (count > 1) {
191 // more than one matching class - find those that are loaded
192 void *clslist[count];
194 classes->getClassesAndHeaders(name, clslist, hilist);
195 for (uint32_t i = 0; i < count; i++) {
196 if (((header_info *)hilist[i])->isLoaded()) {
197 result[(*outCount)++] = (Class)clslist[i];
201 if (*outCount == 0) {
202 // found multiple classes with that name, but none are loaded
209 // no match that is loaded
214 struct objc_headeropt_t {
217 header_info headers[0]; // sorted by mhdr address
219 header_info *get(const headerType *mhdr)
221 assert(entsize == sizeof(header_info));
225 while (start <= end) {
226 int32_t i = (start+end)/2;
227 header_info *hi = headers+i;
228 if (mhdr == hi->mhdr) return hi;
229 else if (mhdr < hi->mhdr) end = i-1;
234 for (uint32_t i = 0; i < count; i++) {
235 header_info *hi = headers+i;
236 if (mhdr == hi->mhdr) {
237 _objc_fatal("failed to find header %p (%d/%d)",
249 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
251 objc_headeropt_t *hinfos = opt ? opt->headeropt() : nil;
252 if (hinfos) return hinfos->get(mhdr);
257 void preopt_init(void)
259 // `opt` not set at compile time in order to detect too-early usage
260 const char *failure = nil;
261 opt = &_objc_opt_data;
264 // OBJC_DISABLE_PREOPTIMIZATION is set
265 // If opt->version != VERSION then you continue at your own risk.
266 failure = "(by OBJC_DISABLE_PREOPTIMIZATION)";
268 else if (opt->version != objc_opt::VERSION) {
269 // This shouldn't happen. You probably forgot to edit objc-sel-table.s.
270 // If dyld really did write the wrong optimization version,
271 // then we must halt because we don't know what bits dyld twiddled.
272 _objc_fatal("bad objc preopt version (want %d, got %d)",
273 objc_opt::VERSION, opt->version);
275 else if (!opt->selopt() || !opt->headeropt()) {
276 // One of the tables is missing.
277 failure = "(dyld shared cache is absent or out of date)";
279 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
281 // GC is on, which renames some selectors
282 // Non-selector optimizations are still valid, but we don't have
284 failure = "(GC is on)";
289 // All preoptimized selector references are invalid.
292 disableSharedCacheOptimizations();
295 _objc_inform("PREOPTIMIZATION: is DISABLED %s", failure);
299 // Valid optimization data written by dyld shared cache
303 _objc_inform("PREOPTIMIZATION: is ENABLED "
304 "(version %d)", opt->version);