linux_exec_elf32.c revision 1.42.2.4 1 /* $NetBSD: linux_exec_elf32.c,v 1.42.2.4 2001/01/05 17:35:26 bouyer Exp $ */
2
3 /*-
4 * Copyright (c) 1995, 1998, 2000 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 <machine/cpu.h>
62 #include <machine/reg.h>
63
64 #include <compat/linux/common/linux_types.h>
65 #include <compat/linux/common/linux_signal.h>
66 #include <compat/linux/common/linux_util.h>
67 #include <compat/linux/common/linux_exec.h>
68 #include <compat/linux/common/linux_machdep.h>
69
70 #include <compat/linux/linux_syscallargs.h>
71 #include <compat/linux/linux_syscall.h>
72
73 static int ELFNAME2(linux,signature) __P((struct proc *, struct exec_package *,
74 Elf_Ehdr *, char *));
75 #ifdef LINUX_GCC_SIGNATURE
76 static int ELFNAME2(linux,gcc_signature) __P((struct proc *p,
77 struct exec_package *, Elf_Ehdr *));
78 #endif
79
80 #ifdef LINUX_GCC_SIGNATURE
81 /*
82 * Take advantage of the fact that all the linux binaries are compiled
83 * with gcc, and gcc sticks in the comment field a signature. Note that
84 * on SVR4 binaries, the gcc signature will follow the OS name signature,
85 * that will not be a problem. We don't bother to read in the string table,
86 * but we check all the progbits headers.
87 *
88 * XXX This only works in the i386. On the alpha (at least)
89 * XXX we have the same gcc signature which incorrectly identifies
90 * XXX NetBSD binaries as Linux.
91 */
92 static int
93 ELFNAME2(linux,gcc_signature)(p, epp, eh)
94 struct proc *p;
95 struct exec_package *epp;
96 Elf_Ehdr *eh;
97 {
98 size_t shsize;
99 size_t i;
100 static const char signature[] = "\0GCC: (GNU) ";
101 char buf[sizeof(signature) - 1];
102 Elf_Shdr *sh;
103 int error;
104
105 shsize = eh->e_shnum * sizeof(Elf_Shdr);
106 sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
107 error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_shoff, (caddr_t)sh,
108 shsize);
109 if (error)
110 goto out;
111
112 for (i = 0; i < eh->e_shnum; i++) {
113 Elf_Shdr *s = &sh[i];
114
115 /*
116 * Identify candidates for the comment header;
117 * Header cannot have a load address, or flags and
118 * it must be large enough.
119 */
120 if (s->sh_type != SHT_PROGBITS ||
121 s->sh_addr != 0 ||
122 s->sh_flags != 0 ||
123 s->sh_size < sizeof(signature) - 1)
124 continue;
125
126 error = ELFNAME(read_from)(p, epp->ep_vp, s->sh_offset,
127 (caddr_t)buf, sizeof(signature) - 1);
128 if (error)
129 continue;
130
131 /*
132 * error is 0, if the signatures match we are done.
133 */
134 #ifdef DEBUG_LINUX
135 printf("linux_gcc_sig: sig=%s\n", buf);
136 #endif
137 if (!memcmp(buf, signature, sizeof(signature) - 1)) {
138 error = 0;
139 goto out;
140 }
141 }
142 error = ENOEXEC;
143
144 out:
145 free(sh, M_TEMP);
146 return (error);
147 }
148 #endif
149
150 static int
151 ELFNAME2(linux,signature)(p, epp, eh, itp)
152 struct proc *p;
153 struct exec_package *epp;
154 Elf_Ehdr *eh;
155 char *itp;
156 {
157 size_t i;
158 Elf_Phdr *ph;
159 size_t phsize;
160 int error;
161
162 phsize = eh->e_phnum * sizeof(Elf_Phdr);
163 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
164 error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, (caddr_t)ph,
165 phsize);
166 if (error)
167 goto out;
168
169 for (i = 0; i < eh->e_phnum; i++) {
170 Elf_Phdr *ephp = &ph[i];
171 Elf_Nhdr *np;
172 u_int32_t *abi;
173
174 if (ephp->p_type != PT_NOTE ||
175 ephp->p_filesz > 1024 ||
176 ephp->p_filesz < sizeof(Elf_Nhdr) + 20)
177 continue;
178
179 np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
180 error = ELFNAME(read_from)(p, epp->ep_vp, ephp->p_offset,
181 (caddr_t)np, ephp->p_filesz);
182 if (error)
183 goto next;
184
185 if (np->n_type != ELF_NOTE_TYPE_ABI_TAG ||
186 np->n_namesz != ELF_NOTE_ABI_NAMESZ ||
187 np->n_descsz != ELF_NOTE_ABI_DESCSZ ||
188 memcmp((caddr_t)(np + 1), ELF_NOTE_ABI_NAME,
189 ELF_NOTE_ABI_NAMESZ))
190 goto next;
191
192 /* Make sure the OS is Linux. */
193 abi = (u_int32_t *)((caddr_t)np + sizeof(Elf_Nhdr) +
194 np->n_namesz);
195 if (abi[0] == ELF_NOTE_ABI_OS_LINUX)
196 error = 0;
197 else
198 error = ENOEXEC;
199 free(np, M_TEMP);
200 goto out;
201
202 next:
203 free(np, M_TEMP);
204 continue;
205 }
206
207 /* Check for certain intepreter names. */
208 if (itp[0]) {
209 if (!strncmp(itp, "/lib/ld-linux", 13) ||
210 !strncmp(itp, "/lib/ld.so.", 11))
211 error = 0;
212 else
213 error = ENOEXEC;
214 goto out;
215 }
216
217 error = ENOEXEC;
218 out:
219 free(ph, M_TEMP);
220 return (error);
221 }
222
223 int
224 ELFNAME2(linux,probe)(p, epp, eh, itp, pos)
225 struct proc *p;
226 struct exec_package *epp;
227 void *eh;
228 char *itp;
229 vaddr_t *pos;
230 {
231 const char *bp;
232 int error;
233 size_t len;
234
235 if ((error = ELFNAME2(linux,signature)(p, epp, eh, itp)) != 0)
236 #ifdef LINUX_GCC_SIGNATURE
237 if ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0)
238 return error;
239 #else
240 return error;
241 #endif
242
243 if (itp[0]) {
244 if ((error = emul_find(p, NULL, epp->ep_esch->es_emul->e_path,
245 itp, &bp, 0)))
246 return error;
247 if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
248 return error;
249 free((void *)bp, M_TEMP);
250 }
251 *pos = ELF_NO_ADDR;
252 #ifdef DEBUG_LINUX
253 printf("linux_probe: returning 0\n");
254 #endif
255 return 0;
256 }
257
258