美文网首页调试objc4源码并编译
iOS底层探究-02:objc4-781源码编译&调试

iOS底层探究-02:objc4-781源码编译&调试

作者: 对你的微笑纯属礼貌_e31 | 来源:发表于2020-09-07 19:17 被阅读0次

    学会编译objc4-781的源码,然后在源码中进行调试,便于探究底层的实现流程

    准备工作

    环境版本 & 最新objc源码
    • mac OS 10.15
    • Xcode 11.6
    • objc4-781
      下载 objc4-781 源码
    依赖文件下载

    需要下载以下依赖文件


    1.png
    • 其中除了其中 launchd-842.92.1 需要在Mac OS X 10.9.5下载,其他的均可以在最新的的版本macOS 10.15中检索到

    源码编译

    编译源码就是不断调试的过程,主要有以下问题:

    问题一:unable to find sdk 'macosx.internal'
    2.png
    • 全局搜索macosx.internal,找到下图所示位置
      3.png
    • macosx.internal改为macOS 10.15,两处均需修改
      4.png
    问题二:文件找不到的报错问题
    1、'sys/reason.h' file not found
    5.png
    • objc4-781的根目录下新建LCCommon文件, 同时在LCCommon文件中创建sys文件
    • 在之前准备的依赖文件中全局搜索reason.h文件
      1.png
    • 然后将其拷贝到sys文件中
      2.png
    • 设置文件检索路径:选择 target -> objc -> Build Settings,在工程的 Header Serach Paths 中添加搜索路径 $(SRCROOT)/LCCommon
    3.png
    2、'mach-o/dyld_priv.h' file not found
    6.png
    • LCCommon文件中 创建mach-o文件

    • 在之前准备的依赖文件中全局搜索dyld_priv.h文件

      4.png
    • 将其拷贝到 mach-o文件中

      5.png
    • 拷贝到文件后,还需要修改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、bridgeos(3.0)、bridgeos(4.0)报错
    1.png
    • dyld_priv.h文件中搜索 bridgeos(3.0)将其删除(可以用空格全部替代)
    • lock_private.h文件中搜索 bridgeos(4.0)将其删除
    4、'os/lock_private.h' file not found 和 'os/base_private.h' file not found
    2.png
    • LCCommon中创建 os文件
    • 在之前准备的依赖文件中全局搜索lock_private.hbase_private.h文件,并将文件拷贝至 os文件中
      3.png
    5、'pthread/tsd_private.h' file 和 'pthread/spinlock_private.h' file not found
    • LCCommon中创建 pthread文件
    • 在之前准备的依赖文件中全局搜索tsd_private.hspinlock_private.h文件,并将文件拷贝至 pthread文件中
    6、'System/machine/cpu_capabilities.h' file not found
    4.png
    • LCCommon中创建 System文件,接着创建machine文件

    在之前准备的依赖文件中全局搜索cpu_capabilities.h文件(选择此路径下的xnu-6153.141.1/osfmk/machine),并将文件拷贝至 machine文件中

    7、'os/tsd.h' file not found
    • 在之前准备的依赖文件中全局搜索tsd.h文件(选择此路径下的xnu-6153.141.1/libsyscall/os),并将文件拷贝至 os文件中
      8.png
    8、'System/pthread_machdep.h' file not found
    • 这个地址下载pthread_machdep.h文件,h文件路径为:Libc-583/pthreads/pthread_machdep.h
    • 将其拷贝至System文件中

    在最新版的macOS 10.15中最新版下载的libc中没有这个h文件,需要下载Libc-583版本

    9、'CrashReporterClient.h' file not found
    • 这个文件在 Libc-825.24 中找到该文件,路径为Libc-825.24/include/CrashReporterClient.h
    • 导入下载的还是报错,可以通过以下方式解决
      • 需要在 Build Settings -> Preprocessor Macros 中加入:LIBC_NO_LIBCRASHREPORTERCLIENT
    10、'objc-shared-cache.h' file not found
    • 文件路径为:dyld-732.8 --> include --> objc-shared-cache.h

      1.png
    • 将.h文件报备制拷贝到LCCommon

    11、'_simple.h' file not found
    • 文件路径为:libplatform-220 --> private --> _simple.h
      2.png
    • 将.h文件拷贝到LCCommon
    12、'kern/restartable.h' file not found
    • 在LCCommon中创建kern文件
    • 找到.h文件,路径为xnu-6153.141.1 --> osfmk --> kern -->restartable.h,拷贝到kern
      3.png
    13、'Block_private.h' file not found
    • 找到 .h 文件,文件路径为libclosure-74 --> Block_private.h

      4.png
    • 将.h文件拷贝到LCCommon

    14、libobjc.order 路径问题

    ld: 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
      5.png
    15、library not found -lCrashReporterClient
    2.png
    • TARGETS - objc - Build Settings 搜索 other link 找到lCrashReporterClient将其删除
      3.png
    16、Xcode 脚本编译问题

    问题:Showing All Messages /xcodebuild:1:1: SDK "macosx.internal" cannot be located.

    • 选择 target -> objc -> Build Phases -> Run Script(markgc)
    • 把脚本文本 macosx.internal 改成 macosx
      4.png

    编译调试

    • 新建一个target :LCTest


      1.png
    • 关联二进制依赖关系

      2.png

    源码调试

    • 自定义一个LCPerson

      3.png
    • 在main.m中 创建 LCPerson的对象,开启你的源码探索之旅吧

      4.png

    相关文章

      网友评论

        本文标题:iOS底层探究-02:objc4-781源码编译&调试

        本文链接:https://www.haomeiwen.com/subject/iymkektx.html