linux_exec_elf32.c revision 1.51.2.7 1 /* $NetBSD: linux_exec_elf32.c,v 1.51.2.7 2002/09/17 21:19:01 nathanw 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 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: linux_exec_elf32.c,v 1.51.2.7 2002/09/17 21:19:01 nathanw Exp $");
46
47 #ifndef ELFSIZE
48 #define ELFSIZE 32 /* XXX should die */
49 #endif
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/proc.h>
55 #include <sys/malloc.h>
56 #include <sys/namei.h>
57 #include <sys/vnode.h>
58 #include <sys/mount.h>
59 #include <sys/exec.h>
60 #include <sys/exec_elf.h>
61
62 #include <sys/mman.h>
63 #include <sys/sa.h>
64 #include <sys/syscallargs.h>
65
66 #include <machine/cpu.h>
67 #include <machine/reg.h>
68
69 #include <compat/linux/common/linux_types.h>
70 #include <compat/linux/common/linux_signal.h>
71 #include <compat/linux/common/linux_util.h>
72 #include <compat/linux/common/linux_exec.h>
73 #include <compat/linux/common/linux_machdep.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 *, char *));
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 #ifdef LINUX_ATEXIT_SIGNATURE
85 static int ELFNAME2(linux,atexit_signature) __P((struct proc *p,
86 struct exec_package *, Elf_Ehdr *));
87 #endif
88
89 #ifdef DEBUG_LINUX
90 #define DPRINTF(a) uprintf a
91 #else
92 #define DPRINTF(a)
93 #endif
94
95 #ifdef LINUX_ATEXIT_SIGNATURE
96 /*
97 * On the PowerPC, statically linked Linux binaries are not recognized
98 * by linux_signature nor by linux_gcc_signature. Fortunately, thoses
99 * binaries features a __libc_atexit ELF section. We therefore assume we
100 * have a Linux binary if we find this section.
101 */
102 static int
103 ELFNAME2(linux,atexit_signature)(p, epp, eh)
104 struct proc *p;
105 struct exec_package *epp;
106 Elf_Ehdr *eh;
107 {
108 size_t shsize;
109 int strndx;
110 size_t i;
111 static const char signature[] = "__libc_atexit";
112 char* strtable;
113 Elf_Shdr *sh;
114
115 int error;
116
117 /*
118 * load the section header table
119 */
120 shsize = eh->e_shnum * sizeof(Elf_Shdr);
121 sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
122 error = exec_read_from(p, epp->ep_vp, eh->e_shoff, sh, shsize);
123 if (error)
124 goto out;
125
126 /*
127 * Now let's find the string table. If it does not exists, give up.
128 */
129 strndx = (int)(eh->e_shstrndx);
130 if (strndx == SHN_UNDEF) {
131 error = ENOEXEC;
132 goto out;
133 }
134
135 /*
136 * strndx is the index in section header table of the string table
137 * section get the whole string table in strtable, and then we get access to the names
138 * s->sh_name is the offset of the section name in strtable.
139 */
140 strtable = malloc(sh[strndx].sh_size, M_TEMP, M_WAITOK);
141 error = exec_read_from(p, epp->ep_vp, sh[strndx].sh_offset, strtable,
142 sh[strndx].sh_size);
143 if (error)
144 goto out;
145
146 for (i = 0; i < eh->e_shnum; i++) {
147 Elf_Shdr *s = &sh[i];
148 if (!memcmp((void*)(&(strtable[s->sh_name])), signature,
149 sizeof(signature))) {
150 DPRINTF(("linux_atexit_sig=%s\n",
151 &(strtable[s->sh_name])));
152 error = 0;
153 goto out;
154 }
155 }
156 error = ENOEXEC;
157
158 out:
159 free(sh, M_TEMP);
160 free(strtable, M_TEMP);
161 return (error);
162 }
163 #endif
164
165 #ifdef LINUX_GCC_SIGNATURE
166 /*
167 * Take advantage of the fact that all the linux binaries are compiled
168 * with gcc, and gcc sticks in the comment field a signature. Note that
169 * on SVR4 binaries, the gcc signature will follow the OS name signature,
170 * that will not be a problem. We don't bother to read in the string table,
171 * but we check all the progbits headers.
172 *
173 * XXX This only works in the i386. On the alpha (at least)
174 * XXX we have the same gcc signature which incorrectly identifies
175 * XXX NetBSD binaries as Linux.
176 */
177 static int
178 ELFNAME2(linux,gcc_signature)(p, epp, eh)
179 struct proc *p;
180 struct exec_package *epp;
181 Elf_Ehdr *eh;
182 {
183 size_t shsize;
184 size_t i;
185 static const char signature[] = "\0GCC: (GNU) ";
186 char buf[sizeof(signature) - 1];
187 Elf_Shdr *sh;
188 int error;
189
190 shsize = eh->e_shnum * sizeof(Elf_Shdr);
191 sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
192 error = exec_read_from(p, epp->ep_vp, eh->e_shoff, sh, shsize);
193 if (error)
194 goto out;
195
196 for (i = 0; i < eh->e_shnum; i++) {
197 Elf_Shdr *s = &sh[i];
198
199 /*
200 * Identify candidates for the comment header;
201 * Header cannot have a load address, or flags and
202 * it must be large enough.
203 */
204 if (s->sh_type != SHT_PROGBITS ||
205 s->sh_addr != 0 ||
206 s->sh_flags != 0 ||
207 s->sh_size < sizeof(signature) - 1)
208 continue;
209
210 error = exec_read_from(p, epp->ep_vp, s->sh_offset, buf,
211 sizeof(signature) - 1);
212 if (error)
213 continue;
214
215 /*
216 * error is 0, if the signatures match we are done.
217 */
218 DPRINTF(("linux_gcc_sig: sig=%s\n", buf));
219 if (!memcmp(buf, signature, sizeof(signature) - 1)) {
220 error = 0;
221 goto out;
222 }
223 }
224 error = ENOEXEC;
225
226 out:
227 free(sh, M_TEMP);
228 return (error);
229 }
230 #endif
231
232 static int
233 ELFNAME2(linux,signature)(p, epp, eh, itp)
234 struct proc *p;
235 struct exec_package *epp;
236 Elf_Ehdr *eh;
237 char *itp;
238 {
239 size_t i;
240 Elf_Phdr *ph;
241 size_t phsize;
242 int error;
243 static const char linux[] = "Linux";
244
245 if (eh->e_ident[EI_OSABI] == 3 ||
246 memcmp(&eh->e_ident[EI_ABIVERSION], linux, sizeof(linux)) == 0)
247 return 0;
248
249 phsize = eh->e_phnum * sizeof(Elf_Phdr);
250 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
251 error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize);
252 if (error)
253 goto out;
254
255 for (i = 0; i < eh->e_phnum; i++) {
256 Elf_Phdr *ephp = &ph[i];
257 Elf_Nhdr *np;
258 u_int32_t *abi;
259
260 if (ephp->p_type != PT_NOTE ||
261 ephp->p_filesz > 1024 ||
262 ephp->p_filesz < sizeof(Elf_Nhdr) + 20)
263 continue;
264
265 np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
266 error = exec_read_from(p, epp->ep_vp, ephp->p_offset, np,
267 ephp->p_filesz);
268 if (error)
269 goto next;
270
271 if (np->n_type != ELF_NOTE_TYPE_ABI_TAG ||
272 np->n_namesz != ELF_NOTE_ABI_NAMESZ ||
273 np->n_descsz != ELF_NOTE_ABI_DESCSZ ||
274 memcmp((caddr_t)(np + 1), ELF_NOTE_ABI_NAME,
275 ELF_NOTE_ABI_NAMESZ))
276 goto next;
277
278 /* Make sure the OS is Linux. */
279 abi = (u_int32_t *)((caddr_t)np + sizeof(Elf_Nhdr) +
280 np->n_namesz);
281 if (abi[0] == ELF_NOTE_ABI_OS_LINUX)
282 error = 0;
283 else
284 error = ENOEXEC;
285 free(np, M_TEMP);
286 goto out;
287
288 next:
289 free(np, M_TEMP);
290 continue;
291 }
292
293 /* Check for certain intepreter names. */
294 if (itp[0]) {
295 if (!strncmp(itp, "/lib/ld-linux", 13) ||
296 !strncmp(itp, "/lib/ld.so.", 11))
297 error = 0;
298 else
299 error = ENOEXEC;
300 goto out;
301 }
302
303 error = ENOEXEC;
304 out:
305 free(ph, M_TEMP);
306 return (error);
307 }
308
309 int
310 ELFNAME2(linux,probe)(p, epp, eh, itp, pos)
311 struct proc *p;
312 struct exec_package *epp;
313 void *eh;
314 char *itp;
315 vaddr_t *pos;
316 {
317 const char *bp;
318 int error;
319 size_t len;
320
321 if (((error = ELFNAME2(linux,signature)(p, epp, eh, itp)) != 0) &&
322 #ifdef LINUX_GCC_SIGNATURE
323 ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0) &&
324 #endif
325 #ifdef LINUX_ATEXIT_SIGNATURE
326 ((error = ELFNAME2(linux,atexit_signature)(p, epp, eh)) != 0) &&
327 #endif
328 1)
329 return error;
330
331 if (itp[0]) {
332 if ((error = emul_find(p, NULL, epp->ep_esch->es_emul->e_path,
333 itp, &bp, 0)))
334 return error;
335 if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
336 return error;
337 free((void *)bp, M_TEMP);
338 }
339 *pos = ELF_NO_ADDR;
340 DPRINTF(("linux_probe: returning 0\n"));
341 return 0;
342 }
343
344