xref: /xnu-8019.80.24/makedefs/MakeInc.def (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
1# -*- mode: makefile;-*-
2#
3# Copyright (C) 1999-2021 Apple Inc. All rights reserved.
4#
5# MakeInc.def contains global definitions for building,
6# linking, and installing files.
7#
8
9#
10# Architecture Configuration options
11#
12SUPPORTED_ARCH_CONFIGS := X86_64 X86_64H ARM ARM64
13
14#
15# Kernel Configuration options
16#
17SUPPORTED_KERNEL_CONFIGS = RELEASE DEVELOPMENT DEBUG PROFILE KASAN
18
19
20#
21# Machine Configuration options
22#
23
24SUPPORTED_X86_64_MACHINE_CONFIGS = NONE
25SUPPORTED_X86_64H_MACHINE_CONFIGS = NONE
26
27ifneq ($(findstring _Sim,$(RC_ProjectName)),)
28SUPPORTED_ARM_MACHINE_CONFIGS = NONE
29SUPPORTED_ARM64_MACHINE_CONFIGS = NONE
30else ifneq ($(findstring _host,$(RC_ProjectName)),)
31SUPPORTED_ARM_MACHINE_CONFIGS = NONE
32SUPPORTED_ARM64_MACHINE_CONFIGS = NONE
33else
34SUPPORTED_ARM_MACHINE_CONFIGS = NONE
35SUPPORTED_ARM64_MACHINE_CONFIGS = BCM2837 T6000 T8101 VMAPPLE
36
37endif
38
39#
40# Setup up *_LC variables during recursive invocations
41#
42
43ifndef CURRENT_ARCH_CONFIG_LC
44	export CURRENT_ARCH_CONFIG_LC	:= $(shell printf "%s" "$(CURRENT_ARCH_CONFIG)" | $(TR) A-Z a-z)
45endif
46
47ifndef CURRENT_KERNEL_CONFIG_LC
48	export CURRENT_KERNEL_CONFIG_LC := $(shell printf "%s" "$(CURRENT_KERNEL_CONFIG)" | $(TR) A-Z a-z)
49endif
50
51ifndef CURRENT_MACHINE_CONFIG_LC
52	export CURRENT_MACHINE_CONFIG_LC := $(shell printf "%s" "$(CURRENT_MACHINE_CONFIG)" | $(TR) A-Z a-z)
53endif
54
55#
56# Component List
57#
58COMPONENT_LIST	= osfmk bsd libkern iokit pexpert libsa security san
59COMPONENT	= $(if $(word 2,$(subst /, ,$(RELATIVE_SOURCE_PATH))),$(word 2,$(subst /, ,$(RELATIVE_SOURCE_PATH))),$(firstword $(subst /, ,$(RELATIVE_SOURCE_PATH))))
60COMPONENT_IMPORT_LIST = $(filter-out $(COMPONENT),$(COMPONENT_LIST))
61
62MACHINE_FLAGS_ARM64_T8101 = -DARM64_BOARD_CONFIG_T8101_T8103			-mcpu=apple-a14
63MACHINE_FLAGS_ARM64_T6000 = -DARM64_BOARD_CONFIG_T6000 			-mcpu=apple-a14
64MACHINE_FLAGS_ARM64_VMAPPLE = -DARM64_BOARD_CONFIG_VMAPPLE              -march=armv8.5a
65MACHINE_FLAGS_ARM64_BCM2837 = -DARM64_BOARD_CONFIG_BCM2837
66
67
68#
69# Deployment target flag
70#
71ifeq ($(PLATFORM),MacOSX)
72    DEPLOYMENT_TARGET_FLAGS = -mmacosx-version-min=$(SDKVERSION) -DXNU_TARGET_OS_OSX
73    DEPLOYMENT_LINKER_FLAGS = -Wl,-macosx_version_min,$(SDKVERSION)
74else ifeq ($(PLATFORM),DriverKit)
75    DEPLOYMENT_TARGET_FLAGS = -target apple-driverkit$(SDKVERSION) -DXNU_TARGET_OS_OSX
76    DEPLOYMENT_LINKER_FLAGS = -Wl,-target,apple-driverkit$(SDKVERSION)
77else ifeq ($(PLATFORM),WatchOS)
78    DEPLOYMENT_TARGET_FLAGS = -mwatchos-version-min=$(SDKVERSION) -DXNU_TARGET_OS_WATCH
79    DEPLOYMENT_LINKER_FLAGS =
80else ifeq ($(PLATFORM),tvOS)
81    DEPLOYMENT_TARGET_FLAGS = -mtvos-version-min=$(SDKVERSION) -DXNU_TARGET_OS_TV
82    DEPLOYMENT_LINKER_FLAGS =
83else ifeq ($(PLATFORM),AppleTVOS)
84    DEPLOYMENT_TARGET_FLAGS = -mtvos-version-min=$(SDKVERSION) -DXNU_TARGET_OS_TV
85else ifeq ($(PLATFORM),BridgeOS)
86    DEPLOYMENT_TARGET_FLAGS = -mbridgeos-version-min=$(SDKVERSION) -DXNU_TARGET_OS_BRIDGE
87    DEPLOYMENT_LINKER_FLAGS =
88else ifneq ($(filter $(SUPPORTED_EMBEDDED_PLATFORMS),$(PLATFORM)),)
89    DEPLOYMENT_TARGET_FLAGS = -miphoneos-version-min=$(SDKVERSION) -DXNU_TARGET_OS_IOS
90    DEPLOYMENT_LINKER_FLAGS = -Wl,-ios_version_min,$(SDKVERSION)
91else ifneq ($(filter $(SUPPORTED_SIMULATOR_PLATFORMS),$(PLATFORM)),)
92    DEPLOYMENT_TARGET_FLAGS =
93    DEPLOYMENT_LINKER_FLAGS =
94else
95    DEPLOYMENT_TARGET_FLAGS =
96    DEPLOYMENT_LINKER_FLAGS =
97endif
98
99DEPLOYMENT_TARGET_DEFINES = -DPLATFORM_$(PLATFORM)
100
101
102ifneq ($(RC_ENABLE_PRODUCT_INFO_FILTER),)
103SEED_DEFINES += -DRC_ENABLE_XNU_PRODUCT_INFO_FILTER
104else
105SEED_DEFINES += -URC_ENABLE_XNU_PRODUCT_INFO_FILTER
106endif
107
108#
109# Standard defines list
110#
111DEFINES = -DAPPLE -DKERNEL -DKERNEL_PRIVATE -DXNU_KERNEL_PRIVATE \
112	-DPRIVATE -D__MACHO__=1 -Dvolatile=__volatile -DXNU_KERN_EVENT_DATA_IS_VLA \
113	-DCURRENT_MACHINE_CONFIG_LC=$(CURRENT_MACHINE_CONFIG_LC) \
114	$(CONFIG_DEFINES) $(SEED_DEFINES)
115
116# Enable caching with `make CCACHE=ccache`
117# This intentionally does not override $(CC) because that will confuse
118# utilities like mig.
119CCACHE ?=
120
121#
122# Compiler command
123#
124KCC  = $(CCACHE) $(CC)
125KC++ = $(CCACHE) $(CXX)
126
127GENASSYM_KCC = $(CCACHE) $(CC)
128
129#
130# Compiler warning flags
131#
132
133USE_WERROR := 1
134ifneq ($(BUILD_WERROR),)
135USE_WERROR := $(BUILD_WERROR)
136endif
137
138ifeq ($(USE_WERROR),1)
139WERROR := -Werror
140endif
141
142# Shared C/C++ warning flags
143# NOTE: order matters here.  -Wno-xxx goes before opt-in of ones we want
144WARNFLAGS_STD := \
145	-Weverything \
146	-Wundef-prefix=TARGET_OS_ \
147	-Wno-pedantic \
148	$(WERROR) \
149	-Wno-bad-function-cast \
150	-Wno-c++-compat \
151	-Wno-c++98-compat \
152	-Wno-conditional-uninitialized \
153	-Wno-covered-switch-default \
154	-Wno-disabled-macro-expansion \
155	-Wno-documentation-unknown-command \
156	-Wno-extra-semi-stmt \
157	-Wno-format-non-iso \
158	-Wno-format-nonliteral \
159	-Wno-language-extension-token \
160	-Wno-missing-variable-declarations \
161	-Wno-packed \
162	-Wno-padded \
163	-Wno-partial-availability \
164	-Wno-reserved-id-macro \
165	-Wno-shift-sign-overflow \
166	-Wno-switch-enum \
167	-Wno-undef \
168	-Wno-unused-macros \
169	-Wno-used-but-marked-unused \
170	-Wno-variadic-macros \
171	-Wno-vla \
172	-Wno-zero-length-array
173
174# When a new clang has new warnings disable them here until the kernel is fixed.
175WARNFLAGS_STD := $(WARNFLAGS_STD) \
176	-Wno-unknown-warning-option \
177	-Wno-anon-enum-enum-conversion \
178	-Wno-error=enum-enum-conversion \
179	-Wno-error=c99-designator \
180	-Wno-error=reorder-init-list
181
182# Hand-written sign conversion diagnostics are resolved, but the
183# auto-generated ones need mig and iig to be updated to fix.  Disable the
184# diagnostic here until we've completed that:
185WARNFLAGS_STD := $(WARNFLAGS_STD) \
186	-Wno-sign-compare \
187	-Wno-sign-conversion
188
189# Opt-ins:
190WARNFLAGS_STD := $(WARNFLAGS_STD) \
191	-Wpointer-arith
192
193CWARNFLAGS_STD = \
194	$(WARNFLAGS_STD)
195
196
197
198# Can be overridden in Makefile.template or Makefile.$arch
199export CWARNFLAGS ?= $(CWARNFLAGS_STD)
200
201define add_perfile_cflags
202$(1)_CWARNFLAGS_ADD += $2
203endef
204
205define rm_perfile_cflags
206$(1)_CFLAGS_RM += $2
207endef
208
209CXXWARNFLAGS_STD = \
210	$(WARNFLAGS_STD) \
211	-Wno-c++98-compat-pedantic \
212	-Wno-exit-time-destructors \
213	-Wno-global-constructors \
214	-Wno-old-style-cast
215
216# Can be overridden in Makefile.template or Makefile.$arch
217export CXXWARNFLAGS ?= $(CXXWARNFLAGS_STD)
218
219define add_perfile_cxxflags
220$(1)_CXXWARNFLAGS_ADD += $2
221endef
222
223#
224# Default ARCH_FLAGS, for use with compiler/linker/assembler/mig drivers
225
226ARCH_FLAGS_X86_64	  = -arch x86_64
227ARCH_FLAGS_X86_64H	  = -arch x86_64h
228
229ifeq ($(RC_ProjectName),xnu_libraries)
230WILL_BUILD_STATIC_KC := 1
231BUILD_STATIC_LINK := 1
232BUILD_XNU_LIBRARY := 1
233RC_NONARCH_CFLAGS += -D__BUILDING_XNU_LIBRARY__=1
234endif
235
236ifneq ($(filter ARM ARM64,$(CURRENT_ARCH_CONFIG)),)
237
238ifneq ($(findstring _Sim,$(RC_ProjectName)),)
239ARCH_FLAGS_ARM64          = -arch arm64e
240else ifneq ($(findstring _host,$(RC_ProjectName)),)
241ARCH_FLAGS_ARM64          = -arch arm64e
242else
243
244ifndef ARCH_STRING_FOR_CURRENT_MACHINE_CONFIG
245
246ifneq ($(EMBEDDED_DEVICE_MAP),)
247export ARCH_STRING_FOR_CURRENT_MACHINE_CONFIG := $(shell $(EMBEDDED_DEVICE_MAP) -db $(EDM_DBPATH) -query SELECT DISTINCT KernelMachOArchitecture FROM Targets WHERE KernelPlatform IS \"$(CURRENT_MACHINE_CONFIG_LC)\" LIMIT 1 || echo UNKNOWN )
248else
249# Without embdedded device map, use a default arch string
250export ARCH_STRING_FOR_CURRENT_MACHINE_CONFIG := $(shell echo $(CURRENT_ARCH_CONFIG) | tr A-Z a-z)
251ifneq ($(filter ARM64,$(CURRENT_ARCH_CONFIG)),)
252export ARCH_STRING_FOR_CURRENT_MACHINE_CONFIG := arm64e
253endif
254endif
255endif
256
257#
258# This can have false negatives, and is used to avoid calling CTF when we'll build a static KC
259#
260ifndef WILL_BUILD_STATIC_KC
261ifneq ($(EMBEDDED_DEVICE_MAP),)
262export WILL_BUILD_STATIC_KC := $(shell $(EMBEDDED_DEVICE_MAP) -db $(EDM_DBPATH)	\
263				-query 'SELECT COUNT(*) != 0 FROM Targets WHERE KernelPlatform IS "$(CURRENT_MACHINE_CONFIG_LC)" \
264					AND (KernelMachOArchitecture LIKE "arm64e" OR ProductType LIKE "iphone10,%")')
265else
266export WILL_BUILD_STATIC_KC := 0
267endif
268endif
269
270BUILD_STATIC_LINK := 1
271
272ARCH_FLAGS_ARM		  = -arch $(ARCH_STRING_FOR_CURRENT_MACHINE_CONFIG)
273ARCH_FLAGS_ARM64	  = -arch $(ARCH_STRING_FOR_CURRENT_MACHINE_CONFIG)
274
275endif
276
277else
278# non arm machine config string
279ifndef ARCH_STRING_FOR_CURRENT_MACHINE_CONFIG
280export ARCH_STRING_FOR_CURRENT_MACHINE_CONFIG := $(shell echo $(CURRENT_ARCH_CONFIG) | tr A-Z a-z)
281endif
282
283endif
284
285#
286# Default CFLAGS
287#
288ifdef RC_NONARCH_CFLAGS
289OTHER_CFLAGS = $(RC_NONARCH_CFLAGS)
290endif
291
292#
293# Debug info
294#
295DSYMINFODIR	= Contents
296DSYMKGMACROSDIR	= Contents/Resources
297DSYMLLDBMACROSDIR = Contents/Resources/Python
298DSYMDWARFDIR	= Contents/Resources/DWARF
299
300DEBUG_CFLAGS := -g
301BUILD_DSYM := 1
302
303#
304# We must not use -fno-keep-inline-functions, or it will remove the dtrace
305# probes from the kernel.
306#
307CFLAGS_GEN = $(DEBUG_CFLAGS) -nostdinc \
308	-ferror-limit=10000 \
309	-fno-builtin \
310	-fno-common \
311	-ftrivial-auto-var-init=zero \
312	-enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang \
313	-fsigned-bitfields \
314	-fmerge-all-constants \
315	-fno-c++-static-destructors \
316	$(OTHER_CFLAGS)
317
318CFLAGS_RELEASE	=
319CFLAGS_DEVELOPMENT	=
320CFLAGS_DEBUG	=
321CFLAGS_KASAN = $(CFLAGS_DEVELOPMENT)
322CFLAGS_PROFILE	=  -pg
323
324CFLAGS_X86_64	= -Dx86_64 -DX86_64 -D__X86_64__ -DLP64 \
325				-DPAGE_SIZE_FIXED -mkernel -msoft-float
326
327CFLAGS_X86_64H = $(CFLAGS_X86_64)
328
329CFLAGS_ARM	= -Darm -DARM -D__ARM__ -DPAGE_SIZE_FIXED \
330			-momit-leaf-frame-pointer -fno-strict-aliasing -D__API__=v4
331
332LARGE_MEMORY_DEFINE=-UARM_LARGE_MEMORY
333ARM64_PLKSEG_ADDR  =0xfffffff004004000
334ARM64_LINK_ADDR    =0xfffffff007004000
335
336# Use ARM_LARGE_MEMORY config for all MacOSX targets.
337ifneq ($(filter $(PLATFORM),MacOSX),)
338LARGE_MEMORY_DEFINE=-DARM_LARGE_MEMORY=1
339ARM64_PLKSEG_ADDR  =0xfffffe0004004000
340ARM64_LINK_ADDR    =0xfffffe0007004000
341endif
342
343
344CFLAGS_ARM64	= -Darm64 -DARM64 -D__ARM64__ -DLP64 -DPAGE_SIZE_FIXED -DVM_KERNEL_LINK_ADDRESS=$(ARM64_LINK_ADDR) \
345			$(LARGE_MEMORY_DEFINE) -momit-leaf-frame-pointer -fno-strict-aliasing -D__API__=v4 -mkernel
346
347CFLAGS_RELEASEX86_64 = -O2
348CFLAGS_DEVELOPMENTX86_64 = -O2
349CFLAGS_KASANX86_64 = $(CFLAGS_DEVELOPMENTX86_64)
350# No space optimization for the DEBUG kernel for the benefit of gdb:
351CFLAGS_DEBUGX86_64 = -O0
352CFLAGS_PROFILEX86_64 = -O2
353
354CFLAGS_RELEASEX86_64H = -O2
355CFLAGS_DEVELOPMENTX86_64H = -O2
356CFLAGS_KASANX86_64H = $(CFLAGS_DEVELOPMENTX86_64H)
357# No space optimization for the DEBUG kernel for the benefit of gdb:
358CFLAGS_DEBUGX86_64H = -O0
359CFLAGS_PROFILEX86_64H = -O2
360
361CFLAGS_RELEASEARM = -O2
362CFLAGS_DEVELOPMENTARM = -O2
363CFLAGS_DEBUGARM = -O0
364CFLAGS_PROFILEARM = -O2
365
366CFLAGS_RELEASEARM64 = -O2
367CFLAGS_DEVELOPMENTARM64 = -O2
368CFLAGS_KASANARM64 = $(CFLAGS_DEVELOPMENTARM64)
369CFLAGS_DEBUGARM64 = -O0
370CFLAGS_PROFILEARM64 = -O2
371
372#
373# bound-checking support
374#
375# BOUND_CHECKS=0 disables, else support is dynamically detected
376#
377### ifndef BOUND_CHECKS
378### ifeq ($(shell $(CC) -E -fbounds-attributes /dev/null 2>/dev/null && echo 1),1)
379###     export BOUND_CHECKS := 1
380### else
381###     export BOUND_CHECKS := 0
382### endif
383### endif # ifndef BOUND_CHECKS
384ifeq ($(BOUND_CHECKS),1)
385CFLAGS_BOUND_CHECKS = -DXNU_BOUND_CHECKS=1 -fbounds-attributes
386ifneq ($(shell $(CC) -E -fbounds-attributes /dev/null 2>/dev/null && echo 1),1)
387    $(error "your toolchain doesn't support bound checking")
388endif
389else
390CFLAGS_BOUND_CHECKS = -UXNU_BOUND_CHECKS
391endif
392
393#
394# Sanitizers Support (KASan, UBSan)
395#
396
397# Which kernel configurations are built with KCOV enabled.
398KCOV_RUNTIME := KASAN
399
400ifneq ($(filter RELEASE, $(KCOV_RUNTIME)),)
401$(error "Sanitizer runtime should not be eabled for RELEASE kernel.")
402endif
403
404
405SAN=0
406
407# KASan support
408#
409
410ifeq ($(CURRENT_KERNEL_CONFIG),KASAN)
411# KASan kernel config implicitly enables the KASan instrumentation.
412# Instrumentation for other sanitizers is enabled explicitly at build time.
413KASAN = 1
414endif
415
416ifeq ($(KASAN),1)
417SAN=1
418BUILD_LTO=0
419
420# KASAN_CLASSIC is currently the default
421KASAN_CLASSIC=1
422
423KASAN_BLACKLIST=$(OBJROOT)/san/kasan-blacklist-$(CURRENT_ARCH_CONFIG_LC)
424
425# KASAN supports two different runtime models, KASAN_CLASSIC and KASAN_TBI.
426# Shadow map scale differs between the two. With KASAN_SCALE=N, the shadow memory consumes 1/2^N of
427# the virtual address space.
428ifeq ($(KASAN_TBI), 1)
429KASAN_SCALE=4
430KASAN_OFFSET_ARM64=0xf000000000000000
431KASAN_OFFSET=$(KASAN_OFFSET_ARM64)
432CFLAGS_KASAN_TBI = -fsanitize=kernel-hwaddress \
433        -fsanitize-blacklist=$(KASAN_BLACKLIST) \
434        -mllvm -hwasan-recover=0 \
435        -mllvm -hwasan-mapping-offset=$(KASAN_OFFSET) \
436        -mllvm -hwasan-instrument-atomics=0 \
437        -mllvm -hwasan-instrument-stack=0 \
438	-mllvm -hwasan-uar-retag-to-zero=1 \
439        -mllvm -hwasan-generate-tags-with-calls=1 \
440        -mllvm -hwasan-instrument-with-calls=0 \
441        -mllvm -hwasan-memory-access-callback-prefix="__asan_"
442
443CFLAGS_KASAN_MODEL_TAG=-DKASAN_TBI=1
444CFLAGS_KASAN_MODEL=$(CFLAGS_KASAN_TBI)
445else ifeq ($(KASAN_CLASSIC), 1)
446KASAN_SCALE=3
447KASAN_OFFSET_ARM64=0xe000000000000000
448# To calculate the kasan offset, subtract the lowest KVA to sanitize, shifted right by 3 bits,
449# from the base address of the kasan shadow area, (e.g. solve the following equation:
450# OFFSET = {VA mapped by the first KASAN PML4 [Currently #494]} - (LOWEST_KVA >> 3)
451# OFFSET = (0ULL - (512GiB * (512 - 494))) - (LOWEST_SAN_KVA >> 3)
452# OFFSET = FFFFF70000000000 - ((0ULL - (512GiB * (512 - 496))) >> 3) [PML4 #496 is the first possible KVA]
453# OFFSET = FFFFF70000000000 - (FFFFF80000000000 >> 3)
454# OFFSET = DFFFF80000000000
455# ).
456KASAN_OFFSET_X86_64=0xdffff80000000000
457KASAN_OFFSET_X86_64H=$(KASAN_OFFSET_X86_64)
458KASAN_OFFSET=$($(addsuffix $(CURRENT_ARCH_CONFIG),KASAN_OFFSET_))
459
460CFLAGS_KASAN_CLASSIC= -fsanitize=address \
461                -mllvm -asan-globals-live-support \
462                -mllvm -asan-mapping-offset=$(KASAN_OFFSET) \
463                -fsanitize-blacklist=$(KASAN_BLACKLIST)
464
465CFLAGS_KASAN_MODEL_TAG = -DKASAN_CLASSIC=1
466CFLAGS_KASAN_MODEL = $(CFLAGS_KASAN_CLASSIC)
467
468else
469$(error No KASAN model specified)
470endif
471
472CFLAGS_GEN += -DKASAN=1 -DKASAN_OFFSET=$(KASAN_OFFSET) -DKASAN_SCALE=$(KASAN_SCALE) \
473	$(CFLAGS_KASAN_MODEL) $(CFLAGS_KASAN_MODEL_TAG)
474
475endif
476
477# UBSan
478#
479# The Undefined Behavior sanitizer runtime is always built as part of, and only for,
480# KASAN variants. UBSan instrumentation is disabled by default and only enabled explicitly
481# when building with UBSAN=1.
482#
483# On iOS RELEASE and DEVELOPMENT kernels, a subset of UBSan checks is enabled along with a minimal
484# runtime that emulates trap mode (but makes it recoverable).
485
486ifeq ($(KASAN), 1)
487
488ifeq ($(UBSAN),1)
489SAN=1
490
491UBSAN_RUNTIME =
492UBSAN_CHECKS += signed-integer-overflow shift pointer-overflow bounds object-size # non-fatal (calls runtime, can return)
493# UBSAN_CHECKS = undefined nullability unsigned-integer-overflow # everything
494UBSAN_CHECKS_FATAL =                                           # fatal (calls runtime, must not return)
495UBSAN_CHECKS_TRAP = vla-bound builtin                          # emit a trap instruction (no runtime support)
496UBSAN_DISABLED += vptr function     # requires unsupported C++ runtime
497
498# UBSan alignment + KASan code size is too large
499# UBSan unreachable doesn't play nice with ASan (40723397)
500UBSAN_DISABLED += alignment unreachable
501
502endif
503
504else
505
506# DEVELOPMENT and RELEASE variants
507ifeq ($(PLATFORM),iPhoneOS)
508
509# Currently we have to keep alive two separated UBSAN runtimes (minimal for DEVELOPMENT,
510# full for KASAN). This implies that we cannot use CFLAGS_$(CURRENT_KERNEL_CONFIG), because
511# CFLAGS_DEVELOPMENT is folded into CFLAGS_KASAN. For the time being we leave this check here,
512# as we work (independently) to both break the CFLAGS direct dependency and commonize the
513# sanitizer runtimes.
514UBSAN_MINIMAL_RUNTIME := DEVELOPMENT DEBUG
515ifneq ($(filter $(CURRENT_KERNEL_CONFIG), $(UBSAN_MINIMAL_RUNTIME)),)
516
517# This is (unfortunately) intentional. Currently the "kasan" blacklist, which folds both
518# ubsan and kasan specific files, is generated for all builds during the
519# setup phase. The blacklist file itself is divided per-sanitizer, so won't
520# affect the UBSAN build outside of the entries that are legitimately
521# intended for it.
522UBSAN_BLACKLIST=$(OBJROOT)/san/kasan-blacklist-$(CURRENT_ARCH_CONFIG_LC)
523
524UBSAN_CHECKS = signed-integer-overflow
525UBSAN_RUNTIME = -fsanitize-minimal-runtime -fsanitize-blacklist=$(UBSAN_BLACKLIST)
526UBSAN_CHECKS_TRAP =
527UBSAN_CHECKS_FATAL =
528UBSAN_DISABLED =
529endif
530endif
531
532endif
533
534CFLAGS_GEN += $(UBSAN_RUNTIME)
535CFLAGS_GEN += $(foreach x,$(UBSAN_CHECKS) $(UBSAN_CHECKS_FATAL) $(UBSAN_CHECKS_TRAP),-fsanitize=$(x))
536CFLAGS_GEN += $(foreach x,$(UBSAN_CHECKS_FATAL),-fno-sanitize-recover=$(x))
537CFLAGS_GEN += $(foreach x,$(UBSAN_CHECKS_TRAP),-fsanitize-trap=$(x))
538CFLAGS_GEN += $(foreach x,$(UBSAN_DISABLED),-fno-sanitize=$(x))
539
540ifeq ($(KSANCOV),1)
541# Enable SanitizerCoverage instrumentation in xnu
542SAN = 1
543KCOV_BLACKLIST := $(OBJROOT)/san/kcov-blacklist-$(CURRENT_ARCH_CONFIG_LC)
544KCOV_CFLAGS := -fsanitize-coverage=trace-pc-guard -fsanitize-coverage-blocklist=$(KCOV_BLACKLIST)
545CFLAGS_GEN += $(KCOV_CFLAGS) -DKSANCOV=1
546endif
547
548ifeq ($(SAN),1)
549CFLAGS_GEN += -fsanitize-blacklist=$(OBJROOT)/san/kasan-blacklist-$(CURRENT_ARCH_CONFIG_LC)
550endif
551
552
553CFLAGS	= $(CFLAGS_GEN) \
554		  $($(addsuffix $(CURRENT_MACHINE_CONFIG),MACHINE_FLAGS_$(CURRENT_ARCH_CONFIG)_)) \
555		  $($(addsuffix $(CURRENT_ARCH_CONFIG),ARCH_FLAGS_)) \
556		  $($(addsuffix $(CURRENT_ARCH_CONFIG),CFLAGS_)) \
557		  $($(addsuffix $(CURRENT_KERNEL_CONFIG),CFLAGS_)) \
558		  $($(addsuffix $(CURRENT_ARCH_CONFIG), $(addsuffix $(CURRENT_KERNEL_CONFIG),CFLAGS_))) \
559		  $(DEPLOYMENT_TARGET_FLAGS) \
560		  $(DEPLOYMENT_TARGET_DEFINES) \
561		  $(BOUND_CHECKS_DEFINES) \
562		  $(DEFINES)
563
564#
565# Default C++ flags
566#
567
568OTHER_CXXFLAGS	=
569
570CXXFLAGS_GEN  = -std=gnu++1z -fsized-deallocation -fapple-kext $(OTHER_CXXFLAGS)
571
572CXXFLAGS      = $(CXXFLAGS_GEN) \
573		  $($(addsuffix $(CURRENT_ARCH_CONFIG),CXXFLAGS_)) \
574		  $($(addsuffix $(CURRENT_KERNEL_CONFIG),CXXFLAGS_))
575
576#
577# Assembler command
578#
579AS	= $(CCACHE) $(CC)
580S_KCC	= $(CC)
581
582#
583# Default SFLAGS
584#
585SFLAGS_GEN = -D__ASSEMBLER__ -DASSEMBLER $(OTHER_CFLAGS)
586
587SFLAGS_RELEASE	=
588SFLAGS_DEVELOPMENT	=
589
590# When making non-compatible changes to the XNU runtime, it can be useful to build
591# a KASAN kernel + runtime, but linked against a DEVELOPMENT kernel cache.
592# Uncomment the lines below to be able to build development, but passing KASAN=1.
593# #_ifeq ($(KASAN),1)
594# SFLAGS_DEVELOPMENT += -DKASAN=1
595# #_endif
596
597SFLAGS_KASAN = $(SFLAGS_DEVELOPMENT) -DKASAN=1
598SFLAGS_DEBUG	=
599SFLAGS_PROFILE	=
600
601SFLAGS_X86_64	= $(CFLAGS_X86_64)
602SFLAGS_X86_64H	= $(CFLAGS_X86_64H)
603SFLAGS_ARM	= $(CFLAGS_ARM)
604SFLAGS_ARM64	= $(CFLAGS_ARM64)
605
606SFLAGS	= $(SFLAGS_GEN) \
607		  $($(addsuffix $(CURRENT_MACHINE_CONFIG),MACHINE_FLAGS_$(CURRENT_ARCH_CONFIG)_)) \
608		  $($(addsuffix $(CURRENT_ARCH_CONFIG),ARCH_FLAGS_)) \
609		  $($(addsuffix $(CURRENT_ARCH_CONFIG),SFLAGS_)) \
610		  $($(addsuffix $(CURRENT_KERNEL_CONFIG),SFLAGS_)) \
611		  $(DEPLOYMENT_TARGET_FLAGS) \
612		  $(DEPLOYMENT_TARGET_DEFINES) \
613		  $(DEFINES)
614
615#
616# Linker command
617#
618LD	= $(KC++) -nostdlib
619
620#
621# Default LDFLAGS
622#
623# Availability of DWARF allows DTrace CTF (compressed type format) to be constructed.
624# ctf_insert creates the CTF section.  It needs reserved padding in the
625# headers for the load command segment and the CTF section structures.
626#
627LDFLAGS_KERNEL_GEN = \
628	-nostdlib \
629	-fapple-kext \
630	-Wl,-e,__start \
631	-Wl,-sectalign,__TEXT,__text,0x1000 \
632	-Wl,-sectalign,__DATA,__percpu,0x80 \
633	-Wl,-sectalign,__DATA,__common,0x1000 \
634	-Wl,-sectalign,__DATA,__bss,0x1000 \
635	-Wl,-sectcreate,__PRELINK_TEXT,__text,/dev/null \
636	-Wl,-segprot,__PRELINK_TEXT,r-x,r-x \
637	-Wl,-sectcreate,__PRELINK_INFO,__info,/dev/null \
638	-Wl,-new_linker \
639	-Wl,-pagezero_size,0x0 \
640	-Wl,-version_load_command \
641	-Wl,-function_starts \
642	-Wl,-headerpad,152
643
644# LDFLAGS_KERNEL_SDK	= -L$(SDKROOT)/usr/local/lib/kernel -lfirehose_kernel
645LDFLAGS_KERNEL_SDK	= -L$(SDKROOT)/usr/local/lib/kernel
646
647LDFLAGS_KERNEL_RELEASE	=
648LDFLAGS_KERNEL_DEVELOPMENT     =
649LDFLAGS_KERNEL_KASAN = $(LDFLAGS_KERNEL_DEVELOPMENT)
650LDFLAGS_KERNEL_DEBUG	=
651LDFLAGS_KERNEL_PROFILE	=
652
653# KASLR static slide config:
654ifndef SLIDE
655SLIDE=0x00
656endif
657KERNEL_MIN_ADDRESS      = 0xffffff8000000000
658KERNEL_BASE_OFFSET      = 0x100000
659# POSIX shells use signed long for their arithmetic expressions. However,
660# we're dealing with uintptr_t values here, so explicitly use bash which
661# is known to be able to handle such larger values. ksh also works; dash
662# and zsh both fail with different results (zsh even warns you).
663KERNEL_STATIC_SLIDE     = $(shell $(BASH) -c 'printf "0x%016x" \
664			  $$(( $(SLIDE) << 21 ))')
665KERNEL_STATIC_BASE      = $(shell $(BASH) -c 'printf "0x%016x" \
666			  $$(( $(KERNEL_MIN_ADDRESS) + $(KERNEL_BASE_OFFSET) ))')
667KERNEL_HIB_SECTION_BASE = $(shell $(BASH) -c 'printf "0x%016x" \
668			  $$(( $(KERNEL_STATIC_BASE) + $(KERNEL_STATIC_SLIDE) ))')
669KERNEL_TEXT_BASE        = $(shell $(BASH) -c 'printf "0x%016x" \
670			  $$(( $(KERNEL_HIB_SECTION_BASE) + 0x100000 ))')
671
672LDFLAGS_KERNEL_RELEASEX86_64 = \
673	-Wl,-pie \
674	-Wl,-segaddr,__HIB,$(KERNEL_HIB_SECTION_BASE) \
675	-Wl,-image_base,$(KERNEL_TEXT_BASE) \
676	-Wl,-seg_page_size,__TEXT,0x200000 \
677	-Wl,-sectalign,__HIB,__bootPT,0x1000 \
678	-Wl,-sectalign,__HIB,__desc,0x1000 \
679	-Wl,-sectalign,__HIB,__data,0x1000 \
680	-Wl,-sectalign,__HIB,__text,0x1000 \
681	-Wl,-sectalign,__HIB,__const,0x1000 \
682	-Wl,-sectalign,__HIB,__bss,0x1000 \
683	-Wl,-sectalign,__HIB,__common,0x1000 \
684	-Wl,-sectalign,__HIB,__llvm_prf_cnts,0x1000 \
685	-Wl,-sectalign,__HIB,__llvm_prf_names,0x1000 \
686	-Wl,-sectalign,__HIB,__llvm_prf_data,0x1000 \
687	-Wl,-sectalign,__HIB,__textcoal_nt,0x1000 \
688	-Wl,-sectalign,__HIB,__cstring,0x1000 \
689	-Wl,-rename_section,__DATA,__const,__DATA_CONST,__const \
690	-Wl,-segprot,__DATA_CONST,r--,r-- \
691	-Wl,-rename_section,__KLD,__const,__KLDDATA,__const \
692	-Wl,-rename_section,__KLD,__cstring,__KLDDATA,__cstring \
693	-Wl,-segprot,__KLDDATA,rw-,rw- \
694	-Wl,-segprot,__KLD,r-x,r-x \
695	-Wl,-no_zero_fill_sections \
696	$(LDFLAGS_NOSTRIP_FLAG)
697
698ifeq ($(SAN),1)
699LDFLAGS_KERNEL_RELEASEX86_64 += \
700	-Wl,-sectalign,__HIB,__cstring,0x1000
701endif
702
703ifeq ($(KSANCOV),1)
704LDFLAGS_KERNEL_RELEASEX86_64 += \
705	-Wl,-sectalign,__HIB,__sancov_guards,0x1000 \
706	-Wl,-sectalign,__HIB,__sancov_pcs,0x1000
707endif
708
709# Define KERNEL_BASE_OFFSET so known at compile time:
710CFLAGS_X86_64 += -DKERNEL_BASE_OFFSET=$(KERNEL_BASE_OFFSET)
711CFLAGS_X86_64H += -DKERNEL_BASE_OFFSET=$(KERNEL_BASE_OFFSET)
712
713LDFLAGS_KERNEL_DEBUGX86_64 = $(LDFLAGS_KERNEL_RELEASEX86_64)
714LDFLAGS_KERNEL_DEVELOPMENTX86_64 = $(LDFLAGS_KERNEL_RELEASEX86_64)
715LDFLAGS_KERNEL_KASANX86_64 = $(LDFLAGS_KERNEL_DEVELOPMENTX86_64) \
716	-Wl,-sectalign,__HIB,__asan_globals,0x1000 \
717	-Wl,-sectalign,__HIB,__asan_liveness,0x1000 \
718	-Wl,-sectalign,__HIB,__mod_term_func,0x1000 \
719	-Wl,-rename_section,__HIB,__mod_init_func,__NULL,__mod_init_func \
720	-Wl,-rename_section,__HIB,__eh_frame,__NULL,__eh_frame
721LDFLAGS_KERNEL_PROFILEX86_64 = $(LDFLAGS_KERNEL_RELEASEX86_64)
722
723LDFLAGS_KERNEL_RELEASEX86_64H = $(LDFLAGS_KERNEL_RELEASEX86_64)
724LDFLAGS_KERNEL_DEBUGX86_64H = $(LDFLAGS_KERNEL_RELEASEX86_64H)
725LDFLAGS_KERNEL_DEVELOPMENTX86_64H = $(LDFLAGS_KERNEL_RELEASEX86_64H)
726LDFLAGS_KERNEL_KASANX86_64H = $(LDFLAGS_KERNEL_KASANX86_64)
727LDFLAGS_KERNEL_PROFILEX86_64H = $(LDFLAGS_KERNEL_RELEASEX86_64H)
728
729# We preload ___udivmoddi4 in order to work around an issue with building
730# LTO on armv7.
731LDFLAGS_KERNEL_GENARM = \
732	-Wl,-pie \
733	-Wl,-static \
734	-Wl,-image_base,0x80001000 \
735	-Wl,-sectalign,__DATA,__const,0x1000 \
736	-Wl,-u,___udivmoddi4 \
737	-Wl,-rename_section,__KLD,__const,__KLDDATA,__const \
738	-Wl,-rename_section,__KLD,__cstring,__KLDDATA,__cstring \
739	-Wl,-segprot,__KLDDATA,rw-,rw- \
740	-Wl,-segprot,__KLD,r-x,r-x
741
742LDFLAGS_KERNEL_RELEASEARM     = \
743	$(LDFLAGS_KERNEL_GENARM) \
744	$(LDFLAGS_KERNEL_STRIP_LTO)
745
746LDFLAGS_KERNEL_ONLY_CONFIG_RELEASEARM     = \
747	-Wl,-exported_symbols_list,$(TARGET)/all-kpi.exp
748
749LDFLAGS_KERNEL_DEVELOPMENTARM     = \
750	$(LDFLAGS_KERNEL_GENARM) \
751	$(LDFLAGS_NOSTRIP_FLAG)
752
753LDFLAGS_KERNEL_ONLY_CONFIG_DEVELOPMENTARM =
754
755LDFLAGS_KERNEL_DEBUGARM = $(LDFLAGS_KERNEL_DEVELOPMENTARM)
756LDFLAGS_KERNEL_ONLY_CONFIG_DEBUGARM = $(LDFLAGS_KERNEL_ONLY_CONFIG_DEVELOPMENTARM)
757
758# Offset image base by page to have iBoot load kernel TEXT correctly.
759# First page is used for various purposes : sleep token, reset vector.
760# We also need a 32MB offset, as this is the minimum block mapping size
761# for a 16KB page runtime, and we wish to use the first virtual block
762# to map the low globals page.  We also need another 4MB to account for
763# the address space reserved by L4 (because the reservation is not a
764# multiple of the block size in alignment/length, we will implictly map
765# it with our block mapping, and we therefore must reflect that the
766# first 4MB of the block mapping for xnu do not belong to xnu).
767# For the moment, kaliber has a unique memory layout (monitor at the top
768# of memory).  Support this by breaking 16KB on other platforms and
769# mandating 32MB alignment. Image base (i.e. __TEXT) must be 16KB
770# aligned since ld64 will link with 16KB alignment for ARM64.
771#
772# We currently offset by an additional 32MB in order to reclaim memory.
773# We need a dedicated virtual page for the low globals.  Our bootloader
774# may have a significant chunk of memory (up to an L2 entry in size)
775# that lies before the kernel.  The addition 32MB of virtual padding
776# ensures that we have enough virtual address space to map all of that
777# memory as part of the V-to-P mapping.
778# 23355738 - put __PRELINK_TEXT first. We reserve enough room
779# for 0x0000000003000000 = 48MB of kexts
780#
781# 0xfffffff000000000 (32MB range for low globals)
782# 0xfffffff002000000 (32MB range to allow for large page physical slide)
783# 0xfffffff004000000 (16KB range to reserve the first available page)
784# 0xfffffff004004000 (48MB range for kexts)
785# 0xfffffff007004000 (Start of xnu proper).
786LDFLAGS_KERNEL_GENARM64 = \
787	-Wl,-pie \
788	-Wl,-static \
789	-Wl,-segaddr,__PRELINK_TEXT,$(ARM64_PLKSEG_ADDR) \
790	-Wl,-image_base,$(ARM64_LINK_ADDR) \
791	\
792	-Wl,-rename_section,__HIB,__text,__TEXT_EXEC,__hib_text \
793	\
794	-Wl,-rename_section,__HIB,__const,__DATA_CONST,__hib_const \
795	-Wl,-rename_section,__HIB,__cstring,__DATA_CONST,__hib_const \
796	-Wl,-rename_section,__HIB,__literal8,__DATA_CONST,__hib_const \
797	-Wl,-rename_section,__HIB,__literal16,__DATA_CONST,__hib_const \
798	\
799	-Wl,-rename_segment,__HIB,__HIBDATA \
800	\
801	-Wl,-sectalign,__DATA,__const,0x4000 \
802	-Wl,-sectalign,__DATA,__data,0x4000 \
803	-Wl,-rename_section,__DATA,__mod_init_func,__DATA_CONST,__mod_init_func \
804	-Wl,-rename_section,__DATA,__mod_term_func,__DATA_CONST,__mod_term_func \
805	-Wl,-rename_section,__DATA,__auth_ptr,__DATA_CONST,__auth_ptr \
806	-Wl,-rename_section,__DATA,__auth_got,__DATA_CONST,__auth_got \
807	-Wl,-rename_section,__DATA,__const,__DATA_CONST,__const \
808	-Wl,-segprot,__DATA_CONST,r--,r-- \
809	-Wl,-rename_section,__KLD,__const,__KLDDATA,__const \
810	-Wl,-rename_section,__KLD,__cstring,__KLDDATA,__cstring \
811	-Wl,-segprot,__KLDDATA,rw-,rw- \
812	-Wl,-segprot,__KLD,r-x,r-x \
813	-Wl,-rename_section,__TEXT,__text,__TEXT_EXEC,__text \
814	-Wl,-rename_section,__TEXT,__stubs,__TEXT_EXEC,__stubs \
815	-Wl,-sectcreate,"__PLK_TEXT_EXEC",__text,/dev/null \
816	-Wl,-sectcreate,__PRELINK_DATA,__data,/dev/null \
817	-Wl,-sectcreate,"__PLK_DATA_CONST",__data,/dev/null \
818	-Wl,-sectcreate,"__PLK_LLVM_COV",__llvm_covmap,/dev/null \
819	-Wl,-sectcreate,"__PLK_LINKEDIT",__data,/dev/null
820
821LDFLAGS_KERNEL_SEGARM64 = \
822	-Wl,-rename_section,__PPLDATA,__const,__PPLDATA_CONST,__const \
823	-Wl,-segment_order,__TEXT:__DATA_CONST:__LINKEDIT:__TEXT_EXEC:__KLD:__PPLTEXT:__PPLTRAMP:__PPLDATA_CONST:__LASTDATA_CONST:__LAST:__PPLDATA:__KLDDATA:__DATA:__HIBDATA:__BOOTDATA \
824	-Wl,-segprot,__PPLTEXT,r-x,r-x  -Wl,-segprot,__PPLTRAMP,r-x,r-x -Wl,-segprot,__PPLDATA_CONST,r--,r-- -Wl,-segprot,__LASTDATA_CONST,r--,r-- -Wl,-segprot,__LAST,r-x,r-x \
825
826LDFLAGS_KERNEL_RELEASEARM64     = \
827	$(LDFLAGS_KERNEL_GENARM64) \
828	$(LDFLAGS_KERNEL_SEGARM64) \
829	$(LDFLAGS_KERNEL_STRIP_LTO)
830
831LDFLAGS_KERNEL_ONLY_CONFIG_RELEASEARM64     = \
832	-Wl,-exported_symbols_list,$(TARGET)/all-kpi.exp
833
834LDFLAGS_KERNEL_DEVELOPMENTARM64     = \
835	$(LDFLAGS_KERNEL_GENARM64) \
836	$(LDFLAGS_KERNEL_SEGARM64) \
837	$(LDFLAGS_NOSTRIP_FLAG)
838
839LDFLAGS_KERNEL_ONLY_CONFIG_DEVELOPMENTARM64 =
840
841LDFLAGS_KERNEL_KASANARM64 = $(LDFLAGS_KERNEL_DEVELOPMENTARM64)
842LDFLAGS_KERNEL_DEBUGARM64 = $(LDFLAGS_KERNEL_DEVELOPMENTARM64)
843
844LDFLAGS_KERNEL_ONLY_CONFIG_KASANARM64 = $(LDFLAGS_KERNEL_ONLY_CONFIG_DEVELOPMENTARM64)
845LDFLAGS_KERNEL_ONLY_CONFIG_DEBUGARM64 = $(LDFLAGS_KERNEL_ONLY_CONFIG_DEVELOPMENTARM64)
846
847#
848# arm64e specific linker flags that should be used only when linking the kernel
849# (and not the static kernel cache / kcgen)
850#
851LDFLAGS_KERNEL_ONLY_SUBARCH_arm64e = \
852	-Wl,-add_split_seg_info \
853	-Wl,-kernel
854
855LDFLAGS_KERNEL_ONLY_SUBARCH_x86_64 = \
856	-Wl,-add_split_seg_info \
857	-Wl,-kernel
858LDFLAGS_KERNEL_ONLY_SUBARCH_x86_64h = $(LDFLAGS_KERNEL_ONLY_SUBARCH_x86_64)
859
860LDFLAGS_KERNEL	= $(LDFLAGS_KERNEL_GEN) \
861		  $(LDFLAGS_KERNEL_SDK) \
862		  $($(addsuffix $(CURRENT_ARCH_CONFIG),ARCH_FLAGS_)) \
863		  $($(addsuffix $(CURRENT_ARCH_CONFIG),LDFLAGS_KERNEL_)) \
864		  $($(addsuffix $(CURRENT_KERNEL_CONFIG),LDFLAGS_KERNEL_)) \
865		  $($(addsuffix $(CURRENT_ARCH_CONFIG), $(addsuffix $(CURRENT_KERNEL_CONFIG),LDFLAGS_KERNEL_))) \
866		  $(DEPLOYMENT_TARGET_FLAGS)
867
868LDFLAGS_KERNEL_ONLY  +=   \
869		  $($(addsuffix $(CURRENT_ARCH_CONFIG), $(addsuffix $(CURRENT_KERNEL_CONFIG),LDFLAGS_KERNEL_ONLY_CONFIG_))) \
870		  $($(addsuffix $(ARCH_STRING_FOR_CURRENT_MACHINE_CONFIG),LDFLAGS_KERNEL_ONLY_SUBARCH_)) \
871		  -Wl,-alias_list,$(TARGET)/all-alias.exp \
872		  -Wl,-sectcreate,__LINKINFO,__symbolsets,$(TARGET)/symbolsets.plist -Wl,-segprot,__LINKINFO,r--,r--
873
874LDFILES_KERNEL_ONLY = $(TARGET)/all-kpi.exp $(TARGET)/all-alias.exp $(TARGET)/symbolsets.plist
875
876#
877# Default runtime libraries to be linked with the kernel
878#
879LD_KERNEL_LIBS	   = -lcc_kext
880LD_KERNEL_ARCHIVES = $(LDFLAGS_KERNEL_SDK) -lfirehose_kernel
881# Link opensource binary library
882ifneq ($(filter T6000 T8101 VMAPPLE T6000 T8101 VMAPPLE,$(CURRENT_MACHINE_CONFIG)),)
883	LDFLAGS_KERNEL_ONLY += -rdynamic -Wl,-force_load,$(KDKROOT)/System/Library/KernelSupport/lib$(CURRENT_MACHINE_CONFIG).os.$(CURRENT_KERNEL_CONFIG).a
884endif
885
886#
887# DTrace support
888#
889ifndef DO_CTFMERGE
890DO_CTFMERGE := 1
891ifeq ($(CURRENT_KERNEL_CONFIG),RELEASE)
892ifneq ($(PLATFORM),MacOSX)
893DO_CTFMERGE := 0
894endif
895endif
896ifneq ($(CURRENT_KERNEL_CONFIG),KASAN)
897ifneq ($(PLATFORM),MacOSX)
898ifeq ($(WILL_BUILD_STATIC_KC),1)
899DO_CTFMERGE := 0
900endif
901endif
902endif
903endif # DO_CTFMERGE
904
905
906#
907# Default INCFLAGS
908#
909INCFLAGS_IMPORT	= $(patsubst %, -I$(OBJROOT)/EXPORT_HDRS/%, $(COMPONENT_IMPORT_LIST))
910INCFLAGS_EXTERN	= -I$(SRCROOT)/EXTERNAL_HEADERS
911INCFLAGS_GEN	= -I$(SRCROOT)/$(COMPONENT) -I$(OBJROOT)/EXPORT_HDRS/$(COMPONENT)
912INCFLAGS_LOCAL	= -I.
913INCFLAGS_SDK	= -I$(SDKROOT)/usr/local/include/kernel
914
915INCFLAGS	= $(INCFLAGS_LOCAL) $(INCFLAGS_GEN) $(INCFLAGS_IMPORT) $(INCFLAGS_EXTERN) $(INCFLAGS_MAKEFILE) $(INCFLAGS_SDK)
916
917#
918# Default MIGFLAGS
919#
920MIGFLAGS	= $(DEFINES) $(INCFLAGS) -novouchers $($(addsuffix $(CURRENT_ARCH_CONFIG),CFLAGS_)) $($(addsuffix $(CURRENT_ARCH_CONFIG),ARCH_FLAGS_)) \
921		$(DEPLOYMENT_TARGET_FLAGS)
922
923# Support for LLVM Profile Guided Optimization (PGO)
924
925ifeq ($(BUILD_PROFILE),1)
926CFLAGS_GEN += -fprofile-instr-generate -DPROFILE
927CXXFLAGS_GEN += -fprofile-instr-generate -DPROFILE
928endif
929
930ifdef USE_PROFILE
931CFLAGS_GEN += -fprofile-instr-use=$(USE_PROFILE)
932CXXFLAGS_GEN += -fprofile-instr-use=$(USE_PROFILE)
933LDFLAGS_KERNEL_GEN += -fprofile-instr-use=$(USE_PROFILE)
934
935CFLAGS_GEN += -Wno-error=profile-instr-out-of-date
936endif
937
938#
939# Support for LLVM Link Time Optimization (LTO)
940#
941# LTO can be explicitly enabled or disabled with BUILD_LTO=1|0
942# and defaults to enabled except for DEBUG kernels
943#
944# CFLAGS_NOLTO_FLAG is needed on a per-file basis (for files
945# that deal poorly with LTO, or files that must be machine
946# code *.o files for xnu to build (i.e, setsegname runs on
947# them).
948#
949# LDFLAGS_NOSTRIP_FLAG is used to support configurations that
950# do not utilize an export list.  For these configs to build,
951# we need to prevent the LTO logic from dead stripping them.
952
953LTO_ENABLED_RELEASE = 1
954LTO_ENABLED_DEVELOPMENT = 1
955LTO_ENABLED_DEBUG = 0
956LTO_ENABLED_KASAN = 0
957
958ifneq ($(BUILD_LTO),)
959USE_LTO = $(BUILD_LTO)
960else
961USE_LTO = $(LTO_ENABLED_$(CURRENT_KERNEL_CONFIG))
962endif
963
964ifeq ($(USE_LTO),1)
965CFLAGS_GEN	+= -flto
966CXXFLAGS_GEN	+= -flto
967LDFLAGS_KERNEL_LTO	= -Wl,-mllvm,-inline-threshold=100
968LDFLAGS_KERNEL_GEN	+= $(LDFLAGS_KERNEL_LTO) -Wl,-object_path_lto,$(TARGET)/lto.o
969LDFLAGS_NOSTRIP_FLAG = -rdynamic
970LDFLAGS_KERNEL_STRIP_LTO = -Wl,-dead_strip,-no_dead_strip_inits_and_terms
971
972CFLAGS_NOLTO_FLAG = -fno-lto
973else
974LDFLAGS_KERNEL_LTO =
975LDFLAGS_NOSTRIP_FLAG =
976LDFLAGS_KERNEL_STRIP_LTO =
977CFLAGS_NOLTO_FLAG =
978endif
979
980ifeq ($(BUILD_JSON_COMPILATION_DATABASE),1)
981BUILD_DSYM	:= 0
982DO_CTFMERGE  	:= 0
983KCC		= $(JSONCOMPILATIONDB) $(OBJPATH)/compile_commands.json $(PWD) $< $(CC)
984KC++		= $(JSONCOMPILATIONDB) $(OBJPATH)/compile_commands.json $(PWD) $< $(CXX)
985S_KCC		= $(JSONCOMPILATIONDB) $(OBJPATH)/compile_commands.json $(PWD) $< $(CC)
986STRIP		= true
987endif
988
989#
990# Default VPATH
991#
992export VPATH = .:$(SOURCE)
993
994
995
996#
997# Macros that control installation of kernel and its header files
998#
999# install flags for header files
1000#
1001INSTALL_FLAGS = -c -S -m 0444
1002DATA_INSTALL_FLAGS = -c -S -m 0644
1003DATA_INSTALL_FLAGS_RO = -c -S -m 0444
1004EXEC_INSTALL_FLAGS = -c -S -m 0755
1005
1006#
1007# Header file destinations
1008#
1009
1010ifeq ($(DRIVERKIT),1)
1011    SDKHEADERSROOT=$(DRIVERKITRUNTIMEROOT)
1012    # only whitelisted headers install outside of the DriverKit Runtime hierarchy
1013    DRIVERKITSDKHEADERSROOT=$(DRIVERKITROOT)
1014    DRIVERKITFRAMEDIR = $(DRIVERKITROOT)/System/Library/Frameworks
1015endif
1016
1017FRAMEDIR = $(SDKHEADERSROOT)/System/Library/Frameworks
1018
1019SINCVERS = B
1020SINCFRAME = $(FRAMEDIR)/System.framework
1021SINCDIR = $(SINCFRAME)/Versions/$(SINCVERS)/Headers
1022SPINCDIR = $(SINCFRAME)/Versions/$(SINCVERS)/PrivateHeaders
1023SRESDIR = $(SINCFRAME)/Versions/$(SINCVERS)/Resources
1024
1025ifndef INCDIR
1026    INCDIR = $(SDKHEADERSROOT)/usr/include
1027endif
1028ifndef DRIVERKITINCDIR
1029    DRIVERKITINCDIR = $(DRIVERKITSDKHEADERSROOT)/usr/include
1030endif
1031ifndef LCLDIR
1032	LCLDIR = $(SDKHEADERSROOT)/usr/local/include
1033endif
1034ifndef DRIVERKITLCLDIR
1035    DRIVERKITLCLDIR = $(DRIVERKITSDKHEADERSROOT)/usr/local/include
1036endif
1037
1038KINCVERS = A
1039KINCFRAME = $(FRAMEDIR)/Kernel.framework
1040KINCDIR = $(KINCFRAME)/Versions/$(KINCVERS)/Headers
1041KPINCDIR = $(KINCFRAME)/Versions/$(KINCVERS)/PrivateHeaders
1042KRESDIR = $(KINCFRAME)/Versions/$(KINCVERS)/Resources
1043
1044DKIT_INCFRAME = DriverKit.framework
1045
1046ifeq ($(PLATFORM),MacOSX)
1047DKIT_INCVERS = A
1048DKIT_INCDIR = $(DKIT_INCFRAME)/Versions/$(DKIT_INCVERS)/Headers
1049DKIT_PINCDIR = $(DKIT_INCFRAME)/Versions/$(DKIT_INCVERS)/PrivateHeaders
1050else
1051# non-macOS SDK frameworks use shallow bundle structure
1052DKIT_INCDIR = $(DKIT_INCFRAME)/Headers
1053DKIT_PINCDIR = $(DKIT_INCFRAME)/PrivateHeaders
1054endif
1055
1056# DriverKit SDK frameworks use shallow bundle structure
1057DRIVERKIT_DKIT_INCDIR = $(DKIT_INCFRAME)/Headers
1058DRIVERKIT_DKIT_PINCDIR = $(DKIT_INCFRAME)/PrivateHeaders
1059
1060XNU_PRIVATE_UNIFDEF :=
1061XNU_PRIVATE_UNIFDEF += -UMACH_KERNEL_PRIVATE
1062XNU_PRIVATE_UNIFDEF += -UBSD_KERNEL_PRIVATE
1063XNU_PRIVATE_UNIFDEF += -UIOKIT_KERNEL_PRIVATE
1064XNU_PRIVATE_UNIFDEF += -ULIBKERN_KERNEL_PRIVATE
1065XNU_PRIVATE_UNIFDEF += -ULIBSA_KERNEL_PRIVATE
1066XNU_PRIVATE_UNIFDEF += -UPEXPERT_KERNEL_PRIVATE
1067XNU_PRIVATE_UNIFDEF += -UXNU_KERNEL_PRIVATE
1068XNU_PRIVATE_UNIFDEF += -UXNU_BOUND_CHECKS
1069
1070
1071PLATFORM_UNIFDEF = $(foreach x,$(SUPPORTED_PLATFORMS),$(if $(filter $(PLATFORM),$(x)),-DPLATFORM_$(x) $(foreach token,$(PLATFORM_UNIFDEF_BLACKLIST_TOKENS_$(x)),-U$(token)),-UPLATFORM_$(x)))
1072
1073
1074# Some header guards need to be present and checked in kernel headers but removed from userspace headers
1075KERNEL_ONLY_GUARDS_UNIFDEF =
1076
1077SPINCFRAME_UNIFDEF  = $(PLATFORM_UNIFDEF) $(XNU_PRIVATE_UNIFDEF) $(SEED_DEFINES) -UKERNEL_PRIVATE -UKERNEL -DPRIVATE -UDRIVERKIT -U_OPEN_SOURCE_ -U__OPEN_SOURCE__ $(KERNEL_ONLY_GUARDS_UNIFDEF)
1078SINCFRAME_UNIFDEF   = $(PLATFORM_UNIFDEF) $(XNU_PRIVATE_UNIFDEF) $(SEED_DEFINES) -UKERNEL_PRIVATE -UKERNEL -UPRIVATE -UDRIVERKIT -D_OPEN_SOURCE_ -D__OPEN_SOURCE__ $(KERNEL_ONLY_GUARDS_UNIFDEF)
1079DKPINCFRAME_UNIFDEF = $(PLATFORM_UNIFDEF) $(XNU_PRIVATE_UNIFDEF) $(SEED_DEFINES) -UKERNEL_PRIVATE -UKERNEL -DPRIVATE -DDRIVERKIT -U_OPEN_SOURCE_ -U__OPEN_SOURCE__ $(KERNEL_ONLY_GUARDS_UNIFDEF)
1080DKINCFRAME_UNIFDEF  = $(PLATFORM_UNIFDEF) $(XNU_PRIVATE_UNIFDEF) $(SEED_DEFINES) -UKERNEL_PRIVATE -UKERNEL -UPRIVATE -DDRIVERKIT -D_OPEN_SOURCE_ -D__OPEN_SOURCE__ $(KERNEL_ONLY_GUARDS_UNIFDEF)
1081KPINCFRAME_UNIFDEF  = $(PLATFORM_UNIFDEF) $(XNU_PRIVATE_UNIFDEF) $(SEED_DEFINES) -DKERNEL_PRIVATE -DKERNEL -DPRIVATE -UDRIVERKIT -U_OPEN_SOURCE_ -U__OPEN_SOURCE__
1082KINCFRAME_UNIFDEF   = $(PLATFORM_UNIFDEF) $(XNU_PRIVATE_UNIFDEF) $(SEED_DEFINES) -UKERNEL_PRIVATE -DKERNEL -UPRIVATE -UDRIVERKIT -D_OPEN_SOURCE_ -D__OPEN_SOURCE__
1083PDATA_UNIFDEF       = $(PLATFORM_UNIFDEF) $(XNU_PRIVATE_UNIFDEF) $(SEED_DEFINES) -DPRIVATE -U_OPEN_SOURCE_ -U__OPEN_SOURCE__
1084DATA_UNIFDEF        = $(PLATFORM_UNIFDEF) $(XNU_PRIVATE_UNIFDEF) $(SEED_DEFINES) -UPRIVATE -D_OPEN_SOURCE_ -D__OPEN_SOURCE__
1085
1086#
1087# Compononent Header file destinations
1088#
1089EXPDIR = EXPORT_HDRS/$(COMPONENT)
1090
1091#
1092# Strip Flags
1093#
1094STRIP_FLAGS_RELEASE	= -S -x
1095STRIP_FLAGS_DEVELOPMENT	= -S
1096STRIP_FLAGS_KASAN = $(STRIP_FLAGS_DEVELOPMENT)
1097STRIP_FLAGS_DEBUG	= -S
1098STRIP_FLAGS_PROFILE	= -S -x
1099
1100STRIP_FLAGS	= $($(addsuffix $(CURRENT_KERNEL_CONFIG),STRIP_FLAGS_))
1101
1102#
1103# dsymutil flags
1104#
1105DSYMUTIL_FLAGS_GEN	= --minimize
1106
1107DSYMUTIL_FLAGS_X86_64	= --arch=x86_64
1108DSYMUTIL_FLAGS_X86_64H	= --arch=x86_64h
1109DSYMUTIL_FLAGS_ARM	= --arch=arm
1110DSYMUTIL_FLAGS_ARM64	=
1111
1112DSYMUTIL_FLAGS = $(DSYMUTIL_FLAGS_GEN) \
1113	$($(addsuffix $(CURRENT_ARCH_CONFIG),DSYMUTIL_FLAGS_))
1114
1115#
1116# Man Page destination
1117#
1118MANDIR = /usr/share/man
1119
1120#
1121# DEBUG alias location
1122#
1123DEVELOPER_EXTRAS_DIR = /AppleInternal/CoreOS/xnu_$(CURRENT_KERNEL_CONFIG_LC)
1124
1125#
1126# mach_kernel install location
1127#
1128INSTALL_KERNEL_DIR = /
1129
1130#
1131# new OS X install location
1132#
1133SYSTEM_LIBRARY_KERNELS_DIR = /System/Library/Kernels
1134
1135#
1136# File names in DSTROOT
1137#
1138
1139ifeq ($(PLATFORM),MacOSX)
1140KERNEL_FILE_NAME_PREFIX = kernel
1141else
1142KERNEL_FILE_NAME_PREFIX = mach
1143endif
1144
1145ifeq ($(CURRENT_MACHINE_CONFIG),NONE)
1146ifeq ($(CURRENT_KERNEL_CONFIG),RELEASE)
1147KERNEL_FILE_NAME = $(KERNEL_FILE_NAME_PREFIX)
1148KERNEL_LLDBBOOTSTRAP_NAME = $(KERNEL_FILE_NAME_PREFIX).py
1149else
1150KERNEL_FILE_NAME = $(KERNEL_FILE_NAME_PREFIX).$(CURRENT_KERNEL_CONFIG_LC)
1151KERNEL_LLDBBOOTSTRAP_NAME = $(KERNEL_FILE_NAME_PREFIX).py
1152endif
1153else
1154KERNEL_FILE_NAME = $(KERNEL_FILE_NAME_PREFIX).$(CURRENT_KERNEL_CONFIG_LC).$(CURRENT_MACHINE_CONFIG_LC)
1155KERNEL_LLDBBOOTSTRAP_NAME = $(KERNEL_FILE_NAME_PREFIX)_$(CURRENT_KERNEL_CONFIG_LC).py
1156endif
1157
1158CURRENT_ALIAS_MACHINE_CONFIG = $(word 4,$(subst ^, ,$(CURRENT_BUILD_CONFIG)))
1159CURRENT_ALIAS_MACHINE_CONFIG_LC = $(shell printf "%s" "$(CURRENT_ALIAS_MACHINE_CONFIG)" | $(TR) A-Z a-z)
1160ifneq ($(CURRENT_ALIAS_MACHINE_CONFIG),)
1161ALIAS_FILE_NAME = $(KERNEL_FILE_NAME_PREFIX).$(CURRENT_KERNEL_CONFIG_LC).$(CURRENT_ALIAS_MACHINE_CONFIG_LC)
1162endif
1163
1164#
1165# System.kext pseudo-kext install location
1166#
1167INSTALL_EXTENSIONS_DIR = /System/Library/Extensions
1168
1169#
1170# System.kext PlugIns install location
1171#
1172DSTROOT_SYSTEM_KEXT_PATH = $(addprefix $(DSTROOT)$(INSTALL_EXTENSIONS_DIR),/System.kext/PlugIns)
1173SYMROOT_SYSTEM_KEXT_PATH = $(addprefix $(SYMROOT),/System.kext/PlugIns)
1174
1175#
1176# KDK location
1177#
1178INSTALL_KERNEL_SYM_DIR = /System/Library/Extensions/KDK
1179
1180#
1181# Misc. Etc.
1182#
1183INSTALL_SHARE_MISC_DIR = /usr/share/misc
1184INSTALL_DTRACE_SCRIPTS_DIR = /usr/lib/dtrace
1185INSTALL_DTRACE_LIBEXEC_DIR = /usr/libexec/dtrace
1186
1187#
1188# Overrides for XBS build aliases
1189#
1190ifneq ($(filter $(RC_ProjectName),xnu_headers_driverkit),)
1191USE_BINARY_PLIST = 1
1192else ifneq ($(filter $(RC_ProjectName),xnu_debug),)
1193INSTALL_KERNEL_DIR := $(DEVELOPER_EXTRAS_DIR)
1194INSTALL_KERNEL_SYM_DIR := $(DEVELOPER_EXTRAS_DIR)
1195INSTALL_KERNEL_SYM_TO_KDK = 1
1196INSTALL_XNU_DEBUG_FILES = 1
1197else ifneq ($(filter $(SUPPORTED_EMBEDDED_PLATFORMS),$(PLATFORM)),)
1198INSTALL_KERNEL_SYM_TO_KDK = 1
1199USE_BINARY_PLIST = 1
1200else ifneq ($(filter $(SUPPORTED_SIMULATOR_PLATFORMS),$(PLATFORM)),)
1201USE_BINARY_PLIST = 1
1202else ifeq ($(PLATFORM),MacOSX)
1203INSTALL_KERNEL_DIR := $(SYSTEM_LIBRARY_KERNELS_DIR)
1204INSTALL_KERNEL_SYM_DIR := $(SYSTEM_LIBRARY_KERNELS_DIR)
1205INSTALL_KERNEL_SYM_TO_KDK = $(if $(filter YES,$(DWARF_DSYM_FILE_SHOULD_ACCOMPANY_PRODUCT)),1,0)
1206endif
1207
1208ifneq ($(filter $(RC_ProjectName),xnu_kasan),)
1209INSTALL_KASAN_ONLY = 1
1210endif
1211
1212# vim: set ft=make:
1213