add support for init_array/fini_array ctors/dtors to dynamic linker
authorRich Felker <dalias@aerifal.cx>
Sat, 20 Jul 2013 22:26:17 +0000 (18:26 -0400)
committerRich Felker <dalias@aerifal.cx>
Sat, 20 Jul 2013 22:26:17 +0000 (18:26 -0400)
src/ldso/dynlink.c

index 1d5e2977825e612540886a636e3343b10031a8a9..dbf404ef2dcefef43d76dd7883921f96f915b308 100644 (file)
@@ -696,7 +696,13 @@ static void do_fini()
        for (p=fini_head; p; p=p->fini_next) {
                if (!p->constructed) continue;
                decode_vec(p->dynv, dyn, DYN_CNT);
-               ((void (*)(void))(p->base + dyn[DT_FINI]))();
+               if (dyn[0] & (1<<DT_FINI_ARRAY)) {
+                       size_t n = dyn[DT_FINI_ARRAYSZ]/sizeof(size_t);
+                       size_t *fn = (void *)(p->base + dyn[DT_FINI_ARRAY]);
+                       while (n--) ((void (*)(void))*fn++)();
+               }
+               if (dyn[0] & (1<<DT_FINI))
+                       ((void (*)(void))(p->base + dyn[DT_FINI]))();
        }
 }
 
@@ -712,12 +718,17 @@ static void do_init_fini(struct dso *p)
                if (p->constructed) continue;
                p->constructed = 1;
                decode_vec(p->dynv, dyn, DYN_CNT);
-               if (dyn[0] & (1<<DT_FINI)) {
+               if (dyn[0] & ((1<<DT_FINI) | (1<<DT_FINI_ARRAY))) {
                        p->fini_next = fini_head;
                        fini_head = p;
                }
                if (dyn[0] & (1<<DT_INIT))
                        ((void (*)(void))(p->base + dyn[DT_INIT]))();
+               if (dyn[0] & (1<<DT_INIT_ARRAY)) {
+                       size_t n = dyn[DT_INIT_ARRAYSZ]/sizeof(size_t);
+                       size_t *fn = (void *)(p->base + dyn[DT_INIT_ARRAY]);
+                       while (n--) ((void (*)(void))*fn++)();
+               }
                if (!need_locking && libc.threads_minus_1) {
                        need_locking = 1;
                        pthread_mutex_lock(&init_fini_lock);