summaryrefslogtreecommitdiffstats
path: root/libc/bionic/libc_init_common.c
diff options
context:
space:
mode:
authorDavid 'Digit' Turner <digit@google.com>2009-05-28 15:54:03 +0200
committerDavid 'Digit' Turner <digit@google.com>2009-06-02 23:27:44 +0200
commit03eabfe65e1e2c36f4d26c78a730fa19a3bdada3 (patch)
treeb965ea27e54b0833639227c619f6e35647c92510 /libc/bionic/libc_init_common.c
parent0353195f344666256dba474a15c9ba22cf0cccc9 (diff)
downloadbionic-03eabfe65e1e2c36f4d26c78a730fa19a3bdada3.zip
bionic-03eabfe65e1e2c36f4d26c78a730fa19a3bdada3.tar.gz
bionic-03eabfe65e1e2c36f4d26c78a730fa19a3bdada3.tar.bz2
Fix the C library initialization to avoid calling static C++ constructors twice.
The problem was due to the fact that, in the case of dynamic executables, the dynamic linker calls the DT_PREINIT_ARRAY, DT_INIT and DT_INIT_ARRAY constructors when loading shared libraries and dynamic executables, *before* calling the executable's entry point (i.e. arch-$ARCH/bionic/crtbegin_dynamic.c) which in turns call __libc_init() in libc.so, as defined by bionic/libc_init_dynamic.c The latter did call these constructors array again, mistakenly. The patch also updates the documentation of many related functions. Also adds a new section to linker/README.TXT explaining restrictions on C library usage. The patch has been tested on a Dream for stability issues with proprietary blobs: - H264 decoding works - Camera + Video recording works - GPS works - Sensors work The tests in system/extra/tests/bionic/libc/common/test_static_cpp_mutex.cpp has been run and shows the static C++ constructor being called only once.
Diffstat (limited to 'libc/bionic/libc_init_common.c')
-rw-r--r--libc/bionic/libc_init_common.c58
1 files changed, 25 insertions, 33 deletions
diff --git a/libc/bionic/libc_init_common.c b/libc/bionic/libc_init_common.c
index de4919d..523afcf 100644
--- a/libc/bionic/libc_init_common.c
+++ b/libc/bionic/libc_init_common.c
@@ -39,8 +39,11 @@
#include <bionic_tls.h>
#include <errno.h>
-extern void _init(void);
-extern void _fini(void);
+/* This contains the common C library initialization code.
+ * To understand what happens here, you should read the
+ * "Initialization and Finalization" section of the file
+ * named bionic/linker/README.TXT
+ */
static void call_array(void(**list)())
{
@@ -50,15 +53,6 @@ static void call_array(void(**list)())
}
}
-static void __bionic_do_global_dtors(structors_array_t const * const p)
-{
- call_array(p->fini_array);
- //_fini();
-}
-
-extern unsigned __get_sp(void);
-extern pid_t gettid(void);
-
char* __progname;
char **environ;
@@ -69,30 +63,28 @@ unsigned int __page_shift = PAGE_SHIFT;
int __system_properties_init(void);
+/* This function can be run under two different contexts:
+ *
+ * - for statically linked executables (i.e. those who do
+ * not depend on shared libraries at all), it will be
+ * called from the __libc_init() function defined in
+ * bionic/libc_init_static.c
+ *
+ * - for dynamic executables, it will be called from the
+ * __libc_init() function defined in bionic/libc_init_dynamic.c
+ *
+ */
void __libc_init_common(uintptr_t *elfdata,
void (*onexit)(void),
int (*slingshot)(int, char**, char**),
structors_array_t const * const structors,
void (*pre_ctor_hook)())
{
- pthread_internal_t thread;
- pthread_attr_t thread_attr;
- void *tls_area[BIONIC_TLS_SLOTS];
int argc;
char **argv, **envp, **envend;
struct auxentry *auxentry;
unsigned int page_size = 0, page_shift = 0;
- /* The main thread's stack has empirically shown to be 84k */
- unsigned stacktop = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE;
- unsigned stacksize = 128 * 1024; //84 * 1024;
- unsigned stackbottom = stacktop - stacksize;
-
- pthread_attr_init(&thread_attr);
- pthread_attr_setstack(&thread_attr, (void*)stackbottom, stacksize);
- _init_thread(&thread, gettid(), &thread_attr, (void*)stackbottom);
- __init_tls(tls_area, &thread);
-
argc = (int) *elfdata++;
argv = (char**) elfdata;
envp = argv+(argc+1);
@@ -106,17 +98,17 @@ void __libc_init_common(uintptr_t *elfdata,
if (pre_ctor_hook) pre_ctor_hook();
- // XXX: we should execute the .fini_array upon exit
-
- // pre-init array.
- // XXX: I'm not sure what's the different with the init array.
- call_array(structors->preinit_array);
+ if (structors != NULL) {
+ // pre-init array.
+ call_array(structors->preinit_array);
- // for compatibility with non-eabi binary, call the .ctors section
- call_array(structors->ctors_array);
+ // for compatibility with non-eabi binary, call the .ctors section
+ // this is only useful for static non-ARM (e.g. x86) executables.
+ call_array(structors->ctors_array);
- // call static constructors
- call_array(structors->init_array);
+ // call static constructors
+ call_array(structors->init_array);
+ }
exit(slingshot(argc, argv, envp));
}