Introduction
Many third-party library developers work perfunctory, technical unclear (the great map, that is, you), do not know the symbol hidden, a bunch of other third-party library symbols exposed, resulting in the link to create a duplicate symbol. As shown
A map library has exported the entire LIBJEPG symbol list, knitting. What's more, if the user links to other versions of Libjpeg, the program will crash directly because the entry address is incorrect.
It is obvious that the problem can not be solved by the official, after all, the three large version (version 2.4 to 4.0) does not change at all, it is not understood at all. Ask for oneself, this article starts from this question, briefly introduces the use of several common binary files modification tools under Mac OS x system
Overview
As we all know, the code to the executable requires a compilation (compile) and link two main steps. Compiling is translating the program language into machine instructions, which is not covered in this article.
The link is to merge the block-compiled object file (obj) into a complete program that addresses the problem of function entry redirection. The main function is to put all the object files into a package, generate an export symbol table, so that other programs can know the structure of this file.
For the Apple system, the rich device contains a variety of architecture platforms, ARMV7, Arm64, i386, x86_64 is the most common instruction set, a library in order to be compatible with each platform, often to the different platform compiled programs, to generate so-called fat files (FAT file), In this way, developers do not have to prepare a real-machine repository and emulator repository (in the early days, many third-party libraries did provide different versions of binaries to reduce the size of the library file).
So for an executable, it actually contains a 3-tier structure: Universal Fat file, single Architecture Binary file, Mach-o Object.
The functions of Lipo, AR, NM, strip, LD and so on will be introduced in this paper to transform and modify the three-layer structure.
This article takes the example of the German map iOS SDK mamapkit.framework 4.0.3, which shows how to remove the exposed PNG symbols from this library file.
Tools Introduction Lipo
Lipo is a tool for managing FAT file, you can view a list of platforms, extract specific platforms, and repackage.
First run lipo-info mamapkit
As you can see, this file contains 4 platform code. All of the next operations are for a single platform, so extract the ARMv7 platform first,
Lipo-thin armv7 mamapkit-output mamapkit.armv7
You can see that the single platform program files are much smaller.
Now let's take a look at the symbol table for this file.
Nm
NM is used to display the symbol table for a package, which displays the entry address, symbol type, and symbol name by default.
nm-j mamapkit.armv7 | grep png > Symbols can get all the libpng export symbols and deposit to the symbols file to prepare for the next work. The-J option controls only the output symbol name.
Strip
Strip is used to remove the symbol table from the program. -R is used to specify a list of symbols to be deleted, using the symbols file generated above. Add the-s option to preserve the other symbols.
Strip-s-R Symbols Mamapkit.armv7-o MAMapKit.armv7.strip
You can verify that the new file that was generated has no PNG symbol.
We apply this method to all other platforms.
ar
We're having problems dealing with arm64 with the above method.
Some of the symbols in this file are used as redirected portals, and the strip command does not allow deletion, requiring a more powerful tool LD to debut. LD in fact the Apple system under the linker, you can more accurately control the export of symbol table.
The operating object of LD is obj, so we need to use the AR Packaging tool first. AR can view a list of object files in a package, extract the object files and repackage them.
ar-t mamapkit.arm64
You can see that the entire package contains a Master object file, Nani? It's swallowed Cluth.
Using- x to extract it, the next turn to LD to play.
Ld
The LD functionality required for this issue is basically consistent with the strip, using the following command
Ld-x-r-unexported_symbols_list Symbols Mamapkit-arm64-master.o-o Mamapkit-arm64-master.o.strip
This generates the object file, rejecting the PNG symbol table
The next thing to do is this inverse process, the object file is merged into a program file
Ar-r mamapkit.arm64 Mamapkit-arm64-master.o.strip pods-mamapkit-dummy.o
Finally, the program of each platform is packaged into FAT file.
Introduction to the use of Lipo, AR, NM and other tools under Mac system