Add a testase for BZ #14602
[glibc.git] / elf / dl-version.c
blobb2ebb165121bf40db06a8364fa7c9001dec369d7
1 /* Handle symbol and library versioning.
2 Copyright (C) 1997-2002, 2003, 2004, 2005 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>
31 #define make_string(string, rest...) \
32 ({ \
33 const char *all[] = { string, ## rest }; \
34 size_t len, cnt; \
35 char *result, *cp; \
37 len = 1; \
38 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
39 len += strlen (all[cnt]); \
41 cp = result = alloca (len); \
42 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
43 cp = __stpcpy (cp, all[cnt]); \
45 result; \
49 static inline struct link_map *
50 __attribute ((always_inline))
51 find_needed (const char *name, struct link_map *map)
53 struct link_map *tmap;
54 unsigned int n;
56 for (tmap = GL(dl_ns)[map->l_ns]._ns_loaded; tmap != NULL;
57 tmap = tmap->l_next)
58 if (_dl_name_match_p (name, tmap))
59 return tmap;
61 /* The required object is not in the global scope, look to see if it is
62 a dependency of the current object. */
63 for (n = 0; n < map->l_searchlist.r_nlist; n++)
64 if (_dl_name_match_p (name, map->l_searchlist.r_list[n]))
65 return map->l_searchlist.r_list[n];
67 /* Should never happen. */
68 return NULL;
72 static int
73 internal_function
74 match_symbol (const char *name, Lmid_t ns, ElfW(Word) hash, const char *string,
75 struct link_map *map, int verbose, int weak)
77 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
78 ElfW(Addr) def_offset;
79 ElfW(Verdef) *def;
80 /* Initialize to make the compiler happy. */
81 const char *errstring = NULL;
82 int result = 0;
84 /* Display information about what we are doing while debugging. */
85 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_VERSIONS, 0))
86 _dl_debug_printf ("\
87 checking for version `%s' in file %s [%lu] required by file %s [%lu]\n",
88 string, map->l_name[0] ? map->l_name : rtld_progname,
89 map->l_ns, name, ns);
91 if (__builtin_expect (map->l_info[VERSYMIDX (DT_VERDEF)] == NULL, 0))
93 /* The file has no symbol versioning. I.e., the dependent
94 object was linked against another version of this file. We
95 only print a message if verbose output is requested. */
96 if (verbose)
98 /* XXX We cannot translate the messages. */
99 errstring = make_string ("\
100 no version information available (required by ", name, ")");
101 goto call_cerror;
103 return 0;
106 def_offset = map->l_info[VERSYMIDX (DT_VERDEF)]->d_un.d_ptr;
107 assert (def_offset != 0);
109 def = (ElfW(Verdef) *) ((char *) map->l_addr + def_offset);
110 while (1)
112 /* Currently the version number of the definition entry is 1.
113 Make sure all we see is this version. */
114 if (__builtin_expect (def->vd_version, 1) != 1)
116 char buf[20];
117 buf[sizeof (buf) - 1] = '\0';
118 /* XXX We cannot translate the message. */
119 errstring = make_string ("unsupported version ",
120 _itoa (def->vd_version,
121 &buf[sizeof (buf) - 1], 10, 0),
122 " of Verdef record");
123 result = 1;
124 goto call_cerror;
127 /* Compare the hash values. */
128 if (hash == def->vd_hash)
130 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
132 /* To be safe, compare the string as well. */
133 if (__builtin_expect (strcmp (string, strtab + aux->vda_name), 0)
134 == 0)
135 /* Bingo! */
136 return 0;
139 /* If no more definitions we failed to find what we want. */
140 if (def->vd_next == 0)
141 break;
143 /* Next definition. */
144 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
147 /* Symbol not found. If it was a weak reference it is not fatal. */
148 if (__builtin_expect (weak, 1))
150 if (verbose)
152 /* XXX We cannot translate the message. */
153 errstring = make_string ("weak version `", string,
154 "' not found (required by ", name, ")");
155 goto call_cerror;
157 return 0;
160 /* XXX We cannot translate the message. */
161 errstring = make_string ("version `", string, "' not found (required by ",
162 name, ")");
163 result = 1;
164 call_cerror:
165 _dl_signal_cerror (0, map->l_name[0] ? map->l_name : rtld_progname,
166 N_("version lookup error"), errstring);
167 return result;
172 internal_function
173 _dl_check_map_versions (struct link_map *map, int verbose, int trace_mode)
175 int result = 0;
176 const char *strtab;
177 /* Pointer to section with needed versions. */
178 ElfW(Dyn) *dyn;
179 /* Pointer to dynamic section with definitions. */
180 ElfW(Dyn) *def;
181 /* We need to find out which is the highest version index used
182 in a dependecy. */
183 unsigned int ndx_high = 0;
184 /* Initialize to make the compiler happy. */
185 const char *errstring = NULL;
186 int errval = 0;
188 /* If we don't have a string table, we must be ok. */
189 if (map->l_info[DT_STRTAB] == NULL)
190 return 0;
191 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
193 dyn = map->l_info[VERSYMIDX (DT_VERNEED)];
194 def = map->l_info[VERSYMIDX (DT_VERDEF)];
196 if (dyn != NULL)
198 /* This file requires special versions from its dependencies. */
199 ElfW(Verneed) *ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
201 /* Currently the version number of the needed entry is 1.
202 Make sure all we see is this version. */
203 if (__builtin_expect (ent->vn_version, 1) != 1)
205 char buf[20];
206 buf[sizeof (buf) - 1] = '\0';
207 /* XXX We cannot translate the message. */
208 errstring = make_string ("unsupported version ",
209 _itoa (ent->vn_version,
210 &buf[sizeof (buf) - 1], 10, 0),
211 " of Verneed record\n");
212 call_error:
213 _dl_signal_error (errval, *map->l_name ? map->l_name : rtld_progname,
214 NULL, errstring);
217 while (1)
219 ElfW(Vernaux) *aux;
220 struct link_map *needed = find_needed (strtab + ent->vn_file, map);
222 /* If NEEDED is NULL this means a dependency was not found
223 and no stub entry was created. This should never happen. */
224 assert (needed != NULL);
226 /* Make sure this is no stub we created because of a missing
227 dependency. */
228 if (__builtin_expect (! trace_mode, 1)
229 || ! __builtin_expect (needed->l_faked, 0))
231 /* NEEDED is the map for the file we need. Now look for the
232 dependency symbols. */
233 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
234 while (1)
236 /* Match the symbol. */
237 result |= match_symbol ((*map->l_name
238 ? map->l_name : rtld_progname),
239 map->l_ns, aux->vna_hash,
240 strtab + aux->vna_name,
241 needed->l_real, verbose,
242 aux->vna_flags & VER_FLG_WEAK);
244 /* Compare the version index. */
245 if ((unsigned int) (aux->vna_other & 0x7fff) > ndx_high)
246 ndx_high = aux->vna_other & 0x7fff;
248 if (aux->vna_next == 0)
249 /* No more symbols. */
250 break;
252 /* Next symbol. */
253 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
257 if (ent->vn_next == 0)
258 /* No more dependencies. */
259 break;
261 /* Next dependency. */
262 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
266 /* We also must store the names of the defined versions. Determine
267 the maximum index here as well.
269 XXX We could avoid the loop by just taking the number of definitions
270 as an upper bound of new indeces. */
271 if (def != NULL)
273 ElfW(Verdef) *ent;
274 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
275 while (1)
277 if ((unsigned int) (ent->vd_ndx & 0x7fff) > ndx_high)
278 ndx_high = ent->vd_ndx & 0x7fff;
280 if (ent->vd_next == 0)
281 /* No more definitions. */
282 break;
284 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
288 if (ndx_high > 0)
290 /* Now we are ready to build the array with the version names
291 which can be indexed by the version index in the VERSYM
292 section. */
293 map->l_versions = (struct r_found_version *)
294 calloc (ndx_high + 1, sizeof (*map->l_versions));
295 if (__builtin_expect (map->l_versions == NULL, 0))
297 errstring = N_("cannot allocate version reference table");
298 errval = ENOMEM;
299 goto call_error;
302 /* Store the number of available symbols. */
303 map->l_nversions = ndx_high + 1;
305 /* Compute the pointer to the version symbols. */
306 map->l_versyms = (void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
308 if (dyn != NULL)
310 ElfW(Verneed) *ent;
311 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
312 while (1)
314 ElfW(Vernaux) *aux;
315 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
316 while (1)
318 ElfW(Half) ndx = aux->vna_other & 0x7fff;
319 /* In trace mode, dependencies may be missing. */
320 if (__builtin_expect (ndx < map->l_nversions, 1))
322 map->l_versions[ndx].hash = aux->vna_hash;
323 map->l_versions[ndx].hidden = aux->vna_other & 0x8000;
324 map->l_versions[ndx].name = &strtab[aux->vna_name];
325 map->l_versions[ndx].filename = &strtab[ent->vn_file];
328 if (aux->vna_next == 0)
329 /* No more symbols. */
330 break;
332 /* Advance to next symbol. */
333 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
336 if (ent->vn_next == 0)
337 /* No more dependencies. */
338 break;
340 /* Advance to next dependency. */
341 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
345 /* And insert the defined versions. */
346 if (def != NULL)
348 ElfW(Verdef) *ent;
349 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
350 while (1)
352 ElfW(Verdaux) *aux;
353 aux = (ElfW(Verdaux) *) ((char *) ent + ent->vd_aux);
355 if ((ent->vd_flags & VER_FLG_BASE) == 0)
357 /* The name of the base version should not be
358 available for matching a versioned symbol. */
359 ElfW(Half) ndx = ent->vd_ndx & 0x7fff;
360 map->l_versions[ndx].hash = ent->vd_hash;
361 map->l_versions[ndx].name = &strtab[aux->vda_name];
362 map->l_versions[ndx].filename = NULL;
365 if (ent->vd_next == 0)
366 /* No more definitions. */
367 break;
369 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
374 return result;
379 internal_function
380 _dl_check_all_versions (struct link_map *map, int verbose, int trace_mode)
382 struct link_map *l;
383 int result = 0;
385 for (l = map; l != NULL; l = l->l_next)
386 result |= (! l->l_faked
387 && _dl_check_map_versions (l, verbose, trace_mode));
389 return result;