* sysdeps/m68k/setjmp.c: Also define setjmp and _setjmp if
[glibc.git] / elf / dl-version.c
blob239507ee3739eb2e0515a5461cc39e1f188e4628
1 /* Handle symbol and library versioning.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001 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 = _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;
85 /* Display information about what we are doing while debugging. */
86 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_VERSIONS, 0))
87 _dl_debug_printf ("\
88 checking for version `%s' in file %s required by file %s\n",
89 string, map->l_name[0] ? map->l_name : _dl_argv[0],
90 name);
92 if (__builtin_expect (map->l_info[VERSYMIDX (DT_VERDEF)] == NULL, 0))
94 /* The file has no symbol versioning. I.e., the dependent
95 object was linked against another version of this file. We
96 only print a message if verbose output is requested. */
97 if (verbose)
98 /* XXX We cannot translate the messages. */
99 _dl_signal_cerror (0, map->l_name[0] ? map->l_name : _dl_argv[0], NULL,
100 make_string ("\
101 no version information available (required by ",
102 name, ")"));
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 _dl_signal_error (0, map->l_name[0] ? map->l_name : _dl_argv[0],
120 NULL,
121 make_string ("unsupported version ",
122 _itoa_word (def->vd_version,
123 &buf[sizeof (buf) - 1],
124 10, 0),
125 " of Verdef record"));
126 return 1;
129 /* Compare the hash values. */
130 if (hash == def->vd_hash)
132 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
134 /* To be safe, compare the string as well. */
135 if (__builtin_expect (strcmp (string, strtab + aux->vda_name), 0)
136 == 0)
137 /* Bingo! */
138 return 0;
141 /* If no more definitions we failed to find what we want. */
142 if (def->vd_next == 0)
143 break;
145 /* Next definition. */
146 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
149 /* Symbol not found. If it was a weak reference it is not fatal. */
150 if (__builtin_expect (weak, 1))
152 if (verbose)
153 /* XXX We cannot translate the message. */
154 _dl_signal_cerror (0, map->l_name[0] ? map->l_name : _dl_argv[0], NULL,
155 make_string ("weak version `", string,
156 "' not found (required by ", name,
157 ")"));
158 return 0;
161 /* XXX We cannot translate the message. */
162 _dl_signal_cerror (0, map->l_name[0] ? map->l_name : _dl_argv[0], NULL,
163 make_string ("version `", string,
164 "' not found (required by ", name, ")"));
165 return 1;
170 internal_function
171 _dl_check_map_versions (struct link_map *map, int verbose, int trace_mode)
173 int result = 0;
174 const char *strtab;
175 /* Pointer to section with needed versions. */
176 ElfW(Dyn) *dyn;
177 /* Pointer to dynamic section with definitions. */
178 ElfW(Dyn) *def;
179 /* We need to find out which is the highest version index used
180 in a dependecy. */
181 unsigned int ndx_high = 0;
183 /* If we don't have a string table, we must be ok. */
184 if (map->l_info[DT_STRTAB] == NULL)
185 return 0;
186 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
188 dyn = map->l_info[VERSYMIDX (DT_VERNEED)];
189 def = map->l_info[VERSYMIDX (DT_VERDEF)];
191 if (dyn != NULL)
193 /* This file requires special versions from its dependencies. */
194 ElfW(Verneed) *ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
196 /* Currently the version number of the needed entry is 1.
197 Make sure all we see is this version. */
198 if (__builtin_expect (ent->vn_version, 1) != 1)
200 char buf[20];
201 buf[sizeof (buf) - 1] = '\0';
202 /* XXX We cannot translate the message. */
203 _dl_signal_error (0, (*map->l_name ? map->l_name : _dl_argv[0]),
204 NULL,
205 make_string ("unsupported version ",
206 _itoa_word (ent->vn_version,
207 &buf[sizeof (buf) - 1],
208 10, 0),
209 " of Verneed record\n"));
210 return 1;
213 while (1)
215 ElfW(Vernaux) *aux;
216 struct link_map *needed = find_needed (strtab + ent->vn_file, map);
218 /* If NEEDED is NULL this means a dependency was not found
219 and no stub entry was created. This should never happen. */
220 assert (needed != NULL);
222 /* Make sure this is no stub we created because of a missing
223 dependency. */
224 if (__builtin_expect (! trace_mode, 1)
225 || ! __builtin_expect (needed->l_faked, 0))
227 /* NEEDED is the map for the file we need. Now look for the
228 dependency symbols. */
229 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
230 while (1)
232 /* Match the symbol. */
233 result |= match_symbol ((*map->l_name
234 ? map->l_name : _dl_argv[0]),
235 aux->vna_hash,
236 strtab + aux->vna_name,
237 needed, verbose,
238 aux->vna_flags & VER_FLG_WEAK);
240 /* Compare the version index. */
241 if ((unsigned int) (aux->vna_other & 0x7fff) > ndx_high)
242 ndx_high = aux->vna_other & 0x7fff;
244 if (aux->vna_next == 0)
245 /* No more symbols. */
246 break;
248 /* Next symbol. */
249 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
253 if (ent->vn_next == 0)
254 /* No more dependencies. */
255 break;
257 /* Next dependency. */
258 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
262 /* We also must store the names of the defined versions. Determine
263 the maximum index here as well.
265 XXX We could avoid the loop by just taking the number of definitions
266 as an upper bound of new indeces. */
267 if (def != NULL)
269 ElfW(Verdef) *ent;
270 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
271 while (1)
273 if ((unsigned int) (ent->vd_ndx & 0x7fff) > ndx_high)
274 ndx_high = ent->vd_ndx & 0x7fff;
276 if (ent->vd_next == 0)
277 /* No more definitions. */
278 break;
280 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
284 if (ndx_high > 0)
286 /* Now we are ready to build the array with the version names
287 which can be indexed by the version index in the VERSYM
288 section. */
289 map->l_versions = (struct r_found_version *)
290 calloc (ndx_high + 1, sizeof (*map->l_versions));
291 if (__builtin_expect (map->l_versions == NULL, 0))
293 _dl_signal_error (ENOMEM, (*map->l_name ? map->l_name : _dl_argv[0]),
294 NULL,
295 N_("cannot allocate version reference table"));
296 result = 1;
298 else
300 /* Store the number of available symbols. */
301 map->l_nversions = ndx_high + 1;
303 /* Compute the pointer to the version symbols. */
304 map->l_versyms =
305 (void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
307 if (dyn != NULL)
309 ElfW(Verneed) *ent;
310 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
311 while (1)
313 ElfW(Vernaux) *aux;
314 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
315 while (1)
317 ElfW(Half) ndx = aux->vna_other & 0x7fff;
318 map->l_versions[ndx].hash = aux->vna_hash;
319 map->l_versions[ndx].hidden = aux->vna_other & 0x8000;
320 map->l_versions[ndx].name = &strtab[aux->vna_name];
321 map->l_versions[ndx].filename = &strtab[ent->vn_file];
323 if (aux->vna_next == 0)
324 /* No more symbols. */
325 break;
327 /* Advance to next symbol. */
328 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
331 if (ent->vn_next == 0)
332 /* No more dependencies. */
333 break;
335 /* Advance to next dependency. */
336 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
340 /* And insert the defined versions. */
341 if (def != NULL)
343 ElfW(Verdef) *ent;
344 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
345 while (1)
347 ElfW(Verdaux) *aux;
348 aux = (ElfW(Verdaux) *) ((char *) ent + ent->vd_aux);
350 if ((ent->vd_flags & VER_FLG_BASE) == 0)
352 /* The name of the base version should not be
353 available for matching a versioned symbol. */
354 ElfW(Half) ndx = ent->vd_ndx & 0x7fff;
355 map->l_versions[ndx].hash = ent->vd_hash;
356 map->l_versions[ndx].name = &strtab[aux->vda_name];
357 map->l_versions[ndx].filename = NULL;
360 if (ent->vd_next == 0)
361 /* No more definitions. */
362 break;
364 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
370 return result;
375 internal_function
376 _dl_check_all_versions (struct link_map *map, int verbose, int trace_mode)
378 struct link_map *l;
379 int result = 0;
381 for (l = map; l != NULL; l = l->l_next)
382 result |= ! l->l_faked && _dl_check_map_versions (l, verbose, trace_mode);
384 return result;