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