tststatic4.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* Global object symbol access tests with a static executable (BZ #15022).
  2. Copyright (C) 2013-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <dlfcn.h>
  16. #include <stddef.h>
  17. #include <stdio.h>
  18. #define MAGIC0 0
  19. #define MAGIC1 0x5500ffaa
  20. #define MAGIC2 0xaaff0055
  21. #define MAGIC3 0xff55aa00
  22. /* Check the ability to access the global symbol object and then
  23. global-scope symbol access consistency via different mappings
  24. requested from a static executable. */
  25. static int
  26. do_test (void)
  27. {
  28. unsigned int (*initial_getfoo) (void);
  29. void (*initial_setfoo) (unsigned int);
  30. unsigned int (*global_getfoo) (void);
  31. void (*global_setfoo) (unsigned int);
  32. unsigned int (*local_getfoo) (void);
  33. void (*local_setfoo) (unsigned int);
  34. unsigned int *initial_foop;
  35. unsigned int *global_foop;
  36. unsigned int *local_foop;
  37. void *initial_handle;
  38. void *global_handle;
  39. void *local_handle;
  40. unsigned int foo;
  41. /* Try to map self. */
  42. initial_handle = dlopen (NULL, RTLD_LAZY | RTLD_GLOBAL);
  43. if (initial_handle == NULL)
  44. {
  45. printf ("dlopen [initial] (NULL): %s\n", dlerror ());
  46. return 1;
  47. }
  48. /* Make sure symbol lookups fail gracefully. */
  49. initial_foop = dlsym (initial_handle, "foo");
  50. if (initial_foop != NULL)
  51. {
  52. printf ("dlsym [initial] (foo): got %p, expected NULL\n", initial_foop);
  53. return 1;
  54. }
  55. initial_getfoo = dlsym (initial_handle, "getfoo");
  56. if (initial_getfoo != NULL)
  57. {
  58. printf ("dlsym [initial] (getfoo): got %p, expected NULL\n",
  59. initial_getfoo);
  60. return 1;
  61. }
  62. initial_setfoo = dlsym (initial_handle, "setfoo");
  63. if (initial_setfoo != NULL)
  64. {
  65. printf ("dlsym [initial] (setfoo): got %p, expected NULL\n",
  66. initial_setfoo);
  67. return 1;
  68. }
  69. /* Try to map a module into the global scope. */
  70. global_handle = dlopen ("modstatic3.so", RTLD_LAZY | RTLD_GLOBAL);
  71. if (global_handle == NULL)
  72. {
  73. printf ("dlopen [global] (modstatic3.so): %s\n", dlerror ());
  74. return 1;
  75. }
  76. /* Get at its symbols. */
  77. global_foop = dlsym (global_handle, "foo");
  78. if (global_foop == NULL)
  79. {
  80. printf ("dlsym [global] (foo): %s\n", dlerror ());
  81. return 1;
  82. }
  83. global_getfoo = dlsym (global_handle, "getfoo");
  84. if (global_getfoo == NULL)
  85. {
  86. printf ("dlsym [global] (getfoo): %s\n", dlerror ());
  87. return 1;
  88. }
  89. global_setfoo = dlsym (global_handle, "setfoo");
  90. if (global_setfoo == NULL)
  91. {
  92. printf ("dlsym [global] (setfoo): %s\n", dlerror ());
  93. return 1;
  94. }
  95. /* Try to map self again now. */
  96. local_handle = dlopen (NULL, RTLD_LAZY | RTLD_LOCAL);
  97. if (local_handle == NULL)
  98. {
  99. printf ("dlopen [local] (NULL): %s\n", dlerror ());
  100. return 1;
  101. }
  102. /* Make sure we can get at the previously loaded module's symbols
  103. via this handle too. */
  104. local_foop = dlsym (local_handle, "foo");
  105. if (local_foop == NULL)
  106. {
  107. printf ("dlsym [local] (foo): %s\n", dlerror ());
  108. return 1;
  109. }
  110. local_getfoo = dlsym (local_handle, "getfoo");
  111. if (local_getfoo == NULL)
  112. {
  113. printf ("dlsym [local] (getfoo): %s\n", dlerror ());
  114. return 1;
  115. }
  116. local_setfoo = dlsym (local_handle, "setfoo");
  117. if (local_setfoo == NULL)
  118. {
  119. printf ("dlsym [local] (setfoo): %s\n", dlerror ());
  120. return 1;
  121. }
  122. /* Make sure we can get at the previously loaded module's symbols
  123. via a handle that was obtained before the module was loaded too. */
  124. initial_foop = dlsym (initial_handle, "foo");
  125. if (initial_foop == NULL)
  126. {
  127. printf ("dlsym [initial] (foo): %s\n", dlerror ());
  128. return 1;
  129. }
  130. initial_getfoo = dlsym (initial_handle, "getfoo");
  131. if (initial_getfoo == NULL)
  132. {
  133. printf ("dlsym [initial] (getfoo): %s\n", dlerror ());
  134. return 1;
  135. }
  136. initial_setfoo = dlsym (initial_handle, "setfoo");
  137. if (initial_setfoo == NULL)
  138. {
  139. printf ("dlsym [initial] (setfoo): %s\n", dlerror ());
  140. return 1;
  141. }
  142. /* Make sure the view of the initial state is consistent. */
  143. foo = *initial_foop;
  144. if (foo != MAGIC0)
  145. {
  146. printf ("*foop [initial]: got %#x, expected %#x\n", foo, MAGIC0);
  147. return 1;
  148. }
  149. foo = *global_foop;
  150. if (foo != MAGIC0)
  151. {
  152. printf ("*foop [global]: got %#x, expected %#x\n", foo, MAGIC0);
  153. return 1;
  154. }
  155. foo = *local_foop;
  156. if (foo != MAGIC0)
  157. {
  158. printf ("*foop [local]: got %#x, expected %#x\n", foo, MAGIC0);
  159. return 1;
  160. }
  161. foo = initial_getfoo ();
  162. if (foo != MAGIC0)
  163. {
  164. printf ("getfoo [initial]: got %#x, expected %#x\n", foo, MAGIC0);
  165. return 1;
  166. }
  167. foo = global_getfoo ();
  168. if (foo != MAGIC0)
  169. {
  170. printf ("getfoo [global]: got %#x, expected %#x\n", foo, MAGIC0);
  171. return 1;
  172. }
  173. foo = local_getfoo ();
  174. if (foo != MAGIC0)
  175. {
  176. printf ("getfoo [local]: got %#x, expected %#x\n", foo, MAGIC0);
  177. return 1;
  178. }
  179. /* Likewise with a change to its state made through the first handle. */
  180. initial_setfoo (MAGIC1);
  181. foo = *initial_foop;
  182. if (foo != MAGIC1)
  183. {
  184. printf ("*foop [initial]: got %#x, expected %#x\n", foo, MAGIC1);
  185. return 1;
  186. }
  187. foo = *global_foop;
  188. if (foo != MAGIC1)
  189. {
  190. printf ("*foop [global]: got %#x, expected %#x\n", foo, MAGIC1);
  191. return 1;
  192. }
  193. foo = *local_foop;
  194. if (foo != MAGIC1)
  195. {
  196. printf ("*foop [local]: got %#x, expected %#x\n", foo, MAGIC1);
  197. return 1;
  198. }
  199. foo = initial_getfoo ();
  200. if (foo != MAGIC1)
  201. {
  202. printf ("getfoo [initial]: got %#x, expected %#x\n", foo, MAGIC1);
  203. return 1;
  204. }
  205. foo = global_getfoo ();
  206. if (foo != MAGIC1)
  207. {
  208. printf ("getfoo [global]: got %#x, expected %#x\n", foo, MAGIC1);
  209. return 1;
  210. }
  211. foo = local_getfoo ();
  212. if (foo != MAGIC1)
  213. {
  214. printf ("getfoo [local]: got %#x, expected %#x\n", foo, MAGIC1);
  215. return 1;
  216. }
  217. /* Likewise with a change to its state made through the second handle. */
  218. global_setfoo (MAGIC2);
  219. foo = *initial_foop;
  220. if (foo != MAGIC2)
  221. {
  222. printf ("*foop [initial]: got %#x, expected %#x\n", foo, MAGIC2);
  223. return 1;
  224. }
  225. foo = *global_foop;
  226. if (foo != MAGIC2)
  227. {
  228. printf ("*foop [global]: got %#x, expected %#x\n", foo, MAGIC2);
  229. return 1;
  230. }
  231. foo = *local_foop;
  232. if (foo != MAGIC2)
  233. {
  234. printf ("*foop [local]: got %#x, expected %#x\n", foo, MAGIC2);
  235. return 1;
  236. }
  237. foo = initial_getfoo ();
  238. if (foo != MAGIC2)
  239. {
  240. printf ("getfoo [initial]: got %#x, expected %#x\n", foo, MAGIC2);
  241. return 1;
  242. }
  243. foo = global_getfoo ();
  244. if (foo != MAGIC2)
  245. {
  246. printf ("getfoo [global]: got %#x, expected %#x\n", foo, MAGIC2);
  247. return 1;
  248. }
  249. foo = local_getfoo ();
  250. if (foo != MAGIC2)
  251. {
  252. printf ("getfoo [local]: got %#x, expected %#x\n", foo, MAGIC2);
  253. return 1;
  254. }
  255. /* Likewise with a change to its state made through the third handle. */
  256. local_setfoo (MAGIC3);
  257. foo = *initial_foop;
  258. if (foo != MAGIC3)
  259. {
  260. printf ("*foop [initial]: got %#x, expected %#x\n", foo, MAGIC3);
  261. return 1;
  262. }
  263. foo = *global_foop;
  264. if (foo != MAGIC3)
  265. {
  266. printf ("*foop [global]: got %#x, expected %#x\n", foo, MAGIC3);
  267. return 1;
  268. }
  269. foo = *local_foop;
  270. if (foo != MAGIC3)
  271. {
  272. printf ("*foop [local]: got %#x, expected %#x\n", foo, MAGIC3);
  273. return 1;
  274. }
  275. foo = initial_getfoo ();
  276. if (foo != MAGIC3)
  277. {
  278. printf ("getfoo [initial]: got %#x, expected %#x\n", foo, MAGIC3);
  279. return 1;
  280. }
  281. foo = global_getfoo ();
  282. if (foo != MAGIC3)
  283. {
  284. printf ("getfoo [global]: got %#x, expected %#x\n", foo, MAGIC3);
  285. return 1;
  286. }
  287. foo = local_getfoo ();
  288. if (foo != MAGIC3)
  289. {
  290. printf ("getfoo [local]: got %#x, expected %#x\n", foo, MAGIC3);
  291. return 1;
  292. }
  293. /* All done, clean up. */
  294. initial_getfoo = NULL;
  295. initial_setfoo = NULL;
  296. initial_foop = NULL;
  297. local_getfoo = NULL;
  298. local_setfoo = NULL;
  299. local_foop = NULL;
  300. dlclose (local_handle);
  301. global_getfoo = NULL;
  302. global_setfoo = NULL;
  303. global_foop = NULL;
  304. dlclose (global_handle);
  305. dlclose (initial_handle);
  306. return 0;
  307. }
  308. #define TEST_FUNCTION do_test ()
  309. #include "../test-skeleton.c"