Skip to content

Instantly share code, notes, and snippets.

@n1tehawk
Created October 21, 2016 09:25
Show Gist options
  • Save n1tehawk/9f9ac04936c651b0220ad237cd38aaa8 to your computer and use it in GitHub Desktop.
Save n1tehawk/9f9ac04936c651b0220ad237cd38aaa8 to your computer and use it in GitHub Desktop.
Dynamic symbol resolution from a static binary
/*
* Dynamic symbol resolution from a static binary:
*
* Windows: gcc -o foobar.exe -Wl,-export-all-symbols foobar.c && ./foobar
* Linux: gcc -o foobar -rdynamic -ldl foobar.c && ./foobar
*/
#ifdef _WIN32
#include <windows.h>
#else
#define _GNU_SOURCE
#include <dlfcn.h>
#endif
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *symbol = "main";
#ifdef _WIN32
void *addr = GetProcAddress(NULL, symbol);
#else
void *addr = dlsym(RTLD_DEFAULT, symbol);
#endif
if (!addr) {
printf("FAILED to resolve symbol '%s'\n", symbol);
return EXIT_FAILURE;
}
printf("symbol '%s' successfully resolved to %p\n", symbol, addr);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment