netbsd32_exec_elf32.c revision 1.1 1 1.1 jdolecek /* $NetBSD: netbsd32_exec_elf32.c,v 1.1 2000/12/01 21:51:09 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.1 jdolecek * Copyright (c) 1998 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.1 jdolecek
35 1.1 jdolecek #define ELFSIZE 32
36 1.1 jdolecek
37 1.1 jdolecek #include <sys/param.h>
38 1.1 jdolecek #include <sys/systm.h>
39 1.1 jdolecek #include <sys/proc.h>
40 1.1 jdolecek #include <sys/malloc.h>
41 1.1 jdolecek #include <sys/vnode.h>
42 1.1 jdolecek #include <sys/exec.h>
43 1.1 jdolecek #include <sys/exec_elf.h>
44 1.1 jdolecek #include <sys/resourcevar.h>
45 1.1 jdolecek #include <sys/signal.h>
46 1.1 jdolecek #include <sys/signalvar.h>
47 1.1 jdolecek
48 1.1 jdolecek #include <compat/netbsd32/netbsd32.h>
49 1.1 jdolecek #include <compat/netbsd32/netbsd32_exec.h>
50 1.1 jdolecek
51 1.1 jdolecek #include <machine/frame.h>
52 1.1 jdolecek #include <machine/netbsd32_machdep.h>
53 1.1 jdolecek
54 1.1 jdolecek int netbsd32_copyinargs __P((struct exec_package *, struct ps_strings *,
55 1.1 jdolecek void *, size_t, const void *, const void *));
56 1.1 jdolecek
57 1.1 jdolecek extern int ELFNAME2(netbsd,signature) __P((struct proc *, struct exec_package *,
58 1.1 jdolecek Elf_Ehdr *));
59 1.1 jdolecek
60 1.1 jdolecek int
61 1.1 jdolecek ELFNAME2(netbsd32,probe)(p, epp, eh, itp, pos)
62 1.1 jdolecek struct proc *p;
63 1.1 jdolecek struct exec_package *epp;
64 1.1 jdolecek void *eh;
65 1.1 jdolecek char *itp;
66 1.1 jdolecek vaddr_t *pos;
67 1.1 jdolecek {
68 1.1 jdolecek int error;
69 1.1 jdolecek size_t i;
70 1.1 jdolecek const char *bp;
71 1.1 jdolecek
72 1.1 jdolecek if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
73 1.1 jdolecek return error;
74 1.1 jdolecek
75 1.1 jdolecek if (itp[0]) {
76 1.1 jdolecek if ((error = emul_find(p, NULL, epp->ep_esch->es_emul->e_path,
77 1.1 jdolecek itp, &bp, 0)) &&
78 1.1 jdolecek (error = emul_find(p, NULL, "", itp, &bp, 0)))
79 1.1 jdolecek return error;
80 1.1 jdolecek if ((error = copystr(bp, itp, MAXPATHLEN, &i)) != 0)
81 1.1 jdolecek return error;
82 1.1 jdolecek free((void *)bp, M_TEMP);
83 1.1 jdolecek }
84 1.1 jdolecek epp->ep_flags |= EXEC_32;
85 1.1 jdolecek *pos = ELFDEFNNAME(NO_ADDR);
86 1.1 jdolecek return 0;
87 1.1 jdolecek }
88 1.1 jdolecek
89 1.1 jdolecek /* round up and down to page boundaries. */
90 1.1 jdolecek #define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
91 1.1 jdolecek #define ELF_TRUNC(a, b) ((a) & ~((b) - 1))
92 1.1 jdolecek
93 1.1 jdolecek /*
94 1.1 jdolecek * Copy arguments onto the stack in the normal way, but add some
95 1.1 jdolecek * extra information in case of dynamic binding.
96 1.1 jdolecek */
97 1.1 jdolecek void *
98 1.1 jdolecek netbsd32_elf32_copyargs(pack, arginfo, stack, argp)
99 1.1 jdolecek struct exec_package *pack;
100 1.1 jdolecek struct ps_strings *arginfo;
101 1.1 jdolecek void *stack;
102 1.1 jdolecek void *argp;
103 1.1 jdolecek {
104 1.1 jdolecek size_t len;
105 1.1 jdolecek AuxInfo ai[ELF_AUX_ENTRIES], *a;
106 1.1 jdolecek struct elf_args *ap;
107 1.1 jdolecek
108 1.1 jdolecek stack = netbsd32_copyargs(pack, arginfo, stack, argp);
109 1.1 jdolecek if (!stack)
110 1.1 jdolecek return NULL;
111 1.1 jdolecek
112 1.1 jdolecek a = ai;
113 1.1 jdolecek
114 1.1 jdolecek /*
115 1.1 jdolecek * Push extra arguments on the stack needed by dynamically
116 1.1 jdolecek * linked binaries
117 1.1 jdolecek */
118 1.1 jdolecek if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
119 1.1 jdolecek
120 1.1 jdolecek a->a_type = AT_PHDR;
121 1.1 jdolecek a->a_v = ap->arg_phaddr;
122 1.1 jdolecek a++;
123 1.1 jdolecek
124 1.1 jdolecek a->a_type = AT_PHENT;
125 1.1 jdolecek a->a_v = ap->arg_phentsize;
126 1.1 jdolecek a++;
127 1.1 jdolecek
128 1.1 jdolecek a->a_type = AT_PHNUM;
129 1.1 jdolecek a->a_v = ap->arg_phnum;
130 1.1 jdolecek a++;
131 1.1 jdolecek
132 1.1 jdolecek a->a_type = AT_PAGESZ;
133 1.1 jdolecek a->a_v = NBPG;
134 1.1 jdolecek a++;
135 1.1 jdolecek
136 1.1 jdolecek a->a_type = AT_BASE;
137 1.1 jdolecek a->a_v = ap->arg_interp;
138 1.1 jdolecek a++;
139 1.1 jdolecek
140 1.1 jdolecek a->a_type = AT_FLAGS;
141 1.1 jdolecek a->a_v = 0;
142 1.1 jdolecek a++;
143 1.1 jdolecek
144 1.1 jdolecek a->a_type = AT_ENTRY;
145 1.1 jdolecek a->a_v = ap->arg_entry;
146 1.1 jdolecek a++;
147 1.1 jdolecek
148 1.1 jdolecek free((char *)ap, M_TEMP);
149 1.1 jdolecek pack->ep_emul_arg = NULL;
150 1.1 jdolecek }
151 1.1 jdolecek
152 1.1 jdolecek a->a_type = AT_NULL;
153 1.1 jdolecek a->a_v = 0;
154 1.1 jdolecek a++;
155 1.1 jdolecek
156 1.1 jdolecek len = (a - ai) * sizeof(AuxInfo);
157 1.1 jdolecek if (copyout(ai, stack, len))
158 1.1 jdolecek return NULL;
159 1.1 jdolecek stack = (caddr_t)stack + len;
160 1.1 jdolecek
161 1.1 jdolecek return stack;
162 1.1 jdolecek }
163