xref: /openbsd/libexec/ld.so/library_mquery.c (revision cca36db2)
1 /*	$OpenBSD: library_mquery.c,v 1.41 2012/05/08 14:32:01 jsing Exp $ */
2 
3 /*
4  * Copyright (c) 2002 Dale Rahn
5  * Copyright (c) 1998 Per Fogelstrom, Opsycon AB
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #define _DYN_LOADER
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <fcntl.h>
35 #include <sys/mman.h>
36 #include "dl_prebind.h"
37 
38 #include "syscall.h"
39 #include "archdep.h"
40 #include "resolve.h"
41 #include "sod.h"
42 
43 #define PFLAGS(X) ((((X) & PF_R) ? PROT_READ : 0) | \
44 		   (((X) & PF_W) ? PROT_WRITE : 0) | \
45 		   (((X) & PF_X) ? PROT_EXEC : 0))
46 
47 void
48 _dl_load_list_free(struct load_list *load_list)
49 {
50 	struct load_list *next;
51 	Elf_Addr align = _dl_pagesz - 1;
52 
53 	while (load_list != NULL) {
54 		if (load_list->start != NULL)
55 			_dl_munmap(load_list->start,
56 			    ((load_list->size) + align) & ~align);
57 		next = load_list->next;
58 		_dl_free(load_list);
59 		load_list = next;
60 	}
61 }
62 
63 
64 void
65 _dl_unload_shlib(elf_object_t *object)
66 {
67 	struct dep_node *n;
68 
69 	DL_DEB(("unload_shlib called on %s\n", object->load_name));
70 	if (OBJECT_REF_CNT(object) == 0 &&
71 	    (object->status & STAT_UNLOADED) == 0) {
72 		object->status |= STAT_UNLOADED;
73 		TAILQ_FOREACH(n, &object->child_list, next_sib)
74 			_dl_unload_shlib(n->data);
75 		TAILQ_FOREACH(n, &object->grpref_list, next_sib)
76 			_dl_unload_shlib(n->data);
77 		DL_DEB(("unload_shlib unloading on %s\n", object->load_name));
78 		_dl_load_list_free(object->load_list);
79 		_dl_remove_object(object);
80 	}
81 }
82 
83 
84 elf_object_t *
85 _dl_tryload_shlib(const char *libname, int type, int flags)
86 {
87 	int libfile, i;
88 	struct load_list *ld, *lowld = NULL;
89 	elf_object_t *object;
90 	Elf_Dyn *dynp = 0;
91 	Elf_Ehdr *ehdr;
92 	Elf_Phdr *phdp;
93 	Elf_Addr load_end = 0;
94 	Elf_Addr align = _dl_pagesz - 1, off, size;
95 	struct stat sb;
96 	void *prebind_data;
97 	char hbuf[4096];
98 
99 #define ROUND_PG(x) (((x) + align) & ~(align))
100 #define TRUNC_PG(x) ((x) & ~(align))
101 
102 	libfile = _dl_open(libname, O_RDONLY);
103 	if (libfile < 0) {
104 		_dl_errno = DL_CANT_OPEN;
105 		return(0);
106 	}
107 
108 	if ( _dl_fstat(libfile, &sb) < 0) {
109 		_dl_errno = DL_CANT_OPEN;
110 		return(0);
111 	}
112 
113 	for (object = _dl_objects; object != NULL; object = object->next) {
114 		if (object->dev == sb.st_dev &&
115 		    object->inode == sb.st_ino) {
116 			object->obj_flags |= flags & DF_1_GLOBAL;
117 			_dl_close(libfile);
118 			if (_dl_loading_object == NULL)
119 				_dl_loading_object = object;
120 			if (object->load_object != _dl_objects &&
121 			    object->load_object != _dl_loading_object) {
122 				_dl_link_grpref(object->load_object,
123 				    _dl_loading_object);
124 			}
125 			return(object);
126 		}
127 	}
128 
129 	_dl_read(libfile, hbuf, sizeof(hbuf));
130 	ehdr = (Elf_Ehdr *)hbuf;
131 	if (ehdr->e_ident[0] != ELFMAG0  || ehdr->e_ident[1] != ELFMAG1 ||
132 	    ehdr->e_ident[2] != ELFMAG2 || ehdr->e_ident[3] != ELFMAG3 ||
133 	    ehdr->e_type != ET_DYN || ehdr->e_machine != MACHID) {
134 		_dl_close(libfile);
135 		_dl_errno = DL_NOT_ELF;
136 		return(0);
137 	}
138 
139 	/* Insertion sort */
140 #define LDLIST_INSERT(ld) do { \
141 	struct load_list **_ld; \
142 	for (_ld = &lowld; *_ld != NULL; _ld = &(*_ld)->next) \
143 		if ((*_ld)->moff > ld->moff) \
144 			break; \
145 	ld->next = *_ld; \
146 	*_ld = ld; \
147 } while (0)
148 	/*
149 	 *  Alright, we might have a winner!
150 	 *  Figure out how much VM space we need and set up the load
151 	 *  list that we'll use to find free VM space.
152 	 */
153 	phdp = (Elf_Phdr *)(hbuf + ehdr->e_phoff);
154 	for (i = 0; i < ehdr->e_phnum; i++, phdp++) {
155 		switch (phdp->p_type) {
156 		case PT_LOAD:
157 			off = (phdp->p_vaddr & align);
158 			size = off + phdp->p_filesz;
159 
160 			if (size != 0) {
161 				ld = _dl_malloc(sizeof(struct load_list));
162 				ld->start = NULL;
163 				ld->size = size;
164 				ld->moff = TRUNC_PG(phdp->p_vaddr);
165 				ld->foff = TRUNC_PG(phdp->p_offset);
166 				ld->prot = PFLAGS(phdp->p_flags);
167 				LDLIST_INSERT(ld);
168 			}
169 
170 			if ((PFLAGS(phdp->p_flags) & PROT_WRITE) == 0 ||
171 			    ROUND_PG(size) == ROUND_PG(off + phdp->p_memsz))
172 				break;
173 			/* This phdr has a zfod section */
174 			ld = _dl_malloc(sizeof(struct load_list));
175 			ld->start = NULL;
176 			ld->size = ROUND_PG(off + phdp->p_memsz) -
177 			    ROUND_PG(size);
178 			ld->moff = TRUNC_PG(phdp->p_vaddr) +
179 			    ROUND_PG(size);
180 			ld->foff = -1;
181 			ld->prot = PFLAGS(phdp->p_flags);
182 			LDLIST_INSERT(ld);
183 			break;
184 		case PT_DYNAMIC:
185 			dynp = (Elf_Dyn *)phdp->p_vaddr;
186 			break;
187 		case PT_TLS:
188 			_dl_printf("%s: unsupported TLS program header in %s\n",
189 			    _dl_progname, libname);
190 			_dl_close(libfile);
191 			_dl_errno = DL_CANT_LOAD_OBJ;
192 			return(0);
193 		default:
194 			break;
195 		}
196 	}
197 
198 #define LOFF ((Elf_Addr)lowld->start - lowld->moff)
199 
200 retry:
201 	for (ld = lowld; ld != NULL; ld = ld->next) {
202 		off_t foff;
203 		int fd, flags;
204 
205 		/*
206 		 * We don't want to provide the fd/off hint for anything
207 		 * but the first mapping, all other might have
208 		 * cache-incoherent aliases and will cause this code to
209 		 * loop forever.
210 		 */
211 		if (ld == lowld) {
212 			fd = libfile;
213 			foff = ld->foff;
214 			flags = 0;
215 		} else {
216 			fd = -1;
217 			foff = 0;
218 			flags = MAP_FIXED;
219 		}
220 
221 		ld->start = (void *)(LOFF + ld->moff);
222 
223 		/*
224 		 * Magic here.
225 		 * The first mquery is done with MAP_FIXED to see if
226 		 * the mapping we want is free. If it's not, we redo the
227 		 * mquery without MAP_FIXED to get the next free mapping,
228 		 * adjust the base mapping address to match this free mapping
229 		 * and restart the process again.
230 		 */
231 		ld->start = _dl_mquery(ld->start, ROUND_PG(ld->size), ld->prot,
232 		    flags, fd, foff);
233 		if (_dl_mmap_error(ld->start)) {
234 			ld->start = (void *)(LOFF + ld->moff);
235 			ld->start = _dl_mquery(ld->start, ROUND_PG(ld->size),
236 			    ld->prot, flags & ~MAP_FIXED, fd, foff);
237 			if (_dl_mmap_error(ld->start))
238 				goto fail;
239 		}
240 
241 		if (ld->start != (void *)(LOFF + ld->moff)) {
242 			lowld->start = ld->start - ld->moff + lowld->moff;
243 			goto retry;
244 		}
245 		/*
246 		 * XXX - we need some kind of boundary condition here,
247 		 * or fix mquery to not run into the stack
248 		 */
249 	}
250 
251 	for (ld = lowld; ld != NULL; ld = ld->next) {
252 		int fd, flags;
253 		off_t foff;
254 		void *res;
255 
256 		if (ld->foff < 0) {
257 			fd = -1;
258 			foff = 0;
259 			flags = MAP_FIXED|MAP_PRIVATE|MAP_ANON;
260 		} else {
261 			fd = libfile;
262 			foff = ld->foff;
263 			flags = MAP_FIXED|MAP_PRIVATE;
264 		}
265 		res = _dl_mmap(ld->start, ROUND_PG(ld->size), ld->prot, flags,
266 		    fd, foff);
267 		if (_dl_mmap_error(res))
268 			goto fail;
269 		/* Zero out everything past the EOF */
270 		if ((ld->prot & PROT_WRITE) != 0 && (ld->size & align) != 0)
271 			_dl_memset((char *)ld->start + ld->size, 0,
272 			    _dl_pagesz - (ld->size & align));
273 		load_end = (Elf_Addr)ld->start + ROUND_PG(ld->size);
274 	}
275 
276 	prebind_data = prebind_load_fd(libfile, libname);
277 
278 	_dl_close(libfile);
279 
280 	dynp = (Elf_Dyn *)((unsigned long)dynp + LOFF);
281 	object = _dl_finalize_object(libname, dynp,
282 	    (Elf_Phdr *)((char *)lowld->start + ehdr->e_phoff), ehdr->e_phnum,
283 	    type, (Elf_Addr)lowld->start, LOFF);
284 	if (object) {
285 		object->prebind_data = prebind_data;
286 		object->load_size = (Elf_Addr)load_end - (Elf_Addr)lowld->start;
287 		object->load_list = lowld;
288 		/* set inode, dev from stat info */
289 		object->dev = sb.st_dev;
290 		object->inode = sb.st_ino;
291 		object->obj_flags |= flags;
292 		_dl_build_sod(object->load_name, &object->sod);
293 	} else {
294 		/* XXX no point. object is never returned NULL */
295 		_dl_load_list_free(lowld);
296 	}
297 	return(object);
298 fail:
299 	_dl_printf("%s: rtld mmap failed mapping %s.\n",
300 	    _dl_progname, libname);
301 	_dl_close(libfile);
302 	_dl_errno = DL_CANT_MMAP;
303 	_dl_load_list_free(lowld);
304 	return(0);
305 }
306