Created
March 12, 2020 16:58
-
-
Save UlisseMini/e9365e94cf61b89e36334bd36c34015e to your computer and use it in GitHub Desktop.
This file contains 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
int add(int x, int y) { return x + y; } | |
int main() { return 0; } |
This file contains 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
#include <dlfcn.h> | |
#include <stdio.h> | |
#include <errno.h> | |
#include <string.h> | |
// works, when i compile with gcc -shared | |
// #define PATH "/home/v/wcc/add.so" | |
// does not work, dlopen() returns NULL | |
#define PATH "/home/v/wcc/add" | |
int main(int argc, char const* argv[]) { | |
void *handle; | |
int (*fptr)(int, int); | |
handle = dlopen(PATH, RTLD_LOCAL | RTLD_NOW); | |
if (handle == NULL) { | |
printf("Failed to open " PATH ": %s\n", strerror(errno)); | |
return 1; | |
} | |
fptr = (int (*)(int, int))dlsym(handle, "add"); | |
if (fptr == NULL) { | |
printf("Failed to find the symbol 'add': %s\n", strerror(errno)); | |
dlclose(handle); | |
return 1; | |
} | |
int result = (*fptr)(10, 20); | |
printf("%d\n", result); | |
dlclose(handle); | |
return 0; | |
} |
This file contains 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
all: build run | |
build: | |
# Pretend I have no control over how add.c is compiled | |
gcc -Wall add.c -o add | |
# I have control over how I compile ./call | |
gcc -Wall -ldl call.c -o call | |
run: | |
./call |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment