symbol.c revision 1.24 1 /* $NetBSD: symbol.c,v 1.24 2002/10/04 20:34:10 mycroft Exp $ */
2
3 /*
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by John Polstra.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Dynamic linker for ELF.
36 *
37 * John Polstra <jdp (at) polstra.com>.
38 */
39
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <sys/types.h>
49 #include <sys/mman.h>
50 #include <dirent.h>
51
52 #include "debug.h"
53 #include "rtld.h"
54
55 /*
56 * Hash function for symbol table lookup. Don't even think about changing
57 * this. It is specified by the System V ABI.
58 */
59 unsigned long
60 _rtld_elf_hash(name)
61 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(name, hash, obj, in_plt)
118 const char *name;
119 unsigned long hash;
120 const Obj_Entry *obj;
121 bool in_plt;
122 {
123 unsigned long symnum;
124
125 for (symnum = obj->buckets[hash % obj->nbuckets];
126 symnum != ELF_SYM_UNDEFINED;
127 symnum = obj->chains[symnum]) {
128 const Elf_Sym *symp;
129 const char *strp;
130
131 assert(symnum < obj->nchains);
132 symp = obj->symtab + symnum;
133 strp = obj->strtab + symp->st_name;
134 rdbg(("check %s vs %s in %p", name, strp, obj));
135 if (name[1] == strp[1] && !strcmp(name, strp)) {
136 if (symp->st_shndx != SHN_UNDEF)
137 return symp;
138 #ifndef __mips__
139 /*
140 * XXX DANGER WILL ROBINSON!
141 * If we have a function pointer in the executable's
142 * data section, it points to the executable's PLT
143 * slot, and there is NO relocation emitted. To make
144 * the function pointer comparable to function pointers
145 * in shared libraries, we must resolve data references
146 * in the libraries to point to PLT slots in the
147 * executable, if they exist.
148 */
149 else if (!in_plt && symp->st_value != 0 &&
150 ELF_ST_TYPE(symp->st_info) == STT_FUNC)
151 return symp;
152 #endif
153 else
154 return NULL;
155 }
156 }
157
158 return NULL;
159 }
160
161 /*
162 * Given a symbol number in a referencing object, find the corresponding
163 * definition of the symbol. Returns a pointer to the symbol, or NULL if
164 * no definition was found. Returns a pointer to the Obj_Entry of the
165 * defining object via the reference parameter DEFOBJ_OUT.
166 */
167 const Elf_Sym *
168 _rtld_find_symdef(symnum, refobj, defobj_out, in_plt)
169 unsigned long symnum;
170 const Obj_Entry *refobj;
171 const Obj_Entry **defobj_out;
172 bool in_plt;
173 {
174 const Elf_Sym *ref;
175 const Elf_Sym *def;
176 const Elf_Sym *symp;
177 const Obj_Entry *obj;
178 const Obj_Entry *defobj;
179 const Objlist_Entry *elm;
180 const char *name;
181 unsigned long hash;
182
183 ref = refobj->symtab + symnum;
184 name = refobj->strtab + ref->st_name;
185
186 hash = _rtld_elf_hash(name);
187 def = NULL;
188 defobj = NULL;
189
190 if (refobj->symbolic) { /* Look first in the referencing object */
191 symp = _rtld_symlook_obj(name, hash, refobj, in_plt);
192 if (symp != NULL) {
193 def = symp;
194 defobj = refobj;
195 }
196 }
197
198 /* Search all objects loaded at program start up. */
199 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
200 rdbg(("search _rtld_list_main"));
201 symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj, in_plt);
202 if (symp != NULL &&
203 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
204 def = symp;
205 defobj = obj;
206 }
207 }
208
209 /* Search all dlopened DAGs containing the referencing object. */
210 SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
211 if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
212 break;
213 rdbg(("search DAG with root %p (%s)", elm->obj, elm->obj->path));
214 symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers, &obj, in_plt);
215 if (symp != NULL &&
216 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
217 def = symp;
218 defobj = obj;
219 }
220 }
221
222 /* Search all RTLD_GLOBAL objects. */
223 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
224 rdbg(("search _rtld_list_global"));
225 symp = _rtld_symlook_list(name, hash, &_rtld_list_global, &obj, in_plt);
226 if (symp != NULL &&
227 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
228 def = symp;
229 defobj = obj;
230 }
231 }
232
233 /*
234 * If we found no definition and the reference is weak, treat the
235 * symbol as having the value zero.
236 */
237 if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
238 rdbg((" returning _rtld_sym_zero@_rtld_objmain"));
239 def = &_rtld_sym_zero;
240 defobj = _rtld_objmain;
241 }
242
243 if (def != NULL)
244 *defobj_out = defobj;
245 else {
246 rdbg(("lookup failed"));
247 _rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
248 refobj->path, in_plt ? "PLT " : "", name, symnum);
249 }
250 return def;
251 }
252