|
| 1 | +//===------------- Linux AUXV Header --------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_AUXV_H |
| 10 | +#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_AUXV_H |
| 11 | + |
| 12 | +#include "hdr/fcntl_macros.h" // For open flags |
| 13 | +#include "src/__support/OSUtil/syscall.h" |
| 14 | +#include "src/__support/common.h" |
| 15 | +#include "src/__support/threads/callonce.h" |
| 16 | + |
| 17 | +#include <linux/auxvec.h> // For AT_ macros |
| 18 | +#include <linux/mman.h> // For mmap flags |
| 19 | +#include <linux/prctl.h> // For prctl |
| 20 | +#include <sys/syscall.h> // For syscall numbers |
| 21 | + |
| 22 | +namespace LIBC_NAMESPACE_DECL { |
| 23 | + |
| 24 | +namespace auxv { |
| 25 | +struct Entry { |
| 26 | + unsigned long type; // Entry type |
| 27 | + unsigned long val; // Integer value |
| 28 | +}; |
| 29 | + |
| 30 | +class Vector { |
| 31 | + LIBC_INLINE_VAR static constexpr Entry END = {AT_NULL, AT_NULL}; |
| 32 | + LIBC_INLINE_VAR static const Entry *entries = &END; |
| 33 | + LIBC_INLINE_VAR static CallOnceFlag init_flag = callonce_impl::NOT_CALLED; |
| 34 | + LIBC_INLINE_VAR constexpr static size_t FALLBACK_AUXV_ENTRIES = 64; |
| 35 | + |
| 36 | + LIBC_INLINE static void fallback_initialize_unsync(); |
| 37 | + LIBC_INLINE static const Entry *get_entries() { |
| 38 | + if (LIBC_LIKELY(entries != &END)) |
| 39 | + return entries; |
| 40 | + callonce(&init_flag, fallback_initialize_unsync); |
| 41 | + return entries; |
| 42 | + } |
| 43 | + |
| 44 | +public: |
| 45 | + class Iterator { |
| 46 | + const Entry *current; |
| 47 | + |
| 48 | + public: |
| 49 | + LIBC_INLINE explicit Iterator(const Entry *entry) : current(entry) {} |
| 50 | + LIBC_INLINE Iterator &operator++() { |
| 51 | + ++current; |
| 52 | + return *this; |
| 53 | + } |
| 54 | + LIBC_INLINE const Entry &operator*() const { return *current; } |
| 55 | + LIBC_INLINE bool operator!=(const Iterator &other) const { |
| 56 | + return current->type != other.current->type; |
| 57 | + } |
| 58 | + LIBC_INLINE bool operator==(const Iterator &other) const { |
| 59 | + return current->type == other.current->type; |
| 60 | + } |
| 61 | + }; |
| 62 | + using iterator = Iterator; |
| 63 | + LIBC_INLINE static Iterator begin() { return Iterator(get_entries()); } |
| 64 | + LIBC_INLINE static Iterator end() { return Iterator(&END); } |
| 65 | + LIBC_INLINE static void initialize_unsafe(const Entry *auxv); |
| 66 | +}; |
| 67 | + |
| 68 | +// Initializes the auxv entries. |
| 69 | +// This function is intended to be called once inside crt0. |
| 70 | +LIBC_INLINE void Vector::initialize_unsafe(const Entry *auxv) { |
| 71 | + init_flag = callonce_impl::FINISH; |
| 72 | + entries = auxv; |
| 73 | +} |
| 74 | + |
| 75 | +// When CRT0 does not setup the global array, this function is called. |
| 76 | +// As its name suggests, this function is not thread-safe and should be |
| 77 | +// backed by a callonce guard. |
| 78 | +// This initialize routine will do a mmap to allocate a memory region. |
| 79 | +// Since auxv tends to live throughout the program lifetime, we do not |
| 80 | +// munmap it. |
| 81 | +[[gnu::cold]] |
| 82 | +LIBC_INLINE void Vector::fallback_initialize_unsync() { |
| 83 | + constexpr size_t AUXV_MMAP_SIZE = FALLBACK_AUXV_ENTRIES * sizeof(Entry); |
| 84 | + long mmap_ret = syscall_impl<long>(SYS_mmap, nullptr, AUXV_MMAP_SIZE, |
| 85 | + PROT_READ | PROT_WRITE, |
| 86 | + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 87 | + // We do not proceed if mmap fails. |
| 88 | + if (mmap_ret <= 0) |
| 89 | + return; |
| 90 | + |
| 91 | + // Initialize the auxv array with AT_NULL entries. |
| 92 | + Entry *vector = reinterpret_cast<Entry *>(mmap_ret); |
| 93 | + for (size_t i = 0; i < FALLBACK_AUXV_ENTRIES; ++i) { |
| 94 | + vector[i].type = AT_NULL; |
| 95 | + vector[i].val = AT_NULL; |
| 96 | + } |
| 97 | + size_t avaiable_size = AUXV_MMAP_SIZE - sizeof(Entry); |
| 98 | + |
| 99 | + // Attempt 1: use PRCTL to get the auxv. |
| 100 | + // We guarantee that the vector is always padded with AT_NULL entries. |
| 101 | + long prctl_ret = syscall_impl<long>(SYS_prctl, PR_GET_AUXV, |
| 102 | + reinterpret_cast<unsigned long>(vector), |
| 103 | + avaiable_size, 0, 0); |
| 104 | + if (prctl_ret >= 0) { |
| 105 | + entries = vector; |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + // Attempt 2: read /proc/self/auxv. |
| 110 | +#ifdef SYS_openat |
| 111 | + int fd = syscall_impl<int>(SYS_openat, AT_FDCWD, "/proc/self/auxv", |
| 112 | + O_RDONLY | O_CLOEXEC); |
| 113 | +#else |
| 114 | + int fd = syscall_impl<int>(SYS_open, "/proc/self/auxv", O_RDONLY | O_CLOEXEC); |
| 115 | +#endif |
| 116 | + if (fd < 0) { |
| 117 | + syscall_impl<long>(SYS_munmap, vector, AUXV_MMAP_SIZE); |
| 118 | + return; |
| 119 | + } |
| 120 | + uint8_t *cursor = reinterpret_cast<uint8_t *>(vector); |
| 121 | + bool has_error = false; |
| 122 | + while (avaiable_size != 0) { |
| 123 | + long bytes_read = syscall_impl<long>(SYS_read, fd, cursor, avaiable_size); |
| 124 | + if (bytes_read <= 0) { |
| 125 | + if (bytes_read == -EINTR) |
| 126 | + continue; |
| 127 | + has_error = bytes_read < 0; |
| 128 | + break; |
| 129 | + } |
| 130 | + avaiable_size -= bytes_read; |
| 131 | + cursor += bytes_read; |
| 132 | + } |
| 133 | + syscall_impl<long>(SYS_close, fd); |
| 134 | + if (has_error) { |
| 135 | + syscall_impl<long>(SYS_munmap, vector, AUXV_MMAP_SIZE); |
| 136 | + return; |
| 137 | + } |
| 138 | + entries = vector; |
| 139 | +} |
| 140 | + |
| 141 | +LIBC_INLINE cpp::optional<unsigned long> get(unsigned long type) { |
| 142 | + Vector auxvec; |
| 143 | + for (const auto &entry : auxvec) |
| 144 | + if (entry.type == type) |
| 145 | + return entry.val; |
| 146 | + return cpp::nullopt; |
| 147 | +} |
| 148 | +} // namespace auxv |
| 149 | +} // namespace LIBC_NAMESPACE_DECL |
| 150 | + |
| 151 | +#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_AUXV_H |
0 commit comments