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