]> git.saurik.com Git - apple/dyld.git/blob - launch-cache/ObjCLegacyAbstraction.hpp
ea0329b410cebc192695aa10cc76570df76a9357
[apple/dyld.git] / launch-cache / ObjCLegacyAbstraction.hpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2008 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 template <typename A>
26 struct objc_image_info {
27 uint32_t version;
28 uint32_t flags;
29
30 void setFlag(uint32_t bits) INLINE { uint32_t old = A::P::E::get32(flags); A::P::E::set32(flags, old | bits); }
31 void setSelectorsPrebound() INLINE { setFlag(1<<3); }
32 };
33
34 template <typename A>
35 struct objc_method {
36 uint32_t method_name; // SEL
37 uint32_t method_types; // char *
38 uint32_t method_imp; // IMP
39
40 uint32_t getName() const INLINE { return A::P::E::get32(method_name); }
41 void setName(uint32_t newName) INLINE { A::P::E::set32(method_name, newName); }
42 };
43
44 template <typename A>
45 struct objc_method_list {
46 enum { OBJC_FIXED_UP = 1771 };
47 uint32_t obsolete; // struct objc_method_list *
48 uint32_t method_count; // int
49 struct objc_method<A> method_list[0];
50
51 uint32_t getCount() const INLINE { return A::P::E::get32(method_count); }
52 void setFixedUp(bool fixed) INLINE { A::P::E::set32(obsolete, fixed ? OBJC_FIXED_UP : 0); }
53 };
54
55 template <typename A>
56 struct objc_class {
57 uint32_t isa; // struct objc_class *
58 uint32_t super_class; // struct objc_class *
59 uint32_t name; // const char *
60 uint32_t version; // long
61 uint32_t info; // long
62 uint32_t instance_size; // long
63 uint32_t ivars; // struct objc_ivar_list *
64 uint32_t methodList; // struct objc_method_list *
65 uint32_t method_cache; // struct objc_cache *
66 uint32_t protocols; // objc_protocol_list *
67 uint32_t ivar_layout; // const char *
68 uint32_t ext; // struct objc_class_ext *
69
70 struct objc_class<A> *getIsa(SharedCache<A> *cache) const INLINE { return (struct objc_class<A> *)cache->mappedCacheAddressForAddress(A::P::E::get32(isa)); }
71 struct objc_method_list<A> *getMethodList(SharedCache<A> *cache) const INLINE { return (struct objc_method_list<A> *)cache->mappedCacheAddressForAddress(A::P::E::get32(methodList)); }
72 };
73
74 template <typename A>
75 struct objc_category {
76 uint32_t category_name; // char *
77 uint32_t class_name; // char *
78 uint32_t instance_methods; // struct objc_method_list *
79 uint32_t class_methods; // struct objc_method_list *
80 uint32_t protocols; // objc_protocol_list *
81 uint32_t size; // uint32_t
82 uint32_t instance_properties; // struct objc_property_list *
83
84 struct objc_method_list<A> *getInstanceMethods(SharedCache<A> *cache) const INLINE { return (struct objc_method_list<A> *)cache->mappedCacheAddressForAddress(A::P::E::get32(instance_methods)); }
85 struct objc_method_list<A> *getClassMethods(SharedCache<A> *cache) const INLINE { return (struct objc_method_list<A> *)cache->mappedCacheAddressForAddress(A::P::E::get32(class_methods)); }
86 };
87
88 template <typename A>
89 struct objc_symtab {
90 uint32_t sel_ref_cnt; // unsigned long
91 uint32_t refs; // SEL *
92 uint16_t cls_def_cnt; // unsigned short
93 uint16_t cat_def_cnt; // unsigned short
94 uint32_t defs[0]; // void *
95
96 uint16_t getClassCount(void) const INLINE { return A::P::E::get16(cls_def_cnt); }
97 uint16_t getCategoryCount(void) const INLINE { return A::P::E::get16(cat_def_cnt); }
98 struct objc_class<A> *getClass(SharedCache<A> *cache, int index) const INLINE { return (struct objc_class<A> *)cache->mappedCacheAddressForAddress(A::P::E::get32(defs[index])); }
99 struct objc_category<A> *getCategory(SharedCache<A> *cache, int index) const INLINE { return (struct objc_category<A> *)cache->mappedCacheAddressForAddress(A::P::E::get32(defs[getClassCount() + index])); }
100 };
101
102 template <typename A>
103 struct objc_module {
104 uint32_t version; // unsigned long
105 uint32_t size; // unsigned long
106 uint32_t name; // char*
107 uint32_t symtab; // Symtab
108
109 struct objc_symtab<A> *getSymtab(SharedCache<A> *cache) const INLINE { return (struct objc_symtab<A> *)cache->mappedCacheAddressForAddress(A::P::E::get32(symtab)); }
110 };
111
112 template <typename A>
113 struct objc_method_description {
114 uint32_t name; // SEL
115 uint32_t types; // char *
116
117 uint32_t getName() const INLINE { return A::P::E::get32(name); }
118 void setName(uint32_t newName) INLINE { A::P::E::set32(name, newName); }
119 };
120
121 template <typename A>
122 struct objc_method_description_list {
123 uint32_t count; // int
124 struct objc_method_description<A> list[0];
125
126 uint32_t getCount() const INLINE { return A::P::E::get32(count); }
127 };
128
129 template <typename A>
130 struct objc_protocol {
131 uint32_t isa; // danger! contains strange values!
132 uint32_t protocol_name; // const char *
133 uint32_t protocol_list; // struct objc_protocol_list
134 uint32_t instance_methods; // struct objc_method_description_list *
135 uint32_t class_methods; // struct objc_method_description_list *
136
137 struct objc_method_description_list<A> *getInstanceMethodDescriptions(SharedCache<A> *cache) const INLINE { return (struct objc_method_description_list<A> *)cache->mappedCacheAddressForAddress(A::P::E::get32(instance_methods)); }
138 struct objc_method_description_list<A> *getClassMethodDescriptions(SharedCache<A> *cache) const INLINE { return (struct objc_method_description_list<A> *)cache->mappedCacheAddressForAddress(A::P::E::get32(class_methods)); }
139 };
140
141
142 template <typename A, typename V>
143 class LegacySelectorUpdater {
144
145 typedef typename A::P P;
146
147 static void visitMethodList(objc_method_list<A> *mlist, V& visitor)
148 {
149 for (uint32_t m = 0; m < mlist->getCount(); m++) {
150 uint64_t oldValue = mlist->method_list[m].getName();
151 uint64_t newValue = visitor.visit(oldValue);
152 mlist->method_list[m].setName(newValue);
153 }
154 mlist->setFixedUp(true);
155 }
156
157 static void visitMethodDescriptionList(objc_method_description_list<A> *mlist, V& visitor)
158 {
159 for (uint32_t m = 0; m < mlist->getCount(); m++) {
160 uint64_t oldValue = mlist->list[m].getName();
161 uint64_t newValue = visitor.visit(oldValue);
162 mlist->list[m].setName(newValue);
163 }
164 }
165
166 public:
167
168 static void update(SharedCache<A>* cache, const macho_header<P>* header,
169 V& visitor)
170 {
171 ArraySection<A, objc_module<A> >
172 modules(cache, header, "__OBJC", "__module_info");
173 for (uint64_t m = 0; m < modules.count(); m++) {
174 objc_symtab<A> *symtab = modules.get(m).getSymtab(cache);
175 if (!symtab) continue;
176
177 // Method lists in classes
178 for (uint64_t c = 0; c < symtab->getClassCount(); c++) {
179 objc_class<A> *cls = symtab->getClass(cache, c);
180 objc_class<A> *isa = cls->getIsa(cache);
181 objc_method_list<A> *mlist;
182 if ((mlist = cls->getMethodList(cache))) {
183 visitMethodList(mlist, visitor);
184 }
185 if ((mlist = isa->getMethodList(cache))) {
186 visitMethodList(mlist, visitor);
187 }
188 }
189
190 // Method lists from categories
191 for (uint64_t c = 0; c < symtab->getCategoryCount(); c++) {
192 objc_category<A> *cat = symtab->getCategory(cache, c);
193 objc_method_list<A> *mlist;
194 if ((mlist = cat->getInstanceMethods(cache))) {
195 visitMethodList(mlist, visitor);
196 }
197 if ((mlist = cat->getClassMethods(cache))) {
198 visitMethodList(mlist, visitor);
199 }
200 }
201 }
202
203 // Method description lists from protocols
204 ArraySection<A, objc_protocol<A> >
205 protocols(cache, header, "__OBJC", "__protocol");
206 for (uint64_t p = 0; p < protocols.count(); p++) {
207 objc_protocol<A>& protocol = protocols.get(p);
208 objc_method_description_list<A> *mlist;
209 if ((mlist = protocol.getInstanceMethodDescriptions(cache))) {
210 visitMethodDescriptionList(mlist, visitor);
211 }
212 if ((mlist = protocol.getClassMethodDescriptions(cache))) {
213 visitMethodDescriptionList(mlist, visitor);
214 }
215 }
216
217 // Message refs
218 PointerSection<A, const char *> selrefs(cache, header, "__OBJC", "__message_refs");
219 for (uint64_t s = 0; s < selrefs.count(); s++) {
220 uint64_t oldValue = selrefs.getUnmapped(s);
221 uint64_t newValue = visitor.visit(oldValue);
222 selrefs.set(s, newValue);
223 }
224
225 // Mark image_info
226 const macho_section<P> *imageInfoSection =
227 header->getSection("__OBJC", "__image_info");
228 if (imageInfoSection) {
229 objc_image_info<A> *info = (objc_image_info<A> *)
230 cache->mappedCacheAddressForAddress(imageInfoSection->addr());
231 info->setSelectorsPrebound();
232 }
233 }
234 };