Skip to content

Instantly share code, notes, and snippets.

@weissi
Created March 15, 2015 01:11
Show Gist options
  • Save weissi/47de0da6ee83621f1e54 to your computer and use it in GitHub Desktop.
Save weissi/47de0da6ee83621f1e54 to your computer and use it in GitHub Desktop.

Building

clang -shared -o /tmp/test-dylib.dylib test-dylib.c

Running

export DYLD_INSERT_LIBRARIES=/tmp/test-dylib.dylib
# test it
zsh -c 'echo Hello from zsh'

Sample output

# 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment