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 <stddef.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 void initTLS() {
40   if (app.tls.size == 0)
41     return;
42 
43   // aarch64 follows the variant 1 TLS layout:
44   //
45   // 1. First entry is the dynamic thread vector pointer
46   // 2. Second entry is a 8-byte reserved word.
47   // 3. Padding for alignment.
48   // 4. The TLS data from the ELF image.
49   //
50   // The thread pointer points to the first entry.
51 
52   const size_t size_of_pointers = 2 * sizeof(uintptr_t);
53   size_t padding = 0;
54   const size_t ALIGNMENT_MASK = app.tls.align - 1;
55   size_t diff = size_of_pointers & ALIGNMENT_MASK;
56   if (diff != 0)
57     padding += (ALIGNMENT_MASK - diff) + 1;
58 
59   size_t alloc_size = size_of_pointers + padding + app.tls.size;
60 
61   // We cannot call the mmap function here as the functions set errno on
62   // failure. Since errno is implemented via a thread local variable, we cannot
63   // use errno before TLS is setup.
64   long mmap_ret_val = __llvm_libc::syscall(MMAP_SYSCALL_NUMBER, nullptr,
65                                            alloc_size, PROT_READ | PROT_WRITE,
66                                            MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
67   // We cannot check the return value with MAP_FAILED as that is the return
68   // of the mmap function and not the mmap syscall.
69   if (mmap_ret_val < 0 && static_cast<uintptr_t>(mmap_ret_val) > -app.pageSize)
70     __llvm_libc::syscall(SYS_exit, 1);
71   uintptr_t thread_ptr = uintptr_t(reinterpret_cast<uintptr_t *>(mmap_ret_val));
72   uintptr_t tls_addr = thread_ptr + size_of_pointers + padding;
73   __llvm_libc::inline_memcpy(reinterpret_cast<char *>(tls_addr),
74                              reinterpret_cast<const char *>(app.tls.address),
75                              app.tls.init_size);
76   __arm_wsr64("tpidr_el0", thread_ptr);
77 }
78 
79 } // namespace __llvm_libc
80 
81 using __llvm_libc::app;
82 
83 // TODO: Would be nice to use the aux entry structure from elf.h when available.
84 struct AuxEntry {
85   uint64_t type;
86   uint64_t value;
87 };
88 
89 extern "C" void _start() {
90   // Skip the Frame Pointer and the Link Register
91   // https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst
92   // Section 6.2.3
93   app.args = reinterpret_cast<__llvm_libc::Args *>(
94       reinterpret_cast<uintptr_t *>(__builtin_frame_address(0)) + 2);
95 
96   // After the argv array, is a 8-byte long NULL value before the array of env
97   // values. The end of the env values is marked by another 8-byte long NULL
98   // value. We step over it (the "+ 1" below) to get to the env values.
99   uint64_t *env_ptr = app.args->argv + app.args->argc + 1;
100   uint64_t *env_end_marker = env_ptr;
101   while (*env_end_marker)
102     ++env_end_marker;
103 
104   // After the env array, is the aux-vector. The end of the aux-vector is
105   // denoted by an AT_NULL entry.
106   Elf64_Phdr *programHdrTable = nullptr;
107   uintptr_t programHdrCount;
108   for (AuxEntry *aux_entry = reinterpret_cast<AuxEntry *>(env_end_marker + 1);
109        aux_entry->type != AT_NULL; ++aux_entry) {
110     switch (aux_entry->type) {
111     case AT_PHDR:
112       programHdrTable = reinterpret_cast<Elf64_Phdr *>(aux_entry->value);
113       break;
114     case AT_PHNUM:
115       programHdrCount = aux_entry->value;
116       break;
117     case AT_PAGESZ:
118       app.pageSize = aux_entry->value;
119       break;
120     default:
121       break; // TODO: Read other useful entries from the aux vector.
122     }
123   }
124 
125   app.tls.size = 0;
126   for (uintptr_t i = 0; i < programHdrCount; ++i) {
127     Elf64_Phdr *phdr = programHdrTable + i;
128     if (phdr->p_type != PT_TLS)
129       continue;
130     // TODO: p_vaddr value has to be adjusted for static-pie executables.
131     app.tls.address = phdr->p_vaddr;
132     app.tls.size = phdr->p_memsz;
133     app.tls.init_size = phdr->p_filesz;
134     app.tls.align = phdr->p_align;
135   }
136 
137   __llvm_libc::initTLS();
138 
139   __llvm_libc::syscall(SYS_exit, main(app.args->argc,
140                                       reinterpret_cast<char **>(app.args->argv),
141                                       reinterpret_cast<char **>(env_ptr)));
142 }
143