Elliott Hughes | 42d949f | 2016-01-06 19:51:43 -0800 | [diff] [blame] | 1 | /* |
| 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 Gao | b6453c5 | 2016-06-29 16:47:53 -0700 | [diff] [blame] | 31 | #include "private/KernelArgumentBlock.h" |
Josh Gao | a170d9b | 2016-11-10 16:08:29 -0800 | [diff] [blame^] | 32 | #include "private/bionic_arc4random.h" |
Elliott Hughes | 42d949f | 2016-01-06 19:51:43 -0800 | [diff] [blame] | 33 | #include "private/bionic_auxv.h" |
| 34 | #include "private/bionic_globals.h" |
Josh Gao | b6453c5 | 2016-06-29 16:47:53 -0700 | [diff] [blame] | 35 | #include "private/bionic_ssp.h" |
Elliott Hughes | 42d949f | 2016-01-06 19:51:43 -0800 | [diff] [blame] | 36 | #include "pthread_internal.h" |
| 37 | |
| 38 | extern "C" int __set_tls(void* ptr); |
| 39 | extern "C" int __set_tid_address(int* tid_address); |
| 40 | |
Josh Gao | b6453c5 | 2016-06-29 16:47:53 -0700 | [diff] [blame] | 41 | // Declared in "private/bionic_ssp.h". |
| 42 | uintptr_t __stack_chk_guard = 0; |
| 43 | |
| 44 | void __libc_init_global_stack_chk_guard(KernelArgumentBlock& args) { |
Josh Gao | a170d9b | 2016-11-10 16:08:29 -0800 | [diff] [blame^] | 45 | __libc_safe_arc4random_buf(&__stack_chk_guard, sizeof(__stack_chk_guard), args); |
Josh Gao | b6453c5 | 2016-06-29 16:47:53 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Elliott Hughes | 42d949f | 2016-01-06 19:51:43 -0800 | [diff] [blame] | 48 | // Setup for the main thread. For dynamic executables, this is called by the |
| 49 | // linker _before_ libc is mapped in memory. This means that all writes to |
| 50 | // globals from this function will apply to linker-private copies and will not |
| 51 | // be visible from libc later on. |
| 52 | // |
| 53 | // Note: this function creates a pthread_internal_t for the initial thread and |
| 54 | // stores the pointer in TLS, but does not add it to pthread's thread list. This |
| 55 | // has to be done later from libc itself (see __libc_init_common). |
| 56 | // |
| 57 | // This is in a file by itself because it needs to be built with |
| 58 | // -fno-stack-protector because it's responsible for setting up the main |
| 59 | // thread's TLS (which stack protector relies on). |
| 60 | |
| 61 | void __libc_init_main_thread(KernelArgumentBlock& args) { |
| 62 | __libc_auxv = args.auxv; |
Mingwei Shi | be91052 | 2015-11-12 07:02:14 +0000 | [diff] [blame] | 63 | #if defined(__i386__) |
| 64 | __libc_init_sysinfo(args); |
| 65 | #endif |
Elliott Hughes | 42d949f | 2016-01-06 19:51:43 -0800 | [diff] [blame] | 66 | |
| 67 | static pthread_internal_t main_thread; |
| 68 | |
| 69 | // The -fstack-protector implementation uses TLS, so make sure that's |
| 70 | // set up before we call any function that might get a stack check inserted. |
Josh Gao | 4159e86 | 2016-09-01 22:58:44 -0700 | [diff] [blame] | 71 | // TLS also needs to be set up before errno (and therefore syscalls) can be used. |
Elliott Hughes | 42d949f | 2016-01-06 19:51:43 -0800 | [diff] [blame] | 72 | __set_tls(main_thread.tls); |
Josh Gao | 4159e86 | 2016-09-01 22:58:44 -0700 | [diff] [blame] | 73 | __init_tls(&main_thread); |
Elliott Hughes | 42d949f | 2016-01-06 19:51:43 -0800 | [diff] [blame] | 74 | |
| 75 | // Tell the kernel to clear our tid field when we exit, so we're like any other pthread. |
| 76 | // As a side-effect, this tells us our pid (which is the same as the main thread's tid). |
| 77 | main_thread.tid = __set_tid_address(&main_thread.tid); |
| 78 | main_thread.set_cached_pid(main_thread.tid); |
| 79 | |
| 80 | // We don't want to free the main thread's stack even when the main thread exits |
| 81 | // because things like environment variables with global scope live on it. |
| 82 | // We also can't free the pthread_internal_t itself, since that lives on the main |
| 83 | // thread's stack rather than on the heap. |
| 84 | // The main thread has no mmap allocated space for stack or pthread_internal_t. |
| 85 | main_thread.mmap_size = 0; |
| 86 | pthread_attr_init(&main_thread.attr); |
| 87 | main_thread.attr.guard_size = 0; // The main thread has no guard page. |
| 88 | main_thread.attr.stack_size = 0; // User code should never see this; we'll compute it when asked. |
| 89 | // TODO: the main thread's sched_policy and sched_priority need to be queried. |
| 90 | |
Elliott Hughes | fc69a8a | 2016-03-04 11:53:09 -0800 | [diff] [blame] | 91 | // The TLS stack guard is set from the global, so ensure that we've initialized the global |
Josh Gao | b6453c5 | 2016-06-29 16:47:53 -0700 | [diff] [blame] | 92 | // before we initialize the TLS. Dynamic executables will initialize their copy of the global |
| 93 | // stack protector from the one in the main thread's TLS. |
Elliott Hughes | fc69a8a | 2016-03-04 11:53:09 -0800 | [diff] [blame] | 94 | __libc_init_global_stack_chk_guard(args); |
Josh Gao | 4159e86 | 2016-09-01 22:58:44 -0700 | [diff] [blame] | 95 | __init_thread_stack_guard(&main_thread); |
Elliott Hughes | fc69a8a | 2016-03-04 11:53:09 -0800 | [diff] [blame] | 96 | |
Elliott Hughes | 42d949f | 2016-01-06 19:51:43 -0800 | [diff] [blame] | 97 | __init_thread(&main_thread); |
Elliott Hughes | 42d949f | 2016-01-06 19:51:43 -0800 | [diff] [blame] | 98 | |
| 99 | // Store a pointer to the kernel argument block in a TLS slot to be |
| 100 | // picked up by the libc constructor. |
| 101 | main_thread.tls[TLS_SLOT_BIONIC_PREINIT] = &args; |
| 102 | |
| 103 | __init_alternate_signal_stack(&main_thread); |
| 104 | } |