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