tst-tls8.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include <dlfcn.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <tls.h>
  5. #include <link.h>
  6. #ifdef __UCLIBC__
  7. #include "dl-elf.h"
  8. #include "dl-hash.h"
  9. #endif
  10. #define TEST_FUNCTION do_test ()
  11. static int
  12. do_test (void)
  13. {
  14. #ifdef USE_TLS
  15. static const char modname1[] = "tst-tlsmod3.so";
  16. static const char modname2[] = "tst-tlsmod4.so";
  17. int result = 0;
  18. int (*fp1) (void);
  19. int (*fp2) (int, int *);
  20. void *h1;
  21. void *h2;
  22. int i;
  23. size_t modid1 = (size_t) -1;
  24. size_t modid2 = (size_t) -1;
  25. int *bazp;
  26. for (i = 0; i < 10; ++i)
  27. {
  28. h1 = dlopen (modname1, RTLD_LAZY);
  29. if (h1 == NULL)
  30. {
  31. printf ("cannot open '%s': %s\n", modname1, dlerror ());
  32. exit (1);
  33. }
  34. /* Dirty test code here: we peek into a private data structure.
  35. We make sure that the module gets assigned the same ID every
  36. time. The value of the first round is used. */
  37. #ifdef __UCLIBC__
  38. if (modid1 == (size_t) -1)
  39. modid1 = ((struct dyn_elf *) h1)->dyn->l_tls_modid;
  40. else if (((struct dyn_elf *)h1)->dyn->l_tls_modid != (size_t) modid1)
  41. {
  42. printf ("round %d: modid now %zd, initially %zd\n",
  43. i,
  44. ((struct dyn_elf *)h1)->dyn->l_tls_modid,
  45. modid1);
  46. result = 1;
  47. }
  48. #else
  49. if (modid1 == (size_t) -1)
  50. modid1 = ((struct link_map *) h1)->l_tls_modid;
  51. else if (((struct link_map *) h1)->l_tls_modid != modid1)
  52. {
  53. printf ("round %d: modid now %zd, initially %zd\n",
  54. i, ((struct link_map *) h1)->l_tls_modid, modid1);
  55. result = 1;
  56. }
  57. #endif
  58. fp1 = dlsym (h1, "in_dso2");
  59. if (fp1 == NULL)
  60. {
  61. printf ("cannot get symbol 'in_dso2' in %s\n", modname1);
  62. exit (1);
  63. }
  64. result |= fp1 ();
  65. h2 = dlopen (modname2, RTLD_LAZY);
  66. if (h2 == NULL)
  67. {
  68. printf ("cannot open '%s': %s\n", modname2, dlerror ());
  69. exit (1);
  70. }
  71. /* Dirty test code here: we peek into a private data structure.
  72. We make sure that the module gets assigned the same ID every
  73. time. The value of the first round is used. */
  74. #ifdef __UCLIBC__
  75. if (modid2 == (size_t) -1)
  76. modid2 = ((struct dyn_elf *)h2)->dyn->l_tls_modid;
  77. else if (((struct dyn_elf *)h2)->dyn->l_tls_modid
  78. != (size_t) modid2)
  79. {
  80. printf ("round %d: modid now %zd, initially %zd\n",
  81. i,
  82. ((struct dyn_elf *)h2)->dyn->l_tls_modid,
  83. modid2);
  84. result = 1;
  85. }
  86. #else
  87. if (modid2 == (size_t) -1)
  88. modid2 = ((struct link_map *) h2)->l_tls_modid;
  89. else if (((struct link_map *) h2)->l_tls_modid != modid2)
  90. {
  91. printf ("round %d: modid now %zd, initially %zd\n",
  92. i, ((struct link_map *) h2)->l_tls_modid, modid2);
  93. result = 1;
  94. }
  95. #endif
  96. bazp = dlsym (h2, "baz");
  97. if (bazp == NULL)
  98. {
  99. printf ("cannot get symbol 'baz' in %s\n", modname2);
  100. exit (1);
  101. }
  102. *bazp = 42 + i;
  103. fp2 = dlsym (h2, "in_dso");
  104. if (fp2 == NULL)
  105. {
  106. printf ("cannot get symbol 'in_dso' in %s\n", modname2);
  107. exit (1);
  108. }
  109. result |= fp2 (42 + i, bazp);
  110. dlclose (h1);
  111. dlclose (h2);
  112. h1 = dlopen (modname1, RTLD_LAZY);
  113. if (h1 == NULL)
  114. {
  115. printf ("cannot open '%s': %s\n", modname1, dlerror ());
  116. exit (1);
  117. }
  118. /* Dirty test code here: we peek into a private data structure.
  119. We make sure that the module gets assigned the same ID every
  120. time. The value of the first round is used. */
  121. #ifdef __UCLIBC__
  122. if (((struct dyn_elf *)h1)->dyn->l_tls_modid
  123. != modid1)
  124. {
  125. printf ("round %d: modid now %zd, initially %zd\n",
  126. i,
  127. ((struct dyn_elf *)h1)->dyn->l_tls_modid,
  128. modid1);
  129. result = 1;
  130. }
  131. #else
  132. if (((struct link_map *) h1)->l_tls_modid != modid1)
  133. {
  134. printf ("round %d: modid now %zd, initially %zd\n",
  135. i, ((struct link_map *) h1)->l_tls_modid, modid1);
  136. result = 1;
  137. }
  138. #endif
  139. fp1 = dlsym (h1, "in_dso2");
  140. if (fp1 == NULL)
  141. {
  142. printf ("cannot get symbol 'in_dso2' in %s\n", modname1);
  143. exit (1);
  144. }
  145. result |= fp1 ();
  146. h2 = dlopen (modname2, RTLD_LAZY);
  147. if (h2 == NULL)
  148. {
  149. printf ("cannot open '%s': %s\n", modname2, dlerror ());
  150. exit (1);
  151. }
  152. /* Dirty test code here: we peek into a private data structure.
  153. We make sure that the module gets assigned the same ID every
  154. time. The value of the first round is used. */
  155. #ifdef __UCLIBC__
  156. if (((struct dyn_elf *)h2)->dyn->l_tls_modid
  157. != modid2)
  158. {
  159. printf ("round %d: modid now %zd, initially %zd\n",
  160. i,
  161. ((struct dyn_elf *)h2)->dyn->l_tls_modid,
  162. modid2);
  163. result = 1;
  164. }
  165. #else
  166. if (((struct link_map *) h2)->l_tls_modid != modid2)
  167. {
  168. printf ("round %d: modid now %zd, initially %zd\n",
  169. i, ((struct link_map *) h2)->l_tls_modid, modid2);
  170. result = 1;
  171. }
  172. #endif
  173. bazp = dlsym (h2, "baz");
  174. if (bazp == NULL)
  175. {
  176. printf ("cannot get symbol 'baz' in %s\n", modname2);
  177. exit (1);
  178. }
  179. *bazp = 62 + i;
  180. fp2 = dlsym (h2, "in_dso");
  181. if (fp2 == NULL)
  182. {
  183. printf ("cannot get symbol 'in_dso' in %s\n", modname2);
  184. exit (1);
  185. }
  186. result |= fp2 (62 + i, bazp);
  187. /* This time the dlclose calls are in reverse order. */
  188. dlclose (h2);
  189. dlclose (h1);
  190. }
  191. return result;
  192. #else
  193. return 0;
  194. #endif
  195. }
  196. #include "../test-skeleton.c"