1/* Handle symbol and library versioning.
2 Copyright (C) 1997-2019 Free Software Foundation, Inc.
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
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.
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
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#include <elf.h>
21#include <errno.h>
22#include <libintl.h>
23#include <stdlib.h>
24#include <string.h>
25#include <ldsodefs.h>
26#include <_itoa.h>
27
28#include <assert.h>
29
30static inline struct link_map *
31__attribute ((always_inline))
32find_needed (const char *name, struct link_map *map)
33{
34 struct link_map *tmap;
35 unsigned int n;
36
37 for (tmap = GL(dl_ns)[map->l_ns]._ns_loaded; tmap != NULL;
38 tmap = tmap->l_next)
39 if (_dl_name_match_p (name, tmap))
40 return tmap;
41
42 /* The required object is not in the global scope, look to see if it is
43 a dependency of the current object. */
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];
47
48 /* Should never happen. */
49 return NULL;
50}
51
52
53static int
54match_symbol (const char *name, Lmid_t ns, ElfW(Word) hash, const char *string,
55 struct link_map *map, int verbose, int weak)
56{
57 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
58 ElfW(Addr) def_offset;
59 ElfW(Verdef) *def;
60 /* Initialize to make the compiler happy. */
61 int result = 0;
62 struct dl_exception exception;
63
64 /* Display information about what we are doing while debugging. */
65 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_VERSIONS))
66 _dl_debug_printf ("\
67checking for version `%s' in file %s [%lu] required by file %s [%lu]\n",
68 string, DSO_FILENAME (map->l_name),
69 map->l_ns, name, ns);
70
71 if (__glibc_unlikely (map->l_info[VERSYMIDX (DT_VERDEF)] == NULL))
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)
77 {
78 /* XXX We cannot translate the messages. */
79 _dl_exception_create_format
80 (&exception, DSO_FILENAME (map->l_name),
81 "no version information available (required by %s)", name);
82 goto call_cerror;
83 }
84 return 0;
85 }
86
87 def_offset = map->l_info[VERSYMIDX (DT_VERDEF)]->d_un.d_ptr;
88 assert (def_offset != 0);
89
90 def = (ElfW(Verdef) *) ((char *) map->l_addr + def_offset);
91 while (1)
92 {
93 /* Currently the version number of the definition entry is 1.
94 Make sure all we see is this version. */
95 if (__builtin_expect (def->vd_version, 1) != 1)
96 {
97 char buf[20];
98 buf[sizeof (buf) - 1] = '\0';
99 /* XXX We cannot translate the message. */
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));
104 result = 1;
105 goto call_cerror;
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. */
114 if (__builtin_expect (strcmp (string, strtab + aux->vda_name), 0)
115 == 0)
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. */
129 if (__glibc_likely (weak))
130 {
131 if (verbose)
132 {
133 /* XXX We cannot translate the message. */
134 _dl_exception_create_format
135 (&exception, DSO_FILENAME (map->l_name),
136 "weak version `%s' not found (required by %s)", string, name);
137 goto call_cerror;
138 }
139 return 0;
140 }
141
142 /* XXX We cannot translate the message. */
143 _dl_exception_create_format
144 (&exception, DSO_FILENAME (map->l_name),
145 "version `%s' not found (required by %s)", string, name);
146 result = 1;
147 call_cerror:
148 _dl_signal_cexception (0, &exception, N_("version lookup error"));
149 _dl_exception_free (&exception);
150 return result;
151}
152
153
154int
155_dl_check_map_versions (struct link_map *map, int verbose, int trace_mode)
156{
157 int result = 0;
158 const char *strtab;
159 /* Pointer to section with needed versions. */
160 ElfW(Dyn) *dyn;
161 /* Pointer to dynamic section with definitions. */
162 ElfW(Dyn) *def;
163 /* We need to find out which is the highest version index used
164 in a dependecy. */
165 unsigned int ndx_high = 0;
166 struct dl_exception exception;
167 /* Initialize to make the compiler happy. */
168 int errval = 0;
169
170 /* If we don't have a string table, we must be ok. */
171 if (map->l_info[DT_STRTAB] == NULL)
172 return 0;
173 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
174
175 dyn = map->l_info[VERSYMIDX (DT_VERNEED)];
176 def = map->l_info[VERSYMIDX (DT_VERDEF)];
177
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. */
185 if (__builtin_expect (ent->vn_version, 1) != 1)
186 {
187 char buf[20];
188 buf[sizeof (buf) - 1] = '\0';
189 /* XXX We cannot translate the message. */
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));
194 call_error:
195 _dl_signal_exception (errval, &exception, NULL);
196 }
197
198 while (1)
199 {
200 ElfW(Vernaux) *aux;
201 struct link_map *needed = find_needed (strtab + ent->vn_file, map);
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
207 /* Make sure this is no stub we created because of a missing
208 dependency. */
209 if (__builtin_expect (! trace_mode, 1)
210 || ! __builtin_expect (needed->l_faked, 0))
211 {
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. */
218 result |= match_symbol (DSO_FILENAME (map->l_name),
219 map->l_ns, aux->vna_hash,
220 strtab + aux->vna_name,
221 needed->l_real, verbose,
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 }
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;
254 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
255 while (1)
256 {
257 if ((unsigned int) (ent->vd_ndx & 0x7fff) > ndx_high)
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. */
273 map->l_versions = (struct r_found_version *)
274 calloc (ndx_high + 1, sizeof (*map->l_versions));
275 if (__glibc_unlikely (map->l_versions == NULL))
276 {
277 _dl_exception_create
278 (&exception, DSO_FILENAME (map->l_name),
279 N_("cannot allocate version reference table"));
280 errval = ENOMEM;
281 goto call_error;
282 }
283
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)]);
289
290 if (dyn != NULL)
291 {
292 ElfW(Verneed) *ent;
293 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
294 while (1)
295 {
296 ElfW(Vernaux) *aux;
297 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
298 while (1)
299 {
300 ElfW(Half) ndx = aux->vna_other & 0x7fff;
301 /* In trace mode, dependencies may be missing. */
302 if (__glibc_likely (ndx < map->l_nversions))
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 }
309
310 if (aux->vna_next == 0)
311 /* No more symbols. */
312 break;
313
314 /* Advance to next symbol. */
315 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
316 }
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);
324 }
325 }
326
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)
333 {
334 ElfW(Verdaux) *aux;
335 aux = (ElfW(Verdaux) *) ((char *) ent + ent->vd_aux);
336
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;
345 }
346
347 if (ent->vd_next == 0)
348 /* No more definitions. */
349 break;
350
351 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
352 }
353 }
354 }
355
356 return result;
357}
358
359
360int
361_dl_check_all_versions (struct link_map *map, int verbose, int trace_mode)
362{
363 struct link_map *l;
364 int result = 0;
365
366 for (l = map; l != NULL; l = l->l_next)
367 result |= (! l->l_faked
368 && _dl_check_map_versions (l, verbose, trace_mode));
369
370 return result;
371}
372