1 /* Handle symbol and library versioning.
2 Copyright (C) 1997-2017 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, see
18 <http://www.gnu.org/licenses/>. */
30 static inline struct link_map
*
31 __attribute ((always_inline
))
32 find_needed (const char *name
, struct link_map
*map
)
34 struct link_map
*tmap
;
37 for (tmap
= GL(dl_ns
)[map
->l_ns
]._ns_loaded
; tmap
!= NULL
;
39 if (_dl_name_match_p (name
, tmap
))
42 /* The required object is not in the global scope, look to see if it is
43 a dependency of the current object. */
44 for (n
= 0; n
< map
->l_searchlist
.r_nlist
; n
++)
45 if (_dl_name_match_p (name
, map
->l_searchlist
.r_list
[n
]))
46 return map
->l_searchlist
.r_list
[n
];
48 /* Should never happen. */
55 match_symbol (const char *name
, Lmid_t ns
, ElfW(Word
) hash
, const char *string
,
56 struct link_map
*map
, int verbose
, int weak
)
58 const char *strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
59 ElfW(Addr
) def_offset
;
61 /* Initialize to make the compiler happy. */
63 struct dl_exception exception
;
65 /* Display information about what we are doing while debugging. */
66 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_VERSIONS
))
68 checking for version `%s' in file %s [%lu] required by file %s [%lu]\n",
69 string
, DSO_FILENAME (map
->l_name
),
72 if (__glibc_unlikely (map
->l_info
[VERSYMIDX (DT_VERDEF
)] == NULL
))
74 /* The file has no symbol versioning. I.e., the dependent
75 object was linked against another version of this file. We
76 only print a message if verbose output is requested. */
79 /* XXX We cannot translate the messages. */
80 _dl_exception_create_format
81 (&exception
, DSO_FILENAME (map
->l_name
),
82 "no version information available (required by %s)", name
);
88 def_offset
= map
->l_info
[VERSYMIDX (DT_VERDEF
)]->d_un
.d_ptr
;
89 assert (def_offset
!= 0);
91 def
= (ElfW(Verdef
) *) ((char *) map
->l_addr
+ def_offset
);
94 /* Currently the version number of the definition entry is 1.
95 Make sure all we see is this version. */
96 if (__builtin_expect (def
->vd_version
, 1) != 1)
99 buf
[sizeof (buf
) - 1] = '\0';
100 /* XXX We cannot translate the message. */
101 _dl_exception_create_format
102 (&exception
, DSO_FILENAME (map
->l_name
),
103 "unsupported version %s of Verdef record",
104 _itoa (def
->vd_version
, &buf
[sizeof (buf
) - 1], 10, 0));
109 /* Compare the hash values. */
110 if (hash
== def
->vd_hash
)
112 ElfW(Verdaux
) *aux
= (ElfW(Verdaux
) *) ((char *) def
+ def
->vd_aux
);
114 /* To be safe, compare the string as well. */
115 if (__builtin_expect (strcmp (string
, strtab
+ aux
->vda_name
), 0)
121 /* If no more definitions we failed to find what we want. */
122 if (def
->vd_next
== 0)
125 /* Next definition. */
126 def
= (ElfW(Verdef
) *) ((char *) def
+ def
->vd_next
);
129 /* Symbol not found. If it was a weak reference it is not fatal. */
130 if (__glibc_likely (weak
))
134 /* XXX We cannot translate the message. */
135 _dl_exception_create_format
136 (&exception
, DSO_FILENAME (map
->l_name
),
137 "weak version `%s' not found (required by %s)", string
, name
);
143 /* XXX We cannot translate the message. */
144 _dl_exception_create_format
145 (&exception
, DSO_FILENAME (map
->l_name
),
146 "version `%s' not found (required by %s)", string
, name
);
149 _dl_signal_cexception (0, &exception
, N_("version lookup error"));
150 _dl_exception_free (&exception
);
157 _dl_check_map_versions (struct link_map
*map
, int verbose
, int trace_mode
)
161 /* Pointer to section with needed versions. */
163 /* Pointer to dynamic section with definitions. */
165 /* We need to find out which is the highest version index used
167 unsigned int ndx_high
= 0;
168 struct dl_exception exception
;
169 /* Initialize to make the compiler happy. */
172 /* If we don't have a string table, we must be ok. */
173 if (map
->l_info
[DT_STRTAB
] == NULL
)
175 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
177 dyn
= map
->l_info
[VERSYMIDX (DT_VERNEED
)];
178 def
= map
->l_info
[VERSYMIDX (DT_VERDEF
)];
182 /* This file requires special versions from its dependencies. */
183 ElfW(Verneed
) *ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
185 /* Currently the version number of the needed entry is 1.
186 Make sure all we see is this version. */
187 if (__builtin_expect (ent
->vn_version
, 1) != 1)
190 buf
[sizeof (buf
) - 1] = '\0';
191 /* XXX We cannot translate the message. */
192 _dl_exception_create_format
193 (&exception
, DSO_FILENAME (map
->l_name
),
194 "unsupported version %s of Verneed record",
195 _itoa (ent
->vn_version
, &buf
[sizeof (buf
) - 1], 10, 0));
197 _dl_signal_exception (errval
, &exception
, NULL
);
203 struct link_map
*needed
= find_needed (strtab
+ ent
->vn_file
, map
);
205 /* If NEEDED is NULL this means a dependency was not found
206 and no stub entry was created. This should never happen. */
207 assert (needed
!= NULL
);
209 /* Make sure this is no stub we created because of a missing
211 if (__builtin_expect (! trace_mode
, 1)
212 || ! __builtin_expect (needed
->l_faked
, 0))
214 /* NEEDED is the map for the file we need. Now look for the
215 dependency symbols. */
216 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
219 /* Match the symbol. */
220 result
|= match_symbol (DSO_FILENAME (map
->l_name
),
221 map
->l_ns
, aux
->vna_hash
,
222 strtab
+ aux
->vna_name
,
223 needed
->l_real
, verbose
,
224 aux
->vna_flags
& VER_FLG_WEAK
);
226 /* Compare the version index. */
227 if ((unsigned int) (aux
->vna_other
& 0x7fff) > ndx_high
)
228 ndx_high
= aux
->vna_other
& 0x7fff;
230 if (aux
->vna_next
== 0)
231 /* No more symbols. */
235 aux
= (ElfW(Vernaux
) *) ((char *) aux
+ aux
->vna_next
);
239 if (ent
->vn_next
== 0)
240 /* No more dependencies. */
243 /* Next dependency. */
244 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
248 /* We also must store the names of the defined versions. Determine
249 the maximum index here as well.
251 XXX We could avoid the loop by just taking the number of definitions
252 as an upper bound of new indeces. */
256 ent
= (ElfW(Verdef
) *) (map
->l_addr
+ def
->d_un
.d_ptr
);
259 if ((unsigned int) (ent
->vd_ndx
& 0x7fff) > ndx_high
)
260 ndx_high
= ent
->vd_ndx
& 0x7fff;
262 if (ent
->vd_next
== 0)
263 /* No more definitions. */
266 ent
= (ElfW(Verdef
) *) ((char *) ent
+ ent
->vd_next
);
272 /* Now we are ready to build the array with the version names
273 which can be indexed by the version index in the VERSYM
275 map
->l_versions
= (struct r_found_version
*)
276 calloc (ndx_high
+ 1, sizeof (*map
->l_versions
));
277 if (__glibc_unlikely (map
->l_versions
== NULL
))
280 (&exception
, DSO_FILENAME (map
->l_name
),
281 N_("cannot allocate version reference table"));
286 /* Store the number of available symbols. */
287 map
->l_nversions
= ndx_high
+ 1;
289 /* Compute the pointer to the version symbols. */
290 map
->l_versyms
= (void *) D_PTR (map
, l_info
[VERSYMIDX (DT_VERSYM
)]);
295 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
299 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
302 ElfW(Half
) ndx
= aux
->vna_other
& 0x7fff;
303 /* In trace mode, dependencies may be missing. */
304 if (__glibc_likely (ndx
< map
->l_nversions
))
306 map
->l_versions
[ndx
].hash
= aux
->vna_hash
;
307 map
->l_versions
[ndx
].hidden
= aux
->vna_other
& 0x8000;
308 map
->l_versions
[ndx
].name
= &strtab
[aux
->vna_name
];
309 map
->l_versions
[ndx
].filename
= &strtab
[ent
->vn_file
];
312 if (aux
->vna_next
== 0)
313 /* No more symbols. */
316 /* Advance to next symbol. */
317 aux
= (ElfW(Vernaux
) *) ((char *) aux
+ aux
->vna_next
);
320 if (ent
->vn_next
== 0)
321 /* No more dependencies. */
324 /* Advance to next dependency. */
325 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
329 /* And insert the defined versions. */
333 ent
= (ElfW(Verdef
) *) (map
->l_addr
+ def
->d_un
.d_ptr
);
337 aux
= (ElfW(Verdaux
) *) ((char *) ent
+ ent
->vd_aux
);
339 if ((ent
->vd_flags
& VER_FLG_BASE
) == 0)
341 /* The name of the base version should not be
342 available for matching a versioned symbol. */
343 ElfW(Half
) ndx
= ent
->vd_ndx
& 0x7fff;
344 map
->l_versions
[ndx
].hash
= ent
->vd_hash
;
345 map
->l_versions
[ndx
].name
= &strtab
[aux
->vda_name
];
346 map
->l_versions
[ndx
].filename
= NULL
;
349 if (ent
->vd_next
== 0)
350 /* No more definitions. */
353 ent
= (ElfW(Verdef
) *) ((char *) ent
+ ent
->vd_next
);
364 _dl_check_all_versions (struct link_map
*map
, int verbose
, int trace_mode
)
369 for (l
= map
; l
!= NULL
; l
= l
->l_next
)
370 result
|= (! l
->l_faked
371 && _dl_check_map_versions (l
, verbose
, trace_mode
));