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