1 /* $NetBSD: symbol.c,v 1.54 2010/10/16 10:27:07 skrll Exp $ */
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt@3am-software.com>
6 * Copyright 2002 Charles M. Hannum <root@ihack.net>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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>
43 __RCSID("$NetBSD: symbol.c,v 1.54 2010/10/16 10:27:07 skrll Exp $");
54 #include <sys/types.h>
56 #include <sys/bitops.h>
62 typedef void (*fptr_t
)(void);
65 * If the given object is already in the donelist, return true. Otherwise
66 * add the object to the list and return false.
69 _rtld_donelist_check(DoneList
*dlp
, const Obj_Entry
*obj
)
73 for (i
= 0; i
< dlp
->num_used
; i
++)
74 if (dlp
->objs
[i
] == obj
)
77 * Our donelist allocation may not always be sufficient as we're not
78 * thread safe. We'll handle it properly anyway.
80 if (dlp
->num_used
< dlp
->num_alloc
)
81 dlp
->objs
[dlp
->num_used
++] = obj
;
86 _rtld_is_exported(const Elf_Sym
*def
)
88 static const fptr_t _rtld_exports
[] = {
95 (fptr_t
)dl_iterate_phdr
,
101 value
= (fptr_t
)(_rtld_objself
.relocbase
+ def
->st_value
);
102 for (i
= 0; _rtld_exports
[i
] != NULL
; i
++) {
103 if (value
== _rtld_exports
[i
])
110 * Hash function for symbol table lookup. Don't even think about changing
111 * this. It is specified by the System V ABI.
114 _rtld_elf_hash(const char *name
)
116 const unsigned char *p
= (const unsigned char *) name
;
121 for (; __predict_true((c
= *p
) != '\0'); p
++) {
124 if ((g
= h
& 0xf0000000) != 0) {
133 _rtld_symlook_list(const char *name
, unsigned long hash
, const Objlist
*objlist
,
134 const Obj_Entry
**defobj_out
, bool in_plt
, DoneList
*dlp
)
138 const Obj_Entry
*defobj
;
139 const Objlist_Entry
*elm
;
143 SIMPLEQ_FOREACH(elm
, objlist
, link
) {
144 if (_rtld_donelist_check(dlp
, elm
->obj
))
146 rdbg(("search object %p (%s) for %s", elm
->obj
, elm
->obj
->path
,
148 if ((symp
= _rtld_symlook_obj(name
, hash
, elm
->obj
, in_plt
))
151 (ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
154 if (ELF_ST_BIND(def
->st_info
) != STB_WEAK
)
160 *defobj_out
= defobj
;
165 * Search the symbol table of a shared object and all objects needed by it for
166 * a symbol of the given name. Search order is breadth-first. Returns a pointer
167 * to the symbol, or NULL if no definition was found.
170 _rtld_symlook_needed(const char *name
, unsigned long hash
,
171 const Needed_Entry
*needed
, const Obj_Entry
**defobj_out
, bool inplt
,
172 DoneList
*breadth
, DoneList
*depth
)
174 const Elf_Sym
*def
, *def_w
;
175 const Needed_Entry
*n
;
176 const Obj_Entry
*obj
, *defobj
, *defobj1
;
180 for (n
= needed
; n
!= NULL
; n
= n
->next
) {
181 if ((obj
= n
->obj
) == NULL
)
183 if (_rtld_donelist_check(breadth
, obj
))
185 if ((def
= _rtld_symlook_obj(name
, hash
, obj
, inplt
)) == NULL
)
188 if (ELF_ST_BIND(def
->st_info
) != STB_WEAK
) {
189 *defobj_out
= defobj
;
195 * Either the symbol definition has not been found in directly needed
196 * objects, or the found symbol is weak.
198 for (n
= needed
; n
!= NULL
; n
= n
->next
) {
199 if ((obj
= n
->obj
) == NULL
)
201 if (_rtld_donelist_check(depth
, obj
))
203 def_w
= _rtld_symlook_needed(name
, hash
, obj
->needed
, &defobj1
,
204 inplt
, breadth
, depth
);
207 if (def
== NULL
|| ELF_ST_BIND(def_w
->st_info
) != STB_WEAK
) {
210 if (ELF_ST_BIND(def_w
->st_info
) != STB_WEAK
)
215 *defobj_out
= defobj
;
221 * Search the symbol table of a single shared object for a symbol of
222 * the given name. Returns a pointer to the symbol, or NULL if no
223 * definition was found.
225 * The symbol's hash value is passed in for efficiency reasons; that
226 * eliminates many recomputations of the hash value.
229 _rtld_symlook_obj(const char *name
, unsigned long hash
,
230 const Obj_Entry
*obj
, bool in_plt
)
232 unsigned long symnum
;
234 for (symnum
= obj
->buckets
[fast_remainder32(hash
, obj
->nbuckets
,
235 obj
->nbuckets_m
, obj
->nbuckets_s1
, obj
->nbuckets_s2
)];
236 symnum
!= ELF_SYM_UNDEFINED
;
237 symnum
= obj
->chains
[symnum
]) {
241 assert(symnum
< obj
->nchains
);
242 symp
= obj
->symtab
+ symnum
;
243 strp
= obj
->strtab
+ symp
->st_name
;
244 rdbg(("check \"%s\" vs \"%s\" in %p", name
, strp
, obj
));
245 if (name
[1] == strp
[1] && !strcmp(name
, strp
)) {
246 if (symp
->st_shndx
!= SHN_UNDEF
)
250 * XXX DANGER WILL ROBINSON!
251 * If we have a function pointer in the executable's
252 * data section, it points to the executable's PLT
253 * slot, and there is NO relocation emitted. To make
254 * the function pointer comparable to function pointers
255 * in shared libraries, we must resolve data references
256 * in the libraries to point to PLT slots in the
257 * executable, if they exist.
259 else if (!in_plt
&& symp
->st_value
!= 0 &&
260 ELF_ST_TYPE(symp
->st_info
) == STT_FUNC
)
272 static const Obj_Entry
*_rtld_last_refobj
;
275 * Called when an object is freed. Reset the cached symbol look up if
276 * our last referencing or definition object just got unloaded.
279 _rtld_combreloc_reset(const Obj_Entry
*obj
)
281 if (_rtld_last_refobj
== obj
)
282 _rtld_last_refobj
= NULL
;
287 * Given a symbol number in a referencing object, find the corresponding
288 * definition of the symbol. Returns a pointer to the symbol, or NULL if
289 * no definition was found. Returns a pointer to the Obj_Entry of the
290 * defining object via the reference parameter DEFOBJ_OUT.
293 _rtld_find_symdef(unsigned long symnum
, const Obj_Entry
*refobj
,
294 const Obj_Entry
**defobj_out
, bool in_plt
)
298 const Obj_Entry
*defobj
;
304 * COMBRELOC combines multiple reloc sections and sorts them to make
305 * dynamic symbol lookup caching possible.
307 * So if the lookup we are doing is the same as the previous lookup
308 * return the cached results.
310 static unsigned long last_symnum
;
311 static const Obj_Entry
*last_defobj
;
312 static const Elf_Sym
*last_def
;
314 if (symnum
== last_symnum
&& refobj
== _rtld_last_refobj
315 && in_plt
== false) {
316 *defobj_out
= last_defobj
;
321 ref
= refobj
->symtab
+ symnum
;
322 name
= refobj
->strtab
+ ref
->st_name
;
325 * We don't have to do a full scale lookup if the symbol is local.
326 * We know it will bind to the instance in this load module; to
327 * which we already have a pointer (ie ref).
329 if (ELF_ST_BIND(ref
->st_info
) != STB_LOCAL
) {
330 if (ELF_ST_TYPE(ref
->st_info
) == STT_SECTION
) {
331 _rtld_error("%s: Bogus symbol table entry %lu",
332 refobj
->path
, symnum
);
335 hash
= _rtld_elf_hash(name
);
337 def
= _rtld_symlook_default(name
, hash
, refobj
, &defobj
, in_plt
);
339 rdbg(("STB_LOCAL symbol %s in %s", name
, refobj
->path
));
345 * If we found no definition and the reference is weak, treat the
346 * symbol as having the value zero.
348 if (def
== NULL
&& ELF_ST_BIND(ref
->st_info
) == STB_WEAK
) {
349 rdbg((" returning _rtld_sym_zero@_rtld_objself"));
350 def
= &_rtld_sym_zero
;
351 defobj
= &_rtld_objself
;
355 *defobj_out
= defobj
;
357 if (in_plt
== false) {
359 * Cache the lookup arguments and results if this was
362 last_symnum
= symnum
;
363 _rtld_last_refobj
= refobj
;
365 last_defobj
= defobj
;
369 rdbg(("lookup failed"));
370 _rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
371 refobj
->path
, in_plt
? "PLT " : "", name
, symnum
);
377 _rtld_find_plt_symdef(unsigned long symnum
, const Obj_Entry
*obj
,
378 const Obj_Entry
**defobj
, bool imm
)
380 const Elf_Sym
*def
= _rtld_find_symdef(symnum
, obj
, defobj
, true);
381 if (__predict_false(def
== NULL
))
384 if (__predict_false(def
== &_rtld_sym_zero
)) {
385 /* tp is set during lazy binding. */
387 const Elf_Sym
*ref
= obj
->symtab
+ symnum
;
388 const char *name
= obj
->strtab
+ ref
->st_name
;
391 "%s: Trying to call undefined weak symbol `%s'",
400 * Given a symbol name in a referencing object, find the corresponding
401 * definition of the symbol. Returns a pointer to the symbol, or NULL if
402 * no definition was found. Returns a pointer to the Obj_Entry of the
403 * defining object via the reference parameter DEFOBJ_OUT.
406 _rtld_symlook_default(const char *name
, unsigned long hash
,
407 const Obj_Entry
*refobj
, const Obj_Entry
**defobj_out
, bool in_plt
)
411 const Obj_Entry
*obj
;
412 const Obj_Entry
*defobj
;
413 const Objlist_Entry
*elm
;
418 _rtld_donelist_init(&donelist
);
420 /* Look first in the referencing object if linked symbolically. */
421 if (refobj
->symbolic
&& !_rtld_donelist_check(&donelist
, refobj
)) {
422 rdbg(("search referencing object for %s", name
));
423 symp
= _rtld_symlook_obj(name
, hash
, refobj
, in_plt
);
430 /* Search all objects loaded at program start up. */
431 if (def
== NULL
|| ELF_ST_BIND(def
->st_info
) == STB_WEAK
) {
432 rdbg(("search _rtld_list_main for %s", name
));
433 symp
= _rtld_symlook_list(name
, hash
, &_rtld_list_main
, &obj
,
436 (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
442 /* Search all RTLD_GLOBAL objects. */
443 if (def
== NULL
|| ELF_ST_BIND(def
->st_info
) == STB_WEAK
) {
444 rdbg(("search _rtld_list_global for %s", name
));
445 symp
= _rtld_symlook_list(name
, hash
, &_rtld_list_global
,
446 &obj
, in_plt
, &donelist
);
448 (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
454 /* Search all dlopened DAGs containing the referencing object. */
455 SIMPLEQ_FOREACH(elm
, &refobj
->dldags
, link
) {
456 if (def
!= NULL
&& ELF_ST_BIND(def
->st_info
) != STB_WEAK
)
458 rdbg(("search DAG with root %p (%s) for %s", elm
->obj
,
459 elm
->obj
->path
, name
));
460 symp
= _rtld_symlook_list(name
, hash
, &elm
->obj
->dagmembers
,
461 &obj
, in_plt
, &donelist
);
463 (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
470 * Search the dynamic linker itself, and possibly resolve the
471 * symbol from there. This is how the application links to
472 * dynamic linker services such as dlopen. Only the values listed
473 * in the "_rtld_exports" array can be resolved from the dynamic
476 if (def
== NULL
|| ELF_ST_BIND(def
->st_info
) == STB_WEAK
) {
477 rdbg(("Search the dynamic linker itself."));
478 symp
= _rtld_symlook_obj(name
, hash
, &_rtld_objself
, in_plt
);
479 if (symp
!= NULL
&& _rtld_is_exported(symp
)) {
481 defobj
= &_rtld_objself
;
486 *defobj_out
= defobj
;