-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjc-opt.mm
More file actions
376 lines (304 loc) · 9.8 KB
/
objc-opt.mm
File metadata and controls
376 lines (304 loc) · 9.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Copyright (c) 2012 Apple Inc. All Rights Reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
objc-opt.mm
Management of optimizations in the dyld shared cache
*/
#include "objc-private.h"
#if !SUPPORT_PREOPT
// Preoptimization not supported on this platform.
struct objc_selopt_t;
bool isPreoptimized(void)
{
return false;
}
bool noMissingWeakSuperclasses(void)
{
return false;
}
bool header_info::isPreoptimized() const
{
return false;
}
objc_selopt_t *preoptimizedSelectors(void)
{
return nil;
}
Protocol *getPreoptimizedProtocol(const char *name)
{
return nil;
}
unsigned int getPreoptimizedClassUnreasonableCount()
{
return 0;
}
Class getPreoptimizedClass(const char *name)
{
return nil;
}
Class* copyPreoptimizedClasses(const char *name, int *outCount)
{
*outCount = 0;
return nil;
}
header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
{
return nil;
}
header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
{
return nil;
}
void preopt_init(void)
{
disableSharedCacheOptimizations();
if (PrintPreopt) {
_objc_inform("PREOPTIMIZATION: is DISABLED "
"(not supported on ths platform)");
}
}
// !SUPPORT_PREOPT
#else
// SUPPORT_PREOPT
#include <objc-shared-cache.h>
using objc_opt::objc_stringhash_offset_t;
using objc_opt::objc_protocolopt_t;
using objc_opt::objc_clsopt_t;
using objc_opt::objc_headeropt_ro_t;
using objc_opt::objc_headeropt_rw_t;
using objc_opt::objc_opt_t;
__BEGIN_DECLS
// preopt: the actual opt used at runtime (nil or &_objc_opt_data)
// _objc_opt_data: opt data possibly written by dyld
// opt is initialized to ~0 to detect incorrect use before preopt_init()
static const objc_opt_t *opt = (objc_opt_t *)~0;
static bool preoptimized;
extern const objc_opt_t _objc_opt_data; // in __TEXT, __objc_opt_ro
/***********************************************************************
* Return YES if we have a valid optimized shared cache.
**********************************************************************/
bool isPreoptimized(void)
{
return preoptimized;
}
/***********************************************************************
* Return YES if the shared cache does not have any classes with
* missing weak superclasses.
**********************************************************************/
bool noMissingWeakSuperclasses(void)
{
if (!preoptimized) return NO; // might have missing weak superclasses
return opt->flags & objc_opt::NoMissingWeakSuperclasses;
}
/***********************************************************************
* Return YES if this image's dyld shared cache optimizations are valid.
**********************************************************************/
bool header_info::isPreoptimized() const
{
// preoptimization disabled for some reason
if (!preoptimized) return NO;
// image not from shared cache, or not fixed inside shared cache
if (!info()->optimizedByDyld()) return NO;
return YES;
}
objc_selopt_t *preoptimizedSelectors(void)
{
return opt ? opt->selopt() : nil;
}
Protocol *getPreoptimizedProtocol(const char *name)
{
objc_protocolopt_t *protocols = opt ? opt->protocolopt() : nil;
if (!protocols) return nil;
return (Protocol *)protocols->getProtocol(name);
}
unsigned int getPreoptimizedClassUnreasonableCount()
{
objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
if (!classes) return 0;
// This is an overestimate: each set of duplicates
// gets double-counted in `capacity` as well.
return classes->capacity + classes->duplicateCount();
}
Class getPreoptimizedClass(const char *name)
{
objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
if (!classes) return nil;
void *cls;
void *hi;
uint32_t count = classes->getClassAndHeader(name, cls, hi);
if (count == 1 && ((header_info *)hi)->isLoaded()) {
// exactly one matching class, and its image is loaded
return (Class)cls;
}
else if (count > 1) {
// more than one matching class - find one that is loaded
void *clslist[count];
void *hilist[count];
classes->getClassesAndHeaders(name, clslist, hilist);
for (uint32_t i = 0; i < count; i++) {
if (((header_info *)hilist[i])->isLoaded()) {
return (Class)clslist[i];
}
}
}
// no match that is loaded
return nil;
}
Class* copyPreoptimizedClasses(const char *name, int *outCount)
{
*outCount = 0;
objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
if (!classes) return nil;
void *cls;
void *hi;
uint32_t count = classes->getClassAndHeader(name, cls, hi);
if (count == 0) return nil;
Class *result = (Class *)calloc(count, sizeof(Class));
if (count == 1 && ((header_info *)hi)->isLoaded()) {
// exactly one matching class, and its image is loaded
result[(*outCount)++] = (Class)cls;
return result;
}
else if (count > 1) {
// more than one matching class - find those that are loaded
void *clslist[count];
void *hilist[count];
classes->getClassesAndHeaders(name, clslist, hilist);
for (uint32_t i = 0; i < count; i++) {
if (((header_info *)hilist[i])->isLoaded()) {
result[(*outCount)++] = (Class)clslist[i];
}
}
if (*outCount == 0) {
// found multiple classes with that name, but none are loaded
free(result);
result = nil;
}
return result;
}
// no match that is loaded
return nil;
}
namespace objc_opt {
struct objc_headeropt_ro_t {
uint32_t count;
uint32_t entsize;
header_info headers[0]; // sorted by mhdr address
header_info *get(const headerType *mhdr)
{
assert(entsize == sizeof(header_info));
int32_t start = 0;
int32_t end = count;
while (start <= end) {
int32_t i = (start+end)/2;
header_info *hi = headers+i;
if (mhdr == hi->mhdr()) return hi;
else if (mhdr < hi->mhdr()) end = i-1;
else start = i+1;
}
#if DEBUG
for (uint32_t i = 0; i < count; i++) {
header_info *hi = headers+i;
if (mhdr == hi->mhdr()) {
_objc_fatal("failed to find header %p (%d/%d)",
mhdr, i, count);
}
}
#endif
return nil;
}
};
struct objc_headeropt_rw_t {
uint32_t count;
uint32_t entsize;
header_info_rw headers[0]; // sorted by mhdr address
};
};
header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
{
#if !__OBJC2__
// fixme old ABI shared cache doesn't prepare these properly
return nil;
#endif
objc_headeropt_ro_t *hinfos = opt ? opt->headeropt_ro() : nil;
if (hinfos) return hinfos->get(mhdr);
else return nil;
}
header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
{
#if !__OBJC2__
// fixme old ABI shared cache doesn't prepare these properly
return nil;
#endif
objc_headeropt_ro_t *hinfoRO = opt ? opt->headeropt_ro() : nil;
objc_headeropt_rw_t *hinfoRW = opt ? opt->headeropt_rw() : nil;
if (!hinfoRO || !hinfoRW) {
_objc_fatal("preoptimized header_info missing for %s (%p %p %p)",
hdr->fname(), hdr, hinfoRO, hinfoRW);
}
int32_t index = (int32_t)(hdr - hinfoRO->headers);
assert(hinfoRW->entsize == sizeof(header_info_rw));
return &hinfoRW->headers[index];
}
void preopt_init(void)
{
// `opt` not set at compile time in order to detect too-early usage
const char *failure = nil;
opt = &_objc_opt_data;
if (DisablePreopt) {
// OBJC_DISABLE_PREOPTIMIZATION is set
// If opt->version != VERSION then you continue at your own risk.
failure = "(by OBJC_DISABLE_PREOPTIMIZATION)";
}
else if (opt->version != objc_opt::VERSION) {
// This shouldn't happen. You probably forgot to edit objc-sel-table.s.
// If dyld really did write the wrong optimization version,
// then we must halt because we don't know what bits dyld twiddled.
_objc_fatal("bad objc preopt version (want %d, got %d)",
objc_opt::VERSION, opt->version);
}
else if (!opt->selopt() || !opt->headeropt_ro()) {
// One of the tables is missing.
failure = "(dyld shared cache is absent or out of date)";
}
if (failure) {
// All preoptimized selector references are invalid.
preoptimized = NO;
opt = nil;
disableSharedCacheOptimizations();
if (PrintPreopt) {
_objc_inform("PREOPTIMIZATION: is DISABLED %s", failure);
}
}
else {
// Valid optimization data written by dyld shared cache
preoptimized = YES;
if (PrintPreopt) {
_objc_inform("PREOPTIMIZATION: is ENABLED "
"(version %d)", opt->version);
}
}
}
__END_DECLS
// SUPPORT_PREOPT
#endif