add support for init/finit (constructors and destructors)
authorRich Felker <dalias@aerifal.cx>
Mon, 6 Feb 2012 19:39:09 +0000 (14:39 -0500)
committerRich Felker <dalias@aerifal.cx>
Mon, 6 Feb 2012 19:39:09 +0000 (14:39 -0500)
this is mainly in hopes of supporting c++ (not yet possible for other
reasons) but will also help applications/libraries which use (and more
often, abuse) the gcc __attribute__((__constructor__)) feature in "C"
code.

x86_64 and arm versions of the new startup asm are untested and may
have minor problems.

crt/arm/crt1.s
crt/arm/crti.s [new file with mode: 0644]
crt/arm/crtn.s [new file with mode: 0644]
crt/i386/crt1.s
crt/i386/crti.s [new file with mode: 0644]
crt/i386/crtn.s [new file with mode: 0644]
crt/x86_64/crt1.s
crt/x86_64/crti.s [new file with mode: 0644]
crt/x86_64/crtn.s [new file with mode: 0644]
src/ldso/dynlink.c

index 74b909498ab91d6c9ff87e72f793aad79defb469..ed2a57a2dbd55c90fe978753fd95a8c5a0097694 100644 (file)
@@ -1,13 +1,16 @@
+.weak _init
+.weak _fini
 .global _start
 _start:
        mov fp,#0
        mov lr,#0
        ldr a2,[sp],#4
        mov a3,sp
-       mov a4,#0
+       ldr a4,=_fini
        str fp,[sp,#-4]!
        str a1,[sp,#-4]!
-       str fp,[sp,#-4]!
+       str a4,[sp,#-4]!
+       ldr a4,=_init
        ldr a1,=main
        bl __libc_start_main
 1:     b 1b
diff --git a/crt/arm/crti.s b/crt/arm/crti.s
new file mode 100644 (file)
index 0000000..2eb23ed
--- /dev/null
@@ -0,0 +1,7 @@
+.section .init
+.global _init
+_init:
+
+.section .fini
+.global _fini
+_fini:
diff --git a/crt/arm/crtn.s b/crt/arm/crtn.s
new file mode 100644 (file)
index 0000000..9d7107d
--- /dev/null
@@ -0,0 +1,9 @@
+.section .init
+       tst lr,#1
+       moveq pc,lr
+       bx lr
+
+.section .fini
+       tst lr,#1
+       moveq pc,lr
+       bx lr
index 3e88c7850635c7e5511bc2ac7dac7e8025b53e86..66ee11ab1a8bdf4342ad6d5bb3c442e76499170c 100644 (file)
@@ -1,3 +1,5 @@
+.weak _init
+.weak _fini
 .text
 .global _start
 _start:
@@ -8,8 +10,10 @@ _start:
        pushl %esp
        pushl %esp
        pushl %edx
-       pushl %ebp
-       pushl %ebp
+       call 1f
+1:     addl $[_fini-.],(%esp)
+       call 1f
+1:     addl $[_init-.],(%esp)
        pushl %eax
        pushl %ecx
        call 1f
diff --git a/crt/i386/crti.s b/crt/i386/crti.s
new file mode 100644 (file)
index 0000000..2eb23ed
--- /dev/null
@@ -0,0 +1,7 @@
+.section .init
+.global _init
+_init:
+
+.section .fini
+.global _fini
+_fini:
diff --git a/crt/i386/crtn.s b/crt/i386/crtn.s
new file mode 100644 (file)
index 0000000..055451e
--- /dev/null
@@ -0,0 +1,5 @@
+.section .init
+       ret
+
+.section .fini
+       ret
index 45cbb9dbd7d62e6cbbb5607ae82f1fa81a92d0fc..50222f1ce6b7c3ea772c1e9bd848eaeda880bc70 100644 (file)
@@ -1,4 +1,6 @@
 /* Written 2011 Nicholas J. Kain, released as Public Domain */
+.weak _init
+.weak _fini
 .text
 .global _start
 _start:
@@ -9,8 +11,8 @@ _start:
        andq $-16,%rsp  /* align stack pointer */
        push %rax       /* 8th arg: glibc ABI compatible */
        push %rsp       /* 7th arg: glibc ABI compatible */
-       xor %r8,%r8     /* 5th arg: always 0 */
-       xor %rcx,%rcx   /* 4th arg: always 0 */
+       mov $_fini,%r8  /* 5th arg: fini/dtors function */
+       mov $_init,%rcx /* 4th arg: init/ctors function */
        mov $main,%rdi  /* 1st arg: application entry ip */
        call __libc_start_main /* musl init will run the program */
-.L0:   jmp .L0
+1:     jmp 1b
diff --git a/crt/x86_64/crti.s b/crt/x86_64/crti.s
new file mode 100644 (file)
index 0000000..2eb23ed
--- /dev/null
@@ -0,0 +1,7 @@
+.section .init
+.global _init
+_init:
+
+.section .fini
+.global _fini
+_fini:
diff --git a/crt/x86_64/crtn.s b/crt/x86_64/crtn.s
new file mode 100644 (file)
index 0000000..055451e
--- /dev/null
@@ -0,0 +1,5 @@
+.section .init
+       ret
+
+.section .fini
+       ret
index 057a4cd33e6444cfbb362546b0808f0a3c62e7a0..ca49f4390ee10e2ba9e1d45494ab4d9741ccdd7a 100644 (file)
@@ -49,6 +49,7 @@ struct dso
        ino_t ino;
        int global;
        int relocated;
+       int constructed;
        struct dso **deps;
        char *name;
        char buf[];
@@ -471,6 +472,20 @@ static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride)
        return 0;
 }
 
+static void do_init_fini(struct dso *p)
+{
+       size_t dyn[DYN_CNT] = {0};
+       for (; p; p=p->prev) {
+               if (p->constructed) return;
+               decode_vec(p->dynv, dyn, DYN_CNT);
+               if (dyn[0] & (1<<DT_FINI))
+                       atexit((void (*)(void))(p->base + dyn[DT_FINI]));
+               if (dyn[0] & (1<<DT_INIT))
+                       ((void (*)(void))(p->base + dyn[DT_INIT]))();
+               p->constructed = 1;
+       }
+}
+
 void *__dynlink(int argc, char **argv)
 {
        size_t *auxv, aux[AUX_CNT] = {0};
@@ -520,6 +535,7 @@ void *__dynlink(int argc, char **argv)
        }
        app->name = argv[0];
        app->global = 1;
+       app->constructed = 1;
        app->dynv = (void *)(app->base + find_dyn(
                (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT]));
        decode_dyn(app);
@@ -577,6 +593,9 @@ void *__dynlink(int argc, char **argv)
         * error. If the dynamic loader (dlopen) will not be used, free
         * all memory used by the dynamic linker. */
        runtime = 1;
+
+       do_init_fini(tail);
+
        if (!rtld_used) {
                free_all(head);
                free(sys_path);