xref: /netbsd-src/sys/compat/linux/common/linux_exec_elf32.c (revision b5cd2489b2952ee409ede7b61183dba7ab0359ad)
1 /*	$NetBSD: linux_exec_elf32.c,v 1.53 2001/08/30 20:22:08 manu Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 1998, 2000, 2001 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, Eric Haszlakiewicz and
9  * Emmanuel Dreyfus.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * based on exec_aout.c, sunos_exec.c and svr4_exec.c
42  */
43 
44 #ifndef ELFSIZE
45 #define	ELFSIZE		32				/* XXX should die */
46 #endif
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/proc.h>
52 #include <sys/malloc.h>
53 #include <sys/namei.h>
54 #include <sys/vnode.h>
55 #include <sys/mount.h>
56 #include <sys/exec.h>
57 #include <sys/exec_elf.h>
58 
59 #include <sys/mman.h>
60 #include <sys/syscallargs.h>
61 
62 #include <machine/cpu.h>
63 #include <machine/reg.h>
64 
65 #include <compat/linux/common/linux_types.h>
66 #include <compat/linux/common/linux_signal.h>
67 #include <compat/linux/common/linux_util.h>
68 #include <compat/linux/common/linux_exec.h>
69 #include <compat/linux/common/linux_machdep.h>
70 
71 #include <compat/linux/linux_syscallargs.h>
72 #include <compat/linux/linux_syscall.h>
73 
74 static int ELFNAME2(linux,signature) __P((struct proc *, struct exec_package *,
75 	Elf_Ehdr *, char *));
76 #ifdef LINUX_GCC_SIGNATURE
77 static int ELFNAME2(linux,gcc_signature) __P((struct proc *p,
78 	struct exec_package *, Elf_Ehdr *));
79 #endif
80 #ifdef LINUX_ATEXIT_SIGNATURE
81 static int ELFNAME2(linux,atexit_signature) __P((struct proc *p,
82 	struct exec_package *, Elf_Ehdr *));
83 #endif
84 
85 #ifdef LINUX_ATEXIT_SIGNATURE
86 /*
87  * On the PowerPC, statically linked Linux binaries are not recognized
88  * by linux_signature nor by linux_gcc_signature. Fortunately, thoses
89  * binaries features a __libc_atexit ELF section. We therefore assume we
90  * have a Linux binary if we find this section.
91  */
92 static int
93 ELFNAME2(linux,atexit_signature)(p, epp, eh)
94 	struct proc *p;
95 	struct exec_package *epp;
96 	Elf_Ehdr *eh;
97 {
98 	size_t shsize;
99 	int	strndx;
100 	size_t i;
101 	static const char signature[] = "__libc_atexit";
102 	char* strtable;
103 	Elf_Shdr *sh;
104 
105 	int error;
106 
107 	/*
108 	 * load the section header table
109 	 */
110 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
111 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
112 	error = exec_read_from(p, epp->ep_vp, eh->e_shoff, sh, shsize);
113 	if (error)
114 		goto out;
115 
116 	/*
117 	 * Now let's find the string table. If it does not exists, give up.
118 	 */
119 	strndx = (int)(eh->e_shstrndx);
120 	if (strndx == SHN_UNDEF) {
121 		error = ENOEXEC;
122 		goto out;
123 	}
124 
125 	/*
126 	 * strndx is the index in section header table of the string table
127 	 * section get the whole string table in strtable, and then we get access to the names
128 	 * s->sh_name is the offset of the section name in strtable.
129 	 */
130 	strtable = malloc(sh[strndx].sh_size, M_TEMP, M_WAITOK);
131 	error = exec_read_from(p, epp->ep_vp, sh[strndx].sh_offset, strtable,
132 	    sh[strndx].sh_size);
133 	if (error)
134 		goto out;
135 
136 	for (i = 0; i < eh->e_shnum; i++) {
137 		Elf_Shdr *s = &sh[i];
138 		if (!memcmp((void*)(&(strtable[s->sh_name])), signature,
139 				sizeof(signature))) {
140 #ifdef DEBUG_LINUX
141 			printf("linux_atexit_sig=%s\n",&(strtable[s->sh_name]));
142 #endif
143 			error = 0;
144 			goto out;
145 		}
146 	}
147 	error = ENOEXEC;
148 
149 out:
150 	free(sh, M_TEMP);
151 	free(strtable, M_TEMP);
152 	return (error);
153 }
154 #endif
155 
156 #ifdef LINUX_GCC_SIGNATURE
157 /*
158  * Take advantage of the fact that all the linux binaries are compiled
159  * with gcc, and gcc sticks in the comment field a signature. Note that
160  * on SVR4 binaries, the gcc signature will follow the OS name signature,
161  * that will not be a problem. We don't bother to read in the string table,
162  * but we check all the progbits headers.
163  *
164  * XXX This only works in the i386.  On the alpha (at least)
165  * XXX we have the same gcc signature which incorrectly identifies
166  * XXX NetBSD binaries as Linux.
167  */
168 static int
169 ELFNAME2(linux,gcc_signature)(p, epp, eh)
170 	struct proc *p;
171 	struct exec_package *epp;
172 	Elf_Ehdr *eh;
173 {
174 	size_t shsize;
175 	size_t i;
176 	static const char signature[] = "\0GCC: (GNU) ";
177 	char buf[sizeof(signature) - 1];
178 	Elf_Shdr *sh;
179 	int error;
180 
181 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
182 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
183 	error = exec_read_from(p, epp->ep_vp, eh->e_shoff, sh, shsize);
184 	if (error)
185 		goto out;
186 
187 	for (i = 0; i < eh->e_shnum; i++) {
188 		Elf_Shdr *s = &sh[i];
189 
190 		/*
191 		 * Identify candidates for the comment header;
192 		 * Header cannot have a load address, or flags and
193 		 * it must be large enough.
194 		 */
195 		if (s->sh_type != SHT_PROGBITS ||
196 		    s->sh_addr != 0 ||
197 		    s->sh_flags != 0 ||
198 		    s->sh_size < sizeof(signature) - 1)
199 			continue;
200 
201 		error = exec_read_from(p, epp->ep_vp, s->sh_offset, buf,
202 		    sizeof(signature) - 1);
203 		if (error)
204 			continue;
205 
206 		/*
207 		 * error is 0, if the signatures match we are done.
208 		 */
209 #ifdef DEBUG_LINUX
210 		printf("linux_gcc_sig: sig=%s\n", buf);
211 #endif
212 		if (!memcmp(buf, signature, sizeof(signature) - 1)) {
213 			error = 0;
214 			goto out;
215 		}
216 	}
217 	error = ENOEXEC;
218 
219 out:
220 	free(sh, M_TEMP);
221 	return (error);
222 }
223 #endif
224 
225 static int
226 ELFNAME2(linux,signature)(p, epp, eh, itp)
227 	struct proc *p;
228 	struct exec_package *epp;
229 	Elf_Ehdr *eh;
230 	char *itp;
231 {
232 	size_t i;
233 	Elf_Phdr *ph;
234 	size_t phsize;
235 	int error;
236 
237 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
238 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
239 	error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize);
240 	if (error)
241 		goto out;
242 
243 	for (i = 0; i < eh->e_phnum; i++) {
244 		Elf_Phdr *ephp = &ph[i];
245 		Elf_Nhdr *np;
246 		u_int32_t *abi;
247 
248 		if (ephp->p_type != PT_NOTE ||
249 		    ephp->p_filesz > 1024 ||
250 		    ephp->p_filesz < sizeof(Elf_Nhdr) + 20)
251 			continue;
252 
253 		np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
254 		error = exec_read_from(p, epp->ep_vp, ephp->p_offset, np,
255 		    ephp->p_filesz);
256 		if (error)
257 			goto next;
258 
259 		if (np->n_type != ELF_NOTE_TYPE_ABI_TAG ||
260 		    np->n_namesz != ELF_NOTE_ABI_NAMESZ ||
261 		    np->n_descsz != ELF_NOTE_ABI_DESCSZ ||
262 		    memcmp((caddr_t)(np + 1), ELF_NOTE_ABI_NAME,
263 		    ELF_NOTE_ABI_NAMESZ))
264 			goto next;
265 
266 		/* Make sure the OS is Linux. */
267 		abi = (u_int32_t *)((caddr_t)np + sizeof(Elf_Nhdr) +
268 		    np->n_namesz);
269 		if (abi[0] == ELF_NOTE_ABI_OS_LINUX)
270 			error = 0;
271 		else
272 			error = ENOEXEC;
273 		free(np, M_TEMP);
274 		goto out;
275 
276 	next:
277 		free(np, M_TEMP);
278 		continue;
279 	}
280 
281 	/* Check for certain intepreter names. */
282 	if (itp[0]) {
283 		if (!strncmp(itp, "/lib/ld-linux", 13) ||
284 		    !strncmp(itp, "/lib/ld.so.", 11))
285 			error = 0;
286 		else
287 			error = ENOEXEC;
288 		goto out;
289 	}
290 
291 	error = ENOEXEC;
292 out:
293 	free(ph, M_TEMP);
294 	return (error);
295 }
296 
297 int
298 ELFNAME2(linux,probe)(p, epp, eh, itp, pos)
299 	struct proc *p;
300 	struct exec_package *epp;
301 	void *eh;
302 	char *itp;
303 	vaddr_t *pos;
304 {
305 	const char *bp;
306 	int error;
307 	size_t len;
308 
309 	if (((error = ELFNAME2(linux,signature)(p, epp, eh, itp)) != 0) &&
310 #ifdef LINUX_GCC_SIGNATURE
311 	    ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0) &&
312 #endif
313 #ifdef LINUX_ATEXIT_SIGNATURE
314 	    ((error = ELFNAME2(linux,atexit_signature)(p, epp, eh)) != 0) &&
315 #endif
316 	    1)
317 			return error;
318 
319 	if (itp[0]) {
320 		if ((error = emul_find(p, NULL, epp->ep_esch->es_emul->e_path,
321 		    itp, &bp, 0)))
322 			return error;
323 		if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
324 			return error;
325 		free((void *)bp, M_TEMP);
326 	}
327 	*pos = ELF_NO_ADDR;
328 #ifdef DEBUG_LINUX
329 	printf("linux_probe: returning 0\n");
330 #endif
331 	return 0;
332 }
333 
334