0
点赞
收藏
分享

微信扫一扫

iOS底层原理01:objc4-818 源码编译 & 调试

boomwu 2021-10-04 阅读 85

准备工作

环境版本 & 最新objc源码

  • mac OS 10.15.4
  • Xcode 12.3
  • objc4-818源码

依赖文件下载

需要下载以下依赖文件


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

源码编译

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

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

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

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

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

  • Apple sourcemacOS10.15 --> xnu-6153.101.6/bsd/sys/reason.h路径自行下载
  • objc4-818.2的根目录下新建HTCommon文件夹, 同时在HTCommon文件夹中创建sys文件夹
  • 最后将 reason.h文件拷贝到sys文件夹中

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

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

  • HTCommon文件夹中 创建 mach-o文件夹
  • 找到文件:dyld-733.6 -- include -- mach-o -- dyld_priv.h

  • 拷贝到HTCommon -- mach-o

  • 拷贝到文件后,还需要修改 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

  • dyld_priv.h文件出现bridgeos(3.0)报错,则将参数bridgeos(3.0)去除;

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

  • HTCommon中创建 os文件夹
  • 找到lock_private.h文件:libplatform-220.100.1 --> private --> os --> lock_private.h,并将文件拷贝至 os 文件夹中

【4】'os/base_private.h' file not found

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


  • lock_private.h dyld_priv.h中如果报错,去掉 bridgeos(3.0)bridgeos(4.0)即可

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

  • HTCommon中创建 pthread 文件夹
  • 找到tsd_private.hspinlock_private.h文件,h文件路径为:libpthread-416.100.3 --> private --> tsd_private.h、spinlock_private.h,并拷贝到 pthread文件夹

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

  • HTCommon中创建 System/machine 文件夹
  • 找到 cpu_capabilities.h文件拷贝到 machine文件夹,h文件路径为:xnu-6153.101.6 --> osfmk --> machine --> cpu_capabilities.h

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

  • 找到 tsd.h文件,拷贝到os文件文件夹, h文件路径为:xnu-6153.101.6 --> libsyscall --> os --> tsd.h

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


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

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

【9】'CrashReporterClient.h' file not found

  • CrashReporterClient.h文件在该地址搜索 Libc-825.24中找到该文件,路径为Libc-825.24/include/CrashReporterClient.h,直接存放在HTCommon文件夹中
  • 导入下载的还是报错,可以在CrashReporterClient.h文件中添加下列宏定义
#define LIBC_NO_LIBCRASHREPORTERCLIENT

【10】Typedef redefinition with different types ('int' vs 'volatile OSSpinLock' (aka 'volatile int'))

  • 有两处定义了pthread_lock_t,注释掉pthread_machdep.h文件中的定义即可。

【11】Static declaration of '_pthread_has_direct_tsd' follows non-static declaration

pthread_machdep.h有三个函数定义重复了:

  • _pthread_has_direct_tsd(void)
  • _pthread_getspecific_direct(unsigned long slot)
  • _pthread_setspecific_direct(unsigned long slot, void * val)

【解决方法】这里选择把pthread_machdep.h文件中的定义注释掉。

【12】'os/feature_private.h' file not found

直接将该引用的头文件注释,objc-runtime.mm注释掉第36行和444~446



【13】'_simple.h' file not found

  • 找到 _simple.h文件,拷贝到HTCommon文件文件夹, h文件路径为:libplatform-220.100.1 --> private

【14】'Cambria/Traps.h' file not found

  • 这两个文件(Cambria/Traps.hCambria/Cambria.h)在开源网站上找不到,可以注释掉头文件和源码objc-cache.mm第1121行~1128行,保留第1127行 即可。

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

  • HTCommon中创建kern文件夹
  • 找到 h文件,路径为xnu-6153.101.6 --> osfmk --> kern -->restartable.h

【16】'objc-bp-assist.h' file not found

  • 直接将该引用的头文件注释

【17】Use of undeclared identifier 'dyld_fall_2020_os_versions'

报这种相关错误的地方,目前可以将对应的代码全部注释。注释的地方有:

  • objc-runtime.mm 第379~380行

【18】Use of undeclared identifier 'dyld_platform_version_macOS_10_13'

直接定位到该段代码,并注释掉。如下所示


【19】Use of undeclared identifier 'dyld_platform_version_macOS_10_11'

直接定位到该段代码,并注释掉。如下所示


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

  • 文件路径为:dyld-750.5 --> include --> objc-shared-cache.h
  • 将h文件报备制拷贝到HTCommon

【21】Use of undeclared identifier 'dyld_fall_2018_os_versions'

直接定位到该段代码,并注释掉。如下所示


【22】'os/linker_set.h' file not found

  • 文件路径为:xnu-6153.101.6 --> bsd --> sys --> linker_set.h
  • 将h文件报备制拷贝到HTCommon/os目录下

【23】Unknown type name 'kernel_mach_header_t'

  • 注释掉报错代码,如下所示

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

  • 文件路径为:dyld-750.5 --> include --> objc-shared-cache.h
  • 将h文件报备制拷贝到HTCommon

【25】'Block_private.h' file not found

  • 找到 h 文件,文件路径为libclosure-74 --> Block_private.h
  • 拷贝至HTCommon目录

【26】'os/reason_private.h' file not found

  • 找到 h 文件,文件路径为xnu-6153.101.6 --> libkern --> os --> reason_private.h
  • 拷贝至HTCommon/os目录

【27】'os/variant_private.h' file not found

  • 找到 h 文件,文件路径为Libc-1353.100.2 --> os --> variant_private.h
  • 拷贝至HTCommon/os目录

【28】Use of undeclared identifier 'dyld_platform_version_bridgeOS_2_0'

  • 不知道咋改,先注释吧

【29】'_static_assert' declared as an array with a negative size

  • 出错行位于objc-runtime-new.mm文件中第176~177行,将该行注释即可

【30】libobjc.order 路径问题

问题描述为:ld: can't open order file: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/AppleInternal/OrderFiles/libobjc.order

  • 选择 target -> objc -> Build Settings
  • 在工程的 Order File 中添加搜索路径 $(SRCROOT)/libobjc.order

【31】library not found for -lCrashReporterClient

  • 找到TARGETS -> objc -> Build Setting -> Other Link Flags,删除-lCrashReporterClient

【32】library not found for -loah

  • 找到TARGETS -> objc -> Build Setting -> Other Link Flags,删除-loah

【33】Xcode 脚本编译问题

问题描述为:SDK "macosx.internal" cannot be located.

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

编译调试

  • 新建一个target :HTTest

  • 绑定二进制依赖关系

源码调试

  • 自定义一个HTPerson

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

补充

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

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

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

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

举报

相关推荐

0 条评论