1#include <dlfcn.h>
2#include <stdio.h>
3#include <gnu/lib-names.h>
4
5
6static int cnt;
7
8static void
9__attribute ((constructor))
10constr (void)
11{
12 ++cnt;
13}
14
15
16int
17foo (Lmid_t ns2)
18{
19 void *h = dlopen (LIBC_SO, RTLD_LAZY|RTLD_NOLOAD);
20 if (h == NULL)
21 {
22 printf (format: "cannot get handle for %s: %s\n", LIBC_SO, dlerror ());
23 return 1;
24 }
25
26 Lmid_t ns = -10;
27 if (dlinfo (handle: h, request: RTLD_DI_LMID, arg: &ns) != 0)
28 {
29 printf (format: "dlinfo for %s in %s failed: %s\n",
30 LIBC_SO, __func__, dlerror ());
31 return 1;
32 }
33
34 if (ns != ns2)
35 {
36 printf (format: "namespace for %s not LM_ID_BASE\n", LIBC_SO);
37 return 1;
38 }
39
40 if (dlclose (handle: h) != 0)
41 {
42 printf (format: "dlclose for %s in %s failed: %s\n",
43 LIBC_SO, __func__, dlerror ());
44 return 1;
45 }
46
47 if (cnt == 0)
48 {
49 puts (s: "constructor did not run");
50 return 1;
51 }
52 else if (cnt != 1)
53 {
54 puts (s: "constructor did not run exactly once");
55 return 1;
56 }
57
58 return 0;
59}
60

source code of glibc/elf/tst-dlmopen1mod.c