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