1 /* Handle symbol and library versioning.
2 Copyright (C) 1997 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. */
28 #include <stdio-common/_itoa.h>
31 /* Set in rtld.c at startup. */
32 extern char **_dl_argv;
34 #define VERSTAG(tag) (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (tag))
37 #define make_string(string, rest...) \
39 const char *all[] = { string, ## rest }; \
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]); \
55 static inline struct link_map *
56 find_needed (const char *name)
60 for (n = 0; n < _dl_loaded->l_nsearchlist; ++n)
61 if (_dl_name_match_p (name, _dl_loaded->l_searchlist[n]))
62 return _dl_loaded->l_searchlist[n];
64 /* Should never happen. */
70 match_symbol (const char *name, ElfW(Word) hash, const char *string,
71 struct link_map *map, int verbose, int weak)
73 const char *strtab = (const char *) (map->l_addr
74 + map->l_info[DT_STRTAB]->d_un.d_ptr);
75 ElfW(Addr) def_offset;
78 if (map->l_info[VERSTAG (DT_VERDEF)] == NULL)
80 /* The file has no symbol versioning. I.e., the dependent
81 object was linked against another version of this file. We
82 only print a message if verbose output is requested. */
84 _dl_signal_error (0, map->l_name, make_string ("\
85 no version information available (required by ",
90 def_offset = map->l_info[VERSTAG (DT_VERDEF)]->d_un.d_ptr;
91 assert (def_offset != 0);
93 def = (ElfW(Verdef) *) ((char *) map->l_addr + def_offset);
96 /* Currently the version number of the definition entry is 1.
97 Make sure all we see is this version. */
98 if (def->vd_version != 1)
101 buf[sizeof (buf) - 1] = '\0';
102 _dl_signal_error (0, map->l_name,
103 make_string ("unsupported version ",
104 _itoa_word (def->vd_version,
105 &buf[sizeof (buf) - 1],
107 " of Verdef record"));
111 /* Compare the hash values. */
112 if (hash == def->vd_hash)
114 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
116 /* To be safe, compare the string as well. */
117 if (strcmp (string, strtab + aux->vda_name) == 0)
122 /* If no more definitions we failed to find what we want. */
123 if (def->vd_next == 0)
126 /* Next definition. */
127 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
130 /* Symbol not found. If it was a weak reference it is not fatal. */
134 _dl_signal_error (0, map->l_name,
135 make_string ("weak version `", string,
136 "' not found (required by ", name,
141 _dl_signal_error (0, map->l_name,
142 make_string ("version `", string,
143 "' not found (required by ", name, ")"));
149 _dl_check_map_versions (struct link_map *map, int verbose)
152 const char *strtab = (const char *) (map->l_addr
153 + map->l_info[DT_STRTAB]->d_un.d_ptr);
154 /* Pointer to section with needed versions. */
155 ElfW(Dyn) *dyn = map->l_info[VERSTAG (DT_VERNEED)];
156 /* Pointer to dynamic section with definitions. */
157 ElfW(Dyn) *def = map->l_info[VERSTAG (DT_VERDEF)];
158 /* We need to find out which is the highest version index used
160 unsigned int ndx_high = 0;
164 /* This file requires special versions from its dependencies. */
165 ElfW(Verneed) *ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
167 /* Currently the version number of the needed entry is 1.
168 Make sure all we see is this version. */
169 if (ent->vn_version != 1)
172 buf[sizeof (buf) - 1] = '\0';
173 _dl_signal_error (0, (*map->l_name ? map->l_name : _dl_argv[0]),
174 make_string ("unsupported version ",
175 _itoa_word (ent->vn_version,
176 &buf[sizeof (buf) - 1],
178 " of Verneed record\n"));
185 struct link_map *needed = find_needed (strtab + ent->vn_file);
187 /* If NEEDED is NULL this means a dependency was not found
188 and no stub entry was created. This should never happen. */
189 assert (needed != NULL);
191 /* NEEDED is the map for the file we need. Now look for the
192 dependency symbols. */
193 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
196 /* Match the symbol. */
197 result |= match_symbol ((*map->l_name
198 ? map->l_name : _dl_argv[0]),
200 strtab + aux->vna_name,
202 aux->vna_flags & VER_FLG_WEAK);
204 /* Compare the version index. */
205 if ((unsigned int) (aux->vna_other & 0x7fff) > ndx_high)
206 ndx_high = aux->vna_other & 0x7fff;
208 if (aux->vna_next == 0)
209 /* No more symbols. */
213 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
216 if (ent->vn_next == 0)
217 /* No more dependencies. */
220 /* Next dependency. */
221 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
225 /* We also must store the names of the defined versions. Determine
226 the maximum index here as well.
228 XXX We could avoid the loop by just taking the number of definitions
229 as an upper bound of new indeces. */
233 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
236 if ((unsigned int) (ent->vd_ndx & 0x7fff) > ndx_high)
237 ndx_high = ent->vd_ndx & 0x7fff;
239 if (ent->vd_next == 0)
240 /* No more definitions. */
243 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
249 /* Now we are ready to build the array with the version names
250 which can be indexed by the version index in the VERSYM
252 map->l_versions = (struct r_found_version *)
253 calloc (ndx_high + 1, sizeof (*map->l_versions));
254 if (map->l_versions == NULL)
256 _dl_signal_error (ENOMEM, (*map->l_name ? map->l_name : _dl_argv[0]),
257 "cannot allocate version reference table");
262 /* Store the number of available symbols. */
263 map->l_nversions = ndx_high + 1;
268 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
272 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
275 ElfW(Half) ndx = aux->vna_other & 0x7fff;
276 map->l_versions[ndx].hash = aux->vna_hash;
277 map->l_versions[ndx].hidden = aux->vna_other & 0x8000;
278 map->l_versions[ndx].name = &strtab[aux->vna_name];
279 map->l_versions[ndx].filename = &strtab[ent->vn_file];
281 if (aux->vna_next == 0)
282 /* No more symbols. */
285 /* Advance to next symbol. */
286 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
289 if (ent->vn_next == 0)
290 /* No more dependencies. */
293 /* Advance to next dependency. */
294 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
298 /* And insert the defined versions. */
302 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
306 aux = (ElfW(Verdaux) *) ((char *) ent + ent->vd_aux);
308 if ((ent->vd_flags & VER_FLG_BASE) == 0)
310 /* The name of the base version should not be
311 available for matching a versioned symbol. */
312 ElfW(Half) ndx = ent->vd_ndx & 0x7fff;
313 map->l_versions[ndx].hash = ent->vd_hash;
314 map->l_versions[ndx].name = &strtab[aux->vda_name];
315 map->l_versions[ndx].filename = NULL;
318 if (ent->vd_next == 0)
319 /* No more definitions. */
322 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
333 _dl_check_all_versions (struct link_map *map, int verbose)
338 for (l = map; l != NULL; l = l->l_next)
339 result |= l->l_opencount != 0 && _dl_check_map_versions (l, verbose);