blob: ab48fb87f211326914d31d2a6d406a11f700d927 [file] [log] [blame]
Elliott Hughes42b2c6a2013-02-07 10:14:39 -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 */
Elliott Hughes42d949f2016-01-06 19:51:43 -080028
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080029/*
30 * libc_init_dynamic.c
31 *
32 * This source files provides two important functions for dynamic
33 * executables:
34 *
35 * - a C runtime initializer (__libc_preinit), which is called by
36 * the dynamic linker when libc.so is loaded. This happens before
37 * any other initializer (e.g. static C++ constructors in other
38 * shared libraries the program depends on).
39 *
40 * - a program launch function (__libc_init), which is called after
41 * all dynamic linking has been performed. Technically, it is called
42 * from arch-$ARCH/bionic/crtbegin_dynamic.S which is itself called
43 * by the dynamic linker after all libraries have been loaded and
44 * initialized.
45 */
46
47#include <stddef.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <stdint.h>
51#include <elf.h>
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080052#include "libc_init_common.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070053
Josh Gao3c8fc2f2015-10-08 14:49:26 -070054#include "private/bionic_globals.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070055#include "private/bionic_tls.h"
56#include "private/KernelArgumentBlock.h"
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080057
58extern "C" {
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070059 extern void netdClientInit(void);
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -070060 extern int __cxa_atexit(void (*)(void *), void *, void *);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080061};
62
63// We flag the __libc_preinit function as a constructor to ensure
64// that its address is listed in libc.so's .init_array section.
65// This ensures that the function is called by the dynamic linker
66// as soon as the shared library is loaded.
Elliott Hughesce532722013-03-15 16:31:09 -070067__attribute__((constructor)) static void __libc_preinit() {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080068 // Read the kernel argument block pointer from TLS.
Elliott Hughes2a0b8732013-10-08 18:50:24 -070069 void** tls = __get_tls();
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080070 KernelArgumentBlock** args_slot = &reinterpret_cast<KernelArgumentBlock**>(tls)[TLS_SLOT_BIONIC_PREINIT];
71 KernelArgumentBlock* args = *args_slot;
72
73 // Clear the slot so no other initializer sees its value.
74 // __libc_init_common() will change the TLS area so the old one won't be accessible anyway.
75 *args_slot = NULL;
76
Josh Gao93c0f5e2015-10-06 11:08:13 -070077 __libc_init_globals(*args);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080078 __libc_init_common(*args);
79
Elliott Hughes07f1ded2014-05-14 11:35:49 -070080 // Hooks for various libraries to let them know that we're starting up.
Josh Gao3c8fc2f2015-10-08 14:49:26 -070081 __libc_globals.mutate(__libc_init_malloc);
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070082 netdClientInit();
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080083}
84
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080085// This function is called from the executable's _start entry point
86// (see arch-$ARCH/bionic/crtbegin_dynamic.S), which is itself
87// called by the dynamic linker after it has loaded all shared
88// libraries the executable depends on.
89//
90// Note that the dynamic linker has also run all constructors in the
91// executable at this point.
92__noreturn void __libc_init(void* raw_args,
Elliott Hughesd2867962014-06-03 15:22:34 -070093 void (*onexit)(void) __unused,
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080094 int (*slingshot)(int, char**, char**),
Dmitriy Ivanov4b415552014-09-04 21:54:34 +000095 structors_array_t const * const structors) {
96
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080097 KernelArgumentBlock args(raw_args);
98
99 // Several Linux ABIs don't pass the onexit pointer, and the ones that
100 // do never use it. Therefore, we ignore it.
101
Dmitriy Ivanov4b415552014-09-04 21:54:34 +0000102 // The executable may have its own destructors listed in its .fini_array
103 // so we need to ensure that these are called when the program exits
104 // normally.
105 if (structors->fini_array) {
106 __cxa_atexit(__libc_fini,structors->fini_array,NULL);
107 }
108
Elliott Hughes42b2c6a2013-02-07 10:14:39 -0800109 exit(slingshot(args.argc, args.argv, args.envp));
110}
Yabin Cuica482742016-01-25 17:38:44 -0800111
112extern "C" uint32_t android_get_application_target_sdk_version();
113
114uint32_t bionic_get_application_target_sdk_version() {
115 return android_get_application_target_sdk_version();
116}