iOS objc-838.1 编译流程

163 阅读4分钟

编译环境

  • macOS Monterey (12.3.1)
  • Xcode 13.2.1 (13C100)

编译问题

1、unable to find sdk 'macosx.internal'
  1. PROJECT -> objc -> Build Settings -> Base SDK设置的macosx.internal更改成macOS

image.png 2. Targets -> objc -> Build Phases -> Run Script(markgc)下的Shall脚本中的macosx.internal更改成macosx

2、sys/reason.h' file not found
  • objc源码根目录下新建common/文件夹,并创建子文件夹sys.
  • Targets -> objc -> Build Setting -> Header Search Paths配置common路径 image.png
  • 下载xnu-8019.80.24源码,在/bsd/sys/目录下找到reason.h文件
  • reason.h复制到自己创建的sys/文件夹下 image.png

image.png

3、'mach-o/dyld_priv.h' file not found

*common目录下创建子文件夹mach-o

  • 下载dyld-941.5源码,在dyld-941.5/include/mach-o/目录下找到dyld_priv.h文件
  • dyld_priv.h复制粘贴到自己创建的common/mach-o/目录下

image.png

3.1、dyld_priv.h文件中报错Expected ','

  • 删除dyld_priv.h文件中所有的报错处的bridgeos(3.0)

image.png

4、'os/lock_private.h' file not found
  • common目录下创建子文件夹os
  • libplatform-273.40.1源码,在/private/os/目录下找到lock_private.h文件
  • lock_private.h复制粘贴到自己创建的common/os/目录下

image.png

4.1、lock_private.h文件中报错'os/base_private.h' file not found

  • 该文件位于xnu-8019.80.24/libkern/os/目录下
  • base_private.h文件复制粘贴到自己创建的common/os/目录下

image.png

4.2、lock_private.h文件中报错Expected ','

  • 同上删除文件中报错位置的bridgeos(4.0)即可 image.png
5、'pthread/tsd_private.h' file not found
  • common/目录下创建子文件夹pthread
  • libpthread-485.60.2源码,在libpthread/private/pthread/目录下找到tsd_private.h文件
  • tsd_private.h文件复制粘贴到自己创建的common/pthread/目录下

image.png

6、'System/machine/cpu_capabilities.h' file not found
  • common目录下新建子文件夹System,然后在System目录下在创建子文件夹machine
  • 该文件位于xnu-8019.80.24/osfmk/machine/目录下
  • cpu_capabilities.h文件复制粘贴到自己创建的common/System/目录下

image.png

7、'os/tsd.h' file not found
  • 该文件位于xnu-8019.80.24/libsyscall/os/目录下
  • tsd.h文件复制粘贴到自己创建的common/os/目录下

image.png

8、'pthread/spinlock_private.h' file not found
  • 该文件位于libpthread-485.60.2/private/pthread/目录下
  • spinlock_private.h文件复制粘贴到自己创建的common/pthread/目录下

image.png

9、'System/pthread_machdep.h' file not found
  • 下载Libc-825.40.1源码,在pthreads/目录下找到pthread_machdep.h文件
  • pthread_machdep.h文件复制粘贴到自己创建的common/System/目录下

image.png

10、'CrashReporterClient.h' file not found

解决方案如下:

  1. 找到CrashReporterClient.h文件
  • 该文件位于Libc-825.40.1/include/目录下
  • CrashReporterClient.h文件复制粘贴到common/目录下
  1. CrashReporterClient.h文件中添加宏定义
#define LIBC_NO_LIBCRASHREPORTERCLIENT
  1. Targers -> objc -> Build Settings -> Preprocessor Macros中添加LIBC_NO_LIBCRASHREPORTERCLIENT

image.png

  1. Targers -> objc -> Build Settings -> Other Linker Flags -> Debug中 删除-lCrashReporterClient image.png
11、pthread_machdep.h中四处报错如下图:

image.png 解决方案: 将报错代码全部注释掉即可

12、'os/feature_private.h' file not found
  • 直接注释掉该头文件
13、 Use of undeclared identifier 'dyld_fall_2020_os_versions'
  • 注释掉报错相关的两行代码

image.png

14、Use of undeclared identifier 'dyld_platform_version_macOS_10_13'
  • 注释掉报错代码块

image.png

15、Use of undeclared identifier 'objc4'
  • 注释掉报错代码块

image.png

16、Use of undeclared identifier 'dyld_platform_version_macOS_10_11'
  • 注释掉&& !dyld_program_sdk_at_least(dyld_platform_version_macOS_10_11)条件判断

image.png

17、'objc-shared-cache.h' file not found
  • 该文件位于dyld-941.5/include/目录下
  • objc-shared-cache.h文件复制粘贴到common/目录下

image.png

18、Use of undeclared identifier 'dyld_fall_2018_os_versions'
  • 注释掉&& dyld_program_sdk_at_least(dyld_fall_2018_os_versions)条件判断

image.png

19、'_simple.h' file not found
  • 该文件位于libplatform-273.40.1/private/目录下
  • _simple.h文件复制粘贴到自己创建的common/目录下

image.png

20、'os/linker_set.h' file not found
  • 该文件位于xnu-8019.80.24/bsd/sys/目录下
  • linker_set.h文件复制粘贴到自己创建的common/os/目录下

image.png

21、'Cambria/Traps.h' file not found
  • 注释掉下图两行代码

image.png

22、'Block_private.h' file not found
  • 下载libclosure-79源码,在其目录下找到Block_private.h文件
  • Block_private.h文件复制粘贴到自己创建的common/目录下

image.png

23、'kern/restartable.h' file not found
  • 该文件位于xnu-8019.80.24/osfmk/kern/目录下 *common/目录下新建子文件夹kern
  • restartable.h文件复制粘贴到自己创建的common/kern/目录下

image.png

24、objc-cache.mm文件报错
  • Use of undeclared identifier 'oah_is_current_process_translated'Use of undeclared identifier 'objc_thread_get_rip'报错
  • 注释掉下图代码块

image.png

25、NSObject.mm文件报错
  1. 'os/feature_private.h' file not found
  • 注释掉该头文件即可
  1. 'os/reason_private.h' file not found
  • 该文件位于xnu-8019.80.24/libkern/os/目录下,
  • reason_private.h文件复制粘贴到自己创建的common/os/目录下

image.png

3.'os/variant_private.h' file not found

  • 该文件位于Libc-1506.40.4/os/目录下
  • variant_private.h文件复制粘贴到自己创建的common/os/目录下

image.png

  1. se of undeclared identifier 'dyld_platform_version_bridgeOS_2_0'
  • 注释掉该代码块即可

image.png

26、variant_private.h 文件中4处报Expected ','问题
  • 同上面的处理方法一样 删掉bridgeos参数即可

image.png

27、'_static_assert' declared as an array with a negative size
  • 注释掉截图两行即可

image.png

28、library not found for -loah
  • Targers -> objc -> Build Settings -> Other Linker Flags -> Debug中 删除-loah

image.png

Build Succeeded

  • 到此为止 objc源码终于编译成功

编译调试

  1. 新建一个macOS commandLine Target,AJObjcBuild

image.png

  1. 绑定二进制依赖关系以及添加依赖库

image.png

  1. 编译AJObjcBuild
  • 此时编译完成 断点也可以成功进入

image.png

源码断点问题

  1. 在objc内部源码打断点时发现断点不走
  • Targers -> AJObjcBuild -> Build Settings -> Singing -> Enable Hardened Runtime中的值设置为NO即可

image.png

  1. 大功告成 断点成功断到我们想要的地方

image.png

遇到的问题

  • 刚开始编译时 其他都可以在修改Enable Hardened RuntimeNO时一直崩溃,后续升级系统版本为macOC Monterey 才可以正常调试, 所以objc-838.1需要在macOC Monterey运行

Demo

  • 最后附上编译好的objc4-838.1Demo
  • 希望能对底层探索的你有所帮助