gconv_dl.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* Handle loading/unloading of shared object for transformation.
  2. Copyright (C) 1997-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 <assert.h>
  16. #include <dlfcn.h>
  17. #include <inttypes.h>
  18. #include <search.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <libc-lock.h>
  22. #include <sys/param.h>
  23. #include <gconv_int.h>
  24. #include <pointer_guard.h>
  25. #ifdef DEBUG
  26. /* For debugging purposes. */
  27. static void print_all (void);
  28. #endif
  29. /* This is a tuning parameter. If a transformation module is not used
  30. anymore it gets not immediately unloaded. Instead we wait a certain
  31. number of load attempts for further modules. If none of the
  32. subsequent load attempts name the same object it finally gets unloaded.
  33. Otherwise it is still available which hopefully is the frequent case.
  34. The following number is the number of unloading attempts we wait
  35. before unloading. */
  36. #define TRIES_BEFORE_UNLOAD 2
  37. /* Array of loaded objects. This is shared by all threads so we have
  38. to use semaphores to access it. */
  39. static void *loaded;
  40. /* Comparison function for searching `loaded_object' tree. */
  41. static int
  42. known_compare (const void *p1, const void *p2)
  43. {
  44. const struct __gconv_loaded_object *s1 =
  45. (const struct __gconv_loaded_object *) p1;
  46. const struct __gconv_loaded_object *s2 =
  47. (const struct __gconv_loaded_object *) p2;
  48. return strcmp (s1->name, s2->name);
  49. }
  50. /* Open the gconv database if necessary. A non-negative return value
  51. means success. */
  52. struct __gconv_loaded_object *
  53. __gconv_find_shlib (const char *name)
  54. {
  55. struct __gconv_loaded_object *found;
  56. void *keyp;
  57. /* Search the tree of shared objects previously requested. Data in
  58. the tree are `loaded_object' structures, whose first member is a
  59. `const char *', the lookup key. The search returns a pointer to
  60. the tree node structure; the first member of the is a pointer to
  61. our structure (i.e. what will be a `loaded_object'); since the
  62. first member of that is the lookup key string, &FCT_NAME is close
  63. enough to a pointer to our structure to use as a lookup key that
  64. will be passed to `known_compare' (above). */
  65. keyp = __tfind (&name, &loaded, known_compare);
  66. if (keyp == NULL)
  67. {
  68. /* This name was not known before. */
  69. size_t namelen = strlen (name) + 1;
  70. found = malloc (sizeof (struct __gconv_loaded_object) + namelen);
  71. if (found != NULL)
  72. {
  73. /* Point the tree node at this new structure. */
  74. found->name = (char *) memcpy (found + 1, name, namelen);
  75. found->counter = -TRIES_BEFORE_UNLOAD - 1;
  76. found->handle = NULL;
  77. if (__builtin_expect (__tsearch (found, &loaded, known_compare)
  78. == NULL, 0))
  79. {
  80. /* Something went wrong while inserting the entry. */
  81. free (found);
  82. found = NULL;
  83. }
  84. }
  85. }
  86. else
  87. found = *(struct __gconv_loaded_object **) keyp;
  88. /* Try to load the shared object if the usage count is 0. This
  89. implies that if the shared object is not loadable, the handle is
  90. NULL and the usage count > 0. */
  91. if (found != NULL)
  92. {
  93. if (found->counter < -TRIES_BEFORE_UNLOAD)
  94. {
  95. assert (found->handle == NULL);
  96. found->handle = __libc_dlopen (found->name);
  97. if (found->handle != NULL)
  98. {
  99. found->fct = __libc_dlsym (found->handle, "gconv");
  100. if (found->fct == NULL)
  101. {
  102. /* Argh, no conversion function. There is something
  103. wrong here. */
  104. __gconv_release_shlib (found);
  105. found = NULL;
  106. }
  107. else
  108. {
  109. found->init_fct = __libc_dlsym (found->handle, "gconv_init");
  110. found->end_fct = __libc_dlsym (found->handle, "gconv_end");
  111. PTR_MANGLE (found->fct);
  112. PTR_MANGLE (found->init_fct);
  113. PTR_MANGLE (found->end_fct);
  114. /* We have succeeded in loading the shared object. */
  115. found->counter = 1;
  116. }
  117. }
  118. else
  119. /* Error while loading the shared object. */
  120. found = NULL;
  121. }
  122. else if (found->handle != NULL)
  123. found->counter = MAX (found->counter + 1, 1);
  124. }
  125. return found;
  126. }
  127. static void
  128. do_release_shlib (const void *nodep, VISIT value, void *closure)
  129. {
  130. struct __gconv_loaded_object *release_handle = closure;
  131. struct __gconv_loaded_object *obj = *(struct __gconv_loaded_object **) nodep;
  132. if (value != preorder && value != leaf)
  133. return;
  134. if (obj == release_handle)
  135. {
  136. /* This is the object we want to unload. Now decrement the
  137. reference counter. */
  138. assert (obj->counter > 0);
  139. --obj->counter;
  140. }
  141. else if (obj->counter <= 0 && obj->counter >= -TRIES_BEFORE_UNLOAD
  142. && --obj->counter < -TRIES_BEFORE_UNLOAD && obj->handle != NULL)
  143. {
  144. /* Unload the shared object. */
  145. __libc_dlclose (obj->handle);
  146. obj->handle = NULL;
  147. }
  148. }
  149. /* Notify system that a shared object is not longer needed. */
  150. void
  151. __gconv_release_shlib (struct __gconv_loaded_object *handle)
  152. {
  153. /* Process all entries. Please note that we also visit entries
  154. with release counts <= 0. This way we can finally unload them
  155. if necessary. */
  156. __twalk_r (loaded, do_release_shlib, handle);
  157. }
  158. /* We run this if we debug the memory allocation. */
  159. static void
  160. do_release_all (void *nodep)
  161. {
  162. struct __gconv_loaded_object *obj = (struct __gconv_loaded_object *) nodep;
  163. /* Unload the shared object. */
  164. if (obj->handle != NULL)
  165. __libc_dlclose (obj->handle);
  166. free (obj);
  167. }
  168. void
  169. __gconv_dl_freemem (void)
  170. {
  171. __tdestroy (loaded, do_release_all);
  172. loaded = NULL;
  173. }
  174. #ifdef DEBUG
  175. #include <stdio.h>
  176. static void
  177. do_print (const void *nodep, VISIT value, int level)
  178. {
  179. struct __gconv_loaded_object *obj = *(struct __gconv_loaded_object **) nodep;
  180. printf ("%10s: \"%s\", %d\n",
  181. value == leaf ? "leaf"
  182. : value == preorder ? "preorder"
  183. : value == postorder ? "postorder" : "endorder",
  184. obj->name, obj->counter);
  185. }
  186. static void __attribute__ ((used))
  187. print_all (void)
  188. {
  189. __twalk (loaded, do_print);
  190. }
  191. #endif