OC底层原理-02:objc4-781-源码编译-&-调试

659 阅读1分钟

本文主要是通过将objc4-781的源码编译成功,然后在源码中进行调试,便于跟踪方法的底层实现流程

准备工作

环境版本 & 最新objc源码

  • mac OS 10.15
  • Xcode 11.4
  • objc4-781

依赖文件下载

需要下载以下依赖文件

image

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

源码编译

源码编译就是不断的调试修改源码的问题,主要有以下问题

问题一:unable to find sdk 'macosx.internal'

image

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

    image

问题二:文件找不到的报错问题

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

image

  • Apple sourcemacOS10.15 --> xnu-6153.11.26/bsd/sys/reason.h 路径自行下载

  • objc4-781的根目录下新建CJLCommon文件, 同时在CJLCommon文件中创建sys文件

  • 最后将 reason.h文件拷贝到sys文件中

    image

  • 设置文件检索路径:选择 target -> objc -> Build Settings,在工程的 Header Serach Paths 中添加搜索路径 $(SRCROOT)/CJLCommon

    image

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

  • CJLCommon文件中 创建 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中创建 os文件

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

    image

  • lock_private.h dyld_priv.h中如果报错,去掉 bridgeos(3.0),感谢@nicedayCoco 童鞋

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

  • 在CJLCommon中创建 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

  • 创建 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

  • 这个地址下载pthread_machdep.h文件,h文件路径为:Libc-583/pthreads/pthread_machdep.h
  • 将其拷贝至system文件中

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

【8】'CrashReporterClient.h' file not found

  • 这个文件在改地址搜索 Libc-825.24中找到该文件,路径为Libc-825.24/include/CrashReporterClient.h,直接存放在CJLCommon文件中
  • 导入下载的还是报错,可以通过以下方式解决
    • 需要在 Build Settings -> Preprocessor Macros 中加入:LIBC_NO_LIBCRASHREPORTERCLIENT
    • 或者下载我给大家的文件CrashReporterClient,这里面我们直接更改了里面的宏信息 #define LIBC_NO_LIBCRASHREPORTERCLIENT
    • 如果还是报错CrashReporterClient 的问题,解决方法是 在BuildSetting --> Other Linker Flags 中去掉CrashReporterClient .

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

  • 文件路径为:dyld-733.6 --> include --> objc-shared-cache.h

    image

  • 将h文件报备制拷贝到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中创建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

编译调试

  • 新建一个target :CJLTest

    image

  • 绑定二进制依赖关系

    image

源码调试

  • 自定义一个CJLPerson类

    image

  • 在main.m中 创建 CJLPerson的对象,进行源码调试

    image

补充

如果有以下调试问题,可以根据提供的方案进行尝试

创建的调试target中main的断点无法断住的问题

  • Build Phases --> Compile Source中,将main文件移至第一位

    image

  • Build Setting --> 将Enable Hardened Runtime 置为NO

    image

objc4-818源码 M1的电脑编译通过,但是运行报错 task_restartable_ranges_register failed (result 0x6: (os/kern) resource shortage) WeChat9c84fa0326f5e01411175ba1c0550d7c.png

解决办法:搜索task_restartable_ranges_register,注释掉 mi报错.png