]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-version.c
hurd: Fix build
[thirdparty/glibc.git] / elf / dl-version.c
CommitLineData
c84142e8 1/* Handle symbol and library versioning.
04277e02 2 Copyright (C) 1997-2019 Free Software Foundation, Inc.
c84142e8
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
c84142e8
UD
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 14 Lesser General Public License for more details.
c84142e8 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
c84142e8
UD
19
20#include <elf.h>
21#include <errno.h>
8e17ea58 22#include <libintl.h>
c84142e8
UD
23#include <stdlib.h>
24#include <string.h>
a42195db 25#include <ldsodefs.h>
eb96ffb0 26#include <_itoa.h>
c84142e8 27
a853022c
UD
28#include <assert.h>
29
c84142e8 30static inline struct link_map *
dd9423a6 31__attribute ((always_inline))
762a2918 32find_needed (const char *name, struct link_map *map)
c84142e8 33{
26b4d766 34 struct link_map *tmap;
c84142e8
UD
35 unsigned int n;
36
c0f62c56
UD
37 for (tmap = GL(dl_ns)[map->l_ns]._ns_loaded; tmap != NULL;
38 tmap = tmap->l_next)
26b4d766
UD
39 if (_dl_name_match_p (name, tmap))
40 return tmap;
c84142e8 41
762a2918
UD
42 /* The required object is not in the global scope, look to see if it is
43 a dependency of the current object. */
be935610
UD
44 for (n = 0; n < map->l_searchlist.r_nlist; n++)
45 if (_dl_name_match_p (name, map->l_searchlist.r_list[n]))
46 return map->l_searchlist.r_list[n];
762a2918 47
c84142e8
UD
48 /* Should never happen. */
49 return NULL;
50}
51
52
53static int
75a142a2 54match_symbol (const char *name, Lmid_t ns, ElfW(Word) hash, const char *string,
c84142e8
UD
55 struct link_map *map, int verbose, int weak)
56{
a42195db 57 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
714a562f 58 ElfW(Addr) def_offset;
c84142e8 59 ElfW(Verdef) *def;
6ed623f8 60 /* Initialize to make the compiler happy. */
6ed623f8 61 int result = 0;
2449ae7b 62 struct dl_exception exception;
c84142e8 63
8193034b 64 /* Display information about what we are doing while debugging. */
a1ffb40e 65 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_VERSIONS))
154d10bd 66 _dl_debug_printf ("\
75a142a2 67checking for version `%s' in file %s [%lu] required by file %s [%lu]\n",
b9375348 68 string, DSO_FILENAME (map->l_name),
75a142a2 69 map->l_ns, name, ns);
8193034b 70
a1ffb40e 71 if (__glibc_unlikely (map->l_info[VERSYMIDX (DT_VERDEF)] == NULL))
c84142e8
UD
72 {
73 /* The file has no symbol versioning. I.e., the dependent
74 object was linked against another version of this file. We
75 only print a message if verbose output is requested. */
76 if (verbose)
6ed623f8
UD
77 {
78 /* XXX We cannot translate the messages. */
2449ae7b
FW
79 _dl_exception_create_format
80 (&exception, DSO_FILENAME (map->l_name),
81 "no version information available (required by %s)", name);
6ed623f8
UD
82 goto call_cerror;
83 }
c84142e8
UD
84 return 0;
85 }
86
f420344c 87 def_offset = map->l_info[VERSYMIDX (DT_VERDEF)]->d_un.d_ptr;
714a562f
UD
88 assert (def_offset != 0);
89
90 def = (ElfW(Verdef) *) ((char *) map->l_addr + def_offset);
c84142e8
UD
91 while (1)
92 {
93 /* Currently the version number of the definition entry is 1.
94 Make sure all we see is this version. */
7b228b68 95 if (__builtin_expect (def->vd_version, 1) != 1)
c84142e8
UD
96 {
97 char buf[20];
98 buf[sizeof (buf) - 1] = '\0';
8e17ea58 99 /* XXX We cannot translate the message. */
2449ae7b
FW
100 _dl_exception_create_format
101 (&exception, DSO_FILENAME (map->l_name),
102 "unsupported version %s of Verdef record",
103 _itoa (def->vd_version, &buf[sizeof (buf) - 1], 10, 0));
6ed623f8
UD
104 result = 1;
105 goto call_cerror;
c84142e8
UD
106 }
107
108 /* Compare the hash values. */
109 if (hash == def->vd_hash)
110 {
111 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
112
113 /* To be safe, compare the string as well. */
7b228b68
UD
114 if (__builtin_expect (strcmp (string, strtab + aux->vda_name), 0)
115 == 0)
c84142e8
UD
116 /* Bingo! */
117 return 0;
118 }
119
120 /* If no more definitions we failed to find what we want. */
121 if (def->vd_next == 0)
122 break;
123
124 /* Next definition. */
125 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
126 }
127
128 /* Symbol not found. If it was a weak reference it is not fatal. */
a1ffb40e 129 if (__glibc_likely (weak))
c84142e8
UD
130 {
131 if (verbose)
6ed623f8
UD
132 {
133 /* XXX We cannot translate the message. */
2449ae7b
FW
134 _dl_exception_create_format
135 (&exception, DSO_FILENAME (map->l_name),
136 "weak version `%s' not found (required by %s)", string, name);
6ed623f8
UD
137 goto call_cerror;
138 }
c84142e8
UD
139 return 0;
140 }
141
8e17ea58 142 /* XXX We cannot translate the message. */
2449ae7b
FW
143 _dl_exception_create_format
144 (&exception, DSO_FILENAME (map->l_name),
145 "version `%s' not found (required by %s)", string, name);
6ed623f8
UD
146 result = 1;
147 call_cerror:
2449ae7b
FW
148 _dl_signal_cexception (0, &exception, N_("version lookup error"));
149 _dl_exception_free (&exception);
6ed623f8 150 return result;
c84142e8
UD
151}
152
153
154int
145b8413 155_dl_check_map_versions (struct link_map *map, int verbose, int trace_mode)
c84142e8
UD
156{
157 int result = 0;
f41c8091 158 const char *strtab;
c84142e8 159 /* Pointer to section with needed versions. */
f41c8091 160 ElfW(Dyn) *dyn;
c84142e8 161 /* Pointer to dynamic section with definitions. */
f41c8091 162 ElfW(Dyn) *def;
c84142e8
UD
163 /* We need to find out which is the highest version index used
164 in a dependecy. */
165 unsigned int ndx_high = 0;
2449ae7b 166 struct dl_exception exception;
6ed623f8 167 /* Initialize to make the compiler happy. */
6ed623f8 168 int errval = 0;
c84142e8 169
f41c8091
UD
170 /* If we don't have a string table, we must be ok. */
171 if (map->l_info[DT_STRTAB] == NULL)
172 return 0;
a42195db 173 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
f41c8091 174
f420344c
UD
175 dyn = map->l_info[VERSYMIDX (DT_VERNEED)];
176 def = map->l_info[VERSYMIDX (DT_VERDEF)];
f41c8091 177
c84142e8
UD
178 if (dyn != NULL)
179 {
180 /* This file requires special versions from its dependencies. */
181 ElfW(Verneed) *ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
182
183 /* Currently the version number of the needed entry is 1.
184 Make sure all we see is this version. */
7b228b68 185 if (__builtin_expect (ent->vn_version, 1) != 1)
c84142e8
UD
186 {
187 char buf[20];
188 buf[sizeof (buf) - 1] = '\0';
8e17ea58 189 /* XXX We cannot translate the message. */
2449ae7b
FW
190 _dl_exception_create_format
191 (&exception, DSO_FILENAME (map->l_name),
192 "unsupported version %s of Verneed record",
193 _itoa (ent->vn_version, &buf[sizeof (buf) - 1], 10, 0));
6ed623f8 194 call_error:
2449ae7b 195 _dl_signal_exception (errval, &exception, NULL);
c84142e8
UD
196 }
197
198 while (1)
199 {
200 ElfW(Vernaux) *aux;
762a2918 201 struct link_map *needed = find_needed (strtab + ent->vn_file, map);
c84142e8
UD
202
203 /* If NEEDED is NULL this means a dependency was not found
204 and no stub entry was created. This should never happen. */
205 assert (needed != NULL);
206
145b8413
UD
207 /* Make sure this is no stub we created because of a missing
208 dependency. */
7b228b68 209 if (__builtin_expect (! trace_mode, 1)
a881e0a0 210 || ! __builtin_expect (needed->l_faked, 0))
c84142e8 211 {
145b8413
UD
212 /* NEEDED is the map for the file we need. Now look for the
213 dependency symbols. */
214 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
215 while (1)
216 {
217 /* Match the symbol. */
b9375348 218 result |= match_symbol (DSO_FILENAME (map->l_name),
75a142a2 219 map->l_ns, aux->vna_hash,
145b8413 220 strtab + aux->vna_name,
c0f62c56 221 needed->l_real, verbose,
145b8413
UD
222 aux->vna_flags & VER_FLG_WEAK);
223
224 /* Compare the version index. */
225 if ((unsigned int) (aux->vna_other & 0x7fff) > ndx_high)
226 ndx_high = aux->vna_other & 0x7fff;
227
228 if (aux->vna_next == 0)
229 /* No more symbols. */
230 break;
231
232 /* Next symbol. */
233 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
234 }
c84142e8
UD
235 }
236
237 if (ent->vn_next == 0)
238 /* No more dependencies. */
239 break;
240
241 /* Next dependency. */
242 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
243 }
244 }
245
246 /* We also must store the names of the defined versions. Determine
247 the maximum index here as well.
248
249 XXX We could avoid the loop by just taking the number of definitions
250 as an upper bound of new indeces. */
251 if (def != NULL)
252 {
253 ElfW(Verdef) *ent;
714a562f 254 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
c84142e8
UD
255 while (1)
256 {
c131718c 257 if ((unsigned int) (ent->vd_ndx & 0x7fff) > ndx_high)
c84142e8
UD
258 ndx_high = ent->vd_ndx & 0x7fff;
259
260 if (ent->vd_next == 0)
261 /* No more definitions. */
262 break;
263
264 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
265 }
266 }
267
268 if (ndx_high > 0)
269 {
270 /* Now we are ready to build the array with the version names
271 which can be indexed by the version index in the VERSYM
272 section. */
1fb05e3d 273 map->l_versions = (struct r_found_version *)
c131718c 274 calloc (ndx_high + 1, sizeof (*map->l_versions));
a1ffb40e 275 if (__glibc_unlikely (map->l_versions == NULL))
c84142e8 276 {
2449ae7b
FW
277 _dl_exception_create
278 (&exception, DSO_FILENAME (map->l_name),
279 N_("cannot allocate version reference table"));
6ed623f8
UD
280 errval = ENOMEM;
281 goto call_error;
c84142e8 282 }
c84142e8 283
6ed623f8
UD
284 /* Store the number of available symbols. */
285 map->l_nversions = ndx_high + 1;
286
287 /* Compute the pointer to the version symbols. */
288 map->l_versyms = (void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
0c367d92 289
6ed623f8
UD
290 if (dyn != NULL)
291 {
292 ElfW(Verneed) *ent;
293 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
294 while (1)
c84142e8 295 {
6ed623f8
UD
296 ElfW(Vernaux) *aux;
297 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
c84142e8
UD
298 while (1)
299 {
6ed623f8 300 ElfW(Half) ndx = aux->vna_other & 0x7fff;
18a26b30 301 /* In trace mode, dependencies may be missing. */
a1ffb40e 302 if (__glibc_likely (ndx < map->l_nversions))
18a26b30
AS
303 {
304 map->l_versions[ndx].hash = aux->vna_hash;
305 map->l_versions[ndx].hidden = aux->vna_other & 0x8000;
306 map->l_versions[ndx].name = &strtab[aux->vna_name];
307 map->l_versions[ndx].filename = &strtab[ent->vn_file];
308 }
6ed623f8
UD
309
310 if (aux->vna_next == 0)
311 /* No more symbols. */
c84142e8
UD
312 break;
313
6ed623f8
UD
314 /* Advance to next symbol. */
315 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
c84142e8 316 }
6ed623f8
UD
317
318 if (ent->vn_next == 0)
319 /* No more dependencies. */
320 break;
321
322 /* Advance to next dependency. */
323 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
c84142e8 324 }
6ed623f8 325 }
c84142e8 326
6ed623f8
UD
327 /* And insert the defined versions. */
328 if (def != NULL)
329 {
330 ElfW(Verdef) *ent;
331 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
332 while (1)
c84142e8 333 {
6ed623f8
UD
334 ElfW(Verdaux) *aux;
335 aux = (ElfW(Verdaux) *) ((char *) ent + ent->vd_aux);
c84142e8 336
6ed623f8
UD
337 if ((ent->vd_flags & VER_FLG_BASE) == 0)
338 {
339 /* The name of the base version should not be
340 available for matching a versioned symbol. */
341 ElfW(Half) ndx = ent->vd_ndx & 0x7fff;
342 map->l_versions[ndx].hash = ent->vd_hash;
343 map->l_versions[ndx].name = &strtab[aux->vda_name];
344 map->l_versions[ndx].filename = NULL;
c84142e8 345 }
6ed623f8
UD
346
347 if (ent->vd_next == 0)
348 /* No more definitions. */
349 break;
350
351 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
c84142e8
UD
352 }
353 }
354 }
355
356 return result;
357}
358
359
360int
145b8413 361_dl_check_all_versions (struct link_map *map, int verbose, int trace_mode)
c84142e8
UD
362{
363 struct link_map *l;
364 int result = 0;
365
366 for (l = map; l != NULL; l = l->l_next)
7969407a 367 result |= (! l->l_faked
154d10bd 368 && _dl_check_map_versions (l, verbose, trace_mode));
c84142e8
UD
369
370 return result;
371}