Skip to content

Commit 1b26bb0

Browse files
[libc] fix aarch64 GCC build (#97932)
This PR fix several build errors on aarch64 targets when building with gcc: - uninitialized values leading to `Werrors` - undefined builtin functions - glibc header pollution
1 parent 29b8b72 commit 1b26bb0

File tree

8 files changed

+26
-6
lines changed

8 files changed

+26
-6
lines changed

libc/hdr/math_macros.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515

1616
#else // Overlay mode
1717

18+
// GCC will include CXX headers when __cplusplus is defined. This behavior
19+
// can be suppressed by defining _GLIBCXX_INCLUDE_NEXT_C_HEADERS.
20+
#if defined(__GNUC__) && !defined(__clang__)
21+
#define _GLIBCXX_INCLUDE_NEXT_C_HEADERS
22+
#endif
1823
#include <math.h>
1924

2025
// Some older math.h header does not have FP_INT_* constants yet.

libc/src/__support/File/linux/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# TODO: migrate to proxy headers
12
add_object_library(
23
file
34
SRCS
@@ -8,6 +9,7 @@ add_object_library(
89
libc.include.fcntl
910
libc.include.stdio
1011
libc.include.sys_syscall
12+
libc.include.sys_stat
1113
libc.src.__support.CPP.new
1214
libc.src.__support.OSUtil.osutil
1315
libc.src.errno.errno

libc/src/__support/threads/sleep.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ LIBC_INLINE void sleep_briefly() {
2222
__builtin_amdgcn_s_sleep(2);
2323
#elif defined(LIBC_TARGET_ARCH_IS_X86)
2424
__builtin_ia32_pause();
25-
#elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
25+
#elif defined(LIBC_TARGET_ARCH_IS_AARCH64) && __has_builtin(__builtin_arm_isb)
2626
__builtin_arm_isb(0xf);
27+
#elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
28+
asm volatile("isb\n" ::: "memory");
2729
#else
2830
// Simply do nothing if sleeping isn't supported on this platform.
2931
#endif

libc/src/math/generic/cos.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ LLVM_LIBC_FUNCTION(double, cos, (double x)) {
6161

6262
DoubleDouble y;
6363
unsigned k;
64-
generic::LargeRangeReduction<NO_FMA> range_reduction_large;
64+
generic::LargeRangeReduction<NO_FMA> range_reduction_large{};
6565

6666
// |x| < 2^32 (with FMA) or |x| < 2^23 (w/o FMA)
6767
if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) {

libc/src/math/generic/sin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ LLVM_LIBC_FUNCTION(double, sin, (double x)) {
6262

6363
DoubleDouble y;
6464
unsigned k;
65-
generic::LargeRangeReduction<NO_FMA> range_reduction_large;
65+
generic::LargeRangeReduction<NO_FMA> range_reduction_large{};
6666

6767
// |x| < 2^32 (with FMA) or |x| < 2^23 (w/o FMA)
6868
if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) {

libc/src/math/generic/sincos.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ LLVM_LIBC_FUNCTION(void, sincos, (double x, double *sin_x, double *cos_x)) {
6363

6464
DoubleDouble y;
6565
unsigned k;
66-
generic::LargeRangeReduction<NO_FMA> range_reduction_large;
66+
generic::LargeRangeReduction<NO_FMA> range_reduction_large{};
6767

6868
// |x| < 2^32 (with FMA) or |x| < 2^23 (w/o FMA)
6969
if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) {

libc/src/search/hsearch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace LIBC_NAMESPACE {
1616
LLVM_LIBC_FUNCTION(ENTRY *, hsearch, (ENTRY item, ACTION action)) {
17-
ENTRY *result;
17+
ENTRY *result = nullptr;
1818
if (internal::global_hash_table == nullptr) {
1919
// If global_hash_table is null, we create a new hash table with a minimal
2020
// capacity. Such hashtable will be expanded as needed.

libc/startup/linux/aarch64/tls.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,18 @@ void cleanup_tls(uintptr_t addr, uintptr_t size) {
8080
}
8181

8282
bool set_thread_ptr(uintptr_t val) {
83-
__arm_wsr64("tpidr_el0", val);
83+
// The PR for __arm_wsr64 support in GCC was merged on Dec 6, 2023, and it is
84+
// not yet usable in 13.3.0
85+
// https://github.com/gcc-mirror/gcc/commit/fc42900d21abd5eacb7537c3c8ffc5278d510195
86+
#if __has_builtin(__builtin_arm_wsr64)
87+
__builtin_arm_wsr64("tpidr_el0", val);
88+
#elif __has_builtin(__builtin_aarch64_wsr)
89+
__builtin_aarch64_wsr("tpidr_el0", val);
90+
#elif defined(__GNUC__)
91+
asm volatile("msr tpidr_el0, %0" ::"r"(val));
92+
#else
93+
#error "Unsupported compiler"
94+
#endif
8495
return true;
8596
}
8697
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)