1 //===-- Implementation of crt for aarch64 ---------------------------------===//
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 #include "config/linux/app.h"
10 #include "src/__support/OSUtil/syscall.h"
11 #include "src/string/memory_utils/memcpy_implementations.h"
12 
13 #include <arm_acle.h>
14 
15 #include <linux/auxvec.h>
16 #include <linux/elf.h>
17 #include <stdint.h>
18 #include <sys/mman.h>
19 #include <sys/syscall.h>
20 
21 extern "C" int main(int, char **, char **);
22 
23 // Source documentation:
24 // https://github.com/ARM-software/abi-aa/tree/main/sysvabi64
25 
26 namespace __llvm_libc {
27 
28 #ifdef SYS_mmap2
29 static constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap2;
30 #elif SYS_mmap
31 static constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap;
32 #else
33 #error "Target platform does not have SYS_mmap or SYS_mmap2 defined"
34 #endif
35 
36 AppProperties app;
37 
38 void init_tls(TLSDescriptor &tls_descriptor) {
39   if (app.tls.size == 0) {
40     tls_descriptor.size = 0;
41     tls_descriptor.tp = 0;
42     return;
43   }
44 
45   // aarch64 follows the variant 1 TLS layout:
46   //
47   // 1. First entry is the dynamic thread vector pointer
48   // 2. Second entry is a 8-byte reserved word.
49   // 3. Padding for alignment.
50   // 4. The TLS data from the ELF image.
51   //
52   // The thread pointer points to the first entry.
53 
54   const uintptr_t size_of_pointers = 2 * sizeof(uintptr_t);
55   uintptr_t padding = 0;
56   const uintptr_t ALIGNMENT_MASK = app.tls.align - 1;
57   uintptr_t diff = size_of_pointers & ALIGNMENT_MASK;
58   if (diff != 0)
59     padding += (ALIGNMENT_MASK - diff) + 1;
60 
61   uintptr_t alloc_size = size_of_pointers + padding + app.tls.size;
62 
63   // We cannot call the mmap function here as the functions set errno on
64   // failure. Since errno is implemented via a thread local variable, we cannot
65   // use errno before TLS is setup.
66   long mmap_ret_val = __llvm_libc::syscall(MMAP_SYSCALL_NUMBER, nullptr,
67                                            alloc_size, PROT_READ | PROT_WRITE,
68                                            MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
69   // We cannot check the return value with MAP_FAILED as that is the return
70   // of the mmap function and not the mmap syscall.
71   if (mmap_ret_val < 0 && static_cast<uintptr_t>(mmap_ret_val) > -app.pageSize)
72     __llvm_libc::syscall(SYS_exit, 1);
73   uintptr_t thread_ptr = uintptr_t(reinterpret_cast<uintptr_t *>(mmap_ret_val));
74   uintptr_t tls_addr = thread_ptr + size_of_pointers + padding;
75   __llvm_libc::inline_memcpy(reinterpret_cast<char *>(tls_addr),
76                              reinterpret_cast<const char *>(app.tls.address),
77                              app.tls.init_size);
78   tls_descriptor.size = alloc_size;
79   tls_descriptor.addr = thread_ptr;
80   tls_descriptor.tp = thread_ptr;
81 }
82 
83 void cleanup_tls(uintptr_t addr, uintptr_t size) {
84   if (size == 0)
85     return;
86   __llvm_libc::syscall(SYS_munmap, addr, size);
87 }
88 
89 static void set_thread_ptr(uintptr_t val) { __arm_wsr64("tpidr_el0", val); }
90 
91 } // namespace __llvm_libc
92 
93 using __llvm_libc::app;
94 
95 // TODO: Would be nice to use the aux entry structure from elf.h when available.
96 struct AuxEntry {
97   uint64_t type;
98   uint64_t value;
99 };
100 
101 extern "C" void _start() {
102   // Skip the Frame Pointer and the Link Register
103   // https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst
104   // Section 6.2.3
105   app.args = reinterpret_cast<__llvm_libc::Args *>(
106       reinterpret_cast<uintptr_t *>(__builtin_frame_address(0)) + 2);
107 
108   // After the argv array, is a 8-byte long NULL value before the array of env
109   // values. The end of the env values is marked by another 8-byte long NULL
110   // value. We step over it (the "+ 1" below) to get to the env values.
111   uint64_t *env_ptr = app.args->argv + app.args->argc + 1;
112   uint64_t *env_end_marker = env_ptr;
113   while (*env_end_marker)
114     ++env_end_marker;
115 
116   // After the env array, is the aux-vector. The end of the aux-vector is
117   // denoted by an AT_NULL entry.
118   Elf64_Phdr *programHdrTable = nullptr;
119   uintptr_t programHdrCount;
120   for (AuxEntry *aux_entry = reinterpret_cast<AuxEntry *>(env_end_marker + 1);
121        aux_entry->type != AT_NULL; ++aux_entry) {
122     switch (aux_entry->type) {
123     case AT_PHDR:
124       programHdrTable = reinterpret_cast<Elf64_Phdr *>(aux_entry->value);
125       break;
126     case AT_PHNUM:
127       programHdrCount = aux_entry->value;
128       break;
129     case AT_PAGESZ:
130       app.pageSize = aux_entry->value;
131       break;
132     default:
133       break; // TODO: Read other useful entries from the aux vector.
134     }
135   }
136 
137   app.tls.size = 0;
138   for (uintptr_t i = 0; i < programHdrCount; ++i) {
139     Elf64_Phdr *phdr = programHdrTable + i;
140     if (phdr->p_type != PT_TLS)
141       continue;
142     // TODO: p_vaddr value has to be adjusted for static-pie executables.
143     app.tls.address = phdr->p_vaddr;
144     app.tls.size = phdr->p_memsz;
145     app.tls.init_size = phdr->p_filesz;
146     app.tls.align = phdr->p_align;
147   }
148 
149   __llvm_libc::TLSDescriptor tls;
150   __llvm_libc::init_tls(tls);
151   if (tls.size != 0)
152     __llvm_libc::set_thread_ptr(tls.tp);
153 
154   int retval = main(app.args->argc, reinterpret_cast<char **>(app.args->argv),
155                     reinterpret_cast<char **>(env_ptr));
156   __llvm_libc::cleanup_tls(tls.addr, tls.size);
157   __llvm_libc::syscall(SYS_exit, retval);
158 }
159