]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/do-lookup.h
(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[thirdparty/glibc.git] / elf / do-lookup.h
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995-2002, 2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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. */
19
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)
30 {
31 struct link_map **list = scope->r_list;
32 size_t n = scope->r_nlist;
33 struct link_map *map;
34
35 do
36 {
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;
44
45 map = list[i]->l_real;
46
47 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
48 if (skip != NULL && map == skip)
49 continue;
50
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;
54
55 /* Print some debugging info if wanted. */
56 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_SYMBOLS, 0))
57 _dl_debug_printf ("symbol=%s; lookup in file=%s\n",
58 undef_name,
59 map->l_name[0] ? map->l_name : rtld_progname);
60
61 symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
62 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
63 verstab = map->l_versyms;
64
65 /* Search the appropriate hash bucket in this object's symbol table
66 for a definition for the same symbol name. */
67 for (symidx = map->l_buckets[hash % map->l_nbuckets];
68 symidx != STN_UNDEF;
69 symidx = map->l_chain[symidx])
70 {
71 sym = &symtab[symidx];
72
73 assert (ELF_RTYPE_CLASS_PLT == 1);
74 if ((sym->st_value == 0 /* No value. */
75 #ifdef USE_TLS
76 && ELFW(ST_TYPE) (sym->st_info) != STT_TLS
77 #endif
78 )
79 || (type_class & (sym->st_shndx == SHN_UNDEF)))
80 continue;
81
82 if (ELFW(ST_TYPE) (sym->st_info) > STT_FUNC
83 #ifdef USE_TLS
84 && ELFW(ST_TYPE) (sym->st_info) != STT_TLS
85 #endif
86 )
87 /* Ignore all but STT_NOTYPE, STT_OBJECT and STT_FUNC
88 entries (and STT_TLS if TLS is supported) since these
89 are no code/data definitions. */
90 continue;
91
92 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
93 /* Not the symbol we are looking for. */
94 continue;
95
96 if (version != NULL)
97 {
98 if (__builtin_expect (verstab == NULL, 0))
99 {
100 /* We need a versioned symbol but haven't found any. If
101 this is the object which is referenced in the verneed
102 entry it is a bug in the library since a symbol must
103 not simply disappear.
104
105 It would also be a bug in the object since it means that
106 the list of required versions is incomplete and so the
107 tests in dl-version.c haven't found a problem.*/
108 assert (version->filename == NULL
109 || ! _dl_name_match_p (version->filename, map));
110
111 /* Otherwise we accept the symbol. */
112 }
113 else
114 {
115 /* We can match the version information or use the
116 default one if it is not hidden. */
117 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
118 if ((map->l_versions[ndx].hash != version->hash
119 || strcmp (map->l_versions[ndx].name, version->name))
120 && (version->hidden || map->l_versions[ndx].hash
121 || (verstab[symidx] & 0x8000)))
122 /* It's not the version we want. */
123 continue;
124 }
125 }
126 else
127 {
128 /* No specific version is selected. There are two ways we
129 can got here:
130
131 - a binary which does not include versioning information
132 is loaded
133
134 - dlsym() instead of dlvsym() is used to get a symbol which
135 might exist in more than one form
136
137 If the library does not provide symbol version
138 information there is no problem at at: we simply use the
139 symbol if it is defined.
140
141 These two lookups need to be handled differently if the
142 library defines versions. In the case of the old
143 unversioned application the oldest (default) version
144 should be used. In case of a dlsym() call the latest and
145 public interface should be returned. */
146 if (verstab != NULL)
147 {
148 if ((verstab[symidx] & 0x7fff)
149 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
150 {
151 /* Don't accept hidden symbols. */
152 if ((verstab[symidx] & 0x8000) == 0
153 && num_versions++ == 0)
154 /* No version so far. */
155 versioned_sym = sym;
156
157 continue;
158 }
159 }
160 }
161
162 /* There cannot be another entry for this symbol so stop here. */
163 goto found_it;
164 }
165
166 /* If we have seen exactly one versioned symbol while we are
167 looking for an unversioned symbol and the version is not the
168 default version we still accept this symbol since there are
169 no possible ambiguities. */
170 sym = num_versions == 1 ? versioned_sym : NULL;
171
172 if (sym != NULL)
173 {
174 found_it:
175 switch (ELFW(ST_BIND) (sym->st_info))
176 {
177 case STB_WEAK:
178 /* Weak definition. Use this value if we don't find another. */
179 if (__builtin_expect (GLRO(dl_dynamic_weak), 0))
180 {
181 if (! result->s)
182 {
183 result->s = sym;
184 result->m = map;
185 }
186 break;
187 }
188 /* FALLTHROUGH */
189 case STB_GLOBAL:
190 /* Global definition. Just what we need. */
191 result->s = sym;
192 result->m = map;
193 return 1;
194 default:
195 /* Local symbols are ignored. */
196 break;
197 }
198 }
199
200 /* If this current map is the one mentioned in the verneed entry
201 and we have not found a weak entry, it is a bug. */
202 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
203 && __builtin_expect (_dl_name_match_p (version->filename, map), 0))
204 return -1;
205 }
206 while (++i < n);
207
208 /* We have not found anything until now. */
209 return 0;
210 }
211
212 #undef FCT
213 #undef ARG
214 #undef VERSIONED