]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-opt.mm
objc4-551.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 using namespace objc_opt;
32
33
34 #if !SUPPORT_PREOPT
35 // Preoptimization not supported on this platform.
36
37 bool isPreoptimized(void)
38 {
39 return false;
40 }
41
42 const objc_selopt_t *preoptimizedSelectors(void)
43 {
44 return nil;
45 }
46
47 Class getPreoptimizedClass(const char *name)
48 {
49 return nil;
50 }
51
52 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
53 {
54 return nil;
55 }
56
57 void preopt_init(void)
58 {
59 disableSharedCacheOptimizations();
60
61 if (PrintPreopt) {
62 _objc_inform("PREOPTIMIZATION: is DISABLED "
63 "(not supported on ths platform)");
64 }
65 }
66
67
68 // !SUPPORT_PREOPT
69 #else
70 // SUPPORT_PREOPT
71
72
73 #include <objc-shared-cache.h>
74
75 __BEGIN_DECLS
76
77 // preopt: the actual opt used at runtime
78 // _objc_opt_data: opt data possibly written by dyld
79 // empty_opt_data: empty data to use if dyld didn't cooperate or DisablePreopt
80
81 static const objc_opt_t *opt = nil;
82 static bool preoptimized;
83
84 extern const objc_opt_t _objc_opt_data; // in __TEXT, __objc_opt_ro
85 static const uint32_t empty_opt_data[] = OPT_INITIALIZER;
86
87 bool isPreoptimized(void)
88 {
89 return preoptimized;
90 }
91
92
93 const objc_selopt_t *preoptimizedSelectors(void)
94 {
95 assert(opt);
96 return opt->selopt();
97 }
98
99 Class getPreoptimizedClass(const char *name)
100 {
101 assert(opt);
102 objc_clsopt_t *classes = opt->clsopt();
103 if (!classes) return nil;
104
105 void *cls;
106 void *hi;
107 uint32_t count = classes->getClassAndHeader(name, cls, hi);
108 if (count == 1 && ((header_info *)hi)->loaded) {
109 // exactly one matching class, and it's image is loaded
110 return (Class)cls;
111 }
112 else if (count > 1) {
113 // more than one matching class - find one that is loaded
114 void *clslist[count];
115 void *hilist[count];
116 classes->getClassesAndHeaders(name, clslist, hilist);
117 for (uint32_t i = 0; i < count; i++) {
118 if (((header_info *)hilist[i])->loaded) {
119 return (Class)clslist[i];
120 }
121 }
122 }
123
124 // no match that is loaded
125 return nil;
126 }
127
128 namespace objc_opt {
129 struct objc_headeropt_t {
130 uint32_t count;
131 uint32_t entsize;
132 header_info headers[0]; // sorted by mhdr address
133
134 header_info *get(const headerType *mhdr)
135 {
136 assert(entsize == sizeof(header_info));
137
138 int32_t start = 0;
139 int32_t end = count;
140 while (start <= end) {
141 int32_t i = (start+end)/2;
142 header_info *hi = headers+i;
143 if (mhdr == hi->mhdr) return hi;
144 else if (mhdr < hi->mhdr) end = i-1;
145 else start = i+1;
146 }
147
148 #if !NDEBUG
149 for (uint32_t i = 0; i < count; i++) {
150 header_info *hi = headers+i;
151 if (mhdr == hi->mhdr) {
152 _objc_fatal("failed to find header %p (%d/%d)",
153 mhdr, i, count);
154 }
155 }
156 #endif
157
158 return nil;
159 }
160 };
161 };
162
163
164 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
165 {
166 assert(opt);
167 objc_headeropt_t *hinfos = opt->headeropt();
168 if (hinfos) return hinfos->get(mhdr);
169 else return nil;
170 }
171
172
173 void preopt_init(void)
174 {
175 // `opt` not set at compile time in order to detect too-early usage
176 const char *failure = nil;
177 opt = &_objc_opt_data;
178
179 if (DisablePreopt) {
180 // OBJC_DISABLE_PREOPTIMIZATION is set
181 // If opt->version != VERSION then you continue at your own risk.
182 failure = "(by OBJC_DISABLE_PREOPTIMIZATION)";
183 }
184 else if (opt->version != objc_opt::VERSION) {
185 // This shouldn't happen. You probably forgot to
186 // change OPT_INITIALIZER and objc-sel-table.s.
187 // If dyld really did write the wrong optimization version,
188 // then we must halt because we don't know what bits dyld twiddled.
189 _objc_fatal("bad objc preopt version (want %d, got %d)",
190 objc_opt::VERSION, opt->version);
191 }
192 else if (!opt->selopt() || !opt->headeropt()) {
193 // One of the tables is missing.
194 failure = "(dyld shared cache is absent or out of date)";
195 }
196 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
197 else if (UseGC) {
198 // GC is on, which renames some selectors
199 // Non-selector optimizations are still valid, but we don't have
200 // any of those yet
201 failure = "(GC is on)";
202 }
203 #endif
204
205 if (failure) {
206 // All preoptimized selector references are invalid.
207 preoptimized = NO;
208 opt = (objc_opt_t *)empty_opt_data;
209 disableSharedCacheOptimizations();
210
211 if (PrintPreopt) {
212 _objc_inform("PREOPTIMIZATION: is DISABLED %s", failure);
213 }
214 }
215 else {
216 // Valid optimization data written by dyld shared cache
217 preoptimized = YES;
218
219 if (PrintPreopt) {
220 _objc_inform("PREOPTIMIZATION: is ENABLED "
221 "(version %d)", opt->version);
222 }
223 }
224 }
225
226
227 __END_DECLS
228
229 // SUPPORT_PREOPT
230 #endif