tst-tls8.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 link_map *)((struct dyn_elf *)h1)->dyn)->l_tls_modid;
  40. else if (((struct link_map *)((struct dyn_elf *)h1)->dyn)->l_tls_modid
  41. != (size_t) modid1)
  42. {
  43. printf ("round %d: modid now %zd, initially %zd\n",
  44. i,
  45. ((struct link_map *)((struct dyn_elf *)h1)->dyn)->l_tls_modid,
  46. modid1);
  47. result = 1;
  48. }
  49. #else
  50. if (modid1 == (size_t) -1)
  51. modid1 = ((struct link_map *) h1)->l_tls_modid;
  52. else if (((struct link_map *) h1)->l_tls_modid != modid1)
  53. {
  54. printf ("round %d: modid now %zd, initially %zd\n",
  55. i, ((struct link_map *) h1)->l_tls_modid, modid1);
  56. result = 1;
  57. }
  58. #endif
  59. fp1 = dlsym (h1, "in_dso2");
  60. if (fp1 == NULL)
  61. {
  62. printf ("cannot get symbol 'in_dso2' in %s\n", modname1);
  63. exit (1);
  64. }
  65. result |= fp1 ();
  66. h2 = dlopen (modname2, RTLD_LAZY);
  67. if (h2 == NULL)
  68. {
  69. printf ("cannot open '%s': %s\n", modname2, dlerror ());
  70. exit (1);
  71. }
  72. /* Dirty test code here: we peek into a private data structure.
  73. We make sure that the module gets assigned the same ID every
  74. time. The value of the first round is used. */
  75. #ifdef __UCLIBC__
  76. if (modid2 == (size_t) -1)
  77. modid2 = ((struct link_map *)((struct dyn_elf *)h1)->dyn)->l_tls_modid;
  78. else if (((struct link_map *)((struct dyn_elf *)h1)->dyn)->l_tls_modid
  79. != (size_t) modid2)
  80. {
  81. printf ("round %d: modid now %zd, initially %zd\n",
  82. i,
  83. ((struct link_map *)((struct dyn_elf *)h1)->dyn)->l_tls_modid,
  84. modid2);
  85. result = 1;
  86. }
  87. #else
  88. if (modid2 == (size_t) -1)
  89. modid2 = ((struct link_map *) h1)->l_tls_modid;
  90. else if (((struct link_map *) h1)->l_tls_modid != modid2)
  91. {
  92. printf ("round %d: modid now %zd, initially %zd\n",
  93. i, ((struct link_map *) h1)->l_tls_modid, modid2);
  94. result = 1;
  95. }
  96. #endif
  97. bazp = dlsym (h2, "baz");
  98. if (bazp == NULL)
  99. {
  100. printf ("cannot get symbol 'baz' in %s\n", modname2);
  101. exit (1);
  102. }
  103. *bazp = 42 + i;
  104. fp2 = dlsym (h2, "in_dso");
  105. if (fp2 == NULL)
  106. {
  107. printf ("cannot get symbol 'in_dso' in %s\n", modname2);
  108. exit (1);
  109. }
  110. result |= fp2 (42 + i, bazp);
  111. dlclose (h1);
  112. dlclose (h2);
  113. h1 = dlopen (modname1, RTLD_LAZY);
  114. if (h1 == NULL)
  115. {
  116. printf ("cannot open '%s': %s\n", modname1, dlerror ());
  117. exit (1);
  118. }
  119. /* Dirty test code here: we peek into a private data structure.
  120. We make sure that the module gets assigned the same ID every
  121. time. The value of the first round is used. */
  122. #ifdef __UCLIBC__
  123. if (((struct link_map *)((struct dyn_elf *)h1)->dyn)->l_tls_modid
  124. != modid1)
  125. {
  126. printf ("round %d: modid now %zd, initially %zd\n",
  127. i,
  128. ((struct link_map *)((struct dyn_elf *)h1)->dyn)->l_tls_modid,
  129. modid1);
  130. result = 1;
  131. }
  132. #else
  133. if (((struct link_map *) h1)->l_tls_modid != modid1)
  134. {
  135. printf ("round %d: modid now %zd, initially %zd\n",
  136. i, ((struct link_map *) h1)->l_tls_modid, modid1);
  137. result = 1;
  138. }
  139. #endif
  140. fp1 = dlsym (h1, "in_dso2");
  141. if (fp1 == NULL)
  142. {
  143. printf ("cannot get symbol 'in_dso2' in %s\n", modname1);
  144. exit (1);
  145. }
  146. result |= fp1 ();
  147. h2 = dlopen (modname2, RTLD_LAZY);
  148. if (h2 == NULL)
  149. {
  150. printf ("cannot open '%s': %s\n", modname2, dlerror ());
  151. exit (1);
  152. }
  153. /* Dirty test code here: we peek into a private data structure.
  154. We make sure that the module gets assigned the same ID every
  155. time. The value of the first round is used. */
  156. #ifdef __UCLIBC__
  157. if (((struct link_map *)((struct dyn_elf *)h1)->dyn)->l_tls_modid
  158. != modid2)
  159. {
  160. printf ("round %d: modid now %zd, initially %zd\n",
  161. i,
  162. ((struct link_map *)((struct dyn_elf *)h1)->dyn)->l_tls_modid,
  163. modid2);
  164. result = 1;
  165. }
  166. #else
  167. if (((struct link_map *) h1)->l_tls_modid != modid2)
  168. {
  169. printf ("round %d: modid now %zd, initially %zd\n",
  170. i, ((struct link_map *) h1)->l_tls_modid, modid2);
  171. result = 1;
  172. }
  173. #endif
  174. bazp = dlsym (h2, "baz");
  175. if (bazp == NULL)
  176. {
  177. printf ("cannot get symbol 'baz' in %s\n", modname2);
  178. exit (1);
  179. }
  180. *bazp = 62 + i;
  181. fp2 = dlsym (h2, "in_dso");
  182. if (fp2 == NULL)
  183. {
  184. printf ("cannot get symbol 'in_dso' in %s\n", modname2);
  185. exit (1);
  186. }
  187. result |= fp2 (62 + i, bazp);
  188. /* This time the dlclose calls are in reverse order. */
  189. dlclose (h2);
  190. dlclose (h1);
  191. }
  192. return result;
  193. #else
  194. return 0;
  195. #endif
  196. }
  197. #include "../test-skeleton.c"