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