From 19d562dbde5e879ef5a072b635457091c457fc67 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 27 Feb 2026 17:02:43 +0100 Subject: [PATCH 1/2] Fixed build errors with cross-clang compiler --- Makefile | 2 +- arch.mk | 8 ++++++-- include/wolfboot/wolfboot.h | 6 +++++- options.mk | 8 +++++--- src/update_flash.c | 4 ++-- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index c25413327f..5631c2f5e9 100644 --- a/Makefile +++ b/Makefile @@ -180,7 +180,7 @@ CFLAGS+=$(WOLFPSA_CFLAGS) # Setup default optimizations (for GCC) ifeq ($(USE_GCC_HEADLESS),1) - CFLAGS+=-Wall -Wextra -Wno-main -ffreestanding -nostartfiles + CFLAGS+=-Wall -Wextra -Wno-main -ffreestanding CFLAGS+=-ffunction-sections -fdata-sections -fomit-frame-pointer # Allow unused parameters and functions CFLAGS+=-Wno-unused-parameter -Wno-unused-function diff --git a/arch.mk b/arch.mk index 849fa0360f..570cf3b205 100644 --- a/arch.mk +++ b/arch.mk @@ -134,8 +134,12 @@ endif ifeq ($(ARCH),ARM) CROSS_COMPILE?=arm-none-eabi- CFLAGS+=-DARCH_ARM - CFLAGS+=-mthumb -mlittle-endian -mthumb-interwork - LDFLAGS+=-mthumb -mlittle-endian -mthumb-interwork + CFLAGS+=-mthumb -mlittle-endian + LDFLAGS+=-mthumb -mlittle-endian + ifeq ($(USE_GCC),1) + CFLAGS+=-mthumb-interwork + LDFLAGS+=-mthumb-interwork + endif ## Target specific configuration ifeq ($(TARGET),samr21) diff --git a/include/wolfboot/wolfboot.h b/include/wolfboot/wolfboot.h index f7b5fdb0b2..33d29923b5 100644 --- a/include/wolfboot/wolfboot.h +++ b/include/wolfboot/wolfboot.h @@ -46,7 +46,11 @@ extern "C" { #ifndef RAMFUNCTION # if defined(__WOLFBOOT) && defined(RAM_CODE) # if defined(ARCH_ARM) -# define RAMFUNCTION __attribute__((used,section(".ramcode"),long_call)) +# if defined(__clang__) +# define RAMFUNCTION __attribute__((used,section(".ramcode"))) +# else +# define RAMFUNCTION __attribute__((used,section(".ramcode"),long_call)) +# endif # elif defined(ARCH_PPC) # define RAMFUNCTION __attribute__((used,section(".ramcode"),longcall)) # else diff --git a/options.mk b/options.mk index 108e330a74..c9884eed81 100644 --- a/options.mk +++ b/options.mk @@ -1030,9 +1030,11 @@ CFLAGS+=$(CFLAGS_EXTRA) OBJS+=$(OBJS_EXTRA) ifeq ($(USE_GCC_HEADLESS),1) - ifneq ($(ARCH),RENESAS_RX) - ifneq ($(ARCH),AURIX_TC3) - CFLAGS+="-Wstack-usage=$(STACK_USAGE)" + ifeq ($(USE_GCC),1) + ifneq ($(ARCH),RENESAS_RX) + ifneq ($(ARCH),AURIX_TC3) + CFLAGS+="-Wstack-usage=$(STACK_USAGE)" + endif endif endif endif diff --git a/src/update_flash.c b/src/update_flash.c index 3927df336a..e3c4de3a27 100644 --- a/src/update_flash.c +++ b/src/update_flash.c @@ -754,7 +754,7 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot, #ifdef WOLFBOOT_ARMORED -# ifdef __GNUC__ +# if defined(__GNUC__) && !defined(__clang__) # pragma GCC push_options # pragma GCC optimize("O0") # elif defined(__IAR_SYSTEMS_ICC__) @@ -1474,7 +1474,7 @@ void RAMFUNCTION wolfBoot_start(void) } #ifdef WOLFBOOT_ARMORED -# ifdef __GNUC__ +# if defined(__GNUC__) && !defined(__clang__) # pragma GCC pop_options # elif defined(__IAR_SYSTEMS_ICC__) # pragma optimize=default From 4eca085b54370236a2be3162d1d1bbc6f21e8532 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Tue, 10 Mar 2026 15:46:52 +0100 Subject: [PATCH 2/2] Added USE_CLANG option to Makefile --- Makefile | 5 +++++ arch.mk | 25 +++++++++++++++++++++++++ include/wolfboot/wolfboot.h | 12 +++++++++--- options.mk | 23 +++++++++++++++++++---- src/update_flash.c | 4 ++++ test-app/Makefile | 21 +++++++++++++++++---- 6 files changed, 79 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 5631c2f5e9..39f5965cd0 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,12 @@ LIBS= SIGN_ALG= OBJCOPY_FLAGS= BIG_ENDIAN?=0 +USE_CLANG?=0 +ifeq ($(USE_CLANG),1) +USE_GCC?=0 +else USE_GCC?=1 +endif USE_GCC_HEADLESS?=1 FLASH_OTP_KEYSTORE?=0 BOOTLOADER_PARTITION_SIZE?=$$(( $(WOLFBOOT_PARTITION_BOOT_ADDRESS) - $(ARCH_FLASH_OFFSET))) diff --git a/arch.mk b/arch.mk index 570cf3b205..410ae67f6b 100644 --- a/arch.mk +++ b/arch.mk @@ -137,8 +137,10 @@ ifeq ($(ARCH),ARM) CFLAGS+=-mthumb -mlittle-endian LDFLAGS+=-mthumb -mlittle-endian ifeq ($(USE_GCC),1) + ifeq ($(findstring clang,$(notdir $(CC))),) CFLAGS+=-mthumb-interwork LDFLAGS+=-mthumb-interwork + endif endif ## Target specific configuration @@ -1220,6 +1222,29 @@ ifeq ($(TARGET),psoc6) endif endif +ifeq ($(USE_CLANG),1) + ifneq ($(ARCH),ARM) + $(error USE_CLANG=1 is currently supported only for ARCH=ARM) + endif + CLANG?=clang + CLANG_GCC_NAME?=$(CROSS_COMPILE)gcc + CLANG_TARGET?=arm-none-eabi + CLANG_DRIVER:=$(CLANG) --target=$(CLANG_TARGET) -ccc-gcc-name $(CLANG_GCC_NAME) + CLANG_LIBC_A?=$(shell $(CLANG_GCC_NAME) -print-file-name=libc.a) + CLANG_NEWLIB_INCLUDE?=$(abspath $(dir $(CLANG_LIBC_A))/../include) + + CC=$(CLANG_DRIVER) + LD=$(CLANG_DRIVER) + AS=$(CLANG_DRIVER) + AR=$(CROSS_COMPILE)ar + OBJCOPY?=$(CROSS_COMPILE)objcopy + SIZE=$(CROSS_COMPILE)size + + CFLAGS+=-isystem $(CLANG_NEWLIB_INCLUDE) + CFLAGS+=-DWOLFSSL_NO_ATOMIC -DWOLFSSL_NO_ATOMICS + LDFLAGS+=-nostdlib +endif + ifeq ($(USE_GCC),1) ## Toolchain setup CC=$(CROSS_COMPILE)gcc diff --git a/include/wolfboot/wolfboot.h b/include/wolfboot/wolfboot.h index 33d29923b5..af9d825b19 100644 --- a/include/wolfboot/wolfboot.h +++ b/include/wolfboot/wolfboot.h @@ -46,10 +46,16 @@ extern "C" { #ifndef RAMFUNCTION # if defined(__WOLFBOOT) && defined(RAM_CODE) # if defined(ARCH_ARM) -# if defined(__clang__) -# define RAMFUNCTION __attribute__((used,section(".ramcode"))) -# else +# if defined(__has_attribute) +# if __has_attribute(long_call) +# define RAMFUNCTION __attribute__((used,section(".ramcode"),long_call)) +# else +# define RAMFUNCTION __attribute__((used,section(".ramcode"))) +# endif +# elif defined(__GNUC__) # define RAMFUNCTION __attribute__((used,section(".ramcode"),long_call)) +# else +# define RAMFUNCTION __attribute__((used,section(".ramcode"))) # endif # elif defined(ARCH_PPC) # define RAMFUNCTION __attribute__((used,section(".ramcode"),longcall)) diff --git a/options.mk b/options.mk index c9884eed81..be66a7dd2e 100644 --- a/options.mk +++ b/options.mk @@ -1,8 +1,19 @@ WOLFCRYPT_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/asn.o -USE_GCC?=1 +USE_CLANG?=0 +ifeq ($(USE_CLANG),1) + USE_GCC?=0 +else + USE_GCC?=1 +endif WOLFBOOT_TEST_FILLER?=0 WOLFBOOT_TIME_TEST?=0 +ifeq ($(USE_CLANG),1) + ifeq ($(USE_GCC),1) + $(error USE_CLANG=1 is incompatible with USE_GCC=1; set USE_GCC=0) + endif +endif + # Support for Built-in ROT into OTP flash memory ifeq ($(FLASH_OTP_KEYSTORE),1) CFLAGS+=-D"FLASH_OTP_KEYSTORE" @@ -722,7 +733,9 @@ ifeq ($(DEBUG_SYMBOLS),1) ifeq ($(USE_GCC),1) CFLAGS+=-ggdb3 else ifneq ($(ARCH),AURIX_TC3) + ifneq ($(USE_CLANG),1) CFLAGS+=-gstabs + endif endif endif @@ -1031,9 +1044,11 @@ OBJS+=$(OBJS_EXTRA) ifeq ($(USE_GCC_HEADLESS),1) ifeq ($(USE_GCC),1) - ifneq ($(ARCH),RENESAS_RX) - ifneq ($(ARCH),AURIX_TC3) - CFLAGS+="-Wstack-usage=$(STACK_USAGE)" + ifneq ($(USE_CLANG),1) + ifneq ($(ARCH),RENESAS_RX) + ifneq ($(ARCH),AURIX_TC3) + CFLAGS+="-Wstack-usage=$(STACK_USAGE)" + endif endif endif endif diff --git a/src/update_flash.c b/src/update_flash.c index e3c4de3a27..29a4d1fbdd 100644 --- a/src/update_flash.c +++ b/src/update_flash.c @@ -757,6 +757,8 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot, # if defined(__GNUC__) && !defined(__clang__) # pragma GCC push_options # pragma GCC optimize("O0") +# elif defined(__clang__) +# pragma clang optimize off # elif defined(__IAR_SYSTEMS_ICC__) # pragma optimize=none # endif @@ -1476,6 +1478,8 @@ void RAMFUNCTION wolfBoot_start(void) #ifdef WOLFBOOT_ARMORED # if defined(__GNUC__) && !defined(__clang__) # pragma GCC pop_options +# elif defined(__clang__) +# pragma clang optimize on # elif defined(__IAR_SYSTEMS_ICC__) # pragma optimize=default # endif diff --git a/test-app/Makefile b/test-app/Makefile index cb9876ecbd..11b0bd1fdc 100644 --- a/test-app/Makefile +++ b/test-app/Makefile @@ -22,7 +22,12 @@ endif CFLAGS+=-I. -I.. DEBUG?=1 DELTA_DATA_SIZE?=2000 -USE_GCC?=1 +USE_CLANG?=0 +ifeq ($(USE_CLANG),1) + USE_GCC?=0 +else + USE_GCC?=1 +endif USE_GCC_HEADLESS?=1 BOOTLOADER_PARTITION_SIZE?=$$(( $(WOLFBOOT_PARTITION_BOOT_ADDRESS) - $(ARCH_FLASH_OFFSET))) @@ -57,14 +62,18 @@ else CFLAGS+=-Wall -ffreestanding -Wno-unused # Stack usage computation not supported on TriCore ifneq ($(ARCH),AURIX_TC3) - CFLAGS+=-Wstack-usage=1024 -nostartfiles + ifneq ($(USE_CLANG),1) + CFLAGS+=-Wstack-usage=1024 + endif endif CFLAGS+=-DTARGET_$(TARGET) -I../include CFLAGS+=-g ifeq ($(USE_GCC),1) CFLAGS+=-ggdb3 else ifneq ($(ARCH),AURIX_TC3) - CFLAGS+=-gstabs + ifneq ($(USE_CLANG),1) + CFLAGS+=-gstabs + endif endif ifeq ($(ARCH),RENESAS_RX) @@ -78,6 +87,10 @@ endif include ../arch.mk +ifeq ($(USE_CLANG),1) + APP_OBJS+=../src/string.o +endif + # Optional alias for clearer TZ PSA selection in app builds. ifeq ($(WOLFCRYPT_TZ_PSA),1) WOLFCRYPT_TZ=1 @@ -85,7 +98,7 @@ ifeq ($(WOLFCRYPT_TZ_PSA),1) endif # Setup default linker flags -LDFLAGS+=-T $(LSCRIPT) -Wl,-gc-sections -Wl,-Map=image.map +LDFLAGS+=-T $(LSCRIPT) -Wl,-gc-sections -Wl,-Map=image.map -nostartfiles # Setup default objcopy flags OBJCOPY_FLAGS+=--gap-fill $(FILL_BYTE)