]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-version.c
Update.
[thirdparty/glibc.git] / elf / dl-version.c
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.
5
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.
10
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.
15
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. */
20
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>
28
29 #include <assert.h>
30
31
32 #ifndef VERSYMIDX
33 # define VERSYMIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
34 #endif
35
36
37 #define make_string(string, rest...) \
38 ({ \
39 const char *all[] = { string, ## rest }; \
40 size_t len, cnt; \
41 char *result, *cp; \
42 \
43 len = 1; \
44 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
45 len += strlen (all[cnt]); \
46 \
47 cp = result = alloca (len); \
48 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
49 cp = __stpcpy (cp, all[cnt]); \
50 \
51 result; \
52 })
53
54
55 static inline struct link_map *
56 find_needed (const char *name, struct link_map *map)
57 {
58 struct link_map *tmap;
59 unsigned int n;
60
61 for (tmap = _dl_loaded; tmap != NULL; tmap = tmap->l_next)
62 if (_dl_name_match_p (name, tmap))
63 return tmap;
64
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];
70
71 /* Should never happen. */
72 return NULL;
73 }
74
75
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)
80 {
81 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
82 ElfW(Addr) def_offset;
83 ElfW(Verdef) *def;
84
85 /* Display information about what we are doing while debugging. */
86 if (__builtin_expect (_dl_debug_versions, 0))
87 _dl_debug_message (1, "checking for version `", string, "' in file ",
88 map->l_name[0] ? map->l_name : _dl_argv[0],
89 " required by file ", name, "\n", NULL);
90
91 if (__builtin_expect (map->l_info[VERSYMIDX (DT_VERDEF)] == NULL, 0))
92 {
93 /* The file has no symbol versioning. I.e., the dependent
94 object was linked against another version of this file. We
95 only print a message if verbose output is requested. */
96 if (verbose)
97 /* XXX We cannot translate the messages. */
98 _dl_signal_cerror (0, map->l_name,
99 make_string ("\
100 no version information available (required by ",
101 name, ")"));
102 return 0;
103 }
104
105 def_offset = map->l_info[VERSYMIDX (DT_VERDEF)]->d_un.d_ptr;
106 assert (def_offset != 0);
107
108 def = (ElfW(Verdef) *) ((char *) map->l_addr + def_offset);
109 while (1)
110 {
111 /* Currently the version number of the definition entry is 1.
112 Make sure all we see is this version. */
113 if (__builtin_expect (def->vd_version, 1) != 1)
114 {
115 char buf[20];
116 buf[sizeof (buf) - 1] = '\0';
117 /* XXX We cannot translate the message. */
118 _dl_signal_error (0, map->l_name,
119 make_string ("unsupported version ",
120 _itoa_word (def->vd_version,
121 &buf[sizeof (buf) - 1],
122 10, 0),
123 " of Verdef record"));
124 return 1;
125 }
126
127 /* Compare the hash values. */
128 if (hash == def->vd_hash)
129 {
130 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
131
132 /* To be safe, compare the string as well. */
133 if (__builtin_expect (strcmp (string, strtab + aux->vda_name), 0)
134 == 0)
135 /* Bingo! */
136 return 0;
137 }
138
139 /* If no more definitions we failed to find what we want. */
140 if (def->vd_next == 0)
141 break;
142
143 /* Next definition. */
144 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
145 }
146
147 /* Symbol not found. If it was a weak reference it is not fatal. */
148 if (__builtin_expect (weak, 1))
149 {
150 if (verbose)
151 /* XXX We cannot translate the message. */
152 _dl_signal_cerror (0, map->l_name,
153 make_string ("weak version `", string,
154 "' not found (required by ", name,
155 ")"));
156 return 0;
157 }
158
159 /* XXX We cannot translate the message. */
160 _dl_signal_cerror (0, map->l_name,
161 make_string ("version `", string,
162 "' not found (required by ", name, ")"));
163 return 1;
164 }
165
166
167 int
168 internal_function
169 _dl_check_map_versions (struct link_map *map, int verbose, int trace_mode)
170 {
171 int result = 0;
172 const char *strtab;
173 /* Pointer to section with needed versions. */
174 ElfW(Dyn) *dyn;
175 /* Pointer to dynamic section with definitions. */
176 ElfW(Dyn) *def;
177 /* We need to find out which is the highest version index used
178 in a dependecy. */
179 unsigned int ndx_high = 0;
180
181 /* If we don't have a string table, we must be ok. */
182 if (map->l_info[DT_STRTAB] == NULL)
183 return 0;
184 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
185
186 dyn = map->l_info[VERSYMIDX (DT_VERNEED)];
187 def = map->l_info[VERSYMIDX (DT_VERDEF)];
188
189 if (dyn != NULL)
190 {
191 /* This file requires special versions from its dependencies. */
192 ElfW(Verneed) *ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
193
194 /* Currently the version number of the needed entry is 1.
195 Make sure all we see is this version. */
196 if (__builtin_expect (ent->vn_version, 1) != 1)
197 {
198 char buf[20];
199 buf[sizeof (buf) - 1] = '\0';
200 /* XXX We cannot translate the message. */
201 _dl_signal_error (0, (*map->l_name ? map->l_name : _dl_argv[0]),
202 make_string ("unsupported version ",
203 _itoa_word (ent->vn_version,
204 &buf[sizeof (buf) - 1],
205 10, 0),
206 " of Verneed record\n"));
207 return 1;
208 }
209
210 while (1)
211 {
212 ElfW(Vernaux) *aux;
213 struct link_map *needed = find_needed (strtab + ent->vn_file, map);
214
215 /* If NEEDED is NULL this means a dependency was not found
216 and no stub entry was created. This should never happen. */
217 assert (needed != NULL);
218
219 /* Make sure this is no stub we created because of a missing
220 dependency. */
221 if (__builtin_expect (! trace_mode, 1)
222 || ! __builtin_expect (needed->l_faked, 0))
223 {
224 /* NEEDED is the map for the file we need. Now look for the
225 dependency symbols. */
226 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
227 while (1)
228 {
229 /* Match the symbol. */
230 result |= match_symbol ((*map->l_name
231 ? map->l_name : _dl_argv[0]),
232 aux->vna_hash,
233 strtab + aux->vna_name,
234 needed, verbose,
235 aux->vna_flags & VER_FLG_WEAK);
236
237 /* Compare the version index. */
238 if ((unsigned int) (aux->vna_other & 0x7fff) > ndx_high)
239 ndx_high = aux->vna_other & 0x7fff;
240
241 if (aux->vna_next == 0)
242 /* No more symbols. */
243 break;
244
245 /* Next symbol. */
246 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
247 }
248 }
249
250 if (ent->vn_next == 0)
251 /* No more dependencies. */
252 break;
253
254 /* Next dependency. */
255 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
256 }
257 }
258
259 /* We also must store the names of the defined versions. Determine
260 the maximum index here as well.
261
262 XXX We could avoid the loop by just taking the number of definitions
263 as an upper bound of new indeces. */
264 if (def != NULL)
265 {
266 ElfW(Verdef) *ent;
267 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
268 while (1)
269 {
270 if ((unsigned int) (ent->vd_ndx & 0x7fff) > ndx_high)
271 ndx_high = ent->vd_ndx & 0x7fff;
272
273 if (ent->vd_next == 0)
274 /* No more definitions. */
275 break;
276
277 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
278 }
279 }
280
281 if (ndx_high > 0)
282 {
283 /* Now we are ready to build the array with the version names
284 which can be indexed by the version index in the VERSYM
285 section. */
286 map->l_versions = (struct r_found_version *)
287 calloc (ndx_high + 1, sizeof (*map->l_versions));
288 if (__builtin_expect (map->l_versions == NULL, 0))
289 {
290 _dl_signal_error (ENOMEM, (*map->l_name ? map->l_name : _dl_argv[0]),
291 N_("cannot allocate version reference table"));
292 result = 1;
293 }
294 else
295 {
296 /* Store the number of available symbols. */
297 map->l_nversions = ndx_high + 1;
298
299 /* Compute the pointer to the version symbols. */
300 map->l_versyms =
301 (void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
302
303 if (dyn != NULL)
304 {
305 ElfW(Verneed) *ent;
306 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
307 while (1)
308 {
309 ElfW(Vernaux) *aux;
310 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
311 while (1)
312 {
313 ElfW(Half) ndx = aux->vna_other & 0x7fff;
314 map->l_versions[ndx].hash = aux->vna_hash;
315 map->l_versions[ndx].hidden = aux->vna_other & 0x8000;
316 map->l_versions[ndx].name = &strtab[aux->vna_name];
317 map->l_versions[ndx].filename = &strtab[ent->vn_file];
318
319 if (aux->vna_next == 0)
320 /* No more symbols. */
321 break;
322
323 /* Advance to next symbol. */
324 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
325 }
326
327 if (ent->vn_next == 0)
328 /* No more dependencies. */
329 break;
330
331 /* Advance to next dependency. */
332 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
333 }
334 }
335
336 /* And insert the defined versions. */
337 if (def != NULL)
338 {
339 ElfW(Verdef) *ent;
340 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
341 while (1)
342 {
343 ElfW(Verdaux) *aux;
344 aux = (ElfW(Verdaux) *) ((char *) ent + ent->vd_aux);
345
346 if ((ent->vd_flags & VER_FLG_BASE) == 0)
347 {
348 /* The name of the base version should not be
349 available for matching a versioned symbol. */
350 ElfW(Half) ndx = ent->vd_ndx & 0x7fff;
351 map->l_versions[ndx].hash = ent->vd_hash;
352 map->l_versions[ndx].name = &strtab[aux->vda_name];
353 map->l_versions[ndx].filename = NULL;
354 }
355
356 if (ent->vd_next == 0)
357 /* No more definitions. */
358 break;
359
360 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
361 }
362 }
363 }
364 }
365
366 return result;
367 }
368
369
370 int
371 internal_function
372 _dl_check_all_versions (struct link_map *map, int verbose, int trace_mode)
373 {
374 struct link_map *l;
375 int result = 0;
376
377 for (l = map; l != NULL; l = l->l_next)
378 result |= ! l->l_faked && _dl_check_map_versions (l, verbose, trace_mode);
379
380 return result;
381 }