symbol.c revision 1.37 1 /* $NetBSD: symbol.c,v 1.37 2004/10/22 05:39:57 skrll Exp $ */
2
3 /*
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 * Copyright 2002 Charles M. Hannum <root (at) ihack.net>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by John Polstra.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * Dynamic linker for ELF.
37 *
38 * John Polstra <jdp (at) polstra.com>.
39 */
40
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __RCSID("$NetBSD: symbol.c,v 1.37 2004/10/22 05:39:57 skrll Exp $");
44 #endif /* not lint */
45
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <sys/types.h>
55 #include <sys/mman.h>
56 #include <dirent.h>
57
58 #include "debug.h"
59 #include "rtld.h"
60
61 static bool
62 _rtld_is_exported(const Elf_Sym *def)
63 {
64 static Elf_Addr _rtld_exports[] = {
65 (Elf_Addr)dlopen,
66 (Elf_Addr)dlclose,
67 (Elf_Addr)dlsym,
68 (Elf_Addr)dlerror,
69 (Elf_Addr)dladdr,
70
71 #if 0
72 /*
73 * Don't need to list these since they are aliases of the
74 * above symbols, and thus have the same value.
75 */
76 (Elf_Addr)__dlopen,
77 (Elf_Addr)__dlclose,
78 (Elf_Addr)__dlsym,
79 (Elf_Addr)__dlerror,
80 (Elf_Addr)__dladdr,
81 #endif
82
83 0
84 };
85 int i;
86
87 Elf_Addr value;
88 value = (Elf_Addr)(_rtld_objself.relocbase + def->st_value);
89
90 for (i = 0; _rtld_exports[i] != 0; i++) {
91 if (value == _rtld_exports[i])
92 return true;
93 }
94 return false;
95 }
96
97 /*
98 * Hash function for symbol table lookup. Don't even think about changing
99 * this. It is specified by the System V ABI.
100 */
101 unsigned long
102 _rtld_elf_hash(const char *name)
103 {
104 const unsigned char *p = (const unsigned char *) name;
105 unsigned long h = 0;
106 unsigned long g;
107 unsigned long c;
108
109 for (; __predict_true((c = *p) != '\0'); p++) {
110 h <<= 4;
111 h += c;
112 if ((g = h & 0xf0000000) != 0) {
113 h ^= g;
114 h ^= g >> 24;
115 }
116 }
117 return (h);
118 }
119
120 const Elf_Sym *
121 _rtld_symlook_list(const char *name, unsigned long hash, const Objlist *objlist,
122 const Obj_Entry **defobj_out, bool in_plt)
123 {
124 const Elf_Sym *symp;
125 const Elf_Sym *def;
126 const Obj_Entry *defobj;
127 const Objlist_Entry *elm;
128
129 def = NULL;
130 defobj = NULL;
131 SIMPLEQ_FOREACH(elm, objlist, link) {
132 rdbg(("search object %p (%s)", elm->obj, elm->obj->path));
133 if ((symp = _rtld_symlook_obj(name, hash, elm->obj, in_plt))
134 != NULL) {
135 if ((def == NULL) ||
136 (ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
137 def = symp;
138 defobj = elm->obj;
139 if (ELF_ST_BIND(def->st_info) != STB_WEAK)
140 break;
141 }
142 }
143 }
144 if (def != NULL)
145 *defobj_out = defobj;
146 return def;
147 }
148
149 /*
150 * Search the symbol table of a single shared object for a symbol of
151 * the given name. Returns a pointer to the symbol, or NULL if no
152 * definition was found.
153 *
154 * The symbol's hash value is passed in for efficiency reasons; that
155 * eliminates many recomputations of the hash value.
156 */
157 const Elf_Sym *
158 _rtld_symlook_obj(const char *name, unsigned long hash,
159 const Obj_Entry *obj, bool in_plt)
160 {
161 unsigned long symnum;
162
163 for (symnum = obj->buckets[hash % obj->nbuckets];
164 symnum != ELF_SYM_UNDEFINED;
165 symnum = obj->chains[symnum]) {
166 const Elf_Sym *symp;
167 const char *strp;
168
169 assert(symnum < obj->nchains);
170 symp = obj->symtab + symnum;
171 strp = obj->strtab + symp->st_name;
172 rdbg(("check %s vs %s in %p", name, strp, obj));
173 if (name[1] == strp[1] && !strcmp(name, strp)) {
174 if (symp->st_shndx != SHN_UNDEF)
175 return symp;
176 #ifndef __mips__
177 /*
178 * XXX DANGER WILL ROBINSON!
179 * If we have a function pointer in the executable's
180 * data section, it points to the executable's PLT
181 * slot, and there is NO relocation emitted. To make
182 * the function pointer comparable to function pointers
183 * in shared libraries, we must resolve data references
184 * in the libraries to point to PLT slots in the
185 * executable, if they exist.
186 */
187 else if (!in_plt && symp->st_value != 0 &&
188 ELF_ST_TYPE(symp->st_info) == STT_FUNC)
189 return symp;
190 #endif
191 else
192 return NULL;
193 }
194 }
195
196 return NULL;
197 }
198
199 /*
200 * Given a symbol number in a referencing object, find the corresponding
201 * definition of the symbol. Returns a pointer to the symbol, or NULL if
202 * no definition was found. Returns a pointer to the Obj_Entry of the
203 * defining object via the reference parameter DEFOBJ_OUT.
204 */
205 const Elf_Sym *
206 _rtld_find_symdef(unsigned long symnum, const Obj_Entry *refobj,
207 const Obj_Entry **defobj_out, bool in_plt)
208 {
209 const Elf_Sym *ref;
210 const Elf_Sym *def;
211 const Elf_Sym *symp;
212 const Obj_Entry *obj;
213 const Obj_Entry *defobj;
214 const Objlist_Entry *elm;
215 const char *name;
216 unsigned long hash;
217
218 ref = refobj->symtab + symnum;
219 name = refobj->strtab + ref->st_name;
220
221 hash = _rtld_elf_hash(name);
222 def = NULL;
223 defobj = NULL;
224
225 /* Look first in the referencing object if linked symbolically */
226 if (refobj->symbolic) {
227 symp = _rtld_symlook_obj(name, hash, refobj, in_plt);
228 if (symp != NULL) {
229 def = symp;
230 defobj = refobj;
231 }
232 }
233
234 /* Search all objects loaded at program start up. */
235 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
236 rdbg(("search _rtld_list_main"));
237 symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj, in_plt);
238 if (symp != NULL &&
239 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
240 def = symp;
241 defobj = obj;
242 }
243 }
244
245 /* Search all RTLD_GLOBAL objects. */
246 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
247 rdbg(("search _rtld_list_global"));
248 symp = _rtld_symlook_list(name, hash, &_rtld_list_global, &obj, in_plt);
249 if (symp != NULL &&
250 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
251 def = symp;
252 defobj = obj;
253 }
254 }
255
256 /* Search all dlopened DAGs containing the referencing object. */
257 SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
258 if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
259 break;
260 rdbg(("search DAG with root %p (%s)", elm->obj, elm->obj->path));
261 symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers, &obj, in_plt);
262 if (symp != NULL &&
263 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
264 def = symp;
265 defobj = obj;
266 }
267 }
268
269 /*
270 * Search the dynamic linker itself, and possibly resolve the
271 * symbol from there. This is how the application links to
272 * dynamic linker services such as dlopen. Only the values listed
273 * in the "_rtld_exports" array can be resolved from the dynamic linker.
274 */
275 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
276 symp = _rtld_symlook_obj(name, hash, &_rtld_objself, in_plt);
277 if (symp != NULL && _rtld_is_exported(symp)) {
278 def = symp;
279 defobj = &_rtld_objself;
280 }
281 }
282
283 /*
284 * If we found no definition and the reference is weak, treat the
285 * symbol as having the value zero.
286 */
287 if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
288 rdbg((" returning _rtld_sym_zero@_rtld_objmain"));
289 def = &_rtld_sym_zero;
290 defobj = _rtld_objmain;
291 }
292
293 if (def != NULL)
294 *defobj_out = defobj;
295 else {
296 rdbg(("lookup failed"));
297 _rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
298 refobj->path, in_plt ? "PLT " : "", name, symnum);
299 }
300 return def;
301 }
302
303 /*
304 * Given a symbol name in a referencing object, find the corresponding
305 * definition of the symbol. Returns a pointer to the symbol, or NULL if
306 * no definition was found. Returns a pointer to the Obj_Entry of the
307 * defining object via the reference parameter DEFOBJ_OUT.
308 */
309 const Elf_Sym *
310 _rtld_symlook_default(const char *name, unsigned long hash,
311 const Obj_Entry *refobj, const Obj_Entry **defobj_out, bool in_plt)
312 {
313 const Elf_Sym *def;
314 const Elf_Sym *symp;
315 const Obj_Entry *obj;
316 const Obj_Entry *defobj;
317 const Objlist_Entry *elm;
318 def = NULL;
319 defobj = NULL;
320
321 /* Look first in the referencing object if linked symbolically. */
322 if (refobj->symbolic) {
323 symp = _rtld_symlook_obj(name, hash, refobj, in_plt);
324 if (symp != NULL) {
325 def = symp;
326 defobj = refobj;
327 }
328 }
329
330 /* Search all objects loaded at program start up. */
331 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
332 symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj, in_plt);
333 if (symp != NULL &&
334 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
335 def = symp;
336 defobj = obj;
337 }
338 }
339
340 /* Search all dlopened DAGs containing the referencing object. */
341 SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
342 if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
343 break;
344 symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers, &obj,
345 in_plt);
346 if (symp != NULL &&
347 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
348 def = symp;
349 defobj = obj;
350 }
351 }
352
353 /* Search all DAGs whose roots are RTLD_GLOBAL objects. */
354 SIMPLEQ_FOREACH(elm, &_rtld_list_global, link) {
355 if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
356 break;
357 symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers, &obj,
358 in_plt);
359 if (symp != NULL &&
360 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
361 def = symp;
362 defobj = obj;
363 }
364 }
365
366 #ifdef notyet
367 /*
368 * Search the dynamic linker itself, and possibly resolve the
369 * symbol from there. This is how the application links to
370 * dynamic linker services such as dlopen. Only the values listed
371 * in the "exports" array can be resolved from the dynamic linker.
372 */
373 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
374 symp = _rtld_symlook_obj(name, hash, &_rtld_objself, in_plt);
375 if (symp != NULL && is_exported(symp)) {
376 def = symp;
377 defobj = &_rtld_objself;
378 }
379 }
380 #endif
381
382 if (def != NULL)
383 *defobj_out = defobj;
384 return def;
385 }
386