9, iOS bottom Analysis - loading the class

Program starts

  1. Static library is compiled directly into the program
    1. DLL is dynamically added when needed, dyld is a dynamic library link
    2. dyld is Apple's dynamic linker, each program depends on dynamic libraries are required by dyld (located in / usr / lib / dyld) one by one, loaded into memory. The image file is loaded
    3. If the running time of each program are repeated to load, it will inevitably result in slow, in order to optimize boot times and improve program performance, it uses a shared cache mechanism. All default dynamic link libraries are combined into one large cache file into the directory under /System/Library/Caches/com.apple.dyld/ were saved from the frame
    4. Inside there dyld_shared_cache_armv7s such as the iPhone and dyld_shared_cache_armv64 two files, as shown below.
    1. objc_init
      1. It provides the runtime environment
      2. After the program starts from dyld how to objc_init of?
        1. Recursive initialization system library (system libraries)
        2. dispatch (scheduling)
        3. objc   ->  objc_init
        4. objc_init the mapping, the map data in the image file to map out table
  2. images 
    1. dyld communication with objc_init
    2. The image file only in the dyld
    3. objc_init dependent image files
  3. Callback
    1. * snotif
    2. registered
    3. pointer
  4. Load images
    1. Stored in memory with the table (stored in memory in the form of an aggregate form, table form table, map)
    2. Image file to be read out - keep in memory
      1. What data are read?
      2. Binary (type, protocol, classification, sel)

 

Class loading

_objc_init

  1. After the program at startup, the first dynamic link library used dyld, finished a series of preparatory operations, will enter into _objc_init method
  2. /***********************************************************************
    * _objc_init
    * Bootstrap initialization. Registers our image notifier with dyld.
    * Called by libSystem BEFORE library initialization time
    **********************************************************************/
    
    void _objc_init(void)
    {
        static bool initialized = false;
        if (initialized) return;
        initialized = true;
        
        // fixme defer initialization until an objc-using image is found?
        environ_init();   //读取环境变量
        tls_init();       //关于线程key的绑定
        static_init();    //系统级别的类,在这里进行初始化
        lock_init();      //是空实现!!这个方法里面什么都没写
        exception_init(); //初始化libobic的异常处理系统
    
        _dyld_objc_notify_register(&map_images, load_images, unmap_image); //注册处理程序,以便在映射,取消映射和初始化objc图像是调用。
    }
    

     

1、environ_init

  1. Reading environmental variables affect the operation time of the terminal can enter export OBJC_HELP = 1 to print environment variables help
  2. objc[3206]: Objective-C runtime debugging. Set variable=YES to enable.
    objc[3206]: OBJC_HELP: describe available environment variables
    objc[3206]: OBJC_PRINT_OPTIONS: list which options are set
    objc[3206]: OBJC_PRINT_IMAGES: log image and library names as they are loaded
    objc[3206]: OBJC_PRINT_IMAGE_TIMES: measure duration of image loading steps
    objc[3206]: OBJC_PRINT_LOAD_METHODS: log calls to class and category +load methods
    objc[3206]: OBJC_PRINT_INITIALIZE_METHODS: log calls to class +initialize methods
    objc[3206]: OBJC_PRINT_RESOLVED_METHODS: log methods created by +resolveClassMethod: and +resolveInstanceMethod:
    objc[3206]: OBJC_PRINT_CLASS_SETUP: log progress of class and category setup
    objc[3206]: OBJC_PRINT_PROTOCOL_SETUP: log progress of protocol setup
    objc[3206]: OBJC_PRINT_IVAR_SETUP: log processing of non-fragile ivars
    objc[3206]: OBJC_PRINT_VTABLE_SETUP: log processing of class vtables
    objc[3206]: OBJC_PRINT_VTABLE_IMAGES: print vtable images showing overridden methods
    objc[3206]: OBJC_PRINT_CACHE_SETUP: log processing of method caches
    
    

     

2、tls_init()

  1. Binding thread on key - for example, the number of threads per destructor
  2. Destructor: destructor (destructor) and the constructor contrast, when the object end of its life cycle , such as where the object function has been invoked when completed, the system automatically performs the destructor. Destructors are often used to make "clean up the aftermath of" work (for example, when creating a new object is opened up a memory space, delete call the destructor is automatically released after memory).

3、static_init()

  1. Run C ++ static constructor. Before dyld call our static constructor, "libc" will call "_objc_init ()", so we have to do it yourself.
  2. This place than some dyld (_dyld_objc_notify_register) forward, because this is a system-level, must be initialized forward than dyld
  3. This is a system-level classes, where initialized. libc call _objc_init dyld before calling the static constructor (),
  4. /***********************************************************************
    * static_init
    * Run C++ static constructor functions.
    * libc calls _objc_init() before dyld would call our static constructors, 
    libc在dyld调用静态构造函数之前调用_objc_init(),
    * so we have to do it ourselves.
    **********************************************************************/
    static void static_init()
    {
        size_t count;
        auto inits = getLibobjcInitializers(&_mh_dylib_header, &count);
        for (size_t i = 0; i < count; i++) {
            inits[i]();
        }
    }

     

4、lock_init()

  1. That objc only exception is the banknote with a set of C ++ that - is itself written in C ++ by
  2. Realization is empty! ! This method is nothing written inside

5、_dyld_objc_notify_register

Foregoing conditions are ready , _dyld_objc_notify_register , annotations can know by the method used to just only objc runtime, for map_images and load_images

_dyld_objc_notify_register(&map_images, load_images, unmap_images)

  1. Use only objc run, objc here is a run-time environment
  2. Register a handler for the mapping, mapping and initialization canceled objc image is called.
  3. dyld callback will use an array of image file contains objc_image_info "mapped" function
  4. map_images
    1. Reads the image file
  5. load_images
  6. unmap_image
    1. Program termination, abnormal, get rid of part of the memory image file

 

 

Unfinished. . . to be continued

 

 

 

 

 

 

 

 

Published 83 original articles · won praise 12 · views 180 000 +

Guess you like

Origin blog.csdn.net/shengdaVolleyball/article/details/104068747