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

source code of libc/config/linux/app.h