Skip to content

Commit 626ce67

Browse files
committed
[libc] Use global errno for baremetal
We want to avoid thread local variables on baremetal since the TLS support may not be universally available in those environments.
1 parent 93869df commit 626ce67

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

libc/include/errno.h.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
#include "llvm-libc-macros/generic-error-number-macros.h"
2626
#endif
2727

28-
#if defined(__AMDGPU__) || defined(__NVPTX__)
28+
#if defined(__AMDGPU__) || defined(__NVPTX__) || \
29+
(defined(__ELF__) && !defined(__linux__) && !defined(__Fuchsia__))
2930
extern int __llvmlibc_errno; // Not thread_local!
3031
#else
3132
#ifdef __cplusplus

libc/src/errno/libc_errno.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ LIBC_NAMESPACE::Errno::operator int() {
2323
return __llvmlibc_errno.load(cpp::MemoryOrder::RELAXED);
2424
}
2525

26+
#elif defined(__ELF__) && !defined(__linux__) && !defined(__Fuchsia__)
27+
// This is the baremetal case which currently uses a global errno.
28+
extern "C" {
29+
int __llvmlibc_errno;
30+
}
31+
32+
void LIBC_NAMESPACE::Errno::operator=(int a) { __llvmlibc_errno = a; }
33+
LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_errno; }
34+
2635
#elif !defined(LIBC_COPT_PUBLIC_PACKAGING)
2736
// This mode is for unit testing. We just use our internal errno.
2837
LIBC_THREAD_LOCAL int __llvmlibc_internal_errno;

0 commit comments

Comments
 (0)