1 /* Handle symbol and library versioning.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001 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. */
27 #include <stdio-common/_itoa.h>
33 # define VERSYMIDX(tag) (DT_NUM + DT_THISPROCNUM + 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
, struct link_map
*map
)
58 struct link_map
*tmap
;
61 for (tmap
= _dl_loaded
; tmap
!= NULL
; tmap
= tmap
->l_next
)
62 if (_dl_name_match_p (name
, tmap
))
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
];
71 /* Should never happen. */
78 match_symbol (const char *name
, ElfW(Word
) hash
, const char *string
,
79 struct link_map
*map
, int verbose
, int weak
)
81 const char *strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
82 ElfW(Addr
) def_offset
;
85 /* Display information about what we are doing while debugging. */
86 if (__builtin_expect (_dl_debug_mask
& DL_DEBUG_VERSIONS
, 0))
88 checking for version `%s' in file %s required by file %s\n",
89 string
, map
->l_name
[0] ? map
->l_name
: _dl_argv
[0],
92 if (__builtin_expect (map
->l_info
[VERSYMIDX (DT_VERDEF
)] == NULL
, 0))
94 /* The file has no symbol versioning. I.e., the dependent
95 object was linked against another version of this file. We
96 only print a message if verbose output is requested. */
98 /* XXX We cannot translate the messages. */
99 _dl_signal_cerror (0, map
->l_name
[0] ? map
->l_name
: _dl_argv
[0],
101 no version information available (required by ",
106 def_offset
= map
->l_info
[VERSYMIDX (DT_VERDEF
)]->d_un
.d_ptr
;
107 assert (def_offset
!= 0);
109 def
= (ElfW(Verdef
) *) ((char *) map
->l_addr
+ def_offset
);
112 /* Currently the version number of the definition entry is 1.
113 Make sure all we see is this version. */
114 if (__builtin_expect (def
->vd_version
, 1) != 1)
117 buf
[sizeof (buf
) - 1] = '\0';
118 /* XXX We cannot translate the message. */
119 _dl_signal_error (0, map
->l_name
[0] ? map
->l_name
: _dl_argv
[0],
120 make_string ("unsupported version ",
121 _itoa_word (def
->vd_version
,
122 &buf
[sizeof (buf
) - 1],
124 " of Verdef record"));
128 /* Compare the hash values. */
129 if (hash
== def
->vd_hash
)
131 ElfW(Verdaux
) *aux
= (ElfW(Verdaux
) *) ((char *) def
+ def
->vd_aux
);
133 /* To be safe, compare the string as well. */
134 if (__builtin_expect (strcmp (string
, strtab
+ aux
->vda_name
), 0)
140 /* If no more definitions we failed to find what we want. */
141 if (def
->vd_next
== 0)
144 /* Next definition. */
145 def
= (ElfW(Verdef
) *) ((char *) def
+ def
->vd_next
);
148 /* Symbol not found. If it was a weak reference it is not fatal. */
149 if (__builtin_expect (weak
, 1))
152 /* XXX We cannot translate the message. */
153 _dl_signal_cerror (0, map
->l_name
[0] ? map
->l_name
: _dl_argv
[0],
154 make_string ("weak version `", string
,
155 "' not found (required by ", name
,
160 /* XXX We cannot translate the message. */
161 _dl_signal_cerror (0, map
->l_name
[0] ? map
->l_name
: _dl_argv
[0],
162 make_string ("version `", string
,
163 "' not found (required by ", name
, ")"));
170 _dl_check_map_versions (struct link_map
*map
, int verbose
, int trace_mode
)
174 /* Pointer to section with needed versions. */
176 /* Pointer to dynamic section with definitions. */
178 /* We need to find out which is the highest version index used
180 unsigned int ndx_high
= 0;
182 /* If we don't have a string table, we must be ok. */
183 if (map
->l_info
[DT_STRTAB
] == NULL
)
185 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
187 dyn
= map
->l_info
[VERSYMIDX (DT_VERNEED
)];
188 def
= map
->l_info
[VERSYMIDX (DT_VERDEF
)];
192 /* This file requires special versions from its dependencies. */
193 ElfW(Verneed
) *ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
195 /* Currently the version number of the needed entry is 1.
196 Make sure all we see is this version. */
197 if (__builtin_expect (ent
->vn_version
, 1) != 1)
200 buf
[sizeof (buf
) - 1] = '\0';
201 /* XXX We cannot translate the message. */
202 _dl_signal_error (0, (*map
->l_name
? map
->l_name
: _dl_argv
[0]),
203 make_string ("unsupported version ",
204 _itoa_word (ent
->vn_version
,
205 &buf
[sizeof (buf
) - 1],
207 " of Verneed record\n"));
214 struct link_map
*needed
= find_needed (strtab
+ ent
->vn_file
, map
);
216 /* If NEEDED is NULL this means a dependency was not found
217 and no stub entry was created. This should never happen. */
218 assert (needed
!= NULL
);
220 /* Make sure this is no stub we created because of a missing
222 if (__builtin_expect (! trace_mode
, 1)
223 || ! __builtin_expect (needed
->l_faked
, 0))
225 /* NEEDED is the map for the file we need. Now look for the
226 dependency symbols. */
227 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
230 /* Match the symbol. */
231 result
|= match_symbol ((*map
->l_name
232 ? map
->l_name
: _dl_argv
[0]),
234 strtab
+ aux
->vna_name
,
236 aux
->vna_flags
& VER_FLG_WEAK
);
238 /* Compare the version index. */
239 if ((unsigned int) (aux
->vna_other
& 0x7fff) > ndx_high
)
240 ndx_high
= aux
->vna_other
& 0x7fff;
242 if (aux
->vna_next
== 0)
243 /* No more symbols. */
247 aux
= (ElfW(Vernaux
) *) ((char *) aux
+ aux
->vna_next
);
251 if (ent
->vn_next
== 0)
252 /* No more dependencies. */
255 /* Next dependency. */
256 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
260 /* We also must store the names of the defined versions. Determine
261 the maximum index here as well.
263 XXX We could avoid the loop by just taking the number of definitions
264 as an upper bound of new indeces. */
268 ent
= (ElfW(Verdef
) *) (map
->l_addr
+ def
->d_un
.d_ptr
);
271 if ((unsigned int) (ent
->vd_ndx
& 0x7fff) > ndx_high
)
272 ndx_high
= ent
->vd_ndx
& 0x7fff;
274 if (ent
->vd_next
== 0)
275 /* No more definitions. */
278 ent
= (ElfW(Verdef
) *) ((char *) ent
+ ent
->vd_next
);
284 /* Now we are ready to build the array with the version names
285 which can be indexed by the version index in the VERSYM
287 map
->l_versions
= (struct r_found_version
*)
288 calloc (ndx_high
+ 1, sizeof (*map
->l_versions
));
289 if (__builtin_expect (map
->l_versions
== NULL
, 0))
291 _dl_signal_error (ENOMEM
, (*map
->l_name
? map
->l_name
: _dl_argv
[0]),
292 N_("cannot allocate version reference table"));
297 /* Store the number of available symbols. */
298 map
->l_nversions
= ndx_high
+ 1;
300 /* Compute the pointer to the version symbols. */
302 (void *) D_PTR (map
, l_info
[VERSYMIDX (DT_VERSYM
)]);
307 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
311 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
314 ElfW(Half
) ndx
= aux
->vna_other
& 0x7fff;
315 map
->l_versions
[ndx
].hash
= aux
->vna_hash
;
316 map
->l_versions
[ndx
].hidden
= aux
->vna_other
& 0x8000;
317 map
->l_versions
[ndx
].name
= &strtab
[aux
->vna_name
];
318 map
->l_versions
[ndx
].filename
= &strtab
[ent
->vn_file
];
320 if (aux
->vna_next
== 0)
321 /* No more symbols. */
324 /* Advance to next symbol. */
325 aux
= (ElfW(Vernaux
) *) ((char *) aux
+ aux
->vna_next
);
328 if (ent
->vn_next
== 0)
329 /* No more dependencies. */
332 /* Advance to next dependency. */
333 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
337 /* And insert the defined versions. */
341 ent
= (ElfW(Verdef
) *) (map
->l_addr
+ def
->d_un
.d_ptr
);
345 aux
= (ElfW(Verdaux
) *) ((char *) ent
+ ent
->vd_aux
);
347 if ((ent
->vd_flags
& VER_FLG_BASE
) == 0)
349 /* The name of the base version should not be
350 available for matching a versioned symbol. */
351 ElfW(Half
) ndx
= ent
->vd_ndx
& 0x7fff;
352 map
->l_versions
[ndx
].hash
= ent
->vd_hash
;
353 map
->l_versions
[ndx
].name
= &strtab
[aux
->vda_name
];
354 map
->l_versions
[ndx
].filename
= NULL
;
357 if (ent
->vd_next
== 0)
358 /* No more definitions. */
361 ent
= (ElfW(Verdef
) *) ((char *) ent
+ ent
->vd_next
);
373 _dl_check_all_versions (struct link_map
*map
, int verbose
, int trace_mode
)
378 for (l
= map
; l
!= NULL
; l
= l
->l_next
)
379 result
|= ! l
->l_faked
&& _dl_check_map_versions (l
, verbose
, trace_mode
);