[PATCH v2] makefile: fix Clang link error when RSA_PUBLIC_KEY_PARSER is selected
Raymond Mao
raymond.mao at linaro.org
Tue Jun 3 16:42:55 CEST 2025
MbedTLS bignum module needs '__udivti3' which is a 128-bit division
function provided by the compiler runtime, typically libgcc for GCC or
clang_rt.builtins for Clang.
Thus 'clang_rt.builtins' library is required when building using Clang.
This patch supports the location layout of clang_rt.builtins libraries
from both LLVM pre-15 pattern [1] and post-15(up to 20) pattern [2].
As a fact that starts from LLVM 20, no official prebuilt multi-targets
are included and users might need to cross-built it from source. [3] is
an example of instructions for building.
[1] <INST_DIR>/lib/clang/<REV>/lib/linux/libclang_rt.builtins-<ARCH>.a
E.g.
lib/clang/14.0.0/lib/linux/libclang_rt.builtins-aarch64.a
lib/clang/14.0.0/lib/linux/libclang_rt.builtins-x86_64.a
[2] <INST_DIR>/lib/clang/<REV_MAJOR>/lib/<CROSSTOOL>/libclang_rt.builtins.a
E.g.
lib/clang/20/lib/aarch64-none-linux-gnu/libclang_rt.builtins.a
lib/clang/20/lib/x86_64-unknown-linux-gnu/libclang_rt.builtins.a
[3]
git clone https://github.com/llvm/llvm-project.git
cd llvm-project
git checkout -b llvmorg-20.1.6 llvmorg-20.1.6
mkdir build-builtin && cd build-builtin
cmake -G Ninja ../llvm \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_C_COMPILER=clang \
-DLLVM_ENABLE_PROJECTS="clang" \
-DLLVM_ENABLE_RUNTIMES="compiler-rt" \
-DLLVM_TARGETS_TO_BUILD="X86;AArch64" \
-DLLVM_BUILTIN_TARGETS=\
"x86_64-unknown-linux-gnu;aarch64-none-linux-gnu" \
-DBUILTINS_aarch64-none-linux-gnu_CMAKE_SYSTEM_NAME=Linux \
-DBUILTINS_aarch64-none-linux-gnu_CMAKE_C_COMPILER=\
<YOUR_TOOLCHAIN_GCC> \
-DBUILTINS_aarch64-none-linux-gnu_CMAKE_ASM_COMPILER=\
<YOUR_TOOLCHAIN_GCC> \
-DBUILTINS_aarch64-none-linux-gnu_CMAKE_SYSROOT=\
<YOUR_TOOLCHAIN_LIBC_DIR> \
-DCOMPILER_RT_BUILD_BUILTINS=ON \
-DCOMPILER_RT_BUILD_SANITIZERS=OFF \
-DCOMPILER_RT_BUILD_XRAY=OFF \
-DCOMPILER_RT_BUILD_LIBFUZZER=OFF \
-DCOMPILER_RT_BUILD_PROFILE=OFF \
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR> \
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF
ninja builtins
sudo ninja install
Fixes: 13de8483388c ("mbedtls: add mbedtls into the build system")
Signed-off-by: Raymond Mao <raymond.mao at linaro.org>
---
Changes in V2:
- Add support for LLVM post-15.
Makefile | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
index f88684947ee..6cec94e1cd3 100644
--- a/Makefile
+++ b/Makefile
@@ -37,7 +37,6 @@ else ifeq ("riscv32", $(MK_ARCH))
else ifeq ("riscv64", $(MK_ARCH))
export HOST_ARCH=$(HOST_ARCH_RISCV64)
endif
-undefine MK_ARCH
# Avoid funny character set dependencies
unexport LC_ALL
@@ -915,8 +914,27 @@ PLATFORM_LIBGCC = arch/$(ARCH)/lib/lib.a
else
ifndef CONFIG_CC_IS_CLANG
PLATFORM_LIBGCC := -L $(shell dirname `$(CC) $(c_flags) -print-libgcc-file-name`) -lgcc
+else
+# mbedtls bignum needs '__udivti3' - a 128-bit division function that's provided by clang-rt.
+ifeq ($(CONFIG_RSA_PUBLIC_KEY_PARSER_MBEDTLS),y)
+CLANG_RES_DIR := $(shell $(CC) --print-resource-dir)
+# Find all matching libclang_rt.builtins*.a files
+CLANG_RT_LIB_FULL := $(shell find $(CLANG_RES_DIR)/lib -name "libclang_rt.builtins*.a" 2>/dev/null | grep "$(MK_ARCH)" | head -n 1)
+CLANG_RT_DIR := $(shell dirname $(CLANG_RT_LIB_FULL))
+ifeq ($(CLANG_RT_LIB_FULL),)
+$(info libclang_rt.builtins.a not found for target $(MK_ARCH))
+else ifneq ($(findstring /linux/, $(CLANG_RT_LIB_FULL)),)
+# Legacy(pre-LLVM15) layout (<INSTALLED_DIR>/lib/clang/<REV>/lib/linux/libclang_rt.builtins-<ARCH>.a)
+PLATFORM_LIBGCC := -L$(CLANG_RT_DIR) -lclang_rt.builtins-$(shell echo $(MK_ARCH) | tr -d '"')
+else
+# New(post-LLVM15) layout (<INSTALLED_DIR>/lib/clang/<REV_MAJOR>/lib/<CROSSTOOL_NAME>/libclang_rt.builtins.a)
+PLATFORM_LIBGCC := -L$(CLANG_RT_DIR) -lclang_rt.builtins
endif
-endif
+endif # CONFIG_RSA_PUBLIC_KEY_PARSER_MBEDTLS
+endif # CONFIG_CC_IS_CLANG
+endif # CONFIG_USE_PRIVATE_LIBGCC
+
+undefine MK_ARCH
PLATFORM_LIBS += $(PLATFORM_LIBGCC)
ifdef CONFIG_CC_COVERAGE
--
2.25.1
More information about the U-Boot
mailing list