1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
27 #include <dl-machine.h>
29 #define VERSTAG(tag) (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (tag))
31 /* We need this string more than once. */
32 static const char undefined_msg[] = "undefined symbol: ";
42 #define make_string(string, rest...) \
44 const char *all[] = { string, ## rest }; \
49 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
50 len += strlen (all[cnt]); \
52 cp = result = alloca (len); \
53 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
54 cp = stpcpy (cp, all[cnt]); \
60 /* Inner part of the lookup functions. We return a value > 0 if we
61 found the symbol, the value 0 if nothing is found and < 0 if
62 something bad happened. */
64 do_lookup (const char *undef_name, unsigned long int hash,
65 const ElfW(Sym) *ref, struct sym_val *result,
66 struct link_map *list[], size_t i, size_t n,
67 const char *reference_name, const struct r_found_version *version,
68 struct link_map *skip, int reloc_type)
74 const ElfW(Sym) *symtab;
76 const ElfW(Half) *verstab;
81 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
82 if (skip != NULL && map == skip)
85 /* Skip objects that could not be opened, which can occur in trace
87 if (map->l_opencount == 0)
90 /* Don't search the executable when resolving a copy reloc. */
91 if (elf_machine_lookup_noexec_p (reloc_type) &&
92 map->l_type == lt_executable)
95 /* Skip objects without symbol tables. */
96 if (map->l_info[DT_SYMTAB] == NULL)
99 symtab = ((void *) map->l_addr + map->l_info[DT_SYMTAB]->d_un.d_ptr);
100 strtab = ((void *) map->l_addr + map->l_info[DT_STRTAB]->d_un.d_ptr);
101 if (map->l_nversions > 0 && map->l_info[VERSTAG (DT_VERSYM)] != NULL)
102 verstab = ((void *) map->l_addr
103 + map->l_info[VERSTAG (DT_VERSYM)]->d_un.d_ptr);
107 /* Search the appropriate hash bucket in this object's symbol table
108 for a definition for the same symbol name. */
109 for (symidx = map->l_buckets[hash % map->l_nbuckets];
111 symidx = map->l_chain[symidx])
113 const ElfW(Sym) *sym = &symtab[symidx];
115 if (sym->st_value == 0 || /* No value. */
116 (elf_machine_lookup_noplt_p (reloc_type) /* Reject PLT entry. */
117 && sym->st_shndx == SHN_UNDEF))
120 if (ELFW(ST_TYPE) (sym->st_info) > STT_FUNC)
121 /* Ignore all but STT_NOTYPE, STT_OBJECT and STT_FUNC entries
122 since these are no code/data definitions. */
125 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
126 /* Not the symbol we are looking for. */
131 /* No specific version is selected. When the object
132 file also does not define a version we have a match.
133 Otherwise we only accept the default version, i.e.,
134 the version which name is "". */
137 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
138 if (ndx > 2) /* map->l_versions[ndx].hash != 0) */
146 /* We need a versioned system but haven't found any.
147 If this is the object which is referenced in the
148 verneed entry it is a bug in the library since a
149 symbol must not simply disappear. */
150 if (version->filename != NULL
151 && _dl_name_match_p (version->filename, map))
153 /* Otherwise we accept the symbol. */
157 /* We can match the version information or use the
159 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
160 if ((map->l_versions[ndx].hash != version->hash
161 || strcmp (map->l_versions[ndx].name, version->name))
162 && (version->hidden || map->l_versions[ndx].hash))
163 /* It's not the version we want. */
168 switch (ELFW(ST_BIND) (sym->st_info))
171 /* Global definition. Just what we need. */
173 result->a = map->l_addr;
176 /* Weak definition. Use this value if we don't find
181 result->a = map->l_addr;
185 /* Local symbols are ignored. */
189 /* There cannot be another entry for this symbol so stop here. */
193 /* If this current map is the one mentioned in the verneed entry
194 and we have not found a weak entry, it is a bug. */
195 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
196 && _dl_name_match_p (version->filename, map))
200 /* We have not found anything until now. */
204 /* Search loaded objects' symbol tables for a definition of the symbol
208 _dl_lookup_symbol (const char *undef_name, const ElfW(Sym) **ref,
209 struct link_map *symbol_scope[],
210 const char *reference_name,
213 const unsigned long int hash = _dl_elf_hash (undef_name);
214 struct sym_val current_value = { 0, NULL };
215 struct link_map **scope;
217 /* Search the relevant loaded objects for a definition. */
218 for (scope = symbol_scope; *scope; ++scope)
219 if (do_lookup (undef_name, hash, *ref, ¤t_value,
220 (*scope)->l_searchlist, 0, (*scope)->l_nsearchlist,
221 reference_name, NULL, NULL, reloc_type))
224 if (current_value.s == NULL &&
225 (*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK))
226 /* We could find no value for a strong reference. */
227 _dl_signal_error (0, reference_name,
228 make_string (undefined_msg, undef_name));
230 *ref = current_value.s;
231 return current_value.a;
235 /* This function is nearly the same as `_dl_lookup_symbol' but it
236 skips in the first list all objects until SKIP_MAP is found. I.e.,
237 it only considers objects which were loaded after the described
238 object. If there are more search lists the object described by
239 SKIP_MAP is only skipped. */
241 _dl_lookup_symbol_skip (const char *undef_name, const ElfW(Sym) **ref,
242 struct link_map *symbol_scope[],
243 const char *reference_name,
244 struct link_map *skip_map)
246 const unsigned long int hash = _dl_elf_hash (undef_name);
247 struct sym_val current_value = { 0, NULL };
248 struct link_map **scope;
251 /* Search the relevant loaded objects for a definition. */
252 scope = symbol_scope;
253 for (i = 0; (*scope)->l_dupsearchlist[i] != skip_map; ++i)
254 assert (i < (*scope)->l_ndupsearchlist);
256 if (! do_lookup (undef_name, hash, *ref, ¤t_value,
257 (*scope)->l_dupsearchlist, i, (*scope)->l_ndupsearchlist,
258 reference_name, NULL, skip_map, 0))
260 if (do_lookup (undef_name, hash, *ref, ¤t_value,
261 (*scope)->l_dupsearchlist, 0, (*scope)->l_ndupsearchlist,
262 reference_name, NULL, skip_map, 0))
265 *ref = current_value.s;
266 return current_value.a;
270 /* This function works like _dl_lookup_symbol but it takes an
271 additional arguement with the version number of the requested
274 XXX We'll see whether we need this separate function. */
276 _dl_lookup_versioned_symbol (const char *undef_name, const ElfW(Sym) **ref,
277 struct link_map *symbol_scope[],
278 const char *reference_name,
279 const struct r_found_version *version,
282 extern char **_dl_argv;
283 const unsigned long int hash = _dl_elf_hash (undef_name);
284 struct sym_val current_value = { 0, NULL };
285 struct link_map **scope;
287 /* Search the relevant loaded objects for a definition. */
288 for (scope = symbol_scope; *scope; ++scope)
290 int res = do_lookup (undef_name, hash, *ref, ¤t_value,
291 (*scope)->l_searchlist, 0, (*scope)->l_nsearchlist,
292 reference_name, version, NULL, reloc_type);
297 /* Oh, oh. The file named in the relocation entry does not
298 contain the needed symbol. */
299 _dl_signal_error (0, (*reference_name
301 : (_dl_argv[0] ?: "<main program>")),
302 make_string ("symbol ", undef_name, ", version ",
304 " not defined in file ",
306 " with link time reference",
308 ? " (no version symbols)" : ""));
311 if (current_value.s == NULL &&
312 (*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK))
313 /* We could find no value for a strong reference. */
314 _dl_signal_error (0, reference_name,
315 make_string (undefined_msg, undef_name,
316 ", version ", version->name ?: NULL));
318 *ref = current_value.s;
319 return current_value.a;
323 /* Similar to _dl_lookup_symbol_skip but takes an additional argument
324 with the version we are looking for. */
326 _dl_lookup_versioned_symbol_skip (const char *undef_name,
327 const ElfW(Sym) **ref,
328 struct link_map *symbol_scope[],
329 const char *reference_name,
330 const struct r_found_version *version,
331 struct link_map *skip_map)
333 const unsigned long int hash = _dl_elf_hash (undef_name);
334 struct sym_val current_value = { 0, NULL };
335 struct link_map **scope;
338 /* Search the relevant loaded objects for a definition. */
339 scope = symbol_scope;
340 for (i = 0; (*scope)->l_dupsearchlist[i] != skip_map; ++i)
341 assert (i < (*scope)->l_ndupsearchlist);
343 if (! do_lookup (undef_name, hash, *ref, ¤t_value,
344 (*scope)->l_dupsearchlist, i, (*scope)->l_ndupsearchlist,
345 reference_name, version, skip_map, 0))
347 if (do_lookup (undef_name, hash, *ref, ¤t_value,
348 (*scope)->l_dupsearchlist, 0, (*scope)->l_ndupsearchlist,
349 reference_name, version, skip_map, 0))
352 if (current_value.s == NULL &&
353 (*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK))
355 /* We could find no value for a strong reference. */
356 const size_t len = strlen (undef_name);
357 char buf[sizeof undefined_msg + len];
358 __mempcpy (__mempcpy (buf, undefined_msg, sizeof undefined_msg - 1),
359 undef_name, len + 1);
360 _dl_signal_error (0, reference_name, buf);
363 *ref = current_value.s;
364 return current_value.a;
368 /* Cache the location of MAP's hash table. */
371 _dl_setup_hash (struct link_map *map)
376 if (!map->l_info[DT_HASH])
378 hash = (void *)(map->l_addr + map->l_info[DT_HASH]->d_un.d_ptr);
380 map->l_nbuckets = *hash++;
382 map->l_buckets = hash;
383 hash += map->l_nbuckets;