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@
29 // iterate an entsize-based list
30 // typedef entsize_iterator<P, type_t<P>, type_list_t<P> > type_iterator;
31 template <typename P, 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<P,T,Tlist>& operator += (ptrdiff_t count) {
50 current = (T*)((uint8_t *)current + count*entsize);
54 const entsize_iterator<P,T,Tlist>& operator -= (ptrdiff_t count) {
55 current = (T*)((uint8_t *)current - count*entsize);
59 const entsize_iterator<P,T,Tlist> operator + (ptrdiff_t count) const {
60 return entsize_iterator(*this) += count;
62 const entsize_iterator<P,T,Tlist> operator - (ptrdiff_t count) const {
63 return entsize_iterator(*this) -= count;
66 entsize_iterator<P,T,Tlist>& operator ++ () { *this += 1; return *this; }
67 entsize_iterator<P,T,Tlist>& operator -- () { *this -= 1; return *this; }
68 entsize_iterator<P,T,Tlist> operator ++ (int) {
69 entsize_iterator<P,T,Tlist> result(*this); *this += 1; return result;
71 entsize_iterator<P,T,Tlist> operator -- (int) {
72 entsize_iterator<P,T,Tlist> result(*this); *this -= 1; return result;
75 ptrdiff_t operator - (const entsize_iterator<P,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<P,T,Tlist>& rhs) {
87 return this->current == rhs.current;
89 bool operator != (const entsize_iterator<P,T,Tlist>& rhs) {
90 return this->current != rhs.current;
93 bool operator < (const entsize_iterator<P,T,Tlist>& rhs) {
94 return this->current < rhs.current;
96 bool operator > (const entsize_iterator<P,T,Tlist>& rhs) {
97 return this->current > rhs.current;
101 static void overwrite(entsize_iterator<P,T,Tlist>& dst, const Tlist* srcList)
103 entsize_iterator<P,T,Tlist> src;
104 uint32_t ee = srcList->getEntsize();
105 for (src = srcList->begin(); src != srcList->end(); ++src) {
106 memcpy(&*dst, &*src, ee);
112 template <typename P>
113 class objc_header_info_rw_t {
115 typedef typename P::uint_t pint_t;
117 pint_t data; // loaded:1, allRealised:1, objc_header_info *:ptr
120 objc_header_info_rw_t(ContentAccessor* cache, const macho_header<P>* mh)
125 template <typename P>
126 class objc_header_info_ro_t {
128 typedef typename P::uint_t pint_t;
130 pint_t mhdr_offset; // offset to mach_header or mach_header_64
131 pint_t info_offset; // offset to objc_image_info *
134 objc_header_info_ro_t(ContentAccessor* cache, const macho_header<P>* mh)
135 : mhdr_offset(0), info_offset(0) {
136 P::setP(mhdr_offset, (uint64_t)cache->vmAddrForContent((void*)mh) - (uint64_t)cache->vmAddrForContent(&mhdr_offset));
137 assert(header_vmaddr(cache) == (uint64_t)cache->vmAddrForContent((void*)mh));
138 const macho_section<P>* sect = mh->getSection("__DATA", "__objc_imageinfo");
140 P::setP(info_offset, (uint64_t)sect->addr() - (uint64_t)cache->vmAddrForContent(&info_offset));
141 // set bit in mach_header.flags to tell dyld that this image has objc content
142 macho_header<P>* rwmh = const_cast<macho_header<P>*>(mh);
143 rwmh->set_flags(mh->flags() | MH_HAS_OBJC);
146 P::setP(info_offset, - (uint64_t)cache->vmAddrForContent(&info_offset));
149 pint_t header_vmaddr(ContentAccessor* cache) const {
150 return (pint_t)(((uint64_t)cache->vmAddrForContent(&mhdr_offset)) + mhdr_offset);
155 template <typename P>
156 class objc_method_list_t; // forward reference
159 template <typename P>
160 class objc_method_t {
161 typedef typename P::uint_t pint_t;
163 pint_t types; // const char *
165 friend class objc_method_list_t<P>;
167 pint_t getName() const { return (pint_t)P::getP(name); }
168 void setName(pint_t newName) { P::setP(name, newName); }
170 struct SortBySELAddress :
171 public std::binary_function<const objc_method_t<P>&,
172 const objc_method_t<P>&, bool>
174 bool operator() (const objc_method_t<P>& lhs,
175 const objc_method_t<P>& rhs)
177 return lhs.getName() < rhs.getName();
182 template <typename P>
183 class objc_method_list_t {
186 objc_method_t<P> first;
188 void* operator new (size_t, void* buf) { return buf; }
192 typedef entsize_iterator<P, objc_method_t<P>, objc_method_list_t<P> > method_iterator;
194 uint32_t getCount() const { return P::E::get32(count); }
196 uint32_t getEntsize() const {return P::E::get32(entsize)&~(uint32_t)3;}
198 objc_method_t<P>& get(uint32_t i) const { return *(objc_method_t<P> *)((uint8_t *)&first + i * getEntsize()); }
200 uint32_t byteSize() const {
201 return byteSizeForCount(getCount(), getEntsize());
204 static uint32_t byteSizeForCount(uint32_t c, uint32_t e = sizeof(objc_method_t<P>)) {
205 return sizeof(objc_method_list_t<P>) - sizeof(objc_method_t<P>) + c*e;
208 method_iterator begin() { return method_iterator(*this, 0); }
209 method_iterator end() { return method_iterator(*this, getCount()); }
210 const method_iterator begin() const { return method_iterator(*this, 0); }
211 const method_iterator end() const { return method_iterator(*this, getCount()); }
213 void setFixedUp() { P::E::set32(entsize, getEntsize() | 3); }
215 void getPointers(std::set<void*>& pointersToRemove) {
216 for(method_iterator it = begin(); it != end(); ++it) {
217 objc_method_t<P>& entry = *it;
218 pointersToRemove.insert(&(entry.name));
219 pointersToRemove.insert(&(entry.types));
220 pointersToRemove.insert(&(entry.imp));
224 static void addPointers(uint8_t* methodList, std::vector<void*>& pointersToAdd) {
225 objc_method_list_t<P>* mlist = (objc_method_list_t<P>*)methodList;
226 for(method_iterator it = mlist->begin(); it != mlist->end(); ++it) {
227 objc_method_t<P>& entry = *it;
228 pointersToAdd.push_back(&(entry.name));
229 pointersToAdd.push_back(&(entry.types));
230 pointersToAdd.push_back(&(entry.imp));
234 static objc_method_list_t<P>* newMethodList(size_t newCount, uint32_t newEntsize) {
235 void *buf = ::calloc(byteSizeForCount(newCount, newEntsize), 1);
236 return new (buf) objc_method_list_t<P>(newCount, newEntsize);
239 void operator delete(void * p) {
243 objc_method_list_t(uint32_t newCount,
244 uint32_t newEntsize = sizeof(objc_method_t<P>))
245 : entsize(newEntsize), count(newCount)
249 // use newMethodList instead
250 void* operator new (size_t);
254 template <typename P>
256 typedef typename P::uint_t pint_t;
258 pint_t offset; // uint32_t* (uint64_t* on x86_64)
259 pint_t name; // const char*
260 pint_t type; // const char*
265 const char* getName(ContentAccessor* cache) const { return (const char *)cache->contentForVMAddr(P::getP(name)); }
267 bool hasOffset() const { return offset != 0; }
268 uint32_t getOffset(ContentAccessor* cache) const { return P::E::get32(*(uint32_t*)(cache->contentForVMAddr(P::getP(offset)))); }
269 void setOffset(ContentAccessor* cache, uint32_t newOffset) { P::E::set32(*(uint32_t*)(cache->contentForVMAddr(P::getP(offset))), newOffset); }
272 uint32_t getAlignment() {
273 uint32_t a = P::E::get32(alignment);
274 return (a == (uint32_t)-1) ? sizeof(pint_t) : 1<<a;
278 template <typename P>
279 class objc_ivar_list_t {
280 typedef typename P::uint_t pint_t;
283 objc_ivar_t<P> first;
285 void* operator new (size_t, void* buf) { return buf; }
289 typedef entsize_iterator<P, objc_ivar_t<P>, objc_ivar_list_t<P> > ivar_iterator;
291 uint32_t getCount() const { return P::E::get32(count); }
293 uint32_t getEntsize() const { return P::E::get32(entsize); }
295 objc_ivar_t<P>& get(pint_t i) const { return *(objc_ivar_t<P> *)((uint8_t *)&first + i * P::E::get32(entsize)); }
297 uint32_t byteSize() const {
298 return byteSizeForCount(getCount(), getEntsize());
301 static uint32_t byteSizeForCount(uint32_t c, uint32_t e = sizeof(objc_ivar_t<P>)) {
302 return sizeof(objc_ivar_list_t<P>) - sizeof(objc_ivar_t<P>) + c*e;
305 ivar_iterator begin() { return ivar_iterator(*this, 0); }
306 ivar_iterator end() { return ivar_iterator(*this, getCount()); }
307 const ivar_iterator begin() const { return ivar_iterator(*this, 0); }
308 const ivar_iterator end() const { return ivar_iterator(*this, getCount()); }
310 static objc_ivar_list_t<P>* newIvarList(size_t newCount, uint32_t newEntsize) {
311 void *buf = ::calloc(byteSizeForCount(newCount, newEntsize), 1);
312 return new (buf) objc_ivar_list_t<P>(newCount, newEntsize);
315 void operator delete(void * p) {
319 objc_ivar_list_t(uint32_t newCount,
320 uint32_t newEntsize = sizeof(objc_ivar_t<P>))
321 : entsize(newEntsize), count(newCount)
324 // use newIvarList instead
325 void* operator new (size_t);
329 template <typename P> class objc_property_list_t; // forward
331 template <typename P>
332 class objc_property_t {
333 typedef typename P::uint_t pint_t;
336 friend class objc_property_list_t<P>;
339 const char * getName(ContentAccessor* cache) const { return (const char *)cache->contentForVMAddr(P::getP(name)); }
341 const char * getAttributes(ContentAccessor* cache) const { return (const char *)cache->contentForVMAddr(P::getP(attributes)); }
344 template <typename P>
345 class objc_property_list_t {
348 objc_property_t<P> first;
350 void* operator new (size_t, void* buf) { return buf; }
354 typedef entsize_iterator<P, objc_property_t<P>, objc_property_list_t<P> > property_iterator;
356 uint32_t getCount() const { return P::E::get32(count); }
358 uint32_t getEntsize() const { return P::E::get32(entsize); }
360 objc_property_t<P>& get(uint32_t i) const { return *(objc_property_t<P> *)((uint8_t *)&first + i * getEntsize()); }
362 uint32_t byteSize() const {
363 return byteSizeForCount(getCount(), getEntsize());
366 static uint32_t byteSizeForCount(uint32_t c, uint32_t e = sizeof(objc_property_t<P>)) {
367 return sizeof(objc_property_list_t<P>) - sizeof(objc_property_t<P>) + c*e;
370 property_iterator begin() { return property_iterator(*this, 0); }
371 property_iterator end() { return property_iterator(*this, getCount()); }
372 const property_iterator begin() const { return property_iterator(*this, 0); }
373 const property_iterator end() const { return property_iterator(*this, getCount()); }
375 void getPointers(std::set<void*>& pointersToRemove) {
376 for(property_iterator it = begin(); it != end(); ++it) {
377 objc_property_t<P>& entry = *it;
378 pointersToRemove.insert(&(entry.name));
379 pointersToRemove.insert(&(entry.attributes));
383 static void addPointers(uint8_t* propertyList, std::vector<void*>& pointersToAdd) {
384 objc_property_list_t<P>* plist = (objc_property_list_t<P>*)propertyList;
385 for(property_iterator it = plist->begin(); it != plist->end(); ++it) {
386 objc_property_t<P>& entry = *it;
387 pointersToAdd.push_back(&(entry.name));
388 pointersToAdd.push_back(&(entry.attributes));
392 static objc_property_list_t<P>* newPropertyList(size_t newCount, uint32_t newEntsize) {
393 void *buf = ::calloc(byteSizeForCount(newCount, newEntsize), 1);
394 return new (buf) objc_property_list_t<P>(newCount, newEntsize);
397 void operator delete(void * p) {
401 objc_property_list_t(uint32_t newCount,
402 uint32_t newEntsize = sizeof(objc_property_t<P>))
403 : entsize(newEntsize), count(newCount)
406 // use newPropertyList instead
407 void* operator new (size_t);
411 template <typename A> class objc_protocol_list_t; // forward reference
413 template <typename P>
414 class objc_protocol_t {
415 typedef typename P::uint_t pint_t;
420 pint_t instanceMethods;
422 pint_t optionalInstanceMethods;
423 pint_t optionalClassMethods;
424 pint_t instanceProperties;
427 pint_t extendedMethodTypes;
428 pint_t demangledName;
429 pint_t classProperties;
432 pint_t getIsaVMAddr() const { return (pint_t)P::getP(isa); }
433 void setIsaVMAddr(pint_t newIsa) { P::setP(isa, newIsa); }
435 const char *getName(ContentAccessor* cache) const { return (const char *)cache->contentForVMAddr(P::getP(name)); }
437 uint32_t getSize() const { return P::E::get32(size); }
438 void setSize(uint32_t newSize) { P::E::set32(size, newSize); }
440 uint32_t getFlags() const { return P::E::get32(flags); }
442 void setFixedUp() { P::E::set32(flags, getFlags() | (1<<30)); }
444 objc_protocol_list_t<P> *getProtocols(ContentAccessor* cache) const { return (objc_protocol_list_t<P> *)cache->contentForVMAddr(P::getP(protocols)); }
446 objc_method_list_t<P> *getInstanceMethods(ContentAccessor* cache) const { return (objc_method_list_t<P> *)cache->contentForVMAddr(P::getP(instanceMethods)); }
448 objc_method_list_t<P> *getClassMethods(ContentAccessor* cache) const { return (objc_method_list_t<P> *)cache->contentForVMAddr(P::getP(classMethods)); }
450 objc_method_list_t<P> *getOptionalInstanceMethods(ContentAccessor* cache) const { return (objc_method_list_t<P> *)cache->contentForVMAddr(P::getP(optionalInstanceMethods)); }
452 objc_method_list_t<P> *getOptionalClassMethods(ContentAccessor* cache) const { return (objc_method_list_t<P> *)cache->contentForVMAddr(P::getP(optionalClassMethods)); }
454 objc_property_list_t<P> *getInstanceProperties(ContentAccessor* cache) const { return (objc_property_list_t<P> *)cache->contentForVMAddr(P::getP(instanceProperties)); }
456 pint_t *getExtendedMethodTypes(ContentAccessor* cache) const {
457 if (getSize() < offsetof(objc_protocol_t<P>, extendedMethodTypes) + sizeof(extendedMethodTypes)) {
460 return (pint_t *)cache->contentForVMAddr(P::getP(extendedMethodTypes));
463 const char *getDemangledName(ContentAccessor* cache) const {
464 if (sizeof(*this) < offsetof(objc_protocol_t<P>, demangledName) + sizeof(demangledName)) {
467 return (const char *)cache->contentForVMAddr(P::getP(demangledName));
470 void setDemangledName(ContentAccessor* cache, const char *newName, Diagnostics& diag) {
471 if (sizeof(*this) < offsetof(objc_protocol_t<P>, demangledName) + sizeof(demangledName))
472 diag.error("objc protocol has the wrong size");
474 P::setP(demangledName, cache->vmAddrForContent((void*)newName));
477 void addPointers(std::vector<void*>& pointersToAdd)
479 pointersToAdd.push_back(&isa);
480 pointersToAdd.push_back(&name);
481 if (protocols) pointersToAdd.push_back(&protocols);
482 if (instanceMethods) pointersToAdd.push_back(&instanceMethods);
483 if (classMethods) pointersToAdd.push_back(&classMethods);
484 if (optionalInstanceMethods) pointersToAdd.push_back(&optionalInstanceMethods);
485 if (optionalClassMethods) pointersToAdd.push_back(&optionalClassMethods);
486 if (instanceProperties) pointersToAdd.push_back(&instanceProperties);
487 if (extendedMethodTypes) pointersToAdd.push_back(&extendedMethodTypes);
488 if (demangledName) pointersToAdd.push_back(&demangledName);
493 template <typename P>
494 class objc_protocol_list_t {
495 typedef typename P::uint_t pint_t;
499 void* operator new (size_t, void* buf) { return buf; }
503 pint_t getCount() const { return (pint_t)P::getP(count); }
505 pint_t getVMAddress(pint_t i) {
506 return (pint_t)P::getP(list[i]);
509 objc_protocol_t<P>* get(ContentAccessor* cache, pint_t i) {
510 return (objc_protocol_t<P>*)cache->contentForVMAddr(getVMAddress(i));
513 void setVMAddress(pint_t i, pint_t protoVMAddr) {
514 P::setP(list[i], protoVMAddr);
517 void set(ContentAccessor* cache, pint_t i, objc_protocol_t<P>* proto) {
518 setVMAddress(i, cache->vmAddrForContent(proto));
521 uint32_t byteSize() const {
522 return byteSizeForCount(getCount());
524 static uint32_t byteSizeForCount(pint_t c) {
525 return sizeof(objc_protocol_list_t<P>) + c*sizeof(pint_t);
528 void getPointers(std::set<void*>& pointersToRemove) {
529 for(int i=0 ; i < count; ++i) {
530 pointersToRemove.insert(&list[i]);
534 static void addPointers(uint8_t* protocolList, std::vector<void*>& pointersToAdd) {
535 objc_protocol_list_t<P>* plist = (objc_protocol_list_t<P>*)protocolList;
536 for(int i=0 ; i < plist->count; ++i) {
537 pointersToAdd.push_back(&plist->list[i]);
541 static objc_protocol_list_t<P>* newProtocolList(pint_t newCount) {
542 void *buf = ::calloc(byteSizeForCount(newCount), 1);
543 return new (buf) objc_protocol_list_t<P>(newCount);
546 void operator delete(void * p) {
550 objc_protocol_list_t(uint32_t newCount) : count(newCount) { }
552 // use newProtocolList instead
553 void* operator new (size_t);
557 template <typename P>
558 class objc_class_data_t {
559 typedef typename P::uint_t pint_t;
561 uint32_t instanceStart;
562 // Note there is 4-bytes of alignment padding between instanceSize and ivarLayout
563 // on 64-bit archs, but no padding on 32-bit archs.
564 // This union is a way to model that.
566 uint32_t instanceSize;
572 pint_t baseProtocols;
574 pint_t weakIvarLayout;
575 pint_t baseProperties;
578 bool isMetaClass() { return P::E::get32(flags) & (1 << 0); }
579 bool isRootClass() { return P::E::get32(flags) & (1 << 1); }
581 uint32_t getInstanceStart() { return P::E::get32(instanceStart); }
582 void setInstanceStart(uint32_t newStart) { P::E::set32(instanceStart, newStart); }
584 uint32_t getInstanceSize() { return P::E::get32(instanceSize.instanceSize); }
585 void setInstanceSize(uint32_t newSiz) { P::E::set32(instanceSize.instanceSize, newSiz); }
587 objc_method_list_t<P> *getMethodList(ContentAccessor* cache) const { return (objc_method_list_t<P> *)cache->contentForVMAddr(P::getP(baseMethods)); }
589 objc_protocol_list_t<P> *getProtocolList(ContentAccessor* cache) const { return (objc_protocol_list_t<P> *)cache->contentForVMAddr(P::getP(baseProtocols)); }
591 objc_ivar_list_t<P> *getIvarList(ContentAccessor* cache) const { return (objc_ivar_list_t<P> *)cache->contentForVMAddr(P::getP(ivars)); }
593 objc_property_list_t<P> *getPropertyList(ContentAccessor* cache) const { return (objc_property_list_t<P> *)cache->contentForVMAddr(P::getP(baseProperties)); }
595 const char * getName(ContentAccessor* cache) const { return (const char *)cache->contentForVMAddr(P::getP(name)); }
597 void setMethodList(ContentAccessor* cache, objc_method_list_t<P>* mlist) {
598 P::setP(baseMethods, cache->vmAddrForContent(mlist));
601 void setProtocolList(ContentAccessor* cache, objc_protocol_list_t<P>* protolist) {
602 P::setP(baseProtocols, cache->vmAddrForContent(protolist));
605 void setPropertyList(ContentAccessor* cache, objc_property_list_t<P>* proplist) {
606 P::setP(baseProperties, cache->vmAddrForContent(proplist));
609 void addMethodListPointer(std::vector<void*>& pointersToAdd) {
610 pointersToAdd.push_back(&this->baseMethods);
613 void addPropertyListPointer(std::vector<void*>& pointersToAdd) {
614 pointersToAdd.push_back(&this->baseProperties);
617 void addProtocolListPointer(std::vector<void*>& pointersToAdd) {
618 pointersToAdd.push_back(&this->baseProtocols);
622 template <typename P>
624 typedef typename P::uint_t pint_t;
633 bool isMetaClass(ContentAccessor* cache) const { return getData(cache)->isMetaClass(); }
634 bool isRootClass(ContentAccessor* cache) const { return getData(cache)->isRootClass(); }
636 objc_class_t<P> *getIsa(ContentAccessor* cache) const { return (objc_class_t<P> *)cache->contentForVMAddr(P::getP(isa)); }
638 objc_class_t<P> *getSuperclass(ContentAccessor* cache) const { return (objc_class_t<P> *)cache->contentForVMAddr(P::getP(superclass)); }
640 // Low bit marks Swift classes.
641 objc_class_data_t<P> *getData(ContentAccessor* cache) const { return (objc_class_data_t<P> *)cache->contentForVMAddr(P::getP(data & ~0x1LL)); }
643 objc_method_list_t<P> *getMethodList(ContentAccessor* cache) const {
644 objc_class_data_t<P>* d = getData(cache);
645 return d->getMethodList(cache);
648 objc_protocol_list_t<P> *getProtocolList(ContentAccessor* cache) const { return getData(cache)->getProtocolList(cache); }
650 objc_property_list_t<P> *getPropertyList(ContentAccessor* cache) const { return getData(cache)->getPropertyList(cache); }
652 const char* getName(ContentAccessor* cache) const {
653 return getData(cache)->getName(cache);
656 void setMethodList(ContentAccessor* cache, objc_method_list_t<P>* mlist) {
657 getData(cache)->setMethodList(cache, mlist);
660 void setProtocolList(ContentAccessor* cache, objc_protocol_list_t<P>* protolist) {
661 getData(cache)->setProtocolList(cache, protolist);
664 void setPropertyList(ContentAccessor* cache, objc_property_list_t<P>* proplist) {
665 getData(cache)->setPropertyList(cache, proplist);
668 void addMethodListPointer(ContentAccessor* cache, std::vector<void*>& pointersToAdd) {
669 getData(cache)->addMethodListPointer(pointersToAdd);
672 void addPropertyListPointer(ContentAccessor* cache, std::vector<void*>& pointersToAdd) {
673 getData(cache)->addPropertyListPointer(pointersToAdd);
676 void addProtocolListPointer(ContentAccessor* cache, std::vector<void*>& pointersToAdd) {
677 getData(cache)->addProtocolListPointer(pointersToAdd);
684 template <typename P>
685 class objc_category_t {
686 typedef typename P::uint_t pint_t;
690 pint_t instanceMethods;
693 pint_t instanceProperties;
697 const char * getName(ContentAccessor* cache) const { return (const char *)cache->contentForVMAddr(P::getP(name)); }
699 objc_class_t<P> *getClass(ContentAccessor* cache) const { return (objc_class_t<P> *)cache->contentForVMAddr(P::getP(cls)); }
701 objc_method_list_t<P> *getInstanceMethods(ContentAccessor* cache) const { return (objc_method_list_t<P> *)cache->contentForVMAddr(P::getP(instanceMethods)); }
703 objc_method_list_t<P> *getClassMethods(ContentAccessor* cache) const { return (objc_method_list_t<P> *)cache->contentForVMAddr(P::getP(classMethods)); }
705 objc_protocol_list_t<P> *getProtocols(ContentAccessor* cache) const { return (objc_protocol_list_t<P> *)cache->contentForVMAddr(P::getP(protocols)); }
707 objc_property_list_t<P> *getInstanceProperties(ContentAccessor* cache) const { return (objc_property_list_t<P> *)cache->contentForVMAddr(P::getP(instanceProperties)); }
709 void getPointers(std::set<void*>& pointersToRemove) {
710 pointersToRemove.insert(&name);
711 pointersToRemove.insert(&cls);
712 pointersToRemove.insert(&instanceMethods);
713 pointersToRemove.insert(&classMethods);
714 pointersToRemove.insert(&protocols);
715 pointersToRemove.insert(&instanceProperties);
721 template <typename P>
722 class objc_message_ref_t {
723 typedef typename P::uint_t pint_t;
729 pint_t getName() const { return (pint_t)P::getP(sel); }
731 void setName(pint_t newName) { P::setP(sel, newName); }
734 // Call visitor.visitIvar() on every ivar in a given class.
735 template <typename P, typename V>
737 typedef typename P::uint_t pint_t;
741 IvarWalker(V& visitor) : ivarVisitor(visitor) { }
743 void walk(ContentAccessor* cache, const macho_header<P>* header, objc_class_t<P> *cls)
745 objc_class_data_t<P> *data = cls->getData(cache);
746 objc_ivar_list_t<P> *ivars = data->getIvarList(cache);
748 for (pint_t i = 0; i < ivars->getCount(); i++) {
749 objc_ivar_t<P>& ivar = ivars->get(i);
750 //fprintf(stderr, "visiting ivar: %s\n", ivar.getName(cache));
751 ivarVisitor.visitIvar(cache, header, cls, &ivar);
754 //fprintf(stderr, "no ivars\n");
758 void visitClass(ContentAccessor* cache, const macho_header<P>* header, objc_class_t<P> *cls)
760 walk(cache, header, cls);
764 // Call visitor.visitClass() on every class.
765 template <typename P, typename V>
767 typedef typename P::uint_t pint_t;
771 ClassWalker(V& visitor) : _visitor(visitor) { }
773 void walk(ContentAccessor* cache, const macho_header<P>* header)
775 PointerSection<P, objc_class_t<P>*> classList(cache, header, "__DATA", "__objc_classlist");
777 for (pint_t i = 0; i < classList.count(); i++) {
778 objc_class_t<P>* cls = classList.get(i);
779 //fprintf(stderr, "visiting class: %s\n", cls->getName(cache));
780 if (cls) _visitor.visitClass(cache, header, cls);
785 // Call visitor.visitProtocol() on every protocol.
786 template <typename P, typename V>
787 class ProtocolWalker {
788 typedef typename P::uint_t pint_t;
792 ProtocolWalker(V& visitor) : _protocolVisitor(visitor) { }
794 void walk(ContentAccessor* cache, const macho_header<P>* header)
796 PointerSection<P, objc_protocol_t<P> *>
797 protocols(cache, header, "__DATA", "__objc_protolist");
799 for (pint_t i = 0; i < protocols.count(); i++) {
800 objc_protocol_t<P> *proto = protocols.get(i);
801 _protocolVisitor.visitProtocol(cache, header, proto);
806 // Call visitor.visitProtocolReference() on every protocol.
807 template <typename P, typename V>
808 class ProtocolReferenceWalker {
809 typedef typename P::uint_t pint_t;
812 void visitProtocolList(ContentAccessor* cache,
813 objc_protocol_list_t<P>* 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 = _visitor.visitProtocolReference(cache, oldValue);
819 protolist->setVMAddress(i, newValue);
823 friend class ClassWalker<P, ProtocolReferenceWalker<P, V>>;
825 void visitClass(ContentAccessor* cache, const macho_header<P>*,
826 objc_class_t<P>* cls)
828 visitProtocolList(cache, cls->getProtocolList(cache));
829 visitProtocolList(cache, cls->getIsa(cache)->getProtocolList(cache));
834 ProtocolReferenceWalker(V& visitor) : _visitor(visitor) { }
835 void walk(ContentAccessor* cache, const macho_header<P>* header)
837 // @protocol expressions
838 PointerSection<P, objc_protocol_t<P> *>
839 protorefs(cache, header, "__DATA", "__objc_protorefs");
840 for (pint_t i = 0; i < protorefs.count(); i++) {
841 pint_t oldValue = protorefs.getVMAddress(i);
842 pint_t newValue = _visitor.visitProtocolReference(cache, oldValue);
843 protorefs.setVMAddress(i, newValue);
846 // protocol lists in classes
847 ClassWalker<P, ProtocolReferenceWalker<P, V>> classes(*this);
848 classes.walk(cache, header);
850 // protocol lists in protocols
851 // __objc_protolists itself is NOT updated
852 PointerSection<P, objc_protocol_t<P> *>
853 protocols(cache, header, "__DATA", "__objc_protolist");
854 for (pint_t i = 0; i < protocols.count(); i++) {
855 objc_protocol_t<P>* proto = protocols.get(i);
856 visitProtocolList(cache, proto->getProtocols(cache));
857 // not recursive: every old protocol object
858 // 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 P, typename V>
868 class MethodListWalker {
870 typedef typename P::uint_t pint_t;
876 MethodListWalker(V& visitor) : mVisitor(visitor) { }
878 void walk(ContentAccessor* cache, const macho_header<P>* header)
880 // Method lists in classes
881 PointerSection<P, objc_class_t<P> *>
882 classes(cache, header, "__DATA", "__objc_classlist");
884 for (pint_t i = 0; i < classes.count(); i++) {
885 objc_class_t<P> *cls = classes.get(i);
886 objc_method_list_t<P> *mlist;
887 if ((mlist = cls->getMethodList(cache))) {
888 mVisitor.visitMethodList(mlist);
890 if ((mlist = cls->getIsa(cache)->getMethodList(cache))) {
891 mVisitor.visitMethodList(mlist);
895 // Method lists from categories
896 PointerSection<P, objc_category_t<P> *>
897 cats(cache, header, "__DATA", "__objc_catlist");
898 for (pint_t i = 0; i < cats.count(); i++) {
899 objc_category_t<P> *cat = cats.get(i);
900 objc_method_list_t<P> *mlist;
901 if ((mlist = cat->getInstanceMethods(cache))) {
902 mVisitor.visitMethodList(mlist);
904 if ((mlist = cat->getClassMethods(cache))) {
905 mVisitor.visitMethodList(mlist);
909 // Method description lists from protocols
910 PointerSection<P, objc_protocol_t<P> *>
911 protocols(cache, header, "__DATA", "__objc_protolist");
912 for (pint_t i = 0; i < protocols.count(); i++) {
913 objc_protocol_t<P> *proto = protocols.get(i);
914 objc_method_list_t<P> *mlist;
915 pint_t *typelist = proto->getExtendedMethodTypes(cache);
917 if ((mlist = proto->getInstanceMethods(cache))) {
918 mVisitor.visitProtocolMethodList(mlist, typelist);
919 if (typelist) typelist += mlist->getCount();
921 if ((mlist = proto->getClassMethods(cache))) {
922 mVisitor.visitProtocolMethodList(mlist, typelist);
923 if (typelist) typelist += mlist->getCount();
925 if ((mlist = proto->getOptionalInstanceMethods(cache))) {
926 mVisitor.visitProtocolMethodList(mlist, typelist);
927 if (typelist) typelist += mlist->getCount();
929 if ((mlist = proto->getOptionalClassMethods(cache))) {
930 mVisitor.visitProtocolMethodList(mlist, typelist);
931 if (typelist) typelist += mlist->getCount();
938 // Update selector references. The visitor performs recording and uniquing.
939 template <typename P, typename V>
940 class SelectorOptimizer {
942 typedef typename P::uint_t pint_t;
946 friend class MethodListWalker<P, SelectorOptimizer<P,V> >;
947 void visitMethodList(objc_method_list_t<P> *mlist)
949 // Gather selectors. Update method names.
950 for (uint32_t m = 0; m < mlist->getCount(); m++) {
951 pint_t oldValue = mlist->get(m).getName();
952 pint_t newValue = mVisitor.visit(oldValue);
953 mlist->get(m).setName(newValue);
955 // Do not setFixedUp: the methods are not yet sorted.
958 void visitProtocolMethodList(objc_method_list_t<P> *mlist, pint_t *types)
960 visitMethodList(mlist);
965 SelectorOptimizer(V& visitor) : mVisitor(visitor) { }
967 void optimize(ContentAccessor* cache, const macho_header<P>* header)
969 // method lists in classes, categories, and protocols
970 MethodListWalker<P, SelectorOptimizer<P,V> > mw(*this);
971 mw.walk(cache, header);
973 // @selector references
974 PointerSection<P, const char *>
975 selrefs(cache, header, "__DATA", "__objc_selrefs");
976 for (pint_t i = 0; i < selrefs.count(); i++) {
977 pint_t oldValue = selrefs.getVMAddress(i);
978 pint_t newValue = mVisitor.visit(oldValue);
979 selrefs.setVMAddress(i, newValue);
982 // message references
983 ArraySection<P, objc_message_ref_t<P> >
984 msgrefs(cache, header, "__DATA", "__objc_msgrefs");
985 for (pint_t i = 0; i < msgrefs.count(); i++) {
986 objc_message_ref_t<P>& msg = msgrefs.get(i);
987 pint_t oldValue = msg.getName();
988 pint_t newValue = mVisitor.visit(oldValue);
989 msg.setName(newValue);
995 // Update selector references. The visitor performs recording and uniquing.
996 template <typename P>
997 class IvarOffsetOptimizer {
999 uint32_t _maxAlignment;
1000 uint32_t _optimized;
1004 IvarOffsetOptimizer() : _optimized(0) { }
1006 size_t optimized() const { return _optimized; }
1008 // dual purpose ivar visitor function
1009 // if slide!=0 then slides the ivar by that amount, otherwise computes _maxAlignment
1010 void visitIvar(ContentAccessor* cache, const macho_header<P>* /*unused, may be NULL*/, objc_class_t<P> *cls, objc_ivar_t<P> *ivar)
1013 uint32_t alignment = ivar->getAlignment();
1014 if (alignment > _maxAlignment) _maxAlignment = alignment;
1016 // skip anonymous bitfields
1017 if (ivar->hasOffset()) {
1018 uint32_t oldOffset = (uint32_t)ivar->getOffset(cache);
1019 ivar->setOffset(cache, oldOffset + _slide);
1021 //fprintf(stderr, "%d -> %d for %s.%s\n", oldOffset, oldOffset + _slide, cls->getName(cache), ivar->getName(cache));
1023 //fprintf(stderr, "NULL offset\n");
1028 // Class visitor function. Evaluates whether to slide ivars and performs slide if needed.
1029 // The slide algorithm is also implemented in objc. Any changes here should be reflected there also.
1030 void visitClass(ContentAccessor* cache, const macho_header<P>* /*unused, may be NULL*/, objc_class_t<P> *cls)
1032 objc_class_t<P> *super = cls->getSuperclass(cache);
1034 // Recursively visit superclasses to ensure we have the correct superclass start
1035 // Note that we don't need the macho_header, so just pass NULL.
1036 visitClass(cache, nullptr, super);
1038 objc_class_data_t<P> *data = cls->getData(cache);
1039 objc_class_data_t<P> *super_data = super->getData(cache);
1040 int32_t diff = super_data->getInstanceSize() - data->getInstanceStart();
1042 IvarWalker<P, IvarOffsetOptimizer<P> > ivarVisitor(*this);
1046 // This walk computes _maxAlignment
1047 ivarVisitor.walk(cache, nullptr, cls);
1049 // Compute a slide value that preserves that alignment
1050 uint32_t alignMask = _maxAlignment - 1;
1051 if (diff & alignMask) diff = (diff + alignMask) & ~alignMask;
1053 // Slide all of this class's ivars en masse
1056 //fprintf(stderr, "Sliding ivars in %s by %u (superclass was %d, now %d)\n", cls->getName(cache), _slide, data->getInstanceStart(), super_data->getInstanceSize());
1057 ivarVisitor.walk(cache, nullptr, cls);
1058 data->setInstanceStart(data->getInstanceStart() + _slide);
1059 data->setInstanceSize(data->getInstanceSize() + _slide);
1065 // Enumerates objc classes in the module and performs any ivar slides
1066 void optimize(ContentAccessor* cache, const macho_header<P>* header)
1068 // The slide code cannot fix up GC layout strings so skip modules that support or require GC
1069 const macho_section<P> *imageInfoSection = header->getSection("__DATA", "__objc_imageinfo");
1070 if (imageInfoSection) {
1071 objc_image_info<P> *info = (objc_image_info<P> *)cache->contentForVMAddr(imageInfoSection->addr());
1072 if (!info->supportsGCFlagSet() && !info->requiresGCFlagSet()) {
1073 ClassWalker<P, IvarOffsetOptimizer<P> > classVisitor(*this);
1074 classVisitor.walk(cache, header);
1076 //fprintf(stderr, "GC support present - skipped module\n");
1083 // Detect classes that have missing weak-import superclasses.
1084 template <typename P>
1085 class WeakClassDetector {
1088 friend class ClassWalker<P, WeakClassDetector<P>>;
1089 void visitClass(ContentAccessor* cache, const macho_header<P>*,
1090 objc_class_t<P>* cls)
1092 auto supercls = cls->getSuperclass(cache);
1094 // okay: class with superclass
1095 // Note that the superclass itself might have a missing superclass.
1096 // That is fine for mere detection because we will visit the
1097 // superclass separately.
1098 } else if (cls->isRootClass(cache)) {
1099 // okay: root class is expected to have no superclass
1101 // bad: cls's superclass is missing.
1102 cache->diagnostics().warning("Superclass of class '%s' is weak-import and missing.",
1103 cls->getName(cache));
1109 bool noMissingWeakSuperclasses(ContentAccessor* cache,
1110 std::vector<const macho_header<P>*> dylibs)
1113 ClassWalker<P, WeakClassDetector<P>> classes(*this);
1114 for (auto mh : dylibs) {
1115 classes.walk(cache, mh);
1122 // Sort methods in place by selector.
1123 template <typename P>
1124 class MethodListSorter {
1126 typedef typename P::uint_t pint_t;
1128 uint32_t _optimized;
1130 friend class MethodListWalker<P, MethodListSorter<P> >;
1131 void visitMethodList(objc_method_list_t<P> *mlist)
1133 typename objc_method_t<P>::SortBySELAddress sorter;
1134 std::stable_sort(mlist->begin(), mlist->end(), sorter);
1135 mlist->setFixedUp();
1139 void visitProtocolMethodList(objc_method_list_t<P> *mlist, pint_t *typelist)
1141 typename objc_method_t<P>::SortBySELAddress sorter;
1142 // can't easily use std::stable_sort here
1143 for (uint32_t i = 0; i < mlist->getCount(); i++) {
1144 for (uint32_t j = i+1; j < mlist->getCount(); j++) {
1145 objc_method_t<P>& mi = mlist->get(i);
1146 objc_method_t<P>& mj = mlist->get(j);
1147 if (! sorter(mi, mj)) {
1149 if (typelist) std::swap(typelist[i], typelist[j]);
1154 mlist->setFixedUp();
1159 MethodListSorter() : _optimized(0) { }
1161 size_t optimized() const { return _optimized; }
1163 void optimize(ContentAccessor* cache, const macho_header<P>* header)
1165 MethodListWalker<P, MethodListSorter<P> > mw(*this);
1166 mw.walk(cache, header);
1171 template <typename P, typename InfoT>
1172 class HeaderInfoOptimizer {
1175 typedef typename P::uint_t pint_t;
1177 HeaderInfoOptimizer() : _hInfos(0), _count(0) { }
1179 const char* init(uint32_t count, uint8_t*& buf, size_t& bufSize) {
1183 size_t requiredSize =
1184 2*sizeof(uint32_t) + count*sizeof(InfoT);
1185 if (bufSize < requiredSize) {
1186 return "libobjc's read/write section is too small (metadata not optimized)";
1189 uint32_t *buf32 = (uint32_t *)buf;
1190 P::E::set32(buf32[0], count);
1191 P::E::set32(buf32[1], sizeof(InfoT));
1192 _hInfos = (InfoT*)(buf32+2);
1194 buf += requiredSize;
1195 bufSize -= requiredSize;
1200 void update(ContentAccessor* cache, const macho_header<P>* mh, std::vector<void*>& pointersInData) {
1201 InfoT* hi = new(&_hInfos[_count++]) InfoT(cache, mh);
1205 InfoT* hinfoForHeader(ContentAccessor* cache, const macho_header<P>* mh) {
1206 // FIXME: could be binary search
1207 uint64_t mh_vmaddr = cache->vmAddrForContent((void*)mh);
1208 for (size_t i = 0; i < _count; i++) {
1209 InfoT* hi = &_hInfos[i];
1210 if (hi->header_vmaddr(cache) == mh_vmaddr) return hi;