Regression with gnu-tools-for-stm32 14.3.1+st.2
We are working on a project that uses MbedTLS v4.0.0, which contains some ASM code, and when we try to compile the project with gnu-tools-for-stm32 14.3.1+st.2, the compilation fails with the following error:
[build] FAILED: [code=1] Middlewares/Third_Party/mbedtls/tf-psa-crypto/drivers/builtin/CMakeFiles/builtin.dir/src/bignum_core.c.obj
[build] C:\Users\Parminder\AppData\Local\stm32cube\bundles\gnu-tools-for-stm32\14.3.1+st.2\bin\arm-none-eabi-gcc.exe -DDEBUG -DESC_TRL_REMOVE_DBG -DESC_USER_BUILD_CONFIG_CUSTOM=SC_vendor_platform.h -DSTM32L475xx -DTF_PSA_CRYPTO_CONFIG_FILE=\"C:/smartconnect-h300/H300/mbedtls_porting/crypto_config.h\" -DUSE_HAL_DRIVER -IC:/smartconnect-h300/Middlewares/Third_Party/mbedtls/tf-psa-crypto/drivers/builtin/src -IC:/smartconnect-h300/Middlewares/Third_Party/mbedtls/tf-psa-crypto/drivers/builtin/include -IC:/smartconnect-h300/Middlewares/Third_Party/mbedtls/tf-psa-crypto/include -IC:/smartconnect-h300/Middlewares/Third_Party/mbedtls/tf-psa-crypto/core -IC:/smartconnect-h300/Middlewares/Third_Party/mbedtls/tf-psa-crypto/drivers/everest/include -IC:/smartconnect-h300/Middlewares/Third_Party/Eseye/SMARTconnect/core -IC:/smartconnect-h300/H300 -IC:/smartconnect-h300/cmake/stm32cubemx/../../Core/Inc -IC:/smartconnect-h300/cmake/stm32cubemx/../../Drivers/STM32L4xx_HAL_Driver/Inc -IC:/smartconnect-h300/cmake/stm32cubemx/../../Drivers/STM32L4xx_HAL_Driver/Inc/Legacy -IC:/smartconnect-h300/cmake/stm32cubemx/../../Middlewares/Third_Party/FreeRTOS/Source/include -IC:/smartconnect-h300/cmake/stm32cubemx/../../Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2 -IC:/smartconnect-h300/cmake/stm32cubemx/../../Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F -IC:/smartconnect-h300/cmake/stm32cubemx/../../Drivers/CMSIS/Device/ST/STM32L4xx/Include -IC:/smartconnect-h300/cmake/stm32cubemx/../../Drivers/CMSIS/Include -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -Wall -fdata-sections -ffunction-sections -fstack-usage -fcyclomatic-complexity -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -Wall -fdata-sections -ffunction-sections -fstack-usage -fcyclomatic-complexity -O0 -g3 -std=c99 -Wall -Wextra -Wwrite-strings -Wmissing-prototypes -Wformat=2 -Wno-format-nonliteral -Wvla -Wlogical-op -Wshadow -Wformat-signedness -Wformat-overflow=2 -Wformat-truncation -O0 -g3 -Werror -Wmissing-declarations -MD -MT Middlewares/Third_Party/mbedtls/tf-psa-crypto/drivers/builtin/CMakeFiles/builtin.dir/src/bignum_core.c.obj -MF Middlewares\Third_Party\mbedtls\tf-psa-crypto\drivers\builtin\CMakeFiles\builtin.dir\src\bignum_core.c.obj.d -o Middlewares/Third_Party/mbedtls/tf-psa-crypto/drivers/builtin/CMakeFiles/builtin.dir/src/bignum_core.c.obj -c C:/smartconnect-h300/Middlewares/Third_Party/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core.c
[build] In file included from C:/smartconnect-h300/Middlewares/Third_Party/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core.c:8:
[build] C:/smartconnect-h300/Middlewares/Third_Party/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core.c: In function 'mbedtls_mpi_core_mla':
[build] C:/smartconnect-h300/Middlewares/Third_Party/mbedtls/tf-psa-crypto/core/tf_psa_crypto_common.h:321:13: error: 'asm' operand has impossible constraints or there are not enough registers
[build] 321 | #define asm __asm__
[build] | ^~~~~~~
[build] C:/smartconnect-h300/Middlewares/Third_Party/mbedtls/tf-psa-crypto/drivers/builtin/src/bn_mul.h:785:9: note: in expansion of macro 'asm'
[build] 785 | asm volatile (
[build] | ^~~
[build] C:/smartconnect-h300/Middlewares/Third_Party/mbedtls/tf-psa-crypto/drivers/builtin/src/bn_mul.h:1082:25: note: in expansion of macro 'MULADDC_X2_INIT'
[build] 1082 | #define MULADDC_X4_INIT MULADDC_X2_INIT
[build] | ^~~~~~~~~~~~~~~
[build] C:/smartconnect-h300/Middlewares/Third_Party/mbedtls/tf-psa-crypto/drivers/builtin/src/bn_mul.h:1088:25: note: in expansion of macro 'MULADDC_X4_INIT'
[build] 1088 | #define MULADDC_X8_INIT MULADDC_X4_INIT
[build] | ^~~~~~~~~~~~~~~
[build] C:/smartconnect-h300/Middlewares/Third_Party/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core.c:481:9: note: in expansion of macro 'MULADDC_X8_INIT'
[build] 481 | MULADDC_X8_INIT
[build] | ^~~~~~~~~~~~~~~However, if we switch the toolchain to gnu-tools-for-stm32 13.3.1+st.9 (via the VSCode Bundle Manager UI), it compiles without any problems.
It looks like that this is the piece of code that is causing the issue
https://github.com/Mbed-TLS/TF-PSA-Crypto/blob/76920edddcad00ac41b248e12d937b845df7bedb/drivers/builtin/src/bn_mul.h#L785-L818, but I'm not sure if this is really a issue with the toolchain or an issue of MbedTLS.
asm volatile (
/* - Make sure loop is 4-byte aligned to avoid stalls
* upon repeated non-word aligned instructions in
* some microarchitectures.
* - Don't use ldm with post-increment or back-to-back
* loads with post-increment and same address register
* to avoid stalls on some microarchitectures.
* - Bunch loads and stores to reduce latency on some
* microarchitectures. E.g., on Cortex-M4, the first
* in a series of load/store operations has latency
* 2 cycles, while subsequent loads/stores are single-cycle. */
#define MULADDC_X2_CORE \
".p2align 2 \n\t" \
"ldr %[a0], [%[in]], #+8 \n\t" \
"ldr %[b0], [%[acc]], #+8 \n\t" \
"ldr %[a1], [%[in], #-4] \n\t" \
"ldr %[b1], [%[acc], #-4] \n\t" \
"umaal %[b0], %[carry], %[scalar], %[a0] \n\t" \
"umaal %[b1], %[carry], %[scalar], %[a1] \n\t" \
"str %[b0], [%[acc], #-8] \n\t" \
"str %[b1], [%[acc], #-4] \n\t"
#define MULADDC_X2_STOP \
: [a0] "=&r" (tmp_a0), \
[b0] "=&r" (tmp_b0), \
[a1] "=&r" (tmp_a1), \
[b1] "=&r" (tmp_b1), \
[in] "+r" (s), \
[acc] "+r" (d), \
[carry] "+l" (c) \
: [scalar] "r" (b) \
: "memory" \
);
