xref: /openbsd-src/libexec/ld.so/library_mquery.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: library_mquery.c,v 1.38 2010/11/16 18:59:00 drahn 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 & RTLD_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 			ld = _dl_malloc(sizeof(struct load_list));
161 			ld->start = NULL;
162 			ld->size = size;
163 			ld->moff = TRUNC_PG(phdp->p_vaddr);
164 			ld->foff = TRUNC_PG(phdp->p_offset);
165 			ld->prot = PFLAGS(phdp->p_flags);
166 			LDLIST_INSERT(ld);
167 
168 			if ((ld->prot & PROT_WRITE) == 0 ||
169 			    ROUND_PG(size) == ROUND_PG(off + phdp->p_memsz))
170 				break;
171 			/* This phdr has a zfod section */
172 			ld = _dl_malloc(sizeof(struct load_list));
173 			ld->start = NULL;
174 			ld->size = ROUND_PG(off + phdp->p_memsz) -
175 			    ROUND_PG(size);
176 			ld->moff = TRUNC_PG(phdp->p_vaddr) +
177 			    ROUND_PG(size);
178 			ld->foff = -1;
179 			ld->prot = PFLAGS(phdp->p_flags);
180 			LDLIST_INSERT(ld);
181 			break;
182 		case PT_DYNAMIC:
183 			dynp = (Elf_Dyn *)phdp->p_vaddr;
184 			break;
185 		default:
186 			break;
187 		}
188 	}
189 
190 #define LOFF ((Elf_Addr)lowld->start - lowld->moff)
191 
192 retry:
193 	for (ld = lowld; ld != NULL; ld = ld->next) {
194 		off_t foff;
195 		int fd, flags;
196 
197 		/*
198 		 * We don't want to provide the fd/off hint for anything
199 		 * but the first mapping, all other might have
200 		 * cache-incoherent aliases and will cause this code to
201 		 * loop forever.
202 		 */
203 		if (ld == lowld) {
204 			fd = libfile;
205 			foff = ld->foff;
206 			flags = 0;
207 		} else {
208 			fd = -1;
209 			foff = 0;
210 			flags = MAP_FIXED;
211 		}
212 
213 		ld->start = (void *)(LOFF + ld->moff);
214 
215 		/*
216 		 * Magic here.
217 		 * The first mquery is done with MAP_FIXED to see if
218 		 * the mapping we want is free. If it's not, we redo the
219 		 * mquery without MAP_FIXED to get the next free mapping,
220 		 * adjust the base mapping address to match this free mapping
221 		 * and restart the process again.
222 		 */
223 		ld->start = _dl_mquery(ld->start, ROUND_PG(ld->size), ld->prot,
224 		    flags, fd, foff);
225 		if (_dl_mmap_error(ld->start)) {
226 			ld->start = (void *)(LOFF + ld->moff);
227 			ld->start = _dl_mquery(ld->start, ROUND_PG(ld->size),
228 			    ld->prot, flags & ~MAP_FIXED, fd, foff);
229 			if (_dl_mmap_error(ld->start))
230 				goto fail;
231 		}
232 
233 		if (ld->start != (void *)(LOFF + ld->moff)) {
234 			lowld->start = ld->start - ld->moff + lowld->moff;
235 			goto retry;
236 		}
237 		/*
238 		 * XXX - we need some kind of boundary condition here,
239 		 * or fix mquery to not run into the stack
240 		 */
241 	}
242 
243 	for (ld = lowld; ld != NULL; ld = ld->next) {
244 		int fd, flags;
245 		off_t foff;
246 		void *res;
247 
248 		if (ld->foff < 0) {
249 			fd = -1;
250 			foff = 0;
251 			flags = MAP_FIXED|MAP_PRIVATE|MAP_ANON;
252 		} else {
253 			fd = libfile;
254 			foff = ld->foff;
255 			flags = MAP_FIXED|MAP_PRIVATE;
256 		}
257 		res = _dl_mmap(ld->start, ROUND_PG(ld->size), ld->prot, flags,
258 		    fd, foff);
259 		if (_dl_mmap_error(res))
260 			goto fail;
261 		/* Zero out everything past the EOF */
262 		if ((ld->prot & PROT_WRITE) != 0 && (ld->size & align) != 0)
263 			_dl_memset((char *)ld->start + ld->size, 0,
264 			    _dl_pagesz - (ld->size & align));
265 		load_end = (Elf_Addr)ld->start + ROUND_PG(ld->size);
266 	}
267 
268 	prebind_data = prebind_load_fd(libfile, libname);
269 
270 	_dl_close(libfile);
271 
272 	dynp = (Elf_Dyn *)((unsigned long)dynp + LOFF);
273 	object = _dl_finalize_object(libname, dynp,
274 	    (Elf_Phdr *)((char *)lowld->start + ehdr->e_phoff), ehdr->e_phnum,
275 	    type, (Elf_Addr)lowld->start, LOFF);
276 	if (object) {
277 		object->prebind_data = prebind_data;
278 		object->load_size = (Elf_Addr)load_end - (Elf_Addr)lowld->start;
279 		object->load_list = lowld;
280 		/* set inode, dev from stat info */
281 		object->dev = sb.st_dev;
282 		object->inode = sb.st_ino;
283 		object->obj_flags |= flags;
284 		_dl_build_sod(object->load_name, &object->sod);
285 	} else {
286 		/* XXX no point. object is never returned NULL */
287 		_dl_load_list_free(lowld);
288 	}
289 	return(object);
290 fail:
291 	_dl_printf("%s: rtld mmap failed mapping %s.\n",
292 	    _dl_progname, libname);
293 	_dl_close(libfile);
294 	_dl_errno = DL_CANT_MMAP;
295 	_dl_load_list_free(lowld);
296 	return(0);
297 }
298