Skip to contentSkip to conversation

Misc compile error/warning fixes

PATCH New Anup Patel 3 participants 7 messages Dec 3, 2021 – Dec 11, 2021 Kbuild

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

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
author
#1 lore raw

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
index 8623c1c..d89a0c5 100644
@@ -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
#2 lore raw

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.

---
?Makefile | 14 ++++++++++----
?1 file changed, 10 insertions(+), 4 deletions(-)

Makefile +0 -0
index 8623c1c..d89a0c5 100644
@@ -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

Makefile +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-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
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
index 8623c1c..4b866ac 100644
@@ -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/null 2>&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/null 2>&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

#3 lore raw

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.

---
 Makefile | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
Makefile +0 -0
index 8623c1c..d89a0c5 100644
@@ -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

Makefile +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.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

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
index 8623c1c..4b866ac 100644
@@ -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/null 2>&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/null

Quoted 1 line

/dev/null 2>&1 && echo y || echo n)

# Build Info:
# OPENSBI_BUILD_TIME_STAMP -- the compilation time stamp # OPENSBI_BUILD_COMPILER_VERSION -- the compiler version info

Makefile +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-
calls

Makefile +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

Applied
#4 lore raw

PATCH v4 1/2 Makefile: Fix -msave-restore compile warning with CLANG-10 (or lower)

In reply to Anup Patel
Quoted 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.

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
index 8623c1c..d89a0c5 100644
@@ -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

author
#5 lore raw

PATCH v4 2/2 lib: sbi: Fix compile errors using -Os option

In reply to Anup Patel

When 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
index c87bce9..c67c02e 100644
@@ -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
Applied
#6 lore raw

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

Quoted 7 lines

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.

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
index c87bce9..c67c02e 100644
@@ -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