Symbols是什么東西呢?雖然我對(duì)它沒有深入的了解,但是大概知道它的作用。摘抄《深入理解計(jì)算機(jī)系統(tǒng)》里的一些描述:
一個(gè)典型的ELF可重定位目標(biāo)文件包含下面幾個(gè)節(jié):
... ...
.symtab:一個(gè)符號(hào)表,它存放在程序中定義和引用的函數(shù)和全局變量信息。一些程序員錯(cuò)誤地認(rèn)為必須通過-g選項(xiàng)來編譯程序才能得到符號(hào)表信息。實(shí)際上,每個(gè)可重定位目標(biāo)文件在.symtab中都有一張符號(hào)表。然而,和編譯器中的符號(hào)表不同,.symtab符號(hào)表不包含局部變量的條目。
... ...
.debug:一個(gè)調(diào)試符號(hào)表,其條目是程序中定義的局部變量和類型定義,程序中定義和引用的全局變量,以及原始的C源文件。只有以-g選項(xiàng)調(diào)用編譯驅(qū)動(dòng)程序時(shí)才會(huì)得到這張表。
... ...
為了構(gòu)造可執(zhí)行文件,鏈接器必須完成兩個(gè)主要任務(wù):
符號(hào)解析(symbol resolution)。目標(biāo)文件定義和引用符號(hào)。符號(hào)解析的目的是將每個(gè)符號(hào)引用剛好和一個(gè)符號(hào)定義聯(lián)系起來。
重定位(relocation)。編譯器和匯編器生成從地址0開始的代碼和數(shù)據(jù)節(jié)。鏈接器通過把每個(gè)符號(hào)定義與一個(gè)存儲(chǔ)器位置聯(lián)系起來,然后修改所有對(duì)這些符號(hào)的引用,使得它們指向這個(gè)存儲(chǔ)器位置,從而重定位這些節(jié)。
Objective-C有一些自己的生成符號(hào)的規(guī)則,比如文檔中有提到:
The dynamic nature of Objective-C complicates things slightly. Because the code that implements a method is not determined until the method is actually called, Objective-C does not define linker symbols for methods. Linker symbols are only defined for classes.
Objective-C不會(huì)為方法定義鏈接符號(hào),只會(huì)為類定義鏈接符號(hào)。
可以在終端中用nm命令查看一個(gè)可重定位文件或可執(zhí)行文件的符號(hào)表,其中加上-a參數(shù)可以顯示包括調(diào)試符號(hào)在內(nèi)的所有符號(hào)。
合理的選擇與symbols有關(guān)的設(shè)置選項(xiàng),可以縮減app的大小,一定程度上能阻礙與源代碼有關(guān)的信息被攻擊者獲得。Xcode的build setting中,有不少與symbols有關(guān),現(xiàn)在我來依次試驗(yàn)這幾個(gè)設(shè)置選項(xiàng),了解一下它們的具體作用。
剛開始的時(shí)候,我使用Xcode7.2.1新建了一個(gè)工程,以下試驗(yàn)均在run和DEBUG模式下進(jìn)行。
Generate Debug Symbols [GCC_GENERATE_DEBUGGING_SYMBOLS]
在Xcode7.2.1中,Generate Debug Symbols這個(gè)設(shè)置在DEBUG和RELEASE下均默認(rèn)為YES。
官方文檔對(duì)這個(gè)設(shè)置的說明:
Enables or disables generation of debug symbols.? When debug symbols are enabled, the level of detail can be controlled by the build 'Level of Debug Symbols' setting.
調(diào)試符號(hào)是在編譯時(shí)生成的。在Xcode中查看構(gòu)建過程,可以發(fā)現(xiàn),當(dāng)Generate Debug Symbols選項(xiàng)設(shè)置為YES時(shí),每個(gè)源文件在編譯成.o文件時(shí),編譯參數(shù)多了-g和-gmodules兩項(xiàng)。但鏈接等其他的過程沒有變化。
Clang文檔對(duì)-g的描述是:
Generate complete debug info.
當(dāng)Generate Debug Symbols設(shè)置為YES時(shí),編譯產(chǎn)生的.o文件會(huì)大一些,當(dāng)然最終生成的可執(zhí)行文件也大一些。
當(dāng)Generate Debug Symbols設(shè)置為NO的時(shí)候,在Xcode中設(shè)置的斷點(diǎn)不會(huì)中斷。但是在程序中打印[NSThread callStackSymbols],依然可以看到類名和方法名,比如:
** 0 XSQSymbolsDemo ???????????????????????????????? 0x00000001000667f4 -[ViewController viewDidLoad] + 100**
在程序崩潰時(shí),也可以得到帶有類名和方法名的函數(shù)調(diào)用棧

現(xiàn)在把Generate Debug Symbols設(shè)置回YES,開始試驗(yàn)下一個(gè)設(shè)置。
Debug Information Level [CLANG_DEBUG_INFORMATION_LEVEL]
在Xcode 7.2.1中,Debug Information Level的默認(rèn)值為Compiler default,還有一個(gè)選項(xiàng)是Line tables only。
官方文檔的描述是:
Toggles the amount of debug information emitted when debug symbols are enabled.? This can impact the size of the generated debug information, which can matter in some cases for large projects (such as when using LTO).
當(dāng)我把Debug Information Level設(shè)置為Line tables only的時(shí)候,然后構(gòu)建app,每個(gè)源文件在編譯時(shí),都多了一個(gè)編譯參數(shù):-gline-tables-only
Clang的文檔中這樣解釋-gline-tables-only:
Generate line number tables only.
This kind of debug info allows to obtain stack traces with function names, file names and line numbers (by such tools as gdb or addr2line). It doesn’t contain any other data (e.g. description of local variables or function parameters).
這種類型的調(diào)試信息允許獲得帶有函數(shù)名、文件名和行號(hào)的函數(shù)調(diào)用棧,但是不包含其他數(shù)據(jù)(比如局部變量和函數(shù)參數(shù))。
所以當(dāng)Debug Information Level設(shè)置為Line tables only的時(shí)候,斷點(diǎn)依然會(huì)中斷,但是無法在調(diào)試器中查看局部變量的值:

現(xiàn)在把Debug Information Level設(shè)置回Compiler default,然后試驗(yàn)下一個(gè)設(shè)置。
Strip Linked Product [STRIP_INSTALLED_PRODUCT]
在Xcode7.2.1中,Strip Linked Product在DEBUG和RELEASE下均默認(rèn)為YES。
這是一個(gè)讓我困惑了很久的設(shè)置選項(xiàng)。當(dāng)我把這一設(shè)置選項(xiàng)改為NO的時(shí)候,最終構(gòu)建生成的app大小沒有任何變化,這讓我覺得很奇怪。
原來,Strip Linked Product也受到Deployment Postprocessing設(shè)置選項(xiàng)的影響。在Build Settings中,我們可以看到,Strip Linked Product是在Deployment這欄中的,而Deployment Postprocessing相當(dāng)于是Deployment的總開關(guān)。
Xcode7.2.1中,Deployment Postprocessing在DEBUG和RELEASE下均默認(rèn)為NO。
現(xiàn)在我們把Deployment Postprocessing設(shè)置為YES,對(duì)比Strip Linked Product設(shè)為YES和NO的這兩種情況,發(fā)現(xiàn)當(dāng)Strip Linked Product設(shè)為YES的時(shí)候,app的構(gòu)建過程多了這樣兩步:
在app構(gòu)建的開始,會(huì)生成一些.hmap輔助文件;(為什么會(huì)多出這一步我好像還不太清楚)
在app構(gòu)建的末尾,會(huì)執(zhí)行Strip操作。

當(dāng)Strip Linked Product設(shè)為YES的時(shí)候,運(yùn)行app,斷點(diǎn)不會(huì)中斷,在程序中打印[NSThread callStackSymbols]也無法看到類名和方法名:
** 0? XSQSymbolsDemo? ? ? ? ? ? ? ? ? ? ? 0x000000010001a7f4 XSQSymbolsDemo + 26612**
而在程序崩潰時(shí),函數(shù)調(diào)用棧中也無法看到類名和方法名,注意右上角變成了unnamed_function:

繼續(xù)保持Strip Linked Product和Deployment Postprocessing為YES,下面來看看Strip Style設(shè)置選項(xiàng)。
Strip Style [STRIP_STYLE]
在Xcode7.2.1中,Strip Style在DEBUG和RELEASE下均默認(rèn)All Symbols。
官方文檔中對(duì)Strip Style的描述:
Defines the level of symbol stripping to be performed on the linked product of the build.? The default value is defined by the target's product type. [STRIP_STYLE]
All Symbols - Completely strips the binary, removing the symbol table and relocation information. [all, -s]
Non-Global Symbols - Strips non-global symbols, but saves external symbols. [non-global, -x]
Debugging Symbols - Strips debugging symbols, but saves local and global symbols. [debugging, -S]
選擇不同的Strip Style時(shí),app構(gòu)建末尾的Strip操作會(huì)被帶上對(duì)應(yīng)的參數(shù)。
如果選擇debugging symbols的話,函數(shù)調(diào)用棧中,類名和方法名還是可以看到的。

如果我們構(gòu)建的不是一個(gè)app,而是一個(gè)靜態(tài)庫,需要注意,靜態(tài)庫是不可以strip all的。這時(shí)構(gòu)建會(huì)失敗。想想符號(hào)在重定位時(shí)的作用,如果構(gòu)建的靜態(tài)庫真的能剝離所有符號(hào),那么它也就沒法被鏈接了。
現(xiàn)在我們保持Deployment Postprocessing為YES,Strip Linked Product改回NO,Strip Style改回All Symbols,接下來看下一個(gè)設(shè)置。
Strip Debug Symbols During Copy [COPY_PHASE_STRIP]
網(wǎng)上有很多文章,以為Strip Debug Symbols During Copy開啟的時(shí)候,app中的調(diào)試符號(hào)會(huì)被剝離掉。我感覺他們混淆了Strip Linked Product和Strip Debug Symbols During Copy的用法。
文檔上的描述是:
Activating this setting causes binary files which are copied during the build (e.g., in a Copy Bundle Resources or Copy Files build phase) to be stripped of debugging symbols.? It does not cause the linked product of a target to be stripped (use Strip Linked Product for that).
Strip Debug Symbols During Copy中的During Copy是什么意思呢?我覺得可能是app中引入的某些類型的庫,在app的構(gòu)建過程中需要被復(fù)制一次。雖然我暫時(shí)沒找全究竟什么樣的“庫”需要在app構(gòu)建時(shí)被復(fù)制,但是我發(fā)現(xiàn),當(dāng)app中包含extension或者watch app的時(shí)候,構(gòu)建過程中會(huì)有Copy的步驟:

當(dāng)我將app(而非extension)的Strip Debug Symbols During Copy設(shè)置為YES之后,在這句copy的命令中會(huì)多出-strip-debug-symbols參數(shù)。
但是這里,strip并不能成功,并且出現(xiàn)了warning:
warning: skipping copy phase strip, binary is code signed: /Users/xsq/Library/Developer/Xcode/DerivedData/XSQSymbolsDemo-cysszdsykroyyddkvvyffgboglvo/Build/Products/Debug-iphoneos/Today.appex/Today
這似乎是由于app中的today extention已經(jīng)經(jīng)過了code sign,導(dǎo)致無法被篡改引起的警告。
那么如果略過code sign的過程,是否就能成功strip呢?我想使用模擬器調(diào)試可以略過code sign過程,于是便在模擬器上試了試。果然這個(gè)warning消失了。
Strip Debug Symbols During Copy設(shè)置為YES時(shí),打開對(duì)應(yīng).app文件的“顯式包內(nèi)容”,可以看到,/PlugIns/Today.appex文件的大小變小了。(不過這些只能在使用模擬器時(shí)奏效)
Strip Debug Symbols During Copy置為YES的時(shí)候,today extension中的斷點(diǎn)將不會(huì)中斷,但是打印[NSThread callStackSymbols]時(shí)的類名和方法名還是可以看見的。
現(xiàn)在我們把Strip Debug Symbols During Copy設(shè)置回NO,來看看下一個(gè)設(shè)置。
Debug Information Format [DEBUG_INFORMATION_FORMAT]
Xcode7.2.1中,Debug Information Format在DEBUG下默認(rèn)為DWARF,在RELEASE下默認(rèn)為DWARF with dSYM File。
官方文檔的解釋是:
This setting controls the format of debug information used by the developer tools. [DEBUG_INFORMATION_FORMAT]
DWARF - Object files and linked products will use DWARF as the debug information format. [dwarf]
DWARF with dSYM File - Object files and linked products will use DWARF as the debug information format, and Xcode will also produce a dSYM file containing the debug information from the individual object files (except that a dSYM file is not needed and will not be created for static library or object file products). [dwarf-with-dsym]
當(dāng)Debug Information Format為DWARF with dSYM File的時(shí)候,構(gòu)建過程中多了一步Generate dSYM File:

最終產(chǎn)出的文件也多了一個(gè)dSYM文件。
不過,既然這個(gè)設(shè)置叫做Debug Information Format,所以首先得有調(diào)試信息。如果此時(shí)Generate Debug Symbols選擇的是NO的話,是沒法產(chǎn)出dSYM文件的。
dSYM文件的生成,是在Strip等命令執(zhí)行之前。所以無論Strip Linked Product是否開啟,生成的dSYM文件都不會(huì)受影響。
不過正如文檔中所說,無法為靜態(tài)庫生成dSYM文件。即便為給一個(gè)靜態(tài)庫的Debug Information Format設(shè)置為DWARF with dSYM File,構(gòu)建過程中依然不會(huì)有生成dSYM文件的步驟。
一種配置方案
了解了每個(gè)設(shè)置的意思,個(gè)人覺得對(duì)于一個(gè)普通的app來說可以這樣配置這些設(shè)置:
Generate Debug Symbols:DEBUG和RELEASE下均設(shè)為YES(和Xcode默認(rèn)一致);
Debug Information Level:DEBUG和RELEASE下均設(shè)為Compiler default(和Xcode默認(rèn)一致);
Deployment Postprocessing:DEBUG下設(shè)為NO,RELEASE下設(shè)為YES,這樣RELEASE模式下就可以去除符號(hào)縮減app的大小(但是似乎設(shè)置為YES后,會(huì)牽涉一些和bitcode有關(guān)的設(shè)置,對(duì)于bitcode暫時(shí)還不太了解(′?_?`));
Strip Linked Product:DEBUG下設(shè)為NO,RELEASE下設(shè)為YES,用于RELEASE模式下縮減app的大??;
Strip Style:DEBUG和RELEASE下均設(shè)為All Symbols(和Xcode默認(rèn)一致);
Strip Debug Symbols During Copy:DEBUG下設(shè)為NO,RELEASE下設(shè)為YES;
Debug Information Format:DEBUG下設(shè)為DWARF,RELEASE下設(shè)為DWARF with dSYM File,dSYM文件需要用于符號(hào)化crash log(和Xcode默認(rèn)一致);