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