]> git.saurik.com Git - apple/dyld.git/blob - launch-cache/ObjCLegacyAbstraction.hpp
dyld-353.2.3.tar.gz
[apple/dyld.git] / launch-cache / ObjCLegacyAbstraction.hpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2008-2010 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 #define OBJC_IMAGE_SUPPORTS_GC (1<<1)
26 #define OBJC_IMAGE_REQUIRES_GC (1<<2)
27
28 template <typename A>
29 struct objc_image_info {
30 uint32_t version;
31 uint32_t flags;
32
33 uint32_t getFlags() INLINE { return A::P::E::get32(flags); }
34
35 bool supportsGCFlagSet() INLINE { return getFlags() & OBJC_IMAGE_SUPPORTS_GC; }
36 bool requiresGCFlagSet() INLINE { return getFlags() & OBJC_IMAGE_REQUIRES_GC; }
37
38 void setFlag(uint32_t bits) INLINE { uint32_t old = A::P::E::get32(flags); A::P::E::set32(flags, old | bits); }
39 void setOptimizedByDyld() INLINE { setFlag(1<<3); }
40 };
41
42 template <typename A>
43 struct objc_method {
44 uint32_t method_name; // SEL
45 uint32_t method_types; // char *
46 uint32_t method_imp; // IMP
47
48 uint32_t getName() const INLINE { return A::P::E::get32(method_name); }
49 void setName(uint32_t newName) INLINE { A::P::E::set32(method_name, newName); }
50 };
51
52 template <typename A>
53 struct objc_method_list {
54 enum { OBJC_FIXED_UP = 1771 };
55 uint32_t obsolete; // struct objc_method_list *
56 uint32_t method_count; // int
57 struct objc_method<A> method_list[0];
58
59 uint32_t getCount() const INLINE { return A::P::E::get32(method_count); }
60 void setFixedUp(bool fixed) INLINE { A::P::E::set32(obsolete, fixed ? OBJC_FIXED_UP : 0); }
61 };
62
63 template <typename A>
64 struct objc_class {
65 uint32_t isa; // struct objc_class *
66 uint32_t super_class; // struct objc_class *
67 uint32_t name; // const char *
68 uint32_t version; // long
69 uint32_t info; // long
70 uint32_t instance_size; // long
71 uint32_t ivars; // struct objc_ivar_list *
72 uint32_t methodList; // struct objc_method_list *
73 uint32_t method_cache; // struct objc_cache *
74 uint32_t protocols; // objc_protocol_list *
75 uint32_t ivar_layout; // const char *
76 uint32_t ext; // struct objc_class_ext *
77
78 struct objc_class<A> *getIsa(SharedCache<A> *cache) const INLINE { return (struct objc_class<A> *)cache->mappedAddressForVMAddress(A::P::E::get32(isa)); }
79 struct objc_method_list<A> *getMethodList(SharedCache<A> *cache) const INLINE { return (struct objc_method_list<A> *)cache->mappedAddressForVMAddress(A::P::E::get32(methodList)); }
80 };
81
82 template <typename A>
83 struct objc_category {
84 uint32_t category_name; // char *
85 uint32_t class_name; // char *
86 uint32_t instance_methods; // struct objc_method_list *
87 uint32_t class_methods; // struct objc_method_list *
88 uint32_t protocols; // objc_protocol_list *
89 uint32_t size; // uint32_t
90 uint32_t instance_properties; // struct objc_property_list *
91
92 struct objc_method_list<A> *getInstanceMethods(SharedCache<A> *cache) const INLINE { return (struct objc_method_list<A> *)cache->mappedAddressForVMAddress(A::P::E::get32(instance_methods)); }
93 struct objc_method_list<A> *getClassMethods(SharedCache<A> *cache) const INLINE { return (struct objc_method_list<A> *)cache->mappedAddressForVMAddress(A::P::E::get32(class_methods)); }
94 };
95
96 template <typename A>
97 struct objc_symtab {
98 uint32_t sel_ref_cnt; // unsigned long
99 uint32_t refs; // SEL *
100 uint16_t cls_def_cnt; // unsigned short
101 uint16_t cat_def_cnt; // unsigned short
102 uint32_t defs[0]; // void *
103
104 uint16_t getClassCount(void) const INLINE { return A::P::E::get16(cls_def_cnt); }
105 uint16_t getCategoryCount(void) const INLINE { return A::P::E::get16(cat_def_cnt); }
106 struct objc_class<A> *getClass(SharedCache<A> *cache, int index) const INLINE { return (struct objc_class<A> *)cache->mappedAddressForVMAddress(A::P::E::get32(defs[index])); }
107 struct objc_category<A> *getCategory(SharedCache<A> *cache, int index) const INLINE { return (struct objc_category<A> *)cache->mappedAddressForVMAddress(A::P::E::get32(defs[getClassCount() + index])); }
108 };
109
110 template <typename A>
111 struct objc_module {
112 uint32_t version; // unsigned long
113 uint32_t size; // unsigned long
114 uint32_t name; // char*
115 uint32_t symtab; // Symtab
116
117 struct objc_symtab<A> *getSymtab(SharedCache<A> *cache) const INLINE { return (struct objc_symtab<A> *)cache->mappedAddressForVMAddress(A::P::E::get32(symtab)); }
118 };
119
120 template <typename A>
121 struct objc_method_description {
122 uint32_t name; // SEL
123 uint32_t types; // char *
124
125 uint32_t getName() const INLINE { return A::P::E::get32(name); }
126 void setName(uint32_t newName) INLINE { A::P::E::set32(name, newName); }
127 };
128
129 template <typename A>
130 struct objc_method_description_list {
131 uint32_t count; // int
132 struct objc_method_description<A> list[0];
133
134 uint32_t getCount() const INLINE { return A::P::E::get32(count); }
135 };
136
137 template <typename A>
138 struct objc_protocol {
139 uint32_t isa; // danger! contains strange values!
140 uint32_t protocol_name; // const char *
141 uint32_t protocol_list; // struct objc_protocol_list
142 uint32_t instance_methods; // struct objc_method_description_list *
143 uint32_t class_methods; // struct objc_method_description_list *
144
145 struct objc_method_description_list<A> *getInstanceMethodDescriptions(SharedCache<A> *cache) const INLINE { return (struct objc_method_description_list<A> *)cache->mappedAddressForVMAddress(A::P::E::get32(instance_methods)); }
146 struct objc_method_description_list<A> *getClassMethodDescriptions(SharedCache<A> *cache) const INLINE { return (struct objc_method_description_list<A> *)cache->mappedAddressForVMAddress(A::P::E::get32(class_methods)); }
147 };
148
149
150 template <typename A, typename V>
151 class LegacySelectorUpdater {
152
153 typedef typename A::P P;
154
155 static void visitMethodList(objc_method_list<A> *mlist, V& visitor)
156 {
157 for (uint32_t m = 0; m < mlist->getCount(); m++) {
158 uint64_t oldValue = mlist->method_list[m].getName();
159 uint64_t newValue = visitor.visit(oldValue);
160 mlist->method_list[m].setName(newValue);
161 }
162 mlist->setFixedUp(true);
163 }
164
165 static void visitMethodDescriptionList(objc_method_description_list<A> *mlist, V& visitor)
166 {
167 for (uint32_t m = 0; m < mlist->getCount(); m++) {
168 uint64_t oldValue = mlist->list[m].getName();
169 uint64_t newValue = visitor.visit(oldValue);
170 mlist->list[m].setName(newValue);
171 }
172 }
173
174 public:
175
176 static void update(SharedCache<A>* cache, const macho_header<P>* header,
177 V& visitor)
178 {
179 ArraySection<A, objc_module<A> >
180 modules(cache, header, "__OBJC", "__module_info");
181 for (uint64_t m = 0; m < modules.count(); m++) {
182 objc_symtab<A> *symtab = modules.get(m).getSymtab(cache);
183 if (!symtab) continue;
184
185 // Method lists in classes
186 for (uint64_t c = 0; c < symtab->getClassCount(); c++) {
187 objc_class<A> *cls = symtab->getClass(cache, c);
188 objc_class<A> *isa = cls->getIsa(cache);
189 objc_method_list<A> *mlist;
190 if ((mlist = cls->getMethodList(cache))) {
191 visitMethodList(mlist, visitor);
192 }
193 if ((mlist = isa->getMethodList(cache))) {
194 visitMethodList(mlist, visitor);
195 }
196 }
197
198 // Method lists from categories
199 for (uint64_t c = 0; c < symtab->getCategoryCount(); c++) {
200 objc_category<A> *cat = symtab->getCategory(cache, c);
201 objc_method_list<A> *mlist;
202 if ((mlist = cat->getInstanceMethods(cache))) {
203 visitMethodList(mlist, visitor);
204 }
205 if ((mlist = cat->getClassMethods(cache))) {
206 visitMethodList(mlist, visitor);
207 }
208 }
209 }
210
211 // Method description lists from protocols
212 ArraySection<A, objc_protocol<A> >
213 protocols(cache, header, "__OBJC", "__protocol");
214 for (uint64_t p = 0; p < protocols.count(); p++) {
215 objc_protocol<A>& protocol = protocols.get(p);
216 objc_method_description_list<A> *mlist;
217 if ((mlist = protocol.getInstanceMethodDescriptions(cache))) {
218 visitMethodDescriptionList(mlist, visitor);
219 }
220 if ((mlist = protocol.getClassMethodDescriptions(cache))) {
221 visitMethodDescriptionList(mlist, visitor);
222 }
223 }
224
225 // Message refs
226 PointerSection<A, const char *> selrefs(cache, header, "__OBJC", "__message_refs");
227 for (uint64_t s = 0; s < selrefs.count(); s++) {
228 uint64_t oldValue = selrefs.getUnmapped(s);
229 uint64_t newValue = visitor.visit(oldValue);
230 selrefs.set(s, newValue);
231 }
232 }
233 };