类的加载之分类

category_t

struct category_t {
    const char *name;
    classref_t cls;
    struct method_list_t *instanceMethods;
    struct method_list_t *classMethods;
    struct protocol_list_t *protocols;
    struct property_list_t *instanceProperties;
    // Fields below this point are not always present on disk.
    struct property_list_t *_classProperties;

    method_list_t *methodsForMeta(bool isMeta) {
        if (isMeta) return classMethods;
        else return instanceMethods;
    }

    property_list_t *propertiesForMeta(bool isMeta, struct header_info *hi);
    
    protocol_list_t *protocolsForMeta(bool isMeta) {
        if (isMeta) return nullptr;
        else return protocols;
    }
};
分类数据加载时机.png

一个主类,一个分类

主类有+load,分类有+load:attachToClass attachCategories方法打断点*

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
  * frame #0: 0x000000010031e0ab libobjc.A.dylib`objc::UnattachedCategories::attachToClass(this=0x000000010036ae78, cls=LGPerson, previously=LGPerson, flags=1) at objc-runtime-new.mm:1215:66
    frame #1: 0x000000010031d869 libobjc.A.dylib`methodizeClass(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:1558:32
    frame #2: 0x0000000100309e7b libobjc.A.dylib`realizeClassWithoutSwift(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:2774:5
    frame #3: 0x00000001002e730f libobjc.A.dylib`_read_images(hList=0x00007ffeefbf21c0, hCount=296, totalClasses=24576, unoptimizedTotalClasses=24576) at objc-runtime-new.mm:3826:13
    frame #4: 0x00000001002e6148 libobjc.A.dylib`map_images_nolock(mhCount=296, mhPaths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-os.mm:596:9
    frame #5: 0x00000001002e5ca3 libobjc.A.dylib`map_images(count=296, paths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-runtime-new.mm:3155:12
    frame #6: 0x0000000100019d2c dyld`dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) + 1778
    frame #7: 0x0000000100019ecf dyld`dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) + 63
    frame #8: 0x00007fff203438ef libdyld.dylib`_dyld_objc_notify_register + 113
    frame #9: 0x00000001002e575a libobjc.A.dylib`_objc_init at objc-os.mm:939:5
    frame #10: 0x00000001004636db libdispatch.dylib`_os_object_init + 13
    frame #11: 0x0000000100474928 libdispatch.dylib`libdispatch_init + 285
    frame #12: 0x00007fff2a53965b libSystem.B.dylib`libSystem_initializer + 224
    frame #13: 0x0000000100031079 dyld`ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) + 559
    frame #14: 0x0000000100031478 dyld`ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) + 40
    frame #15: 0x000000010002bd1a dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 492
    frame #16: 0x000000010002bc85 dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 343
    frame #17: 0x0000000100029b82 dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 188
    frame #18: 0x0000000100029c22 dyld`ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 82
    frame #19: 0x00000001000165e9 dyld`dyld::initializeMainExecutable() + 129
    frame #20: 0x000000010001c9a4 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 8288
    frame #21: 0x000000010001522b dyld`dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 457
    frame #22: 0x0000000100015025 dyld`_dyld_start + 37
(lldb) 


(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
  * frame #0: 0x000000010032332e libobjc.A.dylib`attachCategories(cls=0x0000000100008320, cats_list=0x00007ffeefbf5b68, cats_count=1, flags=10) at objc-runtime-new.mm:1432:62
    frame #1: 0x0000000100325a7f libobjc.A.dylib`load_categories_nolock(this=0x00007ffeefbf5bb0, catlist=0x0000000100004060)::$_6::operator()(category_t* const*) const at objc-runtime-new.mm:3215:25
    frame #2: 0x000000010030b952 libobjc.A.dylib`load_categories_nolock(hi=0x000000010180b100) at objc-runtime-new.mm:3224:5
    frame #3: 0x000000010030a9ba libobjc.A.dylib`loadAllCategories() at objc-runtime-new.mm:3232:9
    frame #4: 0x00000001002e8ce6 libobjc.A.dylib`load_images(path="/private/tmp/objc.dst/usr/lib/libobjc.A.dylib", mh=0x00000001002e4000) at objc-runtime-new.mm:3250:9
    frame #5: 0x000000010001628a dyld`dyld::notifySingle(dyld_image_states, ImageLoader const*, ImageLoader::InitializerTimingList*) + 425
    frame #6: 0x000000010002bd08 dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 474
    frame #7: 0x0000000100029b82 dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 188
    frame #8: 0x0000000100029bac dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 230
    frame #9: 0x0000000100029c22 dyld`ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 82
    frame #10: 0x00000001000165e9 dyld`dyld::initializeMainExecutable() + 129
    frame #11: 0x000000010001c9a4 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 8288
    frame #12: 0x000000010001522b dyld`dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 457
    frame #13: 0x0000000100015025 dyld`_dyld_start + 37
(lldb) 

_objc_init --> map_images -> map_images_nolock -> _read_images -> realizeClassWithoutSwift --> methodizeClass -> attachToClass

load_images --> loadAllCategories -> load_categories_nolock -> attachCategories

在_read_images中对类做实现操作,在load_images方法中触发分类的数据加载,即rwe初始化,同时加载分类数据。

主类没有+load,分类有+load:attachToClass attachCategories方法打断点*

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
  * frame #0: 0x000000010031e0ab libobjc.A.dylib`objc::UnattachedCategories::attachToClass(this=0x000000010036ae78, cls=LGPerson, previously=LGPerson, flags=1) at objc-runtime-new.mm:1215:66
    frame #1: 0x000000010031d869 libobjc.A.dylib`methodizeClass(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:1558:32
    frame #2: 0x0000000100309e7b libobjc.A.dylib`realizeClassWithoutSwift(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:2774:5
    frame #3: 0x00000001002e730f libobjc.A.dylib`_read_images(hList=0x00007ffeefbf21c0, hCount=296, totalClasses=24576, unoptimizedTotalClasses=24576) at objc-runtime-new.mm:3826:13
    frame #4: 0x00000001002e6148 libobjc.A.dylib`map_images_nolock(mhCount=296, mhPaths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-os.mm:596:9
    frame #5: 0x00000001002e5ca3 libobjc.A.dylib`map_images(count=296, paths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-runtime-new.mm:3155:12
    frame #6: 0x0000000100019d2c dyld`dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) + 1778
    frame #7: 0x0000000100019ecf dyld`dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) + 63
    frame #8: 0x00007fff203438ef libdyld.dylib`_dyld_objc_notify_register + 113
    frame #9: 0x00000001002e575a libobjc.A.dylib`_objc_init at objc-os.mm:939:5
    frame #10: 0x00000001004636db libdispatch.dylib`_os_object_init + 13
    frame #11: 0x0000000100474928 libdispatch.dylib`libdispatch_init + 285
    frame #12: 0x00007fff2a53965b libSystem.B.dylib`libSystem_initializer + 224
    frame #13: 0x0000000100031079 dyld`ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) + 559
    frame #14: 0x0000000100031478 dyld`ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) + 40
    frame #15: 0x000000010002bd1a dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 492
    frame #16: 0x000000010002bc85 dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 343
    frame #17: 0x0000000100029b82 dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 188
    frame #18: 0x0000000100029c22 dyld`ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 82
    frame #19: 0x00000001000165e9 dyld`dyld::initializeMainExecutable() + 129
    frame #20: 0x000000010001c9a4 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 8288
    frame #21: 0x000000010001522b dyld`dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 457
    frame #22: 0x0000000100015025 dyld`_dyld_start + 37

_objc_init --> map_images -> map_images_nolock -> _read_images -> realizeClassWithoutSwift --> methodizeClass -> attachToClass

数据加载在_read_images就加载数据,数据来自data,data在编译时期就已经完成,即data中除了类的数据,还有分类的数据,与类绑定在一起

主类有+load,分类没有+load:attachToClass attachToClass方法打断点*

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
  * frame #0: 0x000000010031e0ab libobjc.A.dylib`objc::UnattachedCategories::attachToClass(this=0x000000010036ae78, cls=LGPerson, previously=LGPerson, flags=1) at objc-runtime-new.mm:1215:66
    frame #1: 0x000000010031d869 libobjc.A.dylib`methodizeClass(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:1558:32
    frame #2: 0x0000000100309e7b libobjc.A.dylib`realizeClassWithoutSwift(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:2774:5
    frame #3: 0x00000001002e730f libobjc.A.dylib`_read_images(hList=0x00007ffeefbf21c0, hCount=296, totalClasses=24576, unoptimizedTotalClasses=24576) at objc-runtime-new.mm:3826:13
    frame #4: 0x00000001002e6148 libobjc.A.dylib`map_images_nolock(mhCount=296, mhPaths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-os.mm:596:9
    frame #5: 0x00000001002e5ca3 libobjc.A.dylib`map_images(count=296, paths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-runtime-new.mm:3155:12
    frame #6: 0x0000000100019d2c dyld`dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) + 1778
    frame #7: 0x0000000100019ecf dyld`dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) + 63
    frame #8: 0x00007fff203438ef libdyld.dylib`_dyld_objc_notify_register + 113
    frame #9: 0x00000001002e575a libobjc.A.dylib`_objc_init at objc-os.mm:939:5
    frame #10: 0x00000001004636db libdispatch.dylib`_os_object_init + 13
    frame #11: 0x0000000100474928 libdispatch.dylib`libdispatch_init + 285
    frame #12: 0x00007fff2a53965b libSystem.B.dylib`libSystem_initializer + 224
    frame #13: 0x0000000100031079 dyld`ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) + 559
    frame #14: 0x0000000100031478 dyld`ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) + 40
    frame #15: 0x000000010002bd1a dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 492
    frame #16: 0x000000010002bc85 dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 343
    frame #17: 0x0000000100029b82 dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 188
    frame #18: 0x0000000100029c22 dyld`ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 82
    frame #19: 0x00000001000165e9 dyld`dyld::initializeMainExecutable() + 129
    frame #20: 0x000000010001c9a4 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 8288
    frame #21: 0x000000010001522b dyld`dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 457
    frame #22: 0x0000000100015025 dyld`_dyld_start + 37
(lldb) 

_objc_init --> map_images -> map_images_nolock -> _read_images -> realizeClassWithoutSwift --> methodizeClass -> attachToClass

数据加载在_read_images就加载数据,数据来自data,data在编译时期就已经完成,即data中除了类的数据,还有分类的数据,与类绑定在一起

主类没有+load,分类没有+load:attachToClass attachToClass方法打断点*

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
    frame #0: 0x000000010031e0ab libobjc.A.dylib`objc::UnattachedCategories::attachToClass(this=0x000000010036ae78, cls=0x00000001000082a0, previously=0x00000001000082a0, flags=2) at objc-runtime-new.mm:1215:66
    frame #1: 0x000000010031d869 libobjc.A.dylib`methodizeClass(cls=0x00000001000082a0, previously=0x0000000000000000) at objc-runtime-new.mm:1558:32
    frame #2: 0x0000000100309e7b libobjc.A.dylib`realizeClassWithoutSwift(cls=0x00000001000082a0, previously=0x0000000000000000) at objc-runtime-new.mm:2774:5
    frame #3: 0x0000000100326630 libobjc.A.dylib`realizeClassMaybeSwiftMaybeRelock(cls=0x00000001000082a0, lock=0x000000010036d080, leaveLocked=true) at objc-runtime-new.mm:2918:9
    frame #4: 0x00000001003264f2 libobjc.A.dylib`realizeClassMaybeSwiftAndLeaveLocked(cls=0x00000001000082a0, lock=0x000000010036d080) at objc-runtime-new.mm:2941:12
  * frame #5: 0x000000010031283d libobjc.A.dylib`realizeAndInitializeIfNeeded_locked(inst=0x00000001000082c8, cls=0x00000001000082a0, initialize=true) at objc-runtime-new.mm:6373:15
    frame #6: 0x000000010031231a libobjc.A.dylib`lookUpImpOrForward(inst=0x00000001000082c8, sel="alloc", cls=0x00000001000082a0, behavior=11) at objc-runtime-new.mm:6488:11
    frame #7: 0x00000001002ead1b libobjc.A.dylib`_objc_msgSend_uncached at objc-msg-x86_64.s:1153
    frame #8: 0x000000010034ef87 libobjc.A.dylib`objc_alloc [inlined] callAlloc(cls=LGPerson, checkNil=true, allocWithZone=false) at NSObject.mm:1940:12
    frame #9: 0x000000010034eede libobjc.A.dylib`objc_alloc(cls=LGPerson) at NSObject.mm:1956
    frame #10: 0x0000000100003a89 KCObjcBuild`main(argc=1, argv=0x00007ffeefbff468) at main.m:25:28 [opt]
    frame #11: 0x00007fff20357621 libdyld.dylib`start + 1
    frame #12: 0x00007fff20357621 libdyld.dylib`start + 1
(lldb) 

objc_msgSend -> lookUpImpOrForward -> realizeClassMaybeSwiftMaybeRelock -> realizeClassWithoutSwift -> methodizeClass -> attachToClass

数据加载推迟到第一次消息时。

一个主类,两个分类

主类有+load,分类都有+load:attachToClass attachCategories方法打断点*

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
  * frame #0: 0x000000010031e0ab libobjc.A.dylib`objc::UnattachedCategories::attachToClass(this=0x000000010036ae78, cls=LGPerson, previously=LGPerson, flags=1) at objc-runtime-new.mm:1215:66
    frame #1: 0x000000010031d869 libobjc.A.dylib`methodizeClass(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:1558:32
    frame #2: 0x0000000100309e7b libobjc.A.dylib`realizeClassWithoutSwift(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:2774:5
    frame #3: 0x00000001002e730f libobjc.A.dylib`_read_images(hList=0x00007ffeefbf21c0, hCount=296, totalClasses=24576, unoptimizedTotalClasses=24576) at objc-runtime-new.mm:3826:13
    frame #4: 0x00000001002e6148 libobjc.A.dylib`map_images_nolock(mhCount=296, mhPaths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-os.mm:596:9
    frame #5: 0x00000001002e5ca3 libobjc.A.dylib`map_images(count=296, paths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-runtime-new.mm:3155:12
    frame #6: 0x0000000100019d2c dyld`dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) + 1778
    frame #7: 0x0000000100019ecf dyld`dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) + 63
    frame #8: 0x00007fff203438ef libdyld.dylib`_dyld_objc_notify_register + 113
    frame #9: 0x00000001002e575a libobjc.A.dylib`_objc_init at objc-os.mm:939:5
    frame #10: 0x00000001004636db libdispatch.dylib`_os_object_init + 13
    frame #11: 0x0000000100474928 libdispatch.dylib`libdispatch_init + 285
    frame #12: 0x00007fff2a53965b libSystem.B.dylib`libSystem_initializer + 224
    frame #13: 0x0000000100031079 dyld`ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) + 559
    frame #14: 0x0000000100031478 dyld`ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) + 40
    frame #15: 0x000000010002bd1a dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 492
    frame #16: 0x000000010002bc85 dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 343
    frame #17: 0x0000000100029b82 dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 188
    frame #18: 0x0000000100029c22 dyld`ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 82
    frame #19: 0x00000001000165e9 dyld`dyld::initializeMainExecutable() + 129
    frame #20: 0x000000010001c9a4 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 8288
    frame #21: 0x000000010001522b dyld`dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 457
    frame #22: 0x0000000100015025 dyld`_dyld_start + 37
(lldb) 

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
  * frame #0: 0x000000010032332e libobjc.A.dylib`attachCategories(cls=LGPerson, cats_list=0x00007ffeefbf5b68, cats_count=1, flags=8) at objc-runtime-new.mm:1432:62
    frame #1: 0x00000001003259bb libobjc.A.dylib`load_categories_nolock(this=0x00007ffeefbf5bb0, catlist=0x0000000100004060)::$_6::operator()(category_t* const*) const at objc-runtime-new.mm:3205:25
    frame #2: 0x000000010030b952 libobjc.A.dylib`load_categories_nolock(hi=0x0000000100745090) at objc-runtime-new.mm:3224:5
    frame #3: 0x000000010030a9ba libobjc.A.dylib`loadAllCategories() at objc-runtime-new.mm:3232:9
    frame #4: 0x00000001002e8ce6 libobjc.A.dylib`load_images(path="/private/tmp/objc.dst/usr/lib/libobjc.A.dylib", mh=0x00000001002e4000) at objc-runtime-new.mm:3250:9
    frame #5: 0x000000010001628a dyld`dyld::notifySingle(dyld_image_states, ImageLoader const*, ImageLoader::InitializerTimingList*) + 425
    frame #6: 0x000000010002bd08 dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 474
    frame #7: 0x0000000100029b82 dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 188
    frame #8: 0x0000000100029bac dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 230
    frame #9: 0x0000000100029c22 dyld`ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 82
    frame #10: 0x00000001000165e9 dyld`dyld::initializeMainExecutable() + 129
    frame #11: 0x000000010001c9a4 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 8288
    frame #12: 0x000000010001522b dyld`dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 457
    frame #13: 0x0000000100015025 dyld`_dyld_start + 37
(lldb)

_objc_init --> map_images -> map_images_nolock -> _read_images -> realizeClassWithoutSwift --> methodizeClass -> attachToClass

load_images --> loadAllCategories -> load_categories_nolock -> attachCategories

在_read_images中对类做实现操作,在load_images方法中触发分类的数据加载,即rwe初始化,同时加载分类数据。

主类没有+load,分类都有+load:attachToClass attachCategories方法打断点*

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
  * frame #0: 0x000000010032332e libobjc.A.dylib`attachCategories(cls=LGPerson, cats_list=0x00000001011117a0, cats_count=2, flags=1) at objc-runtime-new.mm:1432:62
    frame #1: 0x000000010031e24b libobjc.A.dylib`objc::UnattachedCategories::attachToClass(this=0x000000010036ae78, cls=LGPerson, previously=LGPerson, flags=1) at objc-runtime-new.mm:1229:17
    frame #2: 0x000000010031d869 libobjc.A.dylib`methodizeClass(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:1558:32
    frame #3: 0x0000000100309e7b libobjc.A.dylib`realizeClassWithoutSwift(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:2774:5
    frame #4: 0x000000010030abc7 libobjc.A.dylib`prepare_load_methods(mhdr=0x0000000100000000) at objc-runtime-new.mm:3966:9
    frame #5: 0x00000001002e8d2d libobjc.A.dylib`load_images(path="/Users/xiaokai/Library/Developer/Xcode/DerivedData/objc-aiwtubulpcfbduezvurrpiupjzfn/Build/Products/Debug/KCObjcBuild", mh=0x0000000100000000) at objc-runtime-new.mm:3261:9
    frame #6: 0x000000010001628a dyld`dyld::notifySingle(dyld_image_states, ImageLoader const*, ImageLoader::InitializerTimingList*) + 425
    frame #7: 0x000000010002bd08 dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 474
    frame #8: 0x0000000100029b82 dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 188
    frame #9: 0x0000000100029c22 dyld`ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 82
    frame #10: 0x000000010001662f dyld`dyld::initializeMainExecutable() + 199
    frame #11: 0x000000010001c9a4 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 8288
    frame #12: 0x000000010001522b dyld`dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 457
    frame #13: 0x0000000100015025 dyld`_dyld_start + 37
(lldb) 

load_images --> prepare_load_methods -> realizeClassWithoutSwift -> methodizeClass -> attachToClass -> attachCategories

只要分类实现了load,会迫使主类提前加载。

主类有+load,分类都没有+load:attachToClass attachToClass方法打断点*

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
    frame #0: 0x000000010031e0ab libobjc.A.dylib`objc::UnattachedCategories::attachToClass(this=0x000000010036ae78, cls=LGPerson, previously=LGPerson, flags=1) at objc-runtime-new.mm:1215:66
    frame #1: 0x000000010031d869 libobjc.A.dylib`methodizeClass(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:1558:32
    frame #2: 0x0000000100309e7b libobjc.A.dylib`realizeClassWithoutSwift(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:2774:5
    frame #3: 0x00000001002e730f libobjc.A.dylib`_read_images(hList=0x00007ffeefbf21c0, hCount=296, totalClasses=24576, unoptimizedTotalClasses=24576) at objc-runtime-new.mm:3826:13
    frame #4: 0x00000001002e6148 libobjc.A.dylib`map_images_nolock(mhCount=296, mhPaths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-os.mm:596:9
  * frame #5: 0x00000001002e5ca3 libobjc.A.dylib`map_images(count=296, paths=0x00007ffeefbf3b40, mhdrs=0x00007ffeefbf2c60) at objc-runtime-new.mm:3155:12
    frame #6: 0x0000000100019d2c dyld`dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) + 1778
    frame #7: 0x0000000100019ecf dyld`dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) + 63
    frame #8: 0x00007fff203438ef libdyld.dylib`_dyld_objc_notify_register + 113
    frame #9: 0x00000001002e575a libobjc.A.dylib`_objc_init at objc-os.mm:939:5
    frame #10: 0x00000001004636db libdispatch.dylib`_os_object_init + 13
    frame #11: 0x0000000100474928 libdispatch.dylib`libdispatch_init + 285
    frame #12: 0x00007fff2a53965b libSystem.B.dylib`libSystem_initializer + 224
    frame #13: 0x0000000100031079 dyld`ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) + 559
    frame #14: 0x0000000100031478 dyld`ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) + 40
    frame #15: 0x000000010002bd1a dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 492
    frame #16: 0x000000010002bc85 dyld`ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 343
    frame #17: 0x0000000100029b82 dyld`ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 188
    frame #18: 0x0000000100029c22 dyld`ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 82
    frame #19: 0x00000001000165e9 dyld`dyld::initializeMainExecutable() + 129
    frame #20: 0x000000010001c9a4 dyld`dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 8288
    frame #21: 0x000000010001522b dyld`dyldbootstrap::start(dyld3::MachOLoaded const*, int, char const**, dyld3::MachOLoaded const*, unsigned long*) + 457
    frame #22: 0x0000000100015025 dyld`_dyld_start + 37
(lldb) 

_objc_init -> map_images -> map_images_nolock -> _read_images -> realizeClassWithoutSwift -> methodizeClass -> attachToClass

数据加载在_read_images就加载数据,数据来自data,data在编译时期就已经完成,即data中除了类的数据,还有分类的数据,与类绑定在一起

主类没有+load,分类都没有+load:attachToClass attachToClass方法打断点

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
  * frame #0: 0x000000010031e0ab libobjc.A.dylib`objc::UnattachedCategories::attachToClass(this=0x000000010036ae78, cls=LGPerson, previously=LGPerson, flags=1) at objc-runtime-new.mm:1215:66
    frame #1: 0x000000010031d869 libobjc.A.dylib`methodizeClass(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:1558:32
    frame #2: 0x0000000100309e7b libobjc.A.dylib`realizeClassWithoutSwift(cls=LGPerson, previously=0x0000000000000000) at objc-runtime-new.mm:2774:5
    frame #3: 0x0000000100326630 libobjc.A.dylib`realizeClassMaybeSwiftMaybeRelock(cls=LGPerson, lock=0x000000010036d080, leaveLocked=false) at objc-runtime-new.mm:2918:9
    frame #4: 0x000000010031662f libobjc.A.dylib`realizeClassMaybeSwiftAndUnlock(cls=LGPerson, lock=0x000000010036d080) at objc-runtime-new.mm:2935:12
    frame #5: 0x0000000100309300 libobjc.A.dylib`initializeAndMaybeRelock(cls=0x00000001000082e8, inst=0x0000000100008310, lock=0x000000010036d080, leaveLocked=true) at objc-runtime-new.mm:2244:19
    frame #6: 0x000000010032a1ea libobjc.A.dylib`initializeAndLeaveLocked(cls=0x00000001000082e8, obj=0x0000000100008310, lock=0x000000010036d080) at objc-runtime-new.mm:2269:12
    frame #7: 0x0000000100312885 libobjc.A.dylib`realizeAndInitializeIfNeeded_locked(inst=0x0000000100008310, cls=0x00000001000082e8, initialize=true) at objc-runtime-new.mm:6378:15
    frame #8: 0x000000010031231a libobjc.A.dylib`lookUpImpOrForward(inst=0x0000000100008310, sel="alloc", cls=0x00000001000082e8, behavior=11) at objc-runtime-new.mm:6488:11
    frame #9: 0x00000001002ead1b libobjc.A.dylib`_objc_msgSend_uncached at objc-msg-x86_64.s:1153
    frame #10: 0x000000010034ef87 libobjc.A.dylib`objc_alloc [inlined] callAlloc(cls=LGPerson, checkNil=true, allocWithZone=false) at NSObject.mm:1940:12
    frame #11: 0x000000010034eede libobjc.A.dylib`objc_alloc(cls=LGPerson) at NSObject.mm:1956
    frame #12: 0x0000000100003989 KCObjcBuild`main(argc=1, argv=0x00007ffeefbff468) at main.m:25:28 [opt]
    frame #13: 0x00007fff20357621 libdyld.dylib`start + 1
    frame #14: 0x00007fff20357621 libdyld.dylib`start + 1

objc_msgSend -> lookUpImpOrForward -> realizeClassMaybeSwiftMaybeRelock -> realizeClassWithoutSwift -> methodizeClass -> attachToClass

数据加载推迟到第一次消息时。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,772评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,458评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,610评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,640评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,657评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,590评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,962评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,631评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,870评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,611评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,704评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,386评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,969评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,944评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,179评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,742评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,440评论 2 342

推荐阅读更多精彩内容