[compiler-rt] 244601f - [builtins] Allow compiling the builtins without libc headers

Alex Richardson via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 15 01:12:37 PDT 2021


Author: Alex Richardson
Date: 2021-06-15T09:08:59+01:00
New Revision: 244601f4720d9cda6e81ea1908f3ce905a4bcb0e

URL: https://github.com/llvm/llvm-project/commit/244601f4720d9cda6e81ea1908f3ce905a4bcb0e
DIFF: https://github.com/llvm/llvm-project/commit/244601f4720d9cda6e81ea1908f3ce905a4bcb0e.diff

LOG: [builtins] Allow compiling the builtins without libc headers

When compiled with -ffreestanding, we should not assume that headers
declaring functions such as abort() are available. While the compiler may
still emit calls to those functions [1], we should not require the headers
to build compiler-rt since that can result in a cyclic dependency graph:
The compiler-rt functions might be required to build libc.so, but the libc
headers such as stdlib.h might only be available once libc has been built.

[1] From https://gcc.gnu.org/onlinedocs/gcc/Standards.html:
GCC requires the freestanding environment provide memcpy, memmove,
memset and memcmp. Finally, if __builtin_trap is used, and the target
does not implement the trap pattern, then GCC emits a call to abort.

Reviewed By: phosek

Differential Revision: https://reviews.llvm.org/D103876

Added: 
    

Modified: 
    compiler-rt/lib/builtins/atomic.c
    compiler-rt/lib/builtins/clear_cache.c
    compiler-rt/lib/builtins/int_util.c

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/builtins/atomic.c b/compiler-rt/lib/builtins/atomic.c
index 2d109d2d1bca2..64bf72dfa345c 100644
--- a/compiler-rt/lib/builtins/atomic.c
+++ b/compiler-rt/lib/builtins/atomic.c
@@ -24,11 +24,15 @@
 //===----------------------------------------------------------------------===//
 
 #include <stdbool.h>
+#include <stddef.h>
 #include <stdint.h>
-#include <string.h>
 
 #include "assembly.h"
 
+// We use __builtin_mem* here to avoid dependencies on libc-provided headers.
+#define memcpy __builtin_memcpy
+#define memcmp __builtin_memcmp
+
 // Clang objects if you redefine a builtin.  This little hack allows us to
 // define a function with the same name as an intrinsic.
 #pragma redefine_extname __atomic_load_c SYMBOL_NAME(__atomic_load)

diff  --git a/compiler-rt/lib/builtins/clear_cache.c b/compiler-rt/lib/builtins/clear_cache.c
index 0284cb699f74a..f0a84c4c59546 100644
--- a/compiler-rt/lib/builtins/clear_cache.c
+++ b/compiler-rt/lib/builtins/clear_cache.c
@@ -7,7 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "int_lib.h"
+#if defined(__linux__)
 #include <assert.h>
+#endif
 #include <stddef.h>
 
 #if __APPLE__

diff  --git a/compiler-rt/lib/builtins/int_util.c b/compiler-rt/lib/builtins/int_util.c
index 226a6e93440d9..e70a6fa1fcca5 100644
--- a/compiler-rt/lib/builtins/int_util.c
+++ b/compiler-rt/lib/builtins/int_util.c
@@ -33,35 +33,25 @@ void __compilerrt_abort_impl(const char *file, int line, const char *function) {
 NORETURN extern void __assert_rtn(const char *func, const char *file, int line,
                                   const char *message);
 
-#ifndef _WIN32
 __attribute__((weak))
 __attribute__((visibility("hidden")))
-#endif
 void __compilerrt_abort_impl(const char *file, int line, const char *function) {
   __assert_rtn(function, file, line, "libcompiler_rt abort");
 }
 
-#elif __Fuchsia__
+#else
 
 #ifndef _WIN32
 __attribute__((weak))
 __attribute__((visibility("hidden")))
 #endif
 void __compilerrt_abort_impl(const char *file, int line, const char *function) {
+#if !__STDC_HOSTED__
+  // Avoid depending on libc when compiling with -ffreestanding.
   __builtin_trap();
-}
-
 #else
-
-// Get the system definition of abort()
-#include <stdlib.h>
-
-#ifndef _WIN32
-__attribute__((weak))
-__attribute__((visibility("hidden")))
+  __builtin_abort();
 #endif
-void __compilerrt_abort_impl(const char *file, int line, const char *function) {
-  abort();
 }
 
 #endif


        


More information about the llvm-commits mailing list