Using compiler-rt

My understanding is that compiler-rt is a library consisting of low level functions that the compiler may generate calls to. Could someone give me an example of C or C++ code that would generate a binary that requires compiler-rt?

My understanding is that compiler-rt is a library consisting of low level
functions that the compiler may generate calls to. Could someone give me
an example of C or C++ code that would generate a binary that requires
compiler-rt?

i686-linux-gnu:

int f(long long ll, long long lj) { return ll / lj; }

That was one of the easiest that I could think of off the top of my head.

Hi Daniel,

My understanding is that compiler-rt is a library consisting of low level
functions that the compiler may generate calls to. Could someone give me an
example of C or C++ code that would generate a binary that requires
compiler-rt?

The bulk of the code is a software implementation of floating-point
operations for CPUs (or kernels!) that don't have access to the real
instructions. For example on primitive ARM CPUs, something as simple
as "float foo(double in) { return in; }" would generate a libcall.

On a modern x86_64 CPU you have to go a lot further. At a glance, the
only candidates I can see are __clear_cache calls (to invalidate the
instruction-cache, likely a NOP even if you do get Clang to produce
it) and __gcc_personality_v0 (C++ exception-related faff, pretty much
any nontrivial "catch" block should use it).

The repository also provides support routines needed for the various
-fsanitize=XYZ options. But that's only generated if requested (both
by clang and when building the compiler-rt library).

The situation is slightly different to the GCC world, which rolls all
of the C++ exception support functions into its equivalent library
(libgcc). So there you'd also expect to find things like
_Unwind_Resume which the LLVM world tends to put into a separate
library like libunwind or libc++abi.

Cheers.

Tim.

I am compiling for bare metal Coretex A8 (Beaglebone Black). I can probably figure out how to disable the floating point with a compiler flag (-fpu ?).

Probably -msoft-float if you want to exercise compiler-rt with
floating-point operations, though it looks like -mfpu=none works too.

Cheers.

Tim.

Minor correction: __gcc_personality_v0 is the C personality function, which is used to implement __attribute__((cleanup)). It doesn't provide a mechanism for handling catches, only cleanups.

David