Consolidate non cancellable openat call
[glibc.git] / elf / dl-version.c
blobc0d76ad42a8dcc4eb6e27d0cd6480aad2ac9d01c
1 /* Handle symbol and library versioning.
2 Copyright (C) 1997-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
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.
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.
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/>. */
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>
28 #include <assert.h>
30 static inline struct link_map *
31 __attribute ((always_inline))
32 find_needed (const char *name, struct link_map *map)
34 struct link_map *tmap;
35 unsigned int n;
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;
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];
48 /* Should never happen. */
49 return NULL;
53 static int
54 internal_function
55 match_symbol (const char *name, Lmid_t ns, ElfW(Word) hash, const char *string,
56 struct link_map *map, int verbose, int weak)
58 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
59 ElfW(Addr) def_offset;
60 ElfW(Verdef) *def;
61 /* Initialize to make the compiler happy. */
62 int result = 0;
63 struct dl_exception exception;
65 /* Display information about what we are doing while debugging. */
66 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_VERSIONS))
67 _dl_debug_printf ("\
68 checking for version `%s' in file %s [%lu] required by file %s [%lu]\n",
69 string, DSO_FILENAME (map->l_name),
70 map->l_ns, name, ns);
72 if (__glibc_unlikely (map->l_info[VERSYMIDX (DT_VERDEF)] == NULL))
74 /* The file has no symbol versioning. I.e., the dependent
75 object was linked against another version of this file. We
76 only print a message if verbose output is requested. */
77 if (verbose)
79 /* XXX We cannot translate the messages. */
80 _dl_exception_create_format
81 (&exception, DSO_FILENAME (map->l_name),
82 "no version information available (required by %s)", name);
83 goto call_cerror;
85 return 0;
88 def_offset = map->l_info[VERSYMIDX (DT_VERDEF)]->d_un.d_ptr;
89 assert (def_offset != 0);
91 def = (ElfW(Verdef) *) ((char *) map->l_addr + def_offset);
92 while (1)
94 /* Currently the version number of the definition entry is 1.
95 Make sure all we see is this version. */
96 if (__builtin_expect (def->vd_version, 1) != 1)
98 char buf[20];
99 buf[sizeof (buf) - 1] = '\0';
100 /* XXX We cannot translate the message. */
101 _dl_exception_create_format
102 (&exception, DSO_FILENAME (map->l_name),
103 "unsupported version %s of Verdef record",
104 _itoa (def->vd_version, &buf[sizeof (buf) - 1], 10, 0));
105 result = 1;
106 goto call_cerror;
109 /* Compare the hash values. */
110 if (hash == def->vd_hash)
112 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
114 /* To be safe, compare the string as well. */
115 if (__builtin_expect (strcmp (string, strtab + aux->vda_name), 0)
116 == 0)
117 /* Bingo! */
118 return 0;
121 /* If no more definitions we failed to find what we want. */
122 if (def->vd_next == 0)
123 break;
125 /* Next definition. */
126 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
129 /* Symbol not found. If it was a weak reference it is not fatal. */
130 if (__glibc_likely (weak))
132 if (verbose)
134 /* XXX We cannot translate the message. */
135 _dl_exception_create_format
136 (&exception, DSO_FILENAME (map->l_name),
137 "weak version `%s' not found (required by %s)", string, name);
138 goto call_cerror;
140 return 0;
143 /* XXX We cannot translate the message. */
144 _dl_exception_create_format
145 (&exception, DSO_FILENAME (map->l_name),
146 "version `%s' not found (required by %s)", string, name);
147 result = 1;
148 call_cerror:
149 _dl_signal_cexception (0, &exception, N_("version lookup error"));
150 _dl_exception_free (&exception);
151 return result;
156 internal_function
157 _dl_check_map_versions (struct link_map *map, int verbose, int trace_mode)
159 int result = 0;
160 const char *strtab;
161 /* Pointer to section with needed versions. */
162 ElfW(Dyn) *dyn;
163 /* Pointer to dynamic section with definitions. */
164 ElfW(Dyn) *def;
165 /* We need to find out which is the highest version index used
166 in a dependecy. */
167 unsigned int ndx_high = 0;
168 struct dl_exception exception;
169 /* Initialize to make the compiler happy. */
170 int errval = 0;
172 /* If we don't have a string table, we must be ok. */
173 if (map->l_info[DT_STRTAB] == NULL)
174 return 0;
175 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
177 dyn = map->l_info[VERSYMIDX (DT_VERNEED)];
178 def = map->l_info[VERSYMIDX (DT_VERDEF)];
180 if (dyn != NULL)
182 /* This file requires special versions from its dependencies. */
183 ElfW(Verneed) *ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
185 /* Currently the version number of the needed entry is 1.
186 Make sure all we see is this version. */
187 if (__builtin_expect (ent->vn_version, 1) != 1)
189 char buf[20];
190 buf[sizeof (buf) - 1] = '\0';
191 /* XXX We cannot translate the message. */
192 _dl_exception_create_format
193 (&exception, DSO_FILENAME (map->l_name),
194 "unsupported version %s of Verneed record",
195 _itoa (ent->vn_version, &buf[sizeof (buf) - 1], 10, 0));
196 call_error:
197 _dl_signal_exception (errval, &exception, NULL);
200 while (1)
202 ElfW(Vernaux) *aux;
203 struct link_map *needed = find_needed (strtab + ent->vn_file, map);
205 /* If NEEDED is NULL this means a dependency was not found
206 and no stub entry was created. This should never happen. */
207 assert (needed != NULL);
209 /* Make sure this is no stub we created because of a missing
210 dependency. */
211 if (__builtin_expect (! trace_mode, 1)
212 || ! __builtin_expect (needed->l_faked, 0))
214 /* NEEDED is the map for the file we need. Now look for the
215 dependency symbols. */
216 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
217 while (1)
219 /* Match the symbol. */
220 result |= match_symbol (DSO_FILENAME (map->l_name),
221 map->l_ns, aux->vna_hash,
222 strtab + aux->vna_name,
223 needed->l_real, verbose,
224 aux->vna_flags & VER_FLG_WEAK);
226 /* Compare the version index. */
227 if ((unsigned int) (aux->vna_other & 0x7fff) > ndx_high)
228 ndx_high = aux->vna_other & 0x7fff;
230 if (aux->vna_next == 0)
231 /* No more symbols. */
232 break;
234 /* Next symbol. */
235 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
239 if (ent->vn_next == 0)
240 /* No more dependencies. */
241 break;
243 /* Next dependency. */
244 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
248 /* We also must store the names of the defined versions. Determine
249 the maximum index here as well.
251 XXX We could avoid the loop by just taking the number of definitions
252 as an upper bound of new indeces. */
253 if (def != NULL)
255 ElfW(Verdef) *ent;
256 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
257 while (1)
259 if ((unsigned int) (ent->vd_ndx & 0x7fff) > ndx_high)
260 ndx_high = ent->vd_ndx & 0x7fff;
262 if (ent->vd_next == 0)
263 /* No more definitions. */
264 break;
266 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
270 if (ndx_high > 0)
272 /* Now we are ready to build the array with the version names
273 which can be indexed by the version index in the VERSYM
274 section. */
275 map->l_versions = (struct r_found_version *)
276 calloc (ndx_high + 1, sizeof (*map->l_versions));
277 if (__glibc_unlikely (map->l_versions == NULL))
279 _dl_exception_create
280 (&exception, DSO_FILENAME (map->l_name),
281 N_("cannot allocate version reference table"));
282 errval = ENOMEM;
283 goto call_error;
286 /* Store the number of available symbols. */
287 map->l_nversions = ndx_high + 1;
289 /* Compute the pointer to the version symbols. */
290 map->l_versyms = (void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
292 if (dyn != NULL)
294 ElfW(Verneed) *ent;
295 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
296 while (1)
298 ElfW(Vernaux) *aux;
299 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
300 while (1)
302 ElfW(Half) ndx = aux->vna_other & 0x7fff;
303 /* In trace mode, dependencies may be missing. */
304 if (__glibc_likely (ndx < map->l_nversions))
306 map->l_versions[ndx].hash = aux->vna_hash;
307 map->l_versions[ndx].hidden = aux->vna_other & 0x8000;
308 map->l_versions[ndx].name = &strtab[aux->vna_name];
309 map->l_versions[ndx].filename = &strtab[ent->vn_file];
312 if (aux->vna_next == 0)
313 /* No more symbols. */
314 break;
316 /* Advance to next symbol. */
317 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
320 if (ent->vn_next == 0)
321 /* No more dependencies. */
322 break;
324 /* Advance to next dependency. */
325 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
329 /* And insert the defined versions. */
330 if (def != NULL)
332 ElfW(Verdef) *ent;
333 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
334 while (1)
336 ElfW(Verdaux) *aux;
337 aux = (ElfW(Verdaux) *) ((char *) ent + ent->vd_aux);
339 if ((ent->vd_flags & VER_FLG_BASE) == 0)
341 /* The name of the base version should not be
342 available for matching a versioned symbol. */
343 ElfW(Half) ndx = ent->vd_ndx & 0x7fff;
344 map->l_versions[ndx].hash = ent->vd_hash;
345 map->l_versions[ndx].name = &strtab[aux->vda_name];
346 map->l_versions[ndx].filename = NULL;
349 if (ent->vd_next == 0)
350 /* No more definitions. */
351 break;
353 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
358 return result;
363 internal_function
364 _dl_check_all_versions (struct link_map *map, int verbose, int trace_mode)
366 struct link_map *l;
367 int result = 0;
369 for (l = map; l != NULL; l = l->l_next)
370 result |= (! l->l_faked
371 && _dl_check_map_versions (l, verbose, trace_mode));
373 return result;