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