Skip to contentSkip to conversation

Fully support standalone Clang/LLVM toolchains

PATCH Superseded Jessica Clarke 3 participants 12 messages Jul 9, 2021 – Jul 10, 2021 Kbuild

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Fully support standalone Clang/LLVM toolchains

This patch series is comprised of three parts.

The first patch fixes a warning seen when building with LLVM due to a bogus combination of assembly directives.

The second patch adds build system support for using Clang and LLVM binutils, provided your Clang is able to locate a libgcc.a in its search path.

However, pure LLVM toolchains do not use libgcc, they use compiler-rt (libclang_rt.builtins.<arch>.a). We could change the Makefile to not hard-code -lgcc and instead use -print-libgcc-file-name, but that still requires a bare metal compiler-rt built for the right -march/-mabi to be present, which is often not the case. Moreover, we need very little from libgcc/compiler-rt; RV64 needs nothing, and RV32 only needs 64-bit division. Thus, the third patch vendors part of FreeBSD's libquad and stops linking against libgcc entirely, allowing OpenSBI to be built with just a cross-compiler. This means that building with any distro-provided LLVM just works, as does compiling with the system Clang compiler and LLD linker on FreeBSD without any external packages needed (beyond GNU make).

Changes in v3:
* Fixed the binding warning seen when building with LLVM
* Provide a default -target when CROSS_COMPILE isn't set so the variable isn't normally required to be set when cross-compiling with LLVM (but is still honoured if set).
* Pass -fuse=bfd when not using LLD to ensure an ld.bfd gets correctly picked up over a plain ld when ld is LLD. Now we always explicitly request Clang and GCC use the linker type corresponding to LD. This allows BFD to be used on FreeBSD for testing purposes despite LLD being the system linker.

Changes in v2:
* Add documentation to README.md
* Pass -fuse-ld=lld to Clang when using LLD
* Modify commit message of first commit to explain -N -> -Wl,-N change
* Bring back the old ?= uses for the non-CROSS_COMPILE (and, now, non-LLVM) case; whilst I still think that's how it should be, it's not required for this patch, just related cleanup I did to reduce the complexity of adding the LLVM case.

Jessica Clarke (3):
fw_base: Don't mark fw_platform_init as both global and weak
Makefile: Support building with Clang and LLVM binutils Drop dependency on libgcc by importing part of FreeBSD's libquad

 Makefile                              |  65 +++++-
 README.md                             |  43 +++-
 firmware/fw_base.S                    |   1 -
 lib/utils/libquad/divdi3.c            |  64 ++++++
 lib/utils/libquad/include/limits.h    |  12 ++
 lib/utils/libquad/include/sys/cdefs.h |  12 ++
 lib/utils/libquad/include/sys/types.h |  25 +++
 lib/utils/libquad/moddi3.c            |  66 ++++++
 lib/utils/libquad/objects.mk          |  14 ++
 lib/utils/libquad/qdivrem.c           | 278 ++++++++++++++++++++++++++
 lib/utils/libquad/quad.h              | 105 ++++++++++
 lib/utils/libquad/udivdi3.c           |  52 +++++
 lib/utils/libquad/umoddi3.c           |  54 +++++
 13 files changed, 782 insertions(+), 9 deletions(-)
 create mode 100644 lib/utils/libquad/divdi3.c
 create mode 100644 lib/utils/libquad/include/limits.h
 create mode 100644 lib/utils/libquad/include/sys/cdefs.h
 create mode 100644 lib/utils/libquad/include/sys/types.h
 create mode 100644 lib/utils/libquad/moddi3.c
 create mode 100644 lib/utils/libquad/objects.mk
 create mode 100644 lib/utils/libquad/qdivrem.c
 create mode 100644 lib/utils/libquad/quad.h
 create mode 100644 lib/utils/libquad/udivdi3.c
 create mode 100644 lib/utils/libquad/umoddi3.c
Signature · 1 line
2.31.0
#1 lore raw

PATCH v3 1/3 fw_base: Don't mark fw_platform_init as both global and weak

These are mutually exclusive. GNU as and LLVM both let later binding directives override earlier ones so this works as intended, but LLVM 12 turned this into a warning as there's no good reason to do such a thing and could be a potential bug. Thus, remove the redundant and incorrect .globl directive for fw_platform_init.

---
 firmware/fw_base.S | 1 -
 1 file changed, 1 deletion(-)
firmware/fw_base.S +0 -1
index a5ce946..ee2a51b 100644
@@ -553,7 +553,6 @@ _start_hang:
 
 	.section .entry, "ax", %progbits
 	.align 3
-	.globl fw_platform_init
 	.weak fw_platform_init
 fw_platform_init:
 	add	a0, a1, zero
Signature · 1 line
2.31.0
#2 lore raw

PATCH v3 1/3 fw_base: Don't mark fw_platform_init as both global and weak

Quoted 6 lines · Jessica Clarke

These are mutually exclusive. GNU as and LLVM both let later binding directives override earlier ones so this works as intended, but LLVM 12 turned this into a warning as there's no good reason to do such a thing and could be a potential bug. Thus, remove the redundant and incorrect .globl directive for fw_platform_init.

*Sigh*, not that this patch is at all interesting copyright-wise...

Jess

Quoted 19 lines

---
firmware/fw_base.S | 1 -

1 file changed, 1 deletion(-)
firmware/fw_base.S +0 -1
index a5ce946..ee2a51b 100644
@@ -553,7 +553,6 @@ _start_hang:
 
 	.section .entry, "ax", %progbits
 	.align 3
-	.globl fw_platform_init
 	.weak fw_platform_init

fw_platform_init:

	add	a0, a1, zero
Signature · 1 line
2.31.0
#3 lore raw

PATCH v3 1/3 fw_base: Don't mark fw_platform_init as both global and weak

Quoted 11 lines · Jessica Clarke
Quoted 6 lines · Jessica Clarke

These are mutually exclusive. GNU as and LLVM both let later binding directives override earlier ones so this works as intended, but LLVM 12 turned this into a warning as there's no good reason to do such a thing and could be a potential bug. Thus, remove the redundant and incorrect .globl directive for fw_platform_init.

Quoted 21 lines

*Sigh*, not that this patch is at all interesting copyright-wise...

Jess

Quoted 17 lines

---
firmware/fw_base.S | 1 -

1 file changed, 1 deletion(-)
firmware/fw_base.S +0 -1
index a5ce946..ee2a51b 100644
@@ -553,7 +553,6 @@ _start_hang:
 
      .section .entry, "ax", %progbits
      .align 3
-     .globl fw_platform_init
      .weak fw_platform_init

fw_platform_init:
add a0, a1, zero
--

#4 lore raw

PATCH v3 2/3 Makefile: Support building with Clang and LLVM binutils

In reply to Jessica Clarke

This is intended to mirror the Linux kernel. Building with CC=clang will use Clang as the compiler but default to using the existing binutils. Building with LLVM=1 will default to using Clang and LLVM binutils.

Whilst GCC will accept the -N linker option and forward it on to the linker, Clang will not, and so in order to support both compilers we must use -Wl, to forward it to the linker as is required for most other linker options.

---
 Makefile  | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
 README.md | 43 ++++++++++++++++++++++++++++++++++--
 2 files changed, 100 insertions(+), 8 deletions(-)
Makefile +59 -6
index 6b64205..50b9f89 100644
@@ -76,26 +76,54 @@ OPENSBI_VERSION_MINOR=`grep "define OPENSBI_VERSION_MINOR" $(include_dir)/sbi/sb
 OPENSBI_VERSION_GIT=$(shell if [ -d $(src_dir)/.git ]; then git describe 2> /dev/null; fi)
 
 # Setup compilation commands
+ifneq ($(LLVM),)
+CC		=	clang
+AR		=	llvm-ar
+LD		=	ld.lld
+OBJCOPY		=	llvm-objcopy
+else
 ifdef CROSS_COMPILE
 CC		=	$(CROSS_COMPILE)gcc
-CPP		=	$(CROSS_COMPILE)cpp
 AR		=	$(CROSS_COMPILE)ar
 LD		=	$(CROSS_COMPILE)ld
 OBJCOPY		=	$(CROSS_COMPILE)objcopy
 else
 CC		?=	gcc
-CPP		?=	cpp
 AR		?=	ar
 LD		?=	ld
 OBJCOPY		?=	objcopy
 endif
+endif
+CPP		=	$(CC) -E
 AS		=	$(CC)
 DTC		=	dtc
 
-# Guess the compillers xlen
-OPENSBI_CC_XLEN := $(shell TMP=`$(CC) -dumpmachine | sed 's/riscv\([0-9][0-9]\).*/\1/'`; echo $${TMP})
+ifneq ($(shell $(CC) --version 2>&1 | head -n 1 | grep clang),)
+CC_IS_CLANG	=	y
+else
+CC_IS_CLANG	=	n
+endif
+
+ifneq ($(shell $(LD) --version 2>&1 | head -n 1 | grep LLD),)
+LD_IS_LLD	=	y
+else
+LD_IS_LLD	=	n
+endif
+
+ifeq ($(CC_IS_CLANG),y)
+ifneq ($(CROSS_COMPILE),)
+CLANG_TARGET	=	-target $(notdir $(CROSS_COMPILE:%-=%))
+endif
+endif
+
+# Guess the compiler's XLEN
+OPENSBI_CC_XLEN := $(shell TMP=`$(CC) $(CLANG_TARGET) -dumpmachine | sed 's/riscv\([0-9][0-9]\).*/\1/'`; echo $${TMP})
+
+# Guess the compiler's ABI and ISA
+ifneq ($(CC_IS_CLANG),y)
 OPENSBI_CC_ABI := $(shell TMP=`$(CC) -v 2>&1 | sed -n 's/.*\(with\-abi=\([a-zA-Z0-9]*\)\).*/\2/p'`; echo $${TMP})
 OPENSBI_CC_ISA := $(shell TMP=`$(CC) -v 2>&1 | sed -n 's/.*\(with\-arch=\([a-zA-Z0-9]*\)\).*/\2/p'`; echo $${TMP})
+endif
 
 # Setup platform XLEN
 ifndef PLATFORM_RISCV_XLEN
@@ -106,6 +134,12 @@ ifndef PLATFORM_RISCV_XLEN
   endif
 endif
 
+ifeq ($(CC_IS_CLANG),y)
+ifeq ($(CROSS_COMPILE),)
+CLANG_TARGET	=	-target riscv$(PLATFORM_RISCV_XLEN)-unknown-elf
+endif
+endif
+
 # Setup list of objects.mk files
 ifdef PLATFORM
 platform-object-mks=$(shell if [ -d $(platform_src_dir)/ ]; then find $(platform_src_dir) -iname "objects.mk" | sort -r; fi)
@@ -194,7 +228,11 @@ else
 endif
 
 # Setup compilation commands flags
-GENFLAGS	=	-I$(platform_src_dir)/include
+ifeq ($(CC_IS_CLANG),y)
+GENFLAGS	+=	$(CLANG_TARGET)
+GENFLAGS	+=	-Wno-unused-command-line-argument
+endif
+GENFLAGS	+=	-I$(platform_src_dir)/include
 GENFLAGS	+=	-I$(include_dir)
 ifneq ($(OPENSBI_VERSION_GIT),)
 GENFLAGS	+=	-DOPENSBI_VERSION_GIT="\"$(OPENSBI_VERSION_GIT)\""
@@ -208,6 +246,9 @@ CFLAGS		+=	-fno-omit-frame-pointer -fno-optimize-sibling-calls
 CFLAGS		+=	-mno-save-restore -mstrict-align
 CFLAGS		+=	-mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA)
 CFLAGS		+=	-mcmodel=$(PLATFORM_RISCV_CODE_MODEL)
+ifeq ($(LD_IS_LLD),y)
+CFLAGS		+=	-mno-relax
+endif
 CFLAGS		+=	$(GENFLAGS)
 CFLAGS		+=	$(platform-cflags-y)
 CFLAGS		+=	-fno-pie -no-pie
@@ -222,18 +263,30 @@ ASFLAGS		+=	-fno-omit-frame-pointer -fno-optimize-sibling-calls
 ASFLAGS		+=	-mno-save-restore -mstrict-align
 ASFLAGS		+=	-mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA)
 ASFLAGS		+=	-mcmodel=$(PLATFORM_RISCV_CODE_MODEL)
+ifeq ($(LD_IS_LLD),y)
+ASFLAGS		+=	-mno-relax
+endif
 ASFLAGS		+=	$(GENFLAGS)
 ASFLAGS		+=	$(platform-asflags-y)
 ASFLAGS		+=	$(firmware-asflags-y)
 
 ARFLAGS		=	rcs
 
-ELFFLAGS	+=	-Wl,--build-id=none -N -static-libgcc -lgcc
+ifeq ($(LD_IS_LLD),y)
+ELFFLAGS	+=	-fuse-ld=lld
+else
+ELFFLAGS	+=	-fuse-ld=bfd
+endif
+ELFFLAGS	+=	-Wl,--build-id=none -Wl,-N -static-libgcc -lgcc
 ELFFLAGS	+=	$(platform-ldflags-y)
 ELFFLAGS	+=	$(firmware-ldflags-y)
 
 MERGEFLAGS	+=	-r
+ifeq ($(LD_IS_LLD),y)
+MERGEFLAGS	+=	-b elf
+else
 MERGEFLAGS	+=	-b elf$(PLATFORM_RISCV_XLEN)-littleriscv
+endif
 MERGEFLAGS	+=	-m elf$(PLATFORM_RISCV_XLEN)lriscv
 
 DTSCPPFLAGS	=	$(CPPFLAGS) -nostdinc -nostdlib -fno-builtin -D__DTS__ -x assembler-with-cpp
README.md +41 -2

55 lines not on this page · raw message

Signature · 1 line
2.31.0
#5 lore raw

PATCH v3 2/3 Makefile: Support building with Clang and LLVM binutils

Quoted 10 lines · Jessica Clarke

This is intended to mirror the Linux kernel. Building with CC=clang will use Clang as the compiler but default to using the existing binutils. Building with LLVM=1 will default to using Clang and LLVM binutils.

Whilst GCC will accept the -N linker option and forward it on to the linker, Clang will not, and so in order to support both compilers we must use -Wl, to forward it to the linker as is required for most other linker options.

Quoted 6 lines
---
 Makefile  | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
 README.md | 43 ++++++++++++++++++++++++++++++++++--
 2 files changed, 100 insertions(+), 8 deletions(-)

Hardcoding "-target" to riscv64-unknown-elf, and building with clang and gnu ld, I got:

$ make CC=clang CROSS_COMPILE=riscv64-linux- PLATFORM=generic

ELF platform/generic/firmware/payloads/test.elf
/usr/bin/ld.bfd: cannot represent machine `riscv'
clang-12: error: ld.lld command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:404:
opensbi/build/platform/generic/firmware/payloads/test.elf] Error 1

Regards,
Bin

#6 lore raw

PATCH v3 3/3 Drop dependency on libgcc by importing part of FreeBSD's libquad

In reply to Jessica Clarke

We only need libgcc for 64-bit division on RV32. Whilst GCC toolchains bundle libgcc, Clang toolchains tend not to ship libclang_rt.builtins given every compiler is a cross-compiler for every target and so you would need a silly number of builds of it, with only the native library available; only vendor-provided Clang toolchains specifically for bare metal cross-compiling are likely to provide it.

Thus, import part of FreeBSD's implementation of the division support functions needed and stop linking against libgcc.

---
 Makefile                              |   2 +-
 lib/utils/libquad/divdi3.c            |  64 ++++++
 lib/utils/libquad/include/limits.h    |  12 ++
 lib/utils/libquad/include/sys/cdefs.h |  12 ++
 lib/utils/libquad/include/sys/types.h |  25 +++
 lib/utils/libquad/moddi3.c            |  66 ++++++
 lib/utils/libquad/objects.mk          |  14 ++
 lib/utils/libquad/qdivrem.c           | 278 ++++++++++++++++++++++++++
 lib/utils/libquad/quad.h              | 105 ++++++++++
 lib/utils/libquad/udivdi3.c           |  52 +++++
 lib/utils/libquad/umoddi3.c           |  54 +++++
 11 files changed, 683 insertions(+), 1 deletion(-)
Makefile +1 -1
index 50b9f89..0672558 100644
@@ -277,7 +277,7 @@ ELFFLAGS	+=	-fuse-ld=lld
 else
 ELFFLAGS	+=	-fuse-ld=bfd
 endif
-ELFFLAGS	+=	-Wl,--build-id=none -Wl,-N -static-libgcc -lgcc
+ELFFLAGS	+=	-Wl,--build-id=none -Wl,-N
 ELFFLAGS	+=	$(platform-ldflags-y)
 ELFFLAGS	+=	$(firmware-ldflags-y)
 
lib/utils/libquad/divdi3.c new file +64 -0

64 lines not on this page · raw message

lib/utils/libquad/include/limits.h new file +12 -0
new file mode 100644
index 0000000..9c69d27
@@ -0,0 +1,12 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 Jessica Clarke <[email protected]>
+ */
+
+#ifndef __LIMITS_H__
+#define __LIMITS_H__
+
+#define CHAR_BIT 8
+
+#endif
lib/utils/libquad/include/sys/cdefs.h new file +12 -0
new file mode 100644
index 0000000..e8d6faa
@@ -0,0 +1,12 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 Jessica Clarke <[email protected]>
+ */
+
+#ifndef __SYS_CDEFS_H__
+#define __SYS_CDEFS_H__
+
+#define __FBSDID(s) struct __hack
+
+#endif
lib/utils/libquad/include/sys/types.h new file +25 -0
new file mode 100644
index 0000000..e372437
@@ -0,0 +1,25 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 Jessica Clarke <[email protected]>
+ */
+
+#ifndef __SYS_TYPES_H__
+#define __SYS_TYPES_H__
+
+#include <sbi/sbi_types.h>
+
+typedef unsigned long u_long;
+
+typedef int64_t quad_t;
+typedef uint64_t u_quad_t;
+
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#define _QUAD_LOWWORD 1
+#define _QUAD_HIGHWORD 0
+#else
+#define _QUAD_LOWWORD 0
+#define _QUAD_HIGHWORD 1
+#endif
+
+#endif
lib/utils/libquad/moddi3.c new file +66 -0

66 lines not on this page · raw message

lib/utils/libquad/objects.mk new file +14 -0
new file mode 100644
index 0000000..5e53cd0
@@ -0,0 +1,14 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2021 Jessica Clarke <[email protected]>
+#
+
+ifeq ($(PLATFORM_RISCV_XLEN),32)
+libsbiutils-objs-y += libquad/divdi3.o
+libsbiutils-objs-y += libquad/moddi3.o
+libsbiutils-objs-y += libquad/qdivrem.o
+libsbiutils-objs-y += libquad/udivdi3.o
+libsbiutils-objs-y += libquad/umoddi3.o
+libsbiutils-genflags-y += -I$(libsbiutils_dir)/libquad/include
+endif
lib/utils/libquad/qdivrem.c new file +278 -0

278 lines not on this page · raw message

lib/utils/libquad/quad.h new file +105 -0

105 lines not on this page · raw message

lib/utils/libquad/udivdi3.c new file +52 -0

52 lines not on this page · raw message

lib/utils/libquad/umoddi3.c new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.31.0
#7 lore raw

v3 cover Fully support standalone Clang/LLVM toolchains

In reply to Jessica Clarke

? 2021-07-09???? 05:12 +0100?Jessica Clarke???
I tested these patches. Some bugs reported

ld.lld: error: can't create dynamic relocation R_RISCV_32 against
symbol: _fw_start in readonly segment; recompile object files with - fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

Quoted 1 line
Quoted 1 line
Quoted 1 line

defined in

/home/merle/workspaces/opensbi/build/platform/generic/firmware/fw_dynam ic.elf.ld:9

Quoted 1 line
Quoted 1 line
Quoted 1 line

referenced by fw_base.S:502

(/home/merle/workspaces/opensbi/firmware/fw_base.S:502)

Quoted 1 line
Quoted 1 line
Quoted 1 line

/home/merle/workspaces/opensbi/build/platform/generic/firmware/fw_dynam ic.o:(.entry+0x3BC)

Data is stored in the code segment of base.S. Can be fix by:

firmware/fw_base.S +1 -0
index ee2a51b..f8aea05 100644
@@ -501,6 +501,7 @@ _skip_trap_exit_rv32_hyp:
        /* We don't expect to reach here hence just hang */
        j       _start_hang
 
+       .data
        .align 3
 #ifdef FW_PIC
 _runtime_offset:
Quoted 90 lines

This patch series is comprised of three parts.

The first patch fixes a warning seen when building with LLVM due to a bogus combination of assembly directives.

The second patch adds build system support for using Clang and LLVM binutils, provided your Clang is able to locate a libgcc.a in its search
path.

However, pure LLVM toolchains do not use libgcc, they use compiler-rt (libclang_rt.builtins.<arch>.a). We could change the Makefile to not hard-code -lgcc and instead use -print-libgcc-file-name, but that still
requires a bare metal compiler-rt built for the right -march/-mabi to be
present, which is often not the case. Moreover, we need very little from
libgcc/compiler-rt; RV64 needs nothing, and RV32 only needs 64-bit division. Thus, the third patch vendors part of FreeBSD's libquad and stops linking against libgcc entirely, allowing OpenSBI to be built with
just a cross-compiler. This means that building with any distro- provided
LLVM just works, as does compiling with the system Clang compiler and LLD linker on FreeBSD without any external packages needed (beyond GNU
make).

Changes in v3:
? * Fixed the binding warning seen when building with LLVM ? * Provide a default -target when CROSS_COMPILE isn't set so the ??? variable isn't normally required to be set when cross-compiling with
??? LLVM (but is still honoured if set).
? * Pass -fuse=bfd when not using LLD to ensure an ld.bfd gets correctly
??? picked up over a plain ld when ld is LLD. Now we always explicitly
??? request Clang and GCC use the linker type corresponding to LD. This
??? allows BFD to be used on FreeBSD for testing purposes despite LLD ??? being the system linker.

Changes in v2:
? * Add documentation to README.md
? * Pass -fuse-ld=lld to Clang when using LLD
? * Modify commit message of first commit to explain -N -> -Wl,-N change
? * Bring back the old ?= uses for the non-CROSS_COMPILE (and, now, ??? non-LLVM) case; whilst I still think that's how it should be, it's
??? not required for this patch, just related cleanup I did to reduce ??? the complexity of adding the LLVM case.

Jessica Clarke (3):
? fw_base: Don't mark fw_platform_init as both global and weak ? Makefile: Support building with Clang and LLVM binutils ? Drop dependency on libgcc by importing part of FreeBSD's libquad

?Makefile????????????????????????????? |? 65 +++++-
?README.md???????????????????????????? |? 43 +++-
?firmware/fw_base.S??????????????????? |?? 1 -
?lib/utils/libquad/divdi3.c??????????? |? 64 ++++++
?lib/utils/libquad/include/limits.h??? |? 12 ++
?lib/utils/libquad/include/sys/cdefs.h |? 12 ++
?lib/utils/libquad/include/sys/types.h |? 25 +++
?lib/utils/libquad/moddi3.c??????????? |? 66 ++++++
?lib/utils/libquad/objects.mk????????? |? 14 ++
?lib/utils/libquad/qdivrem.c?????????? | 278
++++++++++++++++++++++++++
?lib/utils/libquad/quad.h????????????? | 105 ++++++++++
?lib/utils/libquad/udivdi3.c?????????? |? 52 +++++
?lib/utils/libquad/umoddi3.c?????????? |? 54 +++++

?13 files changed, 782 insertions(+), 9 deletions(-) ?create mode 100644 lib/utils/libquad/divdi3.c ?create mode 100644 lib/utils/libquad/include/limits.h ?create mode 100644 lib/utils/libquad/include/sys/cdefs.h ?create mode 100644 lib/utils/libquad/include/sys/types.h ?create mode 100644 lib/utils/libquad/moddi3.c ?create mode 100644 lib/utils/libquad/objects.mk ?create mode 100644 lib/utils/libquad/qdivrem.c ?create mode 100644 lib/utils/libquad/quad.h ?create mode 100644 lib/utils/libquad/udivdi3.c ?create mode 100644 lib/utils/libquad/umoddi3.c

Signature · 1 line
2.31.0
#8 lore raw

v3 cover Fully support standalone Clang/LLVM toolchains

Quoted 7 lines · Xiang W

? 2021-07-09???? 05:12 +0100?Jessica Clarke???
I tested these patches. Some bugs reported

ld.lld: error: can't create dynamic relocation R_RISCV_32 against
symbol: _fw_start in readonly segment; recompile object files with - fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

Quoted 8 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

defined in

/home/merle/workspaces/opensbi/build/platform/generic/firmware/fw_dynam ic.elf.ld:9

Quoted 1 line
Quoted 1 line
Quoted 1 line

referenced by fw_base.S:502

(/home/merle/workspaces/opensbi/firmware/fw_base.S:502)

Quoted 1 line
Quoted 1 line
Quoted 1 line

/home/merle/workspaces/opensbi/build/platform/generic/firmware/fw_dynam ic.o:(.entry+0x3BC)

This was reported by Bin. Don?t use a Linux triple for something that?s neither the Linux kernel nor a Linux userspace executable/library.

Jess

Quoted 110 lines

Data is stored in the code segment of base.S. Can be fix by:

firmware/fw_base.S +1 -0
index ee2a51b..f8aea05 100644
@@ -501,6 +501,7 @@ _skip_trap_exit_rv32_hyp:
       /* We don't expect to reach here hence just hang */
       j       _start_hang
 
+       .data
       .align 3
 #ifdef FW_PIC

_runtime_offset:

Quoted 90 lines

This patch series is comprised of three parts.

The first patch fixes a warning seen when building with LLVM due to a bogus combination of assembly directives.

The second patch adds build system support for using Clang and LLVM binutils, provided your Clang is able to locate a libgcc.a in its search
path.

However, pure LLVM toolchains do not use libgcc, they use compiler-rt (libclang_rt.builtins.<arch>.a). We could change the Makefile to not hard-code -lgcc and instead use -print-libgcc-file-name, but that still
requires a bare metal compiler-rt built for the right -march/-mabi to be
present, which is often not the case. Moreover, we need very little from
libgcc/compiler-rt; RV64 needs nothing, and RV32 only needs 64-bit division. Thus, the third patch vendors part of FreeBSD's libquad and stops linking against libgcc entirely, allowing OpenSBI to be built with
just a cross-compiler. This means that building with any distro- provided
LLVM just works, as does compiling with the system Clang compiler and LLD linker on FreeBSD without any external packages needed (beyond GNU
make).

Changes in v3:
* Fixed the binding warning seen when building with LLVM
* Provide a default -target when CROSS_COMPILE isn't set so the variable isn't normally required to be set when cross-compiling
with
LLVM (but is still honoured if set).
* Pass -fuse=bfd when not using LLD to ensure an ld.bfd gets
correctly
picked up over a plain ld when ld is LLD. Now we always
explicitly
request Clang and GCC use the linker type corresponding to LD.
This
allows BFD to be used on FreeBSD for testing purposes despite LLD being the system linker.

Changes in v2:
* Add documentation to README.md
* Pass -fuse-ld=lld to Clang when using LLD
* Modify commit message of first commit to explain -N -> -Wl,-N
change
* Bring back the old ?= uses for the non-CROSS_COMPILE (and, now, non-LLVM) case; whilst I still think that's how it should be,
it's
not required for this patch, just related cleanup I did to reduce the complexity of adding the LLVM case.

Jessica Clarke (3):
fw_base: Don't mark fw_platform_init as both global and weak
Makefile: Support building with Clang and LLVM binutils Drop dependency on libgcc by importing part of FreeBSD's libquad

 Makefile                              |  65 +++++-
 README.md                             |  43 +++-
 firmware/fw_base.S                    |   1 -
 lib/utils/libquad/divdi3.c            |  64 ++++++
 lib/utils/libquad/include/limits.h    |  12 ++
 lib/utils/libquad/include/sys/cdefs.h |  12 ++
 lib/utils/libquad/include/sys/types.h |  25 +++
 lib/utils/libquad/moddi3.c            |  66 ++++++
 lib/utils/libquad/objects.mk          |  14 ++
 lib/utils/libquad/qdivrem.c           | 278
++++++++++++++++++++++++++
 lib/utils/libquad/quad.h              | 105 ++++++++++
 lib/utils/libquad/udivdi3.c           |  52 +++++
 lib/utils/libquad/umoddi3.c           |  54 +++++
 13 files changed, 782 insertions(+), 9 deletions(-)
 create mode 100644 lib/utils/libquad/divdi3.c
 create mode 100644 lib/utils/libquad/include/limits.h
 create mode 100644 lib/utils/libquad/include/sys/cdefs.h
 create mode 100644 lib/utils/libquad/include/sys/types.h
 create mode 100644 lib/utils/libquad/moddi3.c
 create mode 100644 lib/utils/libquad/objects.mk
 create mode 100644 lib/utils/libquad/qdivrem.c
 create mode 100644 lib/utils/libquad/quad.h
 create mode 100644 lib/utils/libquad/udivdi3.c
 create mode 100644 lib/utils/libquad/umoddi3.c
Signature · 1 line
2.31.0
#9 lore raw

v3 cover Fully support standalone Clang/LLVM toolchains

? 2021-07-09???? 14:49 +0100?Jessica Clarke???

Quoted 21 lines · Xiang W
Quoted 13 lines

? 2021-07-09???? 05:12 +0100?Jessica Clarke???
I tested these patches. Some bugs reported

ld.lld: error: can't create dynamic relocation R_RISCV_32 against
symbol: _fw_start in readonly segment; recompile object files with -
fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

Quoted 1 line
Quoted 1 line
Quoted 1 line

defined in

/home/merle/workspaces/opensbi/build/platform/generic/firmware/fw_d ynam
ic.elf.ld:9

Quoted 6 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

referenced by fw_base.S:502

(/home/merle/workspaces/opensbi/firmware/fw_base.S:502)

Quoted 1 line
Quoted 1 line
Quoted 1 line

/home/merle/workspaces/opensbi/build/platform/generic/firmware/fw_d ynam
ic.o:(.entry+0x3BC)

Quoted 5 lines

This was reported by Bin. Don?t use a Linux triple for something that?s
neither the Linux kernel nor a Linux userspace executable/library.

Jess

Linux triple?

Quoted 130 lines
Quoted 128 lines

Data is stored in the code segment of base.S. Can be fix by:

firmware/fw_base.S +0 -0
index ee2a51b..f8aea05 100644
@@ -501,6 +501,7 @@ _skip_trap_exit_rv32_hyp:

?????? /* We don't expect to reach here hence just hang */ ?????? j?????? _start_hang

+?????? .data
?????? .align 3
#ifdef FW_PIC
_runtime_offset:

Quoted 107 lines

This patch series is comprised of three parts.

The first patch fixes a warning seen when building with LLVM due to a
bogus combination of assembly directives.

The second patch adds build system support for using Clang and LLVM
binutils, provided your Clang is able to locate a libgcc.a in its search
path.

However, pure LLVM toolchains do not use libgcc, they use compiler-rt
(libclang_rt.builtins.<arch>.a). We could change the Makefile to not
hard-code -lgcc and instead use -print-libgcc-file-name, but that still
requires a bare metal compiler-rt built for the right -march/- mabi to
be
present, which is often not the case. Moreover, we need very little
from
libgcc/compiler-rt; RV64 needs nothing, and RV32 only needs 64- bit
division. Thus, the third patch vendors part of FreeBSD's libquad and
stops linking against libgcc entirely, allowing OpenSBI to be built
with
just a cross-compiler. This means that building with any distro- provided
LLVM just works, as does compiling with the system Clang compiler and
LLD linker on FreeBSD without any external packages needed (beyond
GNU
make).

Changes in v3:
? * Fixed the binding warning seen when building with LLVM ? * Provide a default -target when CROSS_COMPILE isn't set so the ??? variable isn't normally required to be set when cross- compiling
with
??? LLVM (but is still honoured if set).
? * Pass -fuse=bfd when not using LLD to ensure an ld.bfd gets correctly
??? picked up over a plain ld when ld is LLD. Now we always explicitly
??? request Clang and GCC use the linker type corresponding to LD.
This
??? allows BFD to be used on FreeBSD for testing purposes despite LLD
??? being the system linker.

Changes in v2:
? * Add documentation to README.md
? * Pass -fuse-ld=lld to Clang when using LLD
? * Modify commit message of first commit to explain -N -> -Wl,-N change
? * Bring back the old ?= uses for the non-CROSS_COMPILE (and, now,
??? non-LLVM) case; whilst I still think that's how it should be, it's
??? not required for this patch, just related cleanup I did to reduce
??? the complexity of adding the LLVM case.

Jessica Clarke (3):
? fw_base: Don't mark fw_platform_init as both global and weak ? Makefile: Support building with Clang and LLVM binutils ? Drop dependency on libgcc by importing part of FreeBSD's libquad

?Makefile????????????????????????????? |? 65 +++++-
?README.md???????????????????????????? |? 43 +++-
?firmware/fw_base.S??????????????????? |?? 1 -
?lib/utils/libquad/divdi3.c??????????? |? 64 ++++++
?lib/utils/libquad/include/limits.h??? |? 12 ++
?lib/utils/libquad/include/sys/cdefs.h |? 12 ++
?lib/utils/libquad/include/sys/types.h |? 25 +++
?lib/utils/libquad/moddi3.c??????????? |? 66 ++++++
?lib/utils/libquad/objects.mk????????? |? 14 ++
?lib/utils/libquad/qdivrem.c?????????? | 278
++++++++++++++++++++++++++
?lib/utils/libquad/quad.h????????????? | 105 ++++++++++
?lib/utils/libquad/udivdi3.c?????????? |? 52 +++++
?lib/utils/libquad/umoddi3.c?????????? |? 54 +++++

?13 files changed, 782 insertions(+), 9 deletions(-) ?create mode 100644 lib/utils/libquad/divdi3.c ?create mode 100644 lib/utils/libquad/include/limits.h ?create mode 100644 lib/utils/libquad/include/sys/cdefs.h ?create mode 100644 lib/utils/libquad/include/sys/types.h ?create mode 100644 lib/utils/libquad/moddi3.c ?create mode 100644 lib/utils/libquad/objects.mk ?create mode 100644 lib/utils/libquad/qdivrem.c ?create mode 100644 lib/utils/libquad/quad.h ?create mode 100644 lib/utils/libquad/udivdi3.c ?create mode 100644 lib/utils/libquad/umoddi3.c

Signature · 1 line
2.31.0
#10 lore raw

v3 cover Fully support standalone Clang/LLVM toolchains

Quoted 23 lines · Xiang W

? 2021-07-09???? 14:49 +0100?Jessica Clarke???

Quoted 21 lines · Xiang W
Quoted 19 lines

? 2021-07-09???? 05:12 +0100?Jessica Clarke???
I tested these patches. Some bugs reported

ld.lld: error: can't create dynamic relocation R_RISCV_32 against
symbol: _fw_start in readonly segment; recompile object files with -
fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

Quoted 1 line
Quoted 1 line
Quoted 1 line

defined in

/home/merle/workspaces/opensbi/build/platform/generic/firmware/fw_d ynam
ic.elf.ld:9

Quoted 1 line
Quoted 1 line
Quoted 1 line

referenced by fw_base.S:502

(/home/merle/workspaces/opensbi/firmware/fw_base.S:502)

Quoted 1 line
Quoted 1 line
Quoted 1 line

/home/merle/workspaces/opensbi/build/platform/generic/firmware/fw_d ynam
ic.o:(.entry+0x3BC)

Quoted 7 lines
Quoted 5 lines

This was reported by Bin. Don?t use a Linux triple for something that?s
neither the Linux kernel nor a Linux userspace executable/library.

Jess

Linux triple?

That error looks like you built with CROSS_COMPILE=riscv64-linux- or similar. What exactly did you run?

Jess

#11 lore raw

v3 cover Fully support standalone Clang/LLVM toolchains

? 2021-07-09???? 15:25 +0100?Jessica Clarke???

Quoted 33 lines · Xiang W
Quoted 32 lines

? 2021-07-09???? 14:49 +0100?Jessica Clarke???

Quoted 30 lines · Xiang W
Quoted 16 lines

? 2021-07-09???? 05:12 +0100?Jessica Clarke???
I tested these patches. Some bugs reported

ld.lld: error: can't create dynamic relocation R_RISCV_32 against
symbol: _fw_start in readonly segment; recompile object files with
-
fPIC or pass '-Wl,-z,notext' to allow text relocations in the output

Quoted 1 line
Quoted 1 line
Quoted 1 line

defined in

/home/merle/workspaces/opensbi/build/platform/generic/firmware/ fw_d
ynam
ic.elf.ld:9

Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

referenced by fw_base.S:502

(/home/merle/workspaces/opensbi/firmware/fw_base.S:502)

Quoted 1 line
Quoted 1 line
Quoted 1 line

/home/merle/workspaces/opensbi/build/platform/generic/firmware/ fw_d
ynam
ic.o:(.entry+0x3BC)

This was reported by Bin. Don?t use a Linux triple for something that?s
neither the Linux kernel nor a Linux userspace executable/library.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Jess

Linux triple?

That error looks like you built with CROSS_COMPILE=riscv64-linux- or similar. What exactly did you run?

Jess

I ran the following command

CROSS_COMPILE=riscv64-linux-gnu- make PLATFORM_RISCV_XLEN=32 PLATFORM=generic LLVM=1

The problem is solved after adding .data in base.S

Regards?
Xiang W