iOS 底层原理03:objc4-781 源码编译 & 调试

iOS 底层原理 文章汇总

本文主要是通过将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

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

  • 在CJLPerson中创建 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
  • 导入下载的还是报错,可以通过以下方式解决
    - 需要在 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文件报备制拷贝到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

猜你喜欢

转载自blog.csdn.net/lin1109221208/article/details/108435967