exec_elf32.c revision 1.6
1/*	$NetBSD: exec_elf32.c,v 1.6 1996/02/09 18:59:18 christos Exp $	*/
2
3/*
4 * Copyright (c) 1994 Christos Zoulas
5 * All rights reserved.
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 * 3. The name of the author may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/kernel.h>
34#include <sys/proc.h>
35#include <sys/malloc.h>
36#include <sys/namei.h>
37#include <sys/vnode.h>
38#include <sys/exec.h>
39#include <sys/exec_elf.h>
40
41#include <sys/mman.h>
42#include <vm/vm.h>
43#include <vm/vm_param.h>
44#include <vm/vm_map.h>
45
46#include <machine/cpu.h>
47#include <machine/reg.h>
48#include <machine/exec.h>
49
50#ifdef COMPAT_LINUX
51#include <compat/linux/linux_exec.h>
52#endif
53
54#ifdef COMPAT_SVR4
55#include <compat/svr4/svr4_exec.h>
56#endif
57
58int (*elf_probe_funcs[]) __P((struct proc *, struct exec_package *,
59			      char *, u_long *)) = {
60#ifdef COMPAT_SVR4
61	svr4_elf_probe,
62#endif
63#ifdef COMPAT_LINUX
64	linux_elf_probe
65#endif
66};
67
68int elf_check_header __P((Elf32_Ehdr *, int));
69int elf_load_file __P((struct proc *, char *, struct exec_vmcmd_set *,
70		       u_long *, struct elf_args *, u_long *));
71
72static int elf_read_from __P((struct proc *, struct vnode *, u_long,
73	caddr_t, int));
74static void elf_load_psection __P((struct exec_vmcmd_set *,
75	struct vnode *, Elf32_Phdr *, u_long *, u_long *, int *));
76
77#define ELF_ALIGN(a, b) ((a) & ~((b) - 1))
78
79/*
80 * Copy arguments onto the stack in the normal way, but add some
81 * extra information in case of dynamic binding.
82 */
83void *
84elf_copyargs(pack, arginfo, stack, argp)
85	struct exec_package *pack;
86	struct ps_strings *arginfo;
87	void *stack;
88	void *argp;
89{
90	size_t len;
91	AuxInfo ai[ELF_AUX_ENTRIES], *a;
92	struct elf_args *ap;
93
94	stack = copyargs(pack, arginfo, stack, argp);
95	if (!stack)
96		return NULL;
97
98	/*
99	 * Push extra arguments on the stack needed by dynamically
100	 * linked binaries
101	 */
102	if ((ap = (struct elf_args *) pack->ep_emul_arg)) {
103		a = ai;
104
105		a->au_id = AUX_phdr;
106		a->au_v = ap->arg_phaddr;
107		a++;
108
109		a->au_id = AUX_phent;
110		a->au_v = ap->arg_phentsize;
111		a++;
112
113		a->au_id = AUX_phnum;
114		a->au_v = ap->arg_phnum;
115		a++;
116
117		a->au_id = AUX_pagesz;
118		a->au_v = NBPG;
119		a++;
120
121		a->au_id = AUX_base;
122		a->au_v = ap->arg_interp;
123		a++;
124
125		a->au_id = AUX_flags;
126		a->au_v = 0;
127		a++;
128
129		a->au_id = AUX_entry;
130		a->au_v = ap->arg_entry;
131		a++;
132
133		a->au_id = AUX_null;
134		a->au_v = 0;
135		a++;
136
137		free((char *) ap, M_TEMP);
138		len = ELF_AUX_ENTRIES * sizeof (AuxInfo);
139		if (copyout(ai, stack, len))
140			return NULL;
141		stack += len;
142	}
143	return stack;
144}
145
146/*
147 * elf_check_header():
148 *
149 * Check header for validity; return 0 of ok ENOEXEC if error
150 *
151 * XXX machine type needs to be moved to <machine/param.h> so
152 * just one comparison can be done. Unfortunately, there is both
153 * em_486 and em_386, so this would not work on the i386.
154 */
155int
156elf_check_header(eh, type)
157	Elf32_Ehdr *eh;
158	int type;
159{
160
161	if (bcmp(eh->e_ident, Elf32_e_ident, Elf32_e_siz) != 0)
162		return ENOEXEC;
163
164	switch (eh->e_machine) {
165	/* XXX */
166#ifdef i386
167	case Elf32_em_386:
168	case Elf32_em_486:
169#endif
170#ifdef sparc
171	case Elf32_em_sparc:
172#endif
173		break;
174
175	default:
176		return ENOEXEC;
177	}
178
179	if (eh->e_type != type)
180		return ENOEXEC;
181
182	return 0;
183}
184
185/*
186 * elf_load_psection():
187 *
188 * Load a psection at the appropriate address
189 */
190static void
191elf_load_psection(vcset, vp, ph, addr, size, prot)
192	struct exec_vmcmd_set *vcset;
193	struct vnode *vp;
194	Elf32_Phdr *ph;
195	u_long *addr;
196	u_long *size;
197	int *prot;
198{
199	u_long uaddr, msize, rm, rf;
200	long diff, offset;
201
202	/*
203         * If the user specified an address, then we load there.
204         */
205	if (*addr != ELF32_NO_ADDR) {
206		if (ph->p_align > 1) {
207			*addr = ELF_ALIGN(*addr + ph->p_align, ph->p_align);
208			uaddr = ELF_ALIGN(ph->p_vaddr, ph->p_align);
209		} else
210			uaddr = ph->p_vaddr;
211		diff = ph->p_vaddr - uaddr;
212	} else {
213		*addr = uaddr = ph->p_vaddr;
214		if (ph->p_align > 1)
215			*addr = ELF_ALIGN(uaddr, ph->p_align);
216		diff = uaddr - *addr;
217	}
218
219	*prot |= (ph->p_flags & Elf32_pf_r) ? VM_PROT_READ : 0;
220	*prot |= (ph->p_flags & Elf32_pf_w) ? VM_PROT_WRITE : 0;
221	*prot |= (ph->p_flags & Elf32_pf_x) ? VM_PROT_EXECUTE : 0;
222
223	offset = ph->p_offset - diff;
224	*size = ph->p_filesz + diff;
225	msize = ph->p_memsz + diff;
226
227	NEW_VMCMD(vcset, vmcmd_map_readvn, *size, *addr, vp, offset, *prot);
228
229	/*
230         * Check if we need to extend the size of the segment
231         */
232	rm = round_page(*addr + msize);
233	rf = round_page(*addr + *size);
234
235	if (rm != rf) {
236		NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 0, *prot);
237		*size = msize;
238	}
239}
240
241/*
242 * elf_read_from():
243 *
244 *	Read from vnode into buffer at offset.
245 */
246static int
247elf_read_from(p, vp, off, buf, size)
248	struct vnode *vp;
249	u_long off;
250	struct proc *p;
251	caddr_t buf;
252	int size;
253{
254	int error;
255	int resid;
256
257	if ((error = vn_rdwr(UIO_READ, vp, buf, size,
258			     off, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
259			     &resid, p)) != 0)
260		return error;
261	/*
262         * See if we got all of it
263         */
264	if (resid != 0)
265		return ENOEXEC;
266	return 0;
267}
268
269/*
270 * elf_load_file():
271 *
272 * Load a file (interpreter/library) pointed to by path
273 * [stolen from coff_load_shlib()]. Made slightly generic
274 * so it might be used externally.
275 */
276int
277elf_load_file(p, path, vcset, entry, ap, last)
278	struct proc *p;
279	char *path;
280	struct exec_vmcmd_set *vcset;
281	u_long *entry;
282	struct elf_args	*ap;
283	u_long *last;
284{
285	int error, i;
286	struct nameidata nd;
287	Elf32_Ehdr eh;
288	Elf32_Phdr *ph = NULL;
289	u_long phsize;
290	char *bp = NULL;
291	u_long addr = *last;
292
293	bp = path;
294	/*
295         * 1. open file
296         * 2. read filehdr
297         * 3. map text, data, and bss out of it using VM_*
298         */
299	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, p);
300	if ((error = namei(&nd)) != 0) {
301		return error;
302	}
303	if ((error = elf_read_from(p, nd.ni_vp, 0, (caddr_t) &eh,
304				    sizeof(eh))) != 0)
305		goto bad;
306
307	if ((error = elf_check_header(&eh, Elf32_et_dyn)) != 0)
308		goto bad;
309
310	phsize = eh.e_phnum * sizeof(Elf32_Phdr);
311	ph = (Elf32_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
312
313	if ((error = elf_read_from(p, nd.ni_vp, eh.e_phoff,
314				    (caddr_t) ph, phsize)) != 0)
315		goto bad;
316
317	/*
318         * Load all the necessary sections
319         */
320	for (i = 0; i < eh.e_phnum; i++) {
321		u_long size = 0;
322		int prot = 0;
323
324		switch (ph[i].p_type) {
325		case Elf32_pt_load:
326			elf_load_psection(vcset, nd.ni_vp, &ph[i], &addr,
327						&size, &prot);
328			/* If entry is within this section it must be text */
329			if (eh.e_entry >= ph[i].p_vaddr &&
330			    eh.e_entry < (ph[i].p_vaddr + size)) {
331				*entry = addr + eh.e_entry;
332				ap->arg_interp = addr;
333			}
334			addr += size;
335			break;
336
337		case Elf32_pt_dynamic:
338		case Elf32_pt_phdr:
339		case Elf32_pt_note:
340			break;
341
342		default:
343			break;
344		}
345	}
346
347bad:
348	if (ph != NULL)
349		free((char *) ph, M_TEMP);
350
351	*last = addr;
352	vrele(nd.ni_vp);
353	return error;
354}
355
356/*
357 * exec_elf_makecmds(): Prepare an Elf binary's exec package
358 *
359 * First, set of the various offsets/lengths in the exec package.
360 *
361 * Then, mark the text image busy (so it can be demand paged) or error
362 * out if this is not possible.  Finally, set up vmcmds for the
363 * text, data, bss, and stack segments.
364 *
365 * XXX no demand paging (yet?)
366 */
367int
368exec_elf_makecmds(p, epp)
369	struct proc *p;
370	struct exec_package *epp;
371{
372	Elf32_Ehdr *eh = epp->ep_hdr;
373	Elf32_Phdr *ph, *pp;
374	Elf32_Addr phdr = 0;
375	int error, i, n, nload;
376	char interp[MAXPATHLEN];
377	u_long pos = 0, phsize;
378
379	if (epp->ep_hdrvalid < sizeof(Elf32_Ehdr))
380		return ENOEXEC;
381
382	if (elf_check_header(eh, Elf32_et_exec))
383		return ENOEXEC;
384
385	/*
386         * check if vnode is in open for writing, because we want to
387         * demand-page out of it.  if it is, don't do it, for various
388         * reasons
389         */
390	if (epp->ep_vp->v_writecount != 0) {
391#ifdef DIAGNOSTIC
392		if (epp->ep_vp->v_flag & VTEXT)
393			panic("exec: a VTEXT vnode has writecount != 0\n");
394#endif
395		return ETXTBSY;
396	}
397	/*
398         * Allocate space to hold all the program headers, and read them
399         * from the file
400         */
401	phsize = eh->e_phnum * sizeof(Elf32_Phdr);
402	ph = (Elf32_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
403
404	if ((error = elf_read_from(p, epp->ep_vp, eh->e_phoff,
405				    (caddr_t) ph, phsize)) != 0)
406		goto bad;
407
408	epp->ep_tsize = ELF32_NO_ADDR;
409	epp->ep_dsize = ELF32_NO_ADDR;
410
411	interp[0] = '\0';
412
413	for (i = 0; i < eh->e_phnum; i++) {
414		pp = &ph[i];
415		if (pp->p_type == Elf32_pt_interp) {
416			if (pp->p_filesz >= sizeof(interp))
417				goto bad;
418			if ((error = elf_read_from(p, epp->ep_vp, pp->p_offset,
419				      (caddr_t) interp, pp->p_filesz)) != 0)
420				goto bad;
421			break;
422		}
423	}
424
425	/*
426	 * On the same architecture, we may be emulating different systems.
427	 * See which one will accept this executable. This currently only
428	 * applies to Linux and SVR4 on the i386.
429	 *
430	 * Probe functions would normally see if the interpreter (if any)
431	 * exists. Emulation packages may possibly replace the interpreter in
432	 * interp[] with a changed path (/emul/xxx/<path>), and also
433	 * set the ep_emul field in the exec package structure.
434	 */
435	if ((n = sizeof elf_probe_funcs / sizeof elf_probe_funcs[0])) {
436		error = ENOEXEC;
437		for (i = 0; i < n && error; i++)
438			error = elf_probe_funcs[i](p, epp, interp, &pos);
439
440		if (error)
441			goto bad;
442	}
443
444	/*
445         * Load all the necessary sections
446         */
447	for (i = nload = 0; i < eh->e_phnum; i++) {
448		u_long  addr = ELF32_NO_ADDR, size = 0;
449		int prot = 0;
450
451		pp = &ph[i];
452
453		switch (ph[i].p_type) {
454		case Elf32_pt_load:
455			/*
456			 * XXX
457			 * Can handle only 2 sections: text and data
458			 */
459			if (nload++ == 2)
460				goto bad;
461			elf_load_psection(&epp->ep_vmcmds, epp->ep_vp,
462				&ph[i], &addr, &size, &prot);
463			/*
464			 * Decide whether it's text or data by looking
465			 * at the entry point.
466			 */
467			if (eh->e_entry >= addr && eh->e_entry < (addr + size)){
468				epp->ep_taddr = addr;
469				epp->ep_tsize = size;
470			} else {
471				epp->ep_daddr = addr;
472				epp->ep_dsize = size;
473			}
474			break;
475
476		case Elf32_pt_shlib:
477			error = ENOEXEC;
478			goto bad;
479
480		case Elf32_pt_interp:
481			/* Already did this one */
482		case Elf32_pt_dynamic:
483		case Elf32_pt_note:
484			break;
485
486		case Elf32_pt_phdr:
487			/* Note address of program headers (in text segment) */
488			phdr = pp->p_vaddr;
489		break;
490
491		default:
492			/*
493			 * Not fatal, we don't need to understand everything
494			 * :-)
495			 */
496			break;
497		}
498	}
499
500	/*
501	 * If no position to load the interpreter was set by a probe
502	 * function, pick the same address that a non-fixed mmap(0, ..)
503	 * would (i.e. something safely out of the way).
504	 */
505	if (pos == ELF32_NO_ADDR)
506		pos = round_page(epp->ep_daddr + MAXDSIZ);
507
508	/*
509         * Check if we found a dynamically linked binary and arrange to load
510         * it's interpreter
511         */
512	if (interp[0]) {
513		struct elf_args *ap;
514
515		ap = (struct elf_args *) malloc(sizeof(struct elf_args),
516						 M_TEMP, M_WAITOK);
517		if ((error = elf_load_file(p, interp, &epp->ep_vmcmds,
518				&epp->ep_entry, ap, &pos)) != 0) {
519			free((char *) ap, M_TEMP);
520			goto bad;
521		}
522		pos += phsize;
523		ap->arg_phaddr = phdr;
524
525		ap->arg_phentsize = eh->e_phentsize;
526		ap->arg_phnum = eh->e_phnum;
527		ap->arg_entry = eh->e_entry;
528
529		epp->ep_emul_arg = ap;
530	} else
531		epp->ep_entry = eh->e_entry;
532
533	free((char *) ph, M_TEMP);
534	epp->ep_vp->v_flag |= VTEXT;
535	return exec_aout_setup_stack(p, epp);
536
537bad:
538	free((char *) ph, M_TEMP);
539	kill_vmcmds(&epp->ep_vmcmds);
540	return ENOEXEC;
541}
542