symbol.c revision 1.40.4.1 1 /* $NetBSD: symbol.c,v 1.40.4.1 2008/10/31 21:00:36 snj Exp $ */
2
3 /*
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 * Copyright 2002 Charles M. Hannum <root (at) ihack.net>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
22 *
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.
33 */
34
35 /*
36 * Dynamic linker for ELF.
37 *
38 * John Polstra <jdp (at) polstra.com>.
39 */
40
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __RCSID("$NetBSD: symbol.c,v 1.40.4.1 2008/10/31 21:00:36 snj Exp $");
44 #endif /* not lint */
45
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <sys/types.h>
55 #include <sys/mman.h>
56 #include <dirent.h>
57
58 #include "debug.h"
59 #include "rtld.h"
60
61 typedef void (*fptr_t)(void);
62
63 static bool
64 _rtld_is_exported(const Elf_Sym *def)
65 {
66 static fptr_t _rtld_exports[] = {
67 (fptr_t)dlopen,
68 (fptr_t)dlclose,
69 (fptr_t)dlsym,
70 (fptr_t)dlerror,
71 (fptr_t)dladdr,
72 NULL
73 };
74 int i;
75 fptr_t value;
76
77 value = (fptr_t)(_rtld_objself.relocbase + def->st_value);
78 for (i = 0; _rtld_exports[i] != NULL; i++) {
79 if (value == _rtld_exports[i])
80 return true;
81 }
82 return false;
83 }
84
85 /*
86 * Hash function for symbol table lookup. Don't even think about changing
87 * this. It is specified by the System V ABI.
88 */
89 unsigned long
90 _rtld_elf_hash(const char *name)
91 {
92 const unsigned char *p = (const unsigned char *) name;
93 unsigned long h = 0;
94 unsigned long g;
95 unsigned long c;
96
97 for (; __predict_true((c = *p) != '\0'); p++) {
98 h <<= 4;
99 h += c;
100 if ((g = h & 0xf0000000) != 0) {
101 h ^= g;
102 h ^= g >> 24;
103 }
104 }
105 return (h);
106 }
107
108 const Elf_Sym *
109 _rtld_symlook_list(const char *name, unsigned long hash, const Objlist *objlist,
110 const Obj_Entry **defobj_out, bool in_plt)
111 {
112 const Elf_Sym *symp;
113 const Elf_Sym *def;
114 const Obj_Entry *defobj;
115 const Objlist_Entry *elm;
116
117 def = NULL;
118 defobj = NULL;
119 SIMPLEQ_FOREACH(elm, objlist, link) {
120 rdbg(("search object %p (%s)", elm->obj, elm->obj->path));
121 if ((symp = _rtld_symlook_obj(name, hash, elm->obj, in_plt))
122 != NULL) {
123 if ((def == NULL) ||
124 (ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
125 def = symp;
126 defobj = elm->obj;
127 if (ELF_ST_BIND(def->st_info) != STB_WEAK)
128 break;
129 }
130 }
131 }
132 if (def != NULL)
133 *defobj_out = defobj;
134 return def;
135 }
136
137 /*
138 * Search the symbol table of a shared object and all objects needed by it for
139 * a symbol of the given name. Search order is breadth-first. Returns a pointer
140 * to the symbol, or NULL if no definition was found.
141 */
142 const Elf_Sym *
143 _rtld_symlook_needed(const char *name, unsigned long hash,
144 const Needed_Entry *needed, const Obj_Entry **defobj_out, bool inplt)
145 {
146 const Elf_Sym *def, *def_w;
147 const Needed_Entry *n;
148 const Obj_Entry *obj, *defobj, *defobj1;
149
150 def = def_w = NULL;
151 defobj = NULL;
152 for (n = needed; n != NULL; n = n->next) {
153 if ((obj = n->obj) == NULL ||
154 (def = _rtld_symlook_obj(name, hash, obj, inplt)) == NULL)
155 continue;
156 defobj = obj;
157 if (ELF_ST_BIND(def->st_info) != STB_WEAK) {
158 *defobj_out = defobj;
159
160 return (def);
161 }
162 }
163 /*
164 * Either the symbol definition has not been found in directly needed
165 * objects, or the found symbol is weak.
166 */
167 for (n = needed; n != NULL; n = n->next) {
168 if ((obj = n->obj) == NULL)
169 continue;
170 def_w = _rtld_symlook_needed(name, hash, obj->needed, &defobj1,
171 inplt);
172 if (def_w == NULL)
173 continue;
174 if (def == NULL || ELF_ST_BIND(def_w->st_info) != STB_WEAK) {
175 def = def_w;
176 defobj = defobj1;
177 if (ELF_ST_BIND(def_w->st_info) != STB_WEAK)
178 break;
179 }
180 }
181 if (def != NULL)
182 *defobj_out = defobj;
183
184 return def;
185 }
186
187 /*
188 * Search the symbol table of a single shared object for a symbol of
189 * the given name. Returns a pointer to the symbol, or NULL if no
190 * definition was found.
191 *
192 * The symbol's hash value is passed in for efficiency reasons; that
193 * eliminates many recomputations of the hash value.
194 */
195 const Elf_Sym *
196 _rtld_symlook_obj(const char *name, unsigned long hash,
197 const Obj_Entry *obj, bool in_plt)
198 {
199 unsigned long symnum;
200
201 for (symnum = obj->buckets[hash % obj->nbuckets];
202 symnum != ELF_SYM_UNDEFINED;
203 symnum = obj->chains[symnum]) {
204 const Elf_Sym *symp;
205 const char *strp;
206
207 assert(symnum < obj->nchains);
208 symp = obj->symtab + symnum;
209 strp = obj->strtab + symp->st_name;
210 rdbg(("check \"%s\" vs \"%s\" in %p", name, strp, obj));
211 if (name[1] == strp[1] && !strcmp(name, strp)) {
212 if (symp->st_shndx != SHN_UNDEF)
213 return symp;
214 #ifndef __mips__
215 /*
216 * XXX DANGER WILL ROBINSON!
217 * If we have a function pointer in the executable's
218 * data section, it points to the executable's PLT
219 * slot, and there is NO relocation emitted. To make
220 * the function pointer comparable to function pointers
221 * in shared libraries, we must resolve data references
222 * in the libraries to point to PLT slots in the
223 * executable, if they exist.
224 */
225 else if (!in_plt && symp->st_value != 0 &&
226 ELF_ST_TYPE(symp->st_info) == STT_FUNC)
227 return symp;
228 #endif
229 else
230 return NULL;
231 }
232 }
233
234 return NULL;
235 }
236
237 /*
238 * Given a symbol number in a referencing object, find the corresponding
239 * definition of the symbol. Returns a pointer to the symbol, or NULL if
240 * no definition was found. Returns a pointer to the Obj_Entry of the
241 * defining object via the reference parameter DEFOBJ_OUT.
242 */
243 const Elf_Sym *
244 _rtld_find_symdef(unsigned long symnum, const Obj_Entry *refobj,
245 const Obj_Entry **defobj_out, bool in_plt)
246 {
247 const Elf_Sym *ref;
248 const Elf_Sym *def;
249 const Obj_Entry *defobj;
250 const char *name;
251 unsigned long hash;
252
253 ref = refobj->symtab + symnum;
254 name = refobj->strtab + ref->st_name;
255
256 /*
257 * We don't have to do a full scale lookup if the symbol is local.
258 * We know it will bind to the instance in this load module; to
259 * which we already have a pointer (ie ref).
260 */
261 if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
262 if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
263 _rtld_error("%s: Bogus symbol table entry %lu",
264 refobj->path, symnum);
265 }
266
267 hash = _rtld_elf_hash(name);
268 defobj = NULL;
269 def = _rtld_symlook_default(name, hash, refobj, &defobj, in_plt);
270 } else {
271 rdbg(("STB_LOCAL symbol %s in %s", name, refobj->path));
272 def = ref;
273 defobj = refobj;
274 }
275
276 /*
277 * If we found no definition and the reference is weak, treat the
278 * symbol as having the value zero.
279 */
280 if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
281 rdbg((" returning _rtld_sym_zero@_rtld_objmain"));
282 def = &_rtld_sym_zero;
283 defobj = _rtld_objmain;
284 }
285
286 if (def != NULL)
287 *defobj_out = defobj;
288 else {
289 rdbg(("lookup failed"));
290 _rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
291 refobj->path, in_plt ? "PLT " : "", name, symnum);
292 }
293 return def;
294 }
295
296 /*
297 * Given a symbol name in a referencing object, find the corresponding
298 * definition of the symbol. Returns a pointer to the symbol, or NULL if
299 * no definition was found. Returns a pointer to the Obj_Entry of the
300 * defining object via the reference parameter DEFOBJ_OUT.
301 */
302 const Elf_Sym *
303 _rtld_symlook_default(const char *name, unsigned long hash,
304 const Obj_Entry *refobj, const Obj_Entry **defobj_out, bool in_plt)
305 {
306 const Elf_Sym *def;
307 const Elf_Sym *symp;
308 const Obj_Entry *obj;
309 const Obj_Entry *defobj;
310 const Objlist_Entry *elm;
311 def = NULL;
312 defobj = NULL;
313
314 /* Look first in the referencing object if linked symbolically. */
315 if (refobj->symbolic) {
316 rdbg(("search referencing object"));
317 symp = _rtld_symlook_obj(name, hash, refobj, in_plt);
318 if (symp != NULL) {
319 def = symp;
320 defobj = refobj;
321 }
322 }
323
324 /* Search all objects loaded at program start up. */
325 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
326 rdbg(("search _rtld_list_main"));
327 symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj,
328 in_plt);
329 if (symp != NULL &&
330 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
331 def = symp;
332 defobj = obj;
333 }
334 }
335
336 /* Search all RTLD_GLOBAL objects. */
337 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
338 rdbg(("search _rtld_list_global"));
339 symp = _rtld_symlook_list(name, hash, &_rtld_list_global,
340 &obj, in_plt);
341 if (symp != NULL &&
342 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
343 def = symp;
344 defobj = obj;
345 }
346 }
347
348 /* Search all dlopened DAGs containing the referencing object. */
349 SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
350 if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
351 break;
352 rdbg(("search DAG with root %p (%s)", elm->obj,
353 elm->obj->path));
354 symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers,
355 &obj, in_plt);
356 if (symp != NULL &&
357 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
358 def = symp;
359 defobj = obj;
360 }
361 }
362
363 /*
364 * Search the dynamic linker itself, and possibly resolve the
365 * symbol from there. This is how the application links to
366 * dynamic linker services such as dlopen. Only the values listed
367 * in the "_rtld_exports" array can be resolved from the dynamic
368 * linker.
369 */
370 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
371 rdbg(("Search the dynamic linker itself."));
372 symp = _rtld_symlook_obj(name, hash, &_rtld_objself, in_plt);
373 if (symp != NULL && _rtld_is_exported(symp)) {
374 def = symp;
375 defobj = &_rtld_objself;
376 }
377 }
378
379 if (def != NULL)
380 *defobj_out = defobj;
381 return def;
382 }
383