OSDN Git Service

Check for bad packets in getaddrinfo.c's getanswer. am: 87c0dba7b1 am: dc0492ac4f...
[android-x86/bionic.git] / libc / bionic / libc_init_dynamic.cpp
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 /*
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>
52 #include "libc_init_common.h"
53
54 #include "private/bionic_globals.h"
55 #include "private/bionic_ssp.h"
56 #include "private/bionic_tls.h"
57 #include "private/KernelArgumentBlock.h"
58
59 extern "C" {
60   extern void netdClientInit(void);
61   extern int __cxa_atexit(void (*)(void *), void *, void *);
62 };
63
64 // We flag the __libc_preinit function as a constructor to ensure
65 // that its address is listed in libc.so's .init_array section.
66 // This ensures that the function is called by the dynamic linker
67 // as soon as the shared library is loaded.
68 __attribute__((constructor)) static void __libc_preinit() {
69   // Read the kernel argument block pointer from TLS.
70   void** tls = __get_tls();
71   KernelArgumentBlock** args_slot = &reinterpret_cast<KernelArgumentBlock**>(tls)[TLS_SLOT_BIONIC_PREINIT];
72   KernelArgumentBlock* args = *args_slot;
73
74   // Clear the slot so no other initializer sees its value.
75   // __libc_init_common() will change the TLS area so the old one won't be accessible anyway.
76   *args_slot = NULL;
77
78   // The linker has initialized its copy of the global stack_chk_guard, and filled in the main
79   // thread's TLS slot with that value. Initialize the local global stack guard with its value.
80   __stack_chk_guard = reinterpret_cast<uintptr_t>(tls[TLS_SLOT_STACK_GUARD]);
81
82   __libc_init_globals(*args);
83   __libc_init_common(*args);
84
85   // Hooks for various libraries to let them know that we're starting up.
86   __libc_globals.mutate(__libc_init_malloc);
87   netdClientInit();
88 }
89
90 // This function is called from the executable's _start entry point
91 // (see arch-$ARCH/bionic/crtbegin_dynamic.S), which is itself
92 // called by the dynamic linker after it has loaded all shared
93 // libraries the executable depends on.
94 //
95 // Note that the dynamic linker has also run all constructors in the
96 // executable at this point.
97 __noreturn void __libc_init(void* raw_args,
98                             void (*onexit)(void) __unused,
99                             int (*slingshot)(int, char**, char**),
100                             structors_array_t const * const structors) {
101
102   KernelArgumentBlock args(raw_args);
103
104   // Several Linux ABIs don't pass the onexit pointer, and the ones that
105   // do never use it.  Therefore, we ignore it.
106
107   // The executable may have its own destructors listed in its .fini_array
108   // so we need to ensure that these are called when the program exits
109   // normally.
110   if (structors->fini_array) {
111     __cxa_atexit(__libc_fini,structors->fini_array,NULL);
112   }
113
114   exit(slingshot(args.argc, args.argv, args.envp));
115 }
116
117 extern "C" uint32_t android_get_application_target_sdk_version();
118
119 uint32_t bionic_get_application_target_sdk_version() {
120   return android_get_application_target_sdk_version();
121 }