Misc compile error/warning fixes
v4 cover Misc compile error/warning fixes
This series does few compile error/warning fixes which I discovered trying CLANG with OpenSBI.
These patches can also be found in clang_fixes_v4 branch at:
https://github.com/riscv-software-src/opensbi.git
Changes since v3:
- Updated commit description of PATCH1 and PATCH2
- Added a comment in Makefile for PATCH1
Changes since v2:
- Make memcpy() and memset() as weak-alias of sbi_memcpy() and sbi_memset() respectively in PATCH2
Changes since v1:
- Use explicit inline macro sbi_trap_info_init() in PATCH2 for initializing struct sbi_trap_info.
Anup Patel (2):
Makefile: Fix -msave-restore compile warning with CLANG-10 (or lower)
lib: sbi: Fix compile errors using -Os option
Makefile | 14 ++++++++++---- lib/sbi/sbi_string.c | 6 ++++++ 2 files changed, 16 insertions(+), 4 deletions(-)
Signature · 1 line
2.25.1
PATCH v4 1/2 Makefile: Fix -msave-restore compile warning with CLANG-10 (or lower)
The riscv target of CLANG-10 (or lower) does not support the -m(no-)save-restore option so we get compile warnings. This patch fixes compile warning by using -m(no-)save-restore option only for GCC.
--- Makefile | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
Makefile +10 -4
@@ -275,8 +275,11 @@ GENFLAGS += $(platform-genflags-y) GENFLAGS += $(firmware-genflags-y) CFLAGS = -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -O2 -CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls -CFLAGS += -mno-save-restore -mstrict-align +CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls -mstrict-align +# CLANG-10 (or lower) does not support -m(no-)save-restore option +ifneq ($(CC_IS_CLANG),y) +CFLAGS += -mno-save-restore +endif CFLAGS += -mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA) CFLAGS += -mcmodel=$(PLATFORM_RISCV_CODE_MODEL) CFLAGS += $(RELAX_FLAG)
@@ -290,8 +293,11 @@ CPPFLAGS += $(platform-cppflags-y) CPPFLAGS += $(firmware-cppflags-y) ASFLAGS = -g -Wall -nostdlib -ASFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls -ASFLAGS += -mno-save-restore -mstrict-align +ASFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls -mstrict-align +# CLANG-10 (or lower) does not support -m(no-)save-restore option +ifneq ($(CC_IS_CLANG),y) +ASFLAGS += -mno-save-restore +endif ASFLAGS += -mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA) ASFLAGS += -mcmodel=$(PLATFORM_RISCV_CODE_MODEL) ASFLAGS += $(RELAX_FLAG)
Signature · 1 line
2.25.1
PATCH v4 1/2 Makefile: Fix -msave-restore compile warning with CLANG-10 (or lower)
? 2021-12-03???? 10:33 +0530?Anup Patel???
Quoted 46 lines
The riscv target of CLANG-10 (or lower) does not support the -m(no-)save-restore option so we get compile warnings. This patch fixes compile warning by using -m(no-)save-restore option only for GCC.
Signed-off-by Anup Patel <[email protected]>Reviewed-by Dong Du <[email protected]>---
?Makefile | 14 ++++++++++----
?1 file changed, 10 insertions(+), 4 deletions(-)Makefile +0
-0@@ -275,8 +275,11 @@ GENFLAGS???+=??????$(platform-genflags-y) ?GENFLAGS???????+=??????$(firmware-genflags-y)?
?CFLAGS?????????=???????-g -Wall -Werror -ffreestanding -nostdlib - fno-stack-protector -fno-strict-aliasing -O2 -CFLAGS?????????+=??????-fno-omit-frame-pointer -fno-optimize- sibling-callsMakefile +1
-1-CFLAGS?????????+=??????-mno-save-restore -mstrict-align +CFLAGS?????????+=??????-fno-omit-frame-pointer -fno-optimize-sibling-calls -mstrict-align
Makefile +4
-0+# CLANG-10 (or lower) does not support -m(no-)save-restore option +ifneq ($(CC_IS_CLANG),y) +CFLAGS?????????+=??????-mno-save-restore +endif?CFLAGS?????????+=??????-mabi=$(PLATFORM_RISCV_ABI) - march=$(PLATFORM_RISCV_ISA) ?CFLAGS?????????+=??????-mcmodel=$(PLATFORM_RISCV_CODE_MODEL) ?CFLAGS?????????+=??????$(RELAX_FLAG)
Makefile +0
-0@@ -290,8 +293,11 @@ CPPFLAGS???+=??????$(platform-cppflags-y) ?CPPFLAGS???????+=??????$(firmware-cppflags-y)?
?ASFLAGS????????????????=???????-g -Wall -nostdlib -ASFLAGS????????????????+=??????-fno-omit-frame-pointer -fno- optimize-sibling-callsMakefile +1
-1-ASFLAGS????????????????+=??????-mno-save-restore -mstrict-align +ASFLAGS????????????????+=??????-fno-omit-frame-pointer -fno-optimize-sibling-calls -mstrict-align
Makefile +4
-0+# CLANG-10 (or lower) does not support -m(no-)save-restore option +ifneq ($(CC_IS_CLANG),y) +ASFLAGS????????????????+=??????-mno-save-restore +endif
Quoted 8 lines
?ASFLAGS????????????????+=??????-mabi=$(PLATFORM_RISCV_ABI) - march=$(PLATFORM_RISCV_ISA) ?ASFLAGS????????????????+=??????-mcmodel=$(PLATFORM_RISCV_CODE_MODEL) ?ASFLAGS????????????????+=??????$(RELAX_FLAG)
Signature · 1 line
2.25.1
Maybe we can dynamically detect whether the toolchain supports -m(no-)save-restore, just like we did before to detect whether ld supports PIE
Makefile +0 -0
@@ -149,7 +149,7 @@ endif # Check whether the linker supports creating PIEs OPENSBI_LD_PIE := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG)
$(USE_LD_FLAG) -fPIE -nostdlib -Wl,-pie -x c /dev/null -o /dev/null
Quoted 1 line
/dev/null2>&1 && echo y || echo n)
-
+CC_SUPPORT_SAVE_RESTORE := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) $(USE_LD_FLAG) -nostdlib -mno-save-restore -x c /dev/null -o /dev/null
Quoted 1 line
/dev/null2>&1 && echo y || echo n)
# Build Info:
# OPENSBI_BUILD_TIME_STAMP -- the compilation time stamp # OPENSBI_BUILD_COMPILER_VERSION -- the compiler version info
@@ -275,8 +275,10 @@ GENFLAGS += $(platform-genflags-y)
GENFLAGS += $(firmware-genflags-y)
CFLAGS = -g -Wall -Werror -ffreestanding -nostdlib -fno-
stack-protector -fno-strict-aliasing -O2
-CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-
calls
-CFLAGS += -mno-save-restore -mstrict-align +CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-
calls -mstrict-align
+ifeq ($(CC_SUPPORT_SAVE_RESTORE),y)
+CFLAGS += -mno-save-restore
+endif
CFLAGS += -mabi=$(PLATFORM_RISCV_ABI) -
march=$(PLATFORM_RISCV_ISA)
CFLAGS += -mcmodel=$(PLATFORM_RISCV_CODE_MODEL) CFLAGS += $(RELAX_FLAG)
@@ -290,8 +292,10 @@ CPPFLAGS += $(platform-cppflags-y)
CPPFLAGS += $(firmware-cppflags-y)
ASFLAGS = -g -Wall -nostdlib -ASFLAGS += -fno-omit-frame-pointer -fno-optimize- sibling-calls -ASFLAGS += -mno-save-restore -mstrict-align +ASFLAGS += -fno-omit-frame-pointer -fno-optimize-
sibling-calls -mstrict-align
+ifeq ($(CC_SUPPORT_SAVE_RESTORE),y)
+ASFLAGS += -mno-save-restore
+endif
ASFLAGS += -mabi=$(PLATFORM_RISCV_ABI) -
march=$(PLATFORM_RISCV_ISA)
ASFLAGS += -mcmodel=$(PLATFORM_RISCV_CODE_MODEL) ASFLAGS += $(RELAX_FLAG)
Regards,
Xiang W
PATCH v4 1/2 Makefile: Fix -msave-restore compile warning with CLANG-10 (or lower)
Quoted 52 lines · Xiang W
? 2021-12-03???? 10:33 +0530?Anup Patel???
Quoted 50 lines
The riscv target of CLANG-10 (or lower) does not support the -m(no-)save-restore option so we get compile warnings. This patch fixes compile warning by using -m(no-)save-restore option only for GCC.
Signed-off-by Anup Patel <[email protected]>Reviewed-by Dong Du <[email protected]>--- Makefile | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)Makefile +0
-0@@ -275,8 +275,11 @@ GENFLAGS += $(platform-genflags-y) GENFLAGS += $(firmware-genflags-y) CFLAGS = -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -O2
-CFLAGS += -fno-omit-frame-pointer -fno-optimize-
sibling-callsMakefile +1
-1-CFLAGS += -mno-save-restore -mstrict-align +CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls -mstrict-align
Makefile +4
-0+# CLANG-10 (or lower) does not support -m(no-)save-restore option +ifneq ($(CC_IS_CLANG),y) +CFLAGS += -mno-save-restore +endif CFLAGS += -mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA)
CFLAGS += -mcmodel=$(PLATFORM_RISCV_CODE_MODEL) CFLAGS += $(RELAX_FLAG)Makefile +0
-1@@ -290,8 +293,11 @@ CPPFLAGS += $(platform-cppflags-y) CPPFLAGS += $(firmware-cppflags-y) ASFLAGS = -g -Wall -nostdlib -ASFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls
Makefile +1
-1-ASFLAGS += -mno-save-restore -mstrict-align +ASFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls -mstrict-align
Makefile +4
-0+# CLANG-10 (or lower) does not support -m(no-)save-restore option +ifneq ($(CC_IS_CLANG),y) +ASFLAGS += -mno-save-restore +endif ASFLAGS += -mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA)
ASFLAGS += -mcmodel=$(PLATFORM_RISCV_CODE_MODEL) ASFLAGS += $(RELAX_FLAG)
Quoted 8 lines
Quoted 4 lines
--
2.25.1Maybe we can dynamically detect whether the toolchain supports -m(no-)save-restore, just like we did before to detect whether ld supports PIE
Thanks Xiang for this suggestion, I tried it but does not work with GCC.
For now, I will go ahead with v4 patch. You can try it separately and send incremental patch.
Regards,
Anup
Quoted 55 lines
Makefile +0
-0@@ -149,7 +149,7 @@ endif # Check whether the linker supports creating PIEs OPENSBI_LD_PIE := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG)$(USE_LD_FLAG) -fPIE -nostdlib -Wl,-pie -x c
/dev/null-o/dev/nullQuoted 1 line
/dev/null2>&1 && echo y || echo n)Makefile +1
-1- +CC_SUPPORT_SAVE_RESTORE := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG)$(USE_LD_FLAG) -nostdlib -mno-save-restore -x c
/dev/null-o/dev/nullQuoted 1 line
/dev/null2>&1 && echo y || echo n)# Build Info:
#OPENSBI_BUILD_TIME_STAMP-- the compilation time stamp #OPENSBI_BUILD_COMPILER_VERSION-- the compiler version infoMakefile +0
-0@@ -275,8 +275,10 @@ GENFLAGS += $(platform-genflags-y) GENFLAGS += $(firmware-genflags-y) CFLAGS = -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -O2
-CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-
callsMakefile +1
-1-CFLAGS += -mno-save-restore -mstrict-align +CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls -mstrict-align
Makefile +3
-0+ifeq ($(CC_SUPPORT_SAVE_RESTORE),y) +CFLAGS += -mno-save-restore +endif CFLAGS += -mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA)
CFLAGS += -mcmodel=$(PLATFORM_RISCV_CODE_MODEL) CFLAGS += $(RELAX_FLAG)Makefile +0
-1@@ -290,8 +292,10 @@ CPPFLAGS += $(platform-cppflags-y) CPPFLAGS += $(firmware-cppflags-y) ASFLAGS = -g -Wall -nostdlib -ASFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls
Makefile +1
-1-ASFLAGS += -mno-save-restore -mstrict-align +ASFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls -mstrict-align
Makefile +3
-0+ifeq ($(CC_SUPPORT_SAVE_RESTORE),y) +ASFLAGS += -mno-save-restore +endif ASFLAGS += -mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA)
ASFLAGS += -mcmodel=$(PLATFORM_RISCV_CODE_MODEL) ASFLAGS += $(RELAX_FLAG)Regards,
Xiang W
Anup Patel
AppliedPATCH v4 1/2 Makefile: Fix -msave-restore compile warning with CLANG-10 (or lower)
In reply to Anup PatelQuoted 8 lines · Anup Patel
The riscv target of CLANG-10 (or lower) does not support the -m(no-)save-restore option so we get compile warnings. This patch fixes compile warning by using -m(no-)save-restore option only for GCC.
Signed-off-by Anup Patel <[email protected]>Reviewed-by Dong Du <[email protected]>
Applied this patch to the riscv/opensbi repo
Regards,
Anup
Quoted 39 lines
--- Makefile | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)Makefile +10
-5@@ -275,8 +275,11 @@ GENFLAGS += $(platform-genflags-y) GENFLAGS += $(firmware-genflags-y) CFLAGS = -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -O2 -CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls -CFLAGS += -mno-save-restore -mstrict-align +CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls -mstrict-align +# CLANG-10 (or lower) does not support -m(no-)save-restore option +ifneq ($(CC_IS_CLANG),y) +CFLAGS += -mno-save-restore +endif CFLAGS += -mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA) CFLAGS += -mcmodel=$(PLATFORM_RISCV_CODE_MODEL) CFLAGS += $(RELAX_FLAG)@@ -290,8 +293,11 @@ CPPFLAGS += $(platform-cppflags-y) CPPFLAGS += $(firmware-cppflags-y) ASFLAGS = -g -Wall -nostdlib -ASFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls -ASFLAGS += -mno-save-restore -mstrict-align +ASFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls -mstrict-align +# CLANG-10 (or lower) does not support -m(no-)save-restore option +ifneq ($(CC_IS_CLANG),y) +ASFLAGS += -mno-save-restore +endif ASFLAGS += -mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA) ASFLAGS += -mcmodel=$(PLATFORM_RISCV_CODE_MODEL) ASFLAGS += $(RELAX_FLAG) --2.25.1
PATCH v4 2/2 lib: sbi: Fix compile errors using -Os option
In reply to Anup PatelWhen compiling with -Os option along with -ffreestanding, both GCC and clang will add implicit calls to memcpy() and memset() for stack variables initialized in declaration.
The C standard as per Clause 4, the compiler cannot necessarily assume that anything beyond:
* float.h
* iso646.h
* limits.h
* stdalign.h
* stdarg.h
* stdbool.h
* stddef.h
* stdint.h
* stdnoreturn.h
* fenv.h
* math.h
* and the numeric conversion functions of stdlib.h.
This patch maps memcpy() and memset() as weak-alias of sbi_memcpy() and sbi_memset() respectively so that implicit calls to memcpy() and memset() will compile properly.
--- lib/sbi/sbi_string.c | 6 ++++++ 1 file changed, 6 insertions(+)
lib/sbi/sbi_string.c +6 -0
@@ -122,6 +122,9 @@ void *sbi_memset(void *s, int c, size_t count)return s; } +void *memset (void *s,int c,size_t count) \ +__attribute__((weak,alias ("sbi_memset" ))); +void *sbi_memcpy (void *dest,const void *src,size_t count) {char *temp1 = dest;
@@ -135,6 +138,9 @@ void *sbi_memcpy(void *dest, const void *src, size_t count)return dest; } +void *memcpy (void *dest,const void *src,size_t count) \ +__attribute__((weak,alias ("sbi_memcpy" ))); +void *sbi_memmove (void *dest,const void *src,size_t count) {char *temp1 = (char *)dest;
Signature · 1 line
2.25.1
Anup Patel
AppliedPATCH v4 2/2 lib: sbi: Fix compile errors using -Os option
Quoted 21 lines · Anup Patel
When compiling with -Os option along with -ffreestanding, both GCC and clang will add implicit calls to
memcpy()andmemset()for stack variables initialized in declaration.The C standard as per Clause 4, the compiler cannot necessarily assume that anything beyond:
*
float.h
*iso646.h
*limits.h
*stdalign.h
*stdarg.h
*stdbool.h
*stddef.h
*stdint.h
*stdnoreturn.h
*fenv.h
*math.h
* and the numeric conversion functions ofstdlib.h.
Quoted 7 lines
This patch maps
memcpy()andmemset()as weak-alias ofsbi_memcpy()andsbi_memset()respectively so that implicit calls tomemcpy()andmemset()will compile properly.Signed-off-by Anup Patel <[email protected]>Reviewed-by Dong Du <[email protected]>Reviewed-by Xiang W <[email protected]>
Applied this patch to the riscv/opensbi repo
Regards,
Anup
Quoted 31 lines
--- lib/sbi/sbi_string.c | 6 ++++++ 1 file changed, 6 insertions(+)lib/sbi/sbi_string.c +6
-1@@ -122,6 +122,9 @@ void *sbi_memset(void *s, int c, size_t count)return s; } +void *memset (void *s,int c,size_t count) \ +__attribute__((weak,alias ("sbi_memset" ))); +void *sbi_memcpy (void *dest,const void *src,size_t count) {char *temp1 = dest;@@ -135,6 +138,9 @@ void *sbi_memcpy(void *dest, const void *src, size_t count)return dest; } +void *memcpy (void *dest,const void *src,size_t count) \ +__attribute__((weak,alias ("sbi_memcpy" ))); +void *sbi_memmove (void *dest,const void *src,size_t count) {char *temp1 = (char *)dest; --2.25.1