1 //===-- Implementation of crt for x86_64 ----------------------------------===//
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 <asm/prctl.h>
14 #include <linux/auxvec.h>
15 #include <linux/elf.h>
16 #include <stdint.h>
17 #include <sys/mman.h>
18 #include <sys/syscall.h>
19 
20 extern "C" int main(int, char **, char **);
21 
22 namespace __llvm_libc {
23 
24 #ifdef SYS_mmap2
25 static constexpr long mmapSyscallNumber = SYS_mmap2;
26 #elif SYS_mmap
27 static constexpr long mmapSyscallNumber = SYS_mmap;
28 #else
29 #error "Target platform does not have SYS_mmap or SYS_mmap2 defined"
30 #endif
31 
32 AppProperties app;
33 
34 // TODO: The function is x86_64 specific. Move it to config/linux/app.h
35 // and generalize it. Also, dynamic loading is not handled currently.
36 void initTLS() {
37   if (app.tls.size == 0)
38     return;
39 
40   // We will assume the alignment is always a power of two.
41   uintptr_t tlsSize = app.tls.size & -app.tls.align;
42   if (tlsSize != app.tls.size)
43     tlsSize += app.tls.align;
44 
45   // Per the x86_64 TLS ABI, the entry pointed to by the thread pointer is the
46   // address of the TLS block. So, we add more size to accomodate this address
47   // entry.
48   size_t tlsSizeWithAddr = tlsSize + sizeof(uintptr_t);
49 
50   // We cannot call the mmap function here as the functions set errno on
51   // failure. Since errno is implemented via a thread local variable, we cannot
52   // use errno before TLS is setup.
53   long mmapRetVal = __llvm_libc::syscall(
54       mmapSyscallNumber, nullptr, tlsSizeWithAddr, PROT_READ | PROT_WRITE,
55       MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
56   // We cannot check the return value with MAP_FAILED as that is the return
57   // of the mmap function and not the mmap syscall.
58   if (mmapRetVal < 0 && static_cast<uintptr_t>(mmapRetVal) > -app.pageSize)
59     __llvm_libc::syscall(SYS_exit, 1);
60   uintptr_t *tlsAddr = reinterpret_cast<uintptr_t *>(mmapRetVal);
61 
62   // x86_64 TLS faces down from the thread pointer with the first entry
63   // pointing to the address of the first real TLS byte.
64   uintptr_t endPtr = reinterpret_cast<uintptr_t>(tlsAddr) + tlsSize;
65   *reinterpret_cast<uintptr_t *>(endPtr) = endPtr;
66 
67   __llvm_libc::inline_memcpy(reinterpret_cast<char *>(tlsAddr),
68                              reinterpret_cast<const char *>(app.tls.address),
69                              app.tls.init_size);
70   if (__llvm_libc::syscall(SYS_arch_prctl, ARCH_SET_FS, endPtr) == -1)
71     __llvm_libc::syscall(SYS_exit, 1);
72 }
73 
74 } // namespace __llvm_libc
75 
76 using __llvm_libc::app;
77 
78 // TODO: Would be nice to use the aux entry structure from elf.h when available.
79 struct AuxEntry {
80   uint64_t type;
81   uint64_t value;
82 };
83 
84 extern "C" void _start() {
85   // This TU is compiled with -fno-omit-frame-pointer. Hence, the previous value
86   // of the base pointer is pushed on to the stack. So, we step over it (the
87   // "+ 1" below) to get to the args.
88   app.args = reinterpret_cast<__llvm_libc::Args *>(
89       reinterpret_cast<uintptr_t *>(__builtin_frame_address(0)) + 1);
90 
91   // The x86_64 ABI requires that the stack pointer is aligned to a 16-byte
92   // boundary. We align it here but we cannot use any local variables created
93   // before the following alignment. Best would be to not create any local
94   // variables before the alignment. Also, note that we are aligning the stack
95   // downwards as the x86_64 stack grows downwards. This ensures that we don't
96   // tread on argc, argv etc.
97   // NOTE: Compiler attributes for alignment do not help here as the stack
98   // pointer on entry to this _start function is controlled by the OS. In fact,
99   // compilers can generate code assuming the alignment as required by the ABI.
100   // If the stack pointers as setup by the OS are already aligned, then the
101   // following code is a NOP.
102   __asm__ __volatile__("andq $0xfffffffffffffff0, %%rsp\n\t" ::: "%rsp");
103   __asm__ __volatile__("andq $0xfffffffffffffff0, %%rbp\n\t" ::: "%rbp");
104 
105   // After the argv array, is a 8-byte long NULL value before the array of env
106   // values. The end of the env values is marked by another 8-byte long NULL
107   // value. We step over it (the "+ 1" below) to get to the env values.
108   uint64_t *env_ptr = app.args->argv + app.args->argc + 1;
109   uint64_t *env_end_marker = env_ptr;
110   app.envPtr = env_ptr;
111   while (*env_end_marker)
112     ++env_end_marker;
113 
114   // After the env array, is the aux-vector. The end of the aux-vector is
115   // denoted by an AT_NULL entry.
116   Elf64_Phdr *programHdrTable = nullptr;
117   uintptr_t programHdrCount;
118   for (AuxEntry *aux_entry = reinterpret_cast<AuxEntry *>(env_end_marker + 1);
119        aux_entry->type != AT_NULL; ++aux_entry) {
120     switch (aux_entry->type) {
121     case AT_PHDR:
122       programHdrTable = reinterpret_cast<Elf64_Phdr *>(aux_entry->value);
123       break;
124     case AT_PHNUM:
125       programHdrCount = aux_entry->value;
126       break;
127     case AT_PAGESZ:
128       app.pageSize = aux_entry->value;
129       break;
130     default:
131       break; // TODO: Read other useful entries from the aux vector.
132     }
133   }
134 
135   app.tls.size = 0;
136   for (uintptr_t i = 0; i < programHdrCount; ++i) {
137     Elf64_Phdr *phdr = programHdrTable + i;
138     if (phdr->p_type != PT_TLS)
139       continue;
140     // TODO: p_vaddr value has to be adjusted for static-pie executables.
141     app.tls.address = phdr->p_vaddr;
142     app.tls.size = phdr->p_memsz;
143     app.tls.init_size = phdr->p_filesz;
144     app.tls.align = phdr->p_align;
145   }
146 
147   __llvm_libc::initTLS();
148 
149   __llvm_libc::syscall(SYS_exit, main(app.args->argc,
150                                       reinterpret_cast<char **>(app.args->argv),
151                                       reinterpret_cast<char **>(env_ptr)));
152 }
153