]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-opt.mm
objc4-750.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 bool sharedRegionContains(const void *ptr)
79 {
80 return false;
81 }
82
83 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
84 {
85 return nil;
86 }
87
88 header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
89 {
90 return nil;
91 }
92
93 void preopt_init(void)
94 {
95 disableSharedCacheOptimizations();
96
97 if (PrintPreopt) {
98 _objc_inform("PREOPTIMIZATION: is DISABLED "
99 "(not supported on ths platform)");
100 }
101 }
102
103
104 // !SUPPORT_PREOPT
105 #else
106 // SUPPORT_PREOPT
107
108 #include <objc-shared-cache.h>
109
110 using objc_opt::objc_stringhash_offset_t;
111 using objc_opt::objc_protocolopt_t;
112 using objc_opt::objc_clsopt_t;
113 using objc_opt::objc_headeropt_ro_t;
114 using objc_opt::objc_headeropt_rw_t;
115 using objc_opt::objc_opt_t;
116
117 __BEGIN_DECLS
118
119 // preopt: the actual opt used at runtime (nil or &_objc_opt_data)
120 // _objc_opt_data: opt data possibly written by dyld
121 // opt is initialized to ~0 to detect incorrect use before preopt_init()
122
123 static const objc_opt_t *opt = (objc_opt_t *)~0;
124 static uintptr_t shared_cache_start;
125 static uintptr_t shared_cache_end;
126 static bool preoptimized;
127
128 extern const objc_opt_t _objc_opt_data; // in __TEXT, __objc_opt_ro
129
130 /***********************************************************************
131 * Return YES if we have a valid optimized shared cache.
132 **********************************************************************/
133 bool isPreoptimized(void)
134 {
135 return preoptimized;
136 }
137
138
139 /***********************************************************************
140 * Return YES if the shared cache does not have any classes with
141 * missing weak superclasses.
142 **********************************************************************/
143 bool noMissingWeakSuperclasses(void)
144 {
145 if (!preoptimized) return NO; // might have missing weak superclasses
146 return opt->flags & objc_opt::NoMissingWeakSuperclasses;
147 }
148
149
150 /***********************************************************************
151 * Return YES if this image's dyld shared cache optimizations are valid.
152 **********************************************************************/
153 bool header_info::isPreoptimized() const
154 {
155 // preoptimization disabled for some reason
156 if (!preoptimized) return NO;
157
158 // image not from shared cache, or not fixed inside shared cache
159 if (!info()->optimizedByDyld()) return NO;
160
161 return YES;
162 }
163
164
165 objc_selopt_t *preoptimizedSelectors(void)
166 {
167 return opt ? opt->selopt() : nil;
168 }
169
170
171 Protocol *getPreoptimizedProtocol(const char *name)
172 {
173 objc_protocolopt_t *protocols = opt ? opt->protocolopt() : nil;
174 if (!protocols) return nil;
175
176 return (Protocol *)protocols->getProtocol(name);
177 }
178
179
180 unsigned int getPreoptimizedClassUnreasonableCount()
181 {
182 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
183 if (!classes) return 0;
184
185 // This is an overestimate: each set of duplicates
186 // gets double-counted in `capacity` as well.
187 return classes->capacity + classes->duplicateCount();
188 }
189
190
191 Class getPreoptimizedClass(const char *name)
192 {
193 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
194 if (!classes) return nil;
195
196 void *cls;
197 void *hi;
198 uint32_t count = classes->getClassAndHeader(name, cls, hi);
199 if (count == 1 && ((header_info *)hi)->isLoaded()) {
200 // exactly one matching class, and its image is loaded
201 return (Class)cls;
202 }
203 else if (count > 1) {
204 // more than one matching class - find one that is loaded
205 void *clslist[count];
206 void *hilist[count];
207 classes->getClassesAndHeaders(name, clslist, hilist);
208 for (uint32_t i = 0; i < count; i++) {
209 if (((header_info *)hilist[i])->isLoaded()) {
210 return (Class)clslist[i];
211 }
212 }
213 }
214
215 // no match that is loaded
216 return nil;
217 }
218
219
220 Class* copyPreoptimizedClasses(const char *name, int *outCount)
221 {
222 *outCount = 0;
223
224 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
225 if (!classes) return nil;
226
227 void *cls;
228 void *hi;
229 uint32_t count = classes->getClassAndHeader(name, cls, hi);
230 if (count == 0) return nil;
231
232 Class *result = (Class *)calloc(count, sizeof(Class));
233 if (count == 1 && ((header_info *)hi)->isLoaded()) {
234 // exactly one matching class, and its image is loaded
235 result[(*outCount)++] = (Class)cls;
236 return result;
237 }
238 else if (count > 1) {
239 // more than one matching class - find those that are loaded
240 void *clslist[count];
241 void *hilist[count];
242 classes->getClassesAndHeaders(name, clslist, hilist);
243 for (uint32_t i = 0; i < count; i++) {
244 if (((header_info *)hilist[i])->isLoaded()) {
245 result[(*outCount)++] = (Class)clslist[i];
246 }
247 }
248
249 if (*outCount == 0) {
250 // found multiple classes with that name, but none are loaded
251 free(result);
252 result = nil;
253 }
254 return result;
255 }
256
257 // no match that is loaded
258 return nil;
259 }
260
261 /***********************************************************************
262 * Return YES if the given pointer lies within the shared cache.
263 * If the shared cache is not set up or is not valid,
264 **********************************************************************/
265 bool sharedRegionContains(const void *ptr)
266 {
267 uintptr_t address = (uintptr_t)ptr;
268 return shared_cache_start <= address && address < shared_cache_end;
269 }
270
271 namespace objc_opt {
272 struct objc_headeropt_ro_t {
273 uint32_t count;
274 uint32_t entsize;
275 header_info headers[0]; // sorted by mhdr address
276
277 header_info *get(const headerType *mhdr)
278 {
279 assert(entsize == sizeof(header_info));
280
281 int32_t start = 0;
282 int32_t end = count;
283 while (start <= end) {
284 int32_t i = (start+end)/2;
285 header_info *hi = headers+i;
286 if (mhdr == hi->mhdr()) return hi;
287 else if (mhdr < hi->mhdr()) end = i-1;
288 else start = i+1;
289 }
290
291 #if DEBUG
292 for (uint32_t i = 0; i < count; i++) {
293 header_info *hi = headers+i;
294 if (mhdr == hi->mhdr()) {
295 _objc_fatal("failed to find header %p (%d/%d)",
296 mhdr, i, count);
297 }
298 }
299 #endif
300
301 return nil;
302 }
303 };
304
305 struct objc_headeropt_rw_t {
306 uint32_t count;
307 uint32_t entsize;
308 header_info_rw headers[0]; // sorted by mhdr address
309 };
310 };
311
312
313 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
314 {
315 #if !__OBJC2__
316 // fixme old ABI shared cache doesn't prepare these properly
317 return nil;
318 #endif
319
320 objc_headeropt_ro_t *hinfos = opt ? opt->headeropt_ro() : nil;
321 if (hinfos) return hinfos->get(mhdr);
322 else return nil;
323 }
324
325
326 header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
327 {
328 #if !__OBJC2__
329 // fixme old ABI shared cache doesn't prepare these properly
330 return nil;
331 #endif
332
333 objc_headeropt_ro_t *hinfoRO = opt ? opt->headeropt_ro() : nil;
334 objc_headeropt_rw_t *hinfoRW = opt ? opt->headeropt_rw() : nil;
335 if (!hinfoRO || !hinfoRW) {
336 _objc_fatal("preoptimized header_info missing for %s (%p %p %p)",
337 hdr->fname(), hdr, hinfoRO, hinfoRW);
338 }
339 int32_t index = (int32_t)(hdr - hinfoRO->headers);
340 assert(hinfoRW->entsize == sizeof(header_info_rw));
341 return &hinfoRW->headers[index];
342 }
343
344
345 void preopt_init(void)
346 {
347 // Get the memory region occupied by the shared cache.
348 size_t length;
349 const void *start = _dyld_get_shared_cache_range(&length);
350 if (start) {
351 shared_cache_start = (uintptr_t)start;
352 shared_cache_end = shared_cache_start + length;
353 } else {
354 shared_cache_start = shared_cache_end = 0;
355 }
356
357 // `opt` not set at compile time in order to detect too-early usage
358 const char *failure = nil;
359 opt = &_objc_opt_data;
360
361 if (DisablePreopt) {
362 // OBJC_DISABLE_PREOPTIMIZATION is set
363 // If opt->version != VERSION then you continue at your own risk.
364 failure = "(by OBJC_DISABLE_PREOPTIMIZATION)";
365 }
366 else if (opt->version != objc_opt::VERSION) {
367 // This shouldn't happen. You probably forgot to edit objc-sel-table.s.
368 // If dyld really did write the wrong optimization version,
369 // then we must halt because we don't know what bits dyld twiddled.
370 _objc_fatal("bad objc preopt version (want %d, got %d)",
371 objc_opt::VERSION, opt->version);
372 }
373 else if (!opt->selopt() || !opt->headeropt_ro()) {
374 // One of the tables is missing.
375 failure = "(dyld shared cache is absent or out of date)";
376 }
377
378 if (failure) {
379 // All preoptimized selector references are invalid.
380 preoptimized = NO;
381 opt = nil;
382 disableSharedCacheOptimizations();
383
384 if (PrintPreopt) {
385 _objc_inform("PREOPTIMIZATION: is DISABLED %s", failure);
386 }
387 }
388 else {
389 // Valid optimization data written by dyld shared cache
390 preoptimized = YES;
391
392 if (PrintPreopt) {
393 _objc_inform("PREOPTIMIZATION: is ENABLED "
394 "(version %d)", opt->version);
395 }
396 }
397 }
398
399
400 __END_DECLS
401
402 // SUPPORT_PREOPT
403 #endif