symbol.c revision 1.58.2.2 1 1.58.2.2 yamt /* $NetBSD: symbol.c,v 1.58.2.2 2012/10/30 18:59:23 yamt 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.58.2.2 yamt __RCSID("$NetBSD: symbol.c,v 1.58.2.2 2012/10/30 18:59:23 yamt 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.53 joerg #include <sys/bitops.h>
57 1.1 cgd #include <dirent.h>
58 1.1 cgd
59 1.1 cgd #include "debug.h"
60 1.1 cgd #include "rtld.h"
61 1.1 cgd
62 1.51 roy /*
63 1.51 roy * If the given object is already in the donelist, return true. Otherwise
64 1.51 roy * add the object to the list and return false.
65 1.51 roy */
66 1.51 roy static bool
67 1.51 roy _rtld_donelist_check(DoneList *dlp, const Obj_Entry *obj)
68 1.51 roy {
69 1.51 roy unsigned int i;
70 1.51 roy
71 1.51 roy for (i = 0; i < dlp->num_used; i++)
72 1.51 roy if (dlp->objs[i] == obj)
73 1.51 roy return true;
74 1.51 roy /*
75 1.51 roy * Our donelist allocation may not always be sufficient as we're not
76 1.51 roy * thread safe. We'll handle it properly anyway.
77 1.51 roy */
78 1.51 roy if (dlp->num_used < dlp->num_alloc)
79 1.51 roy dlp->objs[dlp->num_used++] = obj;
80 1.51 roy return false;
81 1.51 roy }
82 1.51 roy
83 1.33 skrll static bool
84 1.33 skrll _rtld_is_exported(const Elf_Sym *def)
85 1.33 skrll {
86 1.44 yamt static const fptr_t _rtld_exports[] = {
87 1.39 chs (fptr_t)dlopen,
88 1.39 chs (fptr_t)dlclose,
89 1.39 chs (fptr_t)dlsym,
90 1.57 nonaka (fptr_t)dlvsym,
91 1.39 chs (fptr_t)dlerror,
92 1.39 chs (fptr_t)dladdr,
93 1.48 pooka (fptr_t)dlinfo,
94 1.54 skrll (fptr_t)dl_iterate_phdr,
95 1.58.2.1 yamt (fptr_t)_dlauxinfo,
96 1.55 joerg #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
97 1.55 joerg (fptr_t)_rtld_tls_allocate,
98 1.55 joerg (fptr_t)_rtld_tls_free,
99 1.55 joerg (fptr_t)__tls_get_addr,
100 1.56 joerg #ifdef __i386__
101 1.56 joerg (fptr_t)___tls_get_addr,
102 1.56 joerg #endif
103 1.55 joerg #endif
104 1.39 chs NULL
105 1.33 skrll };
106 1.33 skrll int i;
107 1.39 chs fptr_t value;
108 1.33 skrll
109 1.39 chs value = (fptr_t)(_rtld_objself.relocbase + def->st_value);
110 1.39 chs for (i = 0; _rtld_exports[i] != NULL; i++) {
111 1.33 skrll if (value == _rtld_exports[i])
112 1.33 skrll return true;
113 1.33 skrll }
114 1.33 skrll return false;
115 1.33 skrll }
116 1.33 skrll
117 1.1 cgd /*
118 1.1 cgd * Hash function for symbol table lookup. Don't even think about changing
119 1.1 cgd * this. It is specified by the System V ABI.
120 1.1 cgd */
121 1.1 cgd unsigned long
122 1.31 skrll _rtld_elf_hash(const char *name)
123 1.1 cgd {
124 1.3 christos const unsigned char *p = (const unsigned char *) name;
125 1.3 christos unsigned long h = 0;
126 1.3 christos unsigned long g;
127 1.24 mycroft unsigned long c;
128 1.3 christos
129 1.24 mycroft for (; __predict_true((c = *p) != '\0'); p++) {
130 1.24 mycroft h <<= 4;
131 1.24 mycroft h += c;
132 1.24 mycroft if ((g = h & 0xf0000000) != 0) {
133 1.24 mycroft h ^= g;
134 1.3 christos h ^= g >> 24;
135 1.24 mycroft }
136 1.3 christos }
137 1.24 mycroft return (h);
138 1.1 cgd }
139 1.1 cgd
140 1.5 mycroft const Elf_Sym *
141 1.23 mycroft _rtld_symlook_list(const char *name, unsigned long hash, const Objlist *objlist,
142 1.57 nonaka const Obj_Entry **defobj_out, u_int flags, const Ver_Entry *ventry,
143 1.57 nonaka DoneList *dlp)
144 1.5 mycroft {
145 1.5 mycroft const Elf_Sym *symp;
146 1.5 mycroft const Elf_Sym *def;
147 1.27 mycroft const Obj_Entry *defobj;
148 1.5 mycroft const Objlist_Entry *elm;
149 1.5 mycroft
150 1.5 mycroft def = NULL;
151 1.27 mycroft defobj = NULL;
152 1.12 lukem SIMPLEQ_FOREACH(elm, objlist, link) {
153 1.51 roy if (_rtld_donelist_check(dlp, elm->obj))
154 1.51 roy continue;
155 1.43 christos rdbg(("search object %p (%s) for %s", elm->obj, elm->obj->path,
156 1.43 christos name));
157 1.57 nonaka symp = _rtld_symlook_obj(name, hash, elm->obj, flags, ventry);
158 1.57 nonaka if (symp != NULL) {
159 1.5 mycroft if ((def == NULL) ||
160 1.6 thorpej (ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
161 1.5 mycroft def = symp;
162 1.27 mycroft defobj = elm->obj;
163 1.6 thorpej if (ELF_ST_BIND(def->st_info) != STB_WEAK)
164 1.5 mycroft break;
165 1.5 mycroft }
166 1.5 mycroft }
167 1.5 mycroft }
168 1.27 mycroft if (def != NULL)
169 1.27 mycroft *defobj_out = defobj;
170 1.5 mycroft return def;
171 1.5 mycroft }
172 1.5 mycroft
173 1.1 cgd /*
174 1.47 skrll * Search the symbol table of a shared object and all objects needed by it for
175 1.47 skrll * a symbol of the given name. Search order is breadth-first. Returns a pointer
176 1.47 skrll * to the symbol, or NULL if no definition was found.
177 1.47 skrll */
178 1.47 skrll const Elf_Sym *
179 1.47 skrll _rtld_symlook_needed(const char *name, unsigned long hash,
180 1.57 nonaka const Needed_Entry *needed, const Obj_Entry **defobj_out, u_int flags,
181 1.57 nonaka const Ver_Entry *ventry, DoneList *breadth, DoneList *depth)
182 1.47 skrll {
183 1.47 skrll const Elf_Sym *def, *def_w;
184 1.47 skrll const Needed_Entry *n;
185 1.47 skrll const Obj_Entry *obj, *defobj, *defobj1;
186 1.47 skrll
187 1.47 skrll def = def_w = NULL;
188 1.47 skrll defobj = NULL;
189 1.47 skrll for (n = needed; n != NULL; n = n->next) {
190 1.51 roy if ((obj = n->obj) == NULL)
191 1.51 roy continue;
192 1.51 roy if (_rtld_donelist_check(breadth, obj))
193 1.51 roy continue;
194 1.57 nonaka def = _rtld_symlook_obj(name, hash, obj, flags, ventry);
195 1.57 nonaka if (def == NULL)
196 1.47 skrll continue;
197 1.47 skrll defobj = obj;
198 1.47 skrll if (ELF_ST_BIND(def->st_info) != STB_WEAK) {
199 1.47 skrll *defobj_out = defobj;
200 1.47 skrll
201 1.47 skrll return (def);
202 1.47 skrll }
203 1.47 skrll }
204 1.47 skrll /*
205 1.47 skrll * Either the symbol definition has not been found in directly needed
206 1.47 skrll * objects, or the found symbol is weak.
207 1.47 skrll */
208 1.47 skrll for (n = needed; n != NULL; n = n->next) {
209 1.47 skrll if ((obj = n->obj) == NULL)
210 1.47 skrll continue;
211 1.51 roy if (_rtld_donelist_check(depth, obj))
212 1.51 roy continue;
213 1.47 skrll def_w = _rtld_symlook_needed(name, hash, obj->needed, &defobj1,
214 1.57 nonaka flags, ventry, breadth, depth);
215 1.47 skrll if (def_w == NULL)
216 1.47 skrll continue;
217 1.47 skrll if (def == NULL || ELF_ST_BIND(def_w->st_info) != STB_WEAK) {
218 1.47 skrll def = def_w;
219 1.47 skrll defobj = defobj1;
220 1.47 skrll if (ELF_ST_BIND(def_w->st_info) != STB_WEAK)
221 1.47 skrll break;
222 1.47 skrll }
223 1.47 skrll }
224 1.47 skrll if (def != NULL)
225 1.47 skrll *defobj_out = defobj;
226 1.47 skrll
227 1.47 skrll return def;
228 1.47 skrll }
229 1.47 skrll
230 1.47 skrll /*
231 1.1 cgd * Search the symbol table of a single shared object for a symbol of
232 1.1 cgd * the given name. Returns a pointer to the symbol, or NULL if no
233 1.1 cgd * definition was found.
234 1.1 cgd *
235 1.1 cgd * The symbol's hash value is passed in for efficiency reasons; that
236 1.1 cgd * eliminates many recomputations of the hash value.
237 1.1 cgd */
238 1.1 cgd const Elf_Sym *
239 1.31 skrll _rtld_symlook_obj(const char *name, unsigned long hash,
240 1.57 nonaka const Obj_Entry *obj, u_int flags, const Ver_Entry *ventry)
241 1.1 cgd {
242 1.20 mycroft unsigned long symnum;
243 1.57 nonaka const Elf_Sym *vsymp = NULL;
244 1.57 nonaka Elf_Half verndx;
245 1.57 nonaka int vcount = 0;
246 1.1 cgd
247 1.53 joerg for (symnum = obj->buckets[fast_remainder32(hash, obj->nbuckets,
248 1.53 joerg obj->nbuckets_m, obj->nbuckets_s1, obj->nbuckets_s2)];
249 1.20 mycroft symnum != ELF_SYM_UNDEFINED;
250 1.20 mycroft symnum = obj->chains[symnum]) {
251 1.3 christos const Elf_Sym *symp;
252 1.3 christos const char *strp;
253 1.3 christos
254 1.3 christos assert(symnum < obj->nchains);
255 1.3 christos symp = obj->symtab + symnum;
256 1.3 christos strp = obj->strtab + symp->st_name;
257 1.58 christos rdbg(("check \"%s\" vs \"%s\" in %s", name, strp, obj->path));
258 1.57 nonaka if (name[1] != strp[1] || strcmp(name, strp))
259 1.57 nonaka continue;
260 1.58.2.1 yamt #ifdef __mips__
261 1.58.2.1 yamt if (symp->st_shndx == SHN_UNDEF)
262 1.58.2.1 yamt continue;
263 1.58.2.1 yamt #else
264 1.57 nonaka /*
265 1.57 nonaka * XXX DANGER WILL ROBINSON!
266 1.57 nonaka * If we have a function pointer in the executable's
267 1.57 nonaka * data section, it points to the executable's PLT
268 1.57 nonaka * slot, and there is NO relocation emitted. To make
269 1.57 nonaka * the function pointer comparable to function pointers
270 1.57 nonaka * in shared libraries, we must resolve data references
271 1.57 nonaka * in the libraries to point to PLT slots in the
272 1.57 nonaka * executable, if they exist.
273 1.57 nonaka */
274 1.58.2.1 yamt if (symp->st_shndx == SHN_UNDEF &&
275 1.58.2.1 yamt ((flags & SYMLOOK_IN_PLT) ||
276 1.58.2.1 yamt symp->st_value == 0 ||
277 1.58.2.1 yamt ELF_ST_TYPE(symp->st_info) != STT_FUNC))
278 1.57 nonaka continue;
279 1.58.2.1 yamt #endif
280 1.57 nonaka
281 1.57 nonaka if (ventry == NULL) {
282 1.57 nonaka if (obj->versyms != NULL) {
283 1.57 nonaka verndx = VER_NDX(obj->versyms[symnum].vs_vers);
284 1.57 nonaka if (verndx > obj->vertabnum) {
285 1.57 nonaka _rtld_error("%s: symbol %s references "
286 1.57 nonaka "wrong version %d", obj->path,
287 1.57 nonaka &obj->strtab[symnum], verndx);
288 1.57 nonaka continue;
289 1.57 nonaka }
290 1.57 nonaka
291 1.57 nonaka /*
292 1.57 nonaka * If we are not called from dlsym (i.e. this
293 1.57 nonaka * is a normal relocation from unversioned
294 1.57 nonaka * binary), accept the symbol immediately
295 1.57 nonaka * if it happens to have first version after
296 1.57 nonaka * this shared object became versioned.
297 1.57 nonaka * Otherwise, if symbol is versioned and not
298 1.57 nonaka * hidden, remember it. If it is the only
299 1.57 nonaka * symbol with this name exported by the shared
300 1.57 nonaka * object, it will be returned as a match at the
301 1.57 nonaka * end of the function. If symbol is global
302 1.57 nonaka * (verndx < 2) accept it unconditionally.
303 1.57 nonaka */
304 1.57 nonaka if (!(flags & SYMLOOK_DLSYM) &&
305 1.57 nonaka verndx == VER_NDX_GIVEN) {
306 1.57 nonaka return symp;
307 1.57 nonaka } else if (verndx >= VER_NDX_GIVEN) {
308 1.57 nonaka if (!(obj->versyms[symnum].vs_vers & VER_NDX_HIDDEN)) {
309 1.57 nonaka if (vsymp == NULL)
310 1.57 nonaka vsymp = symp;
311 1.57 nonaka vcount++;
312 1.57 nonaka }
313 1.57 nonaka continue;
314 1.57 nonaka }
315 1.57 nonaka }
316 1.57 nonaka return symp;
317 1.57 nonaka } else {
318 1.57 nonaka if (obj->versyms == NULL) {
319 1.57 nonaka if (_rtld_object_match_name(obj, ventry->name)){
320 1.57 nonaka _rtld_error("%s: object %s should "
321 1.57 nonaka "provide version %s for symbol %s",
322 1.57 nonaka _rtld_objself.path, obj->path,
323 1.57 nonaka ventry->name, &obj->strtab[symnum]);
324 1.57 nonaka continue;
325 1.57 nonaka }
326 1.57 nonaka } else {
327 1.57 nonaka verndx = VER_NDX(obj->versyms[symnum].vs_vers);
328 1.57 nonaka if (verndx > obj->vertabnum) {
329 1.57 nonaka _rtld_error("%s: symbol %s references "
330 1.57 nonaka "wrong version %d", obj->path,
331 1.57 nonaka &obj->strtab[symnum], verndx);
332 1.57 nonaka continue;
333 1.57 nonaka }
334 1.57 nonaka if (obj->vertab[verndx].hash != ventry->hash ||
335 1.57 nonaka strcmp(obj->vertab[verndx].name, ventry->name)) {
336 1.57 nonaka /*
337 1.57 nonaka * Version does not match. Look if this
338 1.57 nonaka * is a global symbol and if it is not
339 1.57 nonaka * hidden. If global symbol (verndx < 2)
340 1.57 nonaka * is available, use it. Do not return
341 1.57 nonaka * symbol if we are called by dlvsym,
342 1.57 nonaka * because dlvsym looks for a specific
343 1.57 nonaka * version and default one is not what
344 1.57 nonaka * dlvsym wants.
345 1.57 nonaka */
346 1.57 nonaka if ((flags & SYMLOOK_DLSYM) ||
347 1.57 nonaka (obj->versyms[symnum].vs_vers & VER_NDX_HIDDEN) ||
348 1.57 nonaka (verndx >= VER_NDX_GIVEN))
349 1.57 nonaka continue;
350 1.57 nonaka }
351 1.57 nonaka }
352 1.57 nonaka return symp;
353 1.3 christos }
354 1.1 cgd }
355 1.57 nonaka if (vcount == 1)
356 1.57 nonaka return vsymp;
357 1.3 christos return NULL;
358 1.1 cgd }
359 1.1 cgd
360 1.49 skrll #ifdef COMBRELOC
361 1.49 skrll static const Obj_Entry *_rtld_last_refobj;
362 1.49 skrll
363 1.49 skrll /*
364 1.49 skrll * Called when an object is freed. Reset the cached symbol look up if
365 1.49 skrll * our last referencing or definition object just got unloaded.
366 1.49 skrll */
367 1.49 skrll void
368 1.49 skrll _rtld_combreloc_reset(const Obj_Entry *obj)
369 1.49 skrll {
370 1.49 skrll if (_rtld_last_refobj == obj)
371 1.49 skrll _rtld_last_refobj = NULL;
372 1.49 skrll }
373 1.49 skrll #endif
374 1.49 skrll
375 1.1 cgd /*
376 1.1 cgd * Given a symbol number in a referencing object, find the corresponding
377 1.1 cgd * definition of the symbol. Returns a pointer to the symbol, or NULL if
378 1.1 cgd * no definition was found. Returns a pointer to the Obj_Entry of the
379 1.1 cgd * defining object via the reference parameter DEFOBJ_OUT.
380 1.1 cgd */
381 1.1 cgd const Elf_Sym *
382 1.31 skrll _rtld_find_symdef(unsigned long symnum, const Obj_Entry *refobj,
383 1.57 nonaka const Obj_Entry **defobj_out, u_int flags)
384 1.1 cgd {
385 1.27 mycroft const Elf_Sym *ref;
386 1.5 mycroft const Elf_Sym *def;
387 1.27 mycroft const Obj_Entry *defobj;
388 1.27 mycroft const char *name;
389 1.3 christos unsigned long hash;
390 1.3 christos
391 1.41 matt #ifdef COMBRELOC
392 1.41 matt /*
393 1.41 matt * COMBRELOC combines multiple reloc sections and sorts them to make
394 1.41 matt * dynamic symbol lookup caching possible.
395 1.41 matt *
396 1.41 matt * So if the lookup we are doing is the same as the previous lookup
397 1.41 matt * return the cached results.
398 1.41 matt */
399 1.41 matt static unsigned long last_symnum;
400 1.49 skrll static const Obj_Entry *last_defobj;
401 1.41 matt static const Elf_Sym *last_def;
402 1.41 matt
403 1.49 skrll if (symnum == last_symnum && refobj == _rtld_last_refobj
404 1.57 nonaka && !(flags & SYMLOOK_IN_PLT)) {
405 1.41 matt *defobj_out = last_defobj;
406 1.41 matt return last_def;
407 1.41 matt }
408 1.41 matt #endif
409 1.41 matt
410 1.27 mycroft ref = refobj->symtab + symnum;
411 1.27 mycroft name = refobj->strtab + ref->st_name;
412 1.27 mycroft
413 1.27 mycroft /*
414 1.40 skrll * We don't have to do a full scale lookup if the symbol is local.
415 1.40 skrll * We know it will bind to the instance in this load module; to
416 1.40 skrll * which we already have a pointer (ie ref).
417 1.33 skrll */
418 1.40 skrll if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
419 1.40 skrll if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
420 1.40 skrll _rtld_error("%s: Bogus symbol table entry %lu",
421 1.40 skrll refobj->path, symnum);
422 1.40 skrll }
423 1.40 skrll
424 1.40 skrll hash = _rtld_elf_hash(name);
425 1.40 skrll defobj = NULL;
426 1.57 nonaka def = _rtld_symlook_default(name, hash, refobj, &defobj, flags,
427 1.57 nonaka _rtld_fetch_ventry(refobj, symnum));
428 1.40 skrll } else {
429 1.40 skrll rdbg(("STB_LOCAL symbol %s in %s", name, refobj->path));
430 1.40 skrll def = ref;
431 1.40 skrll defobj = refobj;
432 1.33 skrll }
433 1.40 skrll
434 1.33 skrll /*
435 1.27 mycroft * If we found no definition and the reference is weak, treat the
436 1.27 mycroft * symbol as having the value zero.
437 1.27 mycroft */
438 1.27 mycroft if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
439 1.45 christos rdbg((" returning _rtld_sym_zero@_rtld_objself"));
440 1.27 mycroft def = &_rtld_sym_zero;
441 1.45 christos defobj = &_rtld_objself;
442 1.27 mycroft }
443 1.33 skrll
444 1.41 matt if (def != NULL) {
445 1.27 mycroft *defobj_out = defobj;
446 1.41 matt #ifdef COMBRELOC
447 1.57 nonaka if (!(flags & SYMLOOK_IN_PLT)) {
448 1.42 matt /*
449 1.42 matt * Cache the lookup arguments and results if this was
450 1.42 matt * non-PLT lookup.
451 1.42 matt */
452 1.42 matt last_symnum = symnum;
453 1.49 skrll _rtld_last_refobj = refobj;
454 1.42 matt last_def = def;
455 1.42 matt last_defobj = defobj;
456 1.42 matt }
457 1.41 matt #endif
458 1.41 matt } else {
459 1.27 mycroft rdbg(("lookup failed"));
460 1.27 mycroft _rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
461 1.57 nonaka refobj->path, (flags & SYMLOOK_IN_PLT) ? "PLT " : "",
462 1.57 nonaka name, symnum);
463 1.27 mycroft }
464 1.26 mycroft return def;
465 1.28 christos }
466 1.28 christos
467 1.50 christos const Elf_Sym *
468 1.50 christos _rtld_find_plt_symdef(unsigned long symnum, const Obj_Entry *obj,
469 1.50 christos const Obj_Entry **defobj, bool imm)
470 1.50 christos {
471 1.57 nonaka const Elf_Sym *def = _rtld_find_symdef(symnum, obj, defobj,
472 1.57 nonaka SYMLOOK_IN_PLT);
473 1.50 christos if (__predict_false(def == NULL))
474 1.50 christos return NULL;
475 1.50 christos
476 1.50 christos if (__predict_false(def == &_rtld_sym_zero)) {
477 1.50 christos /* tp is set during lazy binding. */
478 1.50 christos if (imm) {
479 1.50 christos const Elf_Sym *ref = obj->symtab + symnum;
480 1.50 christos const char *name = obj->strtab + ref->st_name;
481 1.50 christos
482 1.50 christos _rtld_error(
483 1.50 christos "%s: Trying to call undefined weak symbol `%s'",
484 1.50 christos obj->path, name);
485 1.50 christos return NULL;
486 1.50 christos }
487 1.50 christos }
488 1.50 christos return def;
489 1.50 christos }
490 1.50 christos
491 1.28 christos /*
492 1.28 christos * Given a symbol name in a referencing object, find the corresponding
493 1.28 christos * definition of the symbol. Returns a pointer to the symbol, or NULL if
494 1.28 christos * no definition was found. Returns a pointer to the Obj_Entry of the
495 1.28 christos * defining object via the reference parameter DEFOBJ_OUT.
496 1.28 christos */
497 1.28 christos const Elf_Sym *
498 1.28 christos _rtld_symlook_default(const char *name, unsigned long hash,
499 1.57 nonaka const Obj_Entry *refobj, const Obj_Entry **defobj_out, u_int flags,
500 1.57 nonaka const Ver_Entry *ventry)
501 1.28 christos {
502 1.29 skrll const Elf_Sym *def;
503 1.29 skrll const Elf_Sym *symp;
504 1.29 skrll const Obj_Entry *obj;
505 1.29 skrll const Obj_Entry *defobj;
506 1.29 skrll const Objlist_Entry *elm;
507 1.29 skrll def = NULL;
508 1.29 skrll defobj = NULL;
509 1.51 roy DoneList donelist;
510 1.51 roy
511 1.51 roy _rtld_donelist_init(&donelist);
512 1.29 skrll
513 1.29 skrll /* Look first in the referencing object if linked symbolically. */
514 1.51 roy if (refobj->symbolic && !_rtld_donelist_check(&donelist, refobj)) {
515 1.43 christos rdbg(("search referencing object for %s", name));
516 1.57 nonaka symp = _rtld_symlook_obj(name, hash, refobj, flags, ventry);
517 1.29 skrll if (symp != NULL) {
518 1.29 skrll def = symp;
519 1.29 skrll defobj = refobj;
520 1.29 skrll }
521 1.29 skrll }
522 1.29 skrll
523 1.29 skrll /* Search all objects loaded at program start up. */
524 1.29 skrll if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
525 1.43 christos rdbg(("search _rtld_list_main for %s", name));
526 1.40 skrll symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj,
527 1.57 nonaka flags, ventry, &donelist);
528 1.29 skrll if (symp != NULL &&
529 1.40 skrll (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
530 1.29 skrll def = symp;
531 1.29 skrll defobj = obj;
532 1.29 skrll }
533 1.29 skrll }
534 1.29 skrll
535 1.40 skrll /* Search all RTLD_GLOBAL objects. */
536 1.40 skrll if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
537 1.43 christos rdbg(("search _rtld_list_global for %s", name));
538 1.40 skrll symp = _rtld_symlook_list(name, hash, &_rtld_list_global,
539 1.57 nonaka &obj, flags, ventry, &donelist);
540 1.29 skrll if (symp != NULL &&
541 1.29 skrll (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
542 1.29 skrll def = symp;
543 1.29 skrll defobj = obj;
544 1.29 skrll }
545 1.29 skrll }
546 1.40 skrll
547 1.40 skrll /* Search all dlopened DAGs containing the referencing object. */
548 1.40 skrll SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
549 1.29 skrll if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
550 1.29 skrll break;
551 1.43 christos rdbg(("search DAG with root %p (%s) for %s", elm->obj,
552 1.43 christos elm->obj->path, name));
553 1.40 skrll symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers,
554 1.57 nonaka &obj, flags, ventry, &donelist);
555 1.29 skrll if (symp != NULL &&
556 1.29 skrll (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
557 1.29 skrll def = symp;
558 1.29 skrll defobj = obj;
559 1.29 skrll }
560 1.28 christos }
561 1.28 christos
562 1.29 skrll /*
563 1.29 skrll * Search the dynamic linker itself, and possibly resolve the
564 1.29 skrll * symbol from there. This is how the application links to
565 1.29 skrll * dynamic linker services such as dlopen. Only the values listed
566 1.40 skrll * in the "_rtld_exports" array can be resolved from the dynamic
567 1.40 skrll * linker.
568 1.29 skrll */
569 1.29 skrll if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
570 1.40 skrll rdbg(("Search the dynamic linker itself."));
571 1.57 nonaka symp = _rtld_symlook_obj(name, hash, &_rtld_objself, flags,
572 1.57 nonaka ventry);
573 1.40 skrll if (symp != NULL && _rtld_is_exported(symp)) {
574 1.29 skrll def = symp;
575 1.30 skrll defobj = &_rtld_objself;
576 1.29 skrll }
577 1.28 christos }
578 1.28 christos
579 1.29 skrll if (def != NULL)
580 1.29 skrll *defobj_out = defobj;
581 1.29 skrll return def;
582 1.1 cgd }
583