[libc-commits] [libc] [libc] Add LIBC_NAMESPACE_HIDDEN_DECL macro (PR #97109)
via libc-commits
libc-commits at lists.llvm.org
Fri Jun 28 13:30:23 PDT 2024
https://github.com/PiJoules created https://github.com/llvm/llvm-project/pull/97109
This defines to LIBC_NAMESPACE with
__attribute__((visibility("hidden"))) so all the symbols under it have hidden visibility. Individual namespace declarations can opt-in to use this. This is useful for baremetal where we can avoid GOT entries for globals.
>From 2245b2ff62c38ac5bb6c435b9e51b9fd826ebf4b Mon Sep 17 00:00:00 2001
From: Leonard Chan <leonardchan at google.com>
Date: Fri, 28 Jun 2024 13:27:20 -0700
Subject: [PATCH] [libc] Add LIBC_NAMESPACE_HIDDEN_DECL macro
This defines to LIBC_NAMESPACE with
__attribute__((visibility("hidden"))) so all the symbols under it have
hidden visibility. Individual namespace declarations can opt-in to use
this. This is useful for baremetal where we can avoid GOT entries for
globals.
---
libc/src/__support/macros/config.h | 4 ++++
libc/src/stdlib/rand_util.cpp | 5 +++--
libc/src/stdlib/rand_util.h | 5 +++--
3 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/libc/src/__support/macros/config.h b/libc/src/__support/macros/config.h
index 6390c7992325d..0380c8d6c8b89 100644
--- a/libc/src/__support/macros/config.h
+++ b/libc/src/__support/macros/config.h
@@ -27,4 +27,8 @@
#define LIBC_HAS_FEATURE(f) 0
#endif
+// Declare a LIBC_NAMESPACE with hidden visibility. This can be used on a
+// case-by-case basis where sets of symbols within this declaration are hidden.
+#define LIBC_NAMESPACE_HIDDEN_DECL [[gnu::visibility("hidden")]] LIBC_NAMESPACE
+
#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_CONFIG_H
diff --git a/libc/src/stdlib/rand_util.cpp b/libc/src/stdlib/rand_util.cpp
index ff3478db70003..1f3e03679ba19 100644
--- a/libc/src/stdlib/rand_util.cpp
+++ b/libc/src/stdlib/rand_util.cpp
@@ -9,11 +9,12 @@
#include "src/stdlib/rand_util.h"
#include "src/__support/CPP/atomic.h"
#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
-namespace LIBC_NAMESPACE {
+namespace LIBC_NAMESPACE_HIDDEN_DECL {
// C standard 7.10p2: If 'rand' is called before 'srand' it is to
// proceed as if the 'srand' function was called with a value of '1'.
cpp::Atomic<unsigned long> rand_next = 1;
-} // namespace LIBC_NAMESPACE
+} // namespace LIBC_NAMESPACE_HIDDEN_DECL
diff --git a/libc/src/stdlib/rand_util.h b/libc/src/stdlib/rand_util.h
index 5d7febf8248d8..2de48f035d784 100644
--- a/libc/src/stdlib/rand_util.h
+++ b/libc/src/stdlib/rand_util.h
@@ -11,14 +11,15 @@
#include "src/__support/CPP/atomic.h"
#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
-namespace LIBC_NAMESPACE {
+namespace LIBC_NAMESPACE_HIDDEN_DECL {
// The ISO C standard does not explicitly require thread-safe behavior for the
// generic `rand()` function. Some implementations expect it however, so we
// provide it here.
extern cpp::Atomic<unsigned long> rand_next;
-} // namespace LIBC_NAMESPACE
+} // namespace LIBC_NAMESPACE_HIDDEN_DECL
#endif // LLVM_LIBC_SRC_STDLIB_RAND_UTIL_H
More information about the libc-commits
mailing list