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