Update.
[glibc.git] / elf / dl-version.c
blob9ba91b2efeb890057df7d1e0abc29fcec376b102
1 /* Handle symbol and library versioning.
2 Copyright (C) 1997,1998,1999,2000,2001,2002 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <elf.h>
22 #include <errno.h>
23 #include <libintl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ldsodefs.h>
27 #include <stdio-common/_itoa.h>
29 #include <assert.h>
32 #ifndef VERSYMIDX
33 # define VERSYMIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
34 #endif
37 #define make_string(string, rest...) \
38 ({ \
39 const char *all[] = { string, ## rest }; \
40 size_t len, cnt; \
41 char *result, *cp; \
43 len = 1; \
44 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
45 len += strlen (all[cnt]); \
47 cp = result = alloca (len); \
48 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
49 cp = __stpcpy (cp, all[cnt]); \
51 result; \
55 static inline struct link_map *
56 find_needed (const char *name, struct link_map *map)
58 struct link_map *tmap;
59 unsigned int n;
61 for (tmap = GL(dl_loaded); tmap != NULL; tmap = tmap->l_next)
62 if (_dl_name_match_p (name, tmap))
63 return tmap;
65 /* The required object is not in the global scope, look to see if it is
66 a dependency of the current object. */
67 for (n = 0; n < map->l_searchlist.r_nlist; n++)
68 if (_dl_name_match_p (name, map->l_searchlist.r_list[n]))
69 return map->l_searchlist.r_list[n];
71 /* Should never happen. */
72 return NULL;
76 static int
77 internal_function
78 match_symbol (const char *name, ElfW(Word) hash, const char *string,
79 struct link_map *map, int verbose, int weak)
81 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
82 ElfW(Addr) def_offset;
83 ElfW(Verdef) *def;
84 /* Initialize to make the compiler happy. */
85 const char *errstring = NULL;
86 int result = 0;
88 /* Display information about what we are doing while debugging. */
89 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_VERSIONS, 0))
90 INT(_dl_debug_printf) ("\
91 checking for version `%s' in file %s required by file %s\n",
92 string, map->l_name[0]
93 ? map->l_name : _dl_argv[0], name);
95 if (__builtin_expect (map->l_info[VERSYMIDX (DT_VERDEF)] == NULL, 0))
97 /* The file has no symbol versioning. I.e., the dependent
98 object was linked against another version of this file. We
99 only print a message if verbose output is requested. */
100 if (verbose)
102 /* XXX We cannot translate the messages. */
103 errstring = make_string ("\
104 no version information available (required by ", name, ")");
105 goto call_cerror;
107 return 0;
110 def_offset = map->l_info[VERSYMIDX (DT_VERDEF)]->d_un.d_ptr;
111 assert (def_offset != 0);
113 def = (ElfW(Verdef) *) ((char *) map->l_addr + def_offset);
114 while (1)
116 /* Currently the version number of the definition entry is 1.
117 Make sure all we see is this version. */
118 if (__builtin_expect (def->vd_version, 1) != 1)
120 char buf[20];
121 buf[sizeof (buf) - 1] = '\0';
122 /* XXX We cannot translate the message. */
123 errstring = make_string ("unsupported version ",
124 _itoa_word (def->vd_version,
125 &buf[sizeof (buf) - 1], 10, 0),
126 " of Verdef record");
127 result = 1;
128 goto call_cerror;
131 /* Compare the hash values. */
132 if (hash == def->vd_hash)
134 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
136 /* To be safe, compare the string as well. */
137 if (__builtin_expect (strcmp (string, strtab + aux->vda_name), 0)
138 == 0)
139 /* Bingo! */
140 return 0;
143 /* If no more definitions we failed to find what we want. */
144 if (def->vd_next == 0)
145 break;
147 /* Next definition. */
148 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
151 /* Symbol not found. If it was a weak reference it is not fatal. */
152 if (__builtin_expect (weak, 1))
154 if (verbose)
156 /* XXX We cannot translate the message. */
157 errstring = make_string ("weak version `", string,
158 "' not found (required by ", name, ")");
159 goto call_cerror;
161 return 0;
164 /* XXX We cannot translate the message. */
165 errstring = make_string ("version `", string, "' not found (required by ",
166 name, ")");
167 result = 1;
168 call_cerror:
169 _dl_signal_cerror (0, map->l_name[0] ? map->l_name : _dl_argv[0],
170 NULL, errstring);
171 return result;
176 internal_function
177 _dl_check_map_versions (struct link_map *map, int verbose, int trace_mode)
179 int result = 0;
180 const char *strtab;
181 /* Pointer to section with needed versions. */
182 ElfW(Dyn) *dyn;
183 /* Pointer to dynamic section with definitions. */
184 ElfW(Dyn) *def;
185 /* We need to find out which is the highest version index used
186 in a dependecy. */
187 unsigned int ndx_high = 0;
188 /* Initialize to make the compiler happy. */
189 const char *errstring = NULL;
190 int errval = 0;
192 /* If we don't have a string table, we must be ok. */
193 if (map->l_info[DT_STRTAB] == NULL)
194 return 0;
195 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
197 dyn = map->l_info[VERSYMIDX (DT_VERNEED)];
198 def = map->l_info[VERSYMIDX (DT_VERDEF)];
200 if (dyn != NULL)
202 /* This file requires special versions from its dependencies. */
203 ElfW(Verneed) *ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
205 /* Currently the version number of the needed entry is 1.
206 Make sure all we see is this version. */
207 if (__builtin_expect (ent->vn_version, 1) != 1)
209 char buf[20];
210 buf[sizeof (buf) - 1] = '\0';
211 /* XXX We cannot translate the message. */
212 errstring = make_string ("unsupported version ",
213 _itoa_word (ent->vn_version,
214 &buf[sizeof (buf) - 1], 10, 0),
215 " of Verneed record\n");
216 call_error:
217 INT(_dl_signal_error) (errval, (*map->l_name
218 ? map->l_name : _dl_argv[0]),
219 NULL, errstring);
222 while (1)
224 ElfW(Vernaux) *aux;
225 struct link_map *needed = find_needed (strtab + ent->vn_file, map);
227 /* If NEEDED is NULL this means a dependency was not found
228 and no stub entry was created. This should never happen. */
229 assert (needed != NULL);
231 /* Make sure this is no stub we created because of a missing
232 dependency. */
233 if (__builtin_expect (! trace_mode, 1)
234 || ! __builtin_expect (needed->l_faked, 0))
236 /* NEEDED is the map for the file we need. Now look for the
237 dependency symbols. */
238 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
239 while (1)
241 /* Match the symbol. */
242 result |= match_symbol ((*map->l_name
243 ? map->l_name : _dl_argv[0]),
244 aux->vna_hash,
245 strtab + aux->vna_name,
246 needed, verbose,
247 aux->vna_flags & VER_FLG_WEAK);
249 /* Compare the version index. */
250 if ((unsigned int) (aux->vna_other & 0x7fff) > ndx_high)
251 ndx_high = aux->vna_other & 0x7fff;
253 if (aux->vna_next == 0)
254 /* No more symbols. */
255 break;
257 /* Next symbol. */
258 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
262 if (ent->vn_next == 0)
263 /* No more dependencies. */
264 break;
266 /* Next dependency. */
267 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
271 /* We also must store the names of the defined versions. Determine
272 the maximum index here as well.
274 XXX We could avoid the loop by just taking the number of definitions
275 as an upper bound of new indeces. */
276 if (def != NULL)
278 ElfW(Verdef) *ent;
279 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
280 while (1)
282 if ((unsigned int) (ent->vd_ndx & 0x7fff) > ndx_high)
283 ndx_high = ent->vd_ndx & 0x7fff;
285 if (ent->vd_next == 0)
286 /* No more definitions. */
287 break;
289 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
293 if (ndx_high > 0)
295 /* Now we are ready to build the array with the version names
296 which can be indexed by the version index in the VERSYM
297 section. */
298 map->l_versions = (struct r_found_version *)
299 calloc (ndx_high + 1, sizeof (*map->l_versions));
300 if (__builtin_expect (map->l_versions == NULL, 0))
302 errstring = N_("cannot allocate version reference table");
303 errval = ENOMEM;
304 goto call_error;
307 /* Store the number of available symbols. */
308 map->l_nversions = ndx_high + 1;
310 /* Compute the pointer to the version symbols. */
311 map->l_versyms = (void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
313 if (dyn != NULL)
315 ElfW(Verneed) *ent;
316 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
317 while (1)
319 ElfW(Vernaux) *aux;
320 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
321 while (1)
323 ElfW(Half) ndx = aux->vna_other & 0x7fff;
324 map->l_versions[ndx].hash = aux->vna_hash;
325 map->l_versions[ndx].hidden = aux->vna_other & 0x8000;
326 map->l_versions[ndx].name = &strtab[aux->vna_name];
327 map->l_versions[ndx].filename = &strtab[ent->vn_file];
329 if (aux->vna_next == 0)
330 /* No more symbols. */
331 break;
333 /* Advance to next symbol. */
334 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
337 if (ent->vn_next == 0)
338 /* No more dependencies. */
339 break;
341 /* Advance to next dependency. */
342 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
346 /* And insert the defined versions. */
347 if (def != NULL)
349 ElfW(Verdef) *ent;
350 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
351 while (1)
353 ElfW(Verdaux) *aux;
354 aux = (ElfW(Verdaux) *) ((char *) ent + ent->vd_aux);
356 if ((ent->vd_flags & VER_FLG_BASE) == 0)
358 /* The name of the base version should not be
359 available for matching a versioned symbol. */
360 ElfW(Half) ndx = ent->vd_ndx & 0x7fff;
361 map->l_versions[ndx].hash = ent->vd_hash;
362 map->l_versions[ndx].name = &strtab[aux->vda_name];
363 map->l_versions[ndx].filename = NULL;
366 if (ent->vd_next == 0)
367 /* No more definitions. */
368 break;
370 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
375 return result;
377 INTDEF (_dl_check_map_versions)
381 internal_function
382 _dl_check_all_versions (struct link_map *map, int verbose, int trace_mode)
384 struct link_map *l;
385 int result = 0;
387 for (l = map; l != NULL; l = l->l_next)
388 result |= (! l->l_faked
389 && INT(_dl_check_map_versions) (l, verbose, trace_mode));
391 return result;