No empty .Rs/.Re
[netbsd-mini2440.git] / sys / compat / irix / irix_exec_elf32.c
blob57ad16e68b4b26c9a83293e7283d812aeb42664e
1 /* $NetBSD: irix_exec_elf32.c,v 1.15 2008/04/28 20:23:41 martin Exp $ */
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Emmanuel Dreyfus.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: irix_exec_elf32.c,v 1.15 2008/04/28 20:23:41 martin Exp $");
35 #ifndef ELFSIZE
36 #define ELFSIZE 32 /* XXX should die */
37 #endif
39 #include "opt_execfmt.h"
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/systm.h>
44 #include <sys/null.h>
45 #include <sys/malloc.h>
46 #include <sys/syslimits.h>
47 #include <sys/exec.h>
48 #include <sys/exec_elf.h>
49 #include <sys/errno.h>
50 #include <sys/namei.h> /* Used for irix_load_addr */
51 #include <sys/vnode.h> /* Used for irix_load_addr */
53 #include <machine/vmparam.h>
55 #include <uvm/uvm_extern.h>
57 #include <compat/common/compat_util.h>
59 #include <compat/irix/irix_exec.h>
61 #if ELFSIZE == 32
63 * IRIX o32 ABI probe function
65 int
66 ELFNAME2(irix,probe_o32)(struct lwp *l, struct exec_package *epp, void *eh,
67 char *itp, vaddr_t *pos)
69 int error = 0;
71 #ifdef DEBUG_IRIX
72 printf("irix_probe_o32()\n");
73 #endif
74 if ((((Elf_Ehdr *)epp->ep_hdr)->e_flags & IRIX_EF_IRIX_ABI_MASK) !=
75 IRIX_EF_IRIX_ABIO32)
76 return error;
78 if (itp) {
79 /* o32 binaries use /lib/libc.so.1 */
80 if (strncmp(itp, "/lib/libc.so", 12) &&
81 strncmp(itp, "/usr/lib/libc.so", 16))
82 return ENOEXEC;
83 if ((error = emul_find_interp(l, epp, itp)))
84 return error;
85 *pos = ELF_LINK_ADDR;
87 #ifdef DEBUG_IRIX
88 printf("irix_probe_o32: returning 0\n");
89 printf("epp->ep_vm_minaddr = 0x%lx\n", epp->ep_vm_minaddr);
90 #endif
91 epp->ep_vm_minaddr = epp->ep_vm_minaddr & ~0xfUL;
92 l->l_proc->p_md.md_abi = _MIPS_BSD_API_O32;
93 return 0;
97 * IRIX n32 ABI probe function
99 int
100 ELFNAME2(irix,probe_n32)(struct lwp *l, struct exec_package *epp, void *eh,
101 char *itp, vaddr_t *pos)
103 int error = 0;
105 #ifdef DEBUG_IRIX
106 printf("irix_probe_n32()\n");
107 #endif
108 if ((((Elf_Ehdr *)epp->ep_hdr)->e_flags & IRIX_EF_IRIX_ABI_MASK) !=
109 IRIX_EF_IRIX_ABIN32)
110 return error;
112 if (itp) {
113 /* n32 binaries use /lib32/libc.so.1 */
114 if (strncmp(itp, "/lib32/libc.so", 14) &&
115 strncmp(itp, "/usr/lib32/libc.so", 18))
116 return ENOEXEC;
117 if ((error = emul_find_interp(l, epp, itp)))
118 return error;
120 #ifdef DEBUG_IRIX
121 printf("irix_probe_n32: returning 0\n");
122 printf("epp->ep_vm_minaddr = 0x%lx\n", epp->ep_vm_minaddr);
123 #endif
124 epp->ep_vm_minaddr = epp->ep_vm_minaddr & ~0xfUL;
125 l->l_proc->p_md.md_abi = _MIPS_BSD_API_N32;
126 return 0;
128 #endif /* ELFSIZE == 32 */
130 #if ELFSIZE == 64
132 * IRIX n64 ABI probe function
135 irix_elf64_probe_n64(struct lwp *l, struct exec_package *epp, void *eh,
136 char *itp, vaddr_t *pos)
138 int error = 0;
140 #ifdef DEBUG_IRIX
141 printf("irix_probe_n64()\n");
142 #endif
143 if ((((Elf_Ehdr *)epp->ep_hdr)->e_flags & IRIX_EF_IRIX_ABI_MASK) !=
144 IRIX_EF_IRIX_ABI64)
145 return error;
147 if (itp) {
148 /* n32 binaries use /lib64/libc.so.1 */
149 if (strncmp(itp, "/lib64/libc.so", 14) &&
150 strncmp(itp, "/usr/lib64/libc.so", 18))
151 return ENOEXEC;
152 if ((error = emul_find_interp(l, epp, itp)))
153 return error;
155 #ifdef DEBUG_IRIX
156 printf("irix_probe_n32: returning 0\n");
157 printf("epp->ep_vm_minaddr = 0x%lx\n", epp->ep_vm_minaddr);
158 #endif
159 epp->ep_vm_minaddr = epp->ep_vm_minaddr & ~0xfUL;
160 l->l_proc->p_md.md_abi = _MIPS_BSD_API_N32;
161 return 0;
163 #endif /* ELFSIZE == 64 */
166 ELFNAME2(irix,copyargs)(l, pack, arginfo, stackp, argp)
167 struct lwp *l;
168 struct exec_package *pack;
169 struct ps_strings *arginfo;
170 char **stackp;
171 void *argp;
173 char **cpp, *dp, *sp;
174 size_t len;
175 void *nullp;
176 long argc, envc;
177 AuxInfo ai[IRIX_ELF_AUX_ENTRIES], *a;
178 struct elf_args *ap;
179 int error;
182 * IRIX seems to expect argc and **argv to be aligned on a
183 * 16 bytes boundary. It seems there is no other way of
184 * getting **argv aligned than duplicating and customizing
185 * the code that sets up the stack in copyargs():
187 #ifdef DEBUG_IRIX
188 printf("%s(): *stackp = %p\n", __func__, *stackp);
189 #endif
191 * This is borrowed from sys/kern/kern_exec.c:copyargs()
193 cpp = (char **)*stackp;
194 nullp = NULL;
195 argc = arginfo->ps_nargvstr;
196 envc = arginfo->ps_nenvstr;
197 if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0)
198 return error;
200 dp = (char *) (cpp + argc + envc + 2 + pack->ep_esch->es_arglen);
202 /* Align **argv on a 16 bytes boundary */
203 dp = (char *)(((unsigned long)dp + 0xf) & ~0xfUL);
205 sp = argp;
207 arginfo->ps_argvstr = cpp; /* remember location of argv for later */
209 for (; --argc >= 0; sp += len, dp += len) {
210 #ifdef DEBUG_IRIX
211 printf("irix_elf32_copyargs(): argc = %d, cpp = %p, dp = %p\n", (int)argc, cpp, dp);
212 #endif
213 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
214 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
215 return error;
218 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
219 return error;
221 arginfo->ps_envstr = cpp; /* remember location of envp for later */
223 for (; --envc >= 0; sp += len, dp += len)
224 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
225 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
226 return error;
228 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
229 return error;
231 *stackp = (char *)cpp;
234 * This is borrowed from sys/kern/exec_elf32.c:elf32_copyargs
236 a = ai;
239 * Push extra arguments on the stack needed by dynamically
240 * linked binaries
242 if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
244 a->a_type = AT_PHDR;
245 a->a_v = ap->arg_phaddr;
246 a++;
248 a->a_type = AT_PHENT;
249 a->a_v = ap->arg_phentsize;
250 a++;
252 a->a_type = AT_PHNUM;
253 a->a_v = ap->arg_phnum;
254 a++;
256 a->a_type = AT_ENTRY;
257 a->a_v = ap->arg_entry;
258 a++;
260 a->a_type = AT_BASE;
261 a->a_v = ap->arg_interp;
262 a++;
264 a->a_type = AT_PAGESZ;
265 a->a_v = PAGE_SIZE;
266 a++;
268 free((char *)ap, M_TEMP);
269 pack->ep_emul_arg = NULL;
272 a->a_type = AT_NULL;
273 a->a_v = 0;
274 a++;
276 len = (a - ai) * sizeof(AuxInfo);
277 if ((error = copyout(ai, *stackp, len)) != 0)
278 return error;
279 /* *stackp += len; */
280 #ifdef DEBUG_IRIX
281 printf("*stackp = %p\n", *stackp);
282 #endif
283 return 0;