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