tst-tls6.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 modname[] = "tst-tlsmod2.so";
  16. int result = 0;
  17. int *foop;
  18. int *foop2;
  19. int (*fp) (int, int *);
  20. void *h;
  21. int i;
  22. int modid = -1;
  23. for (i = 0; i < 10; ++i)
  24. {
  25. h = dlopen (modname, RTLD_LAZY);
  26. if (h == NULL)
  27. {
  28. printf ("cannot open '%s': %s\n", modname, dlerror ());
  29. exit (1);
  30. }
  31. /* Dirty test code here: we peek into a private data structure.
  32. We make sure that the module gets assigned the same ID every
  33. time. The value of the first round is used. */
  34. #ifdef __UCLIBC__
  35. if (modid == -1)
  36. modid = ((struct dyn_elf *) h)->dyn->l_tls_modid;
  37. else if (((struct dyn_elf *)h)->dyn->l_tls_modid != (size_t) modid)
  38. {
  39. printf ("round %d: modid now %zu, initially %d\n",
  40. i,
  41. ((struct dyn_elf *)h)->dyn->l_tls_modid,
  42. modid);
  43. result = 1;
  44. }
  45. #else
  46. if (modid == -1)
  47. modid = ((struct link_map *) h)->l_tls_modid;
  48. else if (((struct link_map *) h)->l_tls_modid != modid)
  49. {
  50. printf ("round %d: modid now %zd, initially %d\n",
  51. i, ((struct link_map *) h)->l_tls_modid, modid);
  52. result = 1;
  53. }
  54. #endif
  55. foop = dlsym (h, "foo");
  56. if (foop == NULL)
  57. {
  58. printf ("cannot get symbol 'foo': %s\n", dlerror ());
  59. exit (1);
  60. }
  61. *foop = 42 + i;
  62. fp = dlsym (h, "in_dso");
  63. if (fp == NULL)
  64. {
  65. printf ("cannot get symbol 'in_dso': %s\n", dlerror ());
  66. exit (1);
  67. }
  68. result |= fp (42 + i, foop);
  69. foop2 = dlsym (h, "foo");
  70. if (foop2 == NULL)
  71. {
  72. printf ("cannot get symbol 'foo' the second time: %s\n", dlerror ());
  73. exit (1);
  74. }
  75. if (foop != foop2)
  76. {
  77. puts ("address of 'foo' different the second time");
  78. result = 1;
  79. }
  80. else if (*foop != 16)
  81. {
  82. puts ("foo != 16");
  83. result = 1;
  84. }
  85. dlclose (h);
  86. }
  87. return result;
  88. #else
  89. return 0;
  90. #endif
  91. }
  92. #include "../test-skeleton.c"