summaryrefslogtreecommitdiff
path: root/libc/misc/internals/shared_flat_initfini.c
diff options
context:
space:
mode:
Diffstat (limited to 'libc/misc/internals/shared_flat_initfini.c')
-rw-r--r--libc/misc/internals/shared_flat_initfini.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/libc/misc/internals/shared_flat_initfini.c b/libc/misc/internals/shared_flat_initfini.c
new file mode 100644
index 000000000..81e53834e
--- /dev/null
+++ b/libc/misc/internals/shared_flat_initfini.c
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2006 CodeSourcery Inc
+ *
+ * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
+ *
+ * This file defines the main initialisation and finalisation code for
+ * shared flat libraries. It in turn calls the initialisation and
+ * finalisation code for each registered library.
+ */
+#include "shared_flat_lib.h"
+
+/* A doubly-linked list of shared libraries. Those nearer the head
+ * of the list should be initialised first and finalised last. */
+struct shared_flat_lib *__first_shared_lib;
+struct shared_flat_lib *__last_shared_lib;
+
+void __shared_flat_init(void)
+{
+ struct shared_flat_lib *lib;
+ void (**start)(void);
+ void (**end)(void);
+
+ for (lib = __first_shared_lib; lib; lib = lib->next) {
+ end = lib->preinit_array_end;
+ for (start = lib->preinit_array_start; start < end; start++)
+ (*start)();
+ }
+
+ for (lib = __first_shared_lib; lib; lib = lib->next) {
+ if (lib->init)
+ lib->init();
+
+ end = lib->init_array_end;
+ for (start = lib->init_array_start; start < end; start++)
+ (*start)();
+ }
+}
+
+void __shared_flat_fini(void)
+{
+ struct shared_flat_lib *lib;
+ void (**start)(void);
+ void (**end)(void);
+
+ for (lib = __last_shared_lib; lib; lib = lib->prev) {
+ start = lib->fini_array_start;
+ for (end = lib->fini_array_end; end > start;)
+ (*--end)();
+
+ if (lib->fini)
+ lib->fini();
+ }
+}