Skip to content

Instantly share code, notes, and snippets.

@lattera
Created August 14, 2015 16:12
Show Gist options
  • Save lattera/c07913190b0155094ac9 to your computer and use it in GitHub Desktop.
Save lattera/c07913190b0155094ac9 to your computer and use it in GitHub Desktop.
relro + dlopen
laptop-dev-01[shawn]:/home/shawn/tmp $ cat dlopen.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dlfcn.h>
int main(int argc, char *argv[])
{
void *handle;
if (argc < 3) {
printf("USAGE: %s /path/to/lib symname\n", argv[0]);
exit(1);
}
handle = dlopen(argv[1], RTLD_NOW | RTLD_GLOBAL);
if (!(handle)) {
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
printf("%s: %p\n", argv[2], dlsym(handle, argv[2]));
dlclose(handle);
return 0;
}
laptop-dev-01[shawn]:/home/shawn/tmp $ clang -Wl,-z,relro -Wl,-z,now -fPIE -pie -o dlopen dlopen.c
laptop-dev-01[shawn]:/home/shawn/tmp $ ./dlopen /lib/libthr.so.3 pthread_create
pthread_create: 0x4a7781dc080
laptop-dev-01[shawn]:/home/shawn/tmp $
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment