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. */
54 match_symbol (const char *name
, Lmid_t ns
, ElfW(Word
) hash
, const char *string
,
55 struct link_map
*map
, int verbose
, int weak
)
57 const char *strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
58 ElfW(Addr
) def_offset
;
60 /* Initialize to make the compiler happy. */
62 struct dl_exception exception
;
64 /* Display information about what we are doing while debugging. */
65 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_VERSIONS
))
67 checking for version `%s' in file %s [%lu] required by file %s [%lu]\n",
68 string
, DSO_FILENAME (map
->l_name
),
71 if (__glibc_unlikely (map
->l_info
[VERSYMIDX (DT_VERDEF
)] == NULL
))
73 /* The file has no symbol versioning. I.e., the dependent
74 object was linked against another version of this file. We
75 only print a message if verbose output is requested. */
78 /* XXX We cannot translate the messages. */
79 _dl_exception_create_format
80 (&exception
, DSO_FILENAME (map
->l_name
),
81 "no version information available (required by %s)", name
);
87 def_offset
= map
->l_info
[VERSYMIDX (DT_VERDEF
)]->d_un
.d_ptr
;
88 assert (def_offset
!= 0);
90 def
= (ElfW(Verdef
) *) ((char *) map
->l_addr
+ def_offset
);
93 /* Currently the version number of the definition entry is 1.
94 Make sure all we see is this version. */
95 if (__builtin_expect (def
->vd_version
, 1) != 1)
98 buf
[sizeof (buf
) - 1] = '\0';
99 /* XXX We cannot translate the message. */
100 _dl_exception_create_format
101 (&exception
, DSO_FILENAME (map
->l_name
),
102 "unsupported version %s of Verdef record",
103 _itoa (def
->vd_version
, &buf
[sizeof (buf
) - 1], 10, 0));
108 /* Compare the hash values. */
109 if (hash
== def
->vd_hash
)
111 ElfW(Verdaux
) *aux
= (ElfW(Verdaux
) *) ((char *) def
+ def
->vd_aux
);
113 /* To be safe, compare the string as well. */
114 if (__builtin_expect (strcmp (string
, strtab
+ aux
->vda_name
), 0)
120 /* If no more definitions we failed to find what we want. */
121 if (def
->vd_next
== 0)
124 /* Next definition. */
125 def
= (ElfW(Verdef
) *) ((char *) def
+ def
->vd_next
);
128 /* Symbol not found. If it was a weak reference it is not fatal. */
129 if (__glibc_likely (weak
))
133 /* XXX We cannot translate the message. */
134 _dl_exception_create_format
135 (&exception
, DSO_FILENAME (map
->l_name
),
136 "weak version `%s' not found (required by %s)", string
, name
);
142 /* XXX We cannot translate the message. */
143 _dl_exception_create_format
144 (&exception
, DSO_FILENAME (map
->l_name
),
145 "version `%s' not found (required by %s)", string
, name
);
148 _dl_signal_cexception (0, &exception
, N_("version lookup error"));
149 _dl_exception_free (&exception
);
155 _dl_check_map_versions (struct link_map
*map
, int verbose
, int trace_mode
)
159 /* Pointer to section with needed versions. */
161 /* Pointer to dynamic section with definitions. */
163 /* We need to find out which is the highest version index used
165 unsigned int ndx_high
= 0;
166 struct dl_exception exception
;
167 /* Initialize to make the compiler happy. */
170 /* If we don't have a string table, we must be ok. */
171 if (map
->l_info
[DT_STRTAB
] == NULL
)
173 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
175 dyn
= map
->l_info
[VERSYMIDX (DT_VERNEED
)];
176 def
= map
->l_info
[VERSYMIDX (DT_VERDEF
)];
180 /* This file requires special versions from its dependencies. */
181 ElfW(Verneed
) *ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
183 /* Currently the version number of the needed entry is 1.
184 Make sure all we see is this version. */
185 if (__builtin_expect (ent
->vn_version
, 1) != 1)
188 buf
[sizeof (buf
) - 1] = '\0';
189 /* XXX We cannot translate the message. */
190 _dl_exception_create_format
191 (&exception
, DSO_FILENAME (map
->l_name
),
192 "unsupported version %s of Verneed record",
193 _itoa (ent
->vn_version
, &buf
[sizeof (buf
) - 1], 10, 0));
195 _dl_signal_exception (errval
, &exception
, NULL
);
201 struct link_map
*needed
= find_needed (strtab
+ ent
->vn_file
, map
);
203 /* If NEEDED is NULL this means a dependency was not found
204 and no stub entry was created. This should never happen. */
205 assert (needed
!= NULL
);
207 /* Make sure this is no stub we created because of a missing
209 if (__builtin_expect (! trace_mode
, 1)
210 || ! __builtin_expect (needed
->l_faked
, 0))
212 /* NEEDED is the map for the file we need. Now look for the
213 dependency symbols. */
214 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
217 /* Match the symbol. */
218 result
|= match_symbol (DSO_FILENAME (map
->l_name
),
219 map
->l_ns
, aux
->vna_hash
,
220 strtab
+ aux
->vna_name
,
221 needed
->l_real
, verbose
,
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
);
237 if (ent
->vn_next
== 0)
238 /* No more dependencies. */
241 /* Next dependency. */
242 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
246 /* We also must store the names of the defined versions. Determine
247 the maximum index here as well.
249 XXX We could avoid the loop by just taking the number of definitions
250 as an upper bound of new indeces. */
254 ent
= (ElfW(Verdef
) *) (map
->l_addr
+ def
->d_un
.d_ptr
);
257 if ((unsigned int) (ent
->vd_ndx
& 0x7fff) > ndx_high
)
258 ndx_high
= ent
->vd_ndx
& 0x7fff;
260 if (ent
->vd_next
== 0)
261 /* No more definitions. */
264 ent
= (ElfW(Verdef
) *) ((char *) ent
+ ent
->vd_next
);
270 /* Now we are ready to build the array with the version names
271 which can be indexed by the version index in the VERSYM
273 map
->l_versions
= (struct r_found_version
*)
274 calloc (ndx_high
+ 1, sizeof (*map
->l_versions
));
275 if (__glibc_unlikely (map
->l_versions
== NULL
))
278 (&exception
, DSO_FILENAME (map
->l_name
),
279 N_("cannot allocate version reference table"));
284 /* Store the number of available symbols. */
285 map
->l_nversions
= ndx_high
+ 1;
287 /* Compute the pointer to the version symbols. */
288 map
->l_versyms
= (void *) D_PTR (map
, l_info
[VERSYMIDX (DT_VERSYM
)]);
293 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
297 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
300 ElfW(Half
) ndx
= aux
->vna_other
& 0x7fff;
301 /* In trace mode, dependencies may be missing. */
302 if (__glibc_likely (ndx
< map
->l_nversions
))
304 map
->l_versions
[ndx
].hash
= aux
->vna_hash
;
305 map
->l_versions
[ndx
].hidden
= aux
->vna_other
& 0x8000;
306 map
->l_versions
[ndx
].name
= &strtab
[aux
->vna_name
];
307 map
->l_versions
[ndx
].filename
= &strtab
[ent
->vn_file
];
310 if (aux
->vna_next
== 0)
311 /* No more symbols. */
314 /* Advance to next symbol. */
315 aux
= (ElfW(Vernaux
) *) ((char *) aux
+ aux
->vna_next
);
318 if (ent
->vn_next
== 0)
319 /* No more dependencies. */
322 /* Advance to next dependency. */
323 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
327 /* And insert the defined versions. */
331 ent
= (ElfW(Verdef
) *) (map
->l_addr
+ def
->d_un
.d_ptr
);
335 aux
= (ElfW(Verdaux
) *) ((char *) ent
+ ent
->vd_aux
);
337 if ((ent
->vd_flags
& VER_FLG_BASE
) == 0)
339 /* The name of the base version should not be
340 available for matching a versioned symbol. */
341 ElfW(Half
) ndx
= ent
->vd_ndx
& 0x7fff;
342 map
->l_versions
[ndx
].hash
= ent
->vd_hash
;
343 map
->l_versions
[ndx
].name
= &strtab
[aux
->vda_name
];
344 map
->l_versions
[ndx
].filename
= NULL
;
347 if (ent
->vd_next
== 0)
348 /* No more definitions. */
351 ent
= (ElfW(Verdef
) *) ((char *) ent
+ ent
->vd_next
);
361 _dl_check_all_versions (struct link_map
*map
, int verbose
, int trace_mode
)
366 for (l
= map
; l
!= NULL
; l
= l
->l_next
)
367 result
|= (! l
->l_faked
368 && _dl_check_map_versions (l
, verbose
, trace_mode
));