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# TODO: some day GCC will be able to LTO __builtin functions (i.e. the libc and 104# with it all libs). When this happens, this should become generic. This can 105# also happen when GCC14 is the oldest reasonable version we support, then this 106# can work with -ffat-lto-objects. 107cc-flags-llvm-clang += $(LTO_CFLAGS) 108 109# arm-clang has the same flags 110cc-flags-arm-clang += $(cc-flags-llvm-clang) 111 112cflags-common += ${DEFINES} ${INCLUDES} ${MBEDTLS_INC} -nostdinc 113 114cflags-common += -ffunction-sections -fdata-sections \ 115 -ffreestanding -fno-common \ 116 -Os -std=gnu99 117 118ifneq (${BP_OPTION},none) 119 cflags-common += -mbranch-protection=${BP_OPTION} 120endif #(BP_OPTION) 121 122ifeq (${SANITIZE_UB},on) 123 cflags-common += -fsanitize=undefined -fno-sanitize-recover 124endif #(${SANITIZE_UB},on) 125 126ifeq (${SANITIZE_UB},trap) 127 cflags-common += -fsanitize=undefined -fno-sanitize-recover \ 128 -fsanitize-undefined-trap-on-error 129endif #(${SANITIZE_UB},trap) 130 131ifeq (${ERROR_DEPRECATED},0) 132 cflags-common += -Wno-error=deprecated-declarations 133 cflags-common += -Wno-error=cpp 134endif #(!ERROR_DEPRECATED) 135 136################################################################################ 137# Platform specific Makefile might provide us ARCH_MAJOR/MINOR use that to come 138# up with appropriate march values for compiler. 139################################################################################ 140include ${MAKE_HELPERS_DIRECTORY}march.mk 141ifeq (${ARM_ARCH_MAJOR},7) 142include make_helpers/armv7-a-cpus.mk 143endif 144 145cflags-common += $(march-directive) 146 147ifneq ($(PIE_FOUND),) 148 cflags-common += -fno-PIE 149endif 150 151ifeq ($(ENABLE_LTO),1) 152ifeq ($($(ARCH)-ld-id),gnu-gcc) 153 cflags-common += -flto-partition=one 154endif 155endif 156 157cflags-common += $(TF_CFLAGS_$(ARCH)) 158cflags-common += $(CPPFLAGS) $(CFLAGS) # some platforms set these 159TF_CFLAGS += $(cflags-common) 160TF_CFLAGS += $(target-$(ARCH)-$($(ARCH)-cc-id)) 161TF_CFLAGS += $(cc-flags-$($(ARCH)-cc-id)) 162 163# it's logical to give the same flags to the linker when it's invoked through 164# the compiler. This is requied for LTO to work correctly 165ifeq ($($(ARCH)-ld-id),$($(ARCH)-cc-id)) 166 TF_LDFLAGS += $(cflags-common) 167 TF_LDFLAGS += $(cc-flags-$($(ARCH)-ld-id)) 168 TF_LDFLAGS += $(LTO_CFLAGS) 169endif 170 171TF_LDFLAGS += $(target-$(ARCH)-$($(ARCH)-ld-id)) 172 173ASFLAGS += -Wa,--fatal-warnings 174TF_LDFLAGS += -z noexecstack 175 176# LD = armlink 177ifeq ($($(ARCH)-ld-id),arm-link) 178 TF_LDFLAGS += --diag_error=warning --lto_level=O1 179 TF_LDFLAGS += --remove --info=unused,unusedsymbols 180 181# LD = gcc or clang 182else 183 ldflags-common := $(call ld_option,--no-warn-rwx-segments) 184 # ld.lld reports section type mismatch warnings, 185 # so don't add --fatal-warnings to it. 186 ifneq ($($(ARCH)-ld-id),$(filter $($(ARCH)-ld-id),llvm-clang llvm-lld)) 187 ldflags-common += $(call ld_prefix,--fatal-warnings) 188 endif 189 ldflags-common += $(call ld_prefix,--gc-sections) 190 ldflags-common += -z common-page-size=4096 # Configure page size constants 191 ldflags-common += -z max-page-size=4096 192 ldflags-common += $(call ld_prefix,--build-id=none) 193 ldflags-common += $(call ld_option,--sort-section=alignment) 194 195 ifeq ($(ENABLE_LTO),1) 196 ldflags-common += -fuse-linker-plugin 197 endif #(ENABLE_LTO) 198 199 ldflags-common += -nostdlib 200 201 ifeq ($($(ARCH)-ld-id),llvm-clang) 202 ldflags-common += -fuse-ld=lld 203 endif 204endif 205 206ifneq ($(PIE_FOUND),) 207ifeq ($($(ARCH)-ld-id),gnu-gcc) 208 ldflags-common += -no-pie 209endif 210endif #(PIE_FOUND) 211TF_LDFLAGS += $(ldflags-common) 212TF_LDFLAGS += $(ldflags-$(ARCH)) 213 214PIE_LDFLAGS += $(call ld_prefix,-pie) 215PIE_LDFLAGS += $(call ld_prefix,--no-dynamic-linker) 216 217ifeq ($(ENABLE_PIE),1) 218 ifeq ($(RESET_TO_BL2),1) 219 ifneq ($(BL2_IN_XIP_MEM),1) 220 BL2_CPPFLAGS += -fpie 221 BL2_CFLAGS += -fpie 222 BL2_LDFLAGS += $(PIE_LDFLAGS) 223 endif #(BL2_IN_XIP_MEM) 224 endif #(RESET_TO_BL2) 225 BL31_CPPFLAGS += -fpie 226 BL31_CFLAGS += -fpie 227 BL31_LDFLAGS += $(PIE_LDFLAGS) 228 229 BL32_CPPFLAGS += -fpie 230 BL32_CFLAGS += -fpie 231 BL32_LDFLAGS += $(PIE_LDFLAGS) 232endif #(ENABLE_PIE) 233 234BL1_CPPFLAGS += -DREPORT_ERRATA=${DEBUG} 235BL31_CPPFLAGS += -DREPORT_ERRATA=${DEBUG} 236BL32_CPPFLAGS += -DREPORT_ERRATA=${DEBUG} 237 238BL1_CPPFLAGS += -DIMAGE_AT_EL3 239ifeq ($(RESET_TO_BL2),1) 240 BL2_CPPFLAGS += -DIMAGE_AT_EL3 241else 242 BL2_CPPFLAGS += -DIMAGE_AT_EL1 243endif #(RESET_TO_BL2) 244 245ifeq (${ARCH},aarch64) 246 BL2U_CPPFLAGS += -DIMAGE_AT_EL1 247 BL31_CPPFLAGS += -DIMAGE_AT_EL3 248 BL32_CPPFLAGS += -DIMAGE_AT_EL1 249else 250 BL32_CPPFLAGS += -DIMAGE_AT_EL3 251endif 252 253ifeq (${SPD},spmd) 254 ifeq ($(findstring optee_sp,$(ARM_SPMC_MANIFEST_DTS)),optee_sp) 255 DTC_CPPFLAGS += -DOPTEE_SP_FW_CONFIG 256 endif 257 258 ifeq ($(findstring trusty_sp,$(ARM_SPMC_MANIFEST_DTS)),trusty_sp) 259 DTC_CPPFLAGS += -DTRUSTY_SP_FW_CONFIG 260 endif 261 262 ifeq ($(TS_SP_FW_CONFIG),1) 263 DTC_CPPFLAGS += -DTS_SP_FW_CONFIG 264 endif 265 266 ifneq ($(ARM_BL2_SP_LIST_DTS),) 267 DTC_CPPFLAGS += -DARM_BL2_SP_LIST_DTS=$(ARM_BL2_SP_LIST_DTS) 268 endif 269endif 270 271 272DTC_FLAGS += -I dts -O dtb 273DTC_CPPFLAGS += -Ifdts -undef 274 275