Update.
[glibc.git] / elf / do-lookup.h
blobc0f9b67ae3a8f36b1266f61db332aba543322693
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995,96,97,98,99,2000,2001 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. */
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
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 noexec, int noplt)
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 (noexec && map->l_type == lt_executable)
60 continue;
62 /* Print some debugging info if wanted. */
63 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_SYMBOLS, 0))
64 _dl_debug_printf ("symbol=%s; lookup in file=%s\n", undef_name,
65 map->l_name[0] ? map->l_name : _dl_argv[0]);
67 symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
68 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
69 verstab = map->l_versyms;
71 /* Search the appropriate hash bucket in this object's symbol table
72 for a definition for the same symbol name. */
73 for (symidx = map->l_buckets[hash % map->l_nbuckets];
74 symidx != STN_UNDEF;
75 symidx = map->l_chain[symidx])
77 sym = &symtab[symidx];
79 if (sym->st_value == 0 || /* No value. */
80 (noplt && sym->st_shndx == SHN_UNDEF))
81 continue;
83 if (ELFW(ST_TYPE) (sym->st_info) > STT_FUNC)
84 /* Ignore all but STT_NOTYPE, STT_OBJECT and STT_FUNC entries
85 since these are no code/data definitions. */
86 continue;
88 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
89 /* Not the symbol we are looking for. */
90 continue;
92 #if VERSIONED
93 if (__builtin_expect (verstab == NULL, 0))
95 /* We need a versioned symbol but haven't found any. If
96 this is the object which is referenced in the verneed
97 entry it is a bug in the library since a symbol must
98 not simply disappear.
100 It would also be a bug in the object since it means that
101 the list of required versions is incomplete and so the
102 tests in dl-version.c haven't found a problem.*/
103 assert (version->filename == NULL
104 || ! _dl_name_match_p (version->filename, map));
106 /* Otherwise we accept the symbol. */
108 else
110 /* We can match the version information or use the
111 default one if it is not hidden. */
112 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
113 if ((map->l_versions[ndx].hash != version->hash
114 || strcmp (map->l_versions[ndx].name, version->name))
115 && (version->hidden || map->l_versions[ndx].hash
116 || (verstab[symidx] & 0x8000)))
117 /* It's not the version we want. */
118 continue;
120 #else
121 /* No specific version is selected. When the object file
122 also does not define a version we have a match.
123 Otherwise we accept the default version, or in case there
124 is only one version defined, this one version. */
125 if (verstab != NULL)
127 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
128 if (ndx > 2) /* map->l_versions[ndx].hash != 0) */
130 /* Don't accept hidden symbols. */
131 if ((verstab[symidx] & 0x8000) == 0 && num_versions++ == 0)
132 /* No version so far. */
133 versioned_sym = sym;
134 continue;
137 #endif
139 /* There cannot be another entry for this symbol so stop here. */
140 goto found_it;
143 /* If we have seen exactly one versioned symbol while we are
144 looking for an unversioned symbol and the version is not the
145 default version we still accept this symbol since there are
146 no possible ambiguities. */
147 #if VERSIONED
148 sym = NULL;
149 #else
150 sym = num_versions == 1 ? versioned_sym : NULL;
151 #endif
153 if (sym != NULL)
155 found_it:
156 switch (ELFW(ST_BIND) (sym->st_info))
158 case STB_WEAK:
159 /* Weak definition. Use this value if we don't find another. */
160 if (__builtin_expect (_dl_dynamic_weak, 0))
162 if (! result->s)
164 result->s = sym;
165 result->m = map;
167 break;
169 /* FALLTHROUGH */
170 case STB_GLOBAL:
171 /* Global definition. Just what we need. */
172 result->s = sym;
173 result->m = map;
174 return 1;
175 default:
176 /* Local symbols are ignored. */
177 break;
181 #if VERSIONED
182 /* If this current map is the one mentioned in the verneed entry
183 and we have not found a weak entry, it is a bug. */
184 if (symidx == STN_UNDEF && version->filename != NULL
185 && __builtin_expect (_dl_name_match_p (version->filename, map), 0))
186 return -1;
187 #endif
189 while (++i < n);
191 /* We have not found anything until now. */
192 return 0;
195 #undef FCT
196 #undef ARG
197 #undef VERSIONED