使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

前言
为什么会想要调试源码?

苹果开源了部分源码, 但相似内容太多, 基本找不到代码见的对应关系, 如果能像自己工程一样进行跳转那多好哇~~
苹果源码开源地址: https://opensource.apple.com/
本文将以macOS 11.2/objc4-818.2的源码进行配置

源码配置
首先选中运行target: objc > My Mac
然后⌘+b进行编译
翻一下爆红的点看看都有哪些错误, 然后找到下面对应的解决方案~
编译报错 ‘sys/reason.h’ file not found

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

解决方案: 谷歌中输入reason.h site:opensource.apple.com定向检索

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

点进去搜索结果里面, 进行”文件另存为”下载
下载后, 在objc源码根目录中创建一个文件夹”ZKLib”,
然后因为reason.h文件是在sys目录下的, 所以我们在”ZKLib”目录下创建”sys”文件夹, 并把reason.h放到该目录里面(如下图)

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

接下来我们在源码工程 target > objc > Build Settings中搜索”Header Search Paths”, 给Debug和Release都加上$(SRCROOT)/ZKLib即可
(不需要把ZKLib拉进源码Xcode工程里哦! )\

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

然后继续⌘+b编译

其它文件缺失
我们会发现相同的报错还有\

'mach-o/dyld_priv.h' file not found
'os/lock_private.h' file not found
'os/base_private.h' file not found
'pthread/tsd_private.h' file not found
'System/machine/cpu_capabilities.h' file not found
'os/tsd.h' file not found
'pthread/spinlock_private.h' file not found
'System/pthread_machdep.h' file not found
'CrashReporterClient.h' file not found
'objc-shared-cache.h' file not found
'_simple.h' file not found
'Block_private.h' file not found

这些报错的解决方案都跟上面的一样处理

objc源码文件代码修改
CrashReporterClient明明放进去了还是”file not found”
打开CrashReporterClient.h文件,

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

在#ifdef前面加上以下代码:

#define LIBC_NO_LIBCRASHREPORTERCLIENT

注释掉一些妨碍编译的代码
总的来说一共有这么多:

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

具体都有:
objc-cache.mm

87行:

#if TARGET_OS_OSX

//#include <cambria traps.h>

//#include <cambria cambria.h>

#endif

1120&#x884C;:

//#if TARGET_OS_OSX

// if (oah_is_current_process_translated()) {

// kern_return_t ret = objc_thread_get_rip(threads[count], (uint64_t*)&pc);

// if (ret != KERN_SUCCESS) {

// pc = PC_SENTINEL;

// }

// } else {

// pc = _get_pc_for_thread (threads[count]);

// }

//#else

pc = _get_pc_for_thread (threads[count]);

//#endif
</cambria></cambria>

NSObject.mm

42&#x884C;:

//#include <os feature_private.h>

extern "C" {

//#include <os reason_private.h>

//#include <os variant_private.h>

}

1185&#x884C;:

// if (DebugPoolAllocation || sdkIsAtLeast(10_12, 10_0, 10_0, 3_0, 2_0)) {

// // OBJC_DEBUG_POOL_ALLOCATION or new SDK. Bad pop is fatal.

// _objc_fatal

// ("Invalid or prematurely-freed autorelease pool %p.", token);

// }
</os></os></os>

objc-runtime.mm

36&#x884C;:

//#include <os feature_private.h> // os_feature_enabled_simple()

379&#x884C;:

// if (!dyld_program_sdk_at_least(dyld_fall_2020_os_versions))

// DisableAutoreleaseCoalescingLRU = true;

444&#x884C;:

// if (!os_feature_enabled_simple(objc4, preoptimizedCaches, true)) {

// DisablePreoptCaches = true;

// }
</os>

objc-class.mm

896&#x884C;:

// LINKER_SET_FOREACH(_dupi, const objc_duplicate_class **, "__objc_dupclass") {

// const objc_duplicate_class *dupi = *_dupi;

//

// if (strcmp(dupi->name, name) == 0) {

// return;

// }

// }

objc-os.mm

31&#x884C;:

//#include "objc-bp-assist.h"

\

567&#x884C;:

// if (!dyld_program_sdk_at_least(dyld_platform_version_macOS_10_13)) {

// DisableInitializeForkSafety = true;

// if (PrintInitializing) {

// _objc_inform("INITIALIZE: disabling +initialize fork "

// "safety enforcement because the app is "

// "too old.)");

// }

// }

objc-runtime-new.mm

3547&#x884C;:

// if (!dyld_program_sdk_at_least(dyld_platform_version_macOS_10_11)) {

// DisableNonpointerIsa = true;

// if (PrintRawIsa) {

// _objc_inform("RAW ISA: disabling non-pointer isa because "

// "the app is too old.");

// }

// }

8328&#x884C;:

if (!DisableTaggedPointerObfuscation /**&& dyld_program_sdk_at_least(dyld_fall_2018_os_versions)*/) {

编译报错 Can’t open order file: libobjc.order
具体来说是这么一个报错:

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

解决方案:
在源码工程 target > objc > Build Settings中搜索”Order File”, 把Debug和Release的内容都改成$(SRCROOT)/libobjc.order即可

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

编译报错 Library not found for -lCrashReporterClient
在源码工程 target > objc > Build Settings中搜索”Other Linker Flags”, 把Debug和Release中的-lCrashReporterClient都删掉

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

编译报错 SDK “macosx.internal” cannot be located.

具体来说是这么一个报错:

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

解决方案:
在源码工程 target > objc > Build Phases中找到Run Script(markgc)里面, 把脚本中的macosx.internal改成macosx即可

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

编译报错 library not found for -loah
具体来说是这么一个报错:

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

解决方案:
在源码工程 target > objc > Build Settings中搜索”Other Linker Flags”, 把Debug和Release里的-loah删掉

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

编译报错 ‘_static_assert’ declared as an array with a negative size
具体来说是这么一个报错:

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

解决方案:
把报错的这两行注释掉~~

编译成功!

诶嘿嘿~
是不是突然间看到”Build Successed”就很兴奋了, 反正我是兴奋了

编译调试
新建一个target
选择macOS里面的Command Line Tool (我们这里不需要界面)

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

将target命名为ZKBuild (如下图)

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

绑定依赖关系
在源码工程 target > ZKBuild > Build Phases中
在Dependencies 添加objc

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

打断点进行调试
切换运行target: ZKBuild > My Mac
在main.m代码中打断点, 并按⌘+r运行

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

此时按

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

(step into)进入这一行代码里面

使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

来到objc源码里面, 则表示已经能成功调试objc源码

Original: https://www.cnblogs.com/mysweetAngleBaby/p/16747152.html
Author: 一眼万年的星空
Title: 使用objc4V818.2源码编译,没有什么比苹果底层源码更有说服力去证明底层原理真假

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/622503/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球