[BZ #2517]
[glibc.git] / elf / do-lookup.h
blob7b62b0feecd676e2bc7bf739cb724cb0f842c4fd
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995-2004, 2005, 2006 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. */
20 /* Inner part of the lookup functions. We return a value > 0 if we
21 found the symbol, the value 0 if nothing is found and < 0 if
22 something bad happened. */
23 static int
24 __attribute_noinline__
25 do_lookup_x (const char *undef_name, unsigned long int hash,
26 const ElfW(Sym) *ref, struct sym_val *result,
27 struct r_scope_elem *scope, size_t i,
28 const struct r_found_version *const version, int flags,
29 struct link_map *skip, int type_class)
31 struct link_map **list = scope->r_list;
32 size_t n = scope->r_nlist;
33 struct link_map *map;
37 const ElfW(Sym) *symtab;
38 const char *strtab;
39 const ElfW(Half) *verstab;
40 Elf_Symndx symidx;
41 const ElfW(Sym) *sym;
42 int num_versions = 0;
43 const ElfW(Sym) *versioned_sym = NULL;
45 map = list[i]->l_real;
47 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
48 if (map == skip)
49 continue;
51 /* Don't search the executable when resolving a copy reloc. */
52 if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable)
53 continue;
55 /* Do not look into objects which are going to be removed. */
56 if (map->l_removed)
57 continue;
59 /* Print some debugging info if wanted. */
60 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_SYMBOLS, 0))
61 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
62 undef_name,
63 map->l_name[0] ? map->l_name : rtld_progname,
64 map->l_ns);
66 symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
67 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
68 verstab = map->l_versyms;
70 /* Search the appropriate hash bucket in this object's symbol table
71 for a definition for the same symbol name. */
72 for (symidx = map->l_buckets[hash % map->l_nbuckets];
73 symidx != STN_UNDEF;
74 symidx = map->l_chain[symidx])
76 sym = &symtab[symidx];
78 assert (ELF_RTYPE_CLASS_PLT == 1);
79 if ((sym->st_value == 0 /* No value. */
80 #ifdef USE_TLS
81 && ELFW(ST_TYPE) (sym->st_info) != STT_TLS
82 #endif
84 || (type_class & (sym->st_shndx == SHN_UNDEF)))
85 continue;
87 if (ELFW(ST_TYPE) (sym->st_info) > STT_FUNC
88 #ifdef USE_TLS
89 && ELFW(ST_TYPE) (sym->st_info) != STT_TLS
90 #endif
92 /* Ignore all but STT_NOTYPE, STT_OBJECT and STT_FUNC
93 entries (and STT_TLS if TLS is supported) since these
94 are no code/data definitions. */
95 continue;
97 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
98 /* Not the symbol we are looking for. */
99 continue;
101 if (version != NULL)
103 if (__builtin_expect (verstab == NULL, 0))
105 /* We need a versioned symbol but haven't found any. If
106 this is the object which is referenced in the verneed
107 entry it is a bug in the library since a symbol must
108 not simply disappear.
110 It would also be a bug in the object since it means that
111 the list of required versions is incomplete and so the
112 tests in dl-version.c haven't found a problem.*/
113 assert (version->filename == NULL
114 || ! _dl_name_match_p (version->filename, map));
116 /* Otherwise we accept the symbol. */
118 else
120 /* We can match the version information or use the
121 default one if it is not hidden. */
122 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
123 if ((map->l_versions[ndx].hash != version->hash
124 || strcmp (map->l_versions[ndx].name, version->name))
125 && (version->hidden || map->l_versions[ndx].hash
126 || (verstab[symidx] & 0x8000)))
127 /* It's not the version we want. */
128 continue;
131 else
133 /* No specific version is selected. There are two ways we
134 can got here:
136 - a binary which does not include versioning information
137 is loaded
139 - dlsym() instead of dlvsym() is used to get a symbol which
140 might exist in more than one form
142 If the library does not provide symbol version
143 information there is no problem at at: we simply use the
144 symbol if it is defined.
146 These two lookups need to be handled differently if the
147 library defines versions. In the case of the old
148 unversioned application the oldest (default) version
149 should be used. In case of a dlsym() call the latest and
150 public interface should be returned. */
151 if (verstab != NULL)
153 if ((verstab[symidx] & 0x7fff)
154 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
156 /* Don't accept hidden symbols. */
157 if ((verstab[symidx] & 0x8000) == 0
158 && num_versions++ == 0)
159 /* No version so far. */
160 versioned_sym = sym;
162 continue;
167 /* There cannot be another entry for this symbol so stop here. */
168 goto found_it;
171 /* If we have seen exactly one versioned symbol while we are
172 looking for an unversioned symbol and the version is not the
173 default version we still accept this symbol since there are
174 no possible ambiguities. */
175 sym = num_versions == 1 ? versioned_sym : NULL;
177 if (sym != NULL)
179 found_it:
180 switch (ELFW(ST_BIND) (sym->st_info))
182 case STB_WEAK:
183 /* Weak definition. Use this value if we don't find another. */
184 if (__builtin_expect (GLRO(dl_dynamic_weak), 0))
186 if (! result->s)
188 result->s = sym;
189 result->m = map;
191 break;
193 /* FALLTHROUGH */
194 case STB_GLOBAL:
195 /* Global definition. Just what we need. */
196 result->s = sym;
197 result->m = map;
198 return 1;
199 default:
200 /* Local symbols are ignored. */
201 break;
205 /* If this current map is the one mentioned in the verneed entry
206 and we have not found a weak entry, it is a bug. */
207 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
208 && __builtin_expect (_dl_name_match_p (version->filename, map), 0))
209 return -1;
211 while (++i < n);
213 /* We have not found anything until now. */
214 return 0;
217 #undef FCT
218 #undef ARG
219 #undef VERSIONED