tststatic2.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <dlfcn.h>
  2. #include <link.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <gnu/lib-names.h>
  7. #include <first-versions.h>
  8. static int
  9. do_test (void)
  10. {
  11. void *handle = dlopen ("modstatic2-nonexistent.so", RTLD_LAZY);
  12. if (handle == NULL)
  13. printf ("nonexistent: %s\n", dlerror ());
  14. else
  15. exit (1);
  16. handle = dlopen ("modstatic2.so", RTLD_LAZY);
  17. if (handle == NULL)
  18. {
  19. printf ("%s\n", dlerror ());
  20. exit (1);
  21. }
  22. int (*test) (FILE *, int);
  23. test = dlsym (handle, "test");
  24. if (test == NULL)
  25. {
  26. printf ("%s\n", dlerror ());
  27. exit (1);
  28. }
  29. Dl_info info;
  30. int res = dladdr (test, &info);
  31. if (res == 0)
  32. {
  33. puts ("dladdr returned 0");
  34. exit (1);
  35. }
  36. else
  37. {
  38. if (strstr (info.dli_fname, "modstatic2.so") == NULL
  39. || strcmp (info.dli_sname, "test") != 0)
  40. {
  41. printf ("fname %s sname %s\n", info.dli_fname, info.dli_sname);
  42. exit (1);
  43. }
  44. if (info.dli_saddr != (void *) test)
  45. {
  46. printf ("saddr %p != test %p\n", info.dli_saddr, test);
  47. exit (1);
  48. }
  49. }
  50. ElfW(Sym) *sym;
  51. void *symp;
  52. res = dladdr1 (test, &info, &symp, RTLD_DL_SYMENT);
  53. if (res == 0)
  54. {
  55. puts ("dladdr1 returned 0");
  56. exit (1);
  57. }
  58. else
  59. {
  60. if (strstr (info.dli_fname, "modstatic2.so") == NULL
  61. || strcmp (info.dli_sname, "test") != 0)
  62. {
  63. printf ("fname %s sname %s\n", info.dli_fname, info.dli_sname);
  64. exit (1);
  65. }
  66. if (info.dli_saddr != (void *) test)
  67. {
  68. printf ("saddr %p != test %p\n", info.dli_saddr, test);
  69. exit (1);
  70. }
  71. sym = symp;
  72. if (sym == NULL)
  73. {
  74. puts ("sym == NULL\n");
  75. exit (1);
  76. }
  77. if (ELF32_ST_BIND (sym->st_info) != STB_GLOBAL
  78. || ELF32_ST_VISIBILITY (sym->st_other) != STV_DEFAULT)
  79. {
  80. printf ("bind %d visibility %d\n",
  81. (int) ELF32_ST_BIND (sym->st_info),
  82. (int) ELF32_ST_VISIBILITY (sym->st_other));
  83. exit (1);
  84. }
  85. }
  86. Lmid_t lmid;
  87. res = dlinfo (handle, RTLD_DI_LMID, &lmid);
  88. if (res != 0)
  89. {
  90. printf ("dlinfo returned %d %s\n", res, dlerror ());
  91. exit (1);
  92. }
  93. else if (lmid != LM_ID_BASE)
  94. {
  95. printf ("lmid %d != %d\n", (int) lmid, (int) LM_ID_BASE);
  96. exit (1);
  97. }
  98. res = test (stdout, 2);
  99. if (res != 4)
  100. {
  101. printf ("Got %i, expected 4\n", res);
  102. exit (1);
  103. }
  104. void *handle2 = dlopen (LIBDL_SO, RTLD_LAZY);
  105. if (handle2 == NULL)
  106. {
  107. printf ("libdl.so: %s\n", dlerror ());
  108. exit (1);
  109. }
  110. /* _exit is very unlikely to receive a second symbol version. */
  111. void *exit_ptr = dlvsym (handle2, "_exit", FIRST_VERSION_libc__exit_STRING);
  112. if (exit_ptr == NULL)
  113. {
  114. printf ("dlvsym: %s\n", dlerror ());
  115. exit (1);
  116. }
  117. if (exit_ptr != dlsym (handle2, "_exit"))
  118. {
  119. printf ("dlvsym for _exit does not match dlsym\n");
  120. exit (1);
  121. }
  122. void *(*dlsymfn) (void *, const char *);
  123. dlsymfn = dlsym (handle2, "dlsym");
  124. if (dlsymfn == NULL)
  125. {
  126. printf ("dlsym \"dlsym\": %s\n", dlerror ());
  127. exit (1);
  128. }
  129. void *test2 = dlsymfn (handle, "test");
  130. if (test2 == NULL)
  131. {
  132. printf ("%s\n", dlerror ());
  133. exit (1);
  134. }
  135. else if (test2 != (void *) test)
  136. {
  137. printf ("test %p != test2 %p\n", test, test2);
  138. exit (1);
  139. }
  140. dlclose (handle2);
  141. dlclose (handle);
  142. handle = dlmopen (LM_ID_BASE, "modstatic2.so", RTLD_LAZY);
  143. if (handle == NULL)
  144. {
  145. printf ("%s\n", dlerror ());
  146. exit (1);
  147. }
  148. dlclose (handle);
  149. handle = dlmopen (LM_ID_NEWLM, "modstatic2.so", RTLD_LAZY);
  150. if (handle == NULL)
  151. printf ("LM_ID_NEWLM: %s\n", dlerror ());
  152. else
  153. {
  154. puts ("LM_ID_NEWLM unexpectedly succeeded");
  155. exit (1);
  156. }
  157. return 0;
  158. }
  159. #define TEST_FUNCTION do_test ()
  160. #include "../test-skeleton.c"