summaryrefslogtreecommitdiff
path: root/make_helpers/cflags.mk
blob: 58ecf38d65ab2dd25370b83c258c8f4c065ca306 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#
# Copyright (c) 2025-2026, Arm Limited. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#

GCC_V_OUTPUT		:=	$(if $($(ARCH)-cc),$(shell $($(ARCH)-cc) -v 2>&1))
PIE_FOUND		:=	$(findstring --enable-default-pie,${GCC_V_OUTPUT})

ifneq (${DEBUG}, 0)
	cflags-common		+=	-g -gdwarf-4
endif #(Debug)

ifeq (${AARCH32_INSTRUCTION_SET},A32)
	cflags-aarch32		:=	-marm
else ifeq (${AARCH32_INSTRUCTION_SET},T32)
	cflags-aarch32		:=	-mthumb
endif #(AARCH32_INSTRUCTION_SET)

cflags-aarch32			+=	-mno-unaligned-access
cflags-aarch64			:=	-mgeneral-regs-only -mstrict-align

cflags-common			+=	$(cflags-$(ARCH))

##############################################################################
# WARNINGS Configuration
###############################################################################
# General warnings
WARNING0		:=	-Wall -Wmissing-include-dirs -Wunused	\
				-Wdisabled-optimization -Wvla -Wshadow	\
				-Wredundant-decls
# stricter warnings
WARNING0		+=	-Wextra -Wno-trigraphs
# too verbose for generic build
WARNING0		+=	-Wno-missing-field-initializers \
				-Wno-type-limits -Wno-sign-compare \
# on clang this flag gets reset if -Wextra is set after it. No difference on gcc
WARNING0		+=	-Wno-unused-parameter

# Additional warnings
# Level 1 - infrequent warnings we should have none of
# full -Wextra
WARNING1 := $(WARNING0)
WARNING1 += -Wsign-compare
WARNING1 += -Wtype-limits
WARNING1 += -Wmissing-field-initializers

# Level 2 - problematic warnings that we want
# zlib, compiler-rt, coreboot, and mbdedtls blow up with these
# TODO: disable just for them and move into default build
WARNING2 := $(WARNING1)
WARNING2 += -Wold-style-definition
WARNING2 += -Wmissing-prototypes
WARNING2 += -Wmissing-format-attribute
# TF-A aims to comply with this eventually. Effort too large at present
WARNING2 += -Wundef
# currently very involved and many platforms set this off
WARNING2 += -Wunused-const-variable=2

# Level 3 - very pedantic, frequently ignored
WARNING3 := $(WARNING2)
WARNING3 += -Wbad-function-cast
WARNING3 += -Waggregate-return
WARNING3 += -Wnested-externs
WARNING3 += -Wcast-align
WARNING3 += -Wcast-qual
WARNING3 += -Wconversion
WARNING3 += -Wpacked
WARNING3 += -Wpointer-arith
WARNING3 += -Wswitch-default

cflags-common	+=	$(WARNING$(W))
ifneq (${E},0)
	cflags-common	+= -Werror
endif #(E)

# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105523
TF_CFLAGS_MIN_PAGE_SIZE	:=	$(call cc_option, --param=min-pagesize=0)
ifeq ($(HARDEN_SLS), 1)
        TF_CFLAGS_MHARDEN_SLS	:=      $(call cc_option, -mharden-sls=all)
endif

LTO_CFLAGS		:=	$(if $(call bool,$(ENABLE_LTO)),-flto)

# Compiler specific warnings
cc-flags-gnu-gcc	+=	-Wunused-but-set-variable -Wmaybe-uninitialized	\
				-Wpacked-bitfield-compat -Wshift-overflow=2 \
				-Wlogical-op $(TF_CFLAGS_MIN_PAGE_SIZE) $(TF_CFLAGS_MHARDEN_SLS)
cc-flags-llvm-clang	+=	-Wshift-overflow -Wshift-sign-overflow \
				-Wlogical-op-parentheses
# TODO: some day GCC will be able to LTO __builtin functions (i.e. the libc and
# with it all libs). When this happens, this should become generic. This can
# also happen when GCC14 is the oldest reasonable version we support, then this
# can work with -ffat-lto-objects.
cc-flags-llvm-clang	+=	$(LTO_CFLAGS)

# arm-clang has the same flags
cc-flags-arm-clang	+=	$(cc-flags-llvm-clang)

cflags-common		+=	${DEFINES} ${INCLUDES} ${MBEDTLS_INC} -nostdinc

cflags-common		+=	-ffunction-sections -fdata-sections		\
				-ffreestanding -fno-common			\
				-Os -std=gnu99

ifneq (${BP_OPTION},none)
	cflags-common	+=	-mbranch-protection=${BP_OPTION}
endif #(BP_OPTION)

ifeq (${SANITIZE_UB},on)
	cflags-common	+=	-fsanitize=undefined -fno-sanitize-recover
endif #(${SANITIZE_UB},on)

ifeq (${SANITIZE_UB},trap)
	cflags-common	+=	-fsanitize=undefined -fno-sanitize-recover	\
				-fsanitize-undefined-trap-on-error
endif #(${SANITIZE_UB},trap)

ifeq (${ERROR_DEPRECATED},0)
        cflags-common	+= 	-Wno-error=deprecated-declarations
        cflags-common	+= 	-Wno-error=cpp
endif #(!ERROR_DEPRECATED)

################################################################################
# Platform specific Makefile might provide us ARCH_MAJOR/MINOR use that to come
# up with appropriate march values for compiler.
################################################################################
include ${MAKE_HELPERS_DIRECTORY}march.mk
ifeq (${ARM_ARCH_MAJOR},7)
include make_helpers/armv7-a-cpus.mk
endif

ifneq ($(ENABLE_FEAT_MORELLO),0)
        TF_CFLAGS	+=	-march=morello
else
        cflags-common	+=	$(march-directive)
endif

ifneq ($(PIE_FOUND),)
        cflags-common	+=	-fno-PIE
endif

cflags-common		+=	$(TF_CFLAGS_$(ARCH))
cflags-common		+=	$(CPPFLAGS) $(CFLAGS) # some platforms set these
TF_CFLAGS		+=	$(cflags-common)
TF_CFLAGS		+=	$(target-flag-$(ARCH)-$($(ARCH)-cc-id))
TF_CFLAGS		+=	$(cc-flags-$($(ARCH)-cc-id))

# it's logical to give the same flags to the linker when it's invoked through
# the compiler. This is requied for LTO to work correctly
ifeq ($($(ARCH)-ld-id),$($(ARCH)-cc-id))
        TF_LDFLAGS	+= 	$(cflags-common)
        TF_LDFLAGS	+=	$(cc-flags-$($(ARCH)-ld-id))
        TF_LDFLAGS	+=	$(LTO_CFLAGS)
endif

TF_LDFLAGS		+= 	$(target-flag-$(ARCH)-$($(ARCH)-ld-id))

ASFLAGS			+=	-Wa,--fatal-warnings
TF_LDFLAGS		+=	-z noexecstack

# LD = armlink
ifeq ($($(ARCH)-ld-id),arm-link)
	TF_LDFLAGS		+=	--diag_error=warning --lto_level=O1
	TF_LDFLAGS		+=	--remove --info=unused,unusedsymbols

# LD = gcc or clang
else
        ifeq ($($(ARCH)-ld-id),llvm-clang)
                ldflags-common	:=	-fuse-ld=lld
        endif

        ifeq ($(ENABLE_LTO),1)
        ifeq ($($(ARCH)-ld-id),gnu-gcc)
                ldflags-common	+=	-flto-partition=one
        endif
        endif

        ldflags-common		+=	$(call ld_option,--no-warn-rwx-segments)
        # ld.lld reports section type mismatch warnings,
        # so don't add --fatal-warnings to it.
        ifneq ($($(ARCH)-ld-id),$(filter $($(ARCH)-ld-id),llvm-clang llvm-lld))
                ldflags-common	+=	$(call ld_prefix,--fatal-warnings)
        endif
        ldflags-common		+=	$(call ld_prefix,--gc-sections)
        ldflags-common		+=	-z common-page-size=4096 # Configure page size constants
        ldflags-common		+=	-z max-page-size=4096
        ldflags-common		+=	$(call ld_prefix,--build-id=none)
        ldflags-common		+=	$(call ld_option,--sort-section=alignment)

        ifeq ($(ENABLE_LTO),1)
                ldflags-common	+=	-fuse-linker-plugin
        endif #(ENABLE_LTO)

        ldflags-common		+= 	-nostdlib

        ifneq ($(call bool,$(USE_ROMLIB)),)
                ldflags-common	+= @${BUILD_PLAT}/romlib/romlib.ldflags
        endif
endif

# Errata build flags
ifneq ($(call bool,$(ERRATA_A53_843419)),)
ldflags-aarch64		+= $(call ld_option,--fix-cortex-a53-843419)
else
# GCC automatically adds fix-cortex-a53-843419 flag when used to link
# which breaks some builds, so disable if errata fix is not explicitly enabled
ldflags-aarch64		+= $(call ld_option,--no-fix-cortex-a53-843419)
endif

ifneq ($(call bool,$(ERRATA_A53_835769)),)
cflags-aarch64		+= -mfix-cortex-a53-835769
ldflags-aarch64		+= $(call ld_option,--fix-cortex-a53-835769)
endif

ifneq ($(PIE_FOUND),)
ifeq ($($(ARCH)-ld-id),gnu-gcc)
        ldflags-common	+=	-no-pie
endif
endif #(PIE_FOUND)
TF_LDFLAGS	+=	$(ldflags-common)
TF_LDFLAGS	+=	$(ldflags-$(ARCH))

PIE_LDFLAGS	+=	$(call ld_prefix,-pie)
PIE_LDFLAGS	+=	$(call ld_prefix,--no-dynamic-linker)

ifeq ($(ENABLE_PIE),1)
	ifeq ($(RESET_TO_BL2),1)
		ifneq ($(BL2_IN_XIP_MEM),1)
			BL2_CPPFLAGS	+=	-fpie
			BL2_CFLAGS	+=	-fpie
			BL2_LDFLAGS	+=	$(PIE_LDFLAGS)
		endif #(BL2_IN_XIP_MEM)
	endif #(RESET_TO_BL2)
	BL31_CPPFLAGS	+=	-fpie
	BL31_CFLAGS 	+=	-fpie
	BL31_LDFLAGS	+=	$(PIE_LDFLAGS)

	BL32_CPPFLAGS	+=	-fpie
	BL32_CFLAGS	+=	-fpie
	BL32_LDFLAGS	+=	$(PIE_LDFLAGS)
endif #(ENABLE_PIE)

BL1_CPPFLAGS  += -DREPORT_ERRATA=${DEBUG}
BL2_CPPFLAGS  += -DREPORT_ERRATA=0
BL31_CPPFLAGS += -DREPORT_ERRATA=${DEBUG}
BL32_CPPFLAGS += -DREPORT_ERRATA=${DEBUG}

BL1_CPPFLAGS += -DIMAGE_AT_EL3
ifeq ($(BL2_RUNS_AT_EL3),1)
	BL2_CPPFLAGS += -DIMAGE_AT_EL3
else
	BL2_CPPFLAGS += -DIMAGE_AT_EL1
endif #(BL2_RUNS_AT_EL3)

ifeq (${ARCH},aarch64)
	BL2U_CPPFLAGS += -DIMAGE_AT_EL1
	BL31_CPPFLAGS += -DIMAGE_AT_EL3
	BL32_CPPFLAGS += -DIMAGE_AT_EL1
else
	BL32_CPPFLAGS += -DIMAGE_AT_EL3
endif

ifeq (${SPD},spmd)
	ifeq ($(findstring optee_sp,$(ARM_SPMC_MANIFEST_DTS)),optee_sp)
		DTC_CPPFLAGS	+=	-DOPTEE_SP_FW_CONFIG
	endif

	ifeq ($(findstring trusty_sp,$(ARM_SPMC_MANIFEST_DTS)),trusty_sp)
		DTC_CPPFLAGS	+=	-DTRUSTY_SP_FW_CONFIG
	endif

	ifeq ($(TS_SP_FW_CONFIG),1)
		DTC_CPPFLAGS	+=	-DTS_SP_FW_CONFIG
	endif

	ifneq ($(ARM_BL2_SP_LIST_DTS),)
		DTC_CPPFLAGS += -DARM_BL2_SP_LIST_DTS=$(ARM_BL2_SP_LIST_DTS)
	endif
endif


DTC_FLAGS		+=	-I dts -O dtb
DTC_CPPFLAGS		+=	-Ifdts -undef