RPi-Distro / repo

Issue tracking for the archive.raspberrypi.org repo
37 stars 1 forks source link

Clang compiler runtime library missing from llvm-toolchain-xx / libclang-common-xx-dev packages #326

Closed David-R-Walker closed 1 year ago

David-R-Walker commented 1 year ago

The Clang compiler runtime library "libclang_rt.builtins-armhf.a" is missing from libclang-common-XX-dev packages in the llvm-toolchain-XX source packages (where XX is 11, 13 or 15, which is as far as I've checked).

The Clang C/C++ compiler provides its own runtime (compiler-rt), which provides an alternative to libgcc, including a number of basic built-in functions that are not provided by libgcc under Clang. (e.g. overflow-safe math operations, which require use of Clang's compiler-rt).

The Raspberry Pi OS versions of these packages do not contain the basic "builtins" runtime necessary for Clang builds of C and C++ software, resulting in linker errors. This makes it impossible to build software that uses the Clang runtime library on Raspberry Pi without either building a custom Clang toolchain, or grabbing the appropriate library from the Debian version of these packages.

The missing file is /usr/lib/clang/XXX/lib/linux/libclang_rt.builtins-armhf.a where XXX is the Clang version.

Note that the equivalent Debian packages do contain this runtime library (and as a workaround can be copied into the appropriate /usr/lib/clang/XX/lib/linux/ directory -- although I'm not sure how safe this is).

Example:

#include <stdio.h>
#include <stdbool.h>

int main(void) {

        long long x = 42, y = 17, z = 0;
        bool overflow_p;

        overflow_p = __builtin_smulll_overflow(x, y, &z);
        if (overflow_p) {
                puts("Overflow!");
                return 1;
        }
        printf("%lld * %lld = %lld\n", x, y, z);
        return 0;

}

This should be compiled with clang -Wall --rtlib=compiler-rt test.c

Expected result: No errors or warnings. Executable "a.out" produced which displays 42 * 17 = 714 as output.

Actual result:

/usr/bin/arm-linux-gnueabihf-ld: cannot find /usr/lib/llvm-13/lib/clang/13.0.1/lib/linux/libclang_rt.builtins-armhf.a: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Note that under GCC the __builtin...() function in the example above would be provided by GCC, but for technical reasons isn't provided by libgcc itself so is not available under Clang with libgcc. In this scenario, the proper solution is to use Clang compiler runtime instead, which does provide this function, but the library is simply missing in Raspberry Pi OS, so it cannot be built by Clang.

I checked the following packages, and it was missing from all of them:

I suspect this is a problem with the build-options or environment variables used when building these binary packages for Raspberry Pi OS. I note there are debug/sanitizer runtimes in the lib/linux directory, so some runtimes are being built, but not the basic libclang_rt-builtins runtime lib.

XECDesign commented 1 year ago

We don't modify llvm-toolchain, so the issue should be reported here.

I had a quick look anyway and it seems like this is intentional:

  • Add -DCOMPILER_RT_BUILD_BUILTINS=OFF to disable parts of compiler-rt that are broken on lower arm architectures.

And here's the initial discussion raised by Raspbian's maintainer on Debian's mailing list about this, which led to the change.

So it looks like this is unlikely to change.