xref: /openbsd-src/libexec/ld.so/resolve.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: resolve.c,v 1.4 2001/06/08 06:46:59 art 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 void * _dl_malloc(int);
49 
50 /*
51  *	Initialize and add a new dynamic object to the object list.
52  *	Perform necessary relocations of pointers.
53  */
54 
55 elf_object_t *
56 _dl_add_object(const char *objname, Elf_Dyn *dynp, const u_long *dl_data,
57 	const int objtype, const long laddr, const long loff)
58 {
59 	elf_object_t *object;
60 #if 0
61 	_dl_printf("objname [%s], dynp %p, dl_data %p, objtype %x laddr %lx, loff %lx\n",
62 		objname, dynp, dl_data, objtype, laddr, loff);
63 #endif
64 
65 	object = _dl_malloc(sizeof(elf_object_t));
66 
67 	if (_dl_objects == 0) {			/* First object ? */
68 		_dl_last_object = _dl_objects = object;
69 	} else {
70 		_dl_last_object->next = object;
71 		object->prev = _dl_last_object;
72 		_dl_last_object = object;
73 	}
74 
75 	object->load_dyn = dynp;
76 	while (dynp->d_tag != DT_NULL) {
77 		if (dynp->d_tag < DT_NUM) {
78 			object->Dyn.info[dynp->d_tag] = dynp->d_un.d_val;
79 		} else if (dynp->d_tag >= DT_LOPROC &&
80 			    dynp->d_tag < DT_LOPROC + DT_NUM) {
81 			object->Dyn.info[dynp->d_tag + DT_NUM - DT_LOPROC] = dynp->
82 d_un.d_val;
83 		}
84 		if (dynp->d_tag == DT_TEXTREL)
85 			object->dyn.textrel = 1;
86 		if (dynp->d_tag == DT_SYMBOLIC)
87 			object->dyn.symbolic = 1;
88 		if (dynp->d_tag == DT_BIND_NOW)
89 			object->dyn.bind_now = 1;
90 		dynp++;
91 	}
92 
93 	/*
94 	 *  Now relocate all pointer to dynamic info, but only
95 	 *  the ones which has pointer values.
96 	 */
97 	if(object->Dyn.info[DT_PLTGOT])
98 		object->Dyn.info[DT_PLTGOT] += loff;
99 	if(object->Dyn.info[DT_HASH])
100 		object->Dyn.info[DT_HASH] += loff;
101 	if(object->Dyn.info[DT_STRTAB])
102 		object->Dyn.info[DT_STRTAB] += loff;
103 	if(object->Dyn.info[DT_SYMTAB])
104 		object->Dyn.info[DT_SYMTAB] += loff;
105 	if(object->Dyn.info[DT_RELA])
106 		object->Dyn.info[DT_RELA] += loff;
107 	if(object->Dyn.info[DT_SONAME])
108 		object->Dyn.info[DT_SONAME] += loff;
109 	if(object->Dyn.info[DT_RPATH])
110 		object->Dyn.info[DT_RPATH] += object->Dyn.info[DT_STRTAB];
111 	if(object->Dyn.info[DT_REL])
112 		object->Dyn.info[DT_REL] += loff;
113 	if(object->Dyn.info[DT_INIT])
114 		object->Dyn.info[DT_INIT] += loff;
115 	if(object->Dyn.info[DT_FINI])
116 		object->Dyn.info[DT_FINI] += loff;
117 	if(object->Dyn.info[DT_JMPREL])
118 		object->Dyn.info[DT_JMPREL] += loff;
119 
120 	object->buckets = object->dyn.hash;
121 	if(object->buckets != 0) {
122 		object->nbuckets = *object->buckets++;
123 		object->nchains  = *object->buckets++;
124 		object->chains   = object->buckets + object->nbuckets;
125 	}
126 
127 	if(dl_data) {
128 		object->phdrp = (Elf_Phdr *) dl_data[AUX_phdr];
129 		object->phdrc = dl_data[AUX_phnum];
130 	}
131 	object->obj_type = objtype;
132 	object->load_addr = laddr;
133 	object->load_offs = loff;
134 	object->load_name = (char *)_dl_malloc(_dl_strlen(objname) + 1);
135 	_dl_strcpy(object->load_name, objname);
136 	object->refcount = 1;
137 
138 	return(object);
139 }
140 
141 
142 void
143 _dl_remove_object(elf_object_t *object)
144 {
145 	elf_object_t *depobj;
146 
147 	object->prev->next = object->next;
148 	if(object->next) {
149 		object->next->prev = object->prev;
150 	}
151 	if(object->load_name) {
152 		_dl_free(object->load_name);
153 	}
154 	while((depobj = object->dep_next)) {
155 		object->dep_next = object->dep_next->dep_next;
156 		_dl_free(depobj);
157 	}
158 	_dl_free(object);
159 }
160 
161 
162 elf_object_t *
163 _dl_lookup_object(const char *name)
164 {
165 	elf_object_t *object;
166 
167 	object = _dl_objects;
168 	while(object) {
169 		if(_dl_strcmp(name, object->load_name) == 0) {
170 			return(object);
171 		}
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;
199 		const char	*strt;
200 		u_int32_t	si;
201 
202 		symt = object->dyn.symtab;
203 		strt = object->dyn.strtab;
204 
205 		for(si = object->buckets[h % object->nbuckets];
206 			si != STN_UNDEF; si = object->chains[si]) {
207 			const Elf_Sym *sym = symt + si;
208 
209 			if(sym->st_value == 0 ||
210 			   sym->st_shndx == SHN_UNDEF) {
211 				continue;
212 			}
213 
214 			if(ELF_ST_TYPE(sym->st_info) != STT_NOTYPE &&
215 			   ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
216 			   ELF_ST_TYPE(sym->st_info) != STT_FUNC) {
217 				continue;
218 			}
219 
220 			if(sym != *ref && _dl_strcmp(strt+sym->st_name, name)) {
221 				continue;
222 			}
223 
224 			if(ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
225 				*ref = sym;
226 				return(object->load_offs);
227 			}
228 			else if(ELF_ST_BIND(sym->st_info) == STB_WEAK) {
229 				if(!weak_sym) {
230 					weak_sym = sym;
231 					weak_offs = object->load_offs;
232 				}
233 			}
234 		}
235 	}
236 	if (warnnotfound) {
237 		if(!weak_sym &&
238 			*ref && ELF_ST_BIND((*ref)->st_info) != STB_WEAK)
239 		{
240 			_dl_printf("%s: undefined symbol '%s'\n",
241 				_dl_progname, name);
242 		}
243 	}
244 	*ref = weak_sym;
245 	return(weak_offs);
246 }
247 
248