xref: /openbsd-src/libexec/ld.so/resolve.c (revision f076da76e7da4a2f66eef006c91df3c410c36027)
1 /*	$OpenBSD: resolve.c,v 1.9 2002/05/24 04:17:00 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  * 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 
53 elf_object_t *
54 _dl_add_object(const char *objname, Elf_Dyn *dynp, const u_long *dl_data,
55 	const int objtype, const long laddr, const long loff)
56 {
57 	elf_object_t *object;
58 #if 0
59 	_dl_printf("objname [%s], dynp %p, dl_data %p, objtype %x laddr %lx, loff %lx\n",
60 	    objname, dynp, dl_data, objtype, laddr, loff);
61 #endif
62 
63 	object = _dl_malloc(sizeof(elf_object_t));
64 
65 	object->next = NULL;
66 	if (_dl_objects == 0) {			/* First object ? */
67 		_dl_last_object = _dl_objects = object;
68 	} else {
69 		_dl_last_object->next = object;
70 		object->prev = _dl_last_object;
71 		_dl_last_object = object;
72 	}
73 
74 	object->load_dyn = dynp;
75 	while (dynp->d_tag != DT_NULL) {
76 		if (dynp->d_tag < DT_NUM) {
77 			object->Dyn.info[dynp->d_tag] = dynp->d_un.d_val;
78 		} else if (dynp->d_tag >= DT_LOPROC &&
79 			    dynp->d_tag < DT_LOPROC + DT_NUM) {
80 			object->Dyn.info[dynp->d_tag + DT_NUM - DT_LOPROC] = dynp->
81 d_un.d_val;
82 		}
83 		if (dynp->d_tag == DT_TEXTREL)
84 			object->dyn.textrel = 1;
85 		if (dynp->d_tag == DT_SYMBOLIC)
86 			object->dyn.symbolic = 1;
87 		if (dynp->d_tag == DT_BIND_NOW)
88 			object->dyn.bind_now = 1;
89 		dynp++;
90 	}
91 
92 	/*
93 	 *  Now relocate all pointer to dynamic info, but only
94 	 *  the ones which has pointer values.
95 	 */
96 	if (object->Dyn.info[DT_PLTGOT])
97 		object->Dyn.info[DT_PLTGOT] += loff;
98 	if (object->Dyn.info[DT_HASH])
99 		object->Dyn.info[DT_HASH] += loff;
100 	if (object->Dyn.info[DT_STRTAB])
101 		object->Dyn.info[DT_STRTAB] += loff;
102 	if (object->Dyn.info[DT_SYMTAB])
103 		object->Dyn.info[DT_SYMTAB] += loff;
104 	if (object->Dyn.info[DT_RELA])
105 		object->Dyn.info[DT_RELA] += loff;
106 	if (object->Dyn.info[DT_SONAME])
107 		object->Dyn.info[DT_SONAME] += loff;
108 	if (object->Dyn.info[DT_RPATH])
109 		object->Dyn.info[DT_RPATH] += object->Dyn.info[DT_STRTAB];
110 	if (object->Dyn.info[DT_REL])
111 		object->Dyn.info[DT_REL] += loff;
112 	if (object->Dyn.info[DT_INIT])
113 		object->Dyn.info[DT_INIT] += loff;
114 	if (object->Dyn.info[DT_FINI])
115 		object->Dyn.info[DT_FINI] += loff;
116 	if (object->Dyn.info[DT_JMPREL])
117 		object->Dyn.info[DT_JMPREL] += loff;
118 
119 	if (object->Dyn.info[DT_HASH] != 0) {
120 		Elf_Word *hashtab = (Elf_Word *)object->Dyn.info[DT_HASH];
121 
122 		object->nbuckets = hashtab[0];
123 		object->nchains = hashtab[1];
124 		object->buckets = hashtab + 2;
125 		object->chains = object->buckets + object->nbuckets;
126 	}
127 
128 	if (dl_data) {
129 		object->phdrp = (Elf_Phdr *) dl_data[AUX_phdr];
130 		object->phdrc = dl_data[AUX_phnum];
131 	}
132 	object->obj_type = objtype;
133 	object->load_addr = laddr;
134 	object->load_offs = loff;
135 	object->load_name = _dl_strdup(objname);
136 	object->refcount = 1;
137 	return(object);
138 }
139 
140 void
141 _dl_remove_object(elf_object_t *object)
142 {
143 	elf_object_t *depobj;
144 
145 	object->prev->next = object->next;
146 	if (object->next)
147 		object->next->prev = object->prev;
148 
149 	if (_dl_last_object == object)
150 		_dl_last_object = object->prev;
151 
152 	if (object->load_name)
153 		_dl_free(object->load_name);
154 
155 	while ((depobj = object->dep_next)) {
156 		object->dep_next = object->dep_next->dep_next;
157 		_dl_free(depobj);
158 	}
159 	_dl_free(object);
160 }
161 
162 
163 elf_object_t *
164 _dl_lookup_object(const char *name)
165 {
166 	elf_object_t *object;
167 
168 	object = _dl_objects;
169 	while (object) {
170 		if (_dl_strcmp(name, object->load_name) == 0)
171 			return(object);
172 		object = object->next;
173 	}
174 	return(0);
175 }
176 
177 
178 Elf_Addr
179 _dl_find_symbol(const char *name, elf_object_t *startlook,
180 			const Elf_Sym **ref, int myself, int warnnotfound)
181 {
182 	unsigned long h = 0;
183 	const char *p = name;
184 	elf_object_t *object;
185 	const Elf_Sym *weak_sym = 0;
186 	Elf_Addr weak_offs = 0;
187 
188 	while (*p) {
189 		unsigned long g;
190 		h = (h << 4) + *p++;
191 		if ((g = h & 0xf0000000)) {
192 			h ^= g >> 24;
193 		}
194 		h &= ~g;
195 	}
196 
197 	for (object = startlook; object; object = (myself ? 0 : object->next)) {
198 		const Elf_Sym	*symt = object->dyn.symtab;
199 		const char	*strt = object->dyn.strtab;
200 		long	si;
201 
202 		for (si = object->buckets[h % object->nbuckets];
203 		    si != STN_UNDEF; si = object->chains[si]) {
204 			const Elf_Sym *sym = symt + si;
205 
206 			if (sym->st_value == 0 ||
207 			    sym->st_shndx == SHN_UNDEF)
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 			if (sym != *ref &&
216 			    _dl_strcmp(strt + sym->st_name, name))
217 				continue;
218 
219 			if (ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
220 				*ref = sym;
221 				return(object->load_offs);
222 			} else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
223 				if (!weak_sym) {
224 					weak_sym = sym;
225 					weak_offs = object->load_offs;
226 				}
227 			}
228 		}
229 	}
230 	if (warnnotfound) {
231 		if (!weak_sym && *ref &&
232 		    ELF_ST_BIND((*ref)->st_info) != STB_WEAK) {
233 			_dl_printf("%s: undefined symbol '%s'\n",
234 			    _dl_progname, name);
235 		}
236 	}
237 	*ref = weak_sym;
238 	return(weak_offs);
239 }
240