iOS-底層(3):objc4-781 源碼編譯及調(diào)試

本文主要是通過將objc4-781的源碼編譯成功,然后在源碼中進(jìn)行調(diào)試,便于跟蹤方法的底層實(shí)現(xiàn)流程

準(zhǔn)備工作

環(huán)境版本 & 最新objc源碼

  • mac OS 10.15
  • Xcode 11.4
  • objc4-781

依賴文件下載

需要下載以下依賴文件

image

其中除了其中 launchd-106.10 需要在Mac OS X 10.4.4下載,其他的均可以在最新的的版本macOS 10.15中檢索到

源碼編譯

源碼編譯就是不斷的調(diào)試修改源碼的問題,主要有以下問題

問題一:unable to find sdk 'macosx.internal'

image
  • 選擇 target -> objc -> Build Settings -> Base SDK -> 選擇 macOS 【target中的 objc 和 obc-trampolines都需要更改】

    image

問題二:文件找不到的報(bào)錯(cuò)問題

【1】'sys/reason.h' file not found

image
  • Apple sourcemacOS10.15 --> xnu-6153.11.26/bsd/sys/reason.h 路徑自行下載

  • objc4-781的根目錄下新建CJLCommon文件, 同時(shí)在CJLCommon文件中創(chuàng)建sys文件

  • 最后將 reason.h文件拷貝到sys文件中

    image
  • 設(shè)置文件檢索路徑:選擇 target -> objc -> Build Settings,在工程的 Header Serach Paths 中添加搜索路徑 $(SRCROOT)/CJLCommon

    image

【2】'mach-o/dyld_priv.h' file not found

  • CJLCommon文件中 創(chuàng)建 mach-o 文件

  • 找到文件:dyld-733.6 -- include -- mach-o -- dyld_priv.h

    image
  • 拷貝到 mach-o文件中

    image
  • 拷貝到文件后,還需要修改 dyld_priv.h 文件,即在 dyld_priv.h文件頂部加入一下宏:

#define DYLD_MACOSX_VERSION_10_11 0x000A0B00
#define DYLD_MACOSX_VERSION_10_12 0x000A0C00
#define DYLD_MACOSX_VERSION_10_13 0x000A0D00
#define DYLD_MACOSX_VERSION_10_14 0x000A0E00

【3】'os/lock_private.h' file not found 和 'os/base_private.h' file not found

  • 在CJLCommon中創(chuàng)建 os文件

  • 找到lock_private.h、base_private.h文件:libplatform-220 --> private --> os --> lock_private.h 、base_private.h,并將文件拷貝至 os 文件中

    image

【4】'pthread/tsd_private.h' file not found 和 'pthread/spinlock_private.h' file not found

  • 在CJLPerson中創(chuàng)建 pthread 文件

  • 找到tsd_private.h、spinlock_private.h文件,h文件路徑為:libpthread-416.11.1 --> private --> tsd_private.h、spinlock_private.h,并拷貝到 pthread文件

    image

【5】'System/machine/cpu_capabilities.h' file not found

  • 創(chuàng)建 System -- machine 文件

  • 找到 cpu_capabilities.h文件拷貝到 machine文件,h文件路徑為:xnu6153.11.26 --> osfmk --> machine --> cpu_capabilities.h

    image

【6】os/tsd.h' file not found

  • 找到 tsd.h文件,拷貝到os文件, h文件路徑為:xnu6153.11.26 --> libsyscall --> os --> tsd.h

    image

【7】'System/pthread_machdep.h' file not found

  • 這個(gè)地址下載pthread_machdep.h文件,h文件路徑為:Libc-583/pthreads/pthread_machdep.h
  • 將其拷貝至system文件中

在最新版的macOS 10.15中最新版下載的libc中沒有這個(gè)h文件,需要下載Libc-583版本

【8】'CrashReporterClient.h' file not found

  • 這個(gè)文件在改地址搜索 Libc-825.24中找到該文件,路徑為Libc-825.24/include/CrashReporterClient.h
  • 導(dǎo)入下載的還是報(bào)錯(cuò),可以通過以下方式解決
    • 需要在 Build Settings -> Preprocessor Macros 中加入:LIBC_NO_LIBCRASHREPORTERCLIENT
    • 或者下載我給大家的文件CrashReporterClient,這里面我們直接更改了里面的宏信息 #define LIBC_NO_LIBCRASHREPORTERCLIENT

【9】'objc-shared-cache.h' file not found

  • 文件路徑為:dyld-733.6 --> include --> objc-shared-cache.h

    image
  • 將h文件報(bào)備制拷貝到CJLCommon

【10】Mismatch in debug-ness macros

  • 注釋掉objc-runtime.mm中的#error mismatch in debug-ness macros

    image

【11】'_simple.h' file not found

  • 文件路徑為:libplatform-220 --> private --> _simple.h

    image
  • 將文件拷貝至CJLCommon

【12】'kern/restartable.h' file not found

  • 在CJLCommon中創(chuàng)建kern 文件

  • 找到 h文件,路徑為xnu-6153.11.26 --> osfmk --> kern -->restartable.h

    image

【13】'Block_private.h' file not found

  • 找到 h 文件,文件路徑為libclosure-74 --> Block_private.h

    image
  • 拷貝至CJLCommon目錄

【14】libobjc.order 路徑問題

問題描述為:can't open order file: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/AppleInternal/OrderFiles/libobjc.order

  • 選擇 target -> objc -> Build Settings

  • 在工程的 Order File 中添加搜索路徑 $(SRCROOT)/libobjc.order

    image

【14】Xcode 腳本編譯問題
問題描述為:/xcodebuild:1:1: SDK "macosx.internal" cannot be located.

  • 選擇 target -> objc -> Build Phases -> Run Script(markgc)

  • 把腳本文本 macosx.internal 改成 macosx

    image

編譯調(diào)試

  • 新建一個(gè)target :CJLTest

    image
  • 綁定二進(jìn)制依賴關(guān)系

    image

源碼調(diào)試

  • 自定義一個(gè)CJLPerson類

    image
  • 在main.m中 創(chuàng)建 CJLPerson的對象,進(jìn)行源碼調(diào)試

    image
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容