xref: /llvm-project-15.0.7/libc/config/linux/app.h (revision b3fc0fa8)
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.
19 struct TLS {
20   // The load address of the TLS.
21   uintptr_t address;
22 
23   // The bytes size of the TLS.
24   uintptr_t size;
25 
26   // The alignment of the TLS layout. It assumed that the alignment
27   // value is a power of 2.
28   uintptr_t align;
29 };
30 
31 #if defined(LLVM_LIBC_ARCH_X86_64) || defined(LLVM_LIBC_ARCH_AARCH64)
32 // At the language level, argc is an int. But we use uint64_t as the x86_64
33 // ABI specifies it as an 8 byte value. Likewise, in the ARM64 ABI, arguments
34 // are usually passed in registers.  x0 is a doubleword register, so this is
35 // 64 bit for aarch64 as well.
36 typedef uint64_t ArgcType;
37 
38 // At the language level, argv is a char** value. However, we use uint64_t as
39 // ABIs specify the argv vector be an |argc| long array of 8-byte values.
40 typedef uint64_t ArgVEntryType;
41 #else
42 #error "argc and argv types are not defined for the target platform."
43 #endif
44 
45 struct Args {
46   ArgcType argc;
47 
48   // A flexible length array would be more suitable here, but C++ doesn't have
49   // flexible arrays: P1039 proposes to fix this. So, for now we just fake it.
50   // Even if argc is zero, "argv[argc] shall be a null pointer"
51   // (ISO C 5.1.2.2.1) so one is fine. Also, length of 1 is not really wrong as
52   // |argc| is guaranteed to be atleast 1, and there is an 8-byte null entry at
53   // the end of the argv array.
54   ArgVEntryType argv[1];
55 };
56 
57 // Data structure which captures properties of a linux application.
58 struct AppProperties {
59   // Page size used for the application.
60   uintptr_t pageSize;
61 
62   Args *args;
63 
64   // The properties of an application's TLS.
65   TLS tls;
66 
67   // Environment data.
68   uint64_t *envPtr;
69 };
70 
71 extern AppProperties app;
72 
73 // Creates and initializes the TLS area for the current thread. Should not
74 // be called before app.tls has been initialized.
75 void initTLS();
76 
77 } // namespace __llvm_libc
78 
79 #endif // LLVM_LIBC_CONFIG_LINUX_APP_H
80