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