]>
Commit | Line | Data |
---|---|---|
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 | typedef typename A::P::uint_t pint_t; | |
155 | ||
156 | static void visitMethodList(objc_method_list<A> *mlist, V& visitor) | |
157 | { | |
158 | for (uint32_t m = 0; m < mlist->getCount(); m++) { | |
159 | uint64_t oldValue = mlist->method_list[m].getName(); | |
160 | uint64_t newValue = visitor.visit(oldValue); | |
161 | mlist->method_list[m].setName(newValue); | |
162 | } | |
163 | mlist->setFixedUp(true); | |
164 | } | |
165 | ||
166 | static void visitMethodDescriptionList(objc_method_description_list<A> *mlist, V& visitor) | |
167 | { | |
168 | for (uint32_t m = 0; m < mlist->getCount(); m++) { | |
169 | uint64_t oldValue = mlist->list[m].getName(); | |
170 | uint64_t newValue = visitor.visit(oldValue); | |
171 | mlist->list[m].setName(newValue); | |
172 | } | |
173 | } | |
174 | ||
175 | public: | |
176 | ||
177 | static void update(SharedCache<A>* cache, const macho_header<P>* header, | |
178 | V& visitor) | |
179 | { | |
180 | ArraySection<A, objc_module<A> > | |
181 | modules(cache, header, "__OBJC", "__module_info"); | |
182 | for (uint64_t m = 0; m < modules.count(); m++) { | |
183 | objc_symtab<A> *symtab = modules.get(m).getSymtab(cache); | |
184 | if (!symtab) continue; | |
185 | ||
186 | // Method lists in classes | |
187 | for (uint64_t c = 0; c < symtab->getClassCount(); c++) { | |
188 | objc_class<A> *cls = symtab->getClass(cache, c); | |
189 | objc_class<A> *isa = cls->getIsa(cache); | |
190 | objc_method_list<A> *mlist; | |
191 | if ((mlist = cls->getMethodList(cache))) { | |
192 | visitMethodList(mlist, visitor); | |
193 | } | |
194 | if ((mlist = isa->getMethodList(cache))) { | |
195 | visitMethodList(mlist, visitor); | |
196 | } | |
197 | } | |
198 | ||
199 | // Method lists from categories | |
200 | for (uint64_t c = 0; c < symtab->getCategoryCount(); c++) { | |
201 | objc_category<A> *cat = symtab->getCategory(cache, c); | |
202 | objc_method_list<A> *mlist; | |
203 | if ((mlist = cat->getInstanceMethods(cache))) { | |
204 | visitMethodList(mlist, visitor); | |
205 | } | |
206 | if ((mlist = cat->getClassMethods(cache))) { | |
207 | visitMethodList(mlist, visitor); | |
208 | } | |
209 | } | |
210 | } | |
211 | ||
212 | // Method description lists from protocols | |
213 | ArraySection<A, objc_protocol<A> > | |
214 | protocols(cache, header, "__OBJC", "__protocol"); | |
215 | for (uint64_t p = 0; p < protocols.count(); p++) { | |
216 | objc_protocol<A>& protocol = protocols.get(p); | |
217 | objc_method_description_list<A> *mlist; | |
218 | if ((mlist = protocol.getInstanceMethodDescriptions(cache))) { | |
219 | visitMethodDescriptionList(mlist, visitor); | |
220 | } | |
221 | if ((mlist = protocol.getClassMethodDescriptions(cache))) { | |
222 | visitMethodDescriptionList(mlist, visitor); | |
223 | } | |
224 | } | |
225 | ||
226 | // Message refs | |
227 | PointerSection<A, const char *> selrefs(cache, header, "__OBJC", "__message_refs"); | |
228 | for (pint_t s = 0; s < selrefs.count(); s++) { | |
229 | pint_t oldValue = selrefs.getVMAddress(s); | |
230 | pint_t newValue = visitor.visit(oldValue); | |
231 | selrefs.setVMAddress(s, newValue); | |
232 | } | |
233 | } | |
234 | }; |