1 /*	$NetBSD: netbsd32_exec_elf32.c,v 1.15 2002/12/01 11:02:11 agc Exp $	*/
2 /*	from: NetBSD: exec_aout.c,v 1.15 1996/09/26 23:34:46 cgd Exp */
3 
4 /*
5  * Copyright (c) 1998, 2001 Matthew R. Green.
6  * Copyright (c) 1993, 1994 Christopher G. Demetriou
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Christopher G. Demetriou.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: netbsd32_exec_elf32.c,v 1.15 2002/12/01 11:02:11 agc Exp $");
37 
38 #define	ELFSIZE		32
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/malloc.h>
44 #include <sys/vnode.h>
45 #include <sys/exec.h>
46 #include <sys/exec_elf.h>
47 #include <sys/resourcevar.h>
48 #include <sys/signal.h>
49 #include <sys/signalvar.h>
50 
51 #include <compat/netbsd32/netbsd32.h>
52 #include <compat/netbsd32/netbsd32_exec.h>
53 
54 #include <machine/frame.h>
55 #include <machine/netbsd32_machdep.h>
56 
57 int netbsd32_copyinargs(struct exec_package *, struct ps_strings *,
58 			void *, size_t, const void *, const void *);
59 int ELFNAME2(netbsd32,probe_noteless)(struct proc *, struct exec_package *epp,
60 				      void *eh, char *itp, vaddr_t *pos);
61 extern int ELFNAME2(netbsd,signature)(struct proc *, struct exec_package *,
62 				      Elf_Ehdr *);
63 
64 int
65 ELFNAME2(netbsd32,probe)(struct proc *p, struct exec_package *epp,
66 			 void *eh, char *itp, vaddr_t *pos)
67 {
68 	int error;
69 
70 	if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
71 		return error;
72 
73 	return ELFNAME2(netbsd32,probe_noteless)(p, epp, eh, itp, pos);
74 }
75 
76 int
77 ELFNAME2(netbsd32,probe_noteless)(struct proc *p, struct exec_package *epp,
78 				  void *eh, char *itp, vaddr_t *pos)
79 {
80 	int error;
81 
82 	if (itp[0]) {
83 		/* Translate interpreter name if needed */
84 		if ((error = emul_find_interp(p, epp->ep_esch->es_emul->e_path,
85 		    itp)) != 0)
86 			return error;
87 	}
88 	epp->ep_flags |= EXEC_32;
89 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
90 	epp->ep_vm_maxaddr = USRSTACK32;
91 	*pos = ELFDEFNNAME(NO_ADDR);
92 	return 0;
93 }
94 
95 /* round up and down to page boundaries. */
96 #define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
97 #define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
98 
99 /*
100  * Copy arguments onto the stack in the normal way, but add some
101  * extra information in case of dynamic binding.
102  */
103 int
104 netbsd32_elf32_copyargs(struct proc *p, struct exec_package *pack,
105     struct ps_strings *arginfo, char **stackp, void *argp)
106 {
107 	size_t len;
108 	AuxInfo ai[ELF_AUX_ENTRIES], *a;
109 	struct elf_args *ap;
110 	int error;
111 
112 	if ((error = netbsd32_copyargs(p, pack, arginfo, stackp, argp)) != 0)
113 		return error;
114 
115 	a = ai;
116 
117 	/*
118 	 * Push extra arguments on the stack needed by dynamically
119 	 * linked binaries
120 	 */
121 	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
122 
123 		a->a_type = AT_PHDR;
124 		a->a_v = ap->arg_phaddr;
125 		a++;
126 
127 		a->a_type = AT_PHENT;
128 		a->a_v = ap->arg_phentsize;
129 		a++;
130 
131 		a->a_type = AT_PHNUM;
132 		a->a_v = ap->arg_phnum;
133 		a++;
134 
135 		a->a_type = AT_PAGESZ;
136 		a->a_v = NBPG;
137 		a++;
138 
139 		a->a_type = AT_BASE;
140 		a->a_v = ap->arg_interp;
141 		a++;
142 
143 		a->a_type = AT_FLAGS;
144 		a->a_v = 0;
145 		a++;
146 
147 		a->a_type = AT_ENTRY;
148 		a->a_v = ap->arg_entry;
149 		a++;
150 
151 		a->a_type = AT_EUID;
152 		a->a_v = p->p_ucred->cr_uid;
153 		a++;
154 
155 		a->a_type = AT_RUID;
156 		a->a_v = p->p_cred->p_ruid;
157 		a++;
158 
159 		a->a_type = AT_EGID;
160 		a->a_v = p->p_ucred->cr_gid;
161 		a++;
162 
163 		a->a_type = AT_RGID;
164 		a->a_v = p->p_cred->p_rgid;
165 		a++;
166 
167 		free((char *)ap, M_TEMP);
168 		pack->ep_emul_arg = NULL;
169 	}
170 
171 	a->a_type = AT_NULL;
172 	a->a_v = 0;
173 	a++;
174 
175 	len = (a - ai) * sizeof(AuxInfo);
176 	if ((error = copyout(ai, *stackp, len)) != 0)
177 		return error;
178 	*stackp += len;
179 
180 	return 0;
181 }
182