Skip to content

[libc] Set default visibility to 'hidden' and make entrypoints default #97123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function(_get_common_compile_options output_var flags)

if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
list(APPEND compile_options "-fpie")
list(APPEND compile_options "-fvisibility=hidden")

if(LLVM_LIBC_FULL_BUILD)
list(APPEND compile_options "-DLIBC_FULL_BUILD")
Expand Down Expand Up @@ -78,6 +79,9 @@ function(_get_common_compile_options output_var flags)
if (LIBC_CONF_ENABLE_STACK_PROTECTOR)
list(APPEND compile_options "-fstack-protector-strong")
endif()
if(LIBC_CONF_VISIBILITY)
list(APPEND compile_options "-DLIBC_VISIBILITY=${LIBC_CONF_VISIBILITY}")
endif()
list(APPEND compile_options "-Wall")
list(APPEND compile_options "-Wextra")
# -DLIBC_WNO_ERROR=ON if you can't build cleanly with -Werror.
Expand All @@ -102,7 +106,6 @@ function(_get_common_compile_options output_var flags)
endif()
if (LIBC_TARGET_OS_IS_GPU)
list(APPEND compile_options "-nogpulib")
list(APPEND compile_options "-fvisibility=hidden")
list(APPEND compile_options "-fconvergent-functions")
list(APPEND compile_options "-flto")
list(APPEND compile_options "-Wno-multi-gpu")
Expand Down
4 changes: 4 additions & 0 deletions libc/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
}
},
"codegen": {
"LIBC_CONF_VISIBILITY": {
"value": "default",
"doc": "Visibility to use on all exported C library symbols."
},
"LIBC_CONF_KEEP_FRAME_POINTER": {
"value": true,
"doc": "Keep frame pointer in functions for better debugging experience."
Expand Down
5 changes: 5 additions & 0 deletions libc/config/gpu/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
"LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE": {
"value": false
}
},
"codegen": {
"LIBC_CONF_VISIBILITY": {
"value": "hidden"
}
}
}
1 change: 1 addition & 0 deletions libc/docs/configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ to learn about the defaults for your platform and target.
* **"codegen" options**
- ``LIBC_CONF_ENABLE_STRONG_STACK_PROTECTOR``: Enable -fstack-protector-strong to defend against stack smashing attack.
- ``LIBC_CONF_KEEP_FRAME_POINTER``: Keep frame pointer in functions for better debugging experience.
- ``LIBC_CONF_VISIBILITY``: Visibility to use on all exported C library symbols.
* **"malloc" options**
- ``LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE``: Default size for the constinit freelist buffer used for the freelist malloc implementation (default 1o 1GB).
* **"printf" options**
Expand Down
3 changes: 2 additions & 1 deletion libc/docs/dev/entrypoints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ Entrypoints in LLVM libc

A public function or a global variable provided by LLVM-libc is called an
entrypoint. The notion of entrypoints is ingrained in LLVM-libc's
source layout, build system and source code.
source layout, build system and source code. Entrypoints will be given the
visibility defined in the ``LIBC_CONF_VISIBILITY`` configuration.
9 changes: 5 additions & 4 deletions libc/src/__support/File/linux/stderr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
//
//===----------------------------------------------------------------------===//

#include "file.h"
#include "src/__support/File/linux/file.h"
#include "src/__support/common.h"

#include <stdio.h>

namespace LIBC_NAMESPACE {
Expand All @@ -18,6 +20,5 @@ File *stderr = &StdErr;

} // namespace LIBC_NAMESPACE

extern "C" {
FILE *stderr = reinterpret_cast<FILE *>(&LIBC_NAMESPACE::StdErr);
}
LLVM_LIBC_GLOBAL(FILE *,
stderr) = reinterpret_cast<FILE *>(&LIBC_NAMESPACE::StdErr);
9 changes: 5 additions & 4 deletions libc/src/__support/File/linux/stdin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
//
//===----------------------------------------------------------------------===//

#include "file.h"
#include "src/__support/File/linux/file.h"
#include "src/__support/common.h"

#include <stdio.h>

namespace LIBC_NAMESPACE {
Expand All @@ -19,6 +21,5 @@ File *stdin = &StdIn;

} // namespace LIBC_NAMESPACE

extern "C" {
FILE *stdin = reinterpret_cast<FILE *>(&LIBC_NAMESPACE::StdIn);
} // extern "C"
LLVM_LIBC_GLOBAL(FILE *,
stdin) = reinterpret_cast<FILE *>(&LIBC_NAMESPACE::StdIn);
9 changes: 5 additions & 4 deletions libc/src/__support/File/linux/stdout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
//
//===----------------------------------------------------------------------===//

#include "file.h"
#include "src/__support/File/linux/file.h"
#include "src/__support/common.h"

#include <stdio.h>

namespace LIBC_NAMESPACE {
Expand All @@ -19,6 +21,5 @@ File *stdout = &StdOut;

} // namespace LIBC_NAMESPACE

extern "C" {
FILE *stdout = reinterpret_cast<FILE *>(&LIBC_NAMESPACE::StdOut);
} // extern "C"
LLVM_LIBC_GLOBAL(FILE *,
stdout) = reinterpret_cast<FILE *>(&LIBC_NAMESPACE::StdOut);
21 changes: 17 additions & 4 deletions libc/src/__support/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,26 @@
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/properties/architectures.h"

#define __LIBC_MACRO_TO_STRING(str) #str
#define LIBC_MACRO_TO_STRING(str) __LIBC_MACRO_TO_STRING(str)

#ifndef LLVM_LIBC_FUNCTION_ATTR
#define LLVM_LIBC_FUNCTION_ATTR
#endif

#ifndef LIBC_VISIBILITY
#define LIBC_VISIBILITY default
#endif

#ifndef LLVM_LIBC_VISIBILITY
#define LLVM_LIBC_VISIBILITY \
[[gnu::visibility(LIBC_MACRO_TO_STRING(LIBC_VISIBILITY))]]
#endif

// MacOS needs to be excluded because it does not support aliasing.
#if defined(LIBC_COPT_PUBLIC_PACKAGING) && (!defined(__APPLE__))
#define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) \
LLVM_LIBC_FUNCTION_ATTR decltype(LIBC_NAMESPACE::name) \
LLVM_LIBC_FUNCTION_ATTR LLVM_LIBC_VISIBILITY decltype(LIBC_NAMESPACE::name) \
__##name##_impl__ __asm__(#name); \
decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]]; \
type __##name##_impl__ arglist
Expand All @@ -35,6 +47,10 @@
#define LLVM_LIBC_FUNCTION(type, name, arglist) \
LLVM_LIBC_FUNCTION_IMPL(type, name, arglist)

// Defines a global variable meant to be exported by the C library.
#define LLVM_LIBC_GLOBAL(type, name) \
LLVM_LIBC_VISIBILITY type name __asm__(#name)

namespace LIBC_NAMESPACE {
namespace internal {
LIBC_INLINE constexpr bool same_string(char const *lhs, char const *rhs) {
Expand All @@ -46,9 +62,6 @@ LIBC_INLINE constexpr bool same_string(char const *lhs, char const *rhs) {
} // namespace internal
} // namespace LIBC_NAMESPACE

#define __LIBC_MACRO_TO_STRING(str) #str
#define LIBC_MACRO_TO_STRING(str) __LIBC_MACRO_TO_STRING(str)

// LLVM_LIBC_IS_DEFINED checks whether a particular macro is defined.
// Usage: constexpr bool kUseAvx = LLVM_LIBC_IS_DEFINED(__AVX__);
//
Expand Down
9 changes: 3 additions & 6 deletions libc/src/errno/libc_errno.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

#include "libc_errno.h"
#include "src/__support/CPP/atomic.h"
#include "src/__support/common.h"

#ifdef LIBC_TARGET_ARCH_IS_GPU
// LIBC_THREAD_LOCAL on GPU currently does nothing. So essentially this is just
// a global errno for gpu to use for now.
extern "C" {
LIBC_THREAD_LOCAL LIBC_NAMESPACE::cpp::Atomic<int> __llvmlibc_errno;
}
LLVM_LIBC_GLOBAL(LIBC_NAMESPACE::cpp::Atomic<int>, __llvmlibc_errno);

void LIBC_NAMESPACE::Errno::operator=(int a) {
__llvmlibc_errno.store(a, cpp::MemoryOrder::RELAXED);
Expand All @@ -33,9 +32,7 @@ LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_internal_errno; }
#elif defined(LIBC_FULL_BUILD)
// This mode is for public libc archive, hermetic, and integration tests.
// In full build mode, we provide the errno storage ourselves.
extern "C" {
LIBC_THREAD_LOCAL int __llvmlibc_errno;
}
LLVM_LIBC_GLOBAL(LIBC_THREAD_LOCAL int, __llvmlibc_errno);

void LIBC_NAMESPACE::Errno::operator=(int a) { __llvmlibc_errno = a; }
LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_errno; }
Expand Down
3 changes: 2 additions & 1 deletion libc/src/stdio/generic/stderr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
//===----------------------------------------------------------------------===//

#include "src/__support/File/file.h"
#include "src/__support/common.h"

#include <stdio.h>

extern "C" FILE *stderr;
LLVM_LIBC_GLOBAL(FILE *, stderr);
3 changes: 2 additions & 1 deletion libc/src/stdio/generic/stdin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
//===----------------------------------------------------------------------===//

#include "src/__support/File/file.h"
#include "src/__support/common.h"

#include <stdio.h>

extern "C" FILE *stdin;
LLVM_LIBC_GLOBAL(FILE *, stdin);
3 changes: 2 additions & 1 deletion libc/src/stdio/generic/stdout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
//===----------------------------------------------------------------------===//

#include "src/__support/File/file.h"
#include "src/__support/common.h"

#include <stdio.h>

extern "C" FILE *stdout;
LLVM_LIBC_GLOBAL(FILE *, stdout);
5 changes: 4 additions & 1 deletion libc/src/stdio/gpu/stderr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
//
//===----------------------------------------------------------------------===//

#include "src/__support/common.h"

#include <stdio.h>

namespace LIBC_NAMESPACE {
static struct {
} stub;
FILE *stderr = reinterpret_cast<FILE *>(&stub);
} // namespace LIBC_NAMESPACE
extern "C" FILE *stderr = reinterpret_cast<FILE *>(&LIBC_NAMESPACE::stub);
LLVM_LIBC_GLOBAL(FILE *,
stderr) = reinterpret_cast<FILE *>(&LIBC_NAMESPACE::stub);
5 changes: 4 additions & 1 deletion libc/src/stdio/gpu/stdin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
//
//===----------------------------------------------------------------------===//

#include "src/__support/common.h"

#include <stdio.h>

namespace LIBC_NAMESPACE {
static struct {
} stub;
FILE *stdin = reinterpret_cast<FILE *>(&stub);
} // namespace LIBC_NAMESPACE
extern "C" FILE *stdin = reinterpret_cast<FILE *>(&LIBC_NAMESPACE::stub);
LLVM_LIBC_GLOBAL(FILE *,
stdin) = reinterpret_cast<FILE *>(&LIBC_NAMESPACE::stub);
5 changes: 4 additions & 1 deletion libc/src/stdio/gpu/stdout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
//
//===----------------------------------------------------------------------===//

#include "src/__support/common.h"

#include <stdio.h>

namespace LIBC_NAMESPACE {
static struct {
} stub;
FILE *stdout = reinterpret_cast<FILE *>(&stub);
} // namespace LIBC_NAMESPACE
extern "C" FILE *stdout = reinterpret_cast<FILE *>(&LIBC_NAMESPACE::stub);
LLVM_LIBC_GLOBAL(FILE *,
stdout) = reinterpret_cast<FILE *>(&LIBC_NAMESPACE::stub);
9 changes: 3 additions & 6 deletions libc/src/stdlib/atexit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@

namespace LIBC_NAMESPACE {

extern "C" {

int __cxa_atexit(AtExitCallback *callback, void *payload, void *) {
LLVM_LIBC_FUNCTION(int, __cxa_atexit,
(AtExitCallback * callback, void *payload, void *)) {
return add_atexit_unit(atexit_callbacks, {callback, payload});
}

void __cxa_finalize(void *dso) {
LLVM_LIBC_FUNCTION(void, __cxa_finalize, (void *dso)) {
if (!dso)
call_exit_callbacks(atexit_callbacks);
}

} // extern "C"

LLVM_LIBC_FUNCTION(int, atexit, (__atexithandler_t callback)) {
return add_atexit_unit(
atexit_callbacks,
Expand Down
6 changes: 6 additions & 0 deletions libc/src/stdlib/atexit.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
#define LLVM_LIBC_SRC_STDLIB_ATEXIT_H

#include "hdr/types/atexithandler_t.h"
#include "src/stdlib/exit_handler.h"

namespace LIBC_NAMESPACE {

int __cxa_atexit(AtExitCallback *callback, void *payload, void *);

void __cxa_finalize(void *dso);

int atexit(__atexithandler_t);

} // namespace LIBC_NAMESPACE
Expand Down
3 changes: 1 addition & 2 deletions libc/src/stdlib/exit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#include "src/stdlib/exit.h"
#include "src/__support/OSUtil/exit.h"
#include "src/__support/common.h"

extern "C" void __cxa_finalize(void *);
#include "src/stdlib/atexit.h"

namespace LIBC_NAMESPACE {

Expand Down
7 changes: 4 additions & 3 deletions libc/src/unistd/environ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
//
//===----------------------------------------------------------------------===//

#include "src/unistd/environ.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

// This is initialized to the correct value by the statup code.
extern "C" {
char **environ = nullptr;
}
LLVM_LIBC_GLOBAL(char **, environ) = nullptr;

} // namespace LIBC_NAMESPACE
10 changes: 4 additions & 6 deletions libc/src/unistd/getopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,10 @@ int getopt_r(int argc, char *const argv[], const char *optstring,

namespace impl {

extern "C" {
char *optarg = nullptr;
int optind = 1;
int optopt = 0;
int opterr = 0;
}
LLVM_LIBC_GLOBAL(char, *optarg) = nullptr;
LLVM_LIBC_GLOBAL(int, optind) = 1;
LLVM_LIBC_GLOBAL(int, optopt) = 0;
LLVM_LIBC_GLOBAL(int, opterr) = 0;

static unsigned optpos;

Expand Down
8 changes: 8 additions & 0 deletions libc/test/IntegrationTest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ void *memcpy(void *__restrict, const void *__restrict, size_t);
void *memmove(void *dst, const void *src, size_t count);
void *memset(void *ptr, int value, size_t count);
int atexit(void (*func)(void));
int __cxa_atexit(void (*func)(void *), void *payload, void *dso);
void __cxa_finalize(void *dso);

} // namespace LIBC_NAMESPACE

Expand All @@ -53,6 +55,12 @@ void *memset(void *ptr, int value, size_t count) {
// This is needed if the test was compiled with '-fno-use-cxa-atexit'.
int atexit(void (*func)(void)) { return LIBC_NAMESPACE::atexit(func); }

int __cxa_atexit(void (*callback)(void *), void *payload, void *dso) {
return LIBC_NAMESPACE::__cxa_atexit(callback, payload, dso);
}

void __cxa_finalize(void *dso) { LIBC_NAMESPACE::__cxa_finalize(dso); }

} // extern "C"

// Integration tests cannot use the SCUDO standalone allocator as SCUDO pulls
Expand Down
Loading
Loading