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 map
The order of the signs in it,xcode
Provided 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 setting
Set up theorder
The file path
Add method in project, editqinhan.order
file
Recompile the project and look againlink map
file
foundorder
This 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-guard
Command, 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
,stop
And what it stores is actually the number of symbols. Read about thestart
Stored 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 zero0x11
That is17
If 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 functiondli_saddr
: By address or de-sign information, saved toDl_info
Structure of the topic
Print result:
dli_fname
: Path of machodli_fbase
: Base address of Machodli_sname
: symbolic namedli_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: – OSQueueHead
Since 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 executesOSAtomicEnqueue
: queues all structures.
Extraction of symbols:
- through
while
Circulation +OSAtomicDequeue
Take 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 totouchesBegan
Join the queue, while looptouchesBegan
They 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 notoc
If it isc
You 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.