Skip to content

Instantly share code, notes, and snippets.

@Cyberax
Created April 23, 2024 17:54
Show Gist options
  • Select an option

  • Save Cyberax/a60dce723d4e80a5d182d60677f1df98 to your computer and use it in GitHub Desktop.

Select an option

Save Cyberax/a60dce723d4e80a5d182d60677f1df98 to your computer and use it in GitHub Desktop.

Revisions

  1. Cyberax created this gist Apr 23, 2024.
    10 changes: 10 additions & 0 deletions Output
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    $ gcc -dynamiclib -o test.dyld file.c
    $ gcc trial.c
    $ ./a.out
    [file.c] [initializer1]
    [file.c] [initializer2]
    [file.c] [initializer3]
    [trial.c] [main] Finished loading. Now quitting.
    [file.c] [finalizer3]
    [file.c] [finalizer2]
    [file.c] [finalizer1]
    31 changes: 31 additions & 0 deletions file.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    #include <stdio.h>

    __attribute__((constructor))
    static void initializer1() {
    printf("[%s] [%s]\n", __FILE__, __FUNCTION__);
    }

    __attribute__((constructor))
    static void initializer2() {
    printf("[%s] [%s]\n", __FILE__, __FUNCTION__);
    }

    __attribute__((constructor))
    static void initializer3() {
    printf("[%s] [%s]\n", __FILE__, __FUNCTION__);
    }

    __attribute__((destructor))
    static void finalizer1() {
    printf("[%s] [%s]\n", __FILE__, __FUNCTION__);
    }

    __attribute__((destructor))
    static void finalizer2() {
    printf("[%s] [%s]\n", __FILE__, __FUNCTION__);
    }

    __attribute__((destructor))
    static void finalizer3() {
    printf("[%s] [%s]\n", __FILE__, __FUNCTION__);
    }
    11 changes: 11 additions & 0 deletions trial.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    #include <stdio.h>
    #include <dlfcn.h>

    int main(int argc, char** argv) {
    void *lib = dlopen("./test.dyld", RTLD_LAZY);

    printf("[%s] [%s] Finished loading. Now quitting.\n", __FILE__, __FUNCTION__);

    return 0;

    }