Warning: This file is not a C or C++ file. It does not have highlighting.
| 1 | //===-- Classes to capture properites of linux applications -----*- 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_CONFIG_LINUX_APP_H |
| 10 | #define LLVM_LIBC_CONFIG_LINUX_APP_H |
| 11 | |
| 12 | #include "src/__support/macros/config.h" |
| 13 | #include "src/__support/macros/properties/architectures.h" |
| 14 | |
| 15 | #include <stdint.h> |
| 16 | |
| 17 | namespace LIBC_NAMESPACE_DECL { |
| 18 | |
| 19 | // Data structure to capture properties of the linux/ELF TLS image. |
| 20 | struct TLSImage { |
| 21 | // The load address of the TLS. |
| 22 | uintptr_t address; |
| 23 | |
| 24 | // The byte size of the TLS image consisting of both initialized and |
| 25 | // uninitialized memory. In ELF executables, it is size of .tdata + size of |
| 26 | // .tbss. Put in another way, it is the memsz field of the PT_TLS header. |
| 27 | uintptr_t size; |
| 28 | |
| 29 | // The byte size of initialized memory in the TLS image. In ELF exectubles, |
| 30 | // this is the size of .tdata. Put in another way, it is the filesz of the |
| 31 | // PT_TLS header. |
| 32 | uintptr_t init_size; |
| 33 | |
| 34 | // The alignment of the TLS layout. It assumed that the alignment |
| 35 | // value is a power of 2. |
| 36 | uintptr_t align; |
| 37 | }; |
| 38 | |
| 39 | // Linux manpage on `proc(5)` says that the aux vector is an array of |
| 40 | // unsigned long pairs. |
| 41 | // (see: https://man7.org/linux/man-pages/man5/proc.5.html) |
| 42 | using AuxEntryType = unsigned long; |
| 43 | // Using the naming convention from `proc(5)`. |
| 44 | // TODO: Would be nice to use the aux entry structure from elf.h when available. |
| 45 | struct AuxEntry { |
| 46 | AuxEntryType id; |
| 47 | AuxEntryType value; |
| 48 | }; |
| 49 | |
| 50 | struct Args { |
| 51 | uintptr_t argc; |
| 52 | |
| 53 | // A flexible length array would be more suitable here, but C++ doesn't have |
| 54 | // flexible arrays: P1039 proposes to fix this. So, for now we just fake it. |
| 55 | // Even if argc is zero, "argv[argc] shall be a null pointer" |
| 56 | // (ISO C 5.1.2.2.1) so one is fine. Also, length of 1 is not really wrong as |
| 57 | // |argc| is guaranteed to be atleast 1, and there is an 8-byte null entry at |
| 58 | // the end of the argv array. |
| 59 | uintptr_t argv[1]; |
| 60 | }; |
| 61 | |
| 62 | // Data structure which captures properties of a linux application. |
| 63 | struct AppProperties { |
| 64 | // Page size used for the application. |
| 65 | uintptr_t page_size; |
| 66 | |
| 67 | Args *args; |
| 68 | |
| 69 | // The properties of an application's TLS image. |
| 70 | TLSImage tls; |
| 71 | |
| 72 | // Environment data. |
| 73 | uintptr_t *env_ptr; |
| 74 | |
| 75 | // Auxiliary vector data. |
| 76 | AuxEntry *auxv_ptr; |
| 77 | }; |
| 78 | |
| 79 | [[gnu::weak]] extern AppProperties app; |
| 80 | |
| 81 | // The descriptor of a thread's TLS area. |
| 82 | struct TLSDescriptor { |
| 83 | // The size of the TLS area. |
| 84 | uintptr_t size = 0; |
| 85 | |
| 86 | // The address of the TLS area. This address can be passed to cleanup |
| 87 | // functions like munmap. |
| 88 | uintptr_t addr = 0; |
| 89 | |
| 90 | // The value the thread pointer register should be initialized to. |
| 91 | // Note that, dependending the target architecture ABI, it can be the |
| 92 | // same as |addr| or something else. |
| 93 | uintptr_t tp = 0; |
| 94 | |
| 95 | constexpr TLSDescriptor() = default; |
| 96 | }; |
| 97 | |
| 98 | // Create and initialize the TLS area for the current thread. Should not |
| 99 | // be called before app.tls has been initialized. |
| 100 | void init_tls(TLSDescriptor &tls); |
| 101 | |
| 102 | // Cleanup the TLS area as described in |tls_descriptor|. |
| 103 | void cleanup_tls(uintptr_t tls_addr, uintptr_t tls_size); |
| 104 | |
| 105 | // Set the thread pointer for the current thread. |
| 106 | bool set_thread_ptr(uintptr_t val); |
| 107 | |
| 108 | } // namespace LIBC_NAMESPACE_DECL |
| 109 | |
| 110 | #endif // LLVM_LIBC_CONFIG_LINUX_APP_H |
| 111 |
Warning: This file is not a C or C++ file. It does not have highlighting.
