2 * Copyright (c) 2012 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 Management of optimizations in the dyld shared cache
29 #include "objc-private.h"
31 using namespace objc_opt;
35 // Preoptimization not supported on this platform.
37 bool isPreoptimized(void)
42 const objc_selopt_t *preoptimizedSelectors(void)
47 Class getPreoptimizedClass(const char *name)
52 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
57 void preopt_init(void)
59 disableSharedCacheOptimizations();
62 _objc_inform("PREOPTIMIZATION: is DISABLED "
63 "(not supported on ths platform)");
73 #include <objc-shared-cache.h>
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
81 static const objc_opt_t *opt = nil;
82 static bool preoptimized;
84 extern const objc_opt_t _objc_opt_data; // in __TEXT, __objc_opt_ro
85 static const uint32_t empty_opt_data[] = OPT_INITIALIZER;
87 bool isPreoptimized(void)
93 const objc_selopt_t *preoptimizedSelectors(void)
99 Class getPreoptimizedClass(const char *name)
102 objc_clsopt_t *classes = opt->clsopt();
103 if (!classes) return nil;
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
112 else if (count > 1) {
113 // more than one matching class - find one that is loaded
114 void *clslist[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];
124 // no match that is loaded
129 struct objc_headeropt_t {
132 header_info headers[0]; // sorted by mhdr address
134 header_info *get(const headerType *mhdr)
136 assert(entsize == sizeof(header_info));
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;
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)",
164 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
167 objc_headeropt_t *hinfos = opt->headeropt();
168 if (hinfos) return hinfos->get(mhdr);
173 void preopt_init(void)
175 // `opt` not set at compile time in order to detect too-early usage
176 const char *failure = nil;
177 opt = &_objc_opt_data;
180 // OBJC_DISABLE_PREOPTIMIZATION is set
181 // If opt->version != VERSION then you continue at your own risk.
182 failure = "(by OBJC_DISABLE_PREOPTIMIZATION)";
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);
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)";
196 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
198 // GC is on, which renames some selectors
199 // Non-selector optimizations are still valid, but we don't have
201 failure = "(GC is on)";
206 // All preoptimized selector references are invalid.
208 opt = (objc_opt_t *)empty_opt_data;
209 disableSharedCacheOptimizations();
212 _objc_inform("PREOPTIMIZATION: is DISABLED %s", failure);
216 // Valid optimization data written by dyld shared cache
220 _objc_inform("PREOPTIMIZATION: is ENABLED "
221 "(version %d)", opt->version);