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:
Elliott Hughes
2013-02-07 10:14:39 -08:00
parent d32fdbaf03
commit 42b2c6a5ee
16 changed files with 383 additions and 417 deletions

View File

@@ -32,17 +32,13 @@
#include <private/bionic_auxv.h>
#include <elf.h>
__LIBC_HIDDEN__
Elf32_auxv_t* __libc_auxv = NULL;
__LIBC_HIDDEN__ Elf32_auxv_t* __libc_auxv = NULL;
extern "C" unsigned long int getauxval(unsigned long int type) {
Elf32_auxv_t* v;
for (v = __libc_auxv; v->a_type != AT_NULL; v++) {
for (Elf32_auxv_t* v = __libc_auxv; v->a_type != AT_NULL; ++v) {
if (v->a_type == type) {
return v->a_un.a_val;
}
}
return 0;
}

View File

@@ -30,29 +30,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <elf.h>
#include <asm/page.h>
#include "pthread_internal.h"
#include "atexit.h"
#include "KernelArgumentBlock.h"
#include "libc_init_common.h"
#include <bionic_tls.h>
#include <errno.h>
#include <private/bionic_auxv.h>
extern unsigned __get_sp(void);
extern pid_t gettid(void);
extern "C" unsigned __get_sp(void);
extern "C" int __system_properties_init(void);
char* __progname;
// Not public, but well-known in the BSDs.
char* __progname;
// Declared in <unistd.h>
char** environ;
/* from asm/page.h */
// Declared in <asm/page.h>.
unsigned int __page_size = PAGE_SIZE;
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
* 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.
@@ -61,10 +63,10 @@ int __system_properties_init(void);
* 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).
*
* 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.
*/
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_size = 128 * 1024;
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];
__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) {
int argc = *elf_data;
char** argv = (char**) (elf_data + 1);
char** envp = argv + argc + 1;
void __libc_init_common(KernelArgumentBlock& args) {
// Initialize various globals.
environ = args.envp;
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.
pthread_internal_t* main_thread = __get_thread();
main_thread->allocated_on_heap = false;
_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'.
}
@@ -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.
*/
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 */
/* Sanity check - first entry must be -1 */
@@ -135,7 +125,7 @@ void __libc_fini(void* array) {
/* Now call each destructor in reverse order. */
while (count > 0) {
void (*func)() = (void (*)) fini_array[--count];
void (*func)() = (void (*)()) fini_array[--count];
/* Sanity check, any -1 in the list is ignored */
if ((size_t)func == minus1) {

View File

@@ -28,16 +28,29 @@
#ifndef LIBC_INIT_COMMON_H
#define LIBC_INIT_COMMON_H
#include <stdint.h>
#include <sys/cdefs.h>
typedef struct
{
void (**preinit_array)(void);
void (**init_array)(void);
void (**fini_array)(void);
typedef struct {
void (**preinit_array)(void);
void (**init_array)(void);
void (**fini_array)(void);
} structors_array_t;
extern void __libc_init_common(uintptr_t *elfdata);
extern void __libc_fini(void* finit_array);
__BEGIN_DECLS
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

View File

@@ -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));
}

View 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));
}

View File

@@ -37,19 +37,20 @@
* arrays that must be run before the program's 'main' routine is launched.
*/
#include <elf.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.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/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'.
#define PAGE_START(x) ((x) & PAGE_MASK)
@@ -58,71 +59,53 @@
// itself at the start of a page.
#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1))
static void call_array(void(**list)())
{
// First element is -1, list is null-terminated
while (*++list) {
(*list)();
}
static void call_array(void(**list)()) {
// First element is -1, list is null-terminated
while (*++list) {
(*list)();
}
}
static void apply_gnu_relro() {
Elf32_Phdr *phdr_start;
unsigned long int phdr_ct;
Elf32_Phdr *phdr;
Elf32_Phdr* phdr_start = reinterpret_cast<Elf32_Phdr*>(getauxval(AT_PHDR));
unsigned long int phdr_ct = getauxval(AT_PHNUM);
phdr_start = (Elf32_Phdr *) getauxval(AT_PHDR);
phdr_ct = getauxval(AT_PHNUM);
for (phdr = phdr_start; phdr < (phdr_start + phdr_ct); phdr++) {
if (phdr->p_type != PT_GNU_RELRO)
continue;
Elf32_Addr seg_page_start = PAGE_START(phdr->p_vaddr);
Elf32_Addr seg_page_end = PAGE_END(phdr->p_vaddr + phdr->p_memsz);
// Check return value here? What do we do if we fail?
mprotect((void *) seg_page_start,
seg_page_end - seg_page_start,
PROT_READ);
for (Elf32_Phdr* phdr = phdr_start; phdr < (phdr_start + phdr_ct); phdr++) {
if (phdr->p_type != PT_GNU_RELRO) {
continue;
}
Elf32_Addr seg_page_start = PAGE_START(phdr->p_vaddr);
Elf32_Addr seg_page_end = PAGE_END(phdr->p_vaddr + phdr->p_memsz);
// Check return value here? What do we do if we fail?
mprotect(reinterpret_cast<void*>(seg_page_start), seg_page_end - seg_page_start, PROT_READ);
}
}
__noreturn void __libc_init(uintptr_t *elfdata,
void (*onexit)(void),
int (*slingshot)(int, char**, char**),
structors_array_t const * const structors)
{
int argc;
char **argv, **envp;
__noreturn void __libc_init(void* raw_args,
void (*onexit)(void),
int (*slingshot)(int, char**, char**),
structors_array_t const * const structors) {
__libc_init_tls(NULL);
__libc_init_tls(NULL);
KernelArgumentBlock args(raw_args);
__libc_init_common(args);
/* Initialize the C runtime environment */
__libc_init_common(elfdata);
apply_gnu_relro();
apply_gnu_relro();
// Several Linux ABIs don't pass the onexit pointer, and the ones that
// do never use it. Therefore, we ignore it.
/* Several Linux ABIs don't pass the onexit pointer, and the ones that
* do never use it. Therefore, we ignore it.
*/
call_array(structors->preinit_array);
call_array(structors->init_array);
/* pre-init array. */
call_array(structors->preinit_array);
// 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 != NULL) {
__cxa_atexit(__libc_fini,structors->fini_array,NULL);
}
// call static constructors
call_array(structors->init_array);
argc = (int) *elfdata;
argv = (char**)(elfdata + 1);
envp = argv + argc + 1;
/* 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));
exit(slingshot(args.argc, args.argv, args.envp));
}