Update.
[glibc.git] / elf / do-lookup.h
blob03eb2c66c76f083f658967a33bec9106082432d3
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995, 1996, 1997, 1998, 1999 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, struct link_map *undef_map,
33 unsigned long int hash, const ElfW(Sym) *ref, struct sym_val *result,
34 struct r_scope_elem *scope, size_t i, ARG struct link_map *skip,
35 int reloc_type)
37 struct link_map **list = scope->r_list;
38 size_t n = scope->r_nlist;
39 struct link_map *map;
43 const ElfW(Sym) *symtab;
44 const char *strtab;
45 const ElfW(Half) *verstab;
46 Elf_Symndx symidx;
47 const ElfW(Sym) *sym;
48 #if ! VERSIONED
49 int num_versions = 0;
50 const ElfW(Sym) *versioned_sym = NULL;
51 #endif
53 map = list[i];
55 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
56 if (skip != NULL && map == skip)
57 continue;
59 /* Don't search the executable when resolving a copy reloc. */
60 if (elf_machine_lookup_noexec_p (reloc_type)
61 && map->l_type == lt_executable)
62 continue;
64 /* Print some debugging info if wanted. */
65 if (_dl_debug_symbols)
66 _dl_debug_message (1, "symbol=", undef_name, "; lookup in file=",
67 map->l_name[0] ? map->l_name : _dl_argv[0],
68 "\n", NULL);
70 symtab = (const void *) map->l_info[DT_SYMTAB]->d_un.d_ptr;
71 strtab = (const void *) map->l_info[DT_STRTAB]->d_un.d_ptr;
72 verstab = map->l_versyms;
74 /* Search the appropriate hash bucket in this object's symbol table
75 for a definition for the same symbol name. */
76 for (symidx = map->l_buckets[hash % map->l_nbuckets];
77 symidx != STN_UNDEF;
78 symidx = map->l_chain[symidx])
80 sym = &symtab[symidx];
82 if (sym->st_value == 0 || /* No value. */
83 (elf_machine_lookup_noplt_p (reloc_type) /* Reject PLT entry. */
84 && sym->st_shndx == SHN_UNDEF))
85 continue;
87 if (ELFW(ST_TYPE) (sym->st_info) > STT_FUNC)
88 /* Ignore all but STT_NOTYPE, STT_OBJECT and STT_FUNC entries
89 since these are no code/data definitions. */
90 continue;
92 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
93 /* Not the symbol we are looking for. */
94 continue;
96 #if VERSIONED
97 if (verstab == NULL)
99 /* We need a versioned symbol but haven't found any. If
100 this is the object which is referenced in the verneed
101 entry it is a bug in the library since a symbol must
102 not simply disappear.
104 It would also be a bug in the object since it means that
105 the list of required versions is incomplete and so the
106 tests in dl-version.c haven't found a problem.*/
107 assert (version->filename == NULL
108 || ! _dl_name_match_p (version->filename, map));
110 /* Otherwise we accept the symbol. */
112 else
114 /* We can match the version information or use the
115 default one if it is not hidden. */
116 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
117 if ((map->l_versions[ndx].hash != version->hash
118 || strcmp (map->l_versions[ndx].name, version->name))
119 && (version->hidden || map->l_versions[ndx].hash
120 || (verstab[symidx] & 0x8000)))
121 /* It's not the version we want. */
122 continue;
124 #else
125 /* No specific version is selected. When the object file
126 also does not define a version we have a match.
127 Otherwise we accept the default version, or in case there
128 is only one version defined, this one version. */
129 if (verstab != NULL)
131 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
132 if (ndx > 2) /* map->l_versions[ndx].hash != 0) */
134 /* Don't accept hidden symbols. */
135 if ((verstab[symidx] & 0x8000) == 0 && num_versions++ == 0)
136 /* No version so far. */
137 versioned_sym = sym;
138 continue;
141 #endif
143 /* There cannot be another entry for this symbol so stop here. */
144 goto found_it;
147 /* If we have seen exactly one versioned symbol while we are
148 looking for an unversioned symbol and the version is not the
149 default version we still accept this symbol since there are
150 no possible ambiguities. */
151 #if VERSIONED
152 sym = NULL;
153 #else
154 sym = num_versions == 1 ? versioned_sym : NULL;
155 #endif
157 if (sym != NULL
158 /* Don't allow binding if the symbol is hidden. When processor
159 specific definitions for STV_INTERNAL are defined we might
160 have to extend this conditional. */
161 && (ELFW(ST_VISIBILITY) (sym->st_other) != STV_HIDDEN
162 || map == undef_map))
164 found_it:
165 switch (ELFW(ST_BIND) (sym->st_info))
167 case STB_GLOBAL:
168 /* Global definition. Just what we need. */
169 result->s = sym;
170 result->m = map;
171 return 1;
172 case STB_WEAK:
173 /* Weak definition. Use this value if we don't find another. */
174 if (! result->s)
176 result->s = sym;
177 result->m = map;
179 break;
180 default:
181 /* Local symbols are ignored. */
182 break;
186 #if VERSIONED
187 /* If this current map is the one mentioned in the verneed entry
188 and we have not found a weak entry, it is a bug. */
189 if (symidx == STN_UNDEF && version->filename != NULL
190 && _dl_name_match_p (version->filename, map))
191 return -1;
192 #endif
194 while (++i < n);
196 /* We have not found anything until now. */
197 return 0;
200 #undef FCT
201 #undef ARG
202 #undef VERSIONED