]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-version.c
Update copyright notices with scripts/update-copyrights
[thirdparty/glibc.git] / elf / dl-version.c
CommitLineData
c84142e8 1/* Handle symbol and library versioning.
d4697bc9 2 Copyright (C) 1997-2014 Free Software Foundation, Inc.
c84142e8
UD
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
41bdb6e2
AJ
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.
c84142e8
UD
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
41bdb6e2 14 Lesser General Public License for more details.
c84142e8 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
c84142e8
UD
19
20#include <elf.h>
21#include <errno.h>
8e17ea58 22#include <libintl.h>
c84142e8
UD
23#include <stdlib.h>
24#include <string.h>
a42195db 25#include <ldsodefs.h>
eb96ffb0 26#include <_itoa.h>
c84142e8 27
a853022c
UD
28#include <assert.h>
29
c84142e8 30
c84142e8
UD
31#define make_string(string, rest...) \
32 ({ \
33 const char *all[] = { string, ## rest }; \
34 size_t len, cnt; \
35 char *result, *cp; \
36 \
37 len = 1; \
38 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
39 len += strlen (all[cnt]); \
40 \
41 cp = result = alloca (len); \
42 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
26b4d766 43 cp = __stpcpy (cp, all[cnt]); \
c84142e8
UD
44 \
45 result; \
46 })
47
48
49static inline struct link_map *
dd9423a6 50__attribute ((always_inline))
762a2918 51find_needed (const char *name, struct link_map *map)
c84142e8 52{
26b4d766 53 struct link_map *tmap;
c84142e8
UD
54 unsigned int n;
55
c0f62c56
UD
56 for (tmap = GL(dl_ns)[map->l_ns]._ns_loaded; tmap != NULL;
57 tmap = tmap->l_next)
26b4d766
UD
58 if (_dl_name_match_p (name, tmap))
59 return tmap;
c84142e8 60
762a2918
UD
61 /* The required object is not in the global scope, look to see if it is
62 a dependency of the current object. */
be935610
UD
63 for (n = 0; n < map->l_searchlist.r_nlist; n++)
64 if (_dl_name_match_p (name, map->l_searchlist.r_list[n]))
65 return map->l_searchlist.r_list[n];
762a2918 66
c84142e8
UD
67 /* Should never happen. */
68 return NULL;
69}
70
71
72static int
d0fc4041 73internal_function
75a142a2 74match_symbol (const char *name, Lmid_t ns, ElfW(Word) hash, const char *string,
c84142e8
UD
75 struct link_map *map, int verbose, int weak)
76{
a42195db 77 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
714a562f 78 ElfW(Addr) def_offset;
c84142e8 79 ElfW(Verdef) *def;
6ed623f8
UD
80 /* Initialize to make the compiler happy. */
81 const char *errstring = NULL;
82 int result = 0;
c84142e8 83
8193034b 84 /* Display information about what we are doing while debugging. */
afdca0f2 85 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_VERSIONS, 0))
154d10bd 86 _dl_debug_printf ("\
75a142a2 87checking for version `%s' in file %s [%lu] required by file %s [%lu]\n",
b9375348 88 string, DSO_FILENAME (map->l_name),
75a142a2 89 map->l_ns, name, ns);
8193034b 90
7b228b68 91 if (__builtin_expect (map->l_info[VERSYMIDX (DT_VERDEF)] == NULL, 0))
c84142e8
UD
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)
6ed623f8
UD
97 {
98 /* XXX We cannot translate the messages. */
99 errstring = make_string ("\
100no version information available (required by ", name, ")");
101 goto call_cerror;
102 }
c84142e8
UD
103 return 0;
104 }
105
f420344c 106 def_offset = map->l_info[VERSYMIDX (DT_VERDEF)]->d_un.d_ptr;
714a562f
UD
107 assert (def_offset != 0);
108
109 def = (ElfW(Verdef) *) ((char *) map->l_addr + def_offset);
c84142e8
UD
110 while (1)
111 {
112 /* Currently the version number of the definition entry is 1.
113 Make sure all we see is this version. */
7b228b68 114 if (__builtin_expect (def->vd_version, 1) != 1)
c84142e8
UD
115 {
116 char buf[20];
117 buf[sizeof (buf) - 1] = '\0';
8e17ea58 118 /* XXX We cannot translate the message. */
6ed623f8 119 errstring = make_string ("unsupported version ",
9710f75d
UD
120 _itoa (def->vd_version,
121 &buf[sizeof (buf) - 1], 10, 0),
6ed623f8
UD
122 " of Verdef record");
123 result = 1;
124 goto call_cerror;
c84142e8
UD
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. */
7b228b68
UD
133 if (__builtin_expect (strcmp (string, strtab + aux->vda_name), 0)
134 == 0)
c84142e8
UD
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. */
7b228b68 148 if (__builtin_expect (weak, 1))
c84142e8
UD
149 {
150 if (verbose)
6ed623f8
UD
151 {
152 /* XXX We cannot translate the message. */
153 errstring = make_string ("weak version `", string,
154 "' not found (required by ", name, ")");
155 goto call_cerror;
156 }
c84142e8
UD
157 return 0;
158 }
159
8e17ea58 160 /* XXX We cannot translate the message. */
6ed623f8
UD
161 errstring = make_string ("version `", string, "' not found (required by ",
162 name, ")");
163 result = 1;
164 call_cerror:
b9375348 165 _dl_signal_cerror (0, DSO_FILENAME (map->l_name),
5be9d05d 166 N_("version lookup error"), errstring);
6ed623f8 167 return result;
c84142e8
UD
168}
169
170
171int
d0fc4041 172internal_function
145b8413 173_dl_check_map_versions (struct link_map *map, int verbose, int trace_mode)
c84142e8
UD
174{
175 int result = 0;
f41c8091 176 const char *strtab;
c84142e8 177 /* Pointer to section with needed versions. */
f41c8091 178 ElfW(Dyn) *dyn;
c84142e8 179 /* Pointer to dynamic section with definitions. */
f41c8091 180 ElfW(Dyn) *def;
c84142e8
UD
181 /* We need to find out which is the highest version index used
182 in a dependecy. */
183 unsigned int ndx_high = 0;
6ed623f8
UD
184 /* Initialize to make the compiler happy. */
185 const char *errstring = NULL;
186 int errval = 0;
c84142e8 187
f41c8091
UD
188 /* If we don't have a string table, we must be ok. */
189 if (map->l_info[DT_STRTAB] == NULL)
190 return 0;
a42195db 191 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
f41c8091 192
f420344c
UD
193 dyn = map->l_info[VERSYMIDX (DT_VERNEED)];
194 def = map->l_info[VERSYMIDX (DT_VERDEF)];
f41c8091 195
c84142e8
UD
196 if (dyn != NULL)
197 {
198 /* This file requires special versions from its dependencies. */
199 ElfW(Verneed) *ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
200
201 /* Currently the version number of the needed entry is 1.
202 Make sure all we see is this version. */
7b228b68 203 if (__builtin_expect (ent->vn_version, 1) != 1)
c84142e8
UD
204 {
205 char buf[20];
206 buf[sizeof (buf) - 1] = '\0';
8e17ea58 207 /* XXX We cannot translate the message. */
6ed623f8 208 errstring = make_string ("unsupported version ",
9710f75d
UD
209 _itoa (ent->vn_version,
210 &buf[sizeof (buf) - 1], 10, 0),
6ed623f8
UD
211 " of Verneed record\n");
212 call_error:
b9375348 213 _dl_signal_error (errval, DSO_FILENAME (map->l_name),
154d10bd 214 NULL, errstring);
c84142e8
UD
215 }
216
217 while (1)
218 {
219 ElfW(Vernaux) *aux;
762a2918 220 struct link_map *needed = find_needed (strtab + ent->vn_file, map);
c84142e8
UD
221
222 /* If NEEDED is NULL this means a dependency was not found
223 and no stub entry was created. This should never happen. */
224 assert (needed != NULL);
225
145b8413
UD
226 /* Make sure this is no stub we created because of a missing
227 dependency. */
7b228b68 228 if (__builtin_expect (! trace_mode, 1)
a881e0a0 229 || ! __builtin_expect (needed->l_faked, 0))
c84142e8 230 {
145b8413
UD
231 /* NEEDED is the map for the file we need. Now look for the
232 dependency symbols. */
233 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
234 while (1)
235 {
236 /* Match the symbol. */
b9375348 237 result |= match_symbol (DSO_FILENAME (map->l_name),
75a142a2 238 map->l_ns, aux->vna_hash,
145b8413 239 strtab + aux->vna_name,
c0f62c56 240 needed->l_real, verbose,
145b8413
UD
241 aux->vna_flags & VER_FLG_WEAK);
242
243 /* Compare the version index. */
244 if ((unsigned int) (aux->vna_other & 0x7fff) > ndx_high)
245 ndx_high = aux->vna_other & 0x7fff;
246
247 if (aux->vna_next == 0)
248 /* No more symbols. */
249 break;
250
251 /* Next symbol. */
252 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
253 }
c84142e8
UD
254 }
255
256 if (ent->vn_next == 0)
257 /* No more dependencies. */
258 break;
259
260 /* Next dependency. */
261 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
262 }
263 }
264
265 /* We also must store the names of the defined versions. Determine
266 the maximum index here as well.
267
268 XXX We could avoid the loop by just taking the number of definitions
269 as an upper bound of new indeces. */
270 if (def != NULL)
271 {
272 ElfW(Verdef) *ent;
714a562f 273 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
c84142e8
UD
274 while (1)
275 {
c131718c 276 if ((unsigned int) (ent->vd_ndx & 0x7fff) > ndx_high)
c84142e8
UD
277 ndx_high = ent->vd_ndx & 0x7fff;
278
279 if (ent->vd_next == 0)
280 /* No more definitions. */
281 break;
282
283 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
284 }
285 }
286
287 if (ndx_high > 0)
288 {
289 /* Now we are ready to build the array with the version names
290 which can be indexed by the version index in the VERSYM
291 section. */
1fb05e3d 292 map->l_versions = (struct r_found_version *)
c131718c 293 calloc (ndx_high + 1, sizeof (*map->l_versions));
7b228b68 294 if (__builtin_expect (map->l_versions == NULL, 0))
c84142e8 295 {
6ed623f8
UD
296 errstring = N_("cannot allocate version reference table");
297 errval = ENOMEM;
298 goto call_error;
c84142e8 299 }
c84142e8 300
6ed623f8
UD
301 /* Store the number of available symbols. */
302 map->l_nversions = ndx_high + 1;
303
304 /* Compute the pointer to the version symbols. */
305 map->l_versyms = (void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
0c367d92 306
6ed623f8
UD
307 if (dyn != NULL)
308 {
309 ElfW(Verneed) *ent;
310 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
311 while (1)
c84142e8 312 {
6ed623f8
UD
313 ElfW(Vernaux) *aux;
314 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
c84142e8
UD
315 while (1)
316 {
6ed623f8 317 ElfW(Half) ndx = aux->vna_other & 0x7fff;
18a26b30
AS
318 /* In trace mode, dependencies may be missing. */
319 if (__builtin_expect (ndx < map->l_nversions, 1))
320 {
321 map->l_versions[ndx].hash = aux->vna_hash;
322 map->l_versions[ndx].hidden = aux->vna_other & 0x8000;
323 map->l_versions[ndx].name = &strtab[aux->vna_name];
324 map->l_versions[ndx].filename = &strtab[ent->vn_file];
325 }
6ed623f8
UD
326
327 if (aux->vna_next == 0)
328 /* No more symbols. */
c84142e8
UD
329 break;
330
6ed623f8
UD
331 /* Advance to next symbol. */
332 aux = (ElfW(Vernaux) *) ((char *) aux + aux->vna_next);
c84142e8 333 }
6ed623f8
UD
334
335 if (ent->vn_next == 0)
336 /* No more dependencies. */
337 break;
338
339 /* Advance to next dependency. */
340 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
c84142e8 341 }
6ed623f8 342 }
c84142e8 343
6ed623f8
UD
344 /* And insert the defined versions. */
345 if (def != NULL)
346 {
347 ElfW(Verdef) *ent;
348 ent = (ElfW(Verdef) *) (map->l_addr + def->d_un.d_ptr);
349 while (1)
c84142e8 350 {
6ed623f8
UD
351 ElfW(Verdaux) *aux;
352 aux = (ElfW(Verdaux) *) ((char *) ent + ent->vd_aux);
c84142e8 353
6ed623f8
UD
354 if ((ent->vd_flags & VER_FLG_BASE) == 0)
355 {
356 /* The name of the base version should not be
357 available for matching a versioned symbol. */
358 ElfW(Half) ndx = ent->vd_ndx & 0x7fff;
359 map->l_versions[ndx].hash = ent->vd_hash;
360 map->l_versions[ndx].name = &strtab[aux->vda_name];
361 map->l_versions[ndx].filename = NULL;
c84142e8 362 }
6ed623f8
UD
363
364 if (ent->vd_next == 0)
365 /* No more definitions. */
366 break;
367
368 ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next);
c84142e8
UD
369 }
370 }
371 }
372
373 return result;
374}
375
376
377int
d0fc4041 378internal_function
145b8413 379_dl_check_all_versions (struct link_map *map, int verbose, int trace_mode)
c84142e8
UD
380{
381 struct link_map *l;
382 int result = 0;
383
384 for (l = map; l != NULL; l = l->l_next)
7969407a 385 result |= (! l->l_faked
154d10bd 386 && _dl_check_map_versions (l, verbose, trace_mode));
c84142e8
UD
387
388 return result;
389}