]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-opt.mm
objc4-779.1.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 bool header_info::hasPreoptimizedSelectors() const
53 {
54 return false;
55 }
56
57 bool header_info::hasPreoptimizedClasses() const
58 {
59 return false;
60 }
61
62 bool header_info::hasPreoptimizedProtocols() const
63 {
64 return false;
65 }
66
67 objc_selopt_t *preoptimizedSelectors(void)
68 {
69 return nil;
70 }
71
72 bool sharedCacheSupportsProtocolRoots(void)
73 {
74 return false;
75 }
76
77 Protocol *getPreoptimizedProtocol(const char *name)
78 {
79 return nil;
80 }
81
82 unsigned int getPreoptimizedClassUnreasonableCount()
83 {
84 return 0;
85 }
86
87 Class getPreoptimizedClass(const char *name)
88 {
89 return nil;
90 }
91
92 Class* copyPreoptimizedClasses(const char *name, int *outCount)
93 {
94 *outCount = 0;
95 return nil;
96 }
97
98 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
99 {
100 return nil;
101 }
102
103 header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
104 {
105 return nil;
106 }
107
108 void preopt_init(void)
109 {
110 disableSharedCacheOptimizations();
111
112 if (PrintPreopt) {
113 _objc_inform("PREOPTIMIZATION: is DISABLED "
114 "(not supported on ths platform)");
115 }
116 }
117
118
119 // !SUPPORT_PREOPT
120 #else
121 // SUPPORT_PREOPT
122
123 #include <objc-shared-cache.h>
124
125 using objc_opt::objc_stringhash_offset_t;
126 using objc_opt::objc_protocolopt_t;
127 using objc_opt::objc_protocolopt2_t;
128 using objc_opt::objc_clsopt_t;
129 using objc_opt::objc_headeropt_ro_t;
130 using objc_opt::objc_headeropt_rw_t;
131 using objc_opt::objc_opt_t;
132
133 __BEGIN_DECLS
134
135 // preopt: the actual opt used at runtime (nil or &_objc_opt_data)
136 // _objc_opt_data: opt data possibly written by dyld
137 // opt is initialized to ~0 to detect incorrect use before preopt_init()
138
139 static const objc_opt_t *opt = (objc_opt_t *)~0;
140 static bool preoptimized;
141
142 extern const objc_opt_t _objc_opt_data; // in __TEXT, __objc_opt_ro
143
144 /***********************************************************************
145 * Return YES if we have a valid optimized shared cache.
146 **********************************************************************/
147 bool isPreoptimized(void)
148 {
149 return preoptimized;
150 }
151
152
153 /***********************************************************************
154 * Return YES if the shared cache does not have any classes with
155 * missing weak superclasses.
156 **********************************************************************/
157 bool noMissingWeakSuperclasses(void)
158 {
159 if (!preoptimized) return NO; // might have missing weak superclasses
160 return opt->flags & objc_opt::NoMissingWeakSuperclasses;
161 }
162
163
164 /***********************************************************************
165 * Return YES if this image's dyld shared cache optimizations are valid.
166 **********************************************************************/
167 bool header_info::isPreoptimized() const
168 {
169 // preoptimization disabled for some reason
170 if (!preoptimized) return NO;
171
172 // image not from shared cache, or not fixed inside shared cache
173 if (!info()->optimizedByDyld()) return NO;
174
175 return YES;
176 }
177
178 bool header_info::hasPreoptimizedSelectors() const
179 {
180 // preoptimization disabled for some reason
181 if (!preoptimized) return NO;
182
183 return info()->optimizedByDyld() || info()->optimizedByDyldClosure();
184 }
185
186 bool header_info::hasPreoptimizedClasses() const
187 {
188 // preoptimization disabled for some reason
189 if (!preoptimized) return NO;
190
191 return info()->optimizedByDyld() || info()->optimizedByDyldClosure();
192 }
193
194 bool header_info::hasPreoptimizedProtocols() const
195 {
196 // preoptimization disabled for some reason
197 if (!preoptimized) return NO;
198
199 return info()->optimizedByDyld() || info()->optimizedByDyldClosure();
200 }
201
202
203 objc_selopt_t *preoptimizedSelectors(void)
204 {
205 return opt ? opt->selopt() : nil;
206 }
207
208 bool sharedCacheSupportsProtocolRoots(void)
209 {
210 return (opt != nil) && (opt->protocolopt2() != nil);
211 }
212
213
214 Protocol *getSharedCachePreoptimizedProtocol(const char *name)
215 {
216 // Look in the new table if we have it
217 if (objc_protocolopt2_t *protocols2 = opt ? opt->protocolopt2() : nil) {
218 // Note, we have to pass the lambda directly here as otherwise we would try
219 // message copy and autorelease.
220 return (Protocol *)protocols2->getProtocol(name, [](const void* hi) -> bool {
221 return ((header_info *)hi)->isLoaded();
222 });
223 }
224
225 objc_protocolopt_t *protocols = opt ? opt->protocolopt() : nil;
226 if (!protocols) return nil;
227
228 return (Protocol *)protocols->getProtocol(name);
229 }
230
231
232 Protocol *getPreoptimizedProtocol(const char *name)
233 {
234 // Try table from dyld closure first. It was built to ignore the dupes it
235 // knows will come from the cache, so anything left in here was there when
236 // we launched
237 Protocol *result = nil;
238 // Note, we have to pass the lambda directly here as otherwise we would try
239 // message copy and autorelease.
240 _dyld_for_each_objc_protocol(name, [&result](void* protocolPtr, bool isLoaded, bool* stop) {
241 // Skip images which aren't loaded. This supports the case where dyld
242 // might soft link an image from the main binary so its possibly not
243 // loaded yet.
244 if (!isLoaded)
245 return;
246
247 // Found a loaded image with this class name, so stop the search
248 result = (Protocol *)protocolPtr;
249 *stop = true;
250 });
251 if (result) return result;
252
253 return getSharedCachePreoptimizedProtocol(name);
254 }
255
256
257 unsigned int getPreoptimizedClassUnreasonableCount()
258 {
259 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
260 if (!classes) return 0;
261
262 // This is an overestimate: each set of duplicates
263 // gets double-counted in `capacity` as well.
264 return classes->capacity + classes->duplicateCount();
265 }
266
267
268 Class getPreoptimizedClass(const char *name)
269 {
270 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
271 if (!classes) return nil;
272
273 // Try table from dyld closure first. It was built to ignore the dupes it
274 // knows will come from the cache, so anything left in here was there when
275 // we launched
276 Class result = nil;
277 // Note, we have to pass the lambda directly here as otherwise we would try
278 // message copy and autorelease.
279 _dyld_for_each_objc_class(name, [&result](void* classPtr, bool isLoaded, bool* stop) {
280 // Skip images which aren't loaded. This supports the case where dyld
281 // might soft link an image from the main binary so its possibly not
282 // loaded yet.
283 if (!isLoaded)
284 return;
285
286 // Found a loaded image with this class name, so stop the search
287 result = (Class)classPtr;
288 *stop = true;
289 });
290 if (result) return result;
291
292 void *cls;
293 void *hi;
294 uint32_t count = classes->getClassAndHeader(name, cls, hi);
295 if (count == 1 && ((header_info *)hi)->isLoaded()) {
296 // exactly one matching class, and its image is loaded
297 return (Class)cls;
298 }
299 else if (count > 1) {
300 // more than one matching class - find one that is loaded
301 void *clslist[count];
302 void *hilist[count];
303 classes->getClassesAndHeaders(name, clslist, hilist);
304 for (uint32_t i = 0; i < count; i++) {
305 if (((header_info *)hilist[i])->isLoaded()) {
306 return (Class)clslist[i];
307 }
308 }
309 }
310
311 // no match that is loaded
312 return nil;
313 }
314
315
316 Class* copyPreoptimizedClasses(const char *name, int *outCount)
317 {
318 *outCount = 0;
319
320 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
321 if (!classes) return nil;
322
323 void *cls;
324 void *hi;
325 uint32_t count = classes->getClassAndHeader(name, cls, hi);
326 if (count == 0) return nil;
327
328 Class *result = (Class *)calloc(count, sizeof(Class));
329 if (count == 1 && ((header_info *)hi)->isLoaded()) {
330 // exactly one matching class, and its image is loaded
331 result[(*outCount)++] = (Class)cls;
332 return result;
333 }
334 else if (count > 1) {
335 // more than one matching class - find those that are loaded
336 void *clslist[count];
337 void *hilist[count];
338 classes->getClassesAndHeaders(name, clslist, hilist);
339 for (uint32_t i = 0; i < count; i++) {
340 if (((header_info *)hilist[i])->isLoaded()) {
341 result[(*outCount)++] = (Class)clslist[i];
342 }
343 }
344
345 if (*outCount == 0) {
346 // found multiple classes with that name, but none are loaded
347 free(result);
348 result = nil;
349 }
350 return result;
351 }
352
353 // no match that is loaded
354 return nil;
355 }
356
357 namespace objc_opt {
358 struct objc_headeropt_ro_t {
359 uint32_t count;
360 uint32_t entsize;
361 header_info headers[0]; // sorted by mhdr address
362
363 header_info *get(const headerType *mhdr)
364 {
365 ASSERT(entsize == sizeof(header_info));
366
367 int32_t start = 0;
368 int32_t end = count;
369 while (start <= end) {
370 int32_t i = (start+end)/2;
371 header_info *hi = headers+i;
372 if (mhdr == hi->mhdr()) return hi;
373 else if (mhdr < hi->mhdr()) end = i-1;
374 else start = i+1;
375 }
376
377 #if DEBUG
378 for (uint32_t i = 0; i < count; i++) {
379 header_info *hi = headers+i;
380 if (mhdr == hi->mhdr()) {
381 _objc_fatal("failed to find header %p (%d/%d)",
382 mhdr, i, count);
383 }
384 }
385 #endif
386
387 return nil;
388 }
389 };
390
391 struct objc_headeropt_rw_t {
392 uint32_t count;
393 uint32_t entsize;
394 header_info_rw headers[0]; // sorted by mhdr address
395 };
396 };
397
398
399 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
400 {
401 #if !__OBJC2__
402 // fixme old ABI shared cache doesn't prepare these properly
403 return nil;
404 #endif
405
406 objc_headeropt_ro_t *hinfos = opt ? opt->headeropt_ro() : nil;
407 if (hinfos) return hinfos->get(mhdr);
408 else return nil;
409 }
410
411
412 header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
413 {
414 #if !__OBJC2__
415 // fixme old ABI shared cache doesn't prepare these properly
416 return nil;
417 #endif
418
419 objc_headeropt_ro_t *hinfoRO = opt ? opt->headeropt_ro() : nil;
420 objc_headeropt_rw_t *hinfoRW = opt ? opt->headeropt_rw() : nil;
421 if (!hinfoRO || !hinfoRW) {
422 _objc_fatal("preoptimized header_info missing for %s (%p %p %p)",
423 hdr->fname(), hdr, hinfoRO, hinfoRW);
424 }
425 int32_t index = (int32_t)(hdr - hinfoRO->headers);
426 ASSERT(hinfoRW->entsize == sizeof(header_info_rw));
427 return &hinfoRW->headers[index];
428 }
429
430
431 void preopt_init(void)
432 {
433 // Get the memory region occupied by the shared cache.
434 size_t length;
435 const uintptr_t start = (uintptr_t)_dyld_get_shared_cache_range(&length);
436
437 if (start) {
438 objc::dataSegmentsRanges.add(start, start + length);
439 }
440
441 // `opt` not set at compile time in order to detect too-early usage
442 const char *failure = nil;
443 opt = &_objc_opt_data;
444
445 if (DisablePreopt) {
446 // OBJC_DISABLE_PREOPTIMIZATION is set
447 // If opt->version != VERSION then you continue at your own risk.
448 failure = "(by OBJC_DISABLE_PREOPTIMIZATION)";
449 }
450 else if (opt->version != objc_opt::VERSION) {
451 // This shouldn't happen. You probably forgot to edit objc-sel-table.s.
452 // If dyld really did write the wrong optimization version,
453 // then we must halt because we don't know what bits dyld twiddled.
454 _objc_fatal("bad objc preopt version (want %d, got %d)",
455 objc_opt::VERSION, opt->version);
456 }
457 else if (!opt->selopt() || !opt->headeropt_ro()) {
458 // One of the tables is missing.
459 failure = "(dyld shared cache is absent or out of date)";
460 }
461
462 if (failure) {
463 // All preoptimized selector references are invalid.
464 preoptimized = NO;
465 opt = nil;
466 disableSharedCacheOptimizations();
467
468 if (PrintPreopt) {
469 _objc_inform("PREOPTIMIZATION: is DISABLED %s", failure);
470 }
471 }
472 else {
473 // Valid optimization data written by dyld shared cache
474 preoptimized = YES;
475
476 if (PrintPreopt) {
477 _objc_inform("PREOPTIMIZATION: is ENABLED "
478 "(version %d)", opt->version);
479 }
480 }
481 }
482
483
484 __END_DECLS
485
486 // SUPPORT_PREOPT
487 #endif