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