blob: cef3d7d228aba223c598ea5e7b7386ac58bf7294 [file] [log] [blame]
Elliott Hughes42d949f2016-01-06 19:51:43 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include "libc_init_common.h"
30
Josh Gaob6453c52016-06-29 16:47:53 -070031#include "private/KernelArgumentBlock.h"
Josh Gaoa170d9b2016-11-10 16:08:29 -080032#include "private/bionic_arc4random.h"
Elliott Hughes42d949f2016-01-06 19:51:43 -080033#include "private/bionic_auxv.h"
dimitryb6d2b872017-10-18 15:15:54 +020034#include "private/bionic_defs.h"
Elliott Hughes42d949f2016-01-06 19:51:43 -080035#include "private/bionic_globals.h"
Josh Gaob6453c52016-06-29 16:47:53 -070036#include "private/bionic_ssp.h"
Elliott Hughes42d949f2016-01-06 19:51:43 -080037#include "pthread_internal.h"
38
Ryan Prichard9cfca862018-11-22 02:44:09 -080039extern "C" pid_t __getpid();
Elliott Hughes42d949f2016-01-06 19:51:43 -080040extern "C" int __set_tid_address(int* tid_address);
41
Josh Gaob6453c52016-06-29 16:47:53 -070042// Declared in "private/bionic_ssp.h".
43uintptr_t __stack_chk_guard = 0;
44
Evgenii Stepanov13e8dcb2018-09-19 16:29:12 -070045static pthread_internal_t main_thread;
46
47__attribute__((no_sanitize("hwaddress")))
48pthread_internal_t* __get_main_thread() {
49 return &main_thread;
50}
51
Elliott Hughes42d949f2016-01-06 19:51:43 -080052// Setup for the main thread. For dynamic executables, this is called by the
53// linker _before_ libc is mapped in memory. This means that all writes to
54// globals from this function will apply to linker-private copies and will not
55// be visible from libc later on.
56//
57// Note: this function creates a pthread_internal_t for the initial thread and
58// stores the pointer in TLS, but does not add it to pthread's thread list. This
59// has to be done later from libc itself (see __libc_init_common).
60//
61// This is in a file by itself because it needs to be built with
62// -fno-stack-protector because it's responsible for setting up the main
63// thread's TLS (which stack protector relies on).
64
Ryan Prichard9cfca862018-11-22 02:44:09 -080065// Do enough setup to:
66// - Let the dynamic linker invoke system calls (and access errno)
67// - Ensure that TLS access functions (__get_{tls,thread}) never return NULL
68// - Allow the stack protector to work (with a zero cookie)
69// Avoid doing much more because, when this code is called within the dynamic
70// linker, the linker binary hasn't been relocated yet, so certain kinds of code
71// are hazardous, such as accessing non-hidden global variables.
dimitryb6d2b872017-10-18 15:15:54 +020072__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Ryan Prichard9cfca862018-11-22 02:44:09 -080073void __libc_init_main_thread_early(KernelArgumentBlock& args) {
Elliott Hughes42d949f2016-01-06 19:51:43 -080074 __libc_auxv = args.auxv;
Mingwei Shibe910522015-11-12 07:02:14 +000075#if defined(__i386__)
76 __libc_init_sysinfo(args);
77#endif
Elliott Hughes42d949f2016-01-06 19:51:43 -080078 __set_tls(main_thread.tls);
Ryan Prichard9cfca862018-11-22 02:44:09 -080079 __init_tls(&main_thread);
80 main_thread.tid = __getpid();
81 main_thread.set_cached_pid(main_thread.tid);
82}
83
84// Finish initializing the main thread.
85__BIONIC_WEAK_FOR_NATIVE_BRIDGE
86void __libc_init_main_thread_late(KernelArgumentBlock& args) {
87 main_thread.bionic_tls = __allocate_bionic_tls();
88 if (main_thread.bionic_tls == nullptr) {
89 // Avoid strerror because it might need bionic_tls.
90 async_safe_fatal("failed to allocate bionic_tls: error %d", errno);
91 }
Elliott Hughes42d949f2016-01-06 19:51:43 -080092
93 // Tell the kernel to clear our tid field when we exit, so we're like any other pthread.
Ryan Prichard9cfca862018-11-22 02:44:09 -080094 __set_tid_address(&main_thread.tid);
Elliott Hughes42d949f2016-01-06 19:51:43 -080095
96 // We don't want to free the main thread's stack even when the main thread exits
97 // because things like environment variables with global scope live on it.
Ryan Prichard6631f9b2018-04-30 18:29:32 -070098 // We also can't free the pthread_internal_t itself, since it is a static variable.
Elliott Hughes42d949f2016-01-06 19:51:43 -080099 // The main thread has no mmap allocated space for stack or pthread_internal_t.
100 main_thread.mmap_size = 0;
Elliott Hughes374848a2017-10-26 12:54:32 -0700101
Elliott Hughes42d949f2016-01-06 19:51:43 -0800102 pthread_attr_init(&main_thread.attr);
Elliott Hughes374848a2017-10-26 12:54:32 -0700103 // We don't want to explicitly set the main thread's scheduler attributes (http://b/68328561).
104 pthread_attr_setinheritsched(&main_thread.attr, PTHREAD_INHERIT_SCHED);
105 // The main thread has no guard page.
106 pthread_attr_setguardsize(&main_thread.attr, 0);
107 // User code should never see this; we'll compute it when asked.
108 pthread_attr_setstacksize(&main_thread.attr, 0);
Elliott Hughes42d949f2016-01-06 19:51:43 -0800109
Elliott Hughesfc69a8a2016-03-04 11:53:09 -0800110 // The TLS stack guard is set from the global, so ensure that we've initialized the global
Josh Gaob6453c52016-06-29 16:47:53 -0700111 // before we initialize the TLS. Dynamic executables will initialize their copy of the global
112 // stack protector from the one in the main thread's TLS.
Ryan Prichardcb5f4102018-05-17 17:00:19 -0700113 __libc_safe_arc4random_buf(&__stack_chk_guard, sizeof(__stack_chk_guard), args);
Ryan Prichard9cfca862018-11-22 02:44:09 -0800114 __init_tls_stack_guard(&main_thread);
Elliott Hughesfc69a8a2016-03-04 11:53:09 -0800115
Elliott Hughes42d949f2016-01-06 19:51:43 -0800116 __init_thread(&main_thread);
Elliott Hughes42d949f2016-01-06 19:51:43 -0800117
Peter Collingbourneda772e22018-09-06 22:20:44 -0700118 __init_additional_stacks(&main_thread);
Elliott Hughes42d949f2016-01-06 19:51:43 -0800119}