From a135deeece11aa23ce2002336c87a64405d6ef58 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 16 Feb 2023 16:56:22 +0800 Subject: [PATCH] Move clang bug workaround to the head of file Signed-off-by: Jerry Yu --- library/sha256.c | 34 +++++++++++++++++++++++----------- library/sha512.c | 37 ++++++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/library/sha256.c b/library/sha256.c index d5be7aa19..c167dbe1a 100644 --- a/library/sha256.c +++ b/library/sha256.c @@ -22,8 +22,31 @@ * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf */ +#if defined(__aarch64__) && !defined(__ARM_FEATURE_CRYPTO) && \ + defined(__clang__) && __clang_major__ < 18 && __clang_major__ > 3 +/* TODO: Re-consider above after https://reviews.llvm.org/D131064 merged. + * + * The intrinsic declaration are guarded with ACLE predefined macros in clang, + * and those macros are only enabled with command line. Define the macros can + * enable those declaration and avoid compile error on it. + */ +#define __ARM_FEATURE_CRYPTO 1 +#pragma clang attribute push (__attribute__((target("crypto"))), apply_to=function) +#define MBEDTLS_POP_TARGET_PRAGMA +#endif /* __aarch64__ && __clang__ && + !__ARM_FEATURE_CRYPTO && __clang_major__ < 18 && __clang_major__ > 3 */ + #include "common.h" +#if defined(MBEDTLS_POP_TARGET_PRAGMA) && \ + !(defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) || \ + defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)) +#if defined(__clang__) +#pragma clang attribute pop +#endif +#undef MBEDTLS_POP_TARGET_PRAGMA +#endif + #if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA224_C) #include "mbedtls/sha256.h" @@ -42,16 +65,6 @@ # if defined(__clang__) # if __clang_major__ < 4 # error "A more recent Clang is required for MBEDTLS_SHA256_USE_A64_CRYPTO_*" -# elif __clang_major__ < 18 - /* TODO: Re-consider above after https://reviews.llvm.org/D131064 - * merged. - * - * The intrinsic declaration are guarded with ACLE predefined macros - * in clang, and those macros are only enabled with command line. - * Define the macros can enable those declaration and avoid compile - * error on it. - */ -# define __ARM_FEATURE_CRYPTO 1 # endif # pragma clang attribute push (__attribute__((target("crypto"))), apply_to=function) # define MBEDTLS_POP_TARGET_PRAGMA @@ -398,7 +411,6 @@ int mbedtls_internal_sha256_process_a64_crypto(mbedtls_sha256_context *ctx, #endif /* MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT || MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY */ - #if !defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) #define mbedtls_internal_sha256_process_many_c mbedtls_internal_sha256_process_many #define mbedtls_internal_sha256_process_c mbedtls_internal_sha256_process diff --git a/library/sha512.c b/library/sha512.c index df8c5a9c1..d0e6d47df 100644 --- a/library/sha512.c +++ b/library/sha512.c @@ -22,8 +22,34 @@ * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf */ +#if defined(__aarch64__) && !defined(__ARM_FEATURE_SHA512) && \ + defined(__clang__) && __clang_major__ < 18 && \ + __clang_major__ >= 13 && __clang_minor__ > 0 && __clang_patchlevel__ > 0 +/* TODO: Re-consider above after https://reviews.llvm.org/D131064 merged. + * + * The intrinsic declaration are guarded with ACLE predefined macros in clang, + * and those macros are only enabled with command line. Define the macros can + * enable those declaration and avoid compile error on it. + */ +#define __ARM_FEATURE_SHA512 1 +#pragma clang attribute push (__attribute__((target("crypto"))), apply_to=function) +#define MBEDTLS_POP_TARGET_PRAGMA +#endif /* __aarch64__ && __clang__ && + !__ARM_FEATURE_SHA512 && __clang_major__ < 18 && + __clang_major__ >= 13 && __clang_minor__ > 0 && + __clang_patchlevel__ > 0 */ + #include "common.h" +#if defined(MBEDTLS_POP_TARGET_PRAGMA) && \ + !(defined(MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT) || \ + defined(MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY)) +#if defined(__clang__) +#pragma clang attribute pop +#endif +#undef MBEDTLS_POP_TARGET_PRAGMA +#endif + #if defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA384_C) #include "mbedtls/sha512.h" @@ -68,17 +94,6 @@ __clang_patchlevel__ == 0) /* We implement the intrinsics with inline assembler, so don't error */ # else -# if __clang_major__ < 18 - /* TODO: Re-consider above after https://reviews.llvm.org/D131064 - * merged. - * - * The intrinsic declaration are guarded with ACLE predefined macros - * in clang, and those macros are only enabled with command line. - * Define the macros can enable those declaration and avoid compile - * error on it. - */ -# define __ARM_FEATURE_SHA512 1 -# endif # pragma clang attribute push (__attribute__((target("sha3"))), apply_to=function) # define MBEDTLS_POP_TARGET_PRAGMA # endif