xref: /llvm-project-15.0.7/libc/config/linux/app.h (revision d3cbcc4e)
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 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.
72   TLS tls;
73 
74   // Environment data.
75   uint64_t *envPtr;
76 };
77 
78 extern AppProperties app;
79 
80 // Creates and initializes the TLS area for the current thread. Should not
81 // be called before app.tls has been initialized.
82 void initTLS();
83 
84 } // namespace __llvm_libc
85 
86 #endif // LLVM_LIBC_CONFIG_LINUX_APP_H
87