Update.
[glibc.git] / elf / dl-version.c
blob0718b8a5a8444efdd80b4110e91adc1d001a3a0e
1 /* Handle symbol and library versioning.
2 Copyright (C) 1997, 1998, 1999, 2000 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <elf.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ldsodefs.h>
26 #include <stdio-common/_itoa.h>
28 #include <assert.h>
31 #ifndef VERSYMIDX
32 # define VERSYMIDX(tag) (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (tag))
33 #endif
36 #define make_string(string, rest...) \
37 ({ \
38 const char *all[] = { string, ## rest }; \
39 size_t len, cnt; \
40 char *result, *cp; \
42 len = 1; \
43 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
44 len += strlen (all[cnt]); \
46 cp = result = alloca (len); \
47 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
48 cp = __stpcpy (cp, all[cnt]); \
50 result; \
54 static inline struct link_map *
55 find_needed (const char *name, struct link_map *map)
57 struct link_map *tmap;
58 unsigned int n;
60 for (tmap = _dl_loaded; tmap != NULL; tmap = tmap->l_next)
61 if (_dl_name_match_p (name, tmap))
62 return tmap;
64 /* The required object is not in the global scope, look to see if it is
65 a dependency of the current object. */
66 for (n = 0; n < map->l_searchlist.r_nlist; n++)
67 if (_dl_name_match_p (name, map->l_searchlist.r_list[n]))
68 return map->l_searchlist.r_list[n];
70 /* Should never happen. */
71 return NULL;
75 static int
76 internal_function
77 match_symbol (const char *name, ElfW(Word) hash, const char *string,
78 struct link_map *map, int verbose, int weak)
80 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
81 ElfW(Addr) def_offset;
82 ElfW(Verdef) *def;
84 /* Display information about what we are doing while debugging. */
85 if (__builtin_expect (_dl_debug_versions, 0))
86 _dl_debug_message (1, "checking for version `", string, "' in file ",
87 map->l_name[0] ? map->l_name : _dl_argv[0],
88 " required by file ", name, "\n", NULL);
90 if (__builtin_expect (map->l_info[VERSYMIDX (DT_VERDEF)] == NULL, 0))
92 /* The file has no symbol versioning. I.e., the dependent
93 object was linked against another version of this file. We
94 only print a message if verbose output is requested. */
95 if (verbose)
96 _dl_signal_cerror (0, map->l_name,
97 make_string ("\
98 no version information available (required by ",
99 name, ")"));
100 return 0;
103 def_offset = map->l_info[VERSYMIDX (DT_VERDEF)]->d_un.d_ptr;
104 assert (def_offset != 0);
106 def = (ElfW(Verdef) *) ((char *) map->l_addr + def_offset);
107 while (1)
109 /* Currently the version number of the definition entry is 1.
110 Make sure all we see is this version. */
111 if (__builtin_expect (def->vd_version, 1) != 1)
113 char buf[20];
114 buf[sizeof (buf) - 1] = '\0';
115 _dl_signal_error (0, map->l_name,
116 make_string ("unsupported version ",
117 _itoa_word (def->vd_version,
118 &buf[sizeof (buf) - 1],
119 10, 0),
120 " of Verdef record"));
121 return 1;
124 /* Compare the hash values. */
125 if (hash == def->vd_hash)
127 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
129 /* To be safe, compare the string as well. */
130 if (__builtin_expect (strcmp (string, strtab + aux->vda_name), 0)
131 == 0)
132 /* Bingo! */
133 return 0;
136 /* If no more definitions we failed to find what we want. */
137 if (def->vd_next == 0)
138 break;
140 /* Next definition. */
141 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
144 /* Symbol not found. If it was a weak reference it is not fatal. */
145 if (__builtin_expect (weak, 1))
147 if (verbose)
148 _dl_signal_cerror (0, map->l_name,
149 make_string ("weak version `", string,
150 "' not found (required by ", name,
151 ")"));
152 return 0;
155 _dl_signal_cerror (0, map->l_name,
156 make_string ("version `", string,
157 "' not found (required by ", name, ")"));
158 return 1;
163 internal_function
164 _dl_check_map_versions (struct link_map *map, int verbose, int trace_mode)
166 int result = 0;
167 const char *strtab;
168 /* Pointer to section with needed versions. */
169 ElfW(Dyn) *dyn;
170 /* Pointer to dynamic section with definitions. */
171 ElfW(Dyn) *def;
172 /* We need to find out which is the highest version index used
173 in a dependecy. */
174 unsigned int ndx_high = 0;
176 /* If we don't have a string table, we must be ok. */
177 if (map->l_info[DT_STRTAB] == NULL)
178 return 0;
179 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
181 dyn = map->l_info[VERSYMIDX (DT_VERNEED)];
182 def = map->l_info[VERSYMIDX (DT_VERDEF)];
184 if (dyn != NULL)
186 /* This file requires special versions from its dependencies. */
187 ElfW(Verneed) *ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
189 /* Currently the version number of the needed entry is 1.
190 Make sure all we see is this version. */
191 if (__builtin_expect (ent->vn_version, 1) != 1)
193 char buf[20];
194 buf[sizeof (buf) - 1] = '\0';
195 _dl_signal_error (0, (*map->l_name ? map->l_name : _dl_argv[0]),
196 make_string ("unsupported version ",
197 _itoa_word (ent->vn_version,
198 &buf[sizeof (buf) - 1],
199 10, 0),
200 " of Verneed record\n"));
201 return 1;
204 while (1)
206 ElfW(Vernaux) *aux;
207 struct link_map *needed = find_needed (strtab + ent->vn_file, map);
209 /* If NEEDED is NULL this means a dependency was not found
210 and no stub entry was created. This should never happen. */
211 assert (needed != NULL);
213 /* Make sure this is no stub we created because of a missing
214 dependency. */
215 if (__builtin_expect (! trace_mode, 1)
216 || __builtin_expect (needed->l_opencount, 1) != 0)
218 /* NEEDED is the map for the file we need. Now look for the
219 dependency symbols. */
220 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
221 while (1)
223 /* Match the symbol. */
224 result |= match_symbol ((*map->l_name
225 ? map->l_name : _dl_argv[0]),
226 aux->vna_hash,
227 strtab + aux->vna_name,
228 needed, verbose,
229 aux->vna_flags & VER_FLG_WEAK);
231 /* Compare the version index. */
232 if ((unsigned int) (aux->vna_other & 0x7fff) > ndx_high)
233 ndx_high = aux->vna_other & 0x7fff;
235 if (aux->vna_next == 0)
236 /* No more symbols. */
237 break;
239 /* Next symbol. */
240 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
244 if (ent->vn_next == 0)
245 /* No more dependencies. */
246 break;
248 /* Next dependency. */
249 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
253 /* We also must store the names of the defined versions. Determine
254 the maximum index here as well.
256 XXX We could avoid the loop by just taking the number of definitions
257 as an upper bound of new indeces. */
258 if (def != NULL)
260 ElfW(Verdef) *ent;
261 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
262 while (1)
264 if ((unsigned int) (ent->vd_ndx & 0x7fff) > ndx_high)
265 ndx_high = ent->vd_ndx & 0x7fff;
267 if (ent->vd_next == 0)
268 /* No more definitions. */
269 break;
271 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
275 if (ndx_high > 0)
277 /* Now we are ready to build the array with the version names
278 which can be indexed by the version index in the VERSYM
279 section. */
280 map->l_versions = (struct r_found_version *)
281 calloc (ndx_high + 1, sizeof (*map->l_versions));
282 if (__builtin_expect (map->l_versions == NULL, 0))
284 _dl_signal_error (ENOMEM, (*map->l_name ? map->l_name : _dl_argv[0]),
285 "cannot allocate version reference table");
286 result = 1;
288 else
290 /* Store the number of available symbols. */
291 map->l_nversions = ndx_high + 1;
293 /* Compute the pointer to the version symbols. */
294 map->l_versyms =
295 (void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
297 if (dyn != NULL)
299 ElfW(Verneed) *ent;
300 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
301 while (1)
303 ElfW(Vernaux) *aux;
304 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
305 while (1)
307 ElfW(Half) ndx = aux->vna_other & 0x7fff;
308 map->l_versions[ndx].hash = aux->vna_hash;
309 map->l_versions[ndx].hidden = aux->vna_other & 0x8000;
310 map->l_versions[ndx].name = &strtab[aux->vna_name];
311 map->l_versions[ndx].filename = &strtab[ent->vn_file];
313 if (aux->vna_next == 0)
314 /* No more symbols. */
315 break;
317 /* Advance to next symbol. */
318 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
321 if (ent->vn_next == 0)
322 /* No more dependencies. */
323 break;
325 /* Advance to next dependency. */
326 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
330 /* And insert the defined versions. */
331 if (def != NULL)
333 ElfW(Verdef) *ent;
334 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
335 while (1)
337 ElfW(Verdaux) *aux;
338 aux = (ElfW(Verdaux) *) ((char *) ent + ent->vd_aux);
340 if ((ent->vd_flags & VER_FLG_BASE) == 0)
342 /* The name of the base version should not be
343 available for matching a versioned symbol. */
344 ElfW(Half) ndx = ent->vd_ndx & 0x7fff;
345 map->l_versions[ndx].hash = ent->vd_hash;
346 map->l_versions[ndx].name = &strtab[aux->vda_name];
347 map->l_versions[ndx].filename = NULL;
350 if (ent->vd_next == 0)
351 /* No more definitions. */
352 break;
354 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
360 return result;
365 internal_function
366 _dl_check_all_versions (struct link_map *map, int verbose, int trace_mode)
368 struct link_map *l;
369 int result = 0;
371 for (l = map; l != NULL; l = l->l_next)
372 result |= (l->l_opencount != 0
373 && _dl_check_map_versions (l, verbose, trace_mode));
375 return result;