libc: fix executable destruction support.
This change allows an executable to call its destructor functions
(declared with __attribute__((destructor))) to be properly called
when it normally exits.
Note that this is different from calling the destructors of a shared
library when it is unloaded with dlclose() or through program exit,
which are already supported.
Bug: 3106500
Change-Id: I1412ef5407f13b613fc6cb6103e0a691dbee4b1a
diff --git a/libc/bionic/libc_init_static.c b/libc/bionic/libc_init_static.c
index d097b6b..3634c7b 100644
--- a/libc/bionic/libc_init_static.c
+++ b/libc/bionic/libc_init_static.c
@@ -85,5 +85,12 @@
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));
}