objc4-818.2

源码下载地址

Apple开发源码:根据对应的版本找到相应的源码,例如: macos --> 10.15 -->搜索 objc4

MacOS 10.15源码:直接搜索想要下载的源码名称,例如:搜索objc --> objc4/ -->选择相应的objc版本下载

依赖资源文件

编译object4的时候需要添加一些依赖文件,这些依赖文件通过以下源码查找(文件名“-”后面的的是版本号):

dyld-733.6
libauto-187
Libc-1353.41.1
libclosure-74
libdispatch-1173.40.5
libplatform-220
libpthread-416.40.3
xnu-6153.11.26

源码编译

1. unable to find sdk 'macosx.internal'

选择`target ->objc -> Build Setting -> Base SDK - > macOS
选择`target ->objc-trampolines -> Build Setting -> Base SDK - > macOS

2. 'sys/reason.h' file not found

  • 从下载的依赖资源文件中找reason.hreason.h文件的目录xnu/bsd/sys/reason.h
  • objc4目录下新建 include文件,将reason.h文件放到include/sys文件夹下
  • 设置reason.h路径,target -> objc -> Build Settings - > Header Search Paths中 添加$(SRCROOT)/include

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

  • include文件中创建mach-o文件
  • 在下载的依赖资源文件中找到 dyld -> include -> mach-o -> dyld_priv.h
  • 拷贝到 include/mach-o文件夹下
  • 打开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添加宏定义

4. 'os/lock_private.h' file not found

  • 在下载的资源文件libplatform ->private -> os ->lock_private.h. base_private.h两个文件,拷贝到include/os

5. 'pthread/tsd_private.h' file not found'pthread/spinlock_private.h' file not found

  • 在下载的资源文件libpthread ->private ->tsd_private.h. spinlock_private.h两个文件,拷贝到include/pthread

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

  • 在下载的资源文件xnu --> osfmk --> machine --> cpu_capabilities.h文件,拷贝到include/System/machine

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

  • 在下载的资源文件xnu --> libsyscall --> os --> tsd.h文件,拷贝到新建的include/os

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

  • 在下载的资源文件Libc --> pthreads --> pthread_machdep.h文件,拷贝到include/System

9. 'CrashReporterClient.h' file not found

  • Libc-825.24 按路径Libc/include/CrashReporterClient.h找到对应文件导入include目录下
  • target -> Build Setting -> Preprocessor Macros中加入LIBC_NO_LIBCRASHREPORTERCLIENT或者在CrashReporterClient.h文件中更改了里面的宏信息 #define LIBC_NO_LIBCRASHREPORTERCLIENT

10. 'objc-shared-cache.h' file not found

  • 在下载的资源文件dyld --> include --> objc-shared-cache.h文件,拷贝到include目录下中

11. Mismatch in debug-ness macros

  • objc-runtime.mm#error mismatch in debug-ness macros注释掉

12. '_simple.h' file not found

  • 在下载的资源文件libplatform --> private --> _simple.h文件,拷贝到include目录下中

13. 'Block_private.h' file not found

  • 在下载的资源文件llibclosure-74 --> Block_private.h文件,拷贝到include目录下中

14. 'kern/restartable.h' file not found

  • 在下载的资源文件xnu --> osfmk --> kern --> restartable.h文件,拷贝到新建的include/kern

15. can't open order file: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/AppleInternal/OrderFiles/libobjc.order

  • target -> objc -> Build Setting -> Order File中添加搜索路径$(SRCROOT)/libobjc.order

16. /xcodebuild:1:1: SDK "macosx.internal" cannot be located.

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

17. library not found for -lCrashReporterClient

  • target -> objc -> Build Settings -> Other -> Linker Flags -> Debug/Release -> Any macOS SDK中删除-lCrashReporterClient

18. Static_assert failed due to requirement 'bucketsMask >= ((unsigned long long)140737454800896ULL)' "Bucket field doesn't have enough bits for arbitrary pointers."

MACH_VM_MAX_ADDRESS` 在arm64架构下对应的是: `0x1000000000
  • objc-runtime-new.h 中将其替换:static_assert(bucketsMask >= 0x1000000000, "Bucket field doesn't have enough bits for arbitrary pointers."); 直接注释掉这行也可以
  • objc-runtime-new.mm中注释掉: STATIC_ASSERT((~ISA_MASK & MACH_VM_MAX_ADDRESS) == 0 || ISA_MASK + sizeof(void*) == MACH_VM_MAX_ADDRESS);

19. libobjc.order 路径问题

Can't open order file: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/AppleInternal/OrderFiles/libobjc.order
  • 选择 target -> objc -> Build Settings
  • 在工程的 Order File 中添加搜索路径 $(SRCROOT)/libobjc.order

20.其他

其他相关报错,直接注释掉代码

以下是举例,但不是全部

#include <os/feature_private.h> 
    if (!os_feature_enabled_simple(objc4, preoptimizedCaches, true)) {
        DisablePreoptCaches = true;
    }
    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.)");
        }
    }

20.3 objc-cache.mm类

#include <Cambria/Traps.h>
#include <Cambria/Cambria.h>
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]);
        }

改为:

//        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]);
//        }

编译调试

  • 新建一个 Target : objc-debug

[![img](http://blog.guohuaden.com/objc-debug-target.png

img

  • 绑定二进制依赖关系

依赖关系

  • 运行代码进入源码

runtime调试