Fix comment in Linux's <sys/epoll.h>.
[glibc.git] / elf / do-lookup.h
blob782f4909645f2f0d9fbc71aed4d1b5ba6b10a809
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995-2007, 2008, 2009 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
21 /* Inner part of the lookup functions. We return a value > 0 if we
22 found the symbol, the value 0 if nothing is found and < 0 if
23 something bad happened. */
24 static int
25 __attribute_noinline__
26 do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
27 unsigned long int *old_hash, const ElfW(Sym) *ref,
28 struct sym_val *result, struct r_scope_elem *scope, size_t i,
29 const struct r_found_version *const version, int flags,
30 struct link_map *skip, int type_class, struct link_map *undef_map)
32 size_t n = scope->r_nlist;
33 /* Make sure we read the value before proceeding. Otherwise we
34 might use r_list pointing to the initial scope and r_nlist being
35 the value after a resize. That is the only path in dl-open.c not
36 protected by GSCOPE. A read barrier here might be to expensive. */
37 __asm volatile ("" : "+r" (n), "+m" (scope->r_list));
38 struct link_map **list = scope->r_list;
42 /* These variables are used in the nested function. */
43 Elf_Symndx symidx;
44 int num_versions = 0;
45 const ElfW(Sym) *versioned_sym = NULL;
47 const struct link_map *map = list[i]->l_real;
49 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
50 if (map == skip)
51 continue;
53 /* Don't search the executable when resolving a copy reloc. */
54 if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable)
55 continue;
57 /* Do not look into objects which are going to be removed. */
58 if (map->l_removed)
59 continue;
61 /* Print some debugging info if wanted. */
62 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_SYMBOLS, 0))
63 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
64 undef_name,
65 map->l_name[0] ? map->l_name : rtld_progname,
66 map->l_ns);
68 /* If the hash table is empty there is nothing to do here. */
69 if (map->l_nbuckets == 0)
70 continue;
72 /* The tables for this map. */
73 const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
74 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
77 /* Nested routine to check whether the symbol matches. */
78 const ElfW(Sym) *
79 __attribute_noinline__
80 check_match (const ElfW(Sym) *sym)
82 unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
83 assert (ELF_RTYPE_CLASS_PLT == 1);
84 if (__builtin_expect ((sym->st_value == 0 /* No value. */
85 && stt != STT_TLS)
86 || (type_class & (sym->st_shndx == SHN_UNDEF)),
87 0))
88 return NULL;
90 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
91 STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
92 code/data definitions. */
93 #define ALLOWED_STT \
94 ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
95 | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
96 if (__builtin_expect (((1 << stt) & ALLOWED_STT) == 0, 0))
97 return NULL;
99 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
100 /* Not the symbol we are looking for. */
101 return NULL;
103 const ElfW(Half) *verstab = map->l_versyms;
104 if (version != NULL)
106 if (__builtin_expect (verstab == NULL, 0))
108 /* We need a versioned symbol but haven't found any. If
109 this is the object which is referenced in the verneed
110 entry it is a bug in the library since a symbol must
111 not simply disappear.
113 It would also be a bug in the object since it means that
114 the list of required versions is incomplete and so the
115 tests in dl-version.c haven't found a problem.*/
116 assert (version->filename == NULL
117 || ! _dl_name_match_p (version->filename, map));
119 /* Otherwise we accept the symbol. */
121 else
123 /* We can match the version information or use the
124 default one if it is not hidden. */
125 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
126 if ((map->l_versions[ndx].hash != version->hash
127 || strcmp (map->l_versions[ndx].name, version->name))
128 && (version->hidden || map->l_versions[ndx].hash
129 || (verstab[symidx] & 0x8000)))
130 /* It's not the version we want. */
131 return NULL;
134 else
136 /* No specific version is selected. There are two ways we
137 can got here:
139 - a binary which does not include versioning information
140 is loaded
142 - dlsym() instead of dlvsym() is used to get a symbol which
143 might exist in more than one form
145 If the library does not provide symbol version information
146 there is no problem at at: we simply use the symbol if it
147 is defined.
149 These two lookups need to be handled differently if the
150 library defines versions. In the case of the old
151 unversioned application the oldest (default) version
152 should be used. In case of a dlsym() call the latest and
153 public interface should be returned. */
154 if (verstab != NULL)
156 if ((verstab[symidx] & 0x7fff)
157 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
159 /* Don't accept hidden symbols. */
160 if ((verstab[symidx] & 0x8000) == 0
161 && num_versions++ == 0)
162 /* No version so far. */
163 versioned_sym = sym;
165 return NULL;
170 /* There cannot be another entry for this symbol so stop here. */
171 return sym;
174 const ElfW(Sym) *sym;
175 const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
176 if (__builtin_expect (bitmask != NULL, 1))
178 ElfW(Addr) bitmask_word
179 = bitmask[(new_hash / __ELF_NATIVE_CLASS)
180 & map->l_gnu_bitmask_idxbits];
182 unsigned int hashbit1 = new_hash & (__ELF_NATIVE_CLASS - 1);
183 unsigned int hashbit2 = ((new_hash >> map->l_gnu_shift)
184 & (__ELF_NATIVE_CLASS - 1));
186 if (__builtin_expect ((bitmask_word >> hashbit1)
187 & (bitmask_word >> hashbit2) & 1, 0))
189 Elf32_Word bucket = map->l_gnu_buckets[new_hash
190 % map->l_nbuckets];
191 if (bucket != 0)
193 const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
196 if (((*hasharr ^ new_hash) >> 1) == 0)
198 symidx = hasharr - map->l_gnu_chain_zero;
199 sym = check_match (&symtab[symidx]);
200 if (sym != NULL)
201 goto found_it;
203 while ((*hasharr++ & 1u) == 0);
206 /* No symbol found. */
207 symidx = SHN_UNDEF;
209 else
211 if (*old_hash == 0xffffffff)
212 *old_hash = _dl_elf_hash (undef_name);
214 /* Use the old SysV-style hash table. Search the appropriate
215 hash bucket in this object's symbol table for a definition
216 for the same symbol name. */
217 for (symidx = map->l_buckets[*old_hash % map->l_nbuckets];
218 symidx != STN_UNDEF;
219 symidx = map->l_chain[symidx])
221 sym = check_match (&symtab[symidx]);
222 if (sym != NULL)
223 goto found_it;
227 /* If we have seen exactly one versioned symbol while we are
228 looking for an unversioned symbol and the version is not the
229 default version we still accept this symbol since there are
230 no possible ambiguities. */
231 sym = num_versions == 1 ? versioned_sym : NULL;
233 if (sym != NULL)
235 found_it:
236 switch (__builtin_expect (ELFW(ST_BIND) (sym->st_info), STB_GLOBAL))
238 case STB_WEAK:
239 /* Weak definition. Use this value if we don't find another. */
240 if (__builtin_expect (GLRO(dl_dynamic_weak), 0))
242 if (! result->s)
244 result->s = sym;
245 result->m = (struct link_map *) map;
247 break;
249 /* FALLTHROUGH */
250 case STB_GLOBAL:
251 success:
252 /* Global definition. Just what we need. */
253 result->s = sym;
254 result->m = (struct link_map *) map;
255 return 1;
257 case STB_GNU_UNIQUE:;
258 /* We have to determine whether we already found a
259 symbol with this name before. If not then we have to
260 add it to the search table. If we already found a
261 definition we have to use it. */
262 void enter (struct unique_sym *table, size_t size,
263 unsigned int hash, const char *name,
264 const ElfW(Sym) *sym, const struct link_map *map)
266 size_t idx = hash % size;
267 size_t hash2 = 1 + hash % (size - 2);
268 while (1)
270 if (table[idx].hashval == 0)
272 table[idx].hashval = hash;
273 table[idx].name = strtab + sym->st_name;
274 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
276 table[idx].sym = ref;
277 table[idx].map = undef_map;
279 else
281 table[idx].sym = sym;
282 table[idx].map = map;
284 return;
287 idx += hash2;
288 if (idx >= size)
289 idx -= size;
293 struct unique_sym_table *tab
294 = &GL(dl_ns)[map->l_ns]._ns_unique_sym_table;
296 __rtld_lock_lock_recursive (tab->lock);
298 struct unique_sym *entries = tab->entries;
299 size_t size = tab->size;
300 if (entries != NULL)
302 size_t idx = new_hash % size;
303 size_t hash2 = 1 + new_hash % (size - 2);
304 while (1)
306 if (entries[idx].hashval == new_hash
307 && strcmp (entries[idx].name, undef_name) == 0)
309 result->s = entries[idx].sym;
310 result->m = (struct link_map *) entries[idx].map;
311 __rtld_lock_unlock_recursive (tab->lock);
312 return 1;
315 if (entries[idx].hashval == 0
316 && entries[idx].name == NULL)
317 break;
319 idx += hash2;
320 if (idx >= size)
321 idx -= size;
324 if (size * 3 <= tab->n_elements)
326 /* Expand the table. */
327 size_t newsize = _dl_higher_prime_number (size);
328 struct unique_sym *newentries
329 = calloc (sizeof (struct unique_sym), newsize);
330 if (newentries == NULL)
332 nomem:
333 __rtld_lock_unlock_recursive (tab->lock);
334 _dl_fatal_printf ("out of memory\n");
337 for (idx = 0; idx < size; ++idx)
338 if (entries[idx].hashval != 0)
339 enter (newentries, newsize, entries[idx].hashval,
340 entries[idx].name, entries[idx].sym,
341 entries[idx].map);
343 tab->free (entries);
344 tab->size = newsize;
345 entries = tab->entries = newentries;
346 tab->free = free;
349 else
351 #define INITIAL_NUNIQUE_SYM_TABLE 31
352 size = INITIAL_NUNIQUE_SYM_TABLE;
353 entries = calloc (sizeof (struct unique_sym), size);
354 if (entries == NULL)
355 goto nomem;
357 tab->entries = entries;
358 tab->size = size;
359 tab->free = free;
362 enter (entries, size, new_hash, strtab + sym->st_name, sym, map);
363 ++tab->n_elements;
365 __rtld_lock_unlock_recursive (tab->lock);
367 goto success;
369 default:
370 /* Local symbols are ignored. */
371 break;
375 /* If this current map is the one mentioned in the verneed entry
376 and we have not found a weak entry, it is a bug. */
377 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
378 && __builtin_expect (_dl_name_match_p (version->filename, map), 0))
379 return -1;
381 while (++i < n);
383 /* We have not found anything until now. */
384 return 0;