Preamble: As a developer, if you are familiar with the development of many languages, you will find that the main function is common in most languages. We all start with the main function to focus on how programs are written, compiled, and executed. Before main, did the system do anything else? What did it do? Let’s see.

I. Application startup analysis

1, the program from writing to execution process

1. How does the code load into memory?

2. How do static libraries and dynamic libraries load into memory?

3, where is objc_init -> objc executed?

So we can think about how does this happen, how does everything get loaded into memory?

Static and dynamic libraries

We often use static libraries and dynamic libraries in our projects. Among them, UIKit, Foundation library, WebKit library and so on provided by the system are dynamic libraries. For example, we often use the customized static Framework. So, how do static libraries and dynamic libraries differ?

  • Dynamic library forms:.dylib and. Framework

  • Static library forms:.a and. Framework

As shown in the figure above, we analyze:

1, static library: when linked, static library will be completely copied to the executable file, multiple use of multiple redundant copies

2, dynamic library: link is not copied, the program is dynamically loaded into memory by the system when it is running, for the program to call, the system is only loaded once, multiple programs share, save memory

But how do the system’s dynamic libraries load into memory? In what way? So here we use a tool called dyLD dynamic linker.

Dynamic linker

3.1 Working process of dynamic linker

The above is the flow chart of dyLD loading work, through which the registration and loading process of dynamic library are mainly carried out.

Ii. Preliminary study on DYLD process

  • With this overview of the principles, let’s move on to the application code execution logic

After running, the program stops at the breakpoint position. After stack printing (BT is the LLDB stack printing command), it is found that the program crashed in LLDB. Here we can’t get more information to trace.

So, from our programming experience, we figured out that we’re going to load the program before main, so let’s try, load the program in ViewController, add a breakpoint, run the program.

It turned out to be fine. We stopped at the load method of the ViewController and found a series of functions about the dyLD by printing the stack. The following leads to the theme of our exploration: DYLD (dynamic linker), our function tracing, will also be carried out in this order!

1. Introduction to DYLD

Dyld (The Dynamic Link Editor) is apple’s dynamic linker, which is an important part of Apple’s operating system. After the program preparation of the system kernel, DyLD is responsible for the remaining work. It is also open source, so anyone can download the source code on apple’s website to read how it works and learn the details of how the system loads the dynamic library.

2, DYLD source code

Apple official dyLD library download address

Here we choose the latest version for research, technology, always keep pace with The Times, after downloading these, don’t be urgent, first come to an official Video introduction of Apple, about DYLD2, to dyLD3 process updates, features, and then we will introduce the dyLD exploration process in the next chapter.

3. Apple’s official introduction video about DYLD

App Startup Time: Past, Present, and Future https://developer.apple.com/videos/play/wwdc2017/413/

Third, summary

Here we introduce the general process analysis of the application startup process, mainly including:

1. The concept and understanding of dynamic library and static library

2. Dyld concept and link process analysis

3. How do we know that the program entered dyld before main

The detailed execution process of DYLD will be explained separately in the next section.

Link: dyLD source code analysis for iOS app startup Process Analysis

🌺 more content look forward to sharing with you, if you like, click a like point attention, continue to create good content for you.