1 /* $NetBSD: symbol.c,v 1.47 2008/10/04 09:37:12 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.47 2008/10/04 09:37:12 skrll Exp $");
54 #include <sys/types.h>
61 typedef void (*fptr_t
)(void);
64 _rtld_is_exported(const Elf_Sym
*def
)
66 static const fptr_t _rtld_exports
[] = {
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
])
87 * Hash function for symbol table lookup. Don't even think about changing
88 * this. It is specified by the System V ABI.
91 _rtld_elf_hash(const char *name
)
93 const unsigned char *p
= (const unsigned char *) name
;
98 for (; __predict_true((c
= *p
) != '\0'); p
++) {
101 if ((g
= h
& 0xf0000000) != 0) {
110 _rtld_symlook_list(const char *name
, unsigned long hash
, const Objlist
*objlist
,
111 const Obj_Entry
**defobj_out
, bool in_plt
)
115 const Obj_Entry
*defobj
;
116 const Objlist_Entry
*elm
;
120 SIMPLEQ_FOREACH(elm
, objlist
, link
) {
121 rdbg(("search object %p (%s) for %s", elm
->obj
, elm
->obj
->path
,
123 if ((symp
= _rtld_symlook_obj(name
, hash
, elm
->obj
, in_plt
))
126 (ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
129 if (ELF_ST_BIND(def
->st_info
) != STB_WEAK
)
135 *defobj_out
= defobj
;
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.
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
;
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
)
159 if (ELF_ST_BIND(def
->st_info
) != STB_WEAK
) {
160 *defobj_out
= defobj
;
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
)
172 def_w
= _rtld_symlook_needed(name
, hash
, obj
->needed
, &defobj1
,
176 if (def
== NULL
|| ELF_ST_BIND(def_w
->st_info
) != STB_WEAK
) {
179 if (ELF_ST_BIND(def_w
->st_info
) != STB_WEAK
)
184 *defobj_out
= defobj
;
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.
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
]) {
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
)
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
)
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.
246 _rtld_find_symdef(unsigned long symnum
, const Obj_Entry
*refobj
,
247 const Obj_Entry
**defobj_out
, bool in_plt
)
251 const Obj_Entry
*defobj
;
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
;
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
);
291 def
= _rtld_symlook_default(name
, hash
, refobj
, &defobj
, in_plt
);
293 rdbg(("STB_LOCAL symbol %s in %s", name
, refobj
->path
));
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
) {
305 "%s: Trying to call undefined weak symbol `%s'",
308 rdbg((" returning _rtld_sym_zero@_rtld_objself"));
309 def
= &_rtld_sym_zero
;
310 defobj
= &_rtld_objself
;
314 *defobj_out
= defobj
;
316 if (in_plt
== false) {
318 * Cache the lookup arguments and results if this was
321 last_symnum
= symnum
;
322 last_refobj
= refobj
;
324 last_defobj
= defobj
;
328 rdbg(("lookup failed"));
329 _rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
330 refobj
->path
, in_plt
? "PLT " : "", name
, symnum
);
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.
342 _rtld_symlook_default(const char *name
, unsigned long hash
,
343 const Obj_Entry
*refobj
, const Obj_Entry
**defobj_out
, bool in_plt
)
347 const Obj_Entry
*obj
;
348 const Obj_Entry
*defobj
;
349 const Objlist_Entry
*elm
;
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
);
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
,
369 (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
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
,
381 (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
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
)
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
,
396 (def
== NULL
|| ELF_ST_BIND(symp
->st_info
) != STB_WEAK
)) {
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
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
)) {
414 defobj
= &_rtld_objself
;
419 *defobj_out
= defobj
;