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