-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_library.c
43 lines (35 loc) · 1.18 KB
/
shared_library.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
/* How to compile
start:
gcc -Wall -Wextra -Werror -fpic -shared -std=gnu11 -o system_allocator.so system_allocator.c
gcc -Wall -Wextra -Werror -std=gnu11 shared_library.c -o shared_library -ldl
*/
// declare shared library functions
void *(*my_malloc)(size_t size);
void (*my_free)(void *memory);
int main() {
// load shared library (.so)
void *sl_handle = dlopen("./system_allocator.so", RTLD_LAZY); // RTLD_LAZA means loading funcitons when needed. RTLD_LOCAL...
if(sl_handle == NULL) {
printf("Error: Could not load library (%s)\n", dlerror());
exit(EXIT_FAILURE);
}
my_malloc = dlsym(sl_handle, "my_malloc");
my_free = dlsym(sl_handle, "my_free");
if(my_malloc == NULL) {
printf("Error at loading function pointer my_malloc.\n");
exit(EXIT_FAILURE);
}
if(my_free == NULL) {
printf("Error at loading function pointer my_free.\n");
exit(EXIT_FAILURE);
}
// call shared library functions
void *memory = my_malloc(1024);
my_free(memory);
// close shared library
dlclose(sl_handle);
return EXIT_SUCCESS;
}