Fix IAR compiler endianness detection in alignment.h #838

Open
nzzlinh wants to merge 3 commits from nzzlinh/fix-iar-endianness into development
nzzlinh commented 2026-07-14 09:39:13 -05:00 (Migrated from github.com)

Description

This PR fixes a critical bug where the IAR compiler always triggers the Little-Endian code path, even when compiling for Big-Endian CPU. Fix https://github.com/Mbed-TLS/mbedtls/issues/10381

The IAR C/C++ compiler always defines both __LITTLE_ENDIAN__ and __BIG_ENDIAN__ macros internally, assigning '1' to the active endianness and '0' to the inactive one. The previous check defined(__BIG_ENDIAN__) always evaluated to true on IAR. This commit updates the check to verify the actual value: (__BIG_ENDIAN__ == 1).

image

References from IAR Development Guide for several MCU families

[1] ARM: https://wwwfiles.iar.com/arm/webic/doc/EWARM_DevelopmentGuide.ENU.pdf - p.494
[2] Renesas RX: https://wwwfiles.iar.com/RX/webic/doc/EWRX_DevelopmentGuide.ENU.pdf - p. 428
[3] Renesas RL78: https://wwwfiles.iar.com/rl78/EWRL78_DevelopmentGuide.ENU.pdf -p.389
[4] STM8: https://wwwfiles.iar.com/stm8/guides/EWSTM8_DevelopmentGuide.pdf - p.334

PR checklist

## Description This PR fixes a critical bug where the IAR compiler always triggers the Little-Endian code path, even when compiling for Big-Endian CPU. Fix https://github.com/Mbed-TLS/mbedtls/issues/10381 The IAR C/C++ compiler always defines both `__LITTLE_ENDIAN__` and `__BIG_ENDIAN__` macros internally, assigning '1' to the active endianness and '0' to the inactive one. The previous check `defined(__BIG_ENDIAN__)` always evaluated to true on IAR. This commit updates the check to verify the actual value: `(__BIG_ENDIAN__ == 1)`. > <img width="627" height="94" alt="image" src="https://github.com/user-attachments/assets/af07401f-62ea-4990-ab5a-a951b0ee4b8f" /> ### References from IAR Development Guide for several MCU families [1] ARM: https://wwwfiles.iar.com/arm/webic/doc/EWARM_DevelopmentGuide.ENU.pdf - p.494 [2] Renesas RX: https://wwwfiles.iar.com/RX/webic/doc/EWRX_DevelopmentGuide.ENU.pdf - p. 428 [3] Renesas RL78: https://wwwfiles.iar.com/rl78/EWRL78_DevelopmentGuide.ENU.pdf -p.389 [4] STM8: https://wwwfiles.iar.com/stm8/guides/EWSTM8_DevelopmentGuide.pdf - p.334 ## PR checklist - [x] **changelog** provided - [x] **framework PR** not required - [x] **TF-PSA-Crypto development PR** provided (this PR) - [x] **TF-PSA-Crypto 1.1 PR** https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/845 - [x] **mbedtls development PR** not required because: fix is isolated to TF-PSA-Crypto core - [x] **mbedtls 4.1 PR** not required because: fix is isolated to TF-PSA-Crypto core - [x] **mbedtls 3.6 PR** https://github.com/Mbed-TLS/mbedtls/pull/10840 - [x] **tests** not required because: we don't have IAR in our CI
gilles-peskine-arm (Migrated from github.com) reviewed 2026-07-14 09:39:13 -05:00
nzzlinh commented 2026-07-14 09:59:01 -05:00 (Migrated from github.com)

Related issue in MbedTLS repo: https://github.com/Mbed-TLS/mbedtls/issues/10381

Related issue in MbedTLS repo: https://github.com/Mbed-TLS/mbedtls/issues/10381
gilles-peskine-arm (Migrated from github.com) reviewed 2026-07-20 08:59:00 -05:00
gilles-peskine-arm (Migrated from github.com) left a comment
No description provided.
LGTM on code reading. (I haven't tested because I don't have a big-endian arm testing environment readily available, and I do have access to IAR for arm but the license server is down at the moment.)
This pull request is broken due to missing fork information.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'nzzlinh/fix-iar-endianness:nzzlinh/fix-iar-endianness'
git switch 'nzzlinh/fix-iar-endianness'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'development'
git merge --no-ff 'nzzlinh/fix-iar-endianness'
git switch 'nzzlinh/fix-iar-endianness'
git rebase 'development'
git switch 'development'
git merge --ff-only 'nzzlinh/fix-iar-endianness'
git switch 'nzzlinh/fix-iar-endianness'
git rebase 'development'
git switch 'development'
git merge --no-ff 'nzzlinh/fix-iar-endianness'
git switch 'development'
git merge --squash 'nzzlinh/fix-iar-endianness'
git switch 'development'
git merge --ff-only 'nzzlinh/fix-iar-endianness'
git switch 'development'
git merge 'nzzlinh/fix-iar-endianness'
git push origin 'development'
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-Mbed-TLS/TF-PSA-Crypto!838
No description provided.