xref: /rk3399_ARM-atf/make_helpers/cflags.mk (revision 885ed9e05eace7f128629380bd0b1a72bb6f6b2c)
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
10################################################################################
11# Compiler Configuration for the correct ARCH
12################################################################################
13target-aarch32-arm-clang	:=	-target arm-arm-none-eabi
14target-aarch64-arm-clang	:=	-target aarch64-arm-none-eabi
15ifeq (${ARM_ARCH_MAJOR},7)
16        target-aarch32-llvm-clang	:= 	-target arm-none-eabi
17else
18        target-aarch32-llvm-clang	:= 	-target armv8a-none-eabi
19endif #(ARM_ARCH_MAJOR)
20target-aarch64-llvm-clang	:=	-target aarch64-elf
21
22ifneq (${DEBUG}, 0)
23	cflags-common		+=	-g -gdwarf-4
24endif #(Debug)
25
26ifeq (${AARCH32_INSTRUCTION_SET},A32)
27	cflags-aarch32		:=	-marm
28else ifeq (${AARCH32_INSTRUCTION_SET},T32)
29	cflags-aarch32		:=	-mthumb
30endif #(AARCH32_INSTRUCTION_SET)
31
32cflags-aarch32			+=	-mno-unaligned-access
33cflags-aarch64			:=	-mgeneral-regs-only -mstrict-align
34
35cflags-common			+=	$(cflags-$(ARCH))
36
37##############################################################################
38# WARNINGS Configuration
39###############################################################################
40# General warnings
41WARNING0		:=	-Wall -Wmissing-include-dirs -Wunused	\
42				-Wdisabled-optimization -Wvla -Wshadow	\
43				-Wredundant-decls
44# stricter warnings
45WARNING0		+=	-Wextra -Wno-trigraphs
46# too verbose for generic build
47WARNING0		+=	-Wno-missing-field-initializers \
48				-Wno-type-limits -Wno-sign-compare \
49# on clang this flag gets reset if -Wextra is set after it. No difference on gcc
50WARNING0		+=	-Wno-unused-parameter
51
52# Additional warnings
53# Level 1 - infrequent warnings we should have none of
54# full -Wextra
55WARNING1 := $(WARNING0)
56WARNING1 += -Wsign-compare
57WARNING1 += -Wtype-limits
58WARNING1 += -Wmissing-field-initializers
59
60# Level 2 - problematic warnings that we want
61# zlib, compiler-rt, coreboot, and mbdedtls blow up with these
62# TODO: disable just for them and move into default build
63WARNING2 := $(WARNING1)
64WARNING2 += -Wold-style-definition
65WARNING2 += -Wmissing-prototypes
66WARNING2 += -Wmissing-format-attribute
67# TF-A aims to comply with this eventually. Effort too large at present
68WARNING2 += -Wundef
69# currently very involved and many platforms set this off
70WARNING2 += -Wunused-const-variable=2
71
72# Level 3 - very pedantic, frequently ignored
73WARNING3 := $(WARNING2)
74WARNING3 += -Wbad-function-cast
75WARNING3 += -Waggregate-return
76WARNING3 += -Wnested-externs
77WARNING3 += -Wcast-align
78WARNING3 += -Wcast-qual
79WARNING3 += -Wconversion
80WARNING3 += -Wpacked
81WARNING3 += -Wpointer-arith
82WARNING3 += -Wswitch-default
83
84cflags-common	+=	$(WARNING$(W))
85ifneq (${E},0)
86	cflags-common	+= -Werror
87endif #(E)
88
89# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105523
90TF_CFLAGS_MIN_PAGE_SIZE	:=	$(call cc_option, --param=min-pagesize=0)
91ifeq ($(HARDEN_SLS), 1)
92        TF_CFLAGS_MHARDEN_SLS	:=      $(call cc_option, -mharden-sls=all)
93endif
94
95LTO_CFLAGS		:=	$(if $(call bool,$(ENABLE_LTO)),-flto)
96
97# Compiler specific warnings
98cc-flags-gnu-gcc	+=	-Wunused-but-set-variable -Wmaybe-uninitialized	\
99				-Wpacked-bitfield-compat -Wshift-overflow=2 \
100				-Wlogical-op $(TF_CFLAGS_MIN_PAGE_SIZE) $(TF_CFLAGS_MHARDEN_SLS)
101cc-flags-llvm-clang	+=	-Wshift-overflow -Wshift-sign-overflow \
102				-Wlogical-op-parentheses
103# arm-clang has the same flags
104cc-flags-arm-clang	+=	$(cc-flags-llvm-clang)
105
106cflags-common		+=	${DEFINES} ${INCLUDES} ${MBEDTLS_INC} -nostdinc
107
108cflags-common		+=	-ffunction-sections -fdata-sections		\
109				-ffreestanding -fno-common			\
110				-Os -std=gnu99
111
112ifneq (${BP_OPTION},none)
113	cflags-common	+=	-mbranch-protection=${BP_OPTION}
114endif #(BP_OPTION)
115
116ifeq (${SANITIZE_UB},on)
117	cflags-common	+=	-fsanitize=undefined -fno-sanitize-recover
118endif #(${SANITIZE_UB},on)
119
120ifeq (${SANITIZE_UB},trap)
121	cflags-common	+=	-fsanitize=undefined -fno-sanitize-recover	\
122				-fsanitize-undefined-trap-on-error
123endif #(${SANITIZE_UB},trap)
124
125ifeq (${ERROR_DEPRECATED},0)
126        cflags-common	+= 	-Wno-error=deprecated-declarations
127        cflags-common	+= 	-Wno-error=cpp
128endif #(!ERROR_DEPRECATED)
129
130################################################################################
131# Platform specific Makefile might provide us ARCH_MAJOR/MINOR use that to come
132# up with appropriate march values for compiler.
133################################################################################
134include ${MAKE_HELPERS_DIRECTORY}march.mk
135ifeq (${ARM_ARCH_MAJOR},7)
136include make_helpers/armv7-a-cpus.mk
137endif
138
139cflags-common		+=	$(march-directive)
140
141ifneq ($(PIE_FOUND),)
142        cflags-common	+=	-fno-PIE
143endif
144
145cflags-common		+=	$(TF_CFLAGS_$(ARCH))
146cflags-common		+=	$(CPPFLAGS) $(CFLAGS) # some platforms set these
147TF_CFLAGS		+=	$(cflags-common)
148TF_CFLAGS		+=	$(target-$(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-$(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	TF_LDFLAGS		+=	$(TF_LDFLAGS_$(ARCH))
169
170# LD = gcc (used when GCC LTO is enabled)
171else ifeq ($($(ARCH)-ld-id),gnu-gcc)
172	# Pass ld options with Wl or Xlinker switches
173	TF_LDFLAGS		+=	$(call ld_option,-Xlinker --no-warn-rwx-segments)
174	TF_LDFLAGS		+=	-Wl,--fatal-warnings
175	TF_LDFLAGS		+=	-Wl,--gc-sections
176
177	TF_LDFLAGS		+=	-Wl,-z,common-page-size=4096 #Configure page size constants
178	TF_LDFLAGS		+=	-Wl,-z,max-page-size=4096
179	TF_LDFLAGS		+=	-Wl,--build-id=none
180
181	ifeq ($(ENABLE_LTO),1)
182		TF_LDFLAGS	+=	-fuse-linker-plugin
183		TF_LDFLAGS      +=	-flto-partition=one
184	endif #(ENABLE_LTO)
185
186	TF_LDFLAGS		+= 	-nostdlib
187	TF_LDFLAGS		+=	$(subst --,-Xlinker --,$(TF_LDFLAGS_$(ARCH)))
188
189# LD = gcc-ld (ld) or llvm-ld (ld.lld) or other
190else
191# With ld.bfd version 2.39 and newer new warnings are added. Skip those since we
192# are not loaded by a elf loader.
193	TF_LDFLAGS		+=	$(call ld_option, --no-warn-rwx-segments)
194	TF_LDFLAGS		+=	-O1
195	TF_LDFLAGS		+=	--gc-sections
196
197	TF_LDFLAGS		+=	-z common-page-size=4096 # Configure page size constants
198	TF_LDFLAGS		+=	-z max-page-size=4096
199	TF_LDFLAGS		+=	--build-id=none
200
201# ld.lld doesn't recognize the errata flags,
202# therefore don't add those in that case.
203# ld.lld reports section type mismatch warnings,
204# therefore don't add --fatal-warnings to it.
205	ifneq ($($(ARCH)-ld-id),llvm-lld)
206		TF_LDFLAGS	+=	$(TF_LDFLAGS_$(ARCH)) --fatal-warnings
207	endif
208
209endif #(LD = armlink)
210
211ifneq ($(PIE_FOUND),)
212ifeq ($($(ARCH)-ld-id),gnu-gcc)
213	TF_LDFLAGS	+=	-no-pie
214endif
215endif #(PIE_FOUND)
216
217ifeq ($($(ARCH)-ld-id),gnu-gcc)
218	PIE_LDFLAGS	+=	-Wl,-pie -Wl,--no-dynamic-linker
219else
220	PIE_LDFLAGS	+=	-pie --no-dynamic-linker
221endif
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