Skip to content

Commit b67ce56

Browse files
LoveKarlssonzejiang0jason
authored andcommitted
[zep fromtree] [1.1] 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 TF-PSA-Crypto does not work with attribute(packed), only with the keyword __packed. Added changelog entry. This fix temporary undefs the macro and restores it after the typedefs. Signed-off-by: Lars-Ove Karlsson <lars-ove.karlsson@qt.io> (cherry picked from commit e544d70)
1 parent dc575a2 commit b67ce56

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

ChangeLog.d/iar-alignment-fix.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Bugfix
2+
* Fix a problem in library/alignment.h where the Zephyr Project use
3+
a pre-defined macro '__packed' which collides with the IAR keyword
4+
'__packed', causing the typedefs of unaligned types to remain aligned.
5+
Fixes mbedtls issue #10334
6+

core/alignment.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,27 @@
4747
#pragma language=extended
4848
#define MBEDTLS_POP_IAR_LANGUAGE_PRAGMA
4949
/* IAR recommend this technique for accessing unaligned data in
50-
* https://www.iar.com/knowledge/support/technical-notes/compiler/accessing-unaligned-data
50+
* https://mypages.iar.com/s/article/Accessing-Unaligned-Data
5151
* This results in a single load / store instruction (if unaligned access is supported).
5252
* According to that document, this is only supported on certain architectures.
5353
*/
54-
#define UINT_UNALIGNED
54+
#define UINT_UNALIGNED
55+
/* Some products, like Zephyr, defines __packed as a macro for attribute(packed) and
56+
* that does not work with typedefs, so if __packed is defined, undef it for the
57+
* typedefs and restore it afterwards.
58+
*/
59+
#ifdef __packed
60+
#pragma push_macro("__packed")
61+
#undef __packed
62+
#define MBEDTLS_IAR_PACKED_MACRO_USED
63+
#endif
5564
typedef uint16_t __packed mbedtls_uint16_unaligned_t;
5665
typedef uint32_t __packed mbedtls_uint32_unaligned_t;
5766
typedef uint64_t __packed mbedtls_uint64_unaligned_t;
67+
#ifdef MBEDTLS_IAR_PACKED_MACRO_USED
68+
#undef MBEDTLS_IAR_PACKED_MACRO_USED
69+
#pragma pop_macro("__packed")
70+
#endif
5871
#elif defined(MBEDTLS_COMPILER_IS_GCC) && (MBEDTLS_GCC_VERSION >= 40504) && \
5972
((MBEDTLS_GCC_VERSION < 60300) || (!defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)))
6073
/*

0 commit comments

Comments
 (0)