Actually, this is a Da lian article

Before you read the article, you may want to read the following questions. If you know them, you may want to skip them.

  • Let’s talk about dynamic and static libraries.Do you really know XXXX and XXX series?
  • Why is using dynamic libraries for dynamic updates only available in House and Develop but not in the AppStore?
  • How many times will dynamic libraries be added to memory?

I am a preface

  • In fact, THIS article is going to be written tomorrow, but I called Sunny when I saw @, why did she help me to point out the problem in the middle of the night? I feel like SLEEPING should be put on hold.
  • For a big procrastinator like me, when it comes to “libraries” in iOS development (ii), updates are happening at an unprecedented rate. (I don’t have a collection, do I? Hahaha)
  • There are quite a few mistakes in this article that need to be fixed.
  • Of course, what needs to be thanked most is @Casa Taloyum who raised some questions in the article and patiently answered some of my little questions on the night I finished the first article.
  • Ok, without further ado, let’s get started.

Error correction article

Static library processing

  • For a static library, it is already compiled, like a.o collection (there is no link here, the link described in the iOS development “library” (1) is not correct). The build process involves only linking, which is simply merging, and the linker only merges the parts of the static library that are used into the executable. Compared with dynamic libraries, static libraries are much simpler to handle, as shown in the following figure:



Static library linking process


  • The linker puts all the global and unresolved symbols used in.o into a temporary table, and the global symbols are unrepeatable.
  • For static library.o, the linker ignores symbols that do not exist in the Unresolved Symbol table.
  • unresolved symbolsimilarextern int test();.h The statement?
  • Void test() {print(“test”)} –.m

  • Finally, the linker replaces the function reference with the actual address of the function.

Therefore, adding headers to executables as mentioned in “Libraries” (1) in iOS development is incorrect.

Dynamic library processing

  • First of all, dynamic libraries are actually differentDynamically linked libraryDynamically loaded libraryTwo. The fundamental difference between the two is load time.
    • Dynamically-linked libraries: When the executable is loaded, the dynamically-linked libraries are loaded into memory without being loaded. Share Libraries set up in Linked Framework and Libraries. 【 Start as the program starts 】
    • Dynamically loaded libraries: use dlopen, etc. to load them by code or command when needed. [After the program is started]
  • But no matter what kind of dynamic library, dynamic library is much trickier to deal with than static library. Because dynamic libraries are dynamic, you don’t know the exact address of a function in advance. So the dynamic linker has a lot of work to do when linking functions.

Because dynamic libraries have a lot of work to do in linking functions, static libraries have already implemented it. The reason “static libraries load a little faster” is because the compiler pre-implements the linking, not the static library implementation. – by Casa Taloyum) so static libraries load faster simply when nothing is loaded. Dynamic libraries can save startup time if the loaded share libraries do not need to be reloaded.

  • The Procedure Linkage Table (PLT) is used to implement this dynamic Linkage. First of all the PLT lists each function call, in the program when the program is running, if the dynamic libraries are loaded into memory, PLT will to find the address of the dynamic and recorded, if each function is called, the next call can jump directly by PLT, and are still a little difference between static library is, however, Each function call still needs to pass a PLT. This is why everything That Sunny said static links do is moved to runtime, making it slower.

The question of which area of memory the dynamic library resides in

  • In fact, this proposition went wrong at the very beginning. After unsuccessful discussion with @cool Aidian and other friends, the old driver @Casa Taloyum pointed out what the problem was.
  • First of all, the difference between a static library and a dynamic library does not matter which area of memory it is in. The essential difference is that a function call is determined at compile time, while a dynamic library is loaded dynamically. In other words, static libraries change things and the whole program needs to be recompiled, whereas dynamic libraries change things and just restart the app (resetting the PLT).
  • In which area of memory, it doesn’t matter if the library is static or dynamic. Code snippet, data snippet, all of that comes in when the program loads. The heap is used for file buffer allocation, object initialization, and so on. The stack is a function entry/exit pointer used locally for general variables. As long as Malloc is in the heap. Specific can refer to here
  • It should also be mentioned that if the library is dynamically loaded, the code and data segments will not be loaded when not loaded. Lazy load.

The use of a

A is a pure binary file, which cannot be used directly. It needs to be used together with header files and resource files.

  • Maybe he didn’t understand what I meant when I said resource files, like images. And he said you can link to a file without using a header file. A, well, I don’t know, but for general use, using.A requires a header file and a resource file, so it’s easier to use the framework.

Whether the simulator architecture needs to be removed for dynamic framework

  • First of all, there is no problem with this theory, but I have encountered this pit, and this problem is also mentioned here, so it is best to remove it under the condition of insurance. (Feels like an Apple bug)

Expand the article

Dynamic library dynamic update problem

Is it possible to update the AppStore version dynamically by means of dynamic library?

  • I was going to try out the AppStore for National Day, but thanks to @casa Taloyum, he’s already been there, and his verdict is: Using dynamic libraries for dynamic updates only works in In House and Develop mode, but not in the AppStore.
  • When uploading and packaging, Apple will perform Code Singing on our Code, including app executable files and all Embedded dynamic libraries. Therefore, as long as you modify the code of a dynamic library and re-sign it, the MD5 hash value will be different. When loading the dynamic library, Apple will check the hash value. When Apple detects that the dynamic library is illegal, it will cause Crash.

So it’s also not true that “library” (I) in iOS development refers to something that works in theory.

How many times does the dynamic library add problems

App1 and APP2 both have the same dynamic framework Embedded in their respective apps. How many times will this dynamic framework be loaded?

  • Of course, not once, but twice. The system will only load dynamic libraries with the same path once.



Dynamic library loading


About… Okay, I’m gonna go to bed. I have more questions. I’ll write it tomorrow. Hope to write this article live up to sunny and Casa old driver’s advice.

Set pieces

Excerpts from personal notes.

About the big five partitions of memory

  • BSS:

    • BSS段( bss segment) usually means to store in a programUninitialized global and static variablesAn area of memory.
    • One thing to note here: most of the books say that global variables and static variables are automatically initialized, so where is the uninitialized variable? Variables can be explicitly initialized and implicitly initialized. Global variables and static variables can be initialized if the programmer does not initialize them themselves. That is, they are initialized to 0 regardless of type.Since they are all zeros, there is no need to store every one of them to save disk space, which is the main purpose of BSS
    • BSS is short for Block Started by Symbol. The BSS segment is a static memory allocation. The BSS section does not contain any data, just a simple maintenance of the start and end addresses, that is, the total size. So that the memory area can be allocated at run time and effectively cleared. The BSS section does not exist in the binary image file of the application, that is, it takes up no disk space but only memory space at runtime.So if global and static variables are not initialized then their executables are much smaller.
  • The data segment (data segment)

    • Usually refers to the storage of a programInitialized global and static variablesAn area of memory. Data segments belong to static memory allocation and can be divided intoRead-only data segmentandRead/write data segment. String constants and so on, but are generally placed in read-only data segments.
  • Code snippet (code segment/text segment)

    • It usually means for storageAn area of memory in which a program executes code. The size of this area is determined before the program runs, and memory areas are usually read-only. Some architectures also allow code segments to be writable, that is, to modify the program. It is possible to include read-only constant variables in a code segment, such as string constants, but these are usually placed in a read-only data segment.
  • Heap (heap)

    • The heap is used to hold the process running byDynamically allocated memory segments, its size is not fixed, can expand or shrink dynamically. When a process calls a function such as malloc to allocate memory, the newly allocated memory is dynamically added to the heap (the heap expands); When memory is freed using functions such as free, the freed memory is removed from the heap (the heap is reduced)
  • Stack (stack heap)

    • Stack, also known as stack, isThe user stores local variables temporarily created by the programBracket, that is to say we function variables defined in “{}” (but not including the static statement variables, static meansData segmentWhere variables are stored. In addition, when a function is called, its parameters are pushed onto the stack of the process that initiated the call, and the return value of the function is pushed back onto the stack when the call ends. Because of the first-in, first-out nature of the stack, it is particularly convenient to save/resume the call scene. In this sense, we can think of the stack as an area of memory where temporary data is stored and exchanged.



Memory partition


Small Tips

  • Variables in the stack area do not require programmer management
  • The heap area needs to be managed by programmers
  • In iOS, the memory of the heap is shared by all applications
  • The system uses a table-level structure to allocate memory space, so logical and physical addresses may be different

reference

  • IOS Application architecture talks about local persistence solutions and dynamic deployment
  • library
  • Dissecting shared libraries

  • The level is limited, if there is a mistake, hope to correct more! [email protected]