[PATCH] makefile: fix Clang link error when RSA_PUBLIC_KEY_PARSER is selected

Raymond Mao raymond.mao at linaro.org
Thu May 29 17:55:27 CEST 2025


Hi Tom, Jerome,

On Wed, 28 May 2025 at 11:46, Raymond Mao <raymond.mao at linaro.org> wrote:
>
> Hi Jerome,
>
> On Wed, 28 May 2025 at 11:37, Jerome Forissier
> <jerome.forissier at linaro.org> wrote:
> >
> >
> >
> > On 5/28/25 17:22, Raymond Mao wrote:
> > > Hi Jerome,
> > >
> > > On Wed, 28 May 2025 at 04:25, Jerome Forissier
> > > <jerome.forissier at linaro.org> wrote:
> > >>
> > >> Hi Rayond,
> > >>
> > >> On 5/27/25 23:05, Raymond Mao wrote:
> > >>> 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.
> > >>
> > >> How do you reproduce the problem? Can you give the config and build
> > >> command you are using?
> > >>
> > > I am using this:
> > > CROSS_COMPILE=aarch64-none-linux-gnu- make O=out CC="clang -target
> > > aarch64-none-linux-gnu" all
> > >
> > >>>
> > >>> Fixes: 13de8483388c ("mbedtls: add mbedtls into the build system")
> > >>> Signed-off-by: Raymond Mao <raymond.mao at linaro.org>
> > >>> ---
> > >>>  Makefile | 6 +++++-
> > >>>  1 file changed, 5 insertions(+), 1 deletion(-)
> > >>>
> > >>> diff --git a/Makefile b/Makefile
> > >>> index f88684947ee..16506dcaec6 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,13 @@ 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 ifeq ($(CONFIG_RSA_PUBLIC_KEY_PARSER_MBEDTLS),y)
> > >>> +# mbedtls bignum needs '__udivti3' - a 128-bit division function that's provided by clang-rt.
> > >>> +CLANG_RT_DIR := $(shell $(CC) --print-resource-dir)/lib/linux
> > >>> +PLATFORM_LIBGCC := -L$(CLANG_RT_DIR) -lclang_rt.builtins-$(MK_ARCH)
> > >>
> > >> I'm afraid this won't work when cross-compiling because the compiler-rt
> > >> builtins libraries are usually not installed with the compiler.
> > >> For example on my Ubuntu 24.04 machine:
> > >>
> > >> $ clang --version
> > >> Ubuntu clang version 18.1.3 (1ubuntu1)
> > >> Target: x86_64-pc-linux-gnu
> > >> Thread model: posix
> > >> InstalledDir: /usr/bin
> > >> $ clang --print-targets
> > >>
> > >>   Registered Targets:
> > >>     aarch64     - AArch64 (little endian)
> > >>     aarch64_32  - AArch64 (little endian ILP32)
> > >>     aarch64_be  - AArch64 (big endian)
> > >>     amdgcn      - AMD GCN GPUs
> > >>     arm         - ARM
> > >>     arm64       - ARM64 (little endian)
> > >>     arm64_32    - ARM64 (little endian ILP32)
> > >>     armeb       - ARM (big endian)
> > >>     avr         - Atmel AVR Microcontroller
> > >>     bpf         - BPF (host endian)
> > >>     bpfeb       - BPF (big endian)
> > >>     bpfel       - BPF (little endian)
> > >>     hexagon     - Hexagon
> > >>     lanai       - Lanai
> > >>     loongarch32 - 32-bit LoongArch
> > >>     loongarch64 - 64-bit LoongArch
> > >>     m68k        - Motorola 68000 family
> > >>     mips        - MIPS (32-bit big endian)
> > >>     mips64      - MIPS (64-bit big endian)
> > >>     mips64el    - MIPS (64-bit little endian)
> > >>     mipsel      - MIPS (32-bit little endian)
> > >>     msp430      - MSP430 [experimental]
> > >>     nvptx       - NVIDIA PTX 32-bit
> > >>     nvptx64     - NVIDIA PTX 64-bit
> > >>     ppc32       - PowerPC 32
> > >>     ppc32le     - PowerPC 32 LE
> > >>     ppc64       - PowerPC 64
> > >>     ppc64le     - PowerPC 64 LE
> > >>     r600        - AMD GPUs HD2XXX-HD6XXX
> > >>     riscv32     - 32-bit RISC-V
> > >>     riscv64     - 64-bit RISC-V
> > >>     sparc       - Sparc
> > >>     sparcel     - Sparc LE
> > >>     sparcv9     - Sparc V9
> > >>     systemz     - SystemZ
> > >>     thumb       - Thumb
> > >>     thumbeb     - Thumb (big endian)
> > >>     ve          - VE
> > >>     wasm32      - WebAssembly 32-bit
> > >>     wasm64      - WebAssembly 64-bit
> > >>     x86         - 32-bit X86: Pentium-Pro and above
> > >>     x86-64      - 64-bit X86: EM64T and AMD64
> > >>     xcore       - XCore
> > >>     xtensa      - Xtensa 32
> > >> $ clang --print-resource-dir
> > >> /usr/lib/llvm-18/lib/clang/18
> > >> $ ls /usr/lib/llvm-18/lib/clang/18/lib/linux/libclang_rt.builtins*
> > >> /usr/lib/llvm-18/lib/clang/18/lib/linux/libclang_rt.builtins-i386.a  /usr/lib/llvm-18/lib/clang/18/lib/linux/libclang_rt.builtins-x86_64.a
> > >>
> > >>
> > >> Are you doing cross compilation for arm or arm64? Did you have to
> > >> install the compiler-rt libraries separately from the compiler?
> > >>
> > > I installed clang manually.
> > >
> > > This is what I have:
> > > $ which clang
> > > /home/raymond/toolchain/clang-12.0.0/bin/clang
> > > $ clang --version
> > > clang version 12.0.0
> > > Target: x86_64-unknown-linux-gnu
> > > Thread model: posix
> > > InstalledDir: /home/raymond/toolchain/clang-12.0.0/bin
> > > $ clang --print-targets
> > >   Registered Targets:
> > >     aarch64    - AArch64 (little endian)
> > >     aarch64_32 - AArch64 (little endian ILP32)
> > >     aarch64_be - AArch64 (big endian)
> > >     amdgcn     - AMD GCN GPUs
> > >     arm        - ARM
> > >     arm64      - ARM64 (little endian)
> > >     arm64_32   - ARM64 (little endian ILP32)
> > >     armeb      - ARM (big endian)
> > >     avr        - Atmel AVR Microcontroller
> > >     bpf        - BPF (host endian)
> > >     bpfeb      - BPF (big endian)
> > >     bpfel      - BPF (little endian)
> > >     hexagon    - Hexagon
> > >     lanai      - Lanai
> > >     mips       - MIPS (32-bit big endian)
> > >     mips64     - MIPS (64-bit big endian)
> > >     mips64el   - MIPS (64-bit little endian)
> > >     mipsel     - MIPS (32-bit little endian)
> > >     msp430     - MSP430 [experimental]
> > >     nvptx      - NVIDIA PTX 32-bit
> > >     nvptx64    - NVIDIA PTX 64-bit
> > >     ppc32      - PowerPC 32
> > >     ppc32le    - PowerPC 32 LE
> > >     ppc64      - PowerPC 64
> > >     ppc64le    - PowerPC 64 LE
> > >     r600       - AMD GPUs HD2XXX-HD6XXX
> > >     riscv32    - 32-bit RISC-V
> > >     riscv64    - 64-bit RISC-V
> > >     sparc      - Sparc
> > >     sparcel    - Sparc LE
> > >     sparcv9    - Sparc V9
> > >     systemz    - SystemZ
> > >     thumb      - Thumb
> > >     thumbeb    - Thumb (big endian)
> > >     wasm32     - WebAssembly 32-bit
> > >     wasm64     - WebAssembly 64-bit
> > >     x86        - 32-bit X86: Pentium-Pro and above
> > >     x86-64     - 64-bit X86: EM64T and AMD64
> > >     xcore      - XCore
> > > $ clang --print-resource-dir
> > > /home/raymond/toolchain/clang-12.0.0/lib/clang/12.0.0
> > > $ ls /home/raymond/toolchain/clang-12.0.0/lib/clang/12.0.0/lib/linux/libclang_rt.builtins*
> > > /home/raymond/toolchain/clang-12.0.0/lib/clang/12.0.0/lib/linux/libclang_rt.builtins-aarch64.a
> > >  /home/raymond/toolchain/clang-12.0.0/lib/clang/12.0.0/lib/linux/libclang_rt.builtins-x86_64.a
> > > /home/raymond/toolchain/clang-12.0.0/lib/clang/12.0.0/lib/linux/libclang_rt.builtins-armhf.a
> >
> > Interesting. How did you install clang/LLVM? Did you get those builins
> > library from multiple release packages?
> >
> That is an old version 12.0.0 and I cannot recall well where I got it
> and how I installed it...
> But looking at the CI job here [1], the clang builds passed and that
> implies it got the libs properly.
> I will take a look at the newer clang releases if I can find some clues.
>
> [1] https://github.com/u-boot/u-boot/pull/773
>
> Regards,
> Raymond
>

After researching on the LLVM project releases, two facts are observed.
1. Since 15.0.0, the location layout of the 'libclang_rt.builtins'
libraries was changed from pattern [1] to pattern [2].
2. Since 20.0.0, no official prebuilt aarch64-linux-gnu targets have
been released.

Thus, if you agree, I can update my patch to support both patterns
when looking up the libraries, but for the case of release >= 20.0.0,
users have to crossbuild the targets by themselves (by default the
outputs are aligned to pattern [2]).
What are your thoughts?

Regards,
Raymond

[1] <INSTALLED_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] <INSTALLED_DIR>/lib/clang/<REV_MAJOR>/lib/<CROSSTOOL_NAME>/libclang_rt.builtins.a
E.g.
lib/clang/21/lib/aarch64-none-linux-gnu/libclang_rt.builtins.a
lib/clang/21/lib/x86_64-unknown-linux-gnu/libclang_rt.builtins.a


> > In the OP-TEE project we used to do that via a get_clang.sh script [1] but
> > it broke when llvm-project stopped releasing all the binary packages we needed
> > (aarch64-linux-gnu and armv7a-linux-gnueabihf). So we ended up importing the
> > __udivti3 implementation in source code instead [3]. I'm not saying this is
> > the way to go, but relying on libclang_rt.builtins assumes that a proper
> > toolchain is available, and that should at least be documented.
> >
> > [1] https://github.com/OP-TEE/build/blob/1b68e0bb8a00/get_clang.sh
> > [2] https://github.com/OP-TEE/build/commit/8dd39dc99f09
> > [3] https://github.com/OP-TEE/optee_os/commit/48952fd403d8
> >
> > Regards,
> > --
> > Jerome
> >
> > >
> > > Regards,
> > > Raymond
> > >
> > >> Thanks,
> > >> --
> > >> Jerome
> > >>
> > >>>  endif
> > >>>  endif
> > >>> +undefine MK_ARCH
> > >>>  PLATFORM_LIBS += $(PLATFORM_LIBGCC)
> > >>>
> > >>>  ifdef CONFIG_CC_COVERAGE


More information about the U-Boot mailing list