2626#include " src/unistd/close.h"
2727#include " src/unistd/read.h"
2828
29- // getauxval will work either with or without atexit support.
30- // In order to detect if atexit is supported, we define a weak symbol.
31- extern " C" [[gnu::weak]] int atexit (void (*)(void ));
29+ // getauxval will work either with or without __cxa_atexit support.
30+ // In order to detect if __cxa_atexit is supported, we define a weak symbol.
31+ // We prefer __cxa_atexit as it is always defined as a C symbol whileas atexit
32+ // may not be created via objcopy yet.
33+ extern " C" [[gnu::weak]] int __cxa_atexit (void (*callback)(void *),
34+ void *payload, void *);
3235
3336namespace LIBC_NAMESPACE {
3437
@@ -49,19 +52,22 @@ static AuxEntry *auxv = nullptr;
4952struct AuxvMMapGuard {
5053 constexpr static size_t AUXV_MMAP_SIZE = sizeof (AuxEntry) * MAX_AUXV_ENTRIES;
5154 void *ptr;
52- AuxvMMapGuard (size_t size)
53- : ptr(mmap(nullptr , size, PROT_READ | PROT_WRITE, MAP_PRIVATE, -1 , 0 )) {}
55+ AuxvMMapGuard ()
56+ : ptr(mmap(nullptr , AUXV_MMAP_SIZE, PROT_READ | PROT_WRITE,
57+ MAP_PRIVATE | MAP_ANONYMOUS, -1 , 0 )) {}
5458 ~AuxvMMapGuard () {
5559 if (ptr != MAP_FAILED) {
5660 munmap (ptr, AUXV_MMAP_SIZE);
5761 }
5862 }
5963 void submit_to_global () {
6064 // atexit may fail, we do not set it to global in that case.
61- int ret = atexit ([]() {
62- munmap (auxv, AUXV_MMAP_SIZE);
63- auxv = nullptr ;
64- });
65+ int ret = __cxa_atexit (
66+ [](void *) {
67+ munmap (auxv, AUXV_MMAP_SIZE);
68+ auxv = nullptr ;
69+ },
70+ nullptr , nullptr );
6571
6672 if (ret != 0 )
6773 return ;
@@ -85,10 +91,10 @@ struct AuxvFdGuard {
8591
8692static void initialize_auxv_once (void ) {
8793 // if we cannot get atexit, we cannot register the cleanup function.
88- if (&atexit == nullptr )
94+ if (&__cxa_atexit == nullptr )
8995 return ;
9096
91- AuxvMMapGuard mmap_guard (AuxvMMapGuard::AUXV_MMAP_SIZE) ;
97+ AuxvMMapGuard mmap_guard;
9298 if (!mmap_guard.allocated ())
9399 return ;
94100 auto *ptr = reinterpret_cast <AuxEntry *>(mmap_guard.ptr );
0 commit comments