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