iOS性能優化-包大小+linkMap分析和查找未使用類和方法

1. LinkMap解析

導讀
IOS在做包大小優化的時候,需要分析包大小組成,然后通過包大小組成來有針對的做優化。其中最主要的工具就是linkmap文件的解析,下面文章講簡單說明如何解析linkmap文件。

1.1 如何生成linkMap文件

  1. Xcode開啟編譯選項Write Link Map File

    XCode -> Project -> Build Settings -> 搜map -> 把Write Link Map File選項設為yes,并指定好linkMap的存儲位置

  2. 編譯后,到編譯目錄里找到該txt文件,文件路徑就是上面設定的路徑,我的位于:

    ~/Library/Developer/Xcode/DerivedData/FFProject-gdxobffdqcwvyleustpwgfdxslqp/Build/Intermediates/FFProject.build/Debug-iphonesimulator/FFProject.build

1.2 linkMap文件結構解析

1.2.1. 基礎信息

# Path: /Users/bolei/Library/Developer/Xcode/DerivedData/FFProject-gdxobffdqcwvyleustpwgfdxslqp/Build/Products/Debug-iphonesimulator/FFProject.app/FFProject //路徑
# Arch: x86_64 //架構

1.2.2.類表

# Object files: //類文件
[  0] linker synthesized
[  1] dtrace
[  2] /Users/bolei/Library/Developer/Xcode/DerivedData/FFProject-gdxobffdqcwvyleustpwgfdxslqp/Build/Intermediates/FFProject.build/Debug-iphonesimulator/FFProject.build/Objects-normal/x86_64/PAFFConfig.o

這里保存了所有用到的類生成的.o文件,也包括用到的dylib庫。前面[num]是序號,類是按照順序保存的,后續可以通過序號查到具體對應的哪個類。

1.2.3.段表

# Sections:
# Address   Size        Segment Section
0x100002460 0x00E382DF  __TEXT  __text
0x100E3A740 0x000019D4  __TEXT  __stubs
0x100E3C114 0x0000273E  __TEXT  __stub_helper
0x100E3E860 0x0009D78B  __TEXT  __cstring
0x100EDBFEB 0x00089F7A  __TEXT  __objc_methname
0x100F65F65 0x0000CFCD  __TEXT  __objc_classname
0x100F72F32 0x00012A27  __TEXT  __objc_methtype
0x100F8595A 0x000122E8  __TEXT  __ustring
0x100F97C44 0x00067DA8  __TEXT  __gcc_except_tab
0x100FFF9F0 0x000259C8  __TEXT  __const
0x1010253B8 0x0000017C  __TEXT  __entitlements
0x101025534 0x0000037B  __TEXT  __dof_RACSignal
0x1010258AF 0x000002E8  __TEXT  __dof_RACCompou
0x101025B98 0x00016928  __TEXT  __unwind_info
0x10103C4C0 0x00013B40  __TEXT  __eh_frame
0x101050000 0x00000010  __DATA  __nl_symbol_ptr
0x101050010 0x00000D30  __DATA  __got
0x101050D40 0x00002270  __DATA  __la_symbol_ptr
0x101052FB0 0x00000030  __DATA  __mod_init_func
0x101052FE0 0x00036580  __DATA  __const
0x101089560 0x0005EB20  __DATA  __cfstring
0x1010E8080 0x000040A8  __DATA  __objc_classlist
0x1010EC128 0x00000448  __DATA  __objc_nlclslist
0x1010EC570 0x00000AA8  __DATA  __objc_catlist
0x1010ED018 0x00000048  __DATA  __objc_nlcatlist
0x1010ED060 0x00000780  __DATA  __objc_protolist
0x1010ED7E0 0x00000008  __DATA  __objc_imageinfo
0x1010ED7E8 0x001A2B80  __DATA  __objc_const
0x101290368 0x00020CE8  __DATA  __objc_selrefs
0x1012B1050 0x00000168  __DATA  __objc_protorefs
0x1012B11B8 0x00003B80  __DATA  __objc_classrefs
0x1012B4D38 0x00002620  __DATA  __objc_superrefs
0x1012B7358 0x00010AF0  __DATA  __objc_ivar
0x1012C7E48 0x000286E0  __DATA  __objc_data
0x1012F0530 0x0000BB48  __DATA  __data
0x1012FC080 0x00011A40  __DATA  __bss
0x10130DAC0 0x00000538  __DATA  __common

接下來是段表,描述了不同功能的數據保存的地址,通過這個地址就可以查到對應內存里存儲的是什么數據。

其中第一列是起始地址,第二列是段占用的大小,第三個是段類型,第四列是段名稱,每一行初始地址 = 上一行的初始地址+占用大小

其中:

__TEXT 表示代碼段,用于執行,可讀不可以寫,可以被執行

__DATA 表示數據段,用于存儲數據,可以讀寫,不可以執行

其中:

第一個段是__PAGEZERO 地址從0到0x100000000,程序保留字段。

1.2.3.1 段表內容含義

__TEXT段節名含義

1. __text: 代碼節,存放機器編譯后的代碼
2. __stubs: 用于輔助做動態鏈接代碼(dyld).
3. __stub_helper:用于輔助做動態鏈接(dyld).
4. __objc_methname:objc的方法名稱
5. __cstring:代碼運行中包含的字符串常量,比如代碼中定義`#define kGeTuiPushAESKey        @"DWE2#@e2!"`,那DWE2#@e2!會存在這個區里。
6. __objc_classname:objc類名
7. __objc_methtype:objc方法類型
8. __ustring:
9. __gcc_except_tab:
10. __const:存儲const修飾的常量
11. __dof_RACSignal:
12. __dof_RACCompou:
13. __unwind_info:

__DATA段節名含義

1. __got:存儲引用符號的實際地址,類似于動態符號表
2. __la_symbol_ptr:lazy symbol pointers。懶加載的函數指針地址。和__stubs和stub_helper配合使用。具體原理暫留。
3. __mod_init_func:模塊初始化的方法。
4. __const:存儲constant常量的數據。比如使用extern導出的const修飾的常量。
5. __cfstring:使用Core Foundation字符串
6. __objc_classlist:objc類列表,保存類信息,映射了__objc_data的地址
7. __objc_nlclslist:Objective-C 的 +load 函數列表,通常比 __mod_init_func 更早執行。具體可以
8. __objc_catlist: categories
9. __objc_nlcatlist:Objective-C 的categories的 +load函數列表。
10. __objc_protolist:objc協議列表
11. __objc_imageinfo:objc鏡像信息
12. __objc_const:objc常量。保存objc_classdata結構體數據。用于映射類相關數據的地址,比如類名,方法名等。
13. __objc_selrefs:引用到的objc方法
14. __objc_protorefs:引用到的objc協議
15. __objc_classrefs:引用到的objc類
16. __objc_superrefs:objc超類引用
17. __objc_ivar:objc ivar指針,存儲屬性。
18. __objc_data:objc的數據。用于保存類需要的數據。最主要的內容是映射__objc_const地址,用于找到類的相關數據。
19. __data:暫時沒理解,從日志看存放了協議和一些固定了地址(已經初始化)的靜態量。
20. __bss:存儲未初始化的靜態量。比如:`static NSThread *_networkRequestThread = nil;`其中這里面的size表示應用運行占用的內存,不是實際的占用空間。所以計算大小的時候應該去掉這部分數據。
21. __common:存儲導出的全局的數據。類似于static,但是沒有用static修飾。比如KSCrash里面`NSDictionary* g_registerOrders;`, g_registerOrders就存儲在__common里面

1.2.4 后續符號表內容

1.2.4.1 代碼節

# Symbols:
# Address   Size        File  Name
0x100002460 0x00000080  [  2] +[PAFFConfig instance]
0x1000024E0 0x00000050  [  2] ___22+[PAFFConfig instance]_block_invoke
0x100002530 0x00000090  [  2] -[PAFFConfig init]
apiType]

這里面保存里類里面的方法內存情況。其中

  1. 第一列是起始地址位置,通過這個地址我們可以查上面的段表,可以知道,對應的節為__text
  2. 第二列是大小,通過這個可以算出方法占用的大小。
  3. 第三列是歸屬的類(.o文件),這里序號是2,通過查類表可以知道對應的類是PAFFConfig。

通過這部分我們可以分析出來每個類對應的方法的大小是多少。

1.2.4.2 方法名節(__objc_methname)

0x100EDBFEB 0x00000006  [  2] literal string: alloc
0x100EDBFF1 0x00000005  [  2] literal string: init
0x100EDBFF6 0x0000000B  [  2] literal string: mainBundle
0x100EDC001 0x0000000F  [  2] literal string: infoDictionary
0x100EDC010 0x0000000E  [  2] literal string: objectForKey:
0x100EDC01E 0x0000000C  [  2] literal string: setAppName:
0x100EDC02A 0x0000000C  [  2] literal string: setVersion:
0x100EDC036 0x0000000C  [  2] literal string: setApiType:
0x100EDC042 0x00000009  [  2] literal string: instance
0x100EDC04B 0x00000008  [  2] literal string: isDebug

這部分保存了類里方法的字符串信息(所以原則上方法名起短一些,是可以減少占用的 - -!)

分析步驟:

  1. 查看第一列起始地址,然后在上面的段表中查看這個地址在那個節里,可以看到在__objc_methname中。
  2. 通過第二列對比大小
  3. 通過第三列解析對應的類和對應方法名稱

1.2.4.3類列表節(__objc_classlist

0x1010E8080 0x00000008  [  2] anon
0x1010E8088 0x00000008  [  3] anon
0x1010E8090 0x00000008  [  4] anon
0x1010E8098 0x00000008  [  5] anon
0x1010E80A0 0x00000008  [  7] anon
0x1010E80A8 0x00000008  [  9] anon
0x1010E80B0 0x00000008  [ 10] anon
0x1010E80B8 0x00000008  [ 11] anon
0x1010E80C0 0x00000008  [ 12] anon
0x1010E80C8 0x00000008  [ 13] anon
0x1010E80D0 0x00000008  [ 14] anon
0x1010E80D8 0x00000008  [ 15] anon
0x1010E80E0 0x00000008  [ 16] anon
0x1010E80E8 0x00000008  [ 17] anon
0x1010E80F0 0x00000008  [ 18] anon
0x1010E80F8 0x00000008  [ 19] anon
0x1010E8100 0x00000038  [ 20] anon
0x1010E8138 0x00000030  [ 21] anon

__objc_classlist存儲了所有類的虛擬地址。即__objc_data地址。這里都是二進制數據,具體保存了什么,看下對應的數據結構

__objc_data的數據結構為:

typedef struct objc_class{
        unsigned long long isa;
        unsigned long long wuperclass;
        unsigned long long cache;
        unsigned long long vtable;
        unsigned long long data;
        unsigned long long reserved1;
        unsigned long long reserved2;
        unsigned long long reserved3;
}objc_class;

其中最主要的是data字段,保存了_objc_const節對應的數據地址。數據結構為:

typedef struct objc_classdata{
    long long flags;
    long long instanceStart;
    long long instanceSize;
    long long reserved;
    unsigned long long ivarlayout;
    unsigned long long name;
    unsigned long long baseMethod;
    unsigned long long baseProtocol;
    unsigned long long ivars;
    unsigned long long weakIvarLayout;
    unsigned long long baseProperties;
}

這里面保存了類名,方法名,協議名,ivar指針和屬性對應的地址。最后對應到相應的TEXT段里就能找到。比如類名在__objc_classname可以找到,方法名可以在__objc_methname。應用程序就是通過這個結構來尋找哪個類對應的那個方法,從而執行相關邏輯

1.3 如何找到沒有用到的類和方法?

我們可以利用linkmapotools結合來找到,具體看下面isee的使用

  • 找到哪些類沒有使用

    通過__objc_classrefs和link map中解析到的所有_objc_classname對比就可以知道哪些類沒用。其中__objc_classrefs的解析需要通過otool命令才能解析,來找到使用到的class。可以用以下命令獲取到所有解析成class對象后的數據

    otool -V -o FFProject -arch arm64 | open -f
    

    輸出的數據中找到Contents of (__DATA,__objc_classrefs) section部分。

    Contents of (__DATA,__objc_classrefs) section
    000000010003cc90 0x10003d348 _OBJC_CLASS_$_AFHTTPSessionManager
    000000010003cc98 0x0 _OBJC_CLASS_$_NSDictionary
    000000010003cca0 0x0 _OBJC_CLASS_$_UISceneConfiguration
    000000010003cca8 0x10003d230 _OBJC_CLASS_$_AppDelegate
    000000010003ccb0 0x0 _OBJC_CLASS_$_NSDate
    000000010003ccb8 0x0 _OBJC_CLASS_$_NSString
    000000010003ccc0 0x0 _OBJC_CLASS_$_NSMutableDictionary
    000000010003ccc8 0x0 _OBJC_CLASS_$_NSUUID
    
  • 找到哪些方法沒有使用:

    通過__objc_selrefs_objc_methname對比可以知道哪些方法沒有使用到。其中__objc_selrefs需要用otool命令才能解析,找到使用到的所有方法。

    otool -v -s __DATA __objc_selrefs <path>
    

1.2.4 otool使用

這個用來做反匯編的,比如分析哪些類被使用了,需要用這個工具。

比如獲取使用到的方法可以用這個命令:

otool -V -s __DATA __objc_selrefs <path> -arch arm64 | open -f

其中path是你的應用編譯后生成的可執行文件。通常在項目的DerivedData目錄下的Build/Products//.app文件,然后顯示包內容,有個和工程同名的可執行文件。比如我的目錄:

/Users/bolei/Library/Developer/Xcode/DerivedData/FFProject-gqpkbetfhlofkxcmyfwpmkfqubun/Build/Products/Release-iphoneos/FFProject.app/FFProject

打印使用到的類: _objc_classrefs

otool -V -o FFProject -arch arm64 | open -f

可以打印出來objc Section中的所有數據

otool說明

2. iSee使用

iSee是一款分析iOS可執行文件成分的工具,參考zyangSir的iSee工程做了修改,github地址 通過加載XCode在項目編譯期間產生的linkMap文件,能夠輸出項目中每個類(包括第三方靜態庫),在最終可執行文件中占用的長度信息。本工具根據zyangSir的代碼做了部分修改。主要功能有:

  1. 各個可執行文件占用大小
  2. 可執行文件中,各個段占用的大小(包括方法+常量字符串等)
  3. 未使用到的類
  4. 未使用到的方法

包括:

  1. 使用的所有庫: custom是非系統庫或第三方庫


    image.png
  2. 庫里面用到的所有類


    image.png
  3. 類里面所有節信息,Z開頭的是輔助信息,記錄使用到的類+方法和未使用的類+方法


    image.png
  4. 節對應詳情信息


    image.png
  5. 未使用的類:


    image.png
  6. 未使用的方法

2.1 使用方法

建議使用真機生成的文件測試,目前看對arm64架構支持最好。優先使用arm64相關文件檢測。分析不了framework里面的,所以如果是cocoapods導入,建議去掉!framework,使用靜態庫導入。強烈建議cocoapods使用靜態庫導入,會顯著提升啟動速度

2.1.1 導出生成linkmap文件

  1. 在XCode編譯選項中打開”WriteLinkMapFile”
    image
  2. 選擇好真機(采用arm64架構的),選Debug。先編譯項目,進入項目的Derived Data目錄
    image
  3. 依次進入Build/Intermediates/項目名.build/ 目錄, 找到相應模式下的編譯產物文件夾
    image
  4. 可以看到一個名為 項目名-LinkMap-normal-CPU架構.txt的文件,在iSee中點擊linkMap文件按鈕,導入這個文件

2.1.2 導出生成的可執行文件

  1. 在上一步的Derived Data目錄下,

    看到.app結尾的文件,郵件顯示包內容,找到和工程名稱一樣的一個可執行文件,把這個文件copy到相關目錄

  2. 在iSee中點擊可執行文件,導入剛才的文件

  3. 之后程序會自動執行分析。

2.2 簡單說明

  1. 現在對arm64支持比較好,所以建議用這個格式測試。可以在debug模式下,鏈接Arm64的機器,然后只編譯生成此平臺下的數據。
  2. 對于未使用方法和未使用類,使用otool工具來做輔助分析
  3. 未使用方法和未使用類,有誤報可能,主要誤報的是一些實現協議的類,這一部分是動態使用的,所以靜態分析不出來。
  4. 目前沒有對swift語言的分析。
2021-04-20 16:31:57.378513+0800 otool[82701:1827934] Failed to open macho file at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/otool for reading: Too many levels of symbolic links
2021-04-20 16:31:57.571279+0800

2.3 原理說明

先讀取linkmap

2.3.1 解析Object files

  1. 解析Object files, 按照行讀取,使用正則解析@"\\[\\s*(\\d+)\\]\\s+(.+)"每一行的數據,知道遇到# Sections:停止。其中最后一個匹配到的是具體路徑,可以切割后文件名,比如ViewController.o,路徑以user開頭的,就是用戶代碼,否則認為系統庫代碼。需要注意的是[數字],里面的數據是具體序號,需要按照順序保存到數組中,供后續使用。

    # Object files:
    [  0] linker synthesized
    [  1] /Users/bolei/Library/Developer/Xcode/DerivedData/iSeeDemo-famzgrqugqqtpjfncdokvahyclhb/Build/Intermediates.noindex/iSeeDemo.build/Debug-iphoneos/iSeeDemo.build/Objects-normal/arm64/ViewController.o
    [  2] /Users/bolei/Library/Developer/Xcode/DerivedData/iSeeDemo-famzgrqugqqtpjfncdokvahyclhb/Build/Intermediates.noindex/iSeeDemo.build/Debug-iphoneos/iSeeDemo.build/Objects-normal/arm64/AppDelegate.o
    [  3] /Users/bolei/Library/Developer/Xcode/DerivedData/iSeeDemo-famzgrqugqqtpjfncdokvahyclhb/Build/Intermediates.noindex/iSeeDemo.build/Debug-iphoneos/iSeeDemo.build/Objects-normal/arm64/main.o
    [  4] /Users/bolei/Library/Developer/Xcode/DerivedData/iSeeDemo-famzgrqugqqtpjfncdokvahyclhb/Build/Intermediates.noindex/iSeeDemo.build/Debug-iphoneos/iSeeDemo.build/Objects-normal/arm64/SceneDelegate.o
    [  5] /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.4.sdk/System/Library/Frameworks//Foundation.framework/Foundation.tbd
    [  6] /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.4.sdk/usr/lib/libobjc.tbd
    [  7] /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.4.sdk/System/Library/Frameworks//UIKit.framework/UIKit.tbd
    

    參考代碼:

/**
 *  解析目標文件log
 */
- (void)parseObjectFileLog
{
    NSMutableArray *tmpArray = [NSMutableArray arrayWithCapacity: 100];

    self.lastLineStr = [_linkMapfileReader readLine];
    while (![self isSectionStartFlag: _lastLineStr]) {//如果沒檢測到下一段不同類型log的起始標識串,則繼續
//        NSLog(@"lastLine = %@",_lastLineStr);
        if ([self.lastLineStr hasPrefix:@"#"]) {
            self.lastLineStr = [_linkMapfileReader readLine];
            continue;
        }
          
        NSString *regexStr = @"\\[\\s*(\\d+)\\]\\s+(.+)";
        NSRegularExpression* regexExpression = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:nil];
        NSArray* matchs = [regexExpression matchesInString:self.lastLineStr options:0 range:NSMakeRange(0, self.lastLineStr.length)];
        
        if (matchs == nil || [matchs count] == 0) {
            return;
        }
        
        NSTextCheckingResult *checkingResult = [matchs objectAtIndex:0];
        
        if ([checkingResult numberOfRanges] < 3) {
            return;
        }
        
        NSString *indexStr = [self.lastLineStr substringWithRange:[checkingResult rangeAtIndex:1]];//索引
        NSUInteger index = indexStr.integerValue;
        NSString *path = [self.lastLineStr substringWithRange:[checkingResult rangeAtIndex:2]];//索引
        NSRange range = [path rangeOfString:@"/"];
        ObjectFileItem * objFileItem = [[ObjectFileItem alloc]             init];
        if (range.location == NSNotFound) {
            objFileItem.fileType = OBJECT_FILE_FROM_CUSTOM_CODE;
            objFileItem.name     = path;
            objFileItem.module = @"Custom";
        } else {
            NSString *pathStr  = [path substringFromIndex:range.location];
            NSString * objectFileName = [pathStr lastPathComponent];
//            NSLog(@"path = %@, fileName= %@",pathStr,objectFileName);
            if ([pathStr hasPrefix: CUSTOM_LIB_PATH_PREFIX]) {
                NSRange bracketRange = [objectFileName rangeOfString: @"("];
                if (bracketRange.location != NSNotFound ) {
                    //靜態庫中的目標文件
                    objFileItem.module = [objectFileName substringToIndex:bracketRange.location];
                    objFileItem.fileType = OBJECT_FILE_FROM_STATIC_FILE;
                    NSRange objNameRange = bracketRange;
                    objNameRange.location ++;
                    objNameRange.length = objectFileName.length - (objNameRange.location + 1) - 1; //去掉兩個括號
                    objFileItem.name = [objectFileName substringWithRange: objNameRange];
                }else
                {
                    //用戶自行創建的類
                    objFileItem.fileType = OBJECT_FILE_FROM_CUSTOM_CODE;
                    objFileItem.name     = objectFileName;
                    objFileItem.module = @"Custom";
                }
                
            }else if ([pathStr hasPrefix: SYSTEM_LIB_PATH_PREFIX])
            {   //系統庫目標文件
                objFileItem.fileType = OBJECT_FILE_FROM_SYSTEM_LIB;
                objFileItem.name     = objectFileName;
                objFileItem.module = @"System";
            }
            
            double progress = [_linkMapfileReader readedFileSizeRatio];
            [self updateAnalyzeProgress: progress];
        }
        
        if (tmpArray.count > index) {
            [tmpArray replaceObjectAtIndex:index withObject:objFileItem];
        } else {
            [tmpArray addObject:objFileItem];
        }
        // one loop end, start parsing next line log
        self.lastLineStr = [_linkMapfileReader readLine];
        double progress = [_linkMapfileReader readedFileSizeRatio];
        [self updateAnalyzeProgress: progress];
    }
    
    self.objectFileArray = [NSArray arrayWithArray: tmpArray];
    
}

2.3.2 解析section

這里保存了每個段的保存地址,需要保存下來,后面用于匹配在哪個段和節中。

數據為:

# Sections:
# Address   Size        Segment Section
0x100006328 0x0001D970  __TEXT  __text
0x100023C98 0x00000570  __TEXT  __stubs
0x100024208 0x00000588  __TEXT  __stub_helper
0x100024790 0x00005E1A  __TEXT  __objc_methname
0x10002A5AA 0x00002CCD  __TEXT  __cstring
0x10002D277 0x000004DE  __TEXT  __objc_classname
0x10002D755 0x00001AB0  __TEXT  __objc_methtype
0x10002F208 0x00000028  __TEXT  __const
0x10002F230 0x00000448  __TEXT  __gcc_except_tab
0x10002F678 0x0000014A  __TEXT  __ustring
0x10002F7C4 0x000007C0  __TEXT  __unwind_info
0x10002FF88 0x00000074  __TEXT  __eh_frame
0x100030000 0x000000E8  __DATA  __got
0x1000300E8 0x000003A0  __DATA  __la_symbol_ptr
0x100030488 0x00000850  __DATA  __const
0x100030CD8 0x00001120  __DATA  __cfstring
0x100031DF8 0x00000108  __DATA  __objc_classlist
0x100031F00 0x00000008  __DATA  __objc_nlclslist
0x100031F08 0x00000038  __DATA  __objc_catlist
0x100031F40 0x00000088  __DATA  __objc_protolist
0x100031FC8 0x00000008  __DATA  __objc_imageinfo
0x100031FD0 0x00009830  __DATA  __objc_const
0x10003B800 0x00001480  __DATA  __objc_selrefs
0x10003CC80 0x00000010  __DATA  __objc_protorefs
0x10003CC90 0x00000228  __DATA  __objc_classrefs
0x10003CEB8 0x000000D0  __DATA  __objc_superrefs
0x10003CF88 0x0000022C  __DATA  __objc_ivar
0x10003D1B8 0x00000A50  __DATA  __objc_data
0x10003DC08 0x00000680  __DATA  __data
0x10003E288 0x000000C0  __DATA  __bss

也按照順序保存到數組里。解析代碼:

/**
 *  解析段表log
 */
- (void)parseSectionTableLog
{
    NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithCapacity: 50];
    
    self.lastLineStr = [_linkMapfileReader readLine];
//    NSLog(@"parseSectionTableLog = %@",self.lastLineStr);
    while (![self isSectionStartFlag: _lastLineStr]) {
        if ([self.lastLineStr hasPrefix:@"#"]) {
            self.lastLineStr = [_linkMapfileReader readLine];
            continue;
        }
        NSArray *oneLineConponents = [_lastLineStr componentsSeparatedByString:@"\t"];
        NSString *address = oneLineConponents[0];
        NSString *sizeStr = oneLineConponents[1];
        NSString *segmentTypeStr = oneLineConponents[2];
        NSString *sectionNameStr = oneLineConponents[3];
        
//        NSLog(@"address = %@, sizeStr = %@ segmentTypeStr = %@ sectionNameStr = %@",address,sizeStr,segmentTypeStr,sectionNameStr);
        
        ExecutableCodeItem *codeItem = [[ExecutableCodeItem alloc] init];
        codeItem.size = strtoul([sizeStr UTF8String], 0, 16);
        NSUInteger lastIndex = [sectionNameStr length] - 1;//2 是制表符 \t 的兩個字符位移
        codeItem.name = [sectionNameStr substringToIndex: lastIndex];
        codeItem.startAddress = strtoul([address UTF8String], 0, 16);
        
        if ([segmentTypeStr isEqualToString: SEGMENT_TYPE_CODE]) {
            codeItem.segmentType = CodeType_TEXT;
        }else if ([segmentTypeStr isEqualToString: SEGMENT_TYPE_DATA])
        {
            codeItem.segmentType = CodeType_DATA;
        }
        [tmpArray addObject: codeItem];
        
        //one loop end , start next circle
        self.lastLineStr = [_linkMapfileReader readLine];
        [self updateAnalyzeProgress: _linkMapfileReader.readedFileSizeRatio];
    }
    [self updateAnalyzeProgress: _linkMapfileReader.readedFileSizeRatio];
    self.executableCodeArray = [NSArray arrayWithArray: tmpArray];
    
}

2.3.3 解析符號表

后面的數據,會按照地址順序,打印相關內容,也按行解析,匹配正則@"(.+?)\\t(.*?)\\t\\[\\s*(\\d+)\\]\\s+(.+)"。。其中[數字]就是匹配的上面第一步里解析Object files里面的.o文件,通過序號匹配就能找到對應的是哪個文件里的內容。通過起始地址就能解析出來具體屬于哪個節section。通過Size就能算出這個方法占用的大小。

# Symbols:
# Address   Size        File  Name
0x100006328 0x00000114  [  1] -[ViewController viewDidLoad]
0x10000643C 0x00000028  [  1] ___29-[ViewController viewDidLoad]_block_invoke
0x100006464 0x00000028  [  1] ___29-[ViewController viewDidLoad]_block_invoke_2
0x10000648C 0x00000008  [  2] -[AppDelegate application:didFinishLaunchingWithOptions:]
0x100006494 0x0000008C  [  2] -[AppDelegate application:configurationForConnectingSceneSession:options:]
0x100006520 0x00000004  [  2] -[AppDelegate application:didDiscardSceneSessions:]
0x100006524 0x00000080  [  3] _main
0x1000065A4 0x00000004  [  4] -[SceneDelegate scene:willConnectToSession:options:]
0x1000065A8 0x00000004  [  4] -[SceneDelegate sceneDidDisconnect:]
0x1000065AC 0x00000004  [  4] -[SceneDelegate sceneDidBecomeActive:]
0x1000065B0 0x00000004  [  4] -[SceneDelegate sceneWillResignActive:]
0x1000065B4 0x00000004  [  4] -[SceneDelegate sceneWillEnterForeground:]
0x1000065B8 0x00000004  [  4] -[SceneDelegate sceneDidEnterBackground:]
0x1000065BC 0x00000010  [  4] -[SceneDelegate window]
0x1000065CC 0x00000014  [  4] -[SceneDelegate setWindow:]
0x1000065E0 0x00000014  [  4] -[SceneDelegate .cxx_destruct]
0x1000065F4 0x0000014C  [  5] -[AFCachedImage initWithImage:identifier:]
0x100006740 0x00000064  [  5] -[AFCachedImage accessImage]
0x1000067A4 0x000000A4  [  5] -[AFCachedImage description]
0x100006848 0x00000008  [  5] -[AFCachedImage image]
0x100006850 0x0000000C  [  5] -[AFCachedImage setImage:]
0x10000685C 0x0000000C  [  5] -[AFCachedImage identifier]

解析代碼:

/**
 *  解析一行符號log
 *
 *  @param oneLineLog 一行符號log
 *
 *  @return 解析結果
 */
- (void)parseOneLineSymbolLog:(NSString *)oneLineLog
{
//    NSLog(@"parseOneLineSymbolLog = %@", oneLineLog);
    //過濾非目標串
    NSString *filtreString = @"\t * \n * \x10\n * %@\n * \r\n";
    NSRange range = [filtreString rangeOfString: oneLineLog];
    if (range.location != NSNotFound) {
        return;
    }
    
    
    NSString *regexStr = @"(.+?)\\t(.*?)\\t\\[\\s*(\\d+)\\]\\s+(.+)";
    NSRegularExpression* regexExpression = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:nil];
    NSArray* matchs = [regexExpression matchesInString:oneLineLog options:0 range:NSMakeRange(0, oneLineLog.length)];
    
    if (matchs == nil || [matchs count] == 0) {
        return;
    }
    
    NSTextCheckingResult *checkingResult = [matchs objectAtIndex:0];
    
    if ([checkingResult numberOfRanges] < 5) {
        return;
    }
    
    
    NSString *startAddressStr = [oneLineLog substringWithRange:[checkingResult rangeAtIndex:1]];//起始地址
    NSString *sizeStr = [oneLineLog substringWithRange:[checkingResult rangeAtIndex:2]];//空間大小
    NSString *indexStr = [oneLineLog substringWithRange:[checkingResult rangeAtIndex:3]];//索引
    NSString *name = [oneLineLog substringWithRange:[checkingResult rangeAtIndex:4]];//名稱
    
    long startAddress = strtoul([startAddressStr UTF8String], 0, 16);
    long size = strtoul([sizeStr UTF8String], 0, 16);
    NSUInteger index = indexStr.integerValue;
    
    ExecutableCodeItem *executable = [self excutableItem:startAddress];//段名稱
    //添加到所屬的目標文件
    if (index < _objectFileArray.count) {
        ObjectFileItem *targetObjectFile = _objectFileArray[ index ];
        targetObjectFile.size += size;

        ObjectSecionItem *section = [targetObjectFile.sectionDictionary objectForKey:executable.name];
        if (section == nil) {
            section = [[ObjectSecionItem alloc] init];
            section.name = executable.name;
            section.fileTypeName = executable.segmentTypeStr;
            [targetObjectFile.sectionDictionary setObject:section forKey:executable.name];
        }
        
        section.size += size;
        
        MethodFileItem *funcItem = [[MethodFileItem alloc] init];
        funcItem.name = name;
        funcItem.size = size;
        funcItem.fileTypeName = executable.name;
        funcItem.startAddress = startAddress;
        [section.objectsList addObject:funcItem];
        
//        NSLog(@"startAddress = %@ size = %@ index = %@ name = %@ target = %@ section = %@",startAddressStr,sizeStr,indexStr,name,targetObjectFile, executable.name);
    }
}

到這里整個linkmap就解析完成了。接著使用otools來解析具體二進制文件。(備注:這里為啥要用otools解析,原因是objc_selrefs__objc_classrefs記錄了程序中用到的方法和類,但linkmap里面只有二進制數據,需要進一步解析成具體數據結構,這個就需要依賴otools來做)。

2.3.4 利用otools解析使用到的所有方法和未使用的所有方法

運行otool -v -s __DATA __objc_selrefs <path>,輸出__objc_selrefs內容。

Contents of (__DATA,__objc_selrefs) section
000000010003b800  __TEXT:__objc_methname:viewDidLoad
000000010003b808  __TEXT:__objc_methname:manager
000000010003b810  __TEXT:__objc_methname:dictionaryWithObjects:forKeys:count:
000000010003b818  __TEXT:__objc_methname:GET:parameters:headers:progress:success:failure:
000000010003b820  __TEXT:__objc_methname:role
000000010003b828  __TEXT:__objc_methname:initWithName:sessionRole:
000000010003b830  __TEXT:__objc_methname:class
000000010003b838  __TEXT:__objc_methname:init
000000010003b840  __TEXT:__objc_methname:setImage:
000000010003b848  __TEXT:__objc_methname:setIdentifier:
000000010003b850  __TEXT:__objc_methname:size
000000010003b858  __TEXT:__objc_methname:scale
000000010003b860  __TEXT:__objc_methname:setTotalBytes:
000000010003b868  __TEXT:__objc_methname:date
000000010003b870  __TEXT:__objc_methname:setLastAccessDate:
...

獲取之后先通過正則解析(.+?)\\s+__TEXT:__objc_methname:(.+), 解析出來起始地址,方法名稱比如viewDidLoad。接著需要找到這個方法對應調用的類是什么。這個需要使用前面前面解析linkmap用到的數據,算法是:

  1. 按照順序從前面o文件列表里面搜索,取到每個類對應的__objc_selrefs數據。
  2. 然后從__objc_selrefs中,比對數據里面那個起始地址一致,說明這個數據就屬于這個類。
  3. 然后把這個方法記錄到已經使用的方法列表里面。
  4. 解析是否有__objc_ivar段,比如_OBJC_IVAR_$_AFSecurityPolicy._pinnedPublicKeys,說明是屬性,需要把_pinnedPublicKeyssetPinnedPublicKeys也加入到方法列表里面。
  5. 檢索上一步linkmap中的__objc_methname里面的數據,literal string: scene:willConnectToSession:options:, 從第三步驟記錄的所有使用的方法查看是否有使用,沒有使用就寫入未使用方法列表里面。

有部分代碼可能比較繞,其實主要是為了加速搜索。因為地址是一直遞增的,所以不會每次搜索都從剛開始檢索,而是從上次的結果之后來檢索。

- (void)anylyzeUsedMethodWithData:(NSString *)string {
  // 解析結果,分解數據
    if (string.length) {
        //數據清空
        for (ObjectFileItem *file in self.resultList) {
            [file.usedMethod removeAllObjects];
            [file.unUsedMethod removeAllObjects];
        }
        
        //解析 0000000100dcd9c0  __TEXT:__objc_methname:alloc
        NSArray *lines = [string componentsSeparatedByString:@"\n"];
        NSString *regexStr = @"(.+?)\\s+__TEXT:__objc_methname:(.+)";
        NSRegularExpression* regexExpression = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:nil];
        
        
        int objIndex = 0; //掃描的obj的索引
        int methodIndex = 0; //掃描到的方法索引
        
        // 解析出來使用到的方法
        for (NSString *line in lines) {
            @autoreleasepool {
                if (objIndex >= [self.resultList count]) {
                    break;
                }
                
                NSArray* matchs = [regexExpression matchesInString:line options:0 range:NSMakeRange(0, line.length)];
                
                if (matchs == nil || [matchs count] == 0) {
                    continue;
                }
                
                NSTextCheckingResult *checkingResult = [matchs objectAtIndex:0];
                
                if ([checkingResult numberOfRanges] < 3) {
                    continue;
                }
                
                NSString *startAddressStr = [line substringWithRange:[checkingResult rangeAtIndex:1]];
                NSString *method = [line substringWithRange:[checkingResult rangeAtIndex:2]];
                long startAddress = strtoul([startAddressStr UTF8String], 0, 16);
                
                
                ObjectSecionItem *section = nil;
                ObjectFileItem *obj = nil;
                
                //需要找到對應哪個類的起始地址
                while (objIndex < [self.resultList count]) {
                    obj = [self.resultList objectAtIndex:objIndex];
                    section = [obj.sectionDictionary objectForKey:@"__objc_selrefs"];
                    MethodFileItem *method = [section.objectsList lastObject];
                    if (method.startAddress < startAddress) {
                        objIndex ++;
                        methodIndex = 0;
                    }else {
                        break;
                    }
                }
                
                if (objIndex >= [self.resultList count]) {
                    break;
                }
                
                //數據其實是一一對應的,如果沒找到可能是異常了
                // 從第0個開始找,如果找到了,就放在usedMethod里面,并繼續循環
                MethodFileItem *methodItem = [section.objectsList objectAtIndex:methodIndex];
                if (methodItem.startAddress == startAddress) {
                    methodItem.name = method;
                    [obj.usedMethod setObject:methodItem forKey:method];
                    methodIndex ++;
                    continue;
                }
                
                // 如果沒找到,循環遍歷查找,找到后放入,,標記下methodIndex,并繼續循環
                for (int j = 0; j < [section.objectsList count]; j ++) {
                    MethodFileItem *methodItem = [section.objectsList objectAtIndex:j];
                    if (methodItem.startAddress == startAddress) {
                        //獲取到
                        methodIndex = j;
                        [obj.usedMethod setObject:methodItem forKey:method];
                        break;
                    }
                }
                
            }
        }
        
        
        
        //填充數據
        for (ObjectFileItem *obj in self.resultList) {
            ObjectSecionItem *allIvarSection = [obj.sectionDictionary objectForKey:@"__objc_ivar"];
            
            for (MethodFileItem *method  in allIvarSection.objectsList) {
                NSRange range = [method.name rangeOfString:@"." options:NSBackwardsSearch];
                if (range.location == NSNotFound) {
                    continue;
                }
                
                //從_開始 0x100DF2014  0x00000004  [ 16] _OBJC_IVAR_$_AFSecurityPolicy._pinnedPublicKeys
                //0x100DF44BC   0x00000004  [723] _OBJC_IVAR_$_HFDataBaseCore.dbPath
                
                NSString *methodStr = [method.name substringFromIndex:range.location + 1];
                if ([methodStr hasPrefix:@"_"]) {
                    methodStr = [methodStr substringFromIndex:1];
                    if ([methodStr length] > 1) {
                        NSString *_methodStr = [NSString stringWithFormat:@"_%@",methodStr];
                        NSString *setMethod = [NSString stringWithFormat:@"set%@%@:",[methodStr substringToIndex:1].uppercaseString,[methodStr substringFromIndex:1]];
                        
                        [obj.usedMethod setObject:method forKey:_methodStr];
                        [obj.usedMethod setObject:method forKey:setMethod];
                    }
                }
                [obj.usedMethod setObject:method forKey:methodStr];
                
            }
            
            ObjectSecionItem *allClassSection = [obj.sectionDictionary objectForKey:@"__objc_methname"];
            NSMutableDictionary *usedMethod = obj.usedMethod;
            for (MethodFileItem *method in allClassSection.objectsList) {
                NSString *methodStr = [method.name substringFromIndex:@"literal string: ".length];
                if ([usedMethod objectForKey:methodStr]) {
                    continue;
                }
            
                [obj.unUsedMethod setObject:method forKey:methodStr];
                
                
                MethodFileItem *unUsedMethod = [MethodFileItem new];
                unUsedMethod.size = method.size;
                unUsedMethod.fileTypeName = method.fileTypeName;
                unUsedMethod.startAddress = method.startAddress;
                unUsedMethod.name = [NSString stringWithFormat:@"[%@ %@]",obj.name,methodStr];
                
                [self.unUsedSelectorList addObject:unUsedMethod];
            }
            
            if ([obj.unUsedMethod count] > 0) {
                ObjectSecionItem *unusedSection = [[ObjectSecionItem alloc] init];
                unusedSection.name = @"Z__unused_selector";
                unusedSection.fileTypeName = @"Custom";
                unusedSection.size = [obj.unUsedMethod count];
                [unusedSection.objectsList addObjectsFromArray:[obj.unUsedMethod allValues]];
                [obj.sectionDictionary setObject:unusedSection forKey:@"Z__unused_selector"];
            }
            
            
            if ([obj.usedMethod count] > 0) {
                ObjectSecionItem *usedSection = [[ObjectSecionItem alloc] init];
                usedSection.name = @"Z__used_selector";
                usedSection.fileTypeName = @"Custom";
                usedSection.size = [obj.usedMethod count];
                [usedSection.objectsList addObjectsFromArray:[obj.usedMethod allValues]];
                [obj.sectionDictionary setObject:usedSection forKey:@"Z__used_selector"];
            }
        
        }
        
    }
}

2.3.5 利用otools解析使用到的所有類和未使用的類

接著和上面的類似,解析出來__objc_selrefs的內容就可以得到所有使用的class, 但是注意的是通過命令otool -V -s __DATA __objc_classrefs 輸出的是二進制的信息。所以需要用otool -V -o <path>,輸出OC解析后的內容。找到Contents of (__DATA,__objc_classrefs) section

Contents of (__DATA,__objc_classrefs) section
000000010003cc90 0x10003d348 _OBJC_CLASS_$_AFHTTPSessionManager
000000010003cc98 0x0 _OBJC_CLASS_$_NSDictionary
000000010003cca0 0x0 _OBJC_CLASS_$_UISceneConfiguration
000000010003cca8 0x10003d230 _OBJC_CLASS_$_AppDelegate
000000010003ccb0 0x0 _OBJC_CLASS_$_NSDate
000000010003ccb8 0x0 _OBJC_CLASS_$_NSString
000000010003ccc0 0x0 _OBJC_CLASS_$_NSMutableDictionary

獲取之后先通過正則解析(.+?)\\s+(.+?)\\s+_OBJC_.+_\\$_(.+), 解析出來起始地址,使用的類名比如_OBJC_CLASS_$_AFHTTPSessionManager。并把他關聯到具體使用的拿個類里面。然后利用linkmap中的__objc_classname來做對比,還要把繼承的協議(__objc_protolist)也作為使用的類。 思路和前面的一致,詳細請參考具體源碼。

2.5 代碼參考

相關代碼歡迎fork:https://github.com/dishibolei/iSee.git

主要功能包含了

  1. 代碼占用分析
  2. 未使用類分析
  3. 未使用方法分析

3. 參考

  1. IOS Section Document
  2. IOS 官方Section介紹
  3. mach-o文件結構分析類名方法名
  4. load 方法全程跟蹤
  5. iOS APP可執行文件的組成
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容