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 "config/linux/syscall.h"
11 #include "include/sys/mman.h"
12 #include "include/sys/syscall.h"
13 #include "src/string/memcpy.h"
14 #include "src/sys/mman/mmap.h"
15
16 #include <asm/prctl.h>
17 #include <linux/auxvec.h>
18 #include <linux/elf.h>
19 #include <stdint.h>
20
21 extern "C" int main(int, char **, char **);
22
23 namespace __llvm_libc {
24
25 #ifdef SYS_mmap2
26 static constexpr long mmapSyscallNumber = SYS_mmap2;
27 #elif SYS_mmap
28 static constexpr long mmapSyscallNumber = SYS_mmap;
29 #else
30 #error "Target platform does not have SYS_mmap or SYS_mmap2 defined"
31 #endif
32
33 // TODO: Declare var an extern var in config/linux/app.h so that other
34 // libc functions can make use of the application wide information. For
35 // example, mmap can pick up the page size from here.
36 AppProperties app;
37
38 // TODO: The function is x86_64 specific. Move it to config/linux/app.h
39 // and generalize it. Also, dynamic loading is not handled currently.
initTLS()40 void initTLS() {
41 if (app.tls.size == 0)
42 return;
43
44 // We will assume the alignment is always a power of two.
45 uintptr_t tlsSize = (app.tls.size + app.tls.align) & -app.tls.align;
46
47 // Per the x86_64 TLS ABI, the entry pointed to by the thread pointer is the
48 // address of the TLS block. So, we add more size to accomodate this address
49 // entry.
50 size_t tlsSizeWithAddr = tlsSize + sizeof(uintptr_t);
51
52 // We cannot call the mmap function here as the functions set errno on
53 // failure. Since errno is implemented via a thread local variable, we cannot
54 // use errno before TLS is setup.
55 long mmapRetVal = __llvm_libc::syscall(
56 mmapSyscallNumber, nullptr, tlsSizeWithAddr, PROT_READ | PROT_WRITE,
57 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
58 // We cannot check the return value with MAP_FAILED as that is the return
59 // of the mmap function and not the mmap syscall.
60 if (mmapRetVal < 0 && static_cast<uintptr_t>(mmapRetVal) > -app.pageSize)
61 __llvm_libc::syscall(SYS_exit, 1);
62 uintptr_t *tlsAddr = reinterpret_cast<uintptr_t *>(mmapRetVal);
63
64 // x86_64 TLS faces down from the thread pointer with the first entry
65 // pointing to the address of the first real TLS byte.
66 uintptr_t endPtr = reinterpret_cast<uintptr_t>(tlsAddr) + tlsSize;
67 *reinterpret_cast<uintptr_t *>(endPtr) = endPtr;
68
69 __llvm_libc::memcpy(tlsAddr, reinterpret_cast<const void *>(app.tls.address),
70 app.tls.size);
71 if (__llvm_libc::syscall(SYS_arch_prctl, ARCH_SET_FS, endPtr) == -1)
72 __llvm_libc::syscall(SYS_exit, 1);
73 }
74
75 } // namespace __llvm_libc
76
77 using __llvm_libc::app;
78
79 struct Args {
80 // At the language level, argc is an int. But we use uint64_t as the x86_64
81 // ABI specifies it as an 8 byte value.
82 uint64_t argc;
83
84 // At the language level, argv is a char** value. However, we use uint64_t as
85 // the x86_64 ABI specifies the argv vector be an |argc| long array of 8-byte
86 // values. Even though a flexible length array would be more suitable here, we
87 // set the array length to 1 to avoid a compiler warning about it being a C99
88 // extension. Length of 1 is not really wrong as |argc| is guaranteed to be
89 // atleast 1, and there is an 8-byte null entry at the end of the argv array.
90 uint64_t argv[1];
91 };
92
93 // TODO: Would be nice to use the aux entry structure from elf.h when available.
94 struct AuxEntry {
95 uint64_t type;
96 uint64_t value;
97 };
98
_start()99 extern "C" void _start() {
100 uintptr_t *frame_ptr =
101 reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
102
103 // This TU is compiled with -fno-omit-frame-pointer. Hence, the previous value
104 // of the base pointer is pushed on to the stack. So, we step over it (the
105 // "+ 1" below) to get to the args.
106 Args *args = reinterpret_cast<Args *>(frame_ptr + 1);
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 = args->argv + 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 for (uintptr_t i = 0; i < programHdrCount; ++i) {
138 Elf64_Phdr *phdr = programHdrTable + i;
139 if (phdr->p_type != PT_TLS)
140 continue;
141 // TODO: p_vaddr value has to be adjusted for static-pie executables.
142 app.tls.address = phdr->p_vaddr;
143 app.tls.size = phdr->p_memsz;
144 app.tls.align = phdr->p_align;
145 }
146
147 __llvm_libc::initTLS();
148
149 __llvm_libc::syscall(SYS_exit,
150 main(args->argc, reinterpret_cast<char **>(args->argv),
151 reinterpret_cast<char **>(env_ptr)));
152 }
153