exec_elf32.c revision 1.10
1/*	$NetBSD: exec_elf32.c,v 1.10 1996/09/26 21:50:56 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
77int ELFNAME(check_header) __P((Elf_Ehdr *, int));
78int ELFNAME(load_file) __P((struct proc *, char *, struct exec_vmcmd_set *,
79			    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 */
196int
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, IO_NODELOCKED, 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 */
325int
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, UIO_SYSSPACE, path, p);
349	if ((error = namei(&nd)) != 0) {
350		return error;
351	}
352	if ((error = ELFNAME(read_from)(p, nd.ni_vp, 0, (caddr_t) &eh,
353				    sizeof(eh))) != 0)
354		goto bad;
355
356	if ((error = ELFNAME(check_header)(&eh, Elf_et_dyn)) != 0)
357		goto bad;
358
359	phsize = eh.e_phnum * sizeof(Elf_Phdr);
360	ph = (Elf_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
361
362	if ((error = ELFNAME(read_from)(p, nd.ni_vp, eh.e_phoff,
363	    (caddr_t) ph, phsize)) != 0)
364		goto bad;
365
366	/*
367         * Load all the necessary sections
368         */
369	for (i = 0; i < eh.e_phnum; i++) {
370		u_long size = 0;
371		int prot = 0;
372
373		switch (ph[i].p_type) {
374		case Elf_pt_load:
375			ELFNAME(load_psection)(vcset, nd.ni_vp, &ph[i], &addr,
376						&size, &prot);
377			/* If entry is within this section it must be text */
378			if (eh.e_entry >= ph[i].p_vaddr &&
379			    eh.e_entry < (ph[i].p_vaddr + size)) {
380				*entry = addr + eh.e_entry;
381				ap->arg_interp = addr;
382			}
383			addr += size;
384			break;
385
386		case Elf_pt_dynamic:
387		case Elf_pt_phdr:
388		case Elf_pt_note:
389			break;
390
391		default:
392			break;
393		}
394	}
395
396bad:
397	if (ph != NULL)
398		free((char *) ph, M_TEMP);
399
400	*last = addr;
401	vrele(nd.ni_vp);
402	return error;
403}
404
405/*
406 * exec_elf_makecmds(): Prepare an Elf binary's exec package
407 *
408 * First, set of the various offsets/lengths in the exec package.
409 *
410 * Then, mark the text image busy (so it can be demand paged) or error
411 * out if this is not possible.  Finally, set up vmcmds for the
412 * text, data, bss, and stack segments.
413 */
414int
415ELFNAME2(exec,makecmds)(p, epp)
416	struct proc *p;
417	struct exec_package *epp;
418{
419	Elf_Ehdr *eh = epp->ep_hdr;
420	Elf_Phdr *ph, *pp;
421	Elf_Addr phdr = 0, pos = 0;
422	int error, i, n, nload;
423	char interp[MAXPATHLEN];
424	u_long phsize;
425
426	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
427		return ENOEXEC;
428
429	if (ELFNAME(check_header)(eh, Elf_et_exec))
430		return ENOEXEC;
431
432	/*
433         * check if vnode is in open for writing, because we want to
434         * demand-page out of it.  if it is, don't do it, for various
435         * reasons
436         */
437	if (epp->ep_vp->v_writecount != 0) {
438#ifdef DIAGNOSTIC
439		if (epp->ep_vp->v_flag & VTEXT)
440			panic("exec: a VTEXT vnode has writecount != 0\n");
441#endif
442		return ETXTBSY;
443	}
444	/*
445         * Allocate space to hold all the program headers, and read them
446         * from the file
447         */
448	phsize = eh->e_phnum * sizeof(Elf_Phdr);
449	ph = (Elf_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
450
451	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
452	    (caddr_t) ph, phsize)) != 0)
453		goto bad;
454
455	epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
456	epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
457
458	interp[0] = '\0';
459
460	for (i = 0; i < eh->e_phnum; i++) {
461		pp = &ph[i];
462		if (pp->p_type == Elf_pt_interp) {
463			if (pp->p_filesz >= sizeof(interp))
464				goto bad;
465			if ((error = ELFNAME(read_from)(p, epp->ep_vp, pp->p_offset,
466				      (caddr_t) interp, pp->p_filesz)) != 0)
467				goto bad;
468			break;
469		}
470	}
471
472	/*
473	 * Setup things for native emulation.
474	 */
475	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
476	pos = ELFDEFNNAME(NO_ADDR);
477
478	/*
479	 * On the same architecture, we may be emulating different systems.
480	 * See which one will accept this executable. This currently only
481	 * applies to Linux and SVR4 on the i386.
482	 *
483	 * Probe functions would normally see if the interpreter (if any)
484	 * exists. Emulation packages may possibly replace the interpreter in
485	 * interp[] with a changed path (/emul/xxx/<path>), and also
486	 * set the ep_emul field in the exec package structure.
487	 */
488	if ((n = sizeof ELFNAME(probe_funcs) / sizeof ELFNAME(probe_funcs)[0])) {
489		error = ENOEXEC;
490		for (i = 0; i < n && error; i++)
491			error = ELFNAME(probe_funcs)[i](p, epp, eh, interp, &pos);
492
493#ifdef notyet
494		/*
495		 * We should really use a signature in our native binaries
496		 * and have our own probe function for matching binaries,
497		 * before trying the emulations. For now, if the emulation
498		 * probes failed we default to native.
499		 */
500		if (error)
501			goto bad;
502#endif
503	}
504
505	/*
506         * Load all the necessary sections
507         */
508	for (i = nload = 0; i < eh->e_phnum; i++) {
509		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
510		u_long size = 0;
511		int prot = 0;
512
513		pp = &ph[i];
514
515		switch (ph[i].p_type) {
516		case Elf_pt_load:
517			/*
518			 * XXX
519			 * Can handle only 2 sections: text and data
520			 */
521			if (nload++ == 2)
522				goto bad;
523			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
524				&ph[i], &addr, &size, &prot);
525			/*
526			 * Decide whether it's text or data by looking
527			 * at the entry point.
528			 */
529			if (eh->e_entry >= addr && eh->e_entry < (addr + size)){
530				epp->ep_taddr = addr;
531				epp->ep_tsize = size;
532			} else {
533				epp->ep_daddr = addr;
534				epp->ep_dsize = size;
535			}
536			break;
537
538		case Elf_pt_shlib:
539			error = ENOEXEC;
540			goto bad;
541
542		case Elf_pt_interp:
543			/* Already did this one */
544		case Elf_pt_dynamic:
545		case Elf_pt_note:
546			break;
547
548		case Elf_pt_phdr:
549			/* Note address of program headers (in text segment) */
550			phdr = pp->p_vaddr;
551			break;
552
553		default:
554			/*
555			 * Not fatal; we don't need to understand everything.
556			 */
557			break;
558		}
559	}
560
561	/*
562	 * If no position to load the interpreter was set by a probe
563	 * function, pick the same address that a non-fixed mmap(0, ..)
564	 * would (i.e. something safely out of the way).
565	 */
566	if (pos == ELFDEFNNAME(NO_ADDR) &&
567	    epp->ep_emul == &ELFNAMEEND(emul_netbsd))
568		pos = round_page(epp->ep_daddr + MAXDSIZ);
569
570	/*
571         * Check if we found a dynamically linked binary and arrange to load
572         * it's interpreter
573         */
574	if (interp[0]) {
575		struct elf_args *ap;
576
577		ap = (struct elf_args *) malloc(sizeof(struct elf_args),
578						 M_TEMP, M_WAITOK);
579		if ((error = ELFNAME(load_file)(p, interp, &epp->ep_vmcmds,
580				&epp->ep_entry, ap, &pos)) != 0) {
581			free((char *) ap, M_TEMP);
582			goto bad;
583		}
584		pos += phsize;
585		ap->arg_phaddr = phdr;
586
587		ap->arg_phentsize = eh->e_phentsize;
588		ap->arg_phnum = eh->e_phnum;
589		ap->arg_entry = eh->e_entry;
590
591		epp->ep_emul_arg = ap;
592	} else
593		epp->ep_entry = eh->e_entry;
594
595#ifdef ELF_MAP_PAGE_ZERO
596	/* Dell SVR4 maps page zero, yeuch! */
597	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0,
598	    VM_PROT_READ);
599#endif
600	free((char *) ph, M_TEMP);
601	epp->ep_vp->v_flag |= VTEXT;
602	return exec_elf_setup_stack(p, epp);
603
604bad:
605	free((char *) ph, M_TEMP);
606	kill_vmcmds(&epp->ep_vmcmds);
607	return ENOEXEC;
608}
609