]>
Commit | Line | Data |
---|---|---|
6973fc01 | 1 | /* Handle loading/unloading of shared object for transformation. |
581c785b | 2 | Copyright (C) 1997-2022 Free Software Foundation, Inc. |
6973fc01 | 3 | This file is part of the GNU C Library. |
6973fc01 UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
6973fc01 UD |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
6973fc01 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
6973fc01 | 18 | |
add40772 | 19 | #include <assert.h> |
6973fc01 | 20 | #include <dlfcn.h> |
6973fc01 | 21 | #include <inttypes.h> |
6973fc01 UD |
22 | #include <search.h> |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
ec999b8e | 25 | #include <libc-lock.h> |
6973fc01 UD |
26 | #include <sys/param.h> |
27 | ||
e62c19f1 | 28 | #include <gconv_int.h> |
915a6c51 | 29 | #include <sysdep.h> |
e62c19f1 | 30 | |
76a2102b UD |
31 | |
32 | #ifdef DEBUG | |
33 | /* For debugging purposes. */ | |
34 | static void print_all (void); | |
35 | #endif | |
36 | ||
37 | ||
6973fc01 UD |
38 | /* This is a tuning parameter. If a transformation module is not used |
39 | anymore it gets not immediately unloaded. Instead we wait a certain | |
40 | number of load attempts for further modules. If none of the | |
41 | subsequent load attempts name the same object it finally gets unloaded. | |
42 | Otherwise it is still available which hopefully is the frequent case. | |
43 | The following number is the number of unloading attempts we wait | |
44 | before unloading. */ | |
45 | #define TRIES_BEFORE_UNLOAD 2 | |
46 | ||
6973fc01 UD |
47 | /* Array of loaded objects. This is shared by all threads so we have |
48 | to use semaphores to access it. */ | |
49 | static void *loaded; | |
6973fc01 | 50 | |
6973fc01 UD |
51 | /* Comparison function for searching `loaded_object' tree. */ |
52 | static int | |
53 | known_compare (const void *p1, const void *p2) | |
54 | { | |
d64b6ad0 UD |
55 | const struct __gconv_loaded_object *s1 = |
56 | (const struct __gconv_loaded_object *) p1; | |
57 | const struct __gconv_loaded_object *s2 = | |
58 | (const struct __gconv_loaded_object *) p2; | |
6973fc01 | 59 | |
76a2102b | 60 | return strcmp (s1->name, s2->name); |
6973fc01 UD |
61 | } |
62 | ||
6973fc01 UD |
63 | /* Open the gconv database if necessary. A non-negative return value |
64 | means success. */ | |
d64b6ad0 | 65 | struct __gconv_loaded_object * |
6973fc01 UD |
66 | __gconv_find_shlib (const char *name) |
67 | { | |
d64b6ad0 | 68 | struct __gconv_loaded_object *found; |
7a68c94a | 69 | void *keyp; |
6973fc01 UD |
70 | |
71 | /* Search the tree of shared objects previously requested. Data in | |
72 | the tree are `loaded_object' structures, whose first member is a | |
73 | `const char *', the lookup key. The search returns a pointer to | |
74 | the tree node structure; the first member of the is a pointer to | |
75 | our structure (i.e. what will be a `loaded_object'); since the | |
76 | first member of that is the lookup key string, &FCT_NAME is close | |
77 | enough to a pointer to our structure to use as a lookup key that | |
78 | will be passed to `known_compare' (above). */ | |
79 | ||
7a68c94a UD |
80 | keyp = __tfind (&name, &loaded, known_compare); |
81 | if (keyp == NULL) | |
6973fc01 UD |
82 | { |
83 | /* This name was not known before. */ | |
0db59742 UD |
84 | size_t namelen = strlen (name) + 1; |
85 | ||
86 | found = malloc (sizeof (struct __gconv_loaded_object) + namelen); | |
6973fc01 UD |
87 | if (found != NULL) |
88 | { | |
89 | /* Point the tree node at this new structure. */ | |
0db59742 | 90 | found->name = (char *) memcpy (found + 1, name, namelen); |
6973fc01 UD |
91 | found->counter = -TRIES_BEFORE_UNLOAD - 1; |
92 | found->handle = NULL; | |
93 | ||
365afefc UD |
94 | if (__builtin_expect (__tsearch (found, &loaded, known_compare) |
95 | == NULL, 0)) | |
6973fc01 UD |
96 | { |
97 | /* Something went wrong while inserting the entry. */ | |
98 | free (found); | |
99 | found = NULL; | |
100 | } | |
101 | } | |
102 | } | |
7a68c94a | 103 | else |
d64b6ad0 | 104 | found = *(struct __gconv_loaded_object **) keyp; |
6973fc01 UD |
105 | |
106 | /* Try to load the shared object if the usage count is 0. This | |
107 | implies that if the shared object is not loadable, the handle is | |
108 | NULL and the usage count > 0. */ | |
109 | if (found != NULL) | |
110 | { | |
111 | if (found->counter < -TRIES_BEFORE_UNLOAD) | |
112 | { | |
fc5f4a97 | 113 | assert (found->handle == NULL); |
b3fc5f84 UD |
114 | found->handle = __libc_dlopen (found->name); |
115 | if (found->handle != NULL) | |
0d9f6793 | 116 | { |
b3fc5f84 | 117 | found->fct = __libc_dlsym (found->handle, "gconv"); |
0d9f6793 UD |
118 | if (found->fct == NULL) |
119 | { | |
120 | /* Argh, no conversion function. There is something | |
121 | wrong here. */ | |
122 | __gconv_release_shlib (found); | |
123 | found = NULL; | |
124 | } | |
125 | else | |
126 | { | |
b3fc5f84 UD |
127 | found->init_fct = __libc_dlsym (found->handle, "gconv_init"); |
128 | found->end_fct = __libc_dlsym (found->handle, "gconv_end"); | |
0d9f6793 | 129 | |
915a6c51 UD |
130 | #ifdef PTR_MANGLE |
131 | PTR_MANGLE (found->fct); | |
1cf1232c PF |
132 | PTR_MANGLE (found->init_fct); |
133 | PTR_MANGLE (found->end_fct); | |
915a6c51 UD |
134 | #endif |
135 | ||
0d9f6793 UD |
136 | /* We have succeeded in loading the shared object. */ |
137 | found->counter = 1; | |
138 | } | |
139 | } | |
140 | else | |
141 | /* Error while loading the shared object. */ | |
142 | found = NULL; | |
6973fc01 UD |
143 | } |
144 | else if (found->handle != NULL) | |
145 | found->counter = MAX (found->counter + 1, 1); | |
6973fc01 UD |
146 | } |
147 | ||
0d9f6793 | 148 | return found; |
6973fc01 UD |
149 | } |
150 | ||
6973fc01 | 151 | static void |
e863dbf6 | 152 | do_release_shlib (const void *nodep, VISIT value, void *closure) |
6973fc01 | 153 | { |
e863dbf6 | 154 | struct __gconv_loaded_object *release_handle = closure; |
d64b6ad0 | 155 | struct __gconv_loaded_object *obj = *(struct __gconv_loaded_object **) nodep; |
6973fc01 UD |
156 | |
157 | if (value != preorder && value != leaf) | |
158 | return; | |
159 | ||
0d9f6793 | 160 | if (obj == release_handle) |
add40772 UD |
161 | { |
162 | /* This is the object we want to unload. Now decrement the | |
163 | reference counter. */ | |
164 | assert (obj->counter > 0); | |
165 | --obj->counter; | |
166 | } | |
fc5f4a97 UD |
167 | else if (obj->counter <= 0 && obj->counter >= -TRIES_BEFORE_UNLOAD |
168 | && --obj->counter < -TRIES_BEFORE_UNLOAD && obj->handle != NULL) | |
6973fc01 | 169 | { |
fc5f4a97 UD |
170 | /* Unload the shared object. */ |
171 | __libc_dlclose (obj->handle); | |
172 | obj->handle = NULL; | |
6973fc01 UD |
173 | } |
174 | } | |
175 | ||
176 | ||
177 | /* Notify system that a shared object is not longer needed. */ | |
b79f74cd | 178 | void |
d64b6ad0 | 179 | __gconv_release_shlib (struct __gconv_loaded_object *handle) |
6973fc01 | 180 | { |
6973fc01 UD |
181 | /* Process all entries. Please note that we also visit entries |
182 | with release counts <= 0. This way we can finally unload them | |
183 | if necessary. */ | |
e863dbf6 | 184 | __twalk_r (loaded, do_release_shlib, handle); |
6973fc01 | 185 | } |
e6df9a56 UD |
186 | |
187 | ||
188 | /* We run this if we debug the memory allocation. */ | |
7c11c4a1 | 189 | static void __libc_freeres_fn_section |
74454183 | 190 | do_release_all (void *nodep) |
e6df9a56 | 191 | { |
d64b6ad0 | 192 | struct __gconv_loaded_object *obj = (struct __gconv_loaded_object *) nodep; |
e6df9a56 | 193 | |
b3fc5f84 | 194 | /* Unload the shared object. */ |
74454183 | 195 | if (obj->handle != NULL) |
9a0a9895 | 196 | __libc_dlclose (obj->handle); |
e6df9a56 UD |
197 | |
198 | free (obj); | |
199 | } | |
200 | ||
c877418f | 201 | libc_freeres_fn (free_mem) |
e6df9a56 | 202 | { |
74454183 | 203 | __tdestroy (loaded, do_release_all); |
f6e50e66 | 204 | loaded = NULL; |
e6df9a56 | 205 | } |
76a2102b UD |
206 | |
207 | ||
208 | #ifdef DEBUG | |
f2b3078e MS |
209 | |
210 | #include <stdio.h> | |
211 | ||
76a2102b UD |
212 | static void |
213 | do_print (const void *nodep, VISIT value, int level) | |
214 | { | |
215 | struct __gconv_loaded_object *obj = *(struct __gconv_loaded_object **) nodep; | |
216 | ||
217 | printf ("%10s: \"%s\", %d\n", | |
a04549c1 JM |
218 | value == leaf ? "leaf" |
219 | : value == preorder ? "preorder" | |
220 | : value == postorder ? "postorder" : "endorder", | |
76a2102b UD |
221 | obj->name, obj->counter); |
222 | } | |
223 | ||
f2b3078e | 224 | static void __attribute__ ((used)) |
76a2102b UD |
225 | print_all (void) |
226 | { | |
227 | __twalk (loaded, do_print); | |
228 | } | |
229 | #endif |