2013-02-07 19:14:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
2016-01-07 04:51:43 +01:00
|
|
|
|
2013-02-07 19:14:39 +01:00
|
|
|
/*
|
|
|
|
* This source files provides two important functions for dynamic
|
|
|
|
* executables:
|
|
|
|
*
|
|
|
|
* - a C runtime initializer (__libc_preinit), which is called by
|
|
|
|
* the dynamic linker when libc.so is loaded. This happens before
|
|
|
|
* any other initializer (e.g. static C++ constructors in other
|
|
|
|
* shared libraries the program depends on).
|
|
|
|
*
|
|
|
|
* - a program launch function (__libc_init), which is called after
|
2020-02-10 19:30:38 +01:00
|
|
|
* all dynamic linking has been performed.
|
2013-02-07 19:14:39 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <elf.h>
|
|
|
|
#include "libc_init_common.h"
|
2013-10-10 00:50:50 +02:00
|
|
|
|
2019-01-18 10:00:59 +01:00
|
|
|
#include "private/bionic_elf_tls.h"
|
2015-10-08 23:49:26 +02:00
|
|
|
#include "private/bionic_globals.h"
|
2019-12-20 01:35:51 +01:00
|
|
|
#include "platform/bionic/macros.h"
|
2016-06-30 01:47:53 +02:00
|
|
|
#include "private/bionic_ssp.h"
|
2013-10-10 00:50:50 +02:00
|
|
|
#include "private/bionic_tls.h"
|
|
|
|
#include "private/KernelArgumentBlock.h"
|
2013-02-07 19:14:39 +01:00
|
|
|
|
|
|
|
extern "C" {
|
2014-05-12 20:19:16 +02:00
|
|
|
extern void netdClientInit(void);
|
2014-07-11 21:59:16 +02:00
|
|
|
extern int __cxa_atexit(void (*)(void *), void *, void *);
|
2013-02-07 19:14:39 +01:00
|
|
|
};
|
|
|
|
|
2018-05-18 02:14:18 +02:00
|
|
|
// Use an initializer so __libc_sysinfo will have a fallback implementation
|
|
|
|
// while .preinit_array constructors run.
|
|
|
|
#if defined(__i386__)
|
|
|
|
__LIBC_HIDDEN__ void* __libc_sysinfo = reinterpret_cast<void*>(__libc_int0x80);
|
|
|
|
#endif
|
|
|
|
|
2019-08-06 01:16:14 +02:00
|
|
|
extern "C" __attribute__((weak)) void __hwasan_library_loaded(ElfW(Addr) base,
|
|
|
|
const ElfW(Phdr)* phdr,
|
|
|
|
ElfW(Half) phnum);
|
|
|
|
extern "C" __attribute__((weak)) void __hwasan_library_unloaded(ElfW(Addr) base,
|
|
|
|
const ElfW(Phdr)* phdr,
|
|
|
|
ElfW(Half) phnum);
|
|
|
|
|
2017-05-09 23:27:43 +02:00
|
|
|
// We need a helper function for __libc_preinit because compiling with LTO may
|
|
|
|
// inline functions requiring a stack protector check, but __stack_chk_guard is
|
|
|
|
// not initialized at the start of __libc_preinit. __libc_preinit_impl will run
|
|
|
|
// after __stack_chk_guard is initialized and therefore can safely have a stack
|
|
|
|
// protector.
|
|
|
|
__attribute__((noinline))
|
2018-11-22 12:16:06 +01:00
|
|
|
static void __libc_preinit_impl() {
|
2018-11-22 01:23:03 +01:00
|
|
|
#if defined(__i386__)
|
2018-11-22 12:16:06 +01:00
|
|
|
__libc_init_sysinfo();
|
2018-11-22 01:23:03 +01:00
|
|
|
#endif
|
2018-06-02 00:30:54 +02:00
|
|
|
|
2019-01-18 10:00:59 +01:00
|
|
|
// Register libc.so's copy of the TLS generation variable so the linker can
|
|
|
|
// update it when it loads or unloads a shared object.
|
|
|
|
TlsModules& tls_modules = __libc_shared_globals()->tls_modules;
|
|
|
|
tls_modules.generation_libc_so = &__libc_tls_generation_copy;
|
|
|
|
__libc_tls_generation_copy = tls_modules.generation;
|
|
|
|
|
2018-11-22 12:16:06 +01:00
|
|
|
__libc_init_globals();
|
2018-11-22 11:41:36 +01:00
|
|
|
__libc_init_common();
|
2020-12-15 22:55:32 +01:00
|
|
|
__libc_init_scudo();
|
2017-05-09 23:27:43 +02:00
|
|
|
|
|
|
|
// Hooks for various libraries to let them know that we're starting up.
|
|
|
|
__libc_globals.mutate(__libc_init_malloc);
|
2019-08-06 01:16:14 +02:00
|
|
|
|
2020-01-02 20:54:57 +01:00
|
|
|
// Install reserved signal handlers for assisting the platform's profilers.
|
|
|
|
__libc_init_profiling_handlers();
|
|
|
|
|
2019-11-15 01:02:09 +01:00
|
|
|
__libc_init_fork_handler();
|
|
|
|
|
2019-08-06 01:16:14 +02:00
|
|
|
#if __has_feature(hwaddress_sanitizer)
|
|
|
|
// Notify the HWASan runtime library whenever a library is loaded or unloaded
|
|
|
|
// so that it can update its shadow memory.
|
|
|
|
__libc_shared_globals()->load_hook = __hwasan_library_loaded;
|
|
|
|
__libc_shared_globals()->unload_hook = __hwasan_library_unloaded;
|
|
|
|
#endif
|
|
|
|
|
2017-05-09 23:27:43 +02:00
|
|
|
netdClientInit();
|
|
|
|
}
|
|
|
|
|
2017-10-23 19:07:55 +02:00
|
|
|
// We flag the __libc_preinit function as a constructor to ensure that
|
|
|
|
// its address is listed in libc.so's .init_array section.
|
|
|
|
// This ensures that the function is called by the dynamic linker as
|
|
|
|
// soon as the shared library is loaded.
|
|
|
|
// We give this constructor priority 1 because we want libc's constructor
|
|
|
|
// to run before any others (such as the jemalloc constructor), and lower
|
|
|
|
// is better (http://b/68046352).
|
|
|
|
__attribute__((constructor(1))) static void __libc_preinit() {
|
2016-06-30 01:47:53 +02:00
|
|
|
// The linker has initialized its copy of the global stack_chk_guard, and filled in the main
|
|
|
|
// thread's TLS slot with that value. Initialize the local global stack guard with its value.
|
2018-11-22 12:16:06 +01:00
|
|
|
__stack_chk_guard = reinterpret_cast<uintptr_t>(__get_tls()[TLS_SLOT_STACK_GUARD]);
|
2016-06-30 01:47:53 +02:00
|
|
|
|
2018-11-22 12:16:06 +01:00
|
|
|
__libc_preinit_impl();
|
2013-02-07 19:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function is called from the executable's _start entry point
|
2018-05-01 03:29:32 +02:00
|
|
|
// (see arch-$ARCH/bionic/crtbegin.c), which is itself called by the dynamic
|
|
|
|
// linker after it has loaded all shared libraries the executable depends on.
|
2013-02-07 19:14:39 +01:00
|
|
|
//
|
|
|
|
// Note that the dynamic linker has also run all constructors in the
|
|
|
|
// executable at this point.
|
|
|
|
__noreturn void __libc_init(void* raw_args,
|
2014-06-04 00:22:34 +02:00
|
|
|
void (*onexit)(void) __unused,
|
2013-02-07 19:14:39 +01:00
|
|
|
int (*slingshot)(int, char**, char**),
|
2014-09-04 23:54:34 +02:00
|
|
|
structors_array_t const * const structors) {
|
2017-10-06 00:18:47 +02:00
|
|
|
BIONIC_STOP_UNWIND;
|
2014-09-04 23:54:34 +02:00
|
|
|
|
2013-02-07 19:14:39 +01:00
|
|
|
KernelArgumentBlock args(raw_args);
|
|
|
|
|
|
|
|
// Several Linux ABIs don't pass the onexit pointer, and the ones that
|
|
|
|
// do never use it. Therefore, we ignore it.
|
|
|
|
|
2014-09-04 23:54:34 +02:00
|
|
|
// The executable may have its own destructors listed in its .fini_array
|
|
|
|
// so we need to ensure that these are called when the program exits
|
|
|
|
// normally.
|
|
|
|
if (structors->fini_array) {
|
2018-08-03 02:31:13 +02:00
|
|
|
__cxa_atexit(__libc_fini,structors->fini_array,nullptr);
|
2014-09-04 23:54:34 +02:00
|
|
|
}
|
|
|
|
|
2018-11-22 11:40:17 +01:00
|
|
|
exit(slingshot(args.argc - __libc_shared_globals()->initial_linker_arg_count,
|
|
|
|
args.argv + __libc_shared_globals()->initial_linker_arg_count,
|
2018-10-02 08:10:05 +02:00
|
|
|
args.envp));
|
2013-02-07 19:14:39 +01:00
|
|
|
}
|
2018-11-22 11:40:17 +01:00
|
|
|
|
|
|
|
extern "C" libc_shared_globals* __loader_shared_globals();
|
|
|
|
|
|
|
|
__LIBC_HIDDEN__ libc_shared_globals* __libc_shared_globals() {
|
|
|
|
return __loader_shared_globals();
|
|
|
|
}
|