xref: /rk3399_ARM-atf/make_helpers/cflags.mk (revision 6fb6bee1dfd7fd896c44cc21b02b4ef3aad3bbd0)
1#
2# Copyright (c) 2025, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6
7GCC_V_OUTPUT		:=	$(if $($(ARCH)-cc),$(shell $($(ARCH)-cc) -v 2>&1))
8PIE_FOUND		:=	$(findstring --enable-default-pie,${GCC_V_OUTPUT})
9
10ifneq (${DEBUG}, 0)
11	cflags-common		+=	-g -gdwarf-4
12endif #(Debug)
13
14ifeq (${AARCH32_INSTRUCTION_SET},A32)
15	cflags-aarch32		:=	-marm
16else ifeq (${AARCH32_INSTRUCTION_SET},T32)
17	cflags-aarch32		:=	-mthumb
18endif #(AARCH32_INSTRUCTION_SET)
19
20cflags-aarch32			+=	-mno-unaligned-access
21cflags-aarch64			:=	-mgeneral-regs-only -mstrict-align
22
23cflags-common			+=	$(cflags-$(ARCH))
24
25##############################################################################
26# WARNINGS Configuration
27###############################################################################
28# General warnings
29WARNING0		:=	-Wall -Wmissing-include-dirs -Wunused	\
30				-Wdisabled-optimization -Wvla -Wshadow	\
31				-Wredundant-decls
32# stricter warnings
33WARNING0		+=	-Wextra -Wno-trigraphs
34# too verbose for generic build
35WARNING0		+=	-Wno-missing-field-initializers \
36				-Wno-type-limits -Wno-sign-compare \
37# on clang this flag gets reset if -Wextra is set after it. No difference on gcc
38WARNING0		+=	-Wno-unused-parameter
39
40# Additional warnings
41# Level 1 - infrequent warnings we should have none of
42# full -Wextra
43WARNING1 := $(WARNING0)
44WARNING1 += -Wsign-compare
45WARNING1 += -Wtype-limits
46WARNING1 += -Wmissing-field-initializers
47
48# Level 2 - problematic warnings that we want
49# zlib, compiler-rt, coreboot, and mbdedtls blow up with these
50# TODO: disable just for them and move into default build
51WARNING2 := $(WARNING1)
52WARNING2 += -Wold-style-definition
53WARNING2 += -Wmissing-prototypes
54WARNING2 += -Wmissing-format-attribute
55# TF-A aims to comply with this eventually. Effort too large at present
56WARNING2 += -Wundef
57# currently very involved and many platforms set this off
58WARNING2 += -Wunused-const-variable=2
59
60# Level 3 - very pedantic, frequently ignored
61WARNING3 := $(WARNING2)
62WARNING3 += -Wbad-function-cast
63WARNING3 += -Waggregate-return
64WARNING3 += -Wnested-externs
65WARNING3 += -Wcast-align
66WARNING3 += -Wcast-qual
67WARNING3 += -Wconversion
68WARNING3 += -Wpacked
69WARNING3 += -Wpointer-arith
70WARNING3 += -Wswitch-default
71
72cflags-common	+=	$(WARNING$(W))
73ifneq (${E},0)
74	cflags-common	+= -Werror
75endif #(E)
76
77# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105523
78TF_CFLAGS_MIN_PAGE_SIZE	:=	$(call cc_option, --param=min-pagesize=0)
79ifeq ($(HARDEN_SLS), 1)
80        TF_CFLAGS_MHARDEN_SLS	:=      $(call cc_option, -mharden-sls=all)
81endif
82
83LTO_CFLAGS		:=	$(if $(call bool,$(ENABLE_LTO)),-flto)
84
85# Compiler specific warnings
86cc-flags-gnu-gcc	+=	-Wunused-but-set-variable -Wmaybe-uninitialized	\
87				-Wpacked-bitfield-compat -Wshift-overflow=2 \
88				-Wlogical-op $(TF_CFLAGS_MIN_PAGE_SIZE) $(TF_CFLAGS_MHARDEN_SLS)
89cc-flags-llvm-clang	+=	-Wshift-overflow -Wshift-sign-overflow \
90				-Wlogical-op-parentheses
91# TODO: some day GCC will be able to LTO __builtin functions (i.e. the libc and
92# with it all libs). When this happens, this should become generic. This can
93# also happen when GCC14 is the oldest reasonable version we support, then this
94# can work with -ffat-lto-objects.
95cc-flags-llvm-clang	+=	$(LTO_CFLAGS)
96
97# arm-clang has the same flags
98cc-flags-arm-clang	+=	$(cc-flags-llvm-clang)
99
100cflags-common		+=	${DEFINES} ${INCLUDES} ${MBEDTLS_INC} -nostdinc
101
102cflags-common		+=	-ffunction-sections -fdata-sections		\
103				-ffreestanding -fno-common			\
104				-Os -std=gnu99
105
106ifneq (${BP_OPTION},none)
107	cflags-common	+=	-mbranch-protection=${BP_OPTION}
108endif #(BP_OPTION)
109
110ifeq (${SANITIZE_UB},on)
111	cflags-common	+=	-fsanitize=undefined -fno-sanitize-recover
112endif #(${SANITIZE_UB},on)
113
114ifeq (${SANITIZE_UB},trap)
115	cflags-common	+=	-fsanitize=undefined -fno-sanitize-recover	\
116				-fsanitize-undefined-trap-on-error
117endif #(${SANITIZE_UB},trap)
118
119ifeq (${ERROR_DEPRECATED},0)
120        cflags-common	+= 	-Wno-error=deprecated-declarations
121        cflags-common	+= 	-Wno-error=cpp
122endif #(!ERROR_DEPRECATED)
123
124################################################################################
125# Platform specific Makefile might provide us ARCH_MAJOR/MINOR use that to come
126# up with appropriate march values for compiler.
127################################################################################
128include ${MAKE_HELPERS_DIRECTORY}march.mk
129ifeq (${ARM_ARCH_MAJOR},7)
130include make_helpers/armv7-a-cpus.mk
131endif
132
133cflags-common		+=	$(march-directive)
134
135ifneq ($(PIE_FOUND),)
136        cflags-common	+=	-fno-PIE
137endif
138
139ifeq ($(ENABLE_LTO),1)
140ifeq ($($(ARCH)-ld-id),gnu-gcc)
141        cflags-common	+=	-flto-partition=one
142endif
143endif
144
145cflags-common		+=	$(TF_CFLAGS_$(ARCH))
146cflags-common		+=	$(CPPFLAGS) $(CFLAGS) # some platforms set these
147TF_CFLAGS		+=	$(cflags-common)
148TF_CFLAGS		+=	$(target-flag-$(ARCH)-$($(ARCH)-cc-id))
149TF_CFLAGS		+=	$(cc-flags-$($(ARCH)-cc-id))
150
151# it's logical to give the same flags to the linker when it's invoked through
152# the compiler. This is requied for LTO to work correctly
153ifeq ($($(ARCH)-ld-id),$($(ARCH)-cc-id))
154        TF_LDFLAGS	+= 	$(cflags-common)
155        TF_LDFLAGS	+=	$(cc-flags-$($(ARCH)-ld-id))
156        TF_LDFLAGS	+=	$(LTO_CFLAGS)
157endif
158
159TF_LDFLAGS		+= 	$(target-flag-$(ARCH)-$($(ARCH)-ld-id))
160
161ASFLAGS			+=	-Wa,--fatal-warnings
162TF_LDFLAGS		+=	-z noexecstack
163
164# LD = armlink
165ifeq ($($(ARCH)-ld-id),arm-link)
166	TF_LDFLAGS		+=	--diag_error=warning --lto_level=O1
167	TF_LDFLAGS		+=	--remove --info=unused,unusedsymbols
168
169# LD = gcc or clang
170else
171        ldflags-common		:=	$(call ld_option,--no-warn-rwx-segments)
172        # ld.lld reports section type mismatch warnings,
173        # so don't add --fatal-warnings to it.
174        ifneq ($($(ARCH)-ld-id),$(filter $($(ARCH)-ld-id),llvm-clang llvm-lld))
175                ldflags-common	+=	$(call ld_prefix,--fatal-warnings)
176        endif
177        ldflags-common		+=	$(call ld_prefix,--gc-sections)
178        ldflags-common		+=	-z common-page-size=4096 # Configure page size constants
179        ldflags-common		+=	-z max-page-size=4096
180        ldflags-common		+=	$(call ld_prefix,--build-id=none)
181        ldflags-common		+=	$(call ld_option,--sort-section=alignment)
182
183        ifeq ($(ENABLE_LTO),1)
184                ldflags-common	+=	-fuse-linker-plugin
185        endif #(ENABLE_LTO)
186
187        ldflags-common		+= 	-nostdlib
188
189        ifeq ($($(ARCH)-ld-id),llvm-clang)
190                ldflags-common		+=	-fuse-ld=lld
191        endif
192
193        ifneq ($(call bool,$(USE_ROMLIB)),)
194                ldflags-common	+= @${BUILD_PLAT}/romlib/romlib.ldflags
195        endif
196endif
197
198# Errata build flags
199ifneq ($(call bool,$(ERRATA_A53_843419)),)
200ldflags-aarch64		+= $(call ld_option,--fix-cortex-a53-843419)
201else
202# GCC automatically adds fix-cortex-a53-843419 flag when used to link
203# which breaks some builds, so disable if errata fix is not explicitly enabled
204ldflags-aarch64		+= $(call ld_option,--no-fix-cortex-a53-843419)
205endif
206
207ifneq ($(call bool,$(ERRATA_A53_835769)),)
208cflags-aarch64		+= -mfix-cortex-a53-835769
209ldflags-aarch64		+= $(call ld_option,--fix-cortex-a53-835769)
210endif
211
212ifneq ($(PIE_FOUND),)
213ifeq ($($(ARCH)-ld-id),gnu-gcc)
214        ldflags-common	+=	-no-pie
215endif
216endif #(PIE_FOUND)
217TF_LDFLAGS	+=	$(ldflags-common)
218TF_LDFLAGS	+=	$(ldflags-$(ARCH))
219
220PIE_LDFLAGS	+=	$(call ld_prefix,-pie)
221PIE_LDFLAGS	+=	$(call ld_prefix,--no-dynamic-linker)
222
223ifeq ($(ENABLE_PIE),1)
224	ifeq ($(RESET_TO_BL2),1)
225		ifneq ($(BL2_IN_XIP_MEM),1)
226			BL2_CPPFLAGS	+=	-fpie
227			BL2_CFLAGS	+=	-fpie
228			BL2_LDFLAGS	+=	$(PIE_LDFLAGS)
229		endif #(BL2_IN_XIP_MEM)
230	endif #(RESET_TO_BL2)
231	BL31_CPPFLAGS	+=	-fpie
232	BL31_CFLAGS 	+=	-fpie
233	BL31_LDFLAGS	+=	$(PIE_LDFLAGS)
234
235	BL32_CPPFLAGS	+=	-fpie
236	BL32_CFLAGS	+=	-fpie
237	BL32_LDFLAGS	+=	$(PIE_LDFLAGS)
238endif #(ENABLE_PIE)
239
240BL1_CPPFLAGS  += -DREPORT_ERRATA=${DEBUG}
241BL31_CPPFLAGS += -DREPORT_ERRATA=${DEBUG}
242BL32_CPPFLAGS += -DREPORT_ERRATA=${DEBUG}
243
244BL1_CPPFLAGS += -DIMAGE_AT_EL3
245ifeq ($(RESET_TO_BL2),1)
246	BL2_CPPFLAGS += -DIMAGE_AT_EL3
247else
248	BL2_CPPFLAGS += -DIMAGE_AT_EL1
249endif #(RESET_TO_BL2)
250
251ifeq (${ARCH},aarch64)
252	BL2U_CPPFLAGS += -DIMAGE_AT_EL1
253	BL31_CPPFLAGS += -DIMAGE_AT_EL3
254	BL32_CPPFLAGS += -DIMAGE_AT_EL1
255else
256	BL32_CPPFLAGS += -DIMAGE_AT_EL3
257endif
258
259ifeq (${SPD},spmd)
260	ifeq ($(findstring optee_sp,$(ARM_SPMC_MANIFEST_DTS)),optee_sp)
261		DTC_CPPFLAGS	+=	-DOPTEE_SP_FW_CONFIG
262	endif
263
264	ifeq ($(findstring trusty_sp,$(ARM_SPMC_MANIFEST_DTS)),trusty_sp)
265		DTC_CPPFLAGS	+=	-DTRUSTY_SP_FW_CONFIG
266	endif
267
268	ifeq ($(TS_SP_FW_CONFIG),1)
269		DTC_CPPFLAGS	+=	-DTS_SP_FW_CONFIG
270	endif
271
272	ifneq ($(ARM_BL2_SP_LIST_DTS),)
273		DTC_CPPFLAGS += -DARM_BL2_SP_LIST_DTS=$(ARM_BL2_SP_LIST_DTS)
274	endif
275endif
276
277
278DTC_FLAGS		+=	-I dts -O dtb
279DTC_CPPFLAGS		+=	-Ifdts -undef
280
281