netbsd32_exec_elf32.c revision 1.23 1 /* $NetBSD: netbsd32_exec_elf32.c,v 1.23 2005/12/11 12:20:22 christos 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.23 2005/12/11 12:20:22 christos 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
77 #include <compat/netbsd32/netbsd32.h>
78 #include <compat/netbsd32/netbsd32_exec.h>
79
80 #include <machine/frame.h>
81 #include <machine/netbsd32_machdep.h>
82
83 int netbsd32_copyinargs(struct exec_package *, struct ps_strings *,
84 void *, size_t, const void *, const void *);
85 int ELFNAME2(netbsd32,probe_noteless)(struct lwp *, struct exec_package *epp,
86 void *eh, char *itp, vaddr_t *pos);
87 extern int ELFNAME2(netbsd,signature)(struct proc *, struct exec_package *,
88 Elf_Ehdr *);
89
90 int
91 ELFNAME2(netbsd32,probe)(struct lwp *l, struct exec_package *epp,
92 void *eh, char *itp, vaddr_t *pos)
93 {
94 int error;
95
96 if ((error = ELFNAME2(netbsd,signature)(l->l_proc, epp, eh)) != 0)
97 return error;
98
99 return ELFNAME2(netbsd32,probe_noteless)(l, epp, eh, itp, pos);
100 }
101
102 int
103 ELFNAME2(netbsd32,probe_noteless)(struct lwp *l, struct exec_package *epp,
104 void *eh, char *itp, vaddr_t *pos)
105 {
106 int error;
107
108 if (itp) {
109 /* Translate interpreter name if needed */
110 if ((error = emul_find_interp(l,
111 epp->ep_esch->es_emul->e_path, itp)) != 0)
112 return error;
113 }
114 epp->ep_flags |= EXEC_32;
115 epp->ep_vm_minaddr = VM_MIN_ADDRESS;
116 epp->ep_vm_maxaddr = USRSTACK32;
117 #ifdef ELF_INTERP_NON_RELOCATABLE
118 *pos = ELF_LINK_ADDR;
119 #endif
120 return 0;
121 }
122
123 /* round up and down to page boundaries. */
124 #define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
125 #define ELF_TRUNC(a, b) ((a) & ~((b) - 1))
126
127 /*
128 * Copy arguments onto the stack in the normal way, but add some
129 * extra information in case of dynamic binding.
130 */
131 int
132 netbsd32_elf32_copyargs(struct lwp *l, struct exec_package *pack,
133 struct ps_strings *arginfo, char **stackp, void *argp)
134 {
135 size_t len;
136 AuxInfo ai[ELF_AUX_ENTRIES], *a;
137 struct elf_args *ap;
138 struct proc *p;
139 int error;
140
141 p = l->l_proc;
142
143 if ((error = netbsd32_copyargs(l, pack, arginfo, stackp, argp)) != 0)
144 return error;
145
146 a = ai;
147
148 /*
149 * Push extra arguments on the stack needed by dynamically
150 * linked binaries
151 */
152 if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
153
154 a->a_type = AT_PHDR;
155 a->a_v = ap->arg_phaddr;
156 a++;
157
158 a->a_type = AT_PHENT;
159 a->a_v = ap->arg_phentsize;
160 a++;
161
162 a->a_type = AT_PHNUM;
163 a->a_v = ap->arg_phnum;
164 a++;
165
166 a->a_type = AT_PAGESZ;
167 a->a_v = PAGE_SIZE;
168 a++;
169
170 a->a_type = AT_BASE;
171 a->a_v = ap->arg_interp;
172 a++;
173
174 a->a_type = AT_FLAGS;
175 a->a_v = 0;
176 a++;
177
178 a->a_type = AT_ENTRY;
179 a->a_v = ap->arg_entry;
180 a++;
181
182 a->a_type = AT_EUID;
183 a->a_v = p->p_ucred->cr_uid;
184 a++;
185
186 a->a_type = AT_RUID;
187 a->a_v = p->p_cred->p_ruid;
188 a++;
189
190 a->a_type = AT_EGID;
191 a->a_v = p->p_ucred->cr_gid;
192 a++;
193
194 a->a_type = AT_RGID;
195 a->a_v = p->p_cred->p_rgid;
196 a++;
197
198 free((char *)ap, M_TEMP);
199 pack->ep_emul_arg = NULL;
200 }
201
202 a->a_type = AT_NULL;
203 a->a_v = 0;
204 a++;
205
206 len = (a - ai) * sizeof(AuxInfo);
207 if ((error = copyout(ai, *stackp, len)) != 0)
208 return error;
209 *stackp += len;
210
211 return 0;
212 }
213