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