一.前言
本文基于
dandelionYD
大神的libmalloc-166.200.60之源码编译的转载和更新整理,感谢dandelionYD
大神的分享。
说明:工程删除了部分源码,并未知影响,所以该方式仅供参考。
环境说明:macOS10.15.3 Xcode11.2
二.准备工作
libmalloc-283.100.6.tar.gz
xnu-6153.141.1.tar.gz
dyld-732.8.tar.gz
Libc-1353.41.1.tar.gz
libplatform-220
将以上代码分别解压
三.编译修复
1.删除非必要Target
(仅保留libsystem_malloc
)
2.删除非必要文件夹及文件
文件夹:resolver
、tests
、xcodeconfig
、tools
文件:perfdata.framework
、ktrace.framework
3、配置头文件路径
libsystem_malloc
-> Build Settings
-> System Header Search Paths
,添加$(SRCROOT)/include
,后面添加的头文件同时也放进include
中。
4、编译出错过程
问题:'_simple.h' file not found
解决:libplatform
中的_simple.h
添加进include/
问题:'platform/string.h' file not found
解决:libplatform
中的string.h
添加进include/platform/
问题:'platform/compat.h' file not found
解决:libplatform
中的compat.h
添加进include/platform/
问题:'mach-o/dyld_priv.h' file not found
解决:dyld
中的dyld_priv.h
添加进include/mach-o/
问题:'os/internal/internal_shared.h' file not found
解决:libplatform
中的internal_shared.h
添加进include/os/internal/
问题:'os/base_private.h' file not found
解决:libplatform
中的base_private.h
添加进include/os/
问题:'os/internal/atomic.h' file not found
解决:libplatform
中的atomic.h
添加进include/os/internal/
问题:'os/internal/crashlog.h' file not found
解决:libplatform
中的crashlog.h
添加进include/os/
问题:'os/lock_private.h' file not found
解决:libplatform
中的lock_private.h
添加进include/os/
问题:'os/once_private.h' file not found
解决:libplatform
中的once_private.h
添加进include/os/
问题:'os/tsd.h' file not found
解决:libplatform
中的tsd.h
添加进include/os/
问题:'thread_stack_pcs.h' file not found
解决:Libc
中的thread_stack_pcs.h
添加进include/
问题:'machine/cpu_capabilities.h' file not found
解决:xnu
中的machine/cpu_capabilities.h
添加进include/machine/
问题:'resolver.h' file not found
解决:libplatform
中的resolver.h
添加进include/
问题:Use of undeclared identifier '_COMM_PAGE_MEMORY_SIZE'
Use of undeclared identifier '_COMM_PAGE_NCPUS'
解决:添加自定义头文件missingDefines.h
放进include/
附:
missingDefines.h
源码:
#ifndef missingDefines_h
#define missingDefines_h
//( 0x00007fffffe00000ULL )
//( 0x00007fffffe00000 )
#define _COMM_PAGE64_BASE_ADDRESS ( 0x00007fffffe00000ULL )
#define _COMM_PAGE_START_ADDRESS _COMM_PAGE64_BASE_ADDRESS
#define _COMM_PAGE_MEMORY_SIZE (_COMM_PAGE_START_ADDRESS+0x038)
#define _COMM_PAGE_NCPUS (_COMM_PAGE_START_ADDRESS+0x022)
#define _COMM_PAGE_PHYSICAL_CPUS (_COMM_PAGE_START_ADDRESS+0x035)
#define _COMM_PAGE_LOGICAL_CPUS (_COMM_PAGE_START_ADDRESS+0x036)
#define _COMM_PAGE_VERSION (_COMM_PAGE_START_ADDRESS+0x01E) // 16-bit version#
#define NOTE_MEMORYSTATUS_PRESSURE_WARN 0x00000002 /* system memory pressure has changed to the warning state */
#define NOTE_MEMORYSTATUS_PROC_LIMIT_WARN 0x00000010 /* process memory limit has hit a warning state */
#define NOTE_MEMORYSTATUS_MSL_STATUS 0xf0000000 /* bits used to request change to process MSL status */
#define NOTE_MEMORYSTATUS_PROC_LIMIT_CRITICAL 0x00000020 /* process memory limit has hit a critical state - soft limit */
#define NOTE_MEMORYSTATUS_PRESSURE_CRITICAL 0x00000004 /* system memory pressure has changed to the critical state */
#endif /* missingDefines_h */
问题:'resolver_internal.h' file not found
解决:libplatform
中的resolver_internal.h
添加进include/
问题:'os/internal.h' file not found
解决:libplatform
中的internal.h
添加进include/os/
问题:'os/semaphore_private.h' file not found
解决:libplatform
中的semaphore_private.h.h
添加进include/os/
问题:'yield.h' file not found
解决:libplatform
中的yield.h
添加进include/
问题:Use of undeclared identifier '_COMM_PAGE_START_ADDRESS'
Use of undeclared identifier '_COMM_PAGE64_BASE_ADDRESS'
Use of undeclared identifier '_COMM_PAGE_PHYSICAL_CPUS'
Use of undeclared identifier '_COMM_PAGE_LOGICAL_CPUS'
Use of undeclared identifier '_COMM_PAGE_VERSION'
解决:参考missingDefines.h
问题:_nanov2_configure
、_nanov2_create_zone
、_nanov2_forked_zone
、_nanov2_init
错误
解决:删除nanov2_malloc.c
、nanov2_malloc.h
、nanov2_zone.h
,并将所有关于nanov2
的缺失的.h文件都注释掉,所有判断nanov2的源码都改为nanov的源码。(存在风险)
至此,编译顺利完成。
四.新建调试Target
1.macOS -> Command Line Tool -> mallocTest
2.添加Build Phasses
->Dependencies
->libsystem_malloc
添加Build Phasses
->Link Binary With Libraries
->.dylib
3.在main.m
文件中编写测试代码。