]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-opt.mm
objc4-709.tar.gz
[apple/objc4.git] / runtime / objc-opt.mm
1 /*
2 * Copyright (c) 2012 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 objc-opt.mm
26 Management of optimizations in the dyld shared cache
27 */
28
29 #include "objc-private.h"
30
31
32 #if !SUPPORT_PREOPT
33 // Preoptimization not supported on this platform.
34
35 struct objc_selopt_t;
36
37 bool isPreoptimized(void)
38 {
39 return false;
40 }
41
42 bool noMissingWeakSuperclasses(void)
43 {
44 return false;
45 }
46
47 bool header_info::isPreoptimized() const
48 {
49 return false;
50 }
51
52 objc_selopt_t *preoptimizedSelectors(void)
53 {
54 return nil;
55 }
56
57 Protocol *getPreoptimizedProtocol(const char *name)
58 {
59 return nil;
60 }
61
62 unsigned int getPreoptimizedClassUnreasonableCount()
63 {
64 return 0;
65 }
66
67 Class getPreoptimizedClass(const char *name)
68 {
69 return nil;
70 }
71
72 Class* copyPreoptimizedClasses(const char *name, int *outCount)
73 {
74 *outCount = 0;
75 return nil;
76 }
77
78 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
79 {
80 return nil;
81 }
82
83 header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
84 {
85 return nil;
86 }
87
88 void preopt_init(void)
89 {
90 disableSharedCacheOptimizations();
91
92 if (PrintPreopt) {
93 _objc_inform("PREOPTIMIZATION: is DISABLED "
94 "(not supported on ths platform)");
95 }
96 }
97
98
99 // !SUPPORT_PREOPT
100 #else
101 // SUPPORT_PREOPT
102
103 #include <objc-shared-cache.h>
104
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;
111
112 __BEGIN_DECLS
113
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()
117
118 static const objc_opt_t *opt = (objc_opt_t *)~0;
119 static bool preoptimized;
120
121 extern const objc_opt_t _objc_opt_data; // in __TEXT, __objc_opt_ro
122
123 /***********************************************************************
124 * Return YES if we have a valid optimized shared cache.
125 **********************************************************************/
126 bool isPreoptimized(void)
127 {
128 return preoptimized;
129 }
130
131
132 /***********************************************************************
133 * Return YES if the shared cache does not have any classes with
134 * missing weak superclasses.
135 **********************************************************************/
136 bool noMissingWeakSuperclasses(void)
137 {
138 if (!preoptimized) return NO; // might have missing weak superclasses
139 return opt->flags & objc_opt::NoMissingWeakSuperclasses;
140 }
141
142
143 /***********************************************************************
144 * Return YES if this image's dyld shared cache optimizations are valid.
145 **********************************************************************/
146 bool header_info::isPreoptimized() const
147 {
148 // preoptimization disabled for some reason
149 if (!preoptimized) return NO;
150
151 // image not from shared cache, or not fixed inside shared cache
152 if (!info()->optimizedByDyld()) return NO;
153
154 return YES;
155 }
156
157
158 objc_selopt_t *preoptimizedSelectors(void)
159 {
160 return opt ? opt->selopt() : nil;
161 }
162
163
164 Protocol *getPreoptimizedProtocol(const char *name)
165 {
166 objc_protocolopt_t *protocols = opt ? opt->protocolopt() : nil;
167 if (!protocols) return nil;
168
169 return (Protocol *)protocols->getProtocol(name);
170 }
171
172
173 unsigned int getPreoptimizedClassUnreasonableCount()
174 {
175 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
176 if (!classes) return 0;
177
178 // This is an overestimate: each set of duplicates
179 // gets double-counted in `capacity` as well.
180 return classes->capacity + classes->duplicateCount();
181 }
182
183
184 Class getPreoptimizedClass(const char *name)
185 {
186 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
187 if (!classes) return nil;
188
189 void *cls;
190 void *hi;
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
194 return (Class)cls;
195 }
196 else if (count > 1) {
197 // more than one matching class - find one that is loaded
198 void *clslist[count];
199 void *hilist[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];
204 }
205 }
206 }
207
208 // no match that is loaded
209 return nil;
210 }
211
212
213 Class* copyPreoptimizedClasses(const char *name, int *outCount)
214 {
215 *outCount = 0;
216
217 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
218 if (!classes) return nil;
219
220 void *cls;
221 void *hi;
222 uint32_t count = classes->getClassAndHeader(name, cls, hi);
223 if (count == 0) return nil;
224
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;
229 return result;
230 }
231 else if (count > 1) {
232 // more than one matching class - find those that are loaded
233 void *clslist[count];
234 void *hilist[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];
239 }
240 }
241
242 if (*outCount == 0) {
243 // found multiple classes with that name, but none are loaded
244 free(result);
245 result = nil;
246 }
247 return result;
248 }
249
250 // no match that is loaded
251 return nil;
252 }
253
254 namespace objc_opt {
255 struct objc_headeropt_ro_t {
256 uint32_t count;
257 uint32_t entsize;
258 header_info headers[0]; // sorted by mhdr address
259
260 header_info *get(const headerType *mhdr)
261 {
262 assert(entsize == sizeof(header_info));
263
264 int32_t start = 0;
265 int32_t end = count;
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;
271 else start = i+1;
272 }
273
274 #if DEBUG
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)",
279 mhdr, i, count);
280 }
281 }
282 #endif
283
284 return nil;
285 }
286 };
287
288 struct objc_headeropt_rw_t {
289 uint32_t count;
290 uint32_t entsize;
291 header_info_rw headers[0]; // sorted by mhdr address
292 };
293 };
294
295
296 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
297 {
298 #if !__OBJC2__
299 // fixme old ABI shared cache doesn't prepare these properly
300 return nil;
301 #endif
302
303 objc_headeropt_ro_t *hinfos = opt ? opt->headeropt_ro() : nil;
304 if (hinfos) return hinfos->get(mhdr);
305 else return nil;
306 }
307
308
309 header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
310 {
311 #if !__OBJC2__
312 // fixme old ABI shared cache doesn't prepare these properly
313 return nil;
314 #endif
315
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);
321 }
322 int32_t index = (int32_t)(hdr - hinfoRO->headers);
323 assert(hinfoRW->entsize == sizeof(header_info_rw));
324 return &hinfoRW->headers[index];
325 }
326
327
328 void preopt_init(void)
329 {
330 // `opt` not set at compile time in order to detect too-early usage
331 const char *failure = nil;
332 opt = &_objc_opt_data;
333
334 if (DisablePreopt) {
335 // OBJC_DISABLE_PREOPTIMIZATION is set
336 // If opt->version != VERSION then you continue at your own risk.
337 failure = "(by OBJC_DISABLE_PREOPTIMIZATION)";
338 }
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);
345 }
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)";
349 }
350
351 if (failure) {
352 // All preoptimized selector references are invalid.
353 preoptimized = NO;
354 opt = nil;
355 disableSharedCacheOptimizations();
356
357 if (PrintPreopt) {
358 _objc_inform("PREOPTIMIZATION: is DISABLED %s", failure);
359 }
360 }
361 else {
362 // Valid optimization data written by dyld shared cache
363 preoptimized = YES;
364
365 if (PrintPreopt) {
366 _objc_inform("PREOPTIMIZATION: is ENABLED "
367 "(version %d)", opt->version);
368 }
369 }
370 }
371
372
373 __END_DECLS
374
375 // SUPPORT_PREOPT
376 #endif