221
|
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
|
252
|
12 #include "src/__support/macros/properties/architectures.h"
|
236
|
13
|
221
|
14 #include <stdint.h>
|
|
15
|
|
16 namespace __llvm_libc {
|
|
17
|
236
|
18 // Data structure to capture properties of the linux/ELF TLS image.
|
|
19 struct TLSImage {
|
221
|
20 // The load address of the TLS.
|
|
21 uintptr_t address;
|
|
22
|
236
|
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.
|
221
|
26 uintptr_t size;
|
|
27
|
236
|
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
|
221
|
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
|
252
|
38 #if defined(LIBC_TARGET_ARCH_IS_X86_64) || \
|
|
39 defined(LIBC_TARGET_ARCH_IS_AARCH64) || \
|
|
40 defined(LIBC_TARGET_ARCH_IS_RISCV64)
|
236
|
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.
|
|
45 typedef uint64_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.
|
|
49 typedef uint64_t ArgVEntryType;
|
|
50 #else
|
|
51 #error "argc and argv types are not defined for the target platform."
|
|
52 #endif
|
|
53
|
|
54 struct Args {
|
|
55 ArgcType argc;
|
|
56
|
|
57 // A flexible length array would be more suitable here, but C++ doesn't have
|
|
58 // flexible arrays: P1039 proposes to fix this. So, for now we just fake it.
|
|
59 // Even if argc is zero, "argv[argc] shall be a null pointer"
|
|
60 // (ISO C 5.1.2.2.1) so one is fine. Also, length of 1 is not really wrong as
|
|
61 // |argc| is guaranteed to be atleast 1, and there is an 8-byte null entry at
|
|
62 // the end of the argv array.
|
|
63 ArgVEntryType argv[1];
|
|
64 };
|
|
65
|
221
|
66 // Data structure which captures properties of a linux application.
|
|
67 struct AppProperties {
|
|
68 // Page size used for the application.
|
|
69 uintptr_t pageSize;
|
|
70
|
236
|
71 Args *args;
|
|
72
|
|
73 // The properties of an application's TLS image.
|
|
74 TLSImage tls;
|
|
75
|
|
76 // Environment data.
|
|
77 uint64_t *envPtr;
|
221
|
78 };
|
|
79
|
236
|
80 extern AppProperties app;
|
|
81
|
|
82 // The descriptor of a thread's TLS area.
|
|
83 struct TLSDescriptor {
|
|
84 // The size of the TLS area.
|
|
85 uintptr_t size = 0;
|
|
86
|
|
87 // The address of the TLS area. This address can be passed to cleanup
|
|
88 // functions like munmap.
|
|
89 uintptr_t addr = 0;
|
|
90
|
|
91 // The value the thread pointer register should be initialized to.
|
|
92 // Note that, dependending the target architecture ABI, it can be the
|
|
93 // same as |addr| or something else.
|
|
94 uintptr_t tp = 0;
|
|
95
|
|
96 constexpr TLSDescriptor() = default;
|
|
97 };
|
|
98
|
|
99 // Create and initialize the TLS area for the current thread. Should not
|
221
|
100 // be called before app.tls has been initialized.
|
236
|
101 void init_tls(TLSDescriptor &tls);
|
|
102
|
|
103 // Cleanup the TLS area as described in |tls_descriptor|.
|
|
104 void cleanup_tls(uintptr_t tls_addr, uintptr_t tls_size);
|
221
|
105
|
|
106 } // namespace __llvm_libc
|
|
107
|
|
108 #endif // LLVM_LIBC_CONFIG_LINUX_APP_H
|