[libc-commits] [libc] [libc] Use global errno for baremetal (PR #98130)
Petr Hosek via libc-commits
libc-commits at lists.llvm.org
Tue Jul 9 02:20:14 PDT 2024
https://github.com/petrhosek updated https://github.com/llvm/llvm-project/pull/98130
>From 626ce673dd17128371eacfb0da1b1ec8d6637b11 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Tue, 9 Jul 2024 01:33:56 -0700
Subject: [PATCH] [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.
---
libc/include/errno.h.def | 3 ++-
libc/src/errno/libc_errno.cpp | 9 +++++++++
2 files changed, 11 insertions(+), 1 deletion(-)
diff --git a/libc/include/errno.h.def b/libc/include/errno.h.def
index 1f7120e63bfc9..91d59d02cc5d3 100644
--- a/libc/include/errno.h.def
+++ b/libc/include/errno.h.def
@@ -25,7 +25,8 @@
#include "llvm-libc-macros/generic-error-number-macros.h"
#endif
-#if defined(__AMDGPU__) || defined(__NVPTX__)
+#if defined(__AMDGPU__) || defined(__NVPTX__) || \
+ (defined(__ELF__) && !defined(__linux__) && !defined(__Fuchsia__))
extern int __llvmlibc_errno; // Not thread_local!
#else
#ifdef __cplusplus
diff --git a/libc/src/errno/libc_errno.cpp b/libc/src/errno/libc_errno.cpp
index bd1438c226143..85aff0dc0642b 100644
--- a/libc/src/errno/libc_errno.cpp
+++ b/libc/src/errno/libc_errno.cpp
@@ -23,6 +23,15 @@ LIBC_NAMESPACE::Errno::operator int() {
return __llvmlibc_errno.load(cpp::MemoryOrder::RELAXED);
}
+#elif defined(__ELF__) && !defined(__linux__) && !defined(__Fuchsia__)
+// This is the baremetal case which currently uses a global errno.
+extern "C" {
+int __llvmlibc_errno;
+}
+
+void LIBC_NAMESPACE::Errno::operator=(int a) { __llvmlibc_errno = a; }
+LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_errno; }
+
#elif !defined(LIBC_COPT_PUBLIC_PACKAGING)
// This mode is for unit testing. We just use our internal errno.
LIBC_THREAD_LOCAL int __llvmlibc_internal_errno;
More information about the libc-commits
mailing list