clang -shared -o /tmp/test-dylib.dylib test-dylib.c
export DYLD_INSERT_LIBRARIES=/tmp/test-dylib.dylib
# test it
zsh -c 'echo Hello from zsh'
# main is at: 0x10d74bdb8
Hello from zsh
#include <stdio.h> | |
#include <dlfcn.h> | |
static void init(void) __attribute__((constructor)); | |
static void init(void) | |
{ | |
/* FIXME: this does only work if there's actually a symbol called 'main' */ | |
void *main_ptr = dlsym(RTLD_DEFAULT, "main"); | |
printf("main is at: %p\n", main_ptr); | |
} |