1#include <dlfcn.h>
2#include <stdio.h>
3#include <malloc.h>
4
5int
6main (void)
7{
8#ifdef M_PERTURB
9 mallopt (M_PERTURB, val: 0xaa);
10#endif
11
12 void *h;
13 int (*fn) (int);
14 h = dlopen (file: "unload4mod1.so", RTLD_LAZY);
15 if (h == NULL)
16 {
17 puts (s: "1st dlopen failed");
18 return 1;
19 }
20 fn = dlsym (handle: h, name: "foo");
21 if (fn == NULL)
22 {
23 puts (s: "dlsym failed");
24 return 1;
25 }
26 int n = fn (10);
27 if (n != 28)
28 {
29 printf (format: "foo (10) returned %d != 28\n", n);
30 return 1;
31 }
32 dlclose (handle: h);
33 h = dlopen (file: "unload4mod3.so", RTLD_LAZY);
34 fn = dlsym (handle: h, name: "mod3fn2");
35 if (fn == NULL)
36 {
37 puts (s: "second dlsym failed");
38 return 1;
39 }
40 n = fn (10);
41 if (n != 22)
42 {
43 printf (format: "mod3fn2 (10) returned %d != 22\n", n);
44 return 1;
45 }
46 dlclose (handle: h);
47 return 0;
48}
49

source code of glibc/elf/unload4.c