1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2008-2010 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
25 #include "MachOLayout.hpp"
29 // iterate an entsize-based list
30 // typedef entsize_iterator< A, type_t<A>, type_list_t<A> > type_iterator;
31 template <typename A, typename T, typename Tlist>
32 struct entsize_iterator {
34 uint32_t index; // keeping track of this saves a divide in operator-
37 typedef std::random_access_iterator_tag iterator_category;
39 typedef ptrdiff_t difference_type;
43 entsize_iterator() { }
45 entsize_iterator(const Tlist& list, uint32_t start = 0)
46 : entsize(list.getEntsize()), index(start), current(&list.get(start))
49 const entsize_iterator<A,T,Tlist>& operator += (ptrdiff_t count) {
50 current = (T*)((uint8_t *)current + count*entsize);
54 const entsize_iterator<A,T,Tlist>& operator -= (ptrdiff_t count) {
55 current = (T*)((uint8_t *)current - count*entsize);
59 const entsize_iterator<A,T,Tlist> operator + (ptrdiff_t count) const {
60 return entsize_iterator(*this) += count;
62 const entsize_iterator<A,T,Tlist> operator - (ptrdiff_t count) const {
63 return entsize_iterator(*this) -= count;
66 entsize_iterator<A,T,Tlist>& operator ++ () { *this += 1; return *this; }
67 entsize_iterator<A,T,Tlist>& operator -- () { *this -= 1; return *this; }
68 entsize_iterator<A,T,Tlist> operator ++ (int) {
69 entsize_iterator<A,T,Tlist> result(*this); *this += 1; return result;
71 entsize_iterator<A,T,Tlist> operator -- (int) {
72 entsize_iterator<A,T,Tlist> result(*this); *this -= 1; return result;
75 ptrdiff_t operator - (const entsize_iterator<A,T,Tlist>& rhs) const {
76 return (ptrdiff_t)this->index - (ptrdiff_t)rhs.index;
79 T& operator * () { return *current; }
80 T& operator * () const { return *current; }
81 T& operator -> () { return *current; }
82 const T& operator -> () const { return *current; }
84 operator T& () const { return *current; }
86 bool operator == (const entsize_iterator<A,T,Tlist>& rhs) {
87 return this->current == rhs.current;
89 bool operator != (const entsize_iterator<A,T,Tlist>& rhs) {
90 return this->current != rhs.current;
93 bool operator < (const entsize_iterator<A,T,Tlist>& rhs) {
94 return this->current < rhs.current;
96 bool operator > (const entsize_iterator<A,T,Tlist>& rhs) {
97 return this->current > rhs.current;
101 template <typename A>
102 class objc_header_info_t {
104 typedef typename A::P P;
105 typedef typename A::P::uint_t pint_t;
107 pint_t next; // objc_header_info *
108 pint_t mhdr; // mach_header or mach_header_64
109 pint_t info; // objc_image_info *
110 pint_t fname; // const char *
113 bool allClassesRealized;
116 objc_header_info_t(SharedCache<A>* cache, const macho_header<P>* mh)
122 allClassesRealized(0)
124 A::P::setP(mhdr, cache->VMAddressForMappedAddress(mh));
125 const macho_section<P>* sect = mh->getSection("__DATA", "__objc_imageinfo");
126 if (sect) A::P::setP(info, sect->addr());
128 // can't set fname because dyld sometimes edits it
131 void addPointers(std::vector<void*>& pointersToAdd) {
132 pointersToAdd.push_back(&mhdr);
133 if (info) pointersToAdd.push_back(&info);
136 uint64_t header_vmaddr() const { return mhdr; }
139 template <typename A> class objc_method_list_t; // forward reference
141 template <typename A>
142 class objc_method_t {
143 typename A::P::uint_t name; // SEL
144 typename A::P::uint_t types; // const char *
145 typename A::P::uint_t imp; // IMP
146 friend class objc_method_list_t<A>;
148 typename A::P::uint_t getName() const { return A::P::getP(name); }
149 void setName(typename A::P::uint_t newName) { A::P::setP(name, newName); }
151 struct SortBySELAddress :
152 public std::binary_function<const objc_method_t<A>&,
153 const objc_method_t<A>&, bool>
155 bool operator() (const objc_method_t<A>& lhs,
156 const objc_method_t<A>& rhs)
158 return lhs.getName() < rhs.getName();
163 template <typename A>
164 class objc_method_list_t {
167 objc_method_t<A> first;
169 void* operator new (size_t, void* buf) { return buf; }
173 typedef entsize_iterator< A, objc_method_t<A>, objc_method_list_t<A> > method_iterator;
175 uint32_t getCount() const { return A::P::E::get32(count); }
177 uint32_t getEntsize() const {return A::P::E::get32(entsize)&~(uint32_t)3;}
179 objc_method_t<A>& get(uint32_t i) const { return *(objc_method_t<A> *)((uint8_t *)&first + i * getEntsize()); }
181 uint32_t byteSize() const {
182 return byteSizeForCount(getCount(), getEntsize());
185 static uint32_t byteSizeForCount(uint32_t c, uint32_t e = sizeof(objc_method_t<A>)) {
186 return sizeof(objc_method_list_t<A>) - sizeof(objc_method_t<A>) + c*e;
189 method_iterator begin() { return method_iterator(*this, 0); }
190 method_iterator end() { return method_iterator(*this, getCount()); }
191 const method_iterator begin() const { return method_iterator(*this, 0); }
192 const method_iterator end() const { return method_iterator(*this, getCount()); }
194 void setFixedUp() { A::P::E::set32(entsize, getEntsize() | 3); }
196 void getPointers(std::set<void*>& pointersToRemove) {
197 for(method_iterator it = begin(); it != end(); ++it) {
198 objc_method_t<A>& entry = *it;
199 pointersToRemove.insert(&(entry.name));
200 pointersToRemove.insert(&(entry.types));
201 pointersToRemove.insert(&(entry.imp));
205 static void addPointers(uint8_t* methodList, std::vector<void*>& pointersToAdd) {
206 objc_method_list_t<A>* mlist = (objc_method_list_t<A>*)methodList;
207 for(method_iterator it = mlist->begin(); it != mlist->end(); ++it) {
208 objc_method_t<A>& entry = *it;
209 pointersToAdd.push_back(&(entry.name));
210 pointersToAdd.push_back(&(entry.types));
211 pointersToAdd.push_back(&(entry.imp));
215 static objc_method_list_t<A>* newMethodList(size_t newCount, uint32_t newEntsize) {
216 void *buf = ::calloc(byteSizeForCount(newCount, newEntsize), 1);
217 return new (buf) objc_method_list_t<A>(newCount, newEntsize);
220 void operator delete(void * p) {
224 objc_method_list_t(uint32_t newCount,
225 uint32_t newEntsize = sizeof(objc_method_t<A>))
226 : entsize(newEntsize), count(newCount)
230 // use newMethodList instead
231 void* operator new (size_t);
235 // Ivar offset variables are 64-bit on x86_64 and 32-bit everywhere else.
237 template <typename A>
238 class objc_ivar_offset_t {
239 typedef typename A::P::uint_t pint_t;
240 typename A::P::uint_t ptr; // uint32_t *
242 uint32_t& offset(SharedCache<A> *cache) const { return *(uint32_t *)cache->mappedAddressForVMAddress(A::P::getP(ptr)); }
245 bool hasOffset() const { return A::P::getP(ptr) != 0; }
246 pint_t getOffset(SharedCache<A> *cache) const { return A::P::E::get32(offset(cache)); }
247 void setOffset(SharedCache<A> *cache, pint_t newOffset) { A::P::E::set32(offset(cache), newOffset); }
251 class objc_ivar_offset_t<x86_64> {
253 typedef typename A::P::uint_t pint_t;
254 typename A::P::uint_t ptr; // uint64_t *
256 uint64_t& offset(SharedCache<A> *cache) const { return *(uint64_t *)cache->mappedAddressForVMAddress(A::P::getP(ptr)); }
259 bool hasOffset() const { return A::P::getP(ptr) != 0; }
260 pint_t getOffset(SharedCache<A> *cache) const { return A::P::E::get64(offset(cache)); }
261 void setOffset(SharedCache<A> *cache, pint_t newOffset) { A::P::E::set64(offset(cache), newOffset); }
264 template <typename A>
266 typedef typename A::P::uint_t pint_t;
267 objc_ivar_offset_t<A> offset; // uint32_t * (uint64_t * on x86_64)
268 typename A::P::uint_t name; // const char *
269 typename A::P::uint_t type; // const char *
274 const char * getName(SharedCache<A> *cache) const { return (const char *)cache->mappedAddressForVMAddress(A::P::getP(name)); }
276 bool hasOffset() const { return offset.hasOffset(); }
277 pint_t getOffset(SharedCache<A> *cache) const { return offset.getOffset(cache); }
278 void setOffset(SharedCache<A> *cache, pint_t newOffset) { offset.setOffset(cache, newOffset); }
280 uint32_t getAlignment()
282 uint32_t a = A::P::E::get32(alignment);
283 return a == (uint32_t)-1 ? sizeof(typename A::P::uint_t) : 1<<a;
287 template <typename A>
288 class objc_ivar_list_t {
289 typedef typename A::P::uint_t pint_t;
292 objc_ivar_t<A> first;
294 void* operator new (size_t, void* buf) { return buf; }
298 typedef entsize_iterator< A, objc_ivar_t<A>, objc_ivar_list_t<A> > ivar_iterator;
300 uint32_t getCount() const { return A::P::E::get32(count); }
302 uint32_t getEntsize() const { return A::P::E::get32(entsize); }
304 objc_ivar_t<A>& get(pint_t i) const { return *(objc_ivar_t<A> *)((uint8_t *)&first + i * A::P::E::get32(entsize)); }
306 uint32_t byteSize() const {
307 return byteSizeForCount(getCount(), getEntsize());
310 static uint32_t byteSizeForCount(uint32_t c, uint32_t e = sizeof(objc_ivar_t<A>)) {
311 return sizeof(objc_ivar_list_t<A>) - sizeof(objc_ivar_t<A>) + c*e;
314 ivar_iterator begin() { return ivar_iterator(*this, 0); }
315 ivar_iterator end() { return ivar_iterator(*this, getCount()); }
316 const ivar_iterator begin() const { return ivar_iterator(*this, 0); }
317 const ivar_iterator end() const { return ivar_iterator(*this, getCount()); }
319 static objc_ivar_list_t<A>* newIvarList(size_t newCount, uint32_t newEntsize) {
320 void *buf = ::calloc(byteSizeForCount(newCount, newEntsize), 1);
321 return new (buf) objc_ivar_list_t<A>(newCount, newEntsize);
324 void operator delete(void * p) {
328 objc_ivar_list_t(uint32_t newCount,
329 uint32_t newEntsize = sizeof(objc_ivar_t<A>))
330 : entsize(newEntsize), count(newCount)
333 // use newIvarList instead
334 void* operator new (size_t);
338 template <typename A> class objc_property_list_t; // forward
340 template <typename A>
341 class objc_property_t {
342 typename A::P::uint_t name;
343 typename A::P::uint_t attributes;
344 friend class objc_property_list_t<A>;
347 const char * getName(SharedCache<A>* cache) const { return (const char *)cache->mappedAddressForVMAddress(A::P::getP(name)); }
349 const char * getAttributes(SharedCache<A>* cache) const { return (const char *)cache->mappedAddressForVMAddress(A::P::getP(attributes)); }
352 template <typename A>
353 class objc_property_list_t {
356 objc_property_t<A> first;
358 void* operator new (size_t, void* buf) { return buf; }
362 typedef entsize_iterator< A, objc_property_t<A>, objc_property_list_t<A> > property_iterator;
364 uint32_t getCount() const { return A::P::E::get32(count); }
366 uint32_t getEntsize() const { return A::P::E::get32(entsize); }
368 objc_property_t<A>& get(uint32_t i) const { return *(objc_property_t<A> *)((uint8_t *)&first + i * getEntsize()); }
370 uint32_t byteSize() const {
371 return byteSizeForCount(getCount(), getEntsize());
374 static uint32_t byteSizeForCount(uint32_t c, uint32_t e = sizeof(objc_property_t<A>)) {
375 return sizeof(objc_property_list_t<A>) - sizeof(objc_property_t<A>) + c*e;
378 property_iterator begin() { return property_iterator(*this, 0); }
379 property_iterator end() { return property_iterator(*this, getCount()); }
380 const property_iterator begin() const { return property_iterator(*this, 0); }
381 const property_iterator end() const { return property_iterator(*this, getCount()); }
383 void getPointers(std::set<void*>& pointersToRemove) {
384 for(property_iterator it = begin(); it != end(); ++it) {
385 objc_property_t<A>& entry = *it;
386 pointersToRemove.insert(&(entry.name));
387 pointersToRemove.insert(&(entry.attributes));
391 static void addPointers(uint8_t* propertyList, std::vector<void*>& pointersToAdd) {
392 objc_property_list_t<A>* plist = (objc_property_list_t<A>*)propertyList;
393 for(property_iterator it = plist->begin(); it != plist->end(); ++it) {
394 objc_property_t<A>& entry = *it;
395 pointersToAdd.push_back(&(entry.name));
396 pointersToAdd.push_back(&(entry.attributes));
400 static objc_property_list_t<A>* newPropertyList(size_t newCount, uint32_t newEntsize) {
401 void *buf = ::calloc(byteSizeForCount(newCount, newEntsize), 1);
402 return new (buf) objc_property_list_t<A>(newCount, newEntsize);
405 void operator delete(void * p) {
409 objc_property_list_t(uint32_t newCount,
410 uint32_t newEntsize = sizeof(objc_property_t<A>))
411 : entsize(newEntsize), count(newCount)
414 // use newPropertyList instead
415 void* operator new (size_t);
419 template <typename A> class objc_protocol_list_t; // forward reference
421 template <typename A>
422 class objc_protocol_t {
423 typedef typename A::P::uint_t pint_t;
428 pint_t instanceMethods;
430 pint_t optionalInstanceMethods;
431 pint_t optionalClassMethods;
432 pint_t instanceProperties;
435 pint_t extendedMethodTypes;
436 pint_t demangledName;
439 pint_t getIsaVMAddr() const { return A::P::getP(isa); }
440 pint_t setIsaVMAddr(pint_t newIsa) { A::P::setP(isa, newIsa); }
442 const char *getName(SharedCache<A>* cache) const { return (const char *)cache->mappedAddressForVMAddress(A::P::getP(name)); }
444 uint32_t getSize() const { return A::P::E::get32(size); }
445 void setSize(uint32_t newSize) { A::P::E::set32(size, newSize); }
447 uint32_t getFlags() const { return A::P::E::get32(flags); }
449 void setFixedUp() { A::P::E::set32(flags, getFlags() | (1<<30)); }
451 objc_protocol_list_t<A> *getProtocols(SharedCache<A>* cache) const { return (objc_protocol_list_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(protocols)); }
453 objc_method_list_t<A> *getInstanceMethods(SharedCache<A>* cache) const { return (objc_method_list_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(instanceMethods)); }
455 objc_method_list_t<A> *getClassMethods(SharedCache<A>* cache) const { return (objc_method_list_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(classMethods)); }
457 objc_method_list_t<A> *getOptionalInstanceMethods(SharedCache<A>* cache) const { return (objc_method_list_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(optionalInstanceMethods)); }
459 objc_method_list_t<A> *getOptionalClassMethods(SharedCache<A>* cache) const { return (objc_method_list_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(optionalClassMethods)); }
461 objc_property_list_t<A> *getInstanceProperties(SharedCache<A>* cache) const { return (objc_property_list_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(instanceProperties)); }
463 pint_t *getExtendedMethodTypes(SharedCache<A>* cache) const {
464 if (getSize() < offsetof(objc_protocol_t<A>, extendedMethodTypes) + sizeof(extendedMethodTypes)) {
467 return (pint_t *)cache->mappedAddressForVMAddress(A::P::getP(extendedMethodTypes));
470 const char *getDemangledName(SharedCache<A>* cache) const {
471 if (sizeof(*this) < offsetof(objc_protocol_t<A>, demangledName) + sizeof(demangledName)) {
474 return (const char *)cache->mappedAddressForVMAddress(A::P::getP(demangledName));
477 void setDemangledName(SharedCache<A>* cache, const char *newName) {
478 if (sizeof(*this) < offsetof(objc_protocol_t<A>, demangledName) + sizeof(demangledName)) {
479 throw "objc protocol has the wrong size";
481 A::P::setP(demangledName, cache->VMAddressForMappedAddress(newName));
484 void addPointers(std::vector<void*>& pointersToAdd)
486 pointersToAdd.push_back(&isa);
487 pointersToAdd.push_back(&name);
488 if (protocols) pointersToAdd.push_back(&protocols);
489 if (instanceMethods) pointersToAdd.push_back(&instanceMethods);
490 if (classMethods) pointersToAdd.push_back(&classMethods);
491 if (optionalInstanceMethods) pointersToAdd.push_back(&optionalInstanceMethods);
492 if (optionalClassMethods) pointersToAdd.push_back(&optionalClassMethods);
493 if (instanceProperties) pointersToAdd.push_back(&instanceProperties);
494 if (extendedMethodTypes) pointersToAdd.push_back(&extendedMethodTypes);
495 if (demangledName) pointersToAdd.push_back(&demangledName);
499 template <typename A>
500 class objc_protocol_list_t {
501 typedef typename A::P::uint_t pint_t;
505 void* operator new (size_t, void* buf) { return buf; }
509 pint_t getCount() const { return A::P::getP(count); }
511 pint_t getVMAddress(pint_t i) {
512 return A::P::getP(list[i]);
515 objc_protocol_t<A>* get(SharedCache<A>* cache, pint_t i) {
516 return (objc_protocol_t<A>*)cache->mappedAddressForVMAddress(getVMAddress(i));
519 void setVMAddress(pint_t i, pint_t protoVMAddr) {
520 A::P::setP(list[i], protoVMAddr);
523 void set(SharedCache<A>* cache, pint_t i, objc_protocol_t<A>* proto) {
524 setVMAddress(i, cache->VMAddressForMappedAddress(proto));
527 uint32_t byteSize() const {
528 return byteSizeForCount(getCount());
530 static uint32_t byteSizeForCount(pint_t c) {
531 return sizeof(objc_protocol_list_t<A>) + c*sizeof(pint_t);
534 void getPointers(std::set<void*>& pointersToRemove) {
535 for(int i=0 ; i < count; ++i) {
536 pointersToRemove.insert(&list[i]);
540 static void addPointers(uint8_t* protocolList, std::vector<void*>& pointersToAdd) {
541 objc_protocol_list_t<A>* plist = (objc_protocol_list_t<A>*)protocolList;
542 for(int i=0 ; i < plist->count; ++i) {
543 pointersToAdd.push_back(&plist->list[i]);
547 static objc_protocol_list_t<A>* newProtocolList(pint_t newCount) {
548 void *buf = ::calloc(byteSizeForCount(newCount), 1);
549 return new (buf) objc_protocol_list_t<A>(newCount);
552 void operator delete(void * p) {
556 objc_protocol_list_t(uint32_t newCount) : count(newCount) { }
558 // use newProtocolList instead
559 void* operator new (size_t);
563 template <typename A>
564 class objc_class_data_t {
566 uint32_t instanceStart;
567 // Note there is 4-bytes of alignment padding between instanceSize and ivarLayout
568 // on 64-bit archs, but no padding on 32-bit archs.
569 // This union is a way to model that.
571 uint32_t instanceSize;
572 typename A::P::uint_t pad;
574 typename A::P::uint_t ivarLayout;
575 typename A::P::uint_t name;
576 typename A::P::uint_t baseMethods;
577 typename A::P::uint_t baseProtocols;
578 typename A::P::uint_t ivars;
579 typename A::P::uint_t weakIvarLayout;
580 typename A::P::uint_t baseProperties;
583 bool isMetaClass() { return A::P::E::get32(flags) & 1; }
585 uint32_t getInstanceStart() { return A::P::E::get32(instanceStart); }
586 void setInstanceStart(uint32_t newStart) { A::P::E::set32(instanceStart, newStart); }
588 uint32_t getInstanceSize() { return A::P::E::get32(instanceSize.instanceSize); }
589 void setInstanceSize(uint32_t newSiz) { A::P::E::set32(instanceSize.instanceSize, newSiz); }
591 objc_method_list_t<A> *getMethodList(SharedCache<A>* cache) const { return (objc_method_list_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(baseMethods)); }
593 objc_protocol_list_t<A> *getProtocolList(SharedCache<A>* cache) const { return (objc_protocol_list_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(baseProtocols)); }
595 objc_ivar_list_t<A> *getIvarList(SharedCache<A>* cache) const { return (objc_ivar_list_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(ivars)); }
597 objc_property_list_t<A> *getPropertyList(SharedCache<A>* cache) const { return (objc_property_list_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(baseProperties)); }
599 const char * getName(SharedCache<A>* cache) const { return (const char *)cache->mappedAddressForVMAddress(A::P::getP(name)); }
601 void setMethodList(SharedCache<A>* cache, objc_method_list_t<A>* mlist) {
602 A::P::setP(baseMethods, cache->VMAddressForMappedAddress(mlist));
605 void setProtocolList(SharedCache<A>* cache, objc_protocol_list_t<A>* protolist) {
606 A::P::setP(baseProtocols, cache->VMAddressForMappedAddress(protolist));
609 void setPropertyList(SharedCache<A>* cache, objc_property_list_t<A>* proplist) {
610 A::P::setP(baseProperties, cache->VMAddressForMappedAddress(proplist));
613 void addMethodListPointer(std::vector<void*>& pointersToAdd) {
614 pointersToAdd.push_back(&this->baseMethods);
617 void addPropertyListPointer(std::vector<void*>& pointersToAdd) {
618 pointersToAdd.push_back(&this->baseProperties);
621 void addProtocolListPointer(std::vector<void*>& pointersToAdd) {
622 pointersToAdd.push_back(&this->baseProtocols);
626 template <typename A>
628 typename A::P::uint_t isa;
629 typename A::P::uint_t superclass;
630 typename A::P::uint_t method_cache;
631 typename A::P::uint_t vtable;
632 typename A::P::uint_t data;
635 bool isMetaClass(SharedCache<A>* cache) const { return getData(cache)->isMetaClass(); }
637 objc_class_t<A> *getIsa(SharedCache<A> *cache) const { return (objc_class_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(isa)); }
639 objc_class_t<A> *getSuperclass(SharedCache<A> *cache) const { return (objc_class_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(superclass)); }
641 objc_class_data_t<A> *getData(SharedCache<A>* cache) const { return (objc_class_data_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(data)); }
643 objc_method_list_t<A> *getMethodList(SharedCache<A>* cache) const { return getData(cache)->getMethodList(cache); }
645 objc_protocol_list_t<A> *getProtocolList(SharedCache<A>* cache) const { return getData(cache)->getProtocolList(cache); }
647 objc_property_list_t<A> *getPropertyList(SharedCache<A>* cache) const { return getData(cache)->getPropertyList(cache); }
649 const char * getName(SharedCache<A>* cache) const {
650 return getData(cache)->getName(cache);
653 void setMethodList(SharedCache<A>* cache, objc_method_list_t<A>* mlist) {
654 getData(cache)->setMethodList(cache, mlist);
657 void setProtocolList(SharedCache<A>* cache, objc_protocol_list_t<A>* protolist) {
658 getData(cache)->setProtocolList(cache, protolist);
661 void setPropertyList(SharedCache<A>* cache, objc_property_list_t<A>* proplist) {
662 getData(cache)->setPropertyList(cache, proplist);
665 void addMethodListPointer(SharedCache<A>* cache, std::vector<void*>& pointersToAdd) {
666 getData(cache)->addMethodListPointer(pointersToAdd);
669 void addPropertyListPointer(SharedCache<A>* cache, std::vector<void*>& pointersToAdd) {
670 getData(cache)->addPropertyListPointer(pointersToAdd);
673 void addProtocolListPointer(SharedCache<A>* cache, std::vector<void*>& pointersToAdd) {
674 getData(cache)->addProtocolListPointer(pointersToAdd);
681 template <typename A>
682 class objc_category_t {
683 typename A::P::uint_t name;
684 typename A::P::uint_t cls;
685 typename A::P::uint_t instanceMethods;
686 typename A::P::uint_t classMethods;
687 typename A::P::uint_t protocols;
688 typename A::P::uint_t instanceProperties;
692 const char * getName(SharedCache<A> *cache) const { return (const char *)cache->mappedAddressForVMAddress(A::P::getP(name)); }
694 objc_class_t<A> *getClass(SharedCache<A> *cache) const { return (objc_class_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(cls)); }
696 objc_method_list_t<A> *getInstanceMethods(SharedCache<A>* cache) const { return (objc_method_list_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(instanceMethods)); }
698 objc_method_list_t<A> *getClassMethods(SharedCache<A>* cache) const { return (objc_method_list_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(classMethods)); }
700 objc_protocol_list_t<A> *getProtocols(SharedCache<A>* cache) const { return (objc_protocol_list_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(protocols)); }
702 objc_property_list_t<A> *getInstanceProperties(SharedCache<A>* cache) const { return (objc_property_list_t<A> *)cache->mappedAddressForVMAddress(A::P::getP(instanceProperties)); }
704 void getPointers(std::set<void*>& pointersToRemove) {
705 pointersToRemove.insert(&name);
706 pointersToRemove.insert(&cls);
707 pointersToRemove.insert(&instanceMethods);
708 pointersToRemove.insert(&classMethods);
709 pointersToRemove.insert(&protocols);
710 pointersToRemove.insert(&instanceProperties);
716 template <typename A>
717 class objc_message_ref_t {
718 typename A::P::uint_t imp;
719 typename A::P::uint_t sel;
722 typename A::P::uint_t getName() const { return A::P::getP(sel); }
724 void setName(typename A::P::uint_t newName) { A::P::setP(sel, newName); }
727 // Call visitor.visitIvar() on every ivar in a given class.
728 template <typename A, typename V>
730 typedef typename A::P P;
731 typedef typename A::P::uint_t pint_t;
735 IvarWalker(V& visitor) : ivarVisitor(visitor) { }
737 void walk(SharedCache<A>* cache, const macho_header<P>* header, objc_class_t<A> *cls)
739 objc_class_data_t<A> *data = cls->getData(cache);
740 objc_ivar_list_t<A> *ivars = data->getIvarList(cache);
742 for (pint_t i = 0; i < ivars->getCount(); i++) {
743 objc_ivar_t<A>& ivar = ivars->get(i);
744 //fprintf(stderr, "visiting ivar: %s\n", ivar.getName(cache));
745 ivarVisitor.visitIvar(cache, header, cls, &ivar);
748 //fprintf(stderr, "no ivars\n");
752 void visitClass(SharedCache<A>* cache, const macho_header<P>* header, objc_class_t<A> *cls)
754 walk(cache, header, cls);
758 // Call visitor.visitClass() on every class.
759 template <typename A, typename V>
761 typedef typename A::P P;
762 typedef typename A::P::uint_t pint_t;
766 ClassWalker(V& visitor) : classVisitor(visitor) { }
768 void walk(SharedCache<A>* cache, const macho_header<P>* header)
770 PointerSection<A, objc_class_t<A> *>
771 classes(cache, header, "__DATA", "__objc_classlist");
773 for (pint_t i = 0; i < classes.count(); i++) {
774 objc_class_t<A> *cls = classes.get(i);
775 //fprintf(stderr, "visiting class: %s\n", cls->getName(cache));
776 if (cls) classVisitor.visitClass(cache, header, cls);
782 // Call visitor.visitProtocol() on every protocol.
783 template <typename A, typename V>
784 class ProtocolWalker {
785 typedef typename A::P P;
786 typedef typename A::P::uint_t pint_t;
790 ProtocolWalker(V& visitor) : protocolVisitor(visitor) { }
792 void walk(SharedCache<A>* cache, const macho_header<P>* header)
794 PointerSection<A, objc_protocol_t<A> *>
795 protocols(cache, header, "__DATA", "__objc_protolist");
797 for (pint_t i = 0; i < protocols.count(); i++) {
798 objc_protocol_t<A> *proto = protocols.get(i);
799 protocolVisitor.visitProtocol(cache, header, proto);
805 // Call visitor.visitProtocolReference() on every protocol.
806 template <typename A, typename V>
807 class ProtocolReferenceWalker {
808 typedef typename A::P P;
809 typedef typename A::P::uint_t pint_t;
812 void visitProtocolList(SharedCache<A>* cache,
813 objc_protocol_list_t<A>* protolist)
815 if (!protolist) return;
816 for (pint_t i = 0; i < protolist->getCount(); i++) {
817 pint_t oldValue = protolist->getVMAddress(i);
818 pint_t newValue = mVisitor.visitProtocolReference(cache, oldValue);
819 protolist->setVMAddress(i, newValue);
823 friend class ClassWalker<A, ProtocolReferenceWalker<A, V>>;
824 void visitClass(SharedCache<A>* cache, const macho_header<P>*,
825 objc_class_t<A>* cls)
827 visitProtocolList(cache, cls->getProtocolList(cache));
828 visitProtocolList(cache, cls->getIsa(cache)->getProtocolList(cache));
833 ProtocolReferenceWalker(V& visitor) : mVisitor(visitor) { }
834 void walk(SharedCache<A>* cache, const macho_header<P>* header)
836 // @protocol expressions
837 PointerSection<A, objc_protocol_t<A> *>
838 protorefs(cache, header, "__DATA", "__objc_protorefs");
839 for (pint_t i = 0; i < protorefs.count(); i++) {
840 pint_t oldValue = protorefs.getVMAddress(i);
841 pint_t newValue = mVisitor.visitProtocolReference(cache, oldValue);
842 protorefs.setVMAddress(i, newValue);
845 // protocol lists in classes
846 ClassWalker<A, ProtocolReferenceWalker<A, V>> classes(*this);
847 classes.walk(cache, header);
849 // protocol lists in protocols
850 // __objc_protolists itself is NOT updated
851 PointerSection<A, objc_protocol_t<A> *>
852 protocols(cache, header, "__DATA", "__objc_protolist");
853 for (pint_t i = 0; i < protocols.count(); i++) {
854 objc_protocol_t<A>* proto = protocols.get(i);
855 visitProtocolList(cache, proto->getProtocols(cache));
856 // not recursive: every old protocol object
857 // must be in some protolist section somewhere
863 // Call visitor.visitMethodList(mlist) on every
864 // class and category method list in a header.
865 // Call visitor.visitProtocolMethodList(mlist, typelist) on every
866 // protocol method list in a header.
867 template <typename A, typename V>
868 class MethodListWalker {
870 typedef typename A::P P;
871 typedef typename A::P::uint_t pint_t;
877 MethodListWalker(V& visitor) : mVisitor(visitor) { }
879 void walk(SharedCache<A>* cache, const macho_header<P>* header)
881 // Method lists in classes
882 PointerSection<A, objc_class_t<A> *>
883 classes(cache, header, "__DATA", "__objc_classlist");
885 for (pint_t i = 0; i < classes.count(); i++) {
886 objc_class_t<A> *cls = classes.get(i);
887 objc_method_list_t<A> *mlist;
888 if ((mlist = cls->getMethodList(cache))) {
889 mVisitor.visitMethodList(mlist);
891 if ((mlist = cls->getIsa(cache)->getMethodList(cache))) {
892 mVisitor.visitMethodList(mlist);
896 // Method lists from categories
897 PointerSection<A, objc_category_t<A> *>
898 cats(cache, header, "__DATA", "__objc_catlist");
899 for (pint_t i = 0; i < cats.count(); i++) {
900 objc_category_t<A> *cat = cats.get(i);
901 objc_method_list_t<A> *mlist;
902 if ((mlist = cat->getInstanceMethods(cache))) {
903 mVisitor.visitMethodList(mlist);
905 if ((mlist = cat->getClassMethods(cache))) {
906 mVisitor.visitMethodList(mlist);
910 // Method description lists from protocols
911 PointerSection<A, objc_protocol_t<A> *>
912 protocols(cache, header, "__DATA", "__objc_protolist");
913 for (pint_t i = 0; i < protocols.count(); i++) {
914 objc_protocol_t<A> *proto = protocols.get(i);
915 objc_method_list_t<A> *mlist;
916 pint_t *typelist = proto->getExtendedMethodTypes(cache);
918 if ((mlist = proto->getInstanceMethods(cache))) {
919 mVisitor.visitProtocolMethodList(mlist, typelist);
920 if (typelist) typelist += mlist->getCount();
922 if ((mlist = proto->getClassMethods(cache))) {
923 mVisitor.visitProtocolMethodList(mlist, typelist);
924 if (typelist) typelist += mlist->getCount();
926 if ((mlist = proto->getOptionalInstanceMethods(cache))) {
927 mVisitor.visitProtocolMethodList(mlist, typelist);
928 if (typelist) typelist += mlist->getCount();
930 if ((mlist = proto->getOptionalClassMethods(cache))) {
931 mVisitor.visitProtocolMethodList(mlist, typelist);
932 if (typelist) typelist += mlist->getCount();
939 // Update selector references. The visitor performs recording and uniquing.
940 template <typename A, typename V>
941 class SelectorOptimizer {
943 typedef typename A::P P;
944 typedef typename A::P::uint_t pint_t;
948 friend class MethodListWalker< A, SelectorOptimizer<A,V> >;
949 void visitMethodList(objc_method_list_t<A> *mlist)
951 // Gather selectors. Update method names.
952 for (pint_t m = 0; m < mlist->getCount(); m++) {
953 pint_t oldValue = mlist->get(m).getName();
954 pint_t newValue = mVisitor.visit(oldValue);
955 mlist->get(m).setName(newValue);
957 // Do not setFixedUp: the methods are not yet sorted.
960 void visitProtocolMethodList(objc_method_list_t<A> *mlist, pint_t *types)
962 visitMethodList(mlist);
967 SelectorOptimizer(V& visitor) : mVisitor(visitor) { }
969 void optimize(SharedCache<A>* cache, const macho_header<P>* header)
971 // method lists in classes, categories, and protocols
972 MethodListWalker< A, SelectorOptimizer<A,V> > mw(*this);
973 mw.walk(cache, header);
975 // @selector references
976 PointerSection<A, const char *>
977 selrefs(cache, header, "__DATA", "__objc_selrefs");
978 for (pint_t i = 0; i < selrefs.count(); i++) {
979 pint_t oldValue = selrefs.getVMAddress(i);
980 pint_t newValue = mVisitor.visit(oldValue);
981 selrefs.setVMAddress(i, newValue);
984 // message references
985 ArraySection<A, objc_message_ref_t<A> >
986 msgrefs(cache, header, "__DATA", "__objc_msgrefs");
987 for (pint_t i = 0; i < msgrefs.count(); i++) {
988 objc_message_ref_t<A>& msg = msgrefs.get(i);
989 pint_t oldValue = msg.getName();
990 pint_t newValue = mVisitor.visit(oldValue);
991 msg.setName(newValue);
997 template <typename A>
998 static bool headerSupportsGC(SharedCache<A>* cache,
999 const macho_header<typename A::P>* header)
1001 const macho_section<typename A::P> *imageInfoSection =
1002 header->getSection("__DATA", "__objc_imageinfo");
1003 if (imageInfoSection) {
1004 objc_image_info<A> *info = (objc_image_info<A> *)
1005 cache->mappedAddressForVMAddress(imageInfoSection->addr());
1006 return (info->supportsGCFlagSet() || info->requiresGCFlagSet());
1013 // Gather the set of GC-supporting classes
1014 template <typename A>
1016 typedef typename A::P P;
1018 std::set<objc_class_t<A>*> fGCClasses;
1021 bool contains(objc_class_t<A>* cls) const {
1022 return fGCClasses.count(cls) != 0;
1025 void visitClass(SharedCache<A>* cache, const macho_header<P>* header, objc_class_t<A> *cls)
1027 fGCClasses.insert(cls);
1032 // Update selector references. The visitor performs recording and uniquing.
1033 template <typename A>
1034 class IvarOffsetOptimizer {
1035 typedef typename A::P P;
1038 uint32_t maxAlignment;
1040 uint32_t fOptimized;
1042 GCClassSet<A> fGCClasses;
1046 IvarOffsetOptimizer() : fOptimized(0) { }
1048 size_t optimized() const { return fOptimized; }
1050 // dual purpose ivar visitor function
1051 // if slide!=0 then slides the ivar by that amount, otherwise computes maxAlignment
1052 void visitIvar(SharedCache<A>* cache, const macho_header<P>* /*unused, may be NULL*/, objc_class_t<A> *cls, objc_ivar_t<A> *ivar)
1055 uint32_t alignment = ivar->getAlignment();
1056 if (alignment > maxAlignment) maxAlignment = alignment;
1058 // skip anonymous bitfields
1059 if (ivar->hasOffset()) {
1060 uint32_t oldOffset = (uint32_t)ivar->getOffset(cache);
1061 ivar->setOffset(cache, oldOffset + slide);
1063 //fprintf(stderr, "%d -> %d for %s.%s\n", oldOffset, oldOffset + slide, cls->getName(cache), ivar->getName(cache));
1065 //fprintf(stderr, "NULL offset\n");
1070 // Class visitor function. Evaluates whether to slide ivars and performs slide if needed.
1071 // The slide algorithm is also implemented in objc. Any changes here should be reflected there also.
1072 void visitClass(SharedCache<A>* cache, const macho_header<P>* /*unused, may be NULL*/, objc_class_t<A> *cls)
1074 if (fGCClasses.contains(cls)) {
1075 // This class supports GC. We don't know how to update
1076 // GC ivar layout bitmaps, so don't touch anything.
1080 objc_class_t<A> *super = cls->getSuperclass(cache);
1082 // Recursively visit superclasses to ensure we have the correct superclass start
1083 // Note that we don't need the macho_header, so just pass NULL.
1084 visitClass(cache, NULL, super);
1086 objc_class_data_t<A> *data = cls->getData(cache);
1087 objc_class_data_t<A> *super_data = super->getData(cache);
1088 int32_t diff = super_data->getInstanceSize() - data->getInstanceStart();
1090 IvarWalker<A, IvarOffsetOptimizer<A> > ivarVisitor(*this);
1094 // This walk computes maxAlignment
1095 ivarVisitor.walk(cache, NULL, cls);
1097 // Compute a slide value that preserves that alignment
1098 uint32_t alignMask = maxAlignment - 1;
1099 if (diff & alignMask) diff = (diff + alignMask) & ~alignMask;
1101 // Slide all of this class's ivars en masse
1104 //fprintf(stderr, "Sliding ivars in %s by %u (superclass was %d, now %d)\n", cls->getName(cache), slide, data->getInstanceStart(), super_data->getInstanceSize());
1105 ivarVisitor.walk(cache, NULL, cls);
1106 data->setInstanceStart(data->getInstanceStart() + slide);
1107 data->setInstanceSize(data->getInstanceSize() + slide);
1113 // Gather the list of GC-supporting classes.
1114 // Ivars in these classes cannot be updated because
1115 // we don't know how to update ivar layout bitmaps.
1116 void findGCClasses(SharedCache<A>* cache, const macho_header<P>* header)
1118 if (headerSupportsGC(cache, header)) {
1119 ClassWalker<A, GCClassSet<A> > classVisitor(fGCClasses);
1120 classVisitor.walk(cache, header);
1124 // Enumerates objc classes in the module and performs any ivar slides
1125 void optimize(SharedCache<A>* cache, const macho_header<P>* header)
1127 if (! headerSupportsGC(cache, header)) {
1128 ClassWalker<A, IvarOffsetOptimizer<A> > classVisitor(*this);
1129 classVisitor.walk(cache, header);
1135 // Sort methods in place by selector.
1136 template <typename A>
1137 class MethodListSorter {
1139 typedef typename A::P P;
1140 typedef typename A::P::uint_t pint_t;
1142 uint32_t fOptimized;
1144 friend class MethodListWalker<A, MethodListSorter<A> >;
1145 void visitMethodList(objc_method_list_t<A> *mlist)
1147 typename objc_method_t<A>::SortBySELAddress sorter;
1148 std::stable_sort(mlist->begin(), mlist->end(), sorter);
1149 mlist->setFixedUp();
1153 void visitProtocolMethodList(objc_method_list_t<A> *mlist, pint_t *typelist)
1155 typename objc_method_t<A>::SortBySELAddress sorter;
1156 // can't easily use std::stable_sort here
1157 for (uint32_t i = 0; i < mlist->getCount(); i++) {
1158 for (uint32_t j = i+1; j < mlist->getCount(); j++) {
1159 objc_method_t<A>& mi = mlist->get(i);
1160 objc_method_t<A>& mj = mlist->get(j);
1161 if (! sorter(mi, mj)) {
1163 if (typelist) std::swap(typelist[i], typelist[j]);
1168 mlist->setFixedUp();
1173 MethodListSorter() : fOptimized(0) { }
1175 size_t optimized() const { return fOptimized; }
1177 void optimize(SharedCache<A>* cache, macho_header<P>* header)
1179 MethodListWalker<A, MethodListSorter<A> > mw(*this);
1180 mw.walk(cache, header);
1185 template <typename A>
1186 class HeaderInfoOptimizer {
1188 typedef typename A::P P;
1189 typedef typename A::P::uint_t pint_t;
1191 objc_header_info_t<A>* fHinfos;
1195 HeaderInfoOptimizer() : fHinfos(0), fCount(0) { }
1197 const char *init(size_t count, uint8_t*& buf, size_t& bufSize)
1199 if (count == 0) return NULL;
1201 size_t requiredSize =
1202 2*sizeof(uint32_t) + count*sizeof(objc_header_info_t<A>);
1203 if (bufSize < requiredSize) {
1204 return "libobjc's read/write section is too small (metadata not optimized)";
1207 uint32_t *buf32 = (uint32_t *)buf;
1208 A::P::E::set32(buf32[0], count);
1209 A::P::E::set32(buf32[1], sizeof(objc_header_info_t<A>));
1210 fHinfos = (objc_header_info_t<A>*)(buf32+2);
1212 buf += requiredSize;
1213 bufSize -= requiredSize;
1218 void update(SharedCache<A>* cache, const macho_header<P>* mh, std::vector<void*>& pointersInData)
1220 objc_header_info_t<A>* hi = new(&fHinfos[fCount++]) objc_header_info_t<A>(cache, mh);
1221 hi->addPointers(pointersInData);
1224 objc_header_info_t<A>* hinfoForHeader(SharedCache<A>* cache, const macho_header<P>* mh)
1226 // fixme could be binary search
1227 pint_t mh_vmaddr = cache->VMAddressForMappedAddress(mh);
1228 for (size_t i = 0; i < fCount; i++) {
1229 objc_header_info_t<A>* hi = &fHinfos[i];
1230 if (hi->header_vmaddr() == mh_vmaddr) return hi;