iOS底层原理01:objc4-818 源码编译 & 调试
2021-03-17 本文已影响0人
黑白森林无间道
准备工作
环境版本 & 最新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中的objc
和obc-trampolines
都需要更改】

问题二:文件找不到的报错问题
【1】'sys/reason.h' file not found

- 在Apple source的
macOS10.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.h
、spinlock_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.h
、Cambria/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
