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