tst-tls6.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 link_map *)((struct dyn_elf *)h)->dyn)->l_tls_modid;
  37. else if (((struct link_map *)((struct dyn_elf *)h)->dyn)->l_tls_modid
  38. != (size_t) modid)
  39. {
  40. printf ("round %d: modid now %zu, initially %d\n",
  41. i,
  42. ((struct link_map *)((struct dyn_elf *)h)->dyn)->l_tls_modid,
  43. modid);
  44. result = 1;
  45. }
  46. #else
  47. if (modid == -1)
  48. modid = ((struct link_map *) h)->l_tls_modid;
  49. else if (((struct link_map *) h)->l_tls_modid != modid)
  50. {
  51. printf ("round %d: modid now %zd, initially %d\n",
  52. i, ((struct link_map *) h)->l_tls_modid, modid);
  53. result = 1;
  54. }
  55. #endif
  56. foop = dlsym (h, "foo");
  57. if (foop == NULL)
  58. {
  59. printf ("cannot get symbol 'foo': %s\n", dlerror ());
  60. exit (1);
  61. }
  62. *foop = 42 + i;
  63. fp = dlsym (h, "in_dso");
  64. if (fp == NULL)
  65. {
  66. printf ("cannot get symbol 'in_dso': %s\n", dlerror ());
  67. exit (1);
  68. }
  69. result |= fp (42 + i, foop);
  70. foop2 = dlsym (h, "foo");
  71. if (foop2 == NULL)
  72. {
  73. printf ("cannot get symbol 'foo' the second time: %s\n", dlerror ());
  74. exit (1);
  75. }
  76. if (foop != foop2)
  77. {
  78. puts ("address of 'foo' different the second time");
  79. result = 1;
  80. }
  81. else if (*foop != 16)
  82. {
  83. puts ("foo != 16");
  84. result = 1;
  85. }
  86. dlclose (h);
  87. }
  88. return result;
  89. #else
  90. return 0;
  91. #endif
  92. }
  93. #include "../test-skeleton.c"