162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci# Copyright (C) 2020 ARM Limited
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci# preserve CC value from top level Makefile
562306a36Sopenharmony_ciifeq ($(CC),cc)
662306a36Sopenharmony_ciCC := $(CROSS_COMPILE)gcc
762306a36Sopenharmony_ciendif
862306a36Sopenharmony_ci
962306a36Sopenharmony_ciCFLAGS += -mbranch-protection=pac-ret
1062306a36Sopenharmony_ci# check if the compiler supports ARMv8.3 and branch protection with PAuth
1162306a36Sopenharmony_cipauth_cc_support := $(shell if ($(CC) $(CFLAGS) -march=armv8.3-a -E -x c /dev/null -o /dev/null 2>&1) then echo "1"; fi)
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ciifeq ($(pauth_cc_support),1)
1462306a36Sopenharmony_ciTEST_GEN_PROGS := pac
1562306a36Sopenharmony_ciTEST_GEN_FILES := pac_corruptor.o helper.o
1662306a36Sopenharmony_ciTEST_GEN_PROGS_EXTENDED := exec_target
1762306a36Sopenharmony_ciendif
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ciinclude ../../lib.mk
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ciifeq ($(pauth_cc_support),1)
2262306a36Sopenharmony_ci# pac* and aut* instructions are not available on architectures berfore
2362306a36Sopenharmony_ci# ARMv8.3. Therefore target ARMv8.3 wherever they are used directly
2462306a36Sopenharmony_ci$(OUTPUT)/pac_corruptor.o: pac_corruptor.S
2562306a36Sopenharmony_ci	$(CC) -c $^ -o $@ $(CFLAGS) -march=armv8.3-a
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci$(OUTPUT)/helper.o: helper.c
2862306a36Sopenharmony_ci	$(CC) -c $^ -o $@ $(CFLAGS) -march=armv8.3-a
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci# when -mbranch-protection is enabled and the target architecture is ARMv8.3 or
3162306a36Sopenharmony_ci# greater, gcc emits pac* instructions which are not in HINT NOP space,
3262306a36Sopenharmony_ci# preventing the tests from occurring at all. Compile for ARMv8.2 so tests can
3362306a36Sopenharmony_ci# run on earlier targets and print a meaningful error messages
3462306a36Sopenharmony_ci$(OUTPUT)/exec_target: exec_target.c $(OUTPUT)/helper.o
3562306a36Sopenharmony_ci	$(CC) $^ -o $@ $(CFLAGS) -march=armv8.2-a
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci$(OUTPUT)/pac: pac.c $(OUTPUT)/pac_corruptor.o $(OUTPUT)/helper.o
3862306a36Sopenharmony_ci	$(CC) $^ -o $@ $(CFLAGS) -march=armv8.2-a
3962306a36Sopenharmony_ciendif
40