xref: /netbsd-src/sys/compat/linux/common/linux_exec_elf32.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: linux_exec_elf32.c,v 1.40 1998/11/05 22:19:25 erh Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas, Frank van der Linden and Eric Haszlakiewicz.
9  *
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.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * based on exec_aout.c, sunos_exec.c and svr4_exec.c
41  */
42 
43 #ifndef ELFSIZE
44 #define	ELFSIZE		32				/* XXX should die */
45 #endif
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/proc.h>
51 #include <sys/malloc.h>
52 #include <sys/namei.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/exec.h>
56 #include <sys/exec_elf.h>
57 
58 #include <sys/mman.h>
59 #include <sys/syscallargs.h>
60 
61 #include <vm/vm.h>
62 #include <vm/vm_param.h>
63 #include <vm/vm_map.h>
64 
65 #include <machine/cpu.h>
66 #include <machine/reg.h>
67 
68 #include <compat/linux/common/linux_types.h>
69 #include <compat/linux/common/linux_signal.h>
70 #include <compat/linux/common/linux_util.h>
71 #include <compat/linux/common/linux_exec.h>
72 #include <compat/linux/common/linux_machdep.h>
73 #include <compat/linux/common/linux_errno.h>
74 
75 #include <compat/linux/linux_syscallargs.h>
76 #include <compat/linux/linux_syscall.h>
77 
78 static int ELFNAME2(linux,signature) __P((struct proc *, struct exec_package *,
79 	Elf_Ehdr *));
80 #ifdef LINUX_GCC_SIGNATURE
81 static int ELFNAME2(linux,gcc_signature) __P((struct proc *p,
82 	struct exec_package *, Elf_Ehdr *));
83 #endif
84 
85 #define LINUX_ELF_AUX_ARGSIZ (sizeof(AuxInfo) * 8 / sizeof(char *))
86 
87 
88 extern char linux_sigcode[], linux_esigcode[];
89 extern struct sysent linux_sysent[];
90 extern char *linux_syscallnames[];
91 
92 struct emul ELFNAMEEND(emul_linux) = {
93 	"linux",
94 	native_to_linux_errno,
95 	linux_sendsig,
96 	LINUX_SYS_syscall,
97 	LINUX_SYS_MAXSYSCALL,
98 	linux_sysent,
99 	linux_syscallnames,
100 	LINUX_ELF_AUX_ARGSIZ,
101 	ELFNAME(copyargs),
102 	linux_setregs,
103 	linux_sigcode,
104 	linux_esigcode,
105 };
106 
107 
108 #ifdef LINUX_GCC_SIGNATURE
109 /*
110  * Take advantage of the fact that all the linux binaries are compiled
111  * with gcc, and gcc sticks in the comment field a signature. Note that
112  * on SVR4 binaries, the gcc signature will follow the OS name signature,
113  * that will not be a problem. We don't bother to read in the string table,
114  * but we check all the progbits headers.
115  *
116  * XXX This only works in the i386.  On the alpha (at least)
117  * XXX we have the same gcc signature which incorrectly identifies
118  * XXX NetBSD binaries as Linux.
119  */
120 static int
121 ELFNAME2(linux,gcc_signature)(p, epp, eh)
122 	struct proc *p;
123 	struct exec_package *epp;
124 	Elf_Ehdr *eh;
125 {
126 	size_t shsize = sizeof(Elf_Shdr) * eh->e_shnum;
127 	size_t i;
128 	static const char signature[] = "\0GCC: (GNU) ";
129 	char buf[sizeof(signature) - 1];
130 	Elf_Shdr *sh;
131 	int error;
132 
133 	error = ENOEXEC;
134 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
135 
136 	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_shoff,
137 	    (caddr_t) sh, shsize)) != 0)
138 		goto out;
139 
140 	for (i = 0; i < eh->e_shnum; i++) {
141 		Elf_Shdr *s = &sh[i];
142 
143 		/*
144 		 * Identify candidates for the comment header;
145 		 * Header cannot have a load address, or flags and
146 		 * it must be large enough.
147 		 */
148 		if (s->sh_type != Elf_sht_progbits ||
149 		    s->sh_addr != 0 ||
150 		    s->sh_flags != 0 ||
151 		    s->sh_size < sizeof(signature) - 1)
152 			continue;
153 
154 		if ((error = ELFNAME(read_from)(p, epp->ep_vp, s->sh_offset,
155 		    (caddr_t) buf, sizeof(signature) - 1)) != 0)
156 			goto out;
157 
158 		/*
159 		 * error is 0, if the signatures match we are done.
160 		 */
161 #ifdef DEBUG_LINUX
162 		printf("linux_gcc_sig: sig=%s\n", buf);
163 #endif
164 		if (memcmp(buf, signature, sizeof(signature) - 1) == 0)
165 			goto out;
166 	}
167 
168 out:
169 	free(sh, M_TEMP);
170 #ifdef DEBUG_LINUX
171 	printf("linux_gcc_sig: returning %d\n", error);
172 #endif
173 	return error;
174 }
175 #endif
176 
177 static int
178 ELFNAME2(linux,signature)(p, epp, eh)
179 	struct proc *p;
180 	struct exec_package *epp;
181 	Elf_Ehdr *eh;
182 {
183 	size_t i;
184 	Elf_Phdr *ph;
185 	Elf_Note *notep;
186 	size_t phsize;
187 	int error = ENOEXEC;
188 
189 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
190 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
191 	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
192 					(caddr_t) ph, phsize)) != 0)
193 		goto out1;
194 
195 	for (i = 0; i < eh->e_phnum; i++) {
196 		Elf_Phdr *ephp = &ph[i];
197 		u_int32_t ostype;
198 
199 		if (ephp->p_type != Elf_pt_interp /* XAX pt_note */
200 #if 0
201 		    || ephp->p_flags != 0
202 		    || ephp->p_filesz < sizeof(Elf_Note))
203 #endif
204 		    )
205 			continue;
206 
207 		notep = (Elf_Note *)malloc(ephp->p_filesz+1, M_TEMP, M_WAITOK);
208 		if ((error = ELFNAME(read_from)(p, epp->ep_vp, ephp->p_offset,
209 					(caddr_t)notep, ephp->p_filesz)) != 0)
210 			goto out3;
211 
212 		/* Check for "linux" in the intepreter name. */
213 		if (ephp->p_filesz < 8 + 5) {
214 			error = ENOEXEC;
215 			goto out3;
216 		}
217 #ifdef DEBUG_LINUX
218 		printf("linux_signature: interp=%s\n", notep);
219 #endif
220 		if (strncmp(&((char *)notep)[8], "linux", 5) == 0 ||
221 		    strncmp((char *)notep, "/lib/ld.so.", 11) == 0) {
222 			error = 0;
223 			goto out3;
224 		}
225 
226 		goto out2;
227 
228 		/* XXX XAX Should handle NETBSD_TYPE_EMULNAME */
229 		if (notep->type != ELF_NOTE_TYPE_OSVERSION) {
230 			free(notep, M_TEMP);
231 			continue;
232 		}
233 
234 		/* Check the name and description sizes. */
235 		if (notep->namesz != ELF_NOTE_GNU_NAMESZ ||
236 		    notep->descsz != ELF_NOTE_GNU_DESCSZ)
237 			goto out2;
238 
239 		/* Is the name "GNU\0"? */
240 		if (memcmp((notep + sizeof(Elf_Note)),
241 			   ELF_NOTE_GNU_NAME, ELF_NOTE_GNU_NAMESZ))
242 			goto out2;
243 
244 		/* Make sure the OS is Linux */
245 		ostype = (u_int32_t)(*((u_int32_t *)notep + sizeof(Elf_Note) +
246 		    notep->namesz)) & ELF_NOTE_GNU_OSMASK;
247 		if (ostype != ELF_NOTE_GNU_OSLINUX)
248 			goto out2;
249 
250 		/* All checks succeeded. */
251 		error = 0;
252 		goto out3;
253 	}
254 
255 	error = ENOEXEC;
256 
257 out1:
258 	free(ph, M_TEMP);
259 #ifdef DEBUG_LINUX
260 	printf("linux_signature: out1=%d\n", error);
261 #endif
262 	return error;
263 
264 out2:
265 	error = ENOEXEC;
266 out3:
267 	free(notep, M_TEMP);
268 	free(ph, M_TEMP);
269 #ifdef DEBUG_LINUX
270 	printf("linux_signature: out2,3=%d\n", error);
271 #endif
272 	return error;
273 }
274 
275 int
276 ELFNAME2(linux,probe)(p, epp, eh, itp, pos)
277 	struct proc *p;
278 	struct exec_package *epp;
279 	Elf_Ehdr *eh;
280 	char *itp;
281 	Elf_Addr *pos;
282 {
283 	char *bp;
284 	int error;
285 	size_t len;
286 
287 	if ((error = ELFNAME2(linux,signature)(p, epp, eh)) != 0)
288 #ifdef LINUX_GCC_SIGNATURE
289 		if ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0)
290 			return error;
291 #else
292 		return error;
293 #endif
294 
295 	if (itp[0]) {
296 		if ((error = emul_find(p, NULL, linux_emul_path, itp, &bp, 0)))
297 			return error;
298 		if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
299 			return error;
300 		free(bp, M_TEMP);
301 	}
302 	epp->ep_emul = &ELFNAMEEND(emul_linux);
303 	*pos = ELF_NO_ADDR;
304 #ifdef DEBUG_LINUX
305 	printf("linux_probe: returning 0\n");
306 #endif
307 	return 0;
308 }
309 
310