tststatic3.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Global-scope DSO mapping test 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. /* Mapping a DSO into the global scope used to crash in static
  22. executables. Check that it succeeds and then that symbols from
  23. the DSO can be accessed and operate as expected. */
  24. static int
  25. do_test (void)
  26. {
  27. unsigned int (*getfoo) (void);
  28. void (*setfoo) (unsigned int);
  29. unsigned int *foop;
  30. unsigned int foo;
  31. void *handle;
  32. /* Try to map a module into the global scope. */
  33. handle = dlopen ("modstatic3.so", RTLD_LAZY | RTLD_GLOBAL);
  34. if (handle == NULL)
  35. {
  36. printf ("dlopen (modstatic3.so): %s\n", dlerror ());
  37. return 1;
  38. }
  39. /* Get at its symbols. */
  40. foop = dlsym (handle, "foo");
  41. if (foop == NULL)
  42. {
  43. printf ("dlsym (foo): %s\n", dlerror ());
  44. return 1;
  45. }
  46. getfoo = dlsym (handle, "getfoo");
  47. if (getfoo == NULL)
  48. {
  49. printf ("dlsym (getfoo): %s\n", dlerror ());
  50. return 1;
  51. }
  52. setfoo = dlsym (handle, "setfoo");
  53. if (setfoo == NULL)
  54. {
  55. printf ("dlsym (setfoo): %s\n", dlerror ());
  56. return 1;
  57. }
  58. /* Make sure the view of the initial state is consistent. */
  59. foo = *foop;
  60. if (foo != MAGIC0)
  61. {
  62. printf ("*foop: got %#x, expected %#x\n", foo, MAGIC0);
  63. return 1;
  64. }
  65. foo = getfoo ();
  66. if (foo != MAGIC0)
  67. {
  68. printf ("getfoo: got %#x, expected %#x\n", foo, MAGIC0);
  69. return 1;
  70. }
  71. /* Likewise with one change to its state. */
  72. setfoo (MAGIC1);
  73. foo = *foop;
  74. if (foo != MAGIC1)
  75. {
  76. printf ("*foop: got %#x, expected %#x\n", foo, MAGIC1);
  77. return 1;
  78. }
  79. foo = getfoo ();
  80. if (foo != MAGIC1)
  81. {
  82. printf ("getfoo: got %#x, expected %#x\n", foo, MAGIC1);
  83. return 1;
  84. }
  85. /* And with another. */
  86. setfoo (MAGIC2);
  87. foo = *foop;
  88. if (foo != MAGIC2)
  89. {
  90. printf ("*foop: got %#x, expected %#x\n", foo, MAGIC2);
  91. return 1;
  92. }
  93. foo = getfoo ();
  94. if (foo != MAGIC2)
  95. {
  96. printf ("getfoo: got %#x, expected %#x\n", foo, MAGIC2);
  97. return 1;
  98. }
  99. /* All done, clean up. */
  100. getfoo = NULL;
  101. setfoo = NULL;
  102. foop = NULL;
  103. dlclose (handle);
  104. return 0;
  105. }
  106. #define TEST_FUNCTION do_test ()
  107. #include "../test-skeleton.c"