xref: /openbsd-src/libexec/ld.so/resolve.c (revision 24781517caa1ed1fba1495b4a36a0ad48d63a50b)
1 /*	$OpenBSD: resolve.c,v 1.14 2002/11/14 15:15:54 drahn Exp $ */
2 
3 /*
4  * Copyright (c) 1998 Per Fogelstrom, Opsycon AB
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed under OpenBSD by
17  *	Per Fogelstrom, Opsycon AB, Sweden.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  */
34 
35 #define _DYN_LOADER
36 
37 #include <sys/types.h>
38 
39 #include <nlist.h>
40 #include <link.h>
41 #include "syscall.h"
42 #include "archdep.h"
43 #include "resolve.h"
44 
45 elf_object_t *_dl_objects;
46 elf_object_t *_dl_last_object;
47 
48 /*
49  *	Initialize and add a new dynamic object to the object list.
50  *	Perform necessary relocations of pointers.
51  */
52 elf_object_t *
53 _dl_add_object(const char *objname, Elf_Dyn *dynp, const u_long *dl_data,
54 	const int objtype, const long laddr, const long loff)
55 {
56 	elf_object_t *object;
57 #if 0
58 	_dl_printf("objname [%s], dynp %p, dl_data %p, objtype %x laddr %lx, loff %lx\n",
59 	    objname, dynp, dl_data, objtype, laddr, loff);
60 #endif
61 
62 	object = _dl_malloc(sizeof(elf_object_t));
63 
64 	object->next = NULL;
65 	if (_dl_objects == 0) {			/* First object ? */
66 		_dl_last_object = _dl_objects = object;
67 	} else {
68 		_dl_last_object->next = object;
69 		object->prev = _dl_last_object;
70 		_dl_last_object = object;
71 	}
72 
73 	object->load_dyn = dynp;
74 	while (dynp->d_tag != DT_NULL) {
75 		if (dynp->d_tag < DT_NUM)
76 			object->Dyn.info[dynp->d_tag] = dynp->d_un.d_val;
77 		else if (dynp->d_tag >= DT_LOPROC &&
78 		    dynp->d_tag < DT_LOPROC + DT_NUM)
79 			object->Dyn.info[dynp->d_tag + DT_NUM - DT_LOPROC] =
80 			    dynp->d_un.d_val;
81 		if (dynp->d_tag == DT_TEXTREL)
82 			object->dyn.textrel = 1;
83 		if (dynp->d_tag == DT_SYMBOLIC)
84 			object->dyn.symbolic = 1;
85 		if (dynp->d_tag == DT_BIND_NOW)
86 			object->dyn.bind_now = 1;
87 		dynp++;
88 	}
89 
90 	/*
91 	 *  Now relocate all pointer to dynamic info, but only
92 	 *  the ones which have pointer values.
93 	 */
94 	if (object->Dyn.info[DT_PLTGOT])
95 		object->Dyn.info[DT_PLTGOT] += loff;
96 	if (object->Dyn.info[DT_HASH])
97 		object->Dyn.info[DT_HASH] += loff;
98 	if (object->Dyn.info[DT_STRTAB])
99 		object->Dyn.info[DT_STRTAB] += loff;
100 	if (object->Dyn.info[DT_SYMTAB])
101 		object->Dyn.info[DT_SYMTAB] += loff;
102 	if (object->Dyn.info[DT_RELA])
103 		object->Dyn.info[DT_RELA] += loff;
104 	if (object->Dyn.info[DT_SONAME])
105 		object->Dyn.info[DT_SONAME] += loff;
106 	if (object->Dyn.info[DT_RPATH])
107 		object->Dyn.info[DT_RPATH] += object->Dyn.info[DT_STRTAB];
108 	if (object->Dyn.info[DT_REL])
109 		object->Dyn.info[DT_REL] += loff;
110 	if (object->Dyn.info[DT_INIT])
111 		object->Dyn.info[DT_INIT] += loff;
112 	if (object->Dyn.info[DT_FINI])
113 		object->Dyn.info[DT_FINI] += loff;
114 	if (object->Dyn.info[DT_JMPREL])
115 		object->Dyn.info[DT_JMPREL] += loff;
116 
117 	if (object->Dyn.info[DT_HASH] != 0) {
118 		Elf_Word *hashtab = (Elf_Word *)object->Dyn.info[DT_HASH];
119 
120 		object->nbuckets = hashtab[0];
121 		object->nchains = hashtab[1];
122 		object->buckets = hashtab + 2;
123 		object->chains = object->buckets + object->nbuckets;
124 	}
125 
126 	if (dl_data) {
127 		object->phdrp = (Elf_Phdr *) dl_data[AUX_phdr];
128 		object->phdrc = dl_data[AUX_phnum];
129 	}
130 	object->obj_type = objtype;
131 	object->load_addr = laddr;
132 	object->load_offs = loff;
133 	object->load_name = _dl_strdup(objname);
134 	object->refcount = 1;
135 	return(object);
136 }
137 
138 void
139 _dl_remove_object(elf_object_t *object)
140 {
141 	elf_object_t *depobj;
142 
143 	object->prev->next = object->next;
144 	if (object->next)
145 		object->next->prev = object->prev;
146 
147 	if (_dl_last_object == object)
148 		_dl_last_object = object->prev;
149 
150 	if (object->load_name)
151 		_dl_free(object->load_name);
152 
153 	while ((depobj = object->dep_next)) {
154 		object->dep_next = object->dep_next->dep_next;
155 		_dl_free(depobj);
156 	}
157 	_dl_free(object);
158 }
159 
160 
161 elf_object_t *
162 _dl_lookup_object(const char *name)
163 {
164 	elf_object_t *object;
165 
166 	object = _dl_objects;
167 	while (object) {
168 		if (_dl_strcmp(name, object->load_name) == 0)
169 			return(object);
170 		object = object->next;
171 	}
172 	return(0);
173 }
174 
175 
176 Elf_Addr
177 _dl_find_symbol(const char *name, elf_object_t *startlook,
178     const Elf_Sym **ref, int flags, int req_size, const char *module_name)
179 {
180 	const Elf_Sym *weak_sym = 0;
181 	const char *weak_symn = NULL; /* remove warning */
182 	Elf_Addr weak_offs = 0;
183 	unsigned long h = 0;
184 	const char *p = name;
185 	elf_object_t *object;
186 
187 	while (*p) {
188 		unsigned long g;
189 		h = (h << 4) + *p++;
190 		if ((g = h & 0xf0000000))
191 			h ^= g >> 24;
192 		h &= ~g;
193 	}
194 
195 	for (object = startlook;
196 	    object;
197 	    object = ((flags & SYM_SEARCH_SELF) ? 0 : object->next)) {
198 		const Elf_Sym	*symt = object->dyn.symtab;
199 		const char	*strt = object->dyn.strtab;
200 		long	si;
201 		const char *symn;
202 
203 		for (si = object->buckets[h % object->nbuckets];
204 		    si != STN_UNDEF; si = object->chains[si]) {
205 			const Elf_Sym *sym = symt + si;
206 
207 			if (sym->st_value == 0)
208 				continue;
209 
210 			if (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE &&
211 			    ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
212 			    ELF_ST_TYPE(sym->st_info) != STT_FUNC)
213 				continue;
214 
215 			symn = strt + sym->st_name;
216 			if (sym != *ref &&
217 			    _dl_strcmp(symn, name))
218 				continue;
219 
220 			/* allow this symbol if we are referring to a function
221 			 * which has a value, even if section is UNDEF.
222 			 * this allows &func to refer to PLT as per the
223 			 * ELF spec. st_value is checked above.
224 			 * if flags has SYM_PLT set, we must have actual
225 			 * symbol, so this symbol is skipped.
226 			 */
227 			if (sym->st_shndx == SHN_UNDEF)  {
228 				if ((flags & SYM_PLT) || sym->st_value == 0 ||
229 				    ELF_ST_TYPE(sym->st_info) != STT_FUNC) {
230 					continue;
231 				}
232 			}
233 
234 			if (ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
235 				*ref = sym;
236 				if (req_size != sym->st_size &&
237 				    req_size != 0 &&
238 				    (ELF_ST_TYPE(sym->st_info) != STT_FUNC)) {
239 					_dl_printf("%s: %s : WARNING: "
240 					    "symbol(%s) size mismatch ",
241 					    _dl_progname, object->load_name,
242 					    symn);
243 					_dl_printf("relink your program\n");
244 				}
245 				return(object->load_offs);
246 			} else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
247 				if (!weak_sym) {
248 					weak_sym = sym;
249 					weak_symn = symn;
250 					weak_offs = object->load_offs;
251 				}
252 			}
253 		}
254 	}
255 	if (flags & SYM_WARNNOTFOUND) {
256 		if (!weak_sym && ((*ref == NULL) ||
257 		    ELF_ST_BIND((*ref)->st_info) != STB_WEAK)) {
258 			_dl_printf("%s:%s: undefined symbol '%s'\n",
259 			    _dl_progname, module_name, name);
260 		}
261 	}
262 	*ref = weak_sym;
263 	if (weak_sym && req_size != weak_sym->st_size &&
264 	    req_size != 0 && (ELF_ST_TYPE(weak_sym->st_info) != STT_FUNC)) {
265 		_dl_printf("%s:%s: %s : WARNING: "
266 		    "symbol(%s) size mismatch ",
267 		    _dl_progname, module_name, object->load_name,
268 		    weak_symn);
269 		_dl_printf("relink your program\n");
270 	}
271 	return(weak_offs);
272 }
273