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