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

准备工作

环境版本 & 最新objc源码

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

依赖文件下载

需要下载以下依赖文件


image

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

源码编译

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

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

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

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

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

image
  • Apple sourcemacOS10.15 --> xnu-6153.101.6/bsd/sys/reason.h路径自行下载
  • objc4-818.2的根目录下新建HTCommon文件夹, 同时在HTCommon文件夹中创建sys文件夹
  • 最后将 reason.h文件拷贝到sys文件夹中
image
  • 设置文件检索路径:选择 target -> objc -> Build Settings,在工程的 Header Serach Paths 中添加搜索路径 $(SRCROOT)/HTCommon
image

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

  • HTCommon文件夹中 创建 mach-o文件夹
  • 找到文件:dyld-733.6 -- include -- mach-o -- dyld_priv.h
image
  • 拷贝到HTCommon -- 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
image
  • dyld_priv.h文件出现bridgeos(3.0)报错,则将参数bridgeos(3.0)去除;
image

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

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

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

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

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

【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文件夹
image

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

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

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

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

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

image

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

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

【9】'CrashReporterClient.h' file not found

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

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

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

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

image

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

image

image

image

【13】'_simple.h' file not found

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

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

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

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

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

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

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

【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'

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


image

【19】Use of undeclared identifier 'dyld_platform_version_macOS_10_11'

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


image

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

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

【21】Use of undeclared identifier 'dyld_fall_2018_os_versions'

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


image

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

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

【23】Unknown type name 'kernel_mach_header_t'

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

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

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

【25】'Block_private.h' file not found

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

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

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

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

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

【28】Use of undeclared identifier 'dyld_platform_version_bridgeOS_2_0'

image
  • 不知道咋改,先注释吧
image

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

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

【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

image

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

【31】library not found for -lCrashReporterClient

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

【32】library not found for -loah

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

【33】Xcode 脚本编译问题

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

image

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

编译调试

  • 新建一个target :HTTest
image
  • 绑定二进制依赖关系
image

源码调试

  • 自定义一个HTPerson
image
  • main.m中 创建 HTPerson的对象,进行源码调试
image

补充

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

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

  • Build Phases --> Compile Source中,将main.m文件移至第一位
image
  • Build Setting --> 将Enable Hardened Runtime 置为NO
image
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容