1 | #include <dlfcn.h> |
---|---|
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
4 | |
5 | #include <link.h> |
6 | |
7 | |
8 | static int |
9 | do_test (void) |
10 | { |
11 | static const char modname[] = "tst-tlsmod2.so"; |
12 | int result = 0; |
13 | int *foop; |
14 | int *foop2; |
15 | int (*fp) (int, int *); |
16 | void *h; |
17 | int i; |
18 | int modid = -1; |
19 | |
20 | for (i = 0; i < 10; ++i) |
21 | { |
22 | h = dlopen (file: modname, RTLD_LAZY); |
23 | if (h == NULL) |
24 | { |
25 | printf (format: "cannot open '%s': %s\n", modname, dlerror ()); |
26 | exit (status: 1); |
27 | } |
28 | |
29 | /* Dirty test code here: we peek into a private data structure. |
30 | We make sure that the module gets assigned the same ID every |
31 | time. The value of the first round is used. */ |
32 | if (modid == -1) |
33 | modid = ((struct link_map *) h)->l_tls_modid; |
34 | else if (((struct link_map *) h)->l_tls_modid != modid) |
35 | { |
36 | printf (format: "round %d: modid now %zd, initially %d\n", |
37 | i, ((struct link_map *) h)->l_tls_modid, modid); |
38 | result = 1; |
39 | } |
40 | |
41 | foop = dlsym (handle: h, name: "foo"); |
42 | if (foop == NULL) |
43 | { |
44 | printf (format: "cannot get symbol 'foo': %s\n", dlerror ()); |
45 | exit (status: 1); |
46 | } |
47 | |
48 | *foop = 42 + i; |
49 | |
50 | fp = dlsym (handle: h, name: "in_dso"); |
51 | if (fp == NULL) |
52 | { |
53 | printf (format: "cannot get symbol 'in_dso': %s\n", dlerror ()); |
54 | exit (status: 1); |
55 | } |
56 | |
57 | result |= fp (42 + i, foop); |
58 | |
59 | foop2 = dlsym (handle: h, name: "foo"); |
60 | if (foop2 == NULL) |
61 | { |
62 | printf (format: "cannot get symbol 'foo' the second time: %s\n", dlerror ()); |
63 | exit (status: 1); |
64 | } |
65 | |
66 | if (foop != foop2) |
67 | { |
68 | puts (s: "address of 'foo' different the second time"); |
69 | result = 1; |
70 | } |
71 | else if (*foop != 16) |
72 | { |
73 | puts (s: "foo != 16"); |
74 | result = 1; |
75 | } |
76 | |
77 | dlclose (handle: h); |
78 | } |
79 | |
80 | return result; |
81 | } |
82 | |
83 | |
84 | #include <support/test-driver.c> |
85 |