xref: /llvm-project-15.0.7/libc/config/linux/app.h (revision 859c1897)
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/architectures.h"
13 
14 #include <stdint.h>
15 
16 namespace __llvm_libc {
17 
18 // Data structure to capture properties of the linux/ELF TLS image.
19 struct 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(LLVM_LIBC_ARCH_X86_64) || defined(LLVM_LIBC_ARCH_AARCH64)
39 // At the language level, argc is an int. But we use uint64_t as the x86_64
40 // ABI specifies it as an 8 byte value. Likewise, in the ARM64 ABI, arguments
41 // are usually passed in registers.  x0 is a doubleword register, so this is
42 // 64 bit for aarch64 as well.
43 typedef uint64_t ArgcType;
44 
45 // At the language level, argv is a char** value. However, we use uint64_t as
46 // ABIs specify the argv vector be an |argc| long array of 8-byte values.
47 typedef uint64_t ArgVEntryType;
48 #else
49 #error "argc and argv types are not defined for the target platform."
50 #endif
51 
52 struct Args {
53   ArgcType argc;
54 
55   // A flexible length array would be more suitable here, but C++ doesn't have
56   // flexible arrays: P1039 proposes to fix this. So, for now we just fake it.
57   // Even if argc is zero, "argv[argc] shall be a null pointer"
58   // (ISO C 5.1.2.2.1) so one is fine. Also, length of 1 is not really wrong as
59   // |argc| is guaranteed to be atleast 1, and there is an 8-byte null entry at
60   // the end of the argv array.
61   ArgVEntryType argv[1];
62 };
63 
64 // Data structure which captures properties of a linux application.
65 struct AppProperties {
66   // Page size used for the application.
67   uintptr_t pageSize;
68 
69   Args *args;
70 
71   // The properties of an application's TLS image.
72   TLSImage tls;
73 
74   // Environment data.
75   uint64_t *envPtr;
76 };
77 
78 extern AppProperties app;
79 
80 // The descriptor of a thread's TLS area.
81 struct TLSDescriptor {
82   // The size of the TLS area.
83   uintptr_t size = 0;
84 
85   // The address of the TLS area. This address can be passed to cleanup
86   // functions like munmap.
87   uintptr_t addr = 0;
88 
89   // The value the thread pointer register should be initialized to.
90   // Note that, dependending the target architecture ABI, it can be the
91   // same as |addr| or something else.
92   uintptr_t tp = 0;
93 
94   constexpr TLSDescriptor() = default;
95 };
96 
97 // Create and initialize the TLS area for the current thread. Should not
98 // be called before app.tls has been initialized.
99 void init_tls(TLSDescriptor &tls);
100 
101 // Cleanup the TLS area as described in |tls_descriptor|.
102 void cleanup_tls(uintptr_t tls_addr, uintptr_t tls_size);
103 
104 } // namespace __llvm_libc
105 
106 #endif // LLVM_LIBC_CONFIG_LINUX_APP_H
107