Merge commit '1df986c21ee52c6756846b4a5e45cb316f772112' into gingerbread-release-to-dalvik-dev

This commit is contained in:
Brian Carlstrom 2010-11-01 11:42:02 -07:00
commit 4e56e7f2a3
4 changed files with 61 additions and 5 deletions

View File

@ -84,3 +84,39 @@ void __libc_init_common(uintptr_t *elfdata)
/* setup system properties - requires environment */ /* setup system properties - requires environment */
__system_properties_init(); __system_properties_init();
} }
/* This function will be called during normal program termination
* to run the destructors that are listed in the .fini_array section
* of the executable, if any.
*
* 'fini_array' points to a list of function addresses. The first
* entry in the list has value -1, the last one has value 0.
*/
void __libc_fini(void* array)
{
int count;
void** fini_array = array;
const size_t minus1 = ~(size_t)0; /* ensure proper sign extension */
/* Sanity check - first entry must be -1 */
if (array == NULL || (size_t)fini_array[0] != minus1) {
return;
}
/* skip over it */
fini_array += 1;
/* Count the number of destructors. */
for (count = 0; fini_array[count] != NULL; count++);
/* Now call each destructor in reverse order. */
while (count > 0) {
void (*func)() = (void (*)) fini_array[--count];
/* Sanity check, any -1 in the list is ignored */
if ((size_t)func == minus1)
continue;
func();
}
}

View File

@ -39,5 +39,6 @@ typedef struct
} structors_array_t; } structors_array_t;
extern void __libc_init_common(uintptr_t *elfdata); extern void __libc_init_common(uintptr_t *elfdata);
extern void __libc_fini(void* finit_array);
#endif #endif

View File

@ -57,9 +57,9 @@
* This ensures that the function is called by the dynamic linker * This ensures that the function is called by the dynamic linker
* as soon as the shared library is loaded. * as soon as the shared library is loaded.
*/ */
void __attribute__((constructor)) __libc_prenit(void); void __attribute__((constructor)) __libc_preinit(void);
void __libc_prenit(void) void __libc_preinit(void)
{ {
/* Read the ELF data pointer form a special slot of the /* Read the ELF data pointer form a special slot of the
* TLS area, then call __libc_init_common with it. * TLS area, then call __libc_init_common with it.
@ -83,14 +83,19 @@ void __libc_prenit(void)
malloc_debug_init(); malloc_debug_init();
} }
/* 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, __noreturn void __libc_init(uintptr_t *elfdata,
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)
{ {
/* When we reach this point, all initializers have been already
* run by the dynamic linker, so ignore 'structors'.
*/
int argc = (int)*elfdata; int argc = (int)*elfdata;
char** argv = (char**)(elfdata + 1); char** argv = (char**)(elfdata + 1);
char** envp = argv + argc + 1; char** envp = argv + argc + 1;
@ -99,5 +104,12 @@ __noreturn void __libc_init(uintptr_t *elfdata,
* do never use it. Therefore, we ignore it. * 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)); exit(slingshot(argc, argv, envp));
} }

View File

@ -85,5 +85,12 @@ __noreturn void __libc_init(uintptr_t *elfdata,
argv = (char**)(elfdata + 1); argv = (char**)(elfdata + 1);
envp = argv + argc + 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(argc, argv, envp));
} }