xref: /freebsd/sys/conf/kern.mk (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1# $FreeBSD$
2
3#
4# Warning flags for compiling the kernel and components of the kernel:
5#
6CWARNFLAGS?=	-Wall -Wredundant-decls -Wnested-externs -Wstrict-prototypes \
7		-Wmissing-prototypes -Wpointer-arith -Wcast-qual \
8		-Wundef -Wno-pointer-sign ${FORMAT_EXTENSIONS} \
9		-Wmissing-include-dirs -fdiagnostics-show-option \
10		-Wno-unknown-pragmas \
11		${CWARNEXTRA}
12#
13# The following flags are next up for working on:
14#	-Wextra
15
16# Disable a few warnings for clang, since there are several places in the
17# kernel where fixing them is more trouble than it is worth, or where there is
18# a false positive.
19.if ${COMPILER_TYPE} == "clang"
20NO_WCONSTANT_CONVERSION=	-Wno-error-constant-conversion
21NO_WSHIFT_COUNT_NEGATIVE=	-Wno-shift-count-negative
22NO_WSHIFT_COUNT_OVERFLOW=	-Wno-shift-count-overflow
23NO_WSELF_ASSIGN=		-Wno-self-assign
24NO_WUNNEEDED_INTERNAL_DECL=	-Wno-error-unneeded-internal-declaration
25NO_WSOMETIMES_UNINITIALIZED=	-Wno-error-sometimes-uninitialized
26NO_WCAST_QUAL=			-Wno-error-cast-qual
27NO_WTAUTOLOGICAL_POINTER_COMPARE= -Wno-tautological-pointer-compare
28# Several other warnings which might be useful in some cases, but not severe
29# enough to error out the whole kernel build.  Display them anyway, so there is
30# some incentive to fix them eventually.
31CWARNEXTRA?=	-Wno-error-tautological-compare -Wno-error-empty-body \
32		-Wno-error-parentheses-equality -Wno-error-unused-function \
33		-Wno-error-pointer-sign
34CWARNEXTRA+=	-Wno-error-shift-negative-value
35CWARNEXTRA+=	-Wno-address-of-packed-member
36.if ${COMPILER_VERSION} >= 100000
37NO_WMISLEADING_INDENTATION=	-Wno-misleading-indentation
38.endif
39.endif	# clang
40
41.if ${COMPILER_TYPE} == "gcc"
42# Catch-all for all the things that are in our tree, but for which we're
43# not yet ready for this compiler.
44NO_WUNUSED_BUT_SET_VARIABLE = -Wno-unused-but-set-variable
45CWARNEXTRA?=	-Wno-error=address				\
46		-Wno-error=aggressive-loop-optimizations	\
47		-Wno-error=array-bounds				\
48		-Wno-error=attributes				\
49		-Wno-error=cast-qual				\
50		-Wno-error=enum-compare				\
51		-Wno-error=inline				\
52		-Wno-error=maybe-uninitialized			\
53		-Wno-error=misleading-indentation		\
54		-Wno-error=nonnull-compare			\
55		-Wno-error=overflow				\
56		-Wno-error=sequence-point			\
57		-Wno-error=shift-overflow			\
58		-Wno-error=tautological-compare			\
59		-Wno-unused-but-set-variable
60.if ${COMPILER_VERSION} >= 70100
61CWARNEXTRA+=	-Wno-error=stringop-overflow
62.endif
63.if ${COMPILER_VERSION} >= 70200
64CWARNEXTRA+=	-Wno-error=memset-elt-size
65.endif
66.if ${COMPILER_VERSION} >= 80000
67CWARNEXTRA+=	-Wno-error=packed-not-aligned
68.endif
69.if ${COMPILER_VERSION} >= 90100
70CWARNEXTRA+=	-Wno-address-of-packed-member
71.endif
72.endif	# gcc
73
74# This warning is utter nonsense
75CWARNFLAGS+=	-Wno-format-zero-length
76
77# External compilers may not support our format extensions.  Allow them
78# to be disabled.  WARNING: format checking is disabled in this case.
79.if ${MK_FORMAT_EXTENSIONS} == "no"
80FORMAT_EXTENSIONS=	-Wno-format
81.elif ${COMPILER_TYPE} == "clang"
82FORMAT_EXTENSIONS=	-D__printf__=__freebsd_kprintf__
83.else
84FORMAT_EXTENSIONS=	-fformat-extensions
85.endif
86
87#
88# On i386, do not align the stack to 16-byte boundaries.  Otherwise GCC 2.95
89# and above adds code to the entry and exit point of every function to align the
90# stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack
91# per function call.  While the 16-byte alignment may benefit micro benchmarks,
92# it is probably an overall loss as it makes the code bigger (less efficient
93# use of code cache tag lines) and uses more stack (less efficient use of data
94# cache tag lines).  Explicitly prohibit the use of FPU, SSE and other SIMD
95# operations inside the kernel itself.  These operations are exclusively
96# reserved for user applications.
97#
98# gcc:
99# Setting -mno-mmx implies -mno-3dnow
100# Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3
101#
102# clang:
103# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
104# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
105#
106.if ${MACHINE_CPUARCH} == "i386"
107CFLAGS.gcc+=	-mpreferred-stack-boundary=2
108CFLAGS.clang+=	-mno-aes -mno-avx
109CFLAGS+=	-mno-mmx -mno-sse -msoft-float
110INLINE_LIMIT?=	8000
111.endif
112
113.if ${MACHINE_CPUARCH} == "arm"
114INLINE_LIMIT?=	8000
115.endif
116
117.if ${MACHINE_CPUARCH} == "aarch64"
118# We generally don't want fpu instructions in the kernel.
119CFLAGS += -mgeneral-regs-only
120# Reserve x18 for pcpu data
121CFLAGS += -ffixed-x18
122INLINE_LIMIT?=	8000
123.endif
124
125#
126# For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating
127# point registers within the kernel. However, for kernels supporting hardware
128# float (FPE), we have to include that in the march so we can have limited
129# floating point support in context switching needed for that. This is different
130# than userland where we use a hard-float ABI (lp64d).
131#
132# We also specify the "medium" code model, which generates code suitable for a
133# 2GiB addressing range located at any offset, allowing modules to be located
134# anywhere in the 64-bit address space.  Note that clang and GCC refer to this
135# code model as "medium" and "medany" respectively.
136#
137.if ${MACHINE_CPUARCH} == "riscv"
138CFLAGS+=	-march=rv64imafdc
139CFLAGS+=	-mabi=lp64
140CFLAGS.clang+=	-mcmodel=medium
141CFLAGS.gcc+=	-mcmodel=medany
142INLINE_LIMIT?=	8000
143
144.if ${LINKER_FEATURES:Mriscv-relaxations} == ""
145CFLAGS+=	-mno-relax
146.endif
147.endif
148
149#
150# For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
151# operations inside the kernel itself.  These operations are exclusively
152# reserved for user applications.
153#
154# gcc:
155# Setting -mno-mmx implies -mno-3dnow
156# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
157#
158# clang:
159# Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
160# Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
161# (-mfpmath= is not supported)
162#
163.if ${MACHINE_CPUARCH} == "amd64"
164CFLAGS.clang+=	-mno-aes -mno-avx
165CFLAGS+=	-mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
166		-fno-asynchronous-unwind-tables
167INLINE_LIMIT?=	8000
168.endif
169
170#
171# For PowerPC we tell gcc to use floating point emulation.  This avoids using
172# floating point registers for integer operations which it has a tendency to do.
173# Also explicitly disable Altivec instructions inside the kernel.
174#
175.if ${MACHINE_CPUARCH} == "powerpc"
176CFLAGS+=	-mno-altivec -msoft-float
177INLINE_LIMIT?=	15000
178.endif
179
180.if ${MACHINE_ARCH} == "powerpcspe"
181CFLAGS.gcc+=	-mno-spe
182.endif
183
184#
185# Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
186# DDB happy. ELFv2, if available, has some other efficiency benefits.
187#
188.if ${MACHINE_ARCH:Mpowerpc64*} != ""
189CFLAGS+=	-mabi=elfv2
190.endif
191
192#
193# For MIPS we also tell gcc to use floating point emulation
194#
195.if ${MACHINE_CPUARCH} == "mips"
196CFLAGS+=	-msoft-float
197INLINE_LIMIT?=	8000
198.endif
199
200#
201# GCC 3.0 and above like to do certain optimizations based on the
202# assumption that the program is linked against libc.  Stop this.
203#
204CFLAGS+=	-ffreestanding
205
206#
207# The C standard leaves signed integer overflow behavior undefined.
208# gcc and clang opimizers take advantage of this.  The kernel makes
209# use of signed integer wraparound mechanics so we need the compiler
210# to treat it as a wraparound and not take shortcuts.
211#
212CFLAGS+=	-fwrapv
213
214#
215# GCC SSP support
216#
217.if ${MK_SSP} != "no" && \
218    ${MACHINE_CPUARCH} != "arm" && ${MACHINE_CPUARCH} != "mips"
219CFLAGS+=	-fstack-protector
220.endif
221
222#
223# Retpoline speculative execution vulnerability mitigation (CVE-2017-5715)
224#
225.if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \
226    ${MK_KERNEL_RETPOLINE} != "no"
227CFLAGS+=	-mretpoline
228.endif
229
230#
231# Initialize stack variables on function entry
232#
233.if ${MK_INIT_ALL_ZERO} == "yes"
234.if ${COMPILER_FEATURES:Minit-all}
235CFLAGS+= -ftrivial-auto-var-init=zero \
236    -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
237.else
238.warning InitAll (zeros) requested but not support by compiler
239.endif
240.elif ${MK_INIT_ALL_PATTERN} == "yes"
241.if ${COMPILER_FEATURES:Minit-all}
242CFLAGS+= -ftrivial-auto-var-init=pattern
243.else
244.warning InitAll (pattern) requested but not support by compiler
245.endif
246.endif
247
248CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
249CFLAGS+= ${CWARNFLAGS.${COMPILER_TYPE}}
250CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
251
252# Tell bmake not to mistake standard targets for things to be searched for
253# or expect to ever be up-to-date.
254PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
255		beforelinking build build-tools buildfiles buildincludes \
256		checkdpadd clean cleandepend cleandir cleanobj configure \
257		depend distclean distribute exe \
258		html includes install installfiles installincludes \
259		obj objlink objs objwarn \
260		realinstall regress \
261		tags whereobj
262
263.PHONY: ${PHONY_NOTMAIN}
264.NOTMAIN: ${PHONY_NOTMAIN}
265
266CSTD=		c99
267
268.if ${CSTD} == "k&r"
269CFLAGS+=        -traditional
270.elif ${CSTD} == "c89" || ${CSTD} == "c90"
271CFLAGS+=        -std=iso9899:1990
272.elif ${CSTD} == "c94" || ${CSTD} == "c95"
273CFLAGS+=        -std=iso9899:199409
274.elif ${CSTD} == "c99"
275CFLAGS+=        -std=iso9899:1999
276.else # CSTD
277CFLAGS+=        -std=${CSTD}
278.endif # CSTD
279
280# Please keep this if in sync with bsd.sys.mk
281.if ${LD} != "ld" && (${CC:[1]:H} != ${LD:[1]:H} || ${LD:[1]:T} != "ld")
282# Add -fuse-ld=${LD} if $LD is in a different directory or not called "ld".
283# Note: Clang 12+ will prefer --ld-path= over -fuse-ld=.
284.if ${COMPILER_TYPE} == "clang"
285# Note: unlike bsd.sys.mk we can't use LDFLAGS here since that is used for the
286# flags required when linking the kernel. We don't need those flags when
287# building the vdsos. However, we do need -fuse-ld, so use ${CCLDFLAGS} instead.
288# Note: Clang does not like relative paths in -fuse-ld so we map ld.lld -> lld.
289CCLDFLAGS+=	-fuse-ld=${LD:[1]:S/^ld.//1W}
290.else
291# GCC does not support an absolute path for -fuse-ld so we just print this
292# warning instead and let the user add the required symlinks.
293.warning LD (${LD}) is not the default linker for ${CC} but -fuse-ld= is not supported
294.endif
295.endif
296
297# Set target-specific linker emulation name.
298LD_EMULATION_aarch64=aarch64elf
299LD_EMULATION_amd64=elf_x86_64_fbsd
300LD_EMULATION_arm=armelf_fbsd
301LD_EMULATION_armv6=armelf_fbsd
302LD_EMULATION_armv7=armelf_fbsd
303LD_EMULATION_i386=elf_i386_fbsd
304LD_EMULATION_mips= elf32btsmip_fbsd
305LD_EMULATION_mipshf= elf32btsmip_fbsd
306LD_EMULATION_mips64= elf64btsmip_fbsd
307LD_EMULATION_mips64hf= elf64btsmip_fbsd
308LD_EMULATION_mipsel= elf32ltsmip_fbsd
309LD_EMULATION_mipselhf= elf32ltsmip_fbsd
310LD_EMULATION_mips64el= elf64ltsmip_fbsd
311LD_EMULATION_mips64elhf= elf64ltsmip_fbsd
312LD_EMULATION_mipsn32= elf32btsmipn32_fbsd
313LD_EMULATION_mipsn32el= elf32btsmipn32_fbsd   # I don't think this is a thing that works
314LD_EMULATION_powerpc= elf32ppc_fbsd
315LD_EMULATION_powerpcspe= elf32ppc_fbsd
316LD_EMULATION_powerpc64= elf64ppc_fbsd
317LD_EMULATION_powerpc64le= elf64lppc_fbsd
318LD_EMULATION_riscv64= elf64lriscv
319LD_EMULATION_riscv64sf= elf64lriscv
320LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}
321