1 /* Handle symbol and library versioning.
2 Copyright (C) 1997, 1998, 1999 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. */
25 #include <elf/ldsodefs.h>
26 #include <stdio-common/_itoa.h>
32 # define VERSYMIDX(tag) (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (tag))
36 #define make_string(string, rest...) \
38 const char *all[] = { string, ## rest }; \
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]); \
54 static inline struct link_map *
55 find_needed (const char *name, struct link_map *map)
57 struct link_map *tmap;
60 for (tmap = _dl_loaded; tmap != NULL; tmap = tmap->l_next)
61 if (_dl_name_match_p (name, 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. */
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 *) map->l_info[DT_STRTAB]->d_un.d_ptr;
81 ElfW(Addr) def_offset;
84 /* Display information about what we are doing while debugging. */
85 if (_dl_debug_versions)
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 (map->l_info[VERSYMIDX (DT_VERDEF)] == NULL)
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. */
96 _dl_signal_error (0, map->l_name, make_string ("\
97 no version information available (required by ",
102 def_offset = map->l_info[VERSYMIDX (DT_VERDEF)]->d_un.d_ptr;
103 assert (def_offset != 0);
105 def = (ElfW(Verdef) *) ((char *) map->l_addr + def_offset);
108 /* Currently the version number of the definition entry is 1.
109 Make sure all we see is this version. */
110 if (def->vd_version != 1)
113 buf[sizeof (buf) - 1] = '\0';
114 _dl_signal_error (0, map->l_name,
115 make_string ("unsupported version ",
116 _itoa_word (def->vd_version,
117 &buf[sizeof (buf) - 1],
119 " of Verdef record"));
123 /* Compare the hash values. */
124 if (hash == def->vd_hash)
126 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
128 /* To be safe, compare the string as well. */
129 if (strcmp (string, strtab + aux->vda_name) == 0)
134 /* If no more definitions we failed to find what we want. */
135 if (def->vd_next == 0)
138 /* Next definition. */
139 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
142 /* Symbol not found. If it was a weak reference it is not fatal. */
146 _dl_signal_error (0, map->l_name,
147 make_string ("weak version `", string,
148 "' not found (required by ", name,
153 _dl_signal_error (0, map->l_name,
154 make_string ("version `", string,
155 "' not found (required by ", name, ")"));
162 _dl_check_map_versions (struct link_map *map, int verbose)
166 /* Pointer to section with needed versions. */
168 /* Pointer to dynamic section with definitions. */
170 /* We need to find out which is the highest version index used
172 unsigned int ndx_high = 0;
174 /* If we don't have a string table, we must be ok. */
175 if (map->l_info[DT_STRTAB] == NULL)
177 strtab = (const void *) map->l_info[DT_STRTAB]->d_un.d_ptr;
179 dyn = map->l_info[VERSYMIDX (DT_VERNEED)];
180 def = map->l_info[VERSYMIDX (DT_VERDEF)];
184 /* This file requires special versions from its dependencies. */
185 ElfW(Verneed) *ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
187 /* Currently the version number of the needed entry is 1.
188 Make sure all we see is this version. */
189 if (ent->vn_version != 1)
192 buf[sizeof (buf) - 1] = '\0';
193 _dl_signal_error (0, (*map->l_name ? map->l_name : _dl_argv[0]),
194 make_string ("unsupported version ",
195 _itoa_word (ent->vn_version,
196 &buf[sizeof (buf) - 1],
198 " of Verneed record\n"));
205 struct link_map *needed = find_needed (strtab + ent->vn_file, map);
207 /* If NEEDED is NULL this means a dependency was not found
208 and no stub entry was created. This should never happen. */
209 assert (needed != NULL);
211 /* NEEDED is the map for the file we need. Now look for the
212 dependency symbols. */
213 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
216 /* Match the symbol. */
217 result |= match_symbol ((*map->l_name
218 ? map->l_name : _dl_argv[0]),
220 strtab + aux->vna_name,
222 aux->vna_flags & VER_FLG_WEAK);
224 /* Compare the version index. */
225 if ((unsigned int) (aux->vna_other & 0x7fff) > ndx_high)
226 ndx_high = aux->vna_other & 0x7fff;
228 if (aux->vna_next == 0)
229 /* No more symbols. */
233 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
236 if (ent->vn_next == 0)
237 /* No more dependencies. */
240 /* Next dependency. */
241 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
245 /* We also must store the names of the defined versions. Determine
246 the maximum index here as well.
248 XXX We could avoid the loop by just taking the number of definitions
249 as an upper bound of new indeces. */
253 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
256 if ((unsigned int) (ent->vd_ndx & 0x7fff) > ndx_high)
257 ndx_high = ent->vd_ndx & 0x7fff;
259 if (ent->vd_next == 0)
260 /* No more definitions. */
263 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
269 /* Now we are ready to build the array with the version names
270 which can be indexed by the version index in the VERSYM
272 map->l_versions = (struct r_found_version *)
273 calloc (ndx_high + 1, sizeof (*map->l_versions));
274 if (map->l_versions == NULL)
276 _dl_signal_error (ENOMEM, (*map->l_name ? map->l_name : _dl_argv[0]),
277 "cannot allocate version reference table");
282 /* Store the number of available symbols. */
283 map->l_nversions = ndx_high + 1;
285 /* Compute the pointer to the version symbols. */
287 (void *) map->l_info[VERSYMIDX (DT_VERSYM)]->d_un.d_ptr;
292 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
296 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
299 ElfW(Half) ndx = aux->vna_other & 0x7fff;
300 map->l_versions[ndx].hash = aux->vna_hash;
301 map->l_versions[ndx].hidden = aux->vna_other & 0x8000;
302 map->l_versions[ndx].name = &strtab[aux->vna_name];
303 map->l_versions[ndx].filename = &strtab[ent->vn_file];
305 if (aux->vna_next == 0)
306 /* No more symbols. */
309 /* Advance to next symbol. */
310 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
313 if (ent->vn_next == 0)
314 /* No more dependencies. */
317 /* Advance to next dependency. */
318 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
322 /* And insert the defined versions. */
326 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
330 aux = (ElfW(Verdaux) *) ((char *) ent + ent->vd_aux);
332 if ((ent->vd_flags & VER_FLG_BASE) == 0)
334 /* The name of the base version should not be
335 available for matching a versioned symbol. */
336 ElfW(Half) ndx = ent->vd_ndx & 0x7fff;
337 map->l_versions[ndx].hash = ent->vd_hash;
338 map->l_versions[ndx].name = &strtab[aux->vda_name];
339 map->l_versions[ndx].filename = NULL;
342 if (ent->vd_next == 0)
343 /* No more definitions. */
346 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
358 _dl_check_all_versions (struct link_map *map, int verbose)
363 for (l = map; l != NULL; l = l->l_next)
364 result |= l->l_opencount != 0 && _dl_check_map_versions (l, verbose);