Clean up the argc/argv/envp/auxv handling.
There's now only one place where we deal with this stuff, it only needs to be parsed once by the dynamic linker (rather than by each recipient), and it's now easier for us to get hold of auxv data early on. Change-Id: I6314224257c736547aac2e2a650e66f2ea53bef5
This commit is contained in:
parent
d32fdbaf03
commit
42b2c6a5ee
@ -177,7 +177,6 @@ libc_common_src_files := \
|
|||||||
bionic/isatty.c \
|
bionic/isatty.c \
|
||||||
bionic/issetugid.c \
|
bionic/issetugid.c \
|
||||||
bionic/ldexp.c \
|
bionic/ldexp.c \
|
||||||
bionic/libc_init_common.c \
|
|
||||||
bionic/logd_write.c \
|
bionic/logd_write.c \
|
||||||
bionic/lseek64.c \
|
bionic/lseek64.c \
|
||||||
bionic/md5.c \
|
bionic/md5.c \
|
||||||
@ -275,6 +274,7 @@ libc_bionic_src_files := \
|
|||||||
bionic/__fgets_chk.cpp \
|
bionic/__fgets_chk.cpp \
|
||||||
bionic/getauxval.cpp \
|
bionic/getauxval.cpp \
|
||||||
bionic/getcwd.cpp \
|
bionic/getcwd.cpp \
|
||||||
|
bionic/libc_init_common.cpp \
|
||||||
bionic/libgen.cpp \
|
bionic/libgen.cpp \
|
||||||
bionic/__memcpy_chk.cpp \
|
bionic/__memcpy_chk.cpp \
|
||||||
bionic/__memmove_chk.cpp \
|
bionic/__memmove_chk.cpp \
|
||||||
@ -824,7 +824,7 @@ include $(CLEAR_VARS)
|
|||||||
LOCAL_SRC_FILES := \
|
LOCAL_SRC_FILES := \
|
||||||
$(libc_arch_static_src_files) \
|
$(libc_arch_static_src_files) \
|
||||||
$(libc_static_common_src_files) \
|
$(libc_static_common_src_files) \
|
||||||
bionic/libc_init_static.c
|
bionic/libc_init_static.cpp
|
||||||
|
|
||||||
LOCAL_C_INCLUDES := $(libc_common_c_includes)
|
LOCAL_C_INCLUDES := $(libc_common_c_includes)
|
||||||
LOCAL_CFLAGS := $(libc_common_cflags) \
|
LOCAL_CFLAGS := $(libc_common_cflags) \
|
||||||
@ -849,7 +849,7 @@ LOCAL_SRC_FILES := \
|
|||||||
$(libc_static_common_src_files) \
|
$(libc_static_common_src_files) \
|
||||||
bionic/dlmalloc.c \
|
bionic/dlmalloc.c \
|
||||||
bionic/malloc_debug_common.cpp \
|
bionic/malloc_debug_common.cpp \
|
||||||
bionic/libc_init_static.c
|
bionic/libc_init_static.cpp
|
||||||
|
|
||||||
LOCAL_CFLAGS := $(libc_common_cflags) \
|
LOCAL_CFLAGS := $(libc_common_cflags) \
|
||||||
-DLIBC_STATIC \
|
-DLIBC_STATIC \
|
||||||
@ -883,7 +883,7 @@ LOCAL_SRC_FILES := \
|
|||||||
bionic/dlmalloc.c \
|
bionic/dlmalloc.c \
|
||||||
bionic/malloc_debug_common.cpp \
|
bionic/malloc_debug_common.cpp \
|
||||||
bionic/pthread_debug.cpp \
|
bionic/pthread_debug.cpp \
|
||||||
bionic/libc_init_dynamic.c
|
bionic/libc_init_dynamic.cpp
|
||||||
|
|
||||||
ifeq ($(TARGET_ARCH),arm)
|
ifeq ($(TARGET_ARCH),arm)
|
||||||
LOCAL_NO_CRT := true
|
LOCAL_NO_CRT := true
|
||||||
|
@ -26,21 +26,8 @@
|
|||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
typedef struct
|
#include "../../bionic/libc_init_common.h"
|
||||||
{
|
#include <stddef.h>
|
||||||
void (**preinit_array)(void);
|
|
||||||
void (**init_array)(void);
|
|
||||||
void (**fini_array)(void);
|
|
||||||
} structors_array_t;
|
|
||||||
|
|
||||||
extern int main(int argc, char **argv, char **env);
|
|
||||||
|
|
||||||
extern void __libc_init(
|
|
||||||
unsigned int *elfdata,
|
|
||||||
void (*onexit)(void),
|
|
||||||
int (*slingshot)(int, char**, char**),
|
|
||||||
structors_array_t const * const structors
|
|
||||||
);
|
|
||||||
|
|
||||||
__attribute__ ((section (".preinit_array")))
|
__attribute__ ((section (".preinit_array")))
|
||||||
void (*__PREINIT_ARRAY__)(void) = (void (*)(void)) -1;
|
void (*__PREINIT_ARRAY__)(void) = (void (*)(void)) -1;
|
||||||
@ -51,17 +38,14 @@ void (*__INIT_ARRAY__)(void) = (void (*)(void)) -1;
|
|||||||
__attribute__ ((section (".fini_array")))
|
__attribute__ ((section (".fini_array")))
|
||||||
void (*__FINI_ARRAY__)(void) = (void (*)(void)) -1;
|
void (*__FINI_ARRAY__)(void) = (void (*)(void)) -1;
|
||||||
|
|
||||||
__attribute__((visibility("hidden")))
|
__LIBC_HIDDEN__ void _start() {
|
||||||
void _start() {
|
|
||||||
structors_array_t array;
|
structors_array_t array;
|
||||||
void *elfdata;
|
|
||||||
|
|
||||||
array.preinit_array = &__PREINIT_ARRAY__;
|
array.preinit_array = &__PREINIT_ARRAY__;
|
||||||
array.init_array = &__INIT_ARRAY__;
|
array.init_array = &__INIT_ARRAY__;
|
||||||
array.fini_array = &__FINI_ARRAY__;
|
array.fini_array = &__FINI_ARRAY__;
|
||||||
|
|
||||||
elfdata = __builtin_frame_address(0) + sizeof(void *);
|
void* raw_args = __builtin_frame_address(0) + sizeof(void*);
|
||||||
__libc_init(elfdata, (void *) 0, &main, &array);
|
__libc_init(raw_args, NULL, &main, &array);
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "__dso_handle.h"
|
#include "__dso_handle.h"
|
||||||
|
@ -26,21 +26,8 @@
|
|||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
typedef struct
|
#include "../../bionic/libc_init_common.h"
|
||||||
{
|
#include <stddef.h>
|
||||||
void (**preinit_array)(void);
|
|
||||||
void (**init_array)(void);
|
|
||||||
void (**fini_array)(void);
|
|
||||||
} structors_array_t;
|
|
||||||
|
|
||||||
extern int main(int argc, char **argv, char **env);
|
|
||||||
|
|
||||||
extern void __libc_init(
|
|
||||||
unsigned int *elfdata,
|
|
||||||
void (*onexit)(void),
|
|
||||||
int (*slingshot)(int, char**, char**),
|
|
||||||
structors_array_t const * const structors
|
|
||||||
);
|
|
||||||
|
|
||||||
__attribute__ ((section (".preinit_array")))
|
__attribute__ ((section (".preinit_array")))
|
||||||
void (*__PREINIT_ARRAY__)(void) = (void (*)(void)) -1;
|
void (*__PREINIT_ARRAY__)(void) = (void (*)(void)) -1;
|
||||||
@ -51,18 +38,16 @@ void (*__INIT_ARRAY__)(void) = (void (*)(void)) -1;
|
|||||||
__attribute__ ((section (".fini_array")))
|
__attribute__ ((section (".fini_array")))
|
||||||
void (*__FINI_ARRAY__)(void) = (void (*)(void)) -1;
|
void (*__FINI_ARRAY__)(void) = (void (*)(void)) -1;
|
||||||
|
|
||||||
__attribute__((visibility("hidden")))
|
__LIBC_HIDDEN__
|
||||||
__attribute__((force_align_arg_pointer))
|
__attribute__((force_align_arg_pointer))
|
||||||
void _start() {
|
void _start() {
|
||||||
structors_array_t array;
|
structors_array_t array;
|
||||||
void *elfdata;
|
|
||||||
|
|
||||||
array.preinit_array = &__PREINIT_ARRAY__;
|
array.preinit_array = &__PREINIT_ARRAY__;
|
||||||
array.init_array = &__INIT_ARRAY__;
|
array.init_array = &__INIT_ARRAY__;
|
||||||
array.fini_array = &__FINI_ARRAY__;
|
array.fini_array = &__FINI_ARRAY__;
|
||||||
|
|
||||||
elfdata = __builtin_frame_address(0) + sizeof(void *);
|
void* raw_args = __builtin_frame_address(0) + sizeof(void*);
|
||||||
__libc_init(elfdata, (void *) 0, &main, &array);
|
__libc_init(raw_args, NULL, &main, &array);
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "__dso_handle.h"
|
#include "__dso_handle.h"
|
||||||
|
@ -32,17 +32,13 @@
|
|||||||
#include <private/bionic_auxv.h>
|
#include <private/bionic_auxv.h>
|
||||||
#include <elf.h>
|
#include <elf.h>
|
||||||
|
|
||||||
__LIBC_HIDDEN__
|
__LIBC_HIDDEN__ Elf32_auxv_t* __libc_auxv = NULL;
|
||||||
Elf32_auxv_t* __libc_auxv = NULL;
|
|
||||||
|
|
||||||
extern "C" unsigned long int getauxval(unsigned long int type) {
|
extern "C" unsigned long int getauxval(unsigned long int type) {
|
||||||
Elf32_auxv_t* v;
|
for (Elf32_auxv_t* v = __libc_auxv; v->a_type != AT_NULL; ++v) {
|
||||||
|
|
||||||
for (v = __libc_auxv; v->a_type != AT_NULL; v++) {
|
|
||||||
if (v->a_type == type) {
|
if (v->a_type == type) {
|
||||||
return v->a_un.a_val;
|
return v->a_un.a_val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -30,29 +30,31 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include <elf.h>
|
#include <elf.h>
|
||||||
#include <asm/page.h>
|
#include <asm/page.h>
|
||||||
#include "pthread_internal.h"
|
#include "pthread_internal.h"
|
||||||
#include "atexit.h"
|
#include "atexit.h"
|
||||||
|
#include "KernelArgumentBlock.h"
|
||||||
#include "libc_init_common.h"
|
#include "libc_init_common.h"
|
||||||
|
|
||||||
#include <bionic_tls.h>
|
#include <bionic_tls.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <private/bionic_auxv.h>
|
#include <private/bionic_auxv.h>
|
||||||
|
|
||||||
extern unsigned __get_sp(void);
|
extern "C" unsigned __get_sp(void);
|
||||||
extern pid_t gettid(void);
|
extern "C" int __system_properties_init(void);
|
||||||
|
|
||||||
|
// Not public, but well-known in the BSDs.
|
||||||
char* __progname;
|
char* __progname;
|
||||||
|
|
||||||
|
// Declared in <unistd.h>
|
||||||
char** environ;
|
char** environ;
|
||||||
|
|
||||||
/* from asm/page.h */
|
// Declared in <asm/page.h>.
|
||||||
unsigned int __page_size = PAGE_SIZE;
|
unsigned int __page_size = PAGE_SIZE;
|
||||||
unsigned int __page_shift = PAGE_SHIFT;
|
unsigned int __page_shift = PAGE_SHIFT;
|
||||||
|
|
||||||
|
|
||||||
int __system_properties_init(void);
|
|
||||||
|
|
||||||
/* Init TLS for the initial thread. Called by the linker _before_ libc is mapped
|
/* Init TLS for the initial thread. Called by the linker _before_ libc is mapped
|
||||||
* in memory. Beware: all writes to libc globals from this function will
|
* in memory. Beware: all writes to libc globals from this function will
|
||||||
* apply to linker-private copies and will not be visible from libc later on.
|
* apply to linker-private copies and will not be visible from libc later on.
|
||||||
@ -61,10 +63,10 @@ int __system_properties_init(void);
|
|||||||
* stores the pointer in TLS, but does not add it to pthread's gThreadList. This
|
* stores the pointer in TLS, but does not add it to pthread's gThreadList. This
|
||||||
* has to be done later from libc itself (see __libc_init_common).
|
* has to be done later from libc itself (see __libc_init_common).
|
||||||
*
|
*
|
||||||
* This function also stores the elf_data argument in a specific TLS slot to be later
|
* This function also stores a pointer to the kernel argument block in a TLS slot to be
|
||||||
* picked up by the libc constructor.
|
* picked up by the libc constructor.
|
||||||
*/
|
*/
|
||||||
void __libc_init_tls(unsigned** elf_data) {
|
extern "C" void __libc_init_tls(void* kernel_argument_block) {
|
||||||
unsigned stack_top = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE;
|
unsigned stack_top = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE;
|
||||||
unsigned stack_size = 128 * 1024;
|
unsigned stack_size = 128 * 1024;
|
||||||
unsigned stack_bottom = stack_top - stack_size;
|
unsigned stack_bottom = stack_top - stack_size;
|
||||||
@ -78,33 +80,21 @@ void __libc_init_tls(unsigned** elf_data) {
|
|||||||
|
|
||||||
static void* tls_area[BIONIC_TLS_SLOTS];
|
static void* tls_area[BIONIC_TLS_SLOTS];
|
||||||
__init_tls(tls_area, &thread);
|
__init_tls(tls_area, &thread);
|
||||||
tls_area[TLS_SLOT_BIONIC_PREINIT] = elf_data;
|
tls_area[TLS_SLOT_BIONIC_PREINIT] = kernel_argument_block;
|
||||||
}
|
}
|
||||||
|
|
||||||
void __libc_init_common(uintptr_t* elf_data) {
|
void __libc_init_common(KernelArgumentBlock& args) {
|
||||||
int argc = *elf_data;
|
// Initialize various globals.
|
||||||
char** argv = (char**) (elf_data + 1);
|
environ = args.envp;
|
||||||
char** envp = argv + argc + 1;
|
errno = 0;
|
||||||
|
__libc_auxv = args.auxv;
|
||||||
|
__progname = args.argv[0] ? args.argv[0] : const_cast<char*>("<unknown>");
|
||||||
|
|
||||||
// Get the main thread from TLS and add it to the thread list.
|
// Get the main thread from TLS and add it to the thread list.
|
||||||
pthread_internal_t* main_thread = __get_thread();
|
pthread_internal_t* main_thread = __get_thread();
|
||||||
main_thread->allocated_on_heap = false;
|
main_thread->allocated_on_heap = false;
|
||||||
_pthread_internal_add(main_thread);
|
_pthread_internal_add(main_thread);
|
||||||
|
|
||||||
// Set various globals.
|
|
||||||
errno = 0;
|
|
||||||
__progname = argv[0] ? argv[0] : "<unknown>";
|
|
||||||
environ = envp;
|
|
||||||
|
|
||||||
// The auxiliary vector is at the end of the environment block
|
|
||||||
while(*envp != NULL) {
|
|
||||||
envp++;
|
|
||||||
}
|
|
||||||
/* The end of the environment block is marked by two NULL pointers */
|
|
||||||
envp++;
|
|
||||||
|
|
||||||
__libc_auxv = (Elf32_auxv_t*) envp;
|
|
||||||
|
|
||||||
__system_properties_init(); // Requires 'environ'.
|
__system_properties_init(); // Requires 'environ'.
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,7 +106,7 @@ void __libc_init_common(uintptr_t* elf_data) {
|
|||||||
* entry in the list has value -1, the last one has value 0.
|
* entry in the list has value -1, the last one has value 0.
|
||||||
*/
|
*/
|
||||||
void __libc_fini(void* array) {
|
void __libc_fini(void* array) {
|
||||||
void** fini_array = array;
|
void** fini_array = reinterpret_cast<void**>(array);
|
||||||
const size_t minus1 = ~(size_t)0; /* ensure proper sign extension */
|
const size_t minus1 = ~(size_t)0; /* ensure proper sign extension */
|
||||||
|
|
||||||
/* Sanity check - first entry must be -1 */
|
/* Sanity check - first entry must be -1 */
|
||||||
@ -135,7 +125,7 @@ void __libc_fini(void* array) {
|
|||||||
|
|
||||||
/* Now call each destructor in reverse order. */
|
/* Now call each destructor in reverse order. */
|
||||||
while (count > 0) {
|
while (count > 0) {
|
||||||
void (*func)() = (void (*)) fini_array[--count];
|
void (*func)() = (void (*)()) fini_array[--count];
|
||||||
|
|
||||||
/* Sanity check, any -1 in the list is ignored */
|
/* Sanity check, any -1 in the list is ignored */
|
||||||
if ((size_t)func == minus1) {
|
if ((size_t)func == minus1) {
|
@ -28,16 +28,29 @@
|
|||||||
#ifndef LIBC_INIT_COMMON_H
|
#ifndef LIBC_INIT_COMMON_H
|
||||||
#define LIBC_INIT_COMMON_H
|
#define LIBC_INIT_COMMON_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <sys/cdefs.h>
|
||||||
|
|
||||||
typedef struct
|
typedef struct {
|
||||||
{
|
|
||||||
void (**preinit_array)(void);
|
void (**preinit_array)(void);
|
||||||
void (**init_array)(void);
|
void (**init_array)(void);
|
||||||
void (**fini_array)(void);
|
void (**fini_array)(void);
|
||||||
} structors_array_t;
|
} structors_array_t;
|
||||||
|
|
||||||
extern void __libc_init_common(uintptr_t *elfdata);
|
__BEGIN_DECLS
|
||||||
extern void __libc_fini(void* finit_array);
|
|
||||||
|
extern int main(int argc, char** argv, char** env);
|
||||||
|
|
||||||
|
__noreturn void __libc_init(void* raw_args,
|
||||||
|
void (*onexit)(void),
|
||||||
|
int (*slingshot)(int, char**, char**),
|
||||||
|
structors_array_t const * const structors);
|
||||||
|
void __libc_fini(void* finit_array);
|
||||||
|
|
||||||
|
__END_DECLS
|
||||||
|
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
struct KernelArgumentBlock;
|
||||||
|
void __libc_init_common(KernelArgumentBlock& args);
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,127 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
* libc_init_dynamic.c
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
* all dynamic linking has been performed. Technically, it is called
|
|
||||||
* from arch-$ARCH/bionic/crtbegin_dynamic.S which is itself called
|
|
||||||
* by the dynamic linker after all libraries have been loaded and
|
|
||||||
* initialized.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <elf.h>
|
|
||||||
#include "atexit.h"
|
|
||||||
#include "libc_init_common.h"
|
|
||||||
#include <bionic_tls.h>
|
|
||||||
|
|
||||||
/* 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.
|
|
||||||
*/
|
|
||||||
void __attribute__((constructor)) __libc_preinit(void);
|
|
||||||
|
|
||||||
void __libc_preinit(void)
|
|
||||||
{
|
|
||||||
/* Read the ELF data pointer from a special slot of the
|
|
||||||
* TLS area, then call __libc_init_common with it.
|
|
||||||
*
|
|
||||||
* Note that:
|
|
||||||
* - we clear the slot so no other initializer sees its value.
|
|
||||||
* - __libc_init_common() will change the TLS area so the old one
|
|
||||||
* won't be accessible anyway.
|
|
||||||
*/
|
|
||||||
void** tls_area = (void**)__get_tls();
|
|
||||||
unsigned* elfdata = tls_area[TLS_SLOT_BIONIC_PREINIT];
|
|
||||||
|
|
||||||
tls_area[TLS_SLOT_BIONIC_PREINIT] = NULL;
|
|
||||||
|
|
||||||
__libc_init_common(elfdata);
|
|
||||||
|
|
||||||
/* Setup pthread routines accordingly to the environment.
|
|
||||||
* Requires system properties
|
|
||||||
*/
|
|
||||||
extern void pthread_debug_init(void);
|
|
||||||
pthread_debug_init();
|
|
||||||
|
|
||||||
/* Setup malloc routines accordingly to the environment.
|
|
||||||
* Requires system properties
|
|
||||||
*/
|
|
||||||
extern void malloc_debug_init(void);
|
|
||||||
malloc_debug_init();
|
|
||||||
}
|
|
||||||
|
|
||||||
void __libc_postfini(void)
|
|
||||||
{
|
|
||||||
extern void malloc_debug_fini(void);
|
|
||||||
malloc_debug_fini();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* This function is called from the executable's _start entry point
|
|
||||||
* (see arch-$ARCH/bionic/crtbegin_dynamic.S), which is itself
|
|
||||||
* called by the dynamic linker after it has loaded all shared
|
|
||||||
* libraries the executable depends on.
|
|
||||||
*
|
|
||||||
* Note that the dynamic linker has also run all constructors in the
|
|
||||||
* executable at this point.
|
|
||||||
*/
|
|
||||||
__noreturn void __libc_init(uintptr_t *elfdata,
|
|
||||||
void (*onexit)(void),
|
|
||||||
int (*slingshot)(int, char**, char**),
|
|
||||||
structors_array_t const * const structors)
|
|
||||||
{
|
|
||||||
int argc = (int)*elfdata;
|
|
||||||
char** argv = (char**)(elfdata + 1);
|
|
||||||
char** envp = argv + argc + 1;
|
|
||||||
|
|
||||||
/* Several Linux ABIs don't pass the onexit pointer, and the ones that
|
|
||||||
* do never use it. Therefore, we ignore it.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* 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)
|
|
||||||
__cxa_atexit(__libc_fini,structors->fini_array,NULL);
|
|
||||||
|
|
||||||
exit(slingshot(argc, argv, envp));
|
|
||||||
}
|
|
115
libc/bionic/libc_init_dynamic.cpp
Normal file
115
libc/bionic/libc_init_dynamic.cpp
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
* libc_init_dynamic.c
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
* all dynamic linking has been performed. Technically, it is called
|
||||||
|
* from arch-$ARCH/bionic/crtbegin_dynamic.S which is itself called
|
||||||
|
* by the dynamic linker after all libraries have been loaded and
|
||||||
|
* initialized.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <elf.h>
|
||||||
|
#include "atexit.h"
|
||||||
|
#include "KernelArgumentBlock.h"
|
||||||
|
#include "libc_init_common.h"
|
||||||
|
#include <bionic_tls.h>
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
extern void pthread_debug_init(void);
|
||||||
|
extern void malloc_debug_init(void);
|
||||||
|
extern void malloc_debug_fini(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
void __attribute__((constructor)) __libc_preinit(void);
|
||||||
|
|
||||||
|
void __libc_preinit() {
|
||||||
|
// Read the kernel argument block pointer from TLS.
|
||||||
|
void* tls = const_cast<void*>(__get_tls());
|
||||||
|
KernelArgumentBlock** args_slot = &reinterpret_cast<KernelArgumentBlock**>(tls)[TLS_SLOT_BIONIC_PREINIT];
|
||||||
|
KernelArgumentBlock* args = *args_slot;
|
||||||
|
|
||||||
|
// Clear the slot so no other initializer sees its value.
|
||||||
|
// __libc_init_common() will change the TLS area so the old one won't be accessible anyway.
|
||||||
|
*args_slot = NULL;
|
||||||
|
|
||||||
|
__libc_init_common(*args);
|
||||||
|
|
||||||
|
// Hooks for the debug malloc and pthread libraries to let them know that we're starting up.
|
||||||
|
pthread_debug_init();
|
||||||
|
malloc_debug_init();
|
||||||
|
}
|
||||||
|
|
||||||
|
void __libc_postfini() {
|
||||||
|
// A hook for the debug malloc library to let it know that we're shutting down.
|
||||||
|
malloc_debug_fini();
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function is called from the executable's _start entry point
|
||||||
|
// (see arch-$ARCH/bionic/crtbegin_dynamic.S), which is itself
|
||||||
|
// called by the dynamic linker after it has loaded all shared
|
||||||
|
// libraries the executable depends on.
|
||||||
|
//
|
||||||
|
// Note that the dynamic linker has also run all constructors in the
|
||||||
|
// executable at this point.
|
||||||
|
__noreturn void __libc_init(void* raw_args,
|
||||||
|
void (*onexit)(void),
|
||||||
|
int (*slingshot)(int, char**, char**),
|
||||||
|
structors_array_t const * const structors) {
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
__cxa_atexit(__libc_fini,structors->fini_array,NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
exit(slingshot(args.argc, args.argv, args.envp));
|
||||||
|
}
|
@ -37,19 +37,20 @@
|
|||||||
* arrays that must be run before the program's 'main' routine is launched.
|
* arrays that must be run before the program's 'main' routine is launched.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <elf.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
|
||||||
#include <elf.h>
|
|
||||||
#include "pthread_internal.h"
|
|
||||||
#include "atexit.h"
|
|
||||||
#include "libc_init_common.h"
|
|
||||||
|
|
||||||
#include <bionic_tls.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <sys/mman.h>
|
|
||||||
#include <sys/auxv.h>
|
#include <sys/auxv.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
|
||||||
|
#include "atexit.h"
|
||||||
|
#include "bionic_tls.h"
|
||||||
|
#include "KernelArgumentBlock.h"
|
||||||
|
#include "libc_init_common.h"
|
||||||
|
#include "pthread_internal.h"
|
||||||
|
|
||||||
// Returns the address of the page containing address 'x'.
|
// Returns the address of the page containing address 'x'.
|
||||||
#define PAGE_START(x) ((x) & PAGE_MASK)
|
#define PAGE_START(x) ((x) & PAGE_MASK)
|
||||||
@ -58,8 +59,7 @@
|
|||||||
// itself at the start of a page.
|
// itself at the start of a page.
|
||||||
#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1))
|
#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1))
|
||||||
|
|
||||||
static void call_array(void(**list)())
|
static void call_array(void(**list)()) {
|
||||||
{
|
|
||||||
// First element is -1, list is null-terminated
|
// First element is -1, list is null-terminated
|
||||||
while (*++list) {
|
while (*++list) {
|
||||||
(*list)();
|
(*list)();
|
||||||
@ -67,62 +67,45 @@ static void call_array(void(**list)())
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void apply_gnu_relro() {
|
static void apply_gnu_relro() {
|
||||||
Elf32_Phdr *phdr_start;
|
Elf32_Phdr* phdr_start = reinterpret_cast<Elf32_Phdr*>(getauxval(AT_PHDR));
|
||||||
unsigned long int phdr_ct;
|
unsigned long int phdr_ct = getauxval(AT_PHNUM);
|
||||||
Elf32_Phdr *phdr;
|
|
||||||
|
|
||||||
phdr_start = (Elf32_Phdr *) getauxval(AT_PHDR);
|
for (Elf32_Phdr* phdr = phdr_start; phdr < (phdr_start + phdr_ct); phdr++) {
|
||||||
phdr_ct = getauxval(AT_PHNUM);
|
if (phdr->p_type != PT_GNU_RELRO) {
|
||||||
|
|
||||||
for (phdr = phdr_start; phdr < (phdr_start + phdr_ct); phdr++) {
|
|
||||||
if (phdr->p_type != PT_GNU_RELRO)
|
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
Elf32_Addr seg_page_start = PAGE_START(phdr->p_vaddr);
|
Elf32_Addr seg_page_start = PAGE_START(phdr->p_vaddr);
|
||||||
Elf32_Addr seg_page_end = PAGE_END(phdr->p_vaddr + phdr->p_memsz);
|
Elf32_Addr seg_page_end = PAGE_END(phdr->p_vaddr + phdr->p_memsz);
|
||||||
|
|
||||||
// Check return value here? What do we do if we fail?
|
// Check return value here? What do we do if we fail?
|
||||||
mprotect((void *) seg_page_start,
|
mprotect(reinterpret_cast<void*>(seg_page_start), seg_page_end - seg_page_start, PROT_READ);
|
||||||
seg_page_end - seg_page_start,
|
|
||||||
PROT_READ);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
__noreturn void __libc_init(uintptr_t *elfdata,
|
__noreturn void __libc_init(void* raw_args,
|
||||||
void (*onexit)(void),
|
void (*onexit)(void),
|
||||||
int (*slingshot)(int, char**, char**),
|
int (*slingshot)(int, char**, char**),
|
||||||
structors_array_t const * const structors)
|
structors_array_t const * const structors) {
|
||||||
{
|
|
||||||
int argc;
|
|
||||||
char **argv, **envp;
|
|
||||||
|
|
||||||
__libc_init_tls(NULL);
|
__libc_init_tls(NULL);
|
||||||
|
|
||||||
/* Initialize the C runtime environment */
|
KernelArgumentBlock args(raw_args);
|
||||||
__libc_init_common(elfdata);
|
__libc_init_common(args);
|
||||||
|
|
||||||
apply_gnu_relro();
|
apply_gnu_relro();
|
||||||
|
|
||||||
/* Several Linux ABIs don't pass the onexit pointer, and the ones that
|
// Several Linux ABIs don't pass the onexit pointer, and the ones that
|
||||||
* do never use it. Therefore, we ignore it.
|
// do never use it. Therefore, we ignore it.
|
||||||
*/
|
|
||||||
|
|
||||||
/* pre-init array. */
|
|
||||||
call_array(structors->preinit_array);
|
call_array(structors->preinit_array);
|
||||||
|
|
||||||
// call static constructors
|
|
||||||
call_array(structors->init_array);
|
call_array(structors->init_array);
|
||||||
|
|
||||||
argc = (int) *elfdata;
|
// The executable may have its own destructors listed in its .fini_array
|
||||||
argv = (char**)(elfdata + 1);
|
// so we need to ensure that these are called when the program exits
|
||||||
envp = argv + argc + 1;
|
// normally.
|
||||||
|
if (structors->fini_array != NULL) {
|
||||||
/* 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)
|
|
||||||
__cxa_atexit(__libc_fini,structors->fini_array,NULL);
|
__cxa_atexit(__libc_fini,structors->fini_array,NULL);
|
||||||
|
}
|
||||||
exit(slingshot(argc, argv, envp));
|
|
||||||
|
exit(slingshot(args.argc, args.argv, args.envp));
|
||||||
}
|
}
|
76
libc/private/KernelArgumentBlock.h
Normal file
76
libc/private/KernelArgumentBlock.h
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2013 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef KERNEL_ARGUMENT_BLOCK_H
|
||||||
|
#define KERNEL_ARGUMENT_BLOCK_H
|
||||||
|
|
||||||
|
#include <elf.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <sys/auxv.h>
|
||||||
|
|
||||||
|
// When the kernel starts the dynamic linker, it passes a pointer to a block
|
||||||
|
// of memory containing argc, the argv array, the environment variable array,
|
||||||
|
// and the array of ELF aux vectors. This class breaks that block up into its
|
||||||
|
// constituents for easy access.
|
||||||
|
class KernelArgumentBlock {
|
||||||
|
public:
|
||||||
|
KernelArgumentBlock(void* raw_args) {
|
||||||
|
uint32_t* args = reinterpret_cast<uint32_t*>(raw_args);
|
||||||
|
argc = static_cast<int>(*args);
|
||||||
|
argv = reinterpret_cast<char**>(args + 1);
|
||||||
|
envp = argv + argc + 1;
|
||||||
|
|
||||||
|
// Skip over all environment variable definitions to find aux vector.
|
||||||
|
// The end of the environment block is marked by two NULL pointers.
|
||||||
|
char** p = envp;
|
||||||
|
while (*p != NULL) {
|
||||||
|
++p;
|
||||||
|
}
|
||||||
|
++p; // Skip second NULL;
|
||||||
|
|
||||||
|
auxv = reinterpret_cast<Elf32_auxv_t*>(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Similar to ::getauxval but doesn't require the libc global variables to be set up,
|
||||||
|
// so it's safe to call this really early on. This function also lets you distinguish
|
||||||
|
// between the inability to find the given type and its value just happening to be 0.
|
||||||
|
unsigned long getauxval(unsigned long type, bool* found_match = NULL) {
|
||||||
|
for (Elf32_auxv_t* v = auxv; v->a_type != AT_NULL; ++v) {
|
||||||
|
if (v->a_type == type) {
|
||||||
|
if (found_match != NULL) {
|
||||||
|
*found_match = true;
|
||||||
|
}
|
||||||
|
return v->a_un.a_val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (found_match != NULL) {
|
||||||
|
*found_match = false;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int argc;
|
||||||
|
char** argv;
|
||||||
|
char** envp;
|
||||||
|
Elf32_auxv_t* auxv;
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Disallow copy and assignment.
|
||||||
|
KernelArgumentBlock(const KernelArgumentBlock&);
|
||||||
|
void operator=(const KernelArgumentBlock&);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // KERNEL_ARGUMENT_BLOCK_H
|
@ -134,7 +134,7 @@ extern void* __get_tls( void );
|
|||||||
extern void* __get_stack_base(int *p_stack_size);
|
extern void* __get_stack_base(int *p_stack_size);
|
||||||
|
|
||||||
/* Initialize the TLS. */
|
/* Initialize the TLS. */
|
||||||
extern void __libc_init_tls(unsigned** elfdata);
|
extern void __libc_init_tls(void* kernel_argument_block);
|
||||||
|
|
||||||
__END_DECLS
|
__END_DECLS
|
||||||
|
|
||||||
|
@ -44,8 +44,12 @@ struct atexit {
|
|||||||
} fns[1]; /* the table itself */
|
} fns[1]; /* the table itself */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
__BEGIN_DECLS
|
||||||
|
|
||||||
extern int __atexit_invalid;
|
extern int __atexit_invalid;
|
||||||
extern struct atexit *__atexit; /* points to head of LIFO stack */
|
extern struct atexit *__atexit; /* points to head of LIFO stack */
|
||||||
|
|
||||||
int __cxa_atexit(void (*)(void *), void *, void *);
|
int __cxa_atexit(void (*)(void *), void *, void *);
|
||||||
void __cxa_finalize(void *);
|
void __cxa_finalize(void *);
|
||||||
|
|
||||||
|
__END_DECLS
|
||||||
|
@ -26,24 +26,23 @@
|
|||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <sys/cdefs.h>
|
||||||
|
|
||||||
extern unsigned __linker_init(unsigned int *elfdata);
|
extern unsigned __linker_init(void* raw_args);
|
||||||
|
|
||||||
__attribute__((visibility("hidden")))
|
__LIBC_HIDDEN__ void _start() {
|
||||||
void _start() {
|
|
||||||
void (*start)(void);
|
void (*start)(void);
|
||||||
|
|
||||||
void* elfdata = (void*) ((uintptr_t) __builtin_frame_address(0) + sizeof(void*));
|
void* raw_args = (void*) ((uintptr_t) __builtin_frame_address(0) + sizeof(void*));
|
||||||
start = (void(*)(void))__linker_init(elfdata);
|
start = (void(*)(void))__linker_init(raw_args);
|
||||||
|
|
||||||
/* linker init returns (%eax) the _entry address in the main image */
|
/* linker init returns (%eax) the _entry address in the main image */
|
||||||
/* entry point expects sp to point to elfdata */
|
/* entry point expects sp to point to raw_args */
|
||||||
|
|
||||||
__asm__ (
|
__asm__ (
|
||||||
"mov %0, %%esp\n\t"
|
"mov %0, %%esp\n\t"
|
||||||
"jmp *%1\n\t"
|
"jmp *%1\n\t"
|
||||||
: : "r"(elfdata), "r"(start) :
|
: : "r"(raw_args), "r"(start) :
|
||||||
);
|
);
|
||||||
|
|
||||||
/* Unreachable */
|
/* Unreachable */
|
||||||
|
@ -42,6 +42,7 @@
|
|||||||
// Private C library headers.
|
// Private C library headers.
|
||||||
#include <private/bionic_tls.h>
|
#include <private/bionic_tls.h>
|
||||||
#include <private/debug_format.h>
|
#include <private/debug_format.h>
|
||||||
|
#include <private/KernelArgumentBlock.h>
|
||||||
#include <private/logd.h>
|
#include <private/logd.h>
|
||||||
#include <private/ScopedPthreadMutexLocker.h>
|
#include <private/ScopedPthreadMutexLocker.h>
|
||||||
|
|
||||||
@ -1771,15 +1772,8 @@ static bool soinfo_link_image(soinfo* si) {
|
|||||||
* fixed it's own GOT. It is safe to make references to externs
|
* fixed it's own GOT. It is safe to make references to externs
|
||||||
* and other non-local data at this point.
|
* and other non-local data at this point.
|
||||||
*/
|
*/
|
||||||
static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linker_base)
|
static unsigned __linker_init_post_relocation(KernelArgumentBlock& args, unsigned linker_base) {
|
||||||
{
|
/* NOTE: we store the args pointer on a special location
|
||||||
static soinfo linker_soinfo;
|
|
||||||
|
|
||||||
int argc = (int) *elfdata;
|
|
||||||
char **argv = (char**) (elfdata + 1);
|
|
||||||
unsigned *vecs = (unsigned*) (argv + argc + 1);
|
|
||||||
|
|
||||||
/* NOTE: we store the elfdata pointer on a special location
|
|
||||||
* of the temporary TLS area in order to pass it to
|
* of the temporary TLS area in order to pass it to
|
||||||
* the C Library's runtime initializer.
|
* the C Library's runtime initializer.
|
||||||
*
|
*
|
||||||
@ -1787,7 +1781,7 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
|
|||||||
* to point to a different location to ensure that no other
|
* to point to a different location to ensure that no other
|
||||||
* shared library constructor can access it.
|
* shared library constructor can access it.
|
||||||
*/
|
*/
|
||||||
__libc_init_tls(elfdata);
|
__libc_init_tls(&args);
|
||||||
|
|
||||||
#if TIMING
|
#if TIMING
|
||||||
struct timeval t0, t1;
|
struct timeval t0, t1;
|
||||||
@ -1795,7 +1789,7 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Initialize environment functions, and get to the ELF aux vectors table.
|
// Initialize environment functions, and get to the ELF aux vectors table.
|
||||||
vecs = linker_env_init(vecs);
|
linker_env_init(args);
|
||||||
|
|
||||||
debugger_init();
|
debugger_init();
|
||||||
|
|
||||||
@ -1815,9 +1809,8 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
|
|||||||
}
|
}
|
||||||
|
|
||||||
INFO("[ android linker & debugger ]\n");
|
INFO("[ android linker & debugger ]\n");
|
||||||
DEBUG("elfdata @ 0x%08x\n", (unsigned)elfdata);
|
|
||||||
|
|
||||||
soinfo* si = soinfo_alloc(argv[0]);
|
soinfo* si = soinfo_alloc(args.argv[0]);
|
||||||
if (si == NULL) {
|
if (si == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -1827,7 +1820,7 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
|
|||||||
link_map* map = &(si->linkmap);
|
link_map* map = &(si->linkmap);
|
||||||
|
|
||||||
map->l_addr = 0;
|
map->l_addr = 0;
|
||||||
map->l_name = argv[0];
|
map->l_name = args.argv[0];
|
||||||
map->l_prev = NULL;
|
map->l_prev = NULL;
|
||||||
map->l_next = NULL;
|
map->l_next = NULL;
|
||||||
|
|
||||||
@ -1841,9 +1834,11 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
|
|||||||
* Don't use soinfo_alloc(), because the linker shouldn't
|
* Don't use soinfo_alloc(), because the linker shouldn't
|
||||||
* be on the soinfo list.
|
* be on the soinfo list.
|
||||||
*/
|
*/
|
||||||
strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
|
static soinfo linker_soinfo;
|
||||||
|
strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
|
||||||
linker_soinfo.flags = 0;
|
linker_soinfo.flags = 0;
|
||||||
linker_soinfo.base = linker_base;
|
linker_soinfo.base = linker_base;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Set the dynamic field in the link map otherwise gdb will complain with
|
* Set the dynamic field in the link map otherwise gdb will complain with
|
||||||
* the following:
|
* the following:
|
||||||
@ -1851,40 +1846,27 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
|
|||||||
* expected address (wrong library or version mismatch?)
|
* expected address (wrong library or version mismatch?)
|
||||||
*/
|
*/
|
||||||
Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
|
Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
|
||||||
Elf32_Phdr *phdr =
|
Elf32_Phdr *phdr = (Elf32_Phdr*)((unsigned char*) linker_base + elf_hdr->e_phoff);
|
||||||
(Elf32_Phdr *)((unsigned char *) linker_base + elf_hdr->e_phoff);
|
|
||||||
phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
|
phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
|
||||||
&linker_soinfo.dynamic, NULL, NULL);
|
&linker_soinfo.dynamic, NULL, NULL);
|
||||||
insert_soinfo_into_debug_map(&linker_soinfo);
|
insert_soinfo_into_debug_map(&linker_soinfo);
|
||||||
|
|
||||||
/* extract information passed from the kernel */
|
// Extract information passed from the kernel.
|
||||||
while (vecs[0] != 0){
|
si->phdr = reinterpret_cast<Elf32_Phdr*>(args.getauxval(AT_PHDR));
|
||||||
switch(vecs[0]){
|
si->phnum = args.getauxval(AT_PHNUM);
|
||||||
case AT_PHDR:
|
si->entry = args.getauxval(AT_ENTRY);
|
||||||
si->phdr = (Elf32_Phdr*) vecs[1];
|
|
||||||
break;
|
|
||||||
case AT_PHNUM:
|
|
||||||
si->phnum = (int) vecs[1];
|
|
||||||
break;
|
|
||||||
case AT_ENTRY:
|
|
||||||
si->entry = vecs[1];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
vecs += 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Compute the value of si->base. We can't rely on the fact that
|
/* Compute the value of si->base. We can't rely on the fact that
|
||||||
* the first entry is the PHDR because this will not be true
|
* the first entry is the PHDR because this will not be true
|
||||||
* for certain executables (e.g. some in the NDK unit test suite)
|
* for certain executables (e.g. some in the NDK unit test suite)
|
||||||
*/
|
*/
|
||||||
int nn;
|
|
||||||
si->base = 0;
|
si->base = 0;
|
||||||
si->size = phdr_table_get_load_size(si->phdr, si->phnum);
|
si->size = phdr_table_get_load_size(si->phdr, si->phnum);
|
||||||
si->load_bias = 0;
|
si->load_bias = 0;
|
||||||
for ( nn = 0; nn < si->phnum; nn++ ) {
|
for (int i = 0; i < si->phnum; ++i) {
|
||||||
if (si->phdr[nn].p_type == PT_PHDR) {
|
if (si->phdr[i].p_type == PT_PHDR) {
|
||||||
si->load_bias = (Elf32_Addr)si->phdr - si->phdr[nn].p_vaddr;
|
si->load_bias = reinterpret_cast<Elf32_Addr>(si->phdr) - si->phdr[i].p_vaddr;
|
||||||
si->base = (Elf32_Addr) si->phdr - si->phdr[nn].p_offset;
|
si->base = reinterpret_cast<Elf32_Addr>(si->phdr) - si->phdr[i].p_offset;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1920,13 +1902,13 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
|
|||||||
|
|
||||||
#if TIMING
|
#if TIMING
|
||||||
gettimeofday(&t1,NULL);
|
gettimeofday(&t1,NULL);
|
||||||
PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
|
PRINT("LINKER TIME: %s: %d microseconds\n", e.argv[0], (int) (
|
||||||
(((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
|
(((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
|
||||||
(((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
|
(((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
|
||||||
));
|
));
|
||||||
#endif
|
#endif
|
||||||
#if STATS
|
#if STATS
|
||||||
PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
|
PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", e.argv[0],
|
||||||
linker_stats.count[kRelocAbsolute],
|
linker_stats.count[kRelocAbsolute],
|
||||||
linker_stats.count[kRelocRelative],
|
linker_stats.count[kRelocRelative],
|
||||||
linker_stats.count[kRelocCopy],
|
linker_stats.count[kRelocCopy],
|
||||||
@ -1946,7 +1928,7 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
|
PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", e.argv[0], count, count * 4);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -1958,31 +1940,6 @@ static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linke
|
|||||||
return si->entry;
|
return si->entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Find the value of AT_BASE passed to us by the kernel. This is the load
|
|
||||||
* location of the linker.
|
|
||||||
*/
|
|
||||||
static unsigned find_linker_base(unsigned **elfdata) {
|
|
||||||
int argc = (int) *elfdata;
|
|
||||||
char **argv = (char**) (elfdata + 1);
|
|
||||||
unsigned *vecs = (unsigned*) (argv + argc + 1);
|
|
||||||
while (vecs[0] != 0) {
|
|
||||||
vecs++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* The end of the environment block is marked by two NULL pointers */
|
|
||||||
vecs++;
|
|
||||||
|
|
||||||
while(vecs[0]) {
|
|
||||||
if (vecs[0] == AT_BASE) {
|
|
||||||
return vecs[1];
|
|
||||||
}
|
|
||||||
vecs += 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0; // should never happen
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Compute the load-bias of an existing executable. This shall only
|
/* Compute the load-bias of an existing executable. This shall only
|
||||||
* be used to compute the load bias of an executable or shared library
|
* be used to compute the load bias of an executable or shared library
|
||||||
* that was loaded by the kernel itself.
|
* that was loaded by the kernel itself.
|
||||||
@ -2018,11 +1975,13 @@ get_elf_exec_load_bias(const Elf32_Ehdr* elf)
|
|||||||
* relocations, any attempt to reference an extern variable, extern
|
* relocations, any attempt to reference an extern variable, extern
|
||||||
* function, or other GOT reference will generate a segfault.
|
* function, or other GOT reference will generate a segfault.
|
||||||
*/
|
*/
|
||||||
extern "C" unsigned __linker_init(unsigned **elfdata) {
|
extern "C" unsigned __linker_init(void* raw_args) {
|
||||||
unsigned linker_addr = find_linker_base(elfdata);
|
KernelArgumentBlock args(raw_args);
|
||||||
|
|
||||||
|
unsigned linker_addr = args.getauxval(AT_BASE);
|
||||||
|
|
||||||
Elf32_Ehdr *elf_hdr = (Elf32_Ehdr*) linker_addr;
|
Elf32_Ehdr *elf_hdr = (Elf32_Ehdr*) linker_addr;
|
||||||
Elf32_Phdr *phdr =
|
Elf32_Phdr *phdr = (Elf32_Phdr*)((unsigned char*) linker_addr + elf_hdr->e_phoff);
|
||||||
(Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff);
|
|
||||||
|
|
||||||
soinfo linker_so;
|
soinfo linker_so;
|
||||||
memset(&linker_so, 0, sizeof(soinfo));
|
memset(&linker_so, 0, sizeof(soinfo));
|
||||||
@ -2047,7 +2006,7 @@ extern "C" unsigned __linker_init(unsigned **elfdata) {
|
|||||||
|
|
||||||
// We have successfully fixed our own relocations. It's safe to run
|
// We have successfully fixed our own relocations. It's safe to run
|
||||||
// the main part of the linker now.
|
// the main part of the linker now.
|
||||||
unsigned start_address = __linker_init_post_relocation(elfdata, linker_addr);
|
unsigned start_address = __linker_init_post_relocation(args, linker_addr);
|
||||||
|
|
||||||
set_soinfo_pool_protection(PROT_READ);
|
set_soinfo_pool_protection(PROT_READ);
|
||||||
|
|
||||||
|
@ -33,6 +33,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <private/KernelArgumentBlock.h>
|
||||||
|
|
||||||
static char** _envp;
|
static char** _envp;
|
||||||
static bool _AT_SECURE_value = true;
|
static bool _AT_SECURE_value = true;
|
||||||
|
|
||||||
@ -40,22 +42,19 @@ bool get_AT_SECURE() {
|
|||||||
return _AT_SECURE_value;
|
return _AT_SECURE_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void __init_AT_SECURE(unsigned* auxv) {
|
static void __init_AT_SECURE(KernelArgumentBlock& args) {
|
||||||
// Check auxv for AT_SECURE first to see if program is setuid, setgid,
|
// Check auxv for AT_SECURE first to see if program is setuid, setgid,
|
||||||
// has file caps, or caused a SELinux/AppArmor domain transition.
|
// has file caps, or caused a SELinux/AppArmor domain transition.
|
||||||
for (unsigned* v = auxv; v[0]; v += 2) {
|
bool kernel_supplied_AT_SECURE;
|
||||||
if (v[0] == AT_SECURE) {
|
_AT_SECURE_value = args.getauxval(AT_SECURE, &kernel_supplied_AT_SECURE);
|
||||||
// Kernel told us whether to enable secure mode.
|
|
||||||
_AT_SECURE_value = v[1];
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// We don't support ancient kernels.
|
// We don't support ancient kernels.
|
||||||
|
if (!kernel_supplied_AT_SECURE) {
|
||||||
const char* msg = "FATAL: kernel did not supply AT_SECURE\n";
|
const char* msg = "FATAL: kernel did not supply AT_SECURE\n";
|
||||||
write(2, msg, strlen(msg));
|
write(2, msg, strlen(msg));
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check if the environment variable definition at 'envstr'
|
// Check if the environment variable definition at 'envstr'
|
||||||
// starts with '<name>=', and if so return the address of the
|
// starts with '<name>=', and if so return the address of the
|
||||||
@ -164,22 +163,12 @@ static void __sanitize_environment_variables() {
|
|||||||
dst[0] = NULL;
|
dst[0] = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned* linker_env_init(unsigned* environment_and_aux_vectors) {
|
void linker_env_init(KernelArgumentBlock& args) {
|
||||||
// Store environment pointer - can't be NULL.
|
// Store environment pointer - can't be NULL.
|
||||||
_envp = reinterpret_cast<char**>(environment_and_aux_vectors);
|
_envp = args.envp;
|
||||||
|
|
||||||
// Skip over all environment variable definitions.
|
__init_AT_SECURE(args);
|
||||||
// The end of the environment block is marked by two NULL pointers.
|
|
||||||
unsigned* aux_vectors = environment_and_aux_vectors;
|
|
||||||
while (aux_vectors[0] != 0) {
|
|
||||||
++aux_vectors;
|
|
||||||
}
|
|
||||||
++aux_vectors;
|
|
||||||
|
|
||||||
__init_AT_SECURE(aux_vectors);
|
|
||||||
__sanitize_environment_variables();
|
__sanitize_environment_variables();
|
||||||
|
|
||||||
return aux_vectors;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* linker_env_get(const char* name) {
|
const char* linker_env_get(const char* name) {
|
||||||
|
@ -29,10 +29,10 @@
|
|||||||
#ifndef LINKER_ENVIRON_H
|
#ifndef LINKER_ENVIRON_H
|
||||||
#define LINKER_ENVIRON_H
|
#define LINKER_ENVIRON_H
|
||||||
|
|
||||||
// Call this function before anything else. 'environment_and_aux_vectors'
|
struct KernelArgumentBlock;
|
||||||
// must point to the environment block in the ELF data block. The function
|
|
||||||
// returns the start of the aux vectors after the environment block.
|
// Call this function before any of the other functions in this header file.
|
||||||
extern unsigned* linker_env_init(unsigned* environment_and_aux_vectors);
|
extern void linker_env_init(KernelArgumentBlock& args);
|
||||||
|
|
||||||
// Returns the value of environment variable 'name' if defined and not
|
// Returns the value of environment variable 'name' if defined and not
|
||||||
// empty, or NULL otherwise.
|
// empty, or NULL otherwise.
|
||||||
@ -41,4 +41,4 @@ extern const char* linker_env_get(const char* name);
|
|||||||
// Returns the value of this program's AT_SECURE variable.
|
// Returns the value of this program's AT_SECURE variable.
|
||||||
extern bool get_AT_SECURE();
|
extern bool get_AT_SECURE();
|
||||||
|
|
||||||
#endif /* LINKER_ENVIRON_H */
|
#endif // LINKER_ENVIRON_H
|
||||||
|
Loading…
Reference in New Issue
Block a user