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

source code of libc/startup/linux/do_start.cpp