Created
October 21, 2016 09:25
-
-
Save n1tehawk/9f9ac04936c651b0220ad237cd38aaa8 to your computer and use it in GitHub Desktop.
Dynamic symbol resolution from a static binary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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