tst-tls8.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <dlfcn.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <link.h>
  5. static int
  6. do_test (void)
  7. {
  8. static const char modname1[] = "$ORIGIN/tst-tlsmod3.so";
  9. static const char modname2[] = "$ORIGIN/tst-tlsmod4.so";
  10. int result = 0;
  11. int (*fp1) (void);
  12. int (*fp2) (int, int *);
  13. void *h1;
  14. void *h2;
  15. int i;
  16. size_t modid1 = (size_t) -1;
  17. size_t modid2 = (size_t) -1;
  18. int *bazp;
  19. for (i = 0; i < 10; ++i)
  20. {
  21. h1 = dlopen (modname1, RTLD_LAZY);
  22. if (h1 == NULL)
  23. {
  24. printf ("cannot open '%s': %s\n", modname1, dlerror ());
  25. exit (1);
  26. }
  27. /* Dirty test code here: we peek into a private data structure.
  28. We make sure that the module gets assigned the same ID every
  29. time. The value of the first round is used. */
  30. if (modid1 == (size_t) -1)
  31. modid1 = ((struct link_map *) h1)->l_tls_modid;
  32. else if (((struct link_map *) h1)->l_tls_modid != modid1)
  33. {
  34. printf ("round %d: modid now %zd, initially %zd\n",
  35. i, ((struct link_map *) h1)->l_tls_modid, modid1);
  36. result = 1;
  37. }
  38. fp1 = dlsym (h1, "in_dso2");
  39. if (fp1 == NULL)
  40. {
  41. printf ("cannot get symbol 'in_dso2' in %s\n", modname1);
  42. exit (1);
  43. }
  44. result |= fp1 ();
  45. h2 = dlopen (modname2, RTLD_LAZY);
  46. if (h2 == NULL)
  47. {
  48. printf ("cannot open '%s': %s\n", modname2, dlerror ());
  49. exit (1);
  50. }
  51. /* Dirty test code here: we peek into a private data structure.
  52. We make sure that the module gets assigned the same ID every
  53. time. The value of the first round is used. */
  54. if (modid2 == (size_t) -1)
  55. modid2 = ((struct link_map *) h1)->l_tls_modid;
  56. else if (((struct link_map *) h1)->l_tls_modid != modid2)
  57. {
  58. printf ("round %d: modid now %zd, initially %zd\n",
  59. i, ((struct link_map *) h1)->l_tls_modid, modid2);
  60. result = 1;
  61. }
  62. bazp = dlsym (h2, "baz");
  63. if (bazp == NULL)
  64. {
  65. printf ("cannot get symbol 'baz' in %s\n", modname2);
  66. exit (1);
  67. }
  68. *bazp = 42 + i;
  69. fp2 = dlsym (h2, "in_dso");
  70. if (fp2 == NULL)
  71. {
  72. printf ("cannot get symbol 'in_dso' in %s\n", modname2);
  73. exit (1);
  74. }
  75. result |= fp2 (42 + i, bazp);
  76. dlclose (h1);
  77. dlclose (h2);
  78. h1 = dlopen (modname1, RTLD_LAZY);
  79. if (h1 == NULL)
  80. {
  81. printf ("cannot open '%s': %s\n", modname1, dlerror ());
  82. exit (1);
  83. }
  84. /* Dirty test code here: we peek into a private data structure.
  85. We make sure that the module gets assigned the same ID every
  86. time. The value of the first round is used. */
  87. if (((struct link_map *) h1)->l_tls_modid != modid1)
  88. {
  89. printf ("round %d: modid now %zd, initially %zd\n",
  90. i, ((struct link_map *) h1)->l_tls_modid, modid1);
  91. result = 1;
  92. }
  93. fp1 = dlsym (h1, "in_dso2");
  94. if (fp1 == NULL)
  95. {
  96. printf ("cannot get symbol 'in_dso2' in %s\n", modname1);
  97. exit (1);
  98. }
  99. result |= fp1 ();
  100. h2 = dlopen (modname2, RTLD_LAZY);
  101. if (h2 == NULL)
  102. {
  103. printf ("cannot open '%s': %s\n", modname2, dlerror ());
  104. exit (1);
  105. }
  106. /* Dirty test code here: we peek into a private data structure.
  107. We make sure that the module gets assigned the same ID every
  108. time. The value of the first round is used. */
  109. if (((struct link_map *) h1)->l_tls_modid != modid2)
  110. {
  111. printf ("round %d: modid now %zd, initially %zd\n",
  112. i, ((struct link_map *) h1)->l_tls_modid, modid2);
  113. result = 1;
  114. }
  115. bazp = dlsym (h2, "baz");
  116. if (bazp == NULL)
  117. {
  118. printf ("cannot get symbol 'baz' in %s\n", modname2);
  119. exit (1);
  120. }
  121. *bazp = 62 + i;
  122. fp2 = dlsym (h2, "in_dso");
  123. if (fp2 == NULL)
  124. {
  125. printf ("cannot get symbol 'in_dso' in %s\n", modname2);
  126. exit (1);
  127. }
  128. result |= fp2 (62 + i, bazp);
  129. /* This time the dlclose calls are in reverse order. */
  130. dlclose (h2);
  131. dlclose (h1);
  132. }
  133. return result;
  134. }
  135. #include <support/test-driver.c>