xref: /netbsd-src/sys/compat/netbsd32/netbsd32_exec_elf32.c (revision deb6f0161a9109e7de9b519dc8dfb9478668dcdd)
1 /*	$NetBSD: netbsd32_exec_elf32.c,v 1.40 2016/08/06 15:13:13 maxv 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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Copyright (c) 1993, 1994 Christopher G. Demetriou
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  * 3. All advertising materials mentioning features or use of this software
42  *    must display the following acknowledgement:
43  *      This product includes software developed by Christopher G. Demetriou.
44  * 4. The name of the author may not be used to endorse or promote products
45  *    derived from this software without specific prior written permission
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57  */
58 
59 #include <sys/cdefs.h>
60 __KERNEL_RCSID(0, "$NetBSD: netbsd32_exec_elf32.c,v 1.40 2016/08/06 15:13:13 maxv Exp $");
61 
62 #define	ELFSIZE		32
63 
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/proc.h>
67 #include <sys/vnode.h>
68 #include <sys/exec.h>
69 #include <sys/exec_elf.h>
70 #include <sys/resourcevar.h>
71 #include <sys/signal.h>
72 #include <sys/signalvar.h>
73 #include <sys/kauth.h>
74 #include <sys/namei.h>
75 
76 #include <compat/common/compat_util.h>
77 
78 #include <compat/netbsd32/netbsd32.h>
79 #include <compat/netbsd32/netbsd32_exec.h>
80 
81 #include <machine/netbsd32_machdep.h>
82 
83 int ELFNAME2(netbsd32,probe_noteless)(struct lwp *, struct exec_package *epp,
84 				      void *eh, char *itp, vaddr_t *pos);
85 extern int ELFNAME2(netbsd,signature)(struct lwp *, struct exec_package *,
86 				      Elf_Ehdr *);
87 
88 int
89 ELFNAME2(netbsd32,probe)(struct lwp *l, struct exec_package *epp,
90 			 void *eh, char *itp, vaddr_t *pos)
91 {
92 	int error;
93 
94 	if ((error = ELFNAME2(netbsd,signature)(l, epp, eh)) != 0)
95 		return error;
96 
97 #ifdef ELF_MD_PROBE_FUNC
98 	if ((error = ELF_MD_PROBE_FUNC(l, epp, eh, itp, pos)) != 0)
99 		return error;
100 #elif defined(ELF_INTERP_NON_RELOCATABLE)
101 	*pos = ELF_LINK_ADDR;
102 #endif
103 
104 	return ELFNAME2(netbsd32,probe_noteless)(l, epp, eh, itp, pos);
105 }
106 
107 int
108 ELFNAME2(netbsd32,probe_noteless)(struct lwp *l, struct exec_package *epp,
109 				  void *eh, char *itp, vaddr_t *pos)
110 {
111  	if (itp && epp->ep_interp == NULL) {
112 		extern const char machine32[];
113 		(void)compat_elf_check_interp(epp, itp, machine32);
114 	}
115 #ifdef _LP64
116 	epp->ep_flags |= EXEC_32 | EXEC_FORCEAUX;
117 #endif
118 	epp->ep_vm_minaddr = exec_vm_minaddr(VM_MIN_ADDRESS);
119 	epp->ep_vm_maxaddr = USRSTACK32;
120 #ifdef ELF_INTERP_NON_RELOCATABLE
121 	*pos = ELF_LINK_ADDR;
122 #endif
123 	return 0;
124 }
125 
126 /*
127  * Copy arguments onto the stack in the normal way, but add some
128  * extra information in case of dynamic binding.
129  */
130 int
131 netbsd32_elf32_copyargs(struct lwp *l, struct exec_package *pack,
132     struct ps_strings *arginfo, char **stackp, void *argp)
133 {
134 	size_t len;
135 	AuxInfo ai[ELF_AUX_ENTRIES], *a;
136 	struct elf_args *ap;
137 	int error;
138 
139 	if ((error = netbsd32_copyargs(l, pack, arginfo, stackp, argp)) != 0)
140 		return error;
141 
142 	a = ai;
143 
144 	memset(ai, 0, sizeof(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 		exec_free_emul_arg(pack);
197 	}
198 
199 	a->a_type = AT_NULL;
200 	a->a_v = 0;
201 	a++;
202 
203 	len = (a - ai) * sizeof(AuxInfo);
204 	if ((error = copyout(ai, *stackp, len)) != 0)
205 		return error;
206 	*stackp += len;
207 
208 	return 0;
209 }
210