xref: /llvm-project/libc/startup/linux/do_start.cpp (revision 779a444009da190c47a2f820395ca001abc29b62)
1 //===-- Implementation file of do_start -----------------------------------===//
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 #include "startup/linux/do_start.h"
9 #include "config/linux/app.h"
10 #include "include/llvm-libc-macros/link-macros.h"
11 #include "src/__support/OSUtil/syscall.h"
12 #include "src/__support/macros/config.h"
13 #include "src/__support/threads/thread.h"
14 #include "src/stdlib/atexit.h"
15 #include "src/stdlib/exit.h"
16 #include "src/unistd/environ.h"
17 
18 #include <linux/auxvec.h>
19 #include <linux/elf.h>
20 #include <stdint.h>
21 #include <sys/mman.h>
22 #include <sys/syscall.h>
23 
24 extern "C" int main(int argc, char **argv, char **envp);
25 
26 extern "C" {
27 // These arrays are present in the .init_array and .fini_array sections.
28 // The symbols are inserted by linker when it sees references to them.
29 extern uintptr_t __preinit_array_start[];
30 extern uintptr_t __preinit_array_end[];
31 extern uintptr_t __init_array_start[];
32 extern uintptr_t __init_array_end[];
33 extern uintptr_t __fini_array_start[];
34 extern uintptr_t __fini_array_end[];
35 // https://refspecs.linuxbase.org/elf/gabi4+/ch5.dynamic.html#dynamic_section
36 // This symbol is provided by the dynamic linker. It can be undefined depending
37 // on how the program is loaded exactly.
38 [[gnu::weak,
39   gnu::visibility("hidden")]] extern const Elf64_Dyn _DYNAMIC[]; // NOLINT
40 }
41 
42 namespace LIBC_NAMESPACE_DECL {
43 AppProperties app;
44 
45 using InitCallback = void(int, char **, char **);
46 using FiniCallback = void(void);
47 
48 static void call_init_array_callbacks(int argc, char **argv, char **env) {
49   size_t preinit_array_size = __preinit_array_end - __preinit_array_start;
50   for (size_t i = 0; i < preinit_array_size; ++i)
51     reinterpret_cast<InitCallback *>(__preinit_array_start[i])(argc, argv, env);
52   size_t init_array_size = __init_array_end - __init_array_start;
53   for (size_t i = 0; i < init_array_size; ++i)
54     reinterpret_cast<InitCallback *>(__init_array_start[i])(argc, argv, env);
55 }
56 
57 static void call_fini_array_callbacks() {
58   size_t fini_array_size = __fini_array_end - __fini_array_start;
59   for (size_t i = fini_array_size; i > 0; --i)
60     reinterpret_cast<FiniCallback *>(__fini_array_start[i - 1])();
61 }
62 
63 static ThreadAttributes main_thread_attrib;
64 static TLSDescriptor tls;
65 // We separate teardown_main_tls from callbacks as callback function themselves
66 // may require TLS.
67 void teardown_main_tls() { cleanup_tls(tls.addr, tls.size); }
68 
69 [[noreturn]] void do_start() {
70   auto tid = syscall_impl<long>(SYS_gettid);
71   if (tid <= 0)
72     syscall_impl<long>(SYS_exit, 1);
73   main_thread_attrib.tid = static_cast<int>(tid);
74 
75   // After the argv array, is a 8-byte long NULL value before the array of env
76   // values. The end of the env values is marked by another 8-byte long NULL
77   // value. We step over it (the "+ 1" below) to get to the env values.
78   uintptr_t *env_ptr = app.args->argv + app.args->argc + 1;
79   uintptr_t *env_end_marker = env_ptr;
80   app.env_ptr = env_ptr;
81   while (*env_end_marker)
82     ++env_end_marker;
83 
84   // Initialize the POSIX global declared in unistd.h
85   environ = reinterpret_cast<char **>(env_ptr);
86 
87   // After the env array, is the aux-vector. The end of the aux-vector is
88   // denoted by an AT_NULL entry.
89   ElfW(Phdr) *program_hdr_table = nullptr;
90   uintptr_t program_hdr_count = 0;
91   app.auxv_ptr = reinterpret_cast<AuxEntry *>(env_end_marker + 1);
92   for (auto *aux_entry = app.auxv_ptr; aux_entry->id != AT_NULL; ++aux_entry) {
93     switch (aux_entry->id) {
94     case AT_PHDR:
95       program_hdr_table = reinterpret_cast<ElfW(Phdr) *>(aux_entry->value);
96       break;
97     case AT_PHNUM:
98       program_hdr_count = aux_entry->value;
99       break;
100     case AT_PAGESZ:
101       app.page_size = aux_entry->value;
102       break;
103     default:
104       break; // TODO: Read other useful entries from the aux vector.
105     }
106   }
107 
108   ptrdiff_t base = 0;
109   app.tls.size = 0;
110   ElfW(Phdr) *tls_phdr = nullptr;
111 
112   for (uintptr_t i = 0; i < program_hdr_count; ++i) {
113     ElfW(Phdr) &phdr = program_hdr_table[i];
114     if (phdr.p_type == PT_PHDR)
115       base = reinterpret_cast<ptrdiff_t>(program_hdr_table) - phdr.p_vaddr;
116     if (phdr.p_type == PT_DYNAMIC && _DYNAMIC)
117       base = reinterpret_cast<ptrdiff_t>(_DYNAMIC) - phdr.p_vaddr;
118     if (phdr.p_type == PT_TLS)
119       tls_phdr = &phdr;
120     // TODO: adjust PT_GNU_STACK
121   }
122 
123   app.tls.address = tls_phdr->p_vaddr + base;
124   app.tls.size = tls_phdr->p_memsz;
125   app.tls.init_size = tls_phdr->p_filesz;
126   app.tls.align = tls_phdr->p_align;
127 
128   // This descriptor has to be static since its cleanup function cannot
129   // capture the context.
130   init_tls(tls);
131   if (tls.size != 0 && !set_thread_ptr(tls.tp))
132     syscall_impl<long>(SYS_exit, 1);
133 
134   self.attrib = &main_thread_attrib;
135   main_thread_attrib.atexit_callback_mgr =
136       internal::get_thread_atexit_callback_mgr();
137 
138   // We want the fini array callbacks to be run after other atexit
139   // callbacks are run. So, we register them before running the init
140   // array callbacks as they can potentially register their own atexit
141   // callbacks.
142   atexit(&call_fini_array_callbacks);
143 
144   call_init_array_callbacks(static_cast<int>(app.args->argc),
145                             reinterpret_cast<char **>(app.args->argv),
146                             reinterpret_cast<char **>(env_ptr));
147 
148   int retval = main(static_cast<int>(app.args->argc),
149                     reinterpret_cast<char **>(app.args->argv),
150                     reinterpret_cast<char **>(env_ptr));
151 
152   exit(retval);
153 }
154 
155 } // namespace LIBC_NAMESPACE_DECL
156