Expand PMF_FN_* macros.
[netbsd-mini2440.git] / libexec / ld.elf_so / symbol.c
blob55f33be63a03dc619460e1a359076394ddaf46a6
1 /* $NetBSD: symbol.c,v 1.47 2008/10/04 09:37:12 skrll Exp $ */
3 /*
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt@3am-software.com>
6 * Copyright 2002 Charles M. Hannum <root@ihack.net>
7 * All rights reserved.
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.
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.
36 * Dynamic linker for ELF.
38 * John Polstra <jdp@polstra.com>.
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __RCSID("$NetBSD: symbol.c,v 1.47 2008/10/04 09:37:12 skrll Exp $");
44 #endif /* not lint */
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>
58 #include "debug.h"
59 #include "rtld.h"
61 typedef void (*fptr_t)(void);
63 static bool
64 _rtld_is_exported(const Elf_Sym *def)
66 static const fptr_t _rtld_exports[] = {
67 (fptr_t)dlopen,
68 (fptr_t)dlclose,
69 (fptr_t)dlsym,
70 (fptr_t)dlerror,
71 (fptr_t)dladdr,
72 (fptr_t)dlinfo,
73 NULL
75 int i;
76 fptr_t value;
78 value = (fptr_t)(_rtld_objself.relocbase + def->st_value);
79 for (i = 0; _rtld_exports[i] != NULL; i++) {
80 if (value == _rtld_exports[i])
81 return true;
83 return false;
87 * Hash function for symbol table lookup. Don't even think about changing
88 * this. It is specified by the System V ABI.
90 unsigned long
91 _rtld_elf_hash(const char *name)
93 const unsigned char *p = (const unsigned char *) name;
94 unsigned long h = 0;
95 unsigned long g;
96 unsigned long c;
98 for (; __predict_true((c = *p) != '\0'); p++) {
99 h <<= 4;
100 h += c;
101 if ((g = h & 0xf0000000) != 0) {
102 h ^= g;
103 h ^= g >> 24;
106 return (h);
109 const Elf_Sym *
110 _rtld_symlook_list(const char *name, unsigned long hash, const Objlist *objlist,
111 const Obj_Entry **defobj_out, bool in_plt)
113 const Elf_Sym *symp;
114 const Elf_Sym *def;
115 const Obj_Entry *defobj;
116 const Objlist_Entry *elm;
118 def = NULL;
119 defobj = NULL;
120 SIMPLEQ_FOREACH(elm, objlist, link) {
121 rdbg(("search object %p (%s) for %s", elm->obj, elm->obj->path,
122 name));
123 if ((symp = _rtld_symlook_obj(name, hash, elm->obj, in_plt))
124 != NULL) {
125 if ((def == NULL) ||
126 (ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
127 def = symp;
128 defobj = elm->obj;
129 if (ELF_ST_BIND(def->st_info) != STB_WEAK)
130 break;
134 if (def != NULL)
135 *defobj_out = defobj;
136 return def;
140 * Search the symbol table of a shared object and all objects needed by it for
141 * a symbol of the given name. Search order is breadth-first. Returns a pointer
142 * to the symbol, or NULL if no definition was found.
144 const Elf_Sym *
145 _rtld_symlook_needed(const char *name, unsigned long hash,
146 const Needed_Entry *needed, const Obj_Entry **defobj_out, bool inplt)
148 const Elf_Sym *def, *def_w;
149 const Needed_Entry *n;
150 const Obj_Entry *obj, *defobj, *defobj1;
152 def = def_w = NULL;
153 defobj = NULL;
154 for (n = needed; n != NULL; n = n->next) {
155 if ((obj = n->obj) == NULL ||
156 (def = _rtld_symlook_obj(name, hash, obj, inplt)) == NULL)
157 continue;
158 defobj = obj;
159 if (ELF_ST_BIND(def->st_info) != STB_WEAK) {
160 *defobj_out = defobj;
162 return (def);
166 * Either the symbol definition has not been found in directly needed
167 * objects, or the found symbol is weak.
169 for (n = needed; n != NULL; n = n->next) {
170 if ((obj = n->obj) == NULL)
171 continue;
172 def_w = _rtld_symlook_needed(name, hash, obj->needed, &defobj1,
173 inplt);
174 if (def_w == NULL)
175 continue;
176 if (def == NULL || ELF_ST_BIND(def_w->st_info) != STB_WEAK) {
177 def = def_w;
178 defobj = defobj1;
179 if (ELF_ST_BIND(def_w->st_info) != STB_WEAK)
180 break;
183 if (def != NULL)
184 *defobj_out = defobj;
186 return def;
190 * Search the symbol table of a single shared object for a symbol of
191 * the given name. Returns a pointer to the symbol, or NULL if no
192 * definition was found.
194 * The symbol's hash value is passed in for efficiency reasons; that
195 * eliminates many recomputations of the hash value.
197 const Elf_Sym *
198 _rtld_symlook_obj(const char *name, unsigned long hash,
199 const Obj_Entry *obj, bool in_plt)
201 unsigned long symnum;
203 for (symnum = obj->buckets[hash % obj->nbuckets];
204 symnum != ELF_SYM_UNDEFINED;
205 symnum = obj->chains[symnum]) {
206 const Elf_Sym *symp;
207 const char *strp;
209 assert(symnum < obj->nchains);
210 symp = obj->symtab + symnum;
211 strp = obj->strtab + symp->st_name;
212 rdbg(("check \"%s\" vs \"%s\" in %p", name, strp, obj));
213 if (name[1] == strp[1] && !strcmp(name, strp)) {
214 if (symp->st_shndx != SHN_UNDEF)
215 return symp;
216 #ifndef __mips__
218 * XXX DANGER WILL ROBINSON!
219 * If we have a function pointer in the executable's
220 * data section, it points to the executable's PLT
221 * slot, and there is NO relocation emitted. To make
222 * the function pointer comparable to function pointers
223 * in shared libraries, we must resolve data references
224 * in the libraries to point to PLT slots in the
225 * executable, if they exist.
227 else if (!in_plt && symp->st_value != 0 &&
228 ELF_ST_TYPE(symp->st_info) == STT_FUNC)
229 return symp;
230 #endif
231 else
232 return NULL;
236 return NULL;
240 * Given a symbol number in a referencing object, find the corresponding
241 * definition of the symbol. Returns a pointer to the symbol, or NULL if
242 * no definition was found. Returns a pointer to the Obj_Entry of the
243 * defining object via the reference parameter DEFOBJ_OUT.
245 const Elf_Sym *
246 _rtld_find_symdef(unsigned long symnum, const Obj_Entry *refobj,
247 const Obj_Entry **defobj_out, bool in_plt)
249 const Elf_Sym *ref;
250 const Elf_Sym *def;
251 const Obj_Entry *defobj;
252 const char *name;
253 unsigned long hash;
255 #ifdef COMBRELOC
257 * COMBRELOC combines multiple reloc sections and sorts them to make
258 * dynamic symbol lookup caching possible.
260 * So if the lookup we are doing is the same as the previous lookup
261 * return the cached results.
263 static unsigned long last_symnum;
264 static const Elf_Sym *last_def;
265 static const Obj_Entry *last_refobj;
266 static const Obj_Entry *last_defobj;
268 if (symnum == last_symnum && refobj == last_refobj
269 && in_plt == false) {
270 *defobj_out = last_defobj;
271 return last_def;
273 #endif
275 ref = refobj->symtab + symnum;
276 name = refobj->strtab + ref->st_name;
279 * We don't have to do a full scale lookup if the symbol is local.
280 * We know it will bind to the instance in this load module; to
281 * which we already have a pointer (ie ref).
283 if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
284 if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
285 _rtld_error("%s: Bogus symbol table entry %lu",
286 refobj->path, symnum);
289 hash = _rtld_elf_hash(name);
290 defobj = NULL;
291 def = _rtld_symlook_default(name, hash, refobj, &defobj, in_plt);
292 } else {
293 rdbg(("STB_LOCAL symbol %s in %s", name, refobj->path));
294 def = ref;
295 defobj = refobj;
299 * If we found no definition and the reference is weak, treat the
300 * symbol as having the value zero.
302 if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
303 if (in_plt) {
304 _rtld_error(
305 "%s: Trying to call undefined weak symbol `%s'",
306 refobj->path, name);
308 rdbg((" returning _rtld_sym_zero@_rtld_objself"));
309 def = &_rtld_sym_zero;
310 defobj = &_rtld_objself;
313 if (def != NULL) {
314 *defobj_out = defobj;
315 #ifdef COMBRELOC
316 if (in_plt == false) {
318 * Cache the lookup arguments and results if this was
319 * non-PLT lookup.
321 last_symnum = symnum;
322 last_refobj = refobj;
323 last_def = def;
324 last_defobj = defobj;
326 #endif
327 } else {
328 rdbg(("lookup failed"));
329 _rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
330 refobj->path, in_plt ? "PLT " : "", name, symnum);
332 return def;
336 * Given a symbol name in a referencing object, find the corresponding
337 * definition of the symbol. Returns a pointer to the symbol, or NULL if
338 * no definition was found. Returns a pointer to the Obj_Entry of the
339 * defining object via the reference parameter DEFOBJ_OUT.
341 const Elf_Sym *
342 _rtld_symlook_default(const char *name, unsigned long hash,
343 const Obj_Entry *refobj, const Obj_Entry **defobj_out, bool in_plt)
345 const Elf_Sym *def;
346 const Elf_Sym *symp;
347 const Obj_Entry *obj;
348 const Obj_Entry *defobj;
349 const Objlist_Entry *elm;
350 def = NULL;
351 defobj = NULL;
353 /* Look first in the referencing object if linked symbolically. */
354 if (refobj->symbolic) {
355 rdbg(("search referencing object for %s", name));
356 symp = _rtld_symlook_obj(name, hash, refobj, in_plt);
357 if (symp != NULL) {
358 def = symp;
359 defobj = refobj;
363 /* Search all objects loaded at program start up. */
364 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
365 rdbg(("search _rtld_list_main for %s", name));
366 symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj,
367 in_plt);
368 if (symp != NULL &&
369 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
370 def = symp;
371 defobj = obj;
375 /* Search all RTLD_GLOBAL objects. */
376 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
377 rdbg(("search _rtld_list_global for %s", name));
378 symp = _rtld_symlook_list(name, hash, &_rtld_list_global,
379 &obj, in_plt);
380 if (symp != NULL &&
381 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
382 def = symp;
383 defobj = obj;
387 /* Search all dlopened DAGs containing the referencing object. */
388 SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
389 if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
390 break;
391 rdbg(("search DAG with root %p (%s) for %s", elm->obj,
392 elm->obj->path, name));
393 symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers,
394 &obj, in_plt);
395 if (symp != NULL &&
396 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
397 def = symp;
398 defobj = obj;
403 * Search the dynamic linker itself, and possibly resolve the
404 * symbol from there. This is how the application links to
405 * dynamic linker services such as dlopen. Only the values listed
406 * in the "_rtld_exports" array can be resolved from the dynamic
407 * linker.
409 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
410 rdbg(("Search the dynamic linker itself."));
411 symp = _rtld_symlook_obj(name, hash, &_rtld_objself, in_plt);
412 if (symp != NULL && _rtld_is_exported(symp)) {
413 def = symp;
414 defobj = &_rtld_objself;
418 if (def != NULL)
419 *defobj_out = defobj;
420 return def;