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