Skip to content

Instantly share code, notes, and snippets.

@Cyberax
Created April 23, 2024 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cyberax/a60dce723d4e80a5d182d60677f1df98 to your computer and use it in GitHub Desktop.
Save Cyberax/a60dce723d4e80a5d182d60677f1df98 to your computer and use it in GitHub Desktop.
macOS DYLD
#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__);
}
$ 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]
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment