1 /* Handle symbol and library versioning.
2 Copyright (C) 1997-2002, 2003 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
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 __attribute ((always_inline
))
57 find_needed (const char *name
, struct link_map
*map
)
59 struct link_map
*tmap
;
62 for (tmap
= GL(dl_loaded
); tmap
!= NULL
; tmap
= tmap
->l_next
)
63 if (_dl_name_match_p (name
, tmap
))
66 /* The required object is not in the global scope, look to see if it is
67 a dependency of the current object. */
68 for (n
= 0; n
< map
->l_searchlist
.r_nlist
; n
++)
69 if (_dl_name_match_p (name
, map
->l_searchlist
.r_list
[n
]))
70 return map
->l_searchlist
.r_list
[n
];
72 /* Should never happen. */
79 match_symbol (const char *name
, ElfW(Word
) hash
, const char *string
,
80 struct link_map
*map
, int verbose
, int weak
)
82 const char *strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
83 ElfW(Addr
) def_offset
;
85 /* Initialize to make the compiler happy. */
86 const char *errstring
= NULL
;
89 /* Display information about what we are doing while debugging. */
90 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_VERSIONS
, 0))
91 INTUSE(_dl_debug_printf
) ("\
92 checking for version `%s' in file %s required by file %s\n",
93 string
, map
->l_name
[0]
94 ? map
->l_name
: rtld_progname
, name
);
96 if (__builtin_expect (map
->l_info
[VERSYMIDX (DT_VERDEF
)] == NULL
, 0))
98 /* The file has no symbol versioning. I.e., the dependent
99 object was linked against another version of this file. We
100 only print a message if verbose output is requested. */
103 /* XXX We cannot translate the messages. */
104 errstring
= make_string ("\
105 no version information available (required by ", name
, ")");
111 def_offset
= map
->l_info
[VERSYMIDX (DT_VERDEF
)]->d_un
.d_ptr
;
112 assert (def_offset
!= 0);
114 def
= (ElfW(Verdef
) *) ((char *) map
->l_addr
+ def_offset
);
117 /* Currently the version number of the definition entry is 1.
118 Make sure all we see is this version. */
119 if (__builtin_expect (def
->vd_version
, 1) != 1)
122 buf
[sizeof (buf
) - 1] = '\0';
123 /* XXX We cannot translate the message. */
124 errstring
= make_string ("unsupported version ",
125 _itoa (def
->vd_version
,
126 &buf
[sizeof (buf
) - 1], 10, 0),
127 " of Verdef record");
132 /* Compare the hash values. */
133 if (hash
== def
->vd_hash
)
135 ElfW(Verdaux
) *aux
= (ElfW(Verdaux
) *) ((char *) def
+ def
->vd_aux
);
137 /* To be safe, compare the string as well. */
138 if (__builtin_expect (strcmp (string
, strtab
+ aux
->vda_name
), 0)
144 /* If no more definitions we failed to find what we want. */
145 if (def
->vd_next
== 0)
148 /* Next definition. */
149 def
= (ElfW(Verdef
) *) ((char *) def
+ def
->vd_next
);
152 /* Symbol not found. If it was a weak reference it is not fatal. */
153 if (__builtin_expect (weak
, 1))
157 /* XXX We cannot translate the message. */
158 errstring
= make_string ("weak version `", string
,
159 "' not found (required by ", name
, ")");
165 /* XXX We cannot translate the message. */
166 errstring
= make_string ("version `", string
, "' not found (required by ",
170 _dl_signal_cerror (0, map
->l_name
[0] ? map
->l_name
: rtld_progname
,
178 _dl_check_map_versions (struct link_map
*map
, int verbose
, int trace_mode
)
182 /* Pointer to section with needed versions. */
184 /* Pointer to dynamic section with definitions. */
186 /* We need to find out which is the highest version index used
188 unsigned int ndx_high
= 0;
189 /* Initialize to make the compiler happy. */
190 const char *errstring
= NULL
;
193 /* If we don't have a string table, we must be ok. */
194 if (map
->l_info
[DT_STRTAB
] == NULL
)
196 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
198 dyn
= map
->l_info
[VERSYMIDX (DT_VERNEED
)];
199 def
= map
->l_info
[VERSYMIDX (DT_VERDEF
)];
203 /* This file requires special versions from its dependencies. */
204 ElfW(Verneed
) *ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
206 /* Currently the version number of the needed entry is 1.
207 Make sure all we see is this version. */
208 if (__builtin_expect (ent
->vn_version
, 1) != 1)
211 buf
[sizeof (buf
) - 1] = '\0';
212 /* XXX We cannot translate the message. */
213 errstring
= make_string ("unsupported version ",
214 _itoa (ent
->vn_version
,
215 &buf
[sizeof (buf
) - 1], 10, 0),
216 " of Verneed record\n");
218 INTUSE(_dl_signal_error
) (errval
, (*map
->l_name
219 ? map
->l_name
: rtld_progname
),
226 struct link_map
*needed
= find_needed (strtab
+ ent
->vn_file
, map
);
228 /* If NEEDED is NULL this means a dependency was not found
229 and no stub entry was created. This should never happen. */
230 assert (needed
!= NULL
);
232 /* Make sure this is no stub we created because of a missing
234 if (__builtin_expect (! trace_mode
, 1)
235 || ! __builtin_expect (needed
->l_faked
, 0))
237 /* NEEDED is the map for the file we need. Now look for the
238 dependency symbols. */
239 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
242 /* Match the symbol. */
243 result
|= match_symbol ((*map
->l_name
244 ? map
->l_name
: rtld_progname
),
246 strtab
+ aux
->vna_name
,
248 aux
->vna_flags
& VER_FLG_WEAK
);
250 /* Compare the version index. */
251 if ((unsigned int) (aux
->vna_other
& 0x7fff) > ndx_high
)
252 ndx_high
= aux
->vna_other
& 0x7fff;
254 if (aux
->vna_next
== 0)
255 /* No more symbols. */
259 aux
= (ElfW(Vernaux
) *) ((char *) aux
+ aux
->vna_next
);
263 if (ent
->vn_next
== 0)
264 /* No more dependencies. */
267 /* Next dependency. */
268 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
272 /* We also must store the names of the defined versions. Determine
273 the maximum index here as well.
275 XXX We could avoid the loop by just taking the number of definitions
276 as an upper bound of new indeces. */
280 ent
= (ElfW(Verdef
) *) (map
->l_addr
+ def
->d_un
.d_ptr
);
283 if ((unsigned int) (ent
->vd_ndx
& 0x7fff) > ndx_high
)
284 ndx_high
= ent
->vd_ndx
& 0x7fff;
286 if (ent
->vd_next
== 0)
287 /* No more definitions. */
290 ent
= (ElfW(Verdef
) *) ((char *) ent
+ ent
->vd_next
);
296 /* Now we are ready to build the array with the version names
297 which can be indexed by the version index in the VERSYM
299 map
->l_versions
= (struct r_found_version
*)
300 calloc (ndx_high
+ 1, sizeof (*map
->l_versions
));
301 if (__builtin_expect (map
->l_versions
== NULL
, 0))
303 errstring
= N_("cannot allocate version reference table");
308 /* Store the number of available symbols. */
309 map
->l_nversions
= ndx_high
+ 1;
311 /* Compute the pointer to the version symbols. */
312 map
->l_versyms
= (void *) D_PTR (map
, l_info
[VERSYMIDX (DT_VERSYM
)]);
317 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
321 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
324 ElfW(Half
) ndx
= aux
->vna_other
& 0x7fff;
325 map
->l_versions
[ndx
].hash
= aux
->vna_hash
;
326 map
->l_versions
[ndx
].hidden
= aux
->vna_other
& 0x8000;
327 map
->l_versions
[ndx
].name
= &strtab
[aux
->vna_name
];
328 map
->l_versions
[ndx
].filename
= &strtab
[ent
->vn_file
];
330 if (aux
->vna_next
== 0)
331 /* No more symbols. */
334 /* Advance to next symbol. */
335 aux
= (ElfW(Vernaux
) *) ((char *) aux
+ aux
->vna_next
);
338 if (ent
->vn_next
== 0)
339 /* No more dependencies. */
342 /* Advance to next dependency. */
343 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
347 /* And insert the defined versions. */
351 ent
= (ElfW(Verdef
) *) (map
->l_addr
+ def
->d_un
.d_ptr
);
355 aux
= (ElfW(Verdaux
) *) ((char *) ent
+ ent
->vd_aux
);
357 if ((ent
->vd_flags
& VER_FLG_BASE
) == 0)
359 /* The name of the base version should not be
360 available for matching a versioned symbol. */
361 ElfW(Half
) ndx
= ent
->vd_ndx
& 0x7fff;
362 map
->l_versions
[ndx
].hash
= ent
->vd_hash
;
363 map
->l_versions
[ndx
].name
= &strtab
[aux
->vda_name
];
364 map
->l_versions
[ndx
].filename
= NULL
;
367 if (ent
->vd_next
== 0)
368 /* No more definitions. */
371 ent
= (ElfW(Verdef
) *) ((char *) ent
+ ent
->vd_next
);
378 INTDEF (_dl_check_map_versions
)
383 _dl_check_all_versions (struct link_map
*map
, int verbose
, int trace_mode
)
388 for (l
= map
; l
!= NULL
; l
= l
->l_next
)
389 result
|= (! l
->l_faked
390 && INTUSE(_dl_check_map_versions
) (l
, verbose
, trace_mode
));