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