preface

The last article focused on iOS startup optimization and saw that binary rearrangement can optimize a certain amount of startup time. This blog is mainly about the practical operation of binary rearrangement.

Link Map File

A link map file can be directly understood as a link map file, which is a text generated when Xcode generates an executable file to record link information. Save executable file path, CPU architecture,.o object path, method symbol.

  • A link map file is generated

Xcode ->Build setting, search link map. Locate the Write Link Map and set it to Yes. Compile the project and find the corresponding file

Open link map file:

So if we’re going to do binary rearrangement, how do we change itlink mapThe order of the signs in it,xcodeProvided us with oneorder file, developers can customize oneorder file

order file

Create a new qinhan.order file in the root directory of the Demo project

xcode->build settingSet up theorderThe file path

Add method in project, editqinhan.orderfile

Recompile the project and look againlink mapfile

foundorderThis setting is valid.

Since setting up the order file does have the effect of binary rearrangement, we are almost halfway there. But how do you know which methods are called when the application starts, and when clang pegs are needed

Clang plugging pile

Clang document

Clang piling is configured

Tracing PCs with guards

According to the document: Pass-fsanitize-coverage=trace-pc-guardCommand, the compiler will insert at each code boundary__sanitizer_cov_trace_pc_guard(&guard_variable)Callback method.

Xcode ->build setting search for Other C Flag, enter the command above

Compiling project, error reported

In fact, the callback function is not added; the documentation also provides Example. Add these two functions to the project and it runs successfully.

  • __sanitizer_cov_trace_pc_guard_init

herestart,stopAnd what it stores is actually the number of symbols. Read about thestartStored data, in units of 4 bytes

So what if I get the total number of signs, which is thetastop -4.

The number of symbols in this case is zero0x11That is17If we add another function, run it again.

The results of0x12 = 18, again verified our conclusion

  • __sanitizer_cov_trace_pc_guard

When you break the point in this function, you will find that every function applied will use this callback method, which is equivalent to applying all functions.

How it works: Once the Clang peg tag is added, the compiler adds the above callback method to the edge of the code implementation of all methods, functions, and blocks.

To obtain symbol

  • __builtin_return_address: gets the address of the previous function
  • dli_saddr: By address or de-sign information, saved toDl_infoStructure of the topic

Print result:

  • dli_fname: Path of macho
  • dli_fbase: Base address of Macho
  • dli_sname : symbolic name
  • dli_saddr : symbolic address

Summary: Since we can get the name of the startup symbols, if we can save the symbols in order as an order file, we can solve the binary rearrangement problem!!!!

Accessing symbols through queues

Saving of symbols: OSQueueHeadSince methods can also run on child threads, to ensure thread-safety, define an atomic queuesymbolList

  • QHNode: defines a structure to hold the symbol’s address, and next executes
  • OSAtomicEnqueue: queues all structures.

Extraction of symbols:

  • throughwhileCirculation +OSAtomicDequeueTake out the symbols

Pothole analysis and resolution

When I got here, I was full of joy. As soon as the result runs, it keeps printing in a loop

Why is that? The main reason is that the intercepted callback somehow intercepts methods, functions,block, is also intercepted inside the while loop. Single execution totouchesBeganJoin the queue, while looptouchesBeganThey join the queue. To solve this problem, I need to set up the compile only method in Xcode-fsanitize-coverage=func,trace-pc-guard

Symbol stitching

Determine whether or notocIf it iscYou need to do something else

Filter the sum of repeated symbols

The following operation is relatively simple, mainly to get the symbol for filtering repeated symbols and take the reverse. Remove the ‘touchesBegan’ himself

Finally, save the data as an ORDER file, and then place the ORDER file in the corresponding directory. At this point the whole binary rearrangement operation is complete.