1 /* $NetBSD: symbol.c,v 1.61 2012/08/15 03:46:07 matt 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.61 2012/08/15 03:46:07 matt Exp $");
54 #include <sys/types.h>
56 #include <sys/bitops.h>
63 * If the given object is already in the donelist, return true. Otherwise
64 * add the object to the list and return false.
67 _rtld_donelist_check(DoneList
*dlp
, const Obj_Entry
*obj
)
71 for (i
= 0; i
< dlp
->num_used
; i
++)
72 if (dlp
->objs
[i
] == obj
)
75 * Our donelist allocation may not always be sufficient as we're not
76 * thread safe. We'll handle it properly anyway.
78 if (dlp
->num_used
< dlp
->num_alloc
)
79 dlp
->objs
[dlp
->num_used
++] = obj
;
84 _rtld_is_exported(const Elf_Sym
*def
)
86 static const fptr_t _rtld_exports
[] = {
94 (fptr_t
)dl_iterate_phdr
,
96 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
97 (fptr_t
)_rtld_tls_allocate
,
98 (fptr_t
)_rtld_tls_free
,
99 (fptr_t
)__tls_get_addr
,
101 (fptr_t
)___tls_get_addr
,
109 value
= (fptr_t
)(_rtld_objself
.relocbase
+ def
->st_value
);
110 for (i
= 0; _rtld_exports
[i
] != NULL
; i
++) {
111 if (value
== _rtld_exports
[i
])
118 * Hash function for symbol table lookup. Don't even think about changing
119 * this. It is specified by the System V ABI.
122 _rtld_elf_hash(const char *name
)
124 const unsigned char *p
= (const unsigned char *) name
;
129 for (; __predict_true((c
= *p
) != '\0'); p
++) {
132 if ((g
= h
& 0xf0000000) != 0) {
141 _rtld_symlook_list(const char *name
, unsigned long hash
, const Objlist
*objlist
,
142 const Obj_Entry
**defobj_out
, u_int flags
, const Ver_Entry
*ventry
,
147 const Obj_Entry
*defobj
;
148 const Objlist_Entry
*elm
;
152 SIMPLEQ_FOREACH(elm
, objlist
, link
) {
153 if (_rtld_donelist_check(dlp
, elm
->obj
))
155 rdbg(("search object %p (%s) for %s", elm
->obj
, elm
->obj
->path
,
157 symp
= _rtld_symlook_obj(name
, hash
, elm
->obj
, flags
, ventry
);
160 (ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
163 if (ELF_ST_BIND(def
->st_info
) != STB_WEAK
)
169 *defobj_out
= defobj
;
174 * Search the symbol table of a shared object and all objects needed by it for
175 * a symbol of the given name. Search order is breadth-first. Returns a pointer
176 * to the symbol, or NULL if no definition was found.
179 _rtld_symlook_needed(const char *name
, unsigned long hash
,
180 const Needed_Entry
*needed
, const Obj_Entry
**defobj_out
, u_int flags
,
181 const Ver_Entry
*ventry
, DoneList
*breadth
, DoneList
*depth
)
183 const Elf_Sym
*def
, *def_w
;
184 const Needed_Entry
*n
;
185 const Obj_Entry
*obj
, *defobj
, *defobj1
;
189 for (n
= needed
; n
!= NULL
; n
= n
->next
) {
190 if ((obj
= n
->obj
) == NULL
)
192 if (_rtld_donelist_check(breadth
, obj
))
194 def
= _rtld_symlook_obj(name
, hash
, obj
, flags
, ventry
);
198 if (ELF_ST_BIND(def
->st_info
) != STB_WEAK
) {
199 *defobj_out
= defobj
;
205 * Either the symbol definition has not been found in directly needed
206 * objects, or the found symbol is weak.
208 for (n
= needed
; n
!= NULL
; n
= n
->next
) {
209 if ((obj
= n
->obj
) == NULL
)
211 if (_rtld_donelist_check(depth
, obj
))
213 def_w
= _rtld_symlook_needed(name
, hash
, obj
->needed
, &defobj1
,
214 flags
, ventry
, breadth
, depth
);
217 if (def
== NULL
|| ELF_ST_BIND(def_w
->st_info
) != STB_WEAK
) {
220 if (ELF_ST_BIND(def_w
->st_info
) != STB_WEAK
)
225 *defobj_out
= defobj
;
231 * Search the symbol table of a single shared object for a symbol of
232 * the given name. Returns a pointer to the symbol, or NULL if no
233 * definition was found.
235 * The symbol's hash value is passed in for efficiency reasons; that
236 * eliminates many recomputations of the hash value.
239 _rtld_symlook_obj(const char *name
, unsigned long hash
,
240 const Obj_Entry
*obj
, u_int flags
, const Ver_Entry
*ventry
)
242 unsigned long symnum
;
243 const Elf_Sym
*vsymp
= NULL
;
247 for (symnum
= obj
->buckets
[fast_remainder32(hash
, obj
->nbuckets
,
248 obj
->nbuckets_m
, obj
->nbuckets_s1
, obj
->nbuckets_s2
)];
249 symnum
!= ELF_SYM_UNDEFINED
;
250 symnum
= obj
->chains
[symnum
]) {
254 assert(symnum
< obj
->nchains
);
255 symp
= obj
->symtab
+ symnum
;
256 strp
= obj
->strtab
+ symp
->st_name
;
257 rdbg(("check \"%s\" vs \"%s\" in %s", name
, strp
, obj
->path
));
258 if (name
[1] != strp
[1] || strcmp(name
, strp
))
261 if (symp
->st_shndx
== SHN_UNDEF
)
265 * XXX DANGER WILL ROBINSON!
266 * If we have a function pointer in the executable's
267 * data section, it points to the executable's PLT
268 * slot, and there is NO relocation emitted. To make
269 * the function pointer comparable to function pointers
270 * in shared libraries, we must resolve data references
271 * in the libraries to point to PLT slots in the
272 * executable, if they exist.
274 if (symp
->st_shndx
== SHN_UNDEF
&&
275 ((flags
& SYMLOOK_IN_PLT
) ||
276 symp
->st_value
== 0 ||
277 ELF_ST_TYPE(symp
->st_info
) != STT_FUNC
))
281 if (ventry
== NULL
) {
282 if (obj
->versyms
!= NULL
) {
283 verndx
= VER_NDX(obj
->versyms
[symnum
].vs_vers
);
284 if (verndx
> obj
->vertabnum
) {
285 _rtld_error("%s: symbol %s references "
286 "wrong version %d", obj
->path
,
287 &obj
->strtab
[symnum
], verndx
);
292 * If we are not called from dlsym (i.e. this
293 * is a normal relocation from unversioned
294 * binary), accept the symbol immediately
295 * if it happens to have first version after
296 * this shared object became versioned.
297 * Otherwise, if symbol is versioned and not
298 * hidden, remember it. If it is the only
299 * symbol with this name exported by the shared
300 * object, it will be returned as a match at the
301 * end of the function. If symbol is global
302 * (verndx < 2) accept it unconditionally.
304 if (!(flags
& SYMLOOK_DLSYM
) &&
305 verndx
== VER_NDX_GIVEN
) {
307 } else if (verndx
>= VER_NDX_GIVEN
) {
308 if (!(obj
->versyms
[symnum
].vs_vers
& VER_NDX_HIDDEN
)) {
318 if (obj
->versyms
== NULL
) {
319 if (_rtld_object_match_name(obj
, ventry
->name
)){
320 _rtld_error("%s: object %s should "
321 "provide version %s for symbol %s",
322 _rtld_objself
.path
, obj
->path
,
323 ventry
->name
, &obj
->strtab
[symnum
]);
327 verndx
= VER_NDX(obj
->versyms
[symnum
].vs_vers
);
328 if (verndx
> obj
->vertabnum
) {
329 _rtld_error("%s: symbol %s references "
330 "wrong version %d", obj
->path
,
331 &obj
->strtab
[symnum
], verndx
);
334 if (obj
->vertab
[verndx
].hash
!= ventry
->hash
||
335 strcmp(obj
->vertab
[verndx
].name
, ventry
->name
)) {
337 * Version does not match. Look if this
338 * is a global symbol and if it is not
339 * hidden. If global symbol (verndx < 2)
340 * is available, use it. Do not return
341 * symbol if we are called by dlvsym,
342 * because dlvsym looks for a specific
343 * version and default one is not what
346 if ((flags
& SYMLOOK_DLSYM
) ||
347 (obj
->versyms
[symnum
].vs_vers
& VER_NDX_HIDDEN
) ||
348 (verndx
>= VER_NDX_GIVEN
))
361 static const Obj_Entry
*_rtld_last_refobj
;
364 * Called when an object is freed. Reset the cached symbol look up if
365 * our last referencing or definition object just got unloaded.
368 _rtld_combreloc_reset(const Obj_Entry
*obj
)
370 if (_rtld_last_refobj
== obj
)
371 _rtld_last_refobj
= NULL
;
376 * Given a symbol number in a referencing object, find the corresponding
377 * definition of the symbol. Returns a pointer to the symbol, or NULL if
378 * no definition was found. Returns a pointer to the Obj_Entry of the
379 * defining object via the reference parameter DEFOBJ_OUT.
382 _rtld_find_symdef(unsigned long symnum
, const Obj_Entry
*refobj
,
383 const Obj_Entry
**defobj_out
, u_int flags
)
387 const Obj_Entry
*defobj
;
393 * COMBRELOC combines multiple reloc sections and sorts them to make
394 * dynamic symbol lookup caching possible.
396 * So if the lookup we are doing is the same as the previous lookup
397 * return the cached results.
399 static unsigned long last_symnum
;
400 static const Obj_Entry
*last_defobj
;
401 static const Elf_Sym
*last_def
;
403 if (symnum
== last_symnum
&& refobj
== _rtld_last_refobj
404 && !(flags
& SYMLOOK_IN_PLT
)) {
405 *defobj_out
= last_defobj
;
410 ref
= refobj
->symtab
+ symnum
;
411 name
= refobj
->strtab
+ ref
->st_name
;
414 * We don't have to do a full scale lookup if the symbol is local.
415 * We know it will bind to the instance in this load module; to
416 * which we already have a pointer (ie ref).
418 if (ELF_ST_BIND(ref
->st_info
) != STB_LOCAL
) {
419 if (ELF_ST_TYPE(ref
->st_info
) == STT_SECTION
) {
420 _rtld_error("%s: Bogus symbol table entry %lu",
421 refobj
->path
, symnum
);
424 hash
= _rtld_elf_hash(name
);
426 def
= _rtld_symlook_default(name
, hash
, refobj
, &defobj
, flags
,
427 _rtld_fetch_ventry(refobj
, symnum
));
429 rdbg(("STB_LOCAL symbol %s in %s", name
, refobj
->path
));
435 * If we found no definition and the reference is weak, treat the
436 * symbol as having the value zero.
438 if (def
== NULL
&& ELF_ST_BIND(ref
->st_info
) == STB_WEAK
) {
439 rdbg((" returning _rtld_sym_zero@_rtld_objself"));
440 def
= &_rtld_sym_zero
;
441 defobj
= &_rtld_objself
;
445 *defobj_out
= defobj
;
447 if (!(flags
& SYMLOOK_IN_PLT
)) {
449 * Cache the lookup arguments and results if this was
452 last_symnum
= symnum
;
453 _rtld_last_refobj
= refobj
;
455 last_defobj
= defobj
;
459 rdbg(("lookup failed"));
460 _rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
461 refobj
->path
, (flags
& SYMLOOK_IN_PLT
) ? "PLT " : "",
468 _rtld_find_plt_symdef(unsigned long symnum
, const Obj_Entry
*obj
,
469 const Obj_Entry
**defobj
, bool imm
)
471 const Elf_Sym
*def
= _rtld_find_symdef(symnum
, obj
, defobj
,
473 if (__predict_false(def
== NULL
))
476 if (__predict_false(def
== &_rtld_sym_zero
)) {
477 /* tp is set during lazy binding. */
479 const Elf_Sym
*ref
= obj
->symtab
+ symnum
;
480 const char *name
= obj
->strtab
+ ref
->st_name
;
483 "%s: Trying to call undefined weak symbol `%s'",
492 * Given a symbol name in a referencing object, find the corresponding
493 * definition of the symbol. Returns a pointer to the symbol, or NULL if
494 * no definition was found. Returns a pointer to the Obj_Entry of the
495 * defining object via the reference parameter DEFOBJ_OUT.
498 _rtld_symlook_default(const char *name
, unsigned long hash
,
499 const Obj_Entry
*refobj
, const Obj_Entry
**defobj_out
, u_int flags
,
500 const Ver_Entry
*ventry
)
504 const Obj_Entry
*obj
;
505 const Obj_Entry
*defobj
;
506 const Objlist_Entry
*elm
;
511 _rtld_donelist_init(&donelist
);
513 /* Look first in the referencing object if linked symbolically. */
514 if (refobj
->symbolic
&& !_rtld_donelist_check(&donelist
, refobj
)) {
515 rdbg(("search referencing object for %s", name
));
516 symp
= _rtld_symlook_obj(name
, hash
, refobj
, flags
, ventry
);
523 /* Search all objects loaded at program start up. */
524 if (def
== NULL
|| ELF_ST_BIND(def
->st_info
) == STB_WEAK
) {
525 rdbg(("search _rtld_list_main for %s", name
));
526 symp
= _rtld_symlook_list(name
, hash
, &_rtld_list_main
, &obj
,
527 flags
, ventry
, &donelist
);
529 (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
535 /* Search all RTLD_GLOBAL objects. */
536 if (def
== NULL
|| ELF_ST_BIND(def
->st_info
) == STB_WEAK
) {
537 rdbg(("search _rtld_list_global for %s", name
));
538 symp
= _rtld_symlook_list(name
, hash
, &_rtld_list_global
,
539 &obj
, flags
, ventry
, &donelist
);
541 (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
547 /* Search all dlopened DAGs containing the referencing object. */
548 SIMPLEQ_FOREACH(elm
, &refobj
->dldags
, link
) {
549 if (def
!= NULL
&& ELF_ST_BIND(def
->st_info
) != STB_WEAK
)
551 rdbg(("search DAG with root %p (%s) for %s", elm
->obj
,
552 elm
->obj
->path
, name
));
553 symp
= _rtld_symlook_list(name
, hash
, &elm
->obj
->dagmembers
,
554 &obj
, flags
, ventry
, &donelist
);
556 (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
563 * Search the dynamic linker itself, and possibly resolve the
564 * symbol from there. This is how the application links to
565 * dynamic linker services such as dlopen. Only the values listed
566 * in the "_rtld_exports" array can be resolved from the dynamic
569 if (def
== NULL
|| ELF_ST_BIND(def
->st_info
) == STB_WEAK
) {
570 rdbg(("Search the dynamic linker itself."));
571 symp
= _rtld_symlook_obj(name
, hash
, &_rtld_objself
, flags
,
573 if (symp
!= NULL
&& _rtld_is_exported(symp
)) {
575 defobj
= &_rtld_objself
;
580 *defobj_out
= defobj
;