exec_elf32.c revision 1.13
1275970Scy/*	$NetBSD: exec_elf32.c,v 1.13 1996/10/02 05:30:29 cgd Exp $	*/
2275970Scy
3275970Scy/*
4275970Scy * Copyright (c) 1996 Christopher G. Demetriou
5275970Scy * Copyright (c) 1994 Christos Zoulas
6275970Scy * All rights reserved.
7275970Scy *
8275970Scy * Redistribution and use in source and binary forms, with or without
9275970Scy * modification, are permitted provided that the following conditions
10275970Scy * are met:
11275970Scy * 1. Redistributions of source code must retain the above copyright
12275970Scy *    notice, this list of conditions and the following disclaimer.
13275970Scy * 2. Redistributions in binary form must reproduce the above copyright
14275970Scy *    notice, this list of conditions and the following disclaimer in the
15275970Scy *    documentation and/or other materials provided with the distribution.
16275970Scy * 3. The name of the author may not be used to endorse or promote products
17275970Scy *    derived from this software without specific prior written permission
18275970Scy *
19275970Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20275970Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21275970Scy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22275970Scy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23275970Scy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24275970Scy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25275970Scy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26275970Scy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27275970Scy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28275970Scy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29275970Scy */
30275970Scy
31275970Scy/* If not included by exec_elf64.c, ELFSIZE won't be defined. */
32275970Scy#ifndef ELFSIZE
33275970Scy#define	ELFSIZE		32
34275970Scy#endif
35275970Scy
36275970Scy#include <sys/param.h>
37275970Scy#include <sys/systm.h>
38275970Scy#include <sys/kernel.h>
39275970Scy#include <sys/proc.h>
40275970Scy#include <sys/malloc.h>
41275970Scy#include <sys/namei.h>
42275970Scy#include <sys/vnode.h>
43275970Scy#include <sys/exec.h>
44275970Scy#include <sys/exec_elf.h>
45275970Scy#include <sys/fcntl.h>
46275970Scy#include <sys/syscall.h>
47275970Scy#include <sys/signalvar.h>
48275970Scy
49275970Scy#include <sys/mman.h>
50275970Scy#include <vm/vm.h>
51275970Scy#include <vm/vm_param.h>
52275970Scy#include <vm/vm_map.h>
53275970Scy
54275970Scy#include <machine/cpu.h>
55275970Scy#include <machine/reg.h>
56275970Scy#include <machine/exec.h>
57275970Scy
58275970Scy#ifdef COMPAT_LINUX
59275970Scy#include <compat/linux/linux_exec.h>
60275970Scy#endif
61275970Scy
62275970Scy#ifdef COMPAT_SVR4
63275970Scy#include <compat/svr4/svr4_exec.h>
64275970Scy#endif
65275970Scy
66275970Scy#define	CONCAT(x,y)	__CONCAT(x,y)
67275970Scy#define	ELFNAME(x)	CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
68275970Scy#define	ELFNAME2(x,y)	CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
69275970Scy#define	ELFNAMEEND(x)	CONCAT(x,CONCAT(_elf,ELFSIZE))
70275970Scy#define	ELFDEFNNAME(x)	CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
71275970Scy
72275970Scystatic int ELFNAME(check_header) __P((Elf_Ehdr *, int));
73275970Scystatic int ELFNAME(load_file) __P((struct proc *, char *,
74275970Scy	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *));
75275970Scystatic void ELFNAME(load_psection) __P((struct exec_vmcmd_set *,
76275970Scy	struct vnode *, Elf_Phdr *, Elf_Addr *, u_long *, int *));
77275970Scy
78275970Scyextern char sigcode[], esigcode[];
79275970Scy#ifdef SYSCALL_DEBUG
80275970Scyextern char *syscallnames[];
81275970Scy#endif
82275970Scy
83275970Scystruct emul ELFNAMEEND(emul_netbsd) = {
84275970Scy	"netbsd",
85275970Scy	NULL,
86275970Scy	sendsig,
87275970Scy	SYS_syscall,
88275970Scy	SYS_MAXSYSCALL,
89275970Scy	sysent,
90275970Scy#ifdef SYSCALL_DEBUG
91275970Scy	syscallnames,
92275970Scy#else
93275970Scy	NULL,
94275970Scy#endif
95275970Scy	ELF_AUX_ENTRIES * sizeof(AuxInfo),
96275970Scy	ELFNAME(copyargs),
97275970Scy	setregs,
98275970Scy	sigcode,
99275970Scy	esigcode,
100275970Scy};
101275970Scy
102275970Scyint (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *,
103275970Scy    Elf_Ehdr *, char *, Elf_Addr *)) = {
104275970Scy#if defined(COMPAT_SVR4) && (ELFSIZE == 32)
105275970Scy	ELFNAME2(svr4,probe),			/* XXX not 64-bit safe */
106275970Scy#endif
107275970Scy#if defined(COMPAT_LINUX) && (ELFSIZE == 32)
108275970Scy	ELFNAME2(linux,probe),			/* XXX not 64-bit safe */
109275970Scy#endif
110275970Scy};
111275970Scy
112275970Scy#define ELF_ALIGN(a, b) ((a) & ~((b) - 1))
113275970Scy
114275970Scy/*
115275970Scy * Copy arguments onto the stack in the normal way, but add some
116275970Scy * extra information in case of dynamic binding.
117275970Scy */
118275970Scyvoid *
119275970ScyELFNAME(copyargs)(pack, arginfo, stack, argp)
120275970Scy	struct exec_package *pack;
121275970Scy	struct ps_strings *arginfo;
122275970Scy	void *stack;
123275970Scy	void *argp;
124275970Scy{
125275970Scy	size_t len;
126275970Scy	AuxInfo ai[ELF_AUX_ENTRIES], *a;
127275970Scy	struct elf_args *ap;
128275970Scy
129275970Scy	stack = copyargs(pack, arginfo, stack, argp);
130275970Scy	if (!stack)
131275970Scy		return NULL;
132275970Scy
133275970Scy	/*
134275970Scy	 * Push extra arguments on the stack needed by dynamically
135275970Scy	 * linked binaries
136275970Scy	 */
137275970Scy	if ((ap = (struct elf_args *) pack->ep_emul_arg)) {
138275970Scy		a = ai;
139275970Scy
140275970Scy		a->au_id = AUX_phdr;
141275970Scy		a->au_v = ap->arg_phaddr;
142275970Scy		a++;
143275970Scy
144275970Scy		a->au_id = AUX_phent;
145275970Scy		a->au_v = ap->arg_phentsize;
146275970Scy		a++;
147275970Scy
148275970Scy		a->au_id = AUX_phnum;
149275970Scy		a->au_v = ap->arg_phnum;
150275970Scy		a++;
151275970Scy
152275970Scy		a->au_id = AUX_pagesz;
153275970Scy		a->au_v = NBPG;
154275970Scy		a++;
155275970Scy
156275970Scy		a->au_id = AUX_base;
157275970Scy		a->au_v = ap->arg_interp;
158275970Scy		a++;
159275970Scy
160275970Scy		a->au_id = AUX_flags;
161275970Scy		a->au_v = 0;
162275970Scy		a++;
163275970Scy
164275970Scy		a->au_id = AUX_entry;
165275970Scy		a->au_v = ap->arg_entry;
166275970Scy		a++;
167
168		a->au_id = AUX_null;
169		a->au_v = 0;
170		a++;
171
172		free((char *) ap, M_TEMP);
173		pack->ep_emul_arg = NULL;
174		len = ELF_AUX_ENTRIES * sizeof (AuxInfo);
175		if (copyout(ai, stack, len))
176			return NULL;
177		stack += len;
178	}
179	return stack;
180}
181
182/*
183 * elf_check_header():
184 *
185 * Check header for validity; return 0 of ok ENOEXEC if error
186 *
187 * XXX machine type needs to be moved to <machine/param.h> so
188 * just one comparison can be done. Unfortunately, there is both
189 * em_486 and em_386, so this would not work on the i386.
190 */
191static int
192ELFNAME(check_header)(eh, type)
193	Elf_Ehdr *eh;
194	int type;
195{
196
197	if (bcmp(eh->e_ident, Elf_e_ident, Elf_e_siz) != 0)
198		return ENOEXEC;
199
200	switch (eh->e_machine) {
201
202	ELFDEFNNAME(MACHDEP_ID_CASES)
203
204	default:
205		return ENOEXEC;
206	}
207
208	if (eh->e_type != type)
209		return ENOEXEC;
210
211	return 0;
212}
213
214/*
215 * elf_load_psection():
216 *
217 * Load a psection at the appropriate address
218 */
219static void
220ELFNAME(load_psection)(vcset, vp, ph, addr, size, prot)
221	struct exec_vmcmd_set *vcset;
222	struct vnode *vp;
223	Elf_Phdr *ph;
224	Elf_Addr *addr;
225	u_long *size;
226	int *prot;
227{
228	u_long uaddr, msize, psize, rm, rf;
229	long diff, offset;
230
231	/*
232         * If the user specified an address, then we load there.
233         */
234	if (*addr != ELFDEFNNAME(NO_ADDR)) {
235		if (ph->p_align > 1) {
236			*addr = ELF_ALIGN(*addr + ph->p_align, ph->p_align);
237			uaddr = ELF_ALIGN(ph->p_vaddr, ph->p_align);
238		} else
239			uaddr = ph->p_vaddr;
240		diff = ph->p_vaddr - uaddr;
241	} else {
242		*addr = uaddr = ph->p_vaddr;
243		if (ph->p_align > 1)
244			*addr = ELF_ALIGN(uaddr, ph->p_align);
245		diff = uaddr - *addr;
246	}
247
248	*prot |= (ph->p_flags & Elf_pf_r) ? VM_PROT_READ : 0;
249	*prot |= (ph->p_flags & Elf_pf_w) ? VM_PROT_WRITE : 0;
250	*prot |= (ph->p_flags & Elf_pf_x) ? VM_PROT_EXECUTE : 0;
251
252	offset = ph->p_offset - diff;
253	*size = ph->p_filesz + diff;
254	msize = ph->p_memsz + diff;
255	psize = round_page(*size);
256
257	if ((ph->p_flags & Elf_pf_w) != 0) {
258		/*
259		 * Because the pagedvn pager can't handle zero fill of the last
260		 * data page if it's not page aligned we map the last page
261		 * readvn.
262		 */
263		psize = trunc_page(*size);
264		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
265		    offset, *prot);
266		if(psize != *size)
267			NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize,
268			    *addr + psize, vp, offset + psize, *prot);
269	} else
270		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
271		    offset, *prot);
272
273	/*
274         * Check if we need to extend the size of the segment
275         */
276	rm = round_page(*addr + msize);
277	rf = round_page(*addr + *size);
278
279	if (rm != rf) {
280		NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 0, *prot);
281		*size = msize;
282	}
283}
284
285/*
286 * elf_read_from():
287 *
288 *	Read from vnode into buffer at offset.
289 */
290int
291ELFNAME(read_from)(p, vp, off, buf, size)
292	struct vnode *vp;
293	u_long off;
294	struct proc *p;
295	caddr_t buf;
296	int size;
297{
298	int error;
299	int resid;
300
301	if ((error = vn_rdwr(UIO_READ, vp, buf, size,
302			     off, UIO_SYSSPACE, 0, p->p_ucred,
303			     &resid, p)) != 0)
304		return error;
305	/*
306         * See if we got all of it
307         */
308	if (resid != 0)
309		return ENOEXEC;
310	return 0;
311}
312
313/*
314 * elf_load_file():
315 *
316 * Load a file (interpreter/library) pointed to by path
317 * [stolen from coff_load_shlib()]. Made slightly generic
318 * so it might be used externally.
319 */
320static int
321ELFNAME(load_file)(p, path, vcset, entry, ap, last)
322	struct proc *p;
323	char *path;
324	struct exec_vmcmd_set *vcset;
325	u_long *entry;
326	struct elf_args	*ap;
327	Elf_Addr *last;
328{
329	int error, i;
330	struct nameidata nd;
331	Elf_Ehdr eh;
332	Elf_Phdr *ph = NULL;
333	u_long phsize;
334	char *bp = NULL;
335	Elf_Addr addr = *last;
336
337	bp = path;
338	/*
339         * 1. open file
340         * 2. read filehdr
341         * 3. map text, data, and bss out of it using VM_*
342         */
343	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
344	if ((error = namei(&nd)) != 0)
345		return error;
346#ifdef notyet /* XXX cgd 960926 */
347	XXX cgd 960926: check vnode type
348	XXX cgd 960926: check mount point for MNT_NOEXEC
349	XXX cgd 960926: check VOP_ACCESS on it.
350	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
351#endif
352	VOP_UNLOCK(nd.ni_vp);
353
354	if ((error = ELFNAME(read_from)(p, nd.ni_vp, 0, (caddr_t) &eh,
355				    sizeof(eh))) != 0)
356		goto bad;
357
358	if ((error = ELFNAME(check_header)(&eh, Elf_et_dyn)) != 0)
359		goto bad;
360
361	phsize = eh.e_phnum * sizeof(Elf_Phdr);
362	ph = (Elf_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
363
364	if ((error = ELFNAME(read_from)(p, nd.ni_vp, eh.e_phoff,
365	    (caddr_t) ph, phsize)) != 0)
366		goto bad;
367
368	/*
369         * Load all the necessary sections
370         */
371	for (i = 0; i < eh.e_phnum; i++) {
372		u_long size = 0;
373		int prot = 0;
374
375		switch (ph[i].p_type) {
376		case Elf_pt_load:
377			ELFNAME(load_psection)(vcset, nd.ni_vp, &ph[i], &addr,
378						&size, &prot);
379			/* If entry is within this section it must be text */
380			if (eh.e_entry >= ph[i].p_vaddr &&
381			    eh.e_entry < (ph[i].p_vaddr + size)) {
382				*entry = addr + eh.e_entry;
383				ap->arg_interp = addr;
384			}
385			addr += size;
386			break;
387
388		case Elf_pt_dynamic:
389		case Elf_pt_phdr:
390		case Elf_pt_note:
391			break;
392
393		default:
394			break;
395		}
396	}
397
398	free((char *) ph, M_TEMP);
399	*last = addr;
400	vrele(nd.ni_vp);
401	return 0;
402
403bad:
404	if (ph != NULL)
405		free((char *) ph, M_TEMP);
406#ifdef notyet /* XXX cgd 960926 */
407	(maybe) VOP_CLOSE it
408#endif
409	vrele(nd.ni_vp);
410	return error;
411}
412
413/*
414 * exec_elf_makecmds(): Prepare an Elf binary's exec package
415 *
416 * First, set of the various offsets/lengths in the exec package.
417 *
418 * Then, mark the text image busy (so it can be demand paged) or error
419 * out if this is not possible.  Finally, set up vmcmds for the
420 * text, data, bss, and stack segments.
421 */
422int
423ELFNAME2(exec,makecmds)(p, epp)
424	struct proc *p;
425	struct exec_package *epp;
426{
427	Elf_Ehdr *eh = epp->ep_hdr;
428	Elf_Phdr *ph, *pp;
429	Elf_Addr phdr = 0, pos = 0;
430	int error, i, n, nload;
431	char interp[MAXPATHLEN];
432	u_long phsize;
433
434	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
435		return ENOEXEC;
436
437	if (ELFNAME(check_header)(eh, Elf_et_exec))
438		return ENOEXEC;
439
440	/*
441         * check if vnode is in open for writing, because we want to
442         * demand-page out of it.  if it is, don't do it, for various
443         * reasons
444         */
445	if (epp->ep_vp->v_writecount != 0) {
446#ifdef DIAGNOSTIC
447		if (epp->ep_vp->v_flag & VTEXT)
448			panic("exec: a VTEXT vnode has writecount != 0\n");
449#endif
450		return ETXTBSY;
451	}
452	/*
453         * Allocate space to hold all the program headers, and read them
454         * from the file
455         */
456	phsize = eh->e_phnum * sizeof(Elf_Phdr);
457	ph = (Elf_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
458
459	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
460	    (caddr_t) ph, phsize)) != 0)
461		goto bad;
462
463	epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
464	epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
465
466	interp[0] = '\0';
467
468	for (i = 0; i < eh->e_phnum; i++) {
469		pp = &ph[i];
470		if (pp->p_type == Elf_pt_interp) {
471			if (pp->p_filesz >= sizeof(interp))
472				goto bad;
473			if ((error = ELFNAME(read_from)(p, epp->ep_vp, pp->p_offset,
474				      (caddr_t) interp, pp->p_filesz)) != 0)
475				goto bad;
476			break;
477		}
478	}
479
480	/*
481	 * Setup things for native emulation.
482	 */
483	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
484	pos = ELFDEFNNAME(NO_ADDR);
485
486	/*
487	 * On the same architecture, we may be emulating different systems.
488	 * See which one will accept this executable. This currently only
489	 * applies to Linux and SVR4 on the i386.
490	 *
491	 * Probe functions would normally see if the interpreter (if any)
492	 * exists. Emulation packages may possibly replace the interpreter in
493	 * interp[] with a changed path (/emul/xxx/<path>), and also
494	 * set the ep_emul field in the exec package structure.
495	 */
496	if ((n = sizeof ELFNAME(probe_funcs) / sizeof ELFNAME(probe_funcs)[0])) {
497		error = ENOEXEC;
498		for (i = 0; i < n && error; i++)
499			error = ELFNAME(probe_funcs)[i](p, epp, eh, interp, &pos);
500
501#ifdef notyet
502		/*
503		 * We should really use a signature in our native binaries
504		 * and have our own probe function for matching binaries,
505		 * before trying the emulations. For now, if the emulation
506		 * probes failed we default to native.
507		 */
508		if (error)
509			goto bad;
510#endif
511	}
512
513	/*
514         * Load all the necessary sections
515         */
516	for (i = nload = 0; i < eh->e_phnum; i++) {
517		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
518		u_long size = 0;
519		int prot = 0;
520
521		pp = &ph[i];
522
523		switch (ph[i].p_type) {
524		case Elf_pt_load:
525			/*
526			 * XXX
527			 * Can handle only 2 sections: text and data
528			 */
529			if (nload++ == 2)
530				goto bad;
531			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
532				&ph[i], &addr, &size, &prot);
533			/*
534			 * Decide whether it's text or data by looking
535			 * at the entry point.
536			 */
537			if (eh->e_entry >= addr && eh->e_entry < (addr + size)){
538				epp->ep_taddr = addr;
539				epp->ep_tsize = size;
540			} else {
541				epp->ep_daddr = addr;
542				epp->ep_dsize = size;
543			}
544			break;
545
546		case Elf_pt_shlib:
547			error = ENOEXEC;
548			goto bad;
549
550		case Elf_pt_interp:
551			/* Already did this one */
552		case Elf_pt_dynamic:
553		case Elf_pt_note:
554			break;
555
556		case Elf_pt_phdr:
557			/* Note address of program headers (in text segment) */
558			phdr = pp->p_vaddr;
559			break;
560
561		default:
562			/*
563			 * Not fatal; we don't need to understand everything.
564			 */
565			break;
566		}
567	}
568
569	/*
570	 * If no position to load the interpreter was set by a probe
571	 * function, pick the same address that a non-fixed mmap(0, ..)
572	 * would (i.e. something safely out of the way).
573	 */
574	if (pos == ELFDEFNNAME(NO_ADDR) &&
575	    epp->ep_emul == &ELFNAMEEND(emul_netbsd))
576		pos = round_page(epp->ep_daddr + MAXDSIZ);
577
578	/*
579         * Check if we found a dynamically linked binary and arrange to load
580         * it's interpreter
581         */
582	if (interp[0]) {
583		struct elf_args *ap;
584
585		ap = (struct elf_args *) malloc(sizeof(struct elf_args),
586						 M_TEMP, M_WAITOK);
587		if ((error = ELFNAME(load_file)(p, interp, &epp->ep_vmcmds,
588				&epp->ep_entry, ap, &pos)) != 0) {
589			free((char *) ap, M_TEMP);
590			goto bad;
591		}
592		pos += phsize;
593		ap->arg_phaddr = phdr;
594
595		ap->arg_phentsize = eh->e_phentsize;
596		ap->arg_phnum = eh->e_phnum;
597		ap->arg_entry = eh->e_entry;
598
599		epp->ep_emul_arg = ap;
600	} else
601		epp->ep_entry = eh->e_entry;
602
603#ifdef ELF_MAP_PAGE_ZERO
604	/* Dell SVR4 maps page zero, yeuch! */
605	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0,
606	    VM_PROT_READ);
607#endif
608	free((char *) ph, M_TEMP);
609	epp->ep_vp->v_flag |= VTEXT;
610	return exec_elf_setup_stack(p, epp);
611
612bad:
613	free((char *) ph, M_TEMP);
614	kill_vmcmds(&epp->ep_vmcmds);
615	return ENOEXEC;
616}
617