From 595b0b577b4b402a404e8f080aecad06caad3b17 Mon Sep 17 00:00:00 2001 From: Lars-Ove Karlsson Date: Mon, 2 Jun 2025 13:08:23 +0200 Subject: [PATCH 1/3] * [3.6] Fix alignment problems with IAR and Zephyr Since __packed is a reserved keyword for IAR compilers, and Zephyr defines it to attribute(__packed__), some typedef constructs in mbedtls does not work with attribute(packed), only with the keyword packed. This fix temporary undefs the macro and restores it after the typedefs. Signed-off-by: Lars-Ove Karlsson --- library/alignment.h | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/library/alignment.h b/library/alignment.h index d304133382..3bb92f9e62 100644 --- a/library/alignment.h +++ b/library/alignment.h @@ -51,10 +51,27 @@ * This results in a single load / store instruction (if unaligned access is supported). * According to that document, this is only supported on certain architectures. */ - #define UINT_UNALIGNED +#define UINT_UNALIGNED + +/* Some products, like Zephyr, defines __packed as a macro for attribute(packed) and + * that does not work with typedefs, so if __packed is defined, undef it for the + * typedefs and restore it afterwards. + */ +#ifdef __packed +#pragma push_macro("__packed") +#undef __packed +#define MBEDTLS_IAR_PACKED_MACRO_USED +#endif + typedef uint16_t __packed mbedtls_uint16_unaligned_t; typedef uint32_t __packed mbedtls_uint32_unaligned_t; typedef uint64_t __packed mbedtls_uint64_unaligned_t; + +#ifdef MBEDTLS_IAR_PACKED_MACRO_USED +#undef MBEDTLS_IAR_PACKED_MACRO_USED +#pragma pop_macro("__packed") +#endif + #elif defined(MBEDTLS_COMPILER_IS_GCC) && (MBEDTLS_GCC_VERSION >= 40504) && \ ((MBEDTLS_GCC_VERSION < 60300) || (!defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS))) /* From 05890c71c84d46f86def57bbb6a0eb4dc6adafd6 Mon Sep 17 00:00:00 2001 From: Lars-Ove Karlsson Date: Mon, 11 Aug 2025 08:22:36 +0200 Subject: [PATCH 2/3] Added a changelog entry for IAR unalignment fix. Signed-off-by: Lars-Ove Karlsson --- ChangeLog.d/iar-alignment-fix.txt | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 ChangeLog.d/iar-alignment-fix.txt diff --git a/ChangeLog.d/iar-alignment-fix.txt b/ChangeLog.d/iar-alignment-fix.txt new file mode 100644 index 0000000000..dde839f16f --- /dev/null +++ b/ChangeLog.d/iar-alignment-fix.txt @@ -0,0 +1,6 @@ +Bugfix + * Fix a problem in library/alignment.h where the Zephyr Project use + a pre-defined macro '__packed' which collides with the IAR keyword + '__packed', causing the typedefs of unaligned types to remain aligned. + Fixes mbedtls issue #10334 + From e7fd09246d58a47d2883659972a779781aee5086 Mon Sep 17 00:00:00 2001 From: Lars-Ove Karlsson Date: Wed, 29 Apr 2026 12:16:35 +0200 Subject: [PATCH 3/3] Updated the url to the IAR technical note about unaligned access To be the same link as in the tf-psa-crypto file. Signed-off-by: Lars-Ove Karlsson --- library/alignment.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alignment.h b/library/alignment.h index 3bb92f9e62..456629abf1 100644 --- a/library/alignment.h +++ b/library/alignment.h @@ -47,7 +47,7 @@ #pragma language=extended #define MBEDTLS_POP_IAR_LANGUAGE_PRAGMA /* IAR recommend this technique for accessing unaligned data in - * https://www.iar.com/knowledge/support/technical-notes/compiler/accessing-unaligned-data + * https://mypages.iar.com/s/article/Accessing-Unaligned-Data * This results in a single load / store instruction (if unaligned access is supported). * According to that document, this is only supported on certain architectures. */