foobar include stdio void foobar_run puts foobar_run exported unexport

 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
==> foobar.c <==
#include <stdio.h>
void foobar_run()
{
puts("foobar_run");
exported();
unexported();
}
/* vim:set tabstop=4 softtabstop=4 shiftwidth=4: */
/* vim:set foldmethod=marker foldlevel=32 foldmarker={,}: */
==> main.c <==
#include <stdio.h>
#include <dlfcn.h>
#include <assert.h>
void unexported()
{
assert(0 == 1);
}
void exported()
{
puts("expoted");
}
typedef void (*foonction)(void);
int main(int argc, char **argv)
{
puts("main");
void *lib = dlopen("./foobar.so", RTLD_LOCAL | RTLD_NOW);
printf("lib: %p, error: %s\n", lib, dlerror());
foonction run = dlsym(lib, "foobar_run");
printf("run: %p\n", run);
run();
return 0;
}
/* vim:set tabstop=4 softtabstop=4 shiftwidth=4: */
/* vim:set foldmethod=marker foldlevel=32 foldmarker={,}: */
==> main-exports <==
{
exported;
};
==> Makefile <==
all: main foobar.so
main: main.c main-exports
gcc main.c -Wl,--dynamic-list=main-exports -ldl -o main
foobar.so: foobar.c
gcc foobar.c -shared -o foobar.so