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.
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 __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 @@
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 @@
if (pre_ctor_hook) pre_ctor_hook();
- // XXX: we should execute the .fini_array upon exit
+ if (structors != NULL) {
+ // pre-init array.
+ call_array(structors->preinit_array);
- // pre-init array.
- // XXX: I'm not sure what's the different with the init array.
- call_array(structors->preinit_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);
- // for compatibility with non-eabi binary, call the .ctors section
- 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));
}