Update.
[glibc.git] / elf / do-lookup.h
blobbe75fb7b00be08b5c098cafb772769cf45e49612
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995,96,97,98,99,2000,2001,2002 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 #if VERSIONED
21 # define FCT do_lookup_versioned
22 # define ARG const struct r_found_version *const version
23 #else
24 # define FCT do_lookup
25 # define ARG int flags
26 #endif
28 /* Inner part of the lookup functions. We return a value > 0 if we
29 found the symbol, the value 0 if nothing is found and < 0 if
30 something bad happened. */
31 static inline int
32 FCT (const char *undef_name, unsigned long int hash, const ElfW(Sym) *ref,
33 struct sym_val *result, struct r_scope_elem *scope, size_t i, ARG,
34 struct link_map *skip, int type_class)
36 struct link_map **list = scope->r_list;
37 size_t n = scope->r_nlist;
38 struct link_map *map;
42 const ElfW(Sym) *symtab;
43 const char *strtab;
44 const ElfW(Half) *verstab;
45 Elf_Symndx symidx;
46 const ElfW(Sym) *sym;
47 #if ! VERSIONED
48 int num_versions = 0;
49 const ElfW(Sym) *versioned_sym = NULL;
50 #endif
52 map = list[i];
54 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
55 if (skip != NULL && map == skip)
56 continue;
58 /* Don't search the executable when resolving a copy reloc. */
59 if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable)
60 continue;
62 /* Print some debugging info if wanted. */
63 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_SYMBOLS, 0))
64 INTUSE(_dl_debug_printf) ("symbol=%s; lookup in file=%s\n",
65 undef_name, (map->l_name[0]
66 ? map->l_name : rtld_progname));
68 symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
69 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
70 verstab = map->l_versyms;
72 /* Search the appropriate hash bucket in this object's symbol table
73 for a definition for the same symbol name. */
74 for (symidx = map->l_buckets[hash % map->l_nbuckets];
75 symidx != STN_UNDEF;
76 symidx = map->l_chain[symidx])
78 sym = &symtab[symidx];
80 assert (ELF_RTYPE_CLASS_PLT == 1);
81 if ((sym->st_value == 0 /* No value. */
82 #ifdef USE_TLS
83 && ELFW(ST_TYPE) (sym->st_info) != STT_TLS
84 #endif
86 || (type_class & (sym->st_shndx == SHN_UNDEF)))
87 continue;
89 if (ELFW(ST_TYPE) (sym->st_info) > STT_FUNC
90 #ifdef USE_TLS
91 && ELFW(ST_TYPE) (sym->st_info) != STT_TLS
92 #endif
94 /* Ignore all but STT_NOTYPE, STT_OBJECT and STT_FUNC
95 entries (and STT_TLS if TLS is supported) since these
96 are no code/data definitions. */
97 continue;
99 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
100 /* Not the symbol we are looking for. */
101 continue;
103 #if VERSIONED
104 if (__builtin_expect (verstab == NULL, 0))
106 /* We need a versioned symbol but haven't found any. If
107 this is the object which is referenced in the verneed
108 entry it is a bug in the library since a symbol must
109 not simply disappear.
111 It would also be a bug in the object since it means that
112 the list of required versions is incomplete and so the
113 tests in dl-version.c haven't found a problem.*/
114 assert (version->filename == NULL
115 || ! _dl_name_match_p (version->filename, map));
117 /* Otherwise we accept the symbol. */
119 else
121 /* We can match the version information or use the
122 default one if it is not hidden. */
123 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
124 if ((map->l_versions[ndx].hash != version->hash
125 || strcmp (map->l_versions[ndx].name, version->name))
126 && (version->hidden || map->l_versions[ndx].hash
127 || (verstab[symidx] & 0x8000)))
128 /* It's not the version we want. */
129 continue;
131 #else
132 /* No specific version is selected. There are two ways we
133 can got here:
135 - a binary which does not include versioning information
136 is loaded
138 - dlsym() instead of dlvsym() is used to get a symbol which
139 might exist in more than one form
141 If the library does not provide symbol version
142 information there is no problem at at: we simply use the
143 symbol if it is defined.
145 These two lookups need to be handled differently if the
146 library defines versions. In the case of the old
147 unversioned application the oldest (default) version
148 should be used. In case of a dlsym() call the latest and
149 public interface should be returned. */
150 if (verstab != NULL)
152 if ((verstab[symidx] & 0x7fff)
153 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
155 /* Don't accept hidden symbols. */
156 if ((verstab[symidx] & 0x8000) == 0 && num_versions++ == 0)
157 /* No version so far. */
158 versioned_sym = sym;
160 continue;
163 #endif
165 /* There cannot be another entry for this symbol so stop here. */
166 goto found_it;
169 /* If we have seen exactly one versioned symbol while we are
170 looking for an unversioned symbol and the version is not the
171 default version we still accept this symbol since there are
172 no possible ambiguities. */
173 #if VERSIONED
174 sym = NULL;
175 #else
176 sym = num_versions == 1 ? versioned_sym : NULL;
177 #endif
179 if (sym != NULL)
181 found_it:
182 switch (ELFW(ST_BIND) (sym->st_info))
184 case STB_WEAK:
185 /* Weak definition. Use this value if we don't find another. */
186 if (__builtin_expect (GL(dl_dynamic_weak), 0))
188 if (! result->s)
190 result->s = sym;
191 result->m = map;
193 break;
195 /* FALLTHROUGH */
196 case STB_GLOBAL:
197 /* Global definition. Just what we need. */
198 result->s = sym;
199 result->m = map;
200 return 1;
201 default:
202 /* Local symbols are ignored. */
203 break;
207 #if VERSIONED
208 /* If this current map is the one mentioned in the verneed entry
209 and we have not found a weak entry, it is a bug. */
210 if (symidx == STN_UNDEF && version->filename != NULL
211 && __builtin_expect (_dl_name_match_p (version->filename, map), 0))
212 return -1;
213 #endif
215 while (++i < n);
217 /* We have not found anything until now. */
218 return 0;
221 #undef FCT
222 #undef ARG
223 #undef VERSIONED