Skip to content

Fix IAR compiler endianness detection in alignment.h - #838

Open
nzzlinh wants to merge 3 commits into
Mbed-TLS:developmentfrom
nzzlinh:fix-iar-endianness
Open

Fix IAR compiler endianness detection in alignment.h#838
nzzlinh wants to merge 3 commits into
Mbed-TLS:developmentfrom
nzzlinh:fix-iar-endianness

Conversation

@nzzlinh

@nzzlinh nzzlinh commented Jul 14, 2026

Copy link
Copy Markdown

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 Mbed-TLS/mbedtls#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

@nzzlinh

nzzlinh commented Jul 14, 2026

Copy link
Copy Markdown
Author

Related issue in MbedTLS repo: Mbed-TLS/mbedtls#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(__LITTLE_ENDIAN__) always evaluated to true on IAR, causing big-endian targets to be incorrectly treated as little-endian.

This commit updates the check to verify the value (__XXXX_ENDIAN__ == 1).

Signed-off-by: Linh D. Nguyen <dlinhvn@gmail.com>
@nzzlinh
nzzlinh force-pushed the fix-iar-endianness branch from a24fdd7 to df9bddb Compare July 14, 2026 15:04

@gilles-peskine-arm gilles-peskine-arm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

@gilles-peskine-arm gilles-peskine-arm added bug Something isn't working needs-review Every commit must be reviewed by at least two team members needs-reviewer This PR needs someone to pick it up for review priority-medium Medium priority - this can be reviewed as time permits size-xs Estimated task size: extra small (a few hours at most) needs-backports Backports are missing or are pending review and approval. labels Jul 20, 2026
@gilles-peskine-arm gilles-peskine-arm moved this from In Development to In Review in Non-roadmap pull requests Jul 20, 2026
@gilles-peskine-arm gilles-peskine-arm moved this to Scoped in Community Jul 20, 2026
Signed-off-by: Linh D. Nguyen <dlinhvn@gmail.com>
Signed-off-by: Linh D. Nguyen <dlinhvn@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working needs-backports Backports are missing or are pending review and approval. needs-review Every commit must be reviewed by at least two team members needs-reviewer This PR needs someone to pick it up for review priority-medium Medium priority - this can be reviewed as time permits size-xs Estimated task size: extra small (a few hours at most)

Projects

Status: Scoped
Status: In Review

Development

Successfully merging this pull request may close these issues.

mbedtls 3.6 does not build correctly with IAR Tools in big endian

2 participants