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