1 /*-
2 * Copyright (c) 1994 Sean Eric Fagan
3 * Copyright (c) 1994 Søren Schmidt
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * $FreeBSD: releng/5.0/sys/i386/ibcs2/imgact_coff.c 103767 2002-09-21 22:07:17Z jake $
30 */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/fcntl.h>
35 #include <sys/imgact.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mman.h>
40 #include <sys/mount.h>
41 #include <sys/namei.h>
42 #include <sys/vnode.h>
43
44 #include <vm/vm.h>
45 #include <vm/pmap.h>
46 #include <vm/vm_map.h>
47 #include <vm/vm_kern.h>
48 #include <vm/vm_extern.h>
49
50 #include <i386/ibcs2/coff.h>
51 #include <i386/ibcs2/ibcs2_util.h>
52
53 MODULE_DEPEND(coff, ibcs2, 1, 1, 1);
54
55 extern struct sysentvec ibcs2_svr3_sysvec;
56
57 static int coff_load_file(struct thread *td, char *name);
58 static int exec_coff_imgact(struct image_params *imgp);
59
60 static int load_coff_section(struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot);
61
62 static int
63 load_coff_section(struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset,
64 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
65 {
66 size_t map_len;
67 vm_offset_t map_offset;
68 vm_offset_t map_addr;
69 int error;
70 unsigned char *data_buf = 0;
71 size_t copy_len;
72
73 map_offset = trunc_page(offset);
74 map_addr = trunc_page((vm_offset_t)vmaddr);
75
76 if (memsz > filsz) {
77 /*
78 * We have the stupid situation that
79 * the section is longer than it is on file,
80 * which means it has zero-filled areas, and
81 * we have to work for it. Stupid iBCS!
82 */
83 map_len = trunc_page(offset + filsz) - trunc_page(map_offset);
84 } else {
85 /*
86 * The only stuff we care about is on disk, and we
87 * don't care if we map in more than is really there.
88 */
89 map_len = round_page(offset + filsz) - trunc_page(map_offset);
90 }
91
92 DPRINTF(("%s(%d): vm_mmap(&vmspace->vm_map, &0x%08lx, 0x%x, 0x%x, "
93 "VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED, vp, 0x%x)\n",
94 __FILE__, __LINE__, map_addr, map_len, prot, map_offset));
95
96 if ((error = vm_mmap(&vmspace->vm_map,
97 &map_addr,
98 map_len,
99 prot,
100 VM_PROT_ALL,
101 MAP_PRIVATE | MAP_FIXED,
102 (caddr_t) vp,
103 map_offset)) != 0)
104 return error;
105
106 if (memsz == filsz) {
107 /* We're done! */
108 return 0;
109 }
110
111 /*
112 * Now we have screwball stuff, to accomodate stupid COFF.
113 * We have to map the remaining bit of the file into the kernel's
114 * memory map, allocate some anonymous memory, copy that last
115 * bit into it, and then we're done. *sigh*
116 * For clean-up reasons, we actally map in the file last.
117 */
118
119 copy_len = (offset + filsz) - trunc_page(offset + filsz);
120 map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
121 map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
122
123 DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx,0x%x, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n", __FILE__, __LINE__, map_addr, map_len));
124
125 if (map_len != 0) {
126 error = vm_map_find(&vmspace->vm_map, NULL, 0, &map_addr,
127 map_len, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
128 if (error)
129 return error;
130 }
131
132 if ((error = vm_mmap(kernel_map,
133 (vm_offset_t *) &data_buf,
134 PAGE_SIZE,
135 VM_PROT_READ,
136 VM_PROT_READ,
137 0,
138 (caddr_t) vp,
139 trunc_page(offset + filsz))) != 0)
140 return error;
141
142 error = copyout(data_buf, (caddr_t) map_addr, copy_len);
143
144 if (vm_map_remove(kernel_map,
145 (vm_offset_t) data_buf,
146 (vm_offset_t) data_buf + PAGE_SIZE))
147 panic("load_coff_section vm_map_remove failed");
148
149 return error;
150 }
151
152 static int
153 coff_load_file(struct thread *td, char *name)
154 {
155 struct proc *p = td->td_proc;
156 struct vmspace *vmspace = p->p_vmspace;
157 int error;
158 struct nameidata nd;
159 struct vnode *vp;
160 struct vattr attr;
161 struct filehdr *fhdr;
162 struct aouthdr *ahdr;
163 struct scnhdr *scns;
164 char *ptr = 0;
165 int nscns;
166 unsigned long text_offset = 0, text_address = 0, text_size = 0;
167 unsigned long data_offset = 0, data_address = 0, data_size = 0;
168 unsigned long bss_size = 0;
169 int i;
170
171 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, UIO_SYSSPACE, name, td);
172
173 error = namei(&nd);
174 if (error)
175 return error;
176
177 vp = nd.ni_vp;
178 if (vp == NULL)
179 return ENOEXEC;
180
181 if (vp->v_writecount) {
182 error = ETXTBSY;
183 goto fail;
184 }
185
186 if ((error = VOP_GETATTR(vp, &attr, td->td_ucred, td)) != 0)
187 goto fail;
188
189 if ((vp->v_mount->mnt_flag & MNT_NOEXEC)
190 || ((attr.va_mode & 0111) == 0)
191 || (attr.va_type != VREG))
192 goto fail;
193
194 if (attr.va_size == 0) {
195 error = ENOEXEC;
196 goto fail;
197 }
198
199 if ((error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td)) != 0)
200 goto fail;
201
202 if ((error = VOP_OPEN(vp, FREAD, td->td_ucred, td)) != 0)
203 goto fail;
204
205 /*
206 * Lose the lock on the vnode. It's no longer needed, and must not
207 * exist for the pagefault paging to work below.
208 */
209 VOP_UNLOCK(vp, 0, td);
210
211 if ((error = vm_mmap(kernel_map,
212 (vm_offset_t *) &ptr,
213 PAGE_SIZE,
214 VM_PROT_READ,
215 VM_PROT_READ,
216 0,
217 (caddr_t) vp,
218 0)) != 0)
219 goto unlocked_fail;
220
221 fhdr = (struct filehdr *)ptr;
222
223 if (fhdr->f_magic != I386_COFF) {
224 error = ENOEXEC;
225 goto dealloc_and_fail;
226 }
227
228 nscns = fhdr->f_nscns;
229
230 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
231 /*
232 * XXX -- just fail. I'm so lazy.
233 */
234 error = ENOEXEC;
235 goto dealloc_and_fail;
236 }
237
238 ahdr = (struct aouthdr*)(ptr + sizeof(struct filehdr));
239
240 scns = (struct scnhdr*)(ptr + sizeof(struct filehdr)
241 + sizeof(struct aouthdr));
242
243 for (i = 0; i < nscns; i++) {
244 if (scns[i].s_flags & STYP_NOLOAD)
245 continue;
246 else if (scns[i].s_flags & STYP_TEXT) {
247 text_address = scns[i].s_vaddr;
248 text_size = scns[i].s_size;
249 text_offset = scns[i].s_scnptr;
250 }
251 else if (scns[i].s_flags & STYP_DATA) {
252 data_address = scns[i].s_vaddr;
253 data_size = scns[i].s_size;
254 data_offset = scns[i].s_scnptr;
255 } else if (scns[i].s_flags & STYP_BSS) {
256 bss_size = scns[i].s_size;
257 }
258 }
259
260 if ((error = load_coff_section(vmspace, vp, text_offset,
261 (caddr_t)(void *)(uintptr_t)text_address,
262 text_size, text_size,
263 VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
264 goto dealloc_and_fail;
265 }
266 if ((error = load_coff_section(vmspace, vp, data_offset,
267 (caddr_t)(void *)(uintptr_t)data_address,
268 data_size + bss_size, data_size,
269 VM_PROT_ALL)) != 0) {
270 goto dealloc_and_fail;
271 }
272
273 error = 0;
274
275 dealloc_and_fail:
276 if (vm_map_remove(kernel_map,
277 (vm_offset_t) ptr,
278 (vm_offset_t) ptr + PAGE_SIZE))
279 panic("%s vm_map_remove failed", __func__);
280
281 fail:
282 VOP_UNLOCK(vp, 0, td);
283 unlocked_fail:
284 NDFREE(&nd, NDF_ONLY_PNBUF);
285 vrele(nd.ni_vp);
286 return error;
287 }
288
289 static int
290 exec_coff_imgact(imgp)
291 struct image_params *imgp;
292 {
293 const struct filehdr *fhdr = (const struct filehdr*)imgp->image_header;
294 const struct aouthdr *ahdr;
295 const struct scnhdr *scns;
296 int i;
297 struct vmspace *vmspace;
298 int nscns;
299 int error;
300 unsigned long text_offset = 0, text_address = 0, text_size = 0;
301 unsigned long data_offset = 0, data_address = 0, data_size = 0;
302 unsigned long bss_size = 0;
303 caddr_t hole;
304 struct thread *td = curthread;
305
306 if (fhdr->f_magic != I386_COFF ||
307 !(fhdr->f_flags & F_EXEC)) {
308
309 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
310 return -1;
311 }
312
313 nscns = fhdr->f_nscns;
314 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
315 /*
316 * For now, return an error -- need to be able to
317 * read in all of the section structures.
318 */
319
320 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
321 return -1;
322 }
323
324 ahdr = (const struct aouthdr*)
325 ((const char*)(imgp->image_header) + sizeof(struct filehdr));
326 imgp->entry_addr = ahdr->entry;
327
328 scns = (const struct scnhdr*)
329 ((const char*)(imgp->image_header) + sizeof(struct filehdr) +
330 sizeof(struct aouthdr));
331
332 VOP_UNLOCK(imgp->vp, 0, td);
333
334 if ((error = exec_extract_strings(imgp)) != 0) {
335 DPRINTF(("%s(%d): return %d\n", __FILE__, __LINE__, error));
336 goto fail;
337 }
338
339 exec_new_vmspace(imgp, &ibcs2_svr3_sysvec);
340 vmspace = imgp->proc->p_vmspace;
341
342 for (i = 0; i < nscns; i++) {
343
344 DPRINTF(("i = %d, scns[i].s_name = %s, scns[i].s_vaddr = %08lx, "
345 "scns[i].s_scnptr = %d\n", i, scns[i].s_name,
346 scns[i].s_vaddr, scns[i].s_scnptr));
347 if (scns[i].s_flags & STYP_NOLOAD) {
348 /*
349 * A section that is not loaded, for whatever
350 * reason. It takes precedance over other flag
351 * bits...
352 */
353 continue;
354 } else if (scns[i].s_flags & STYP_TEXT) {
355 text_address = scns[i].s_vaddr;
356 text_size = scns[i].s_size;
357 text_offset = scns[i].s_scnptr;
358 } else if (scns[i].s_flags & STYP_DATA) {
359 /* .data section */
360 data_address = scns[i].s_vaddr;
361 data_size = scns[i].s_size;
362 data_offset = scns[i].s_scnptr;
363 } else if (scns[i].s_flags & STYP_BSS) {
364 /* .bss section */
365 bss_size = scns[i].s_size;
366 } else if (scns[i].s_flags & STYP_LIB) {
367 char *buf = 0;
368 int foff = trunc_page(scns[i].s_scnptr);
369 int off = scns[i].s_scnptr - foff;
370 int len = round_page(scns[i].s_size + PAGE_SIZE);
371 int j;
372
373 if ((error = vm_mmap(kernel_map,
374 (vm_offset_t *) &buf,
375 len,
376 VM_PROT_READ,
377 VM_PROT_READ,
378 0,
379 (caddr_t) imgp->vp,
380 foff)) != 0) {
381 error = ENOEXEC;
382 goto fail;
383 }
384 if(scns[i].s_size) {
385 char *libbuf;
386 int emul_path_len = strlen(ibcs2_emul_path);
387
388 libbuf = malloc(MAXPATHLEN + emul_path_len,
389 M_TEMP, M_WAITOK);
390 strcpy(libbuf, ibcs2_emul_path);
391
392 for (j = off; j < scns[i].s_size + off;) {
393 long stroff, nextoff;
394 char *libname;
395
396 nextoff = 4 * *(long *)(buf + j);
397 stroff = 4 * *(long *)(buf + j + sizeof(long));
398
399 libname = buf + j + stroff;
400 j += nextoff;
401
402 DPRINTF(("%s(%d): shared library %s\n",
403 __FILE__, __LINE__, libname));
404 strcpy(&libbuf[emul_path_len], libname);
405 /* XXXKSE only 1:1 in coff */ error = coff_load_file(
406 FIRST_THREAD_IN_PROC(imgp->proc), libbuf);
407 if (error)
408 error = coff_load_file(
409 FIRST_THREAD_IN_PROC(imgp->proc),
410 libname);
411 if (error)
412 break;
413 }
414 free(libbuf, M_TEMP);
415 }
416 if (vm_map_remove(kernel_map,
417 (vm_offset_t) buf,
418 (vm_offset_t) buf + len))
419 panic("exec_coff_imgact vm_map_remove failed");
420 if (error)
421 goto fail;
422 }
423 }
424 /*
425 * Map in .text now
426 */
427
428 DPRINTF(("%s(%d): load_coff_section(vmspace, "
429 "imgp->vp, %08lx, %08lx, 0x%x, 0x%x, 0x%x)\n",
430 __FILE__, __LINE__, text_offset, text_address,
431 text_size, text_size, VM_PROT_READ | VM_PROT_EXECUTE));
432 if ((error = load_coff_section(vmspace, imgp->vp,
433 text_offset,
434 (caddr_t)(void *)(uintptr_t)text_address,
435 text_size, text_size,
436 VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
437 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
438 goto fail;
439 }
440 /*
441 * Map in .data and .bss now
442 */
443
444
445 DPRINTF(("%s(%d): load_coff_section(vmspace, "
446 "imgp->vp, 0x%08lx, 0x%08lx, 0x%x, 0x%x, 0x%x)\n",
447 __FILE__, __LINE__, data_offset, data_address,
448 data_size + bss_size, data_size, VM_PROT_ALL));
449 if ((error = load_coff_section(vmspace, imgp->vp,
450 data_offset,
451 (caddr_t)(void *)(uintptr_t)data_address,
452 data_size + bss_size, data_size,
453 VM_PROT_ALL)) != 0) {
454
455 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
456 goto fail;
457 }
458
459 imgp->interpreted = 0;
460 imgp->proc->p_sysent = &ibcs2_svr3_sysvec;
461
462 vmspace->vm_tsize = round_page(text_size) >> PAGE_SHIFT;
463 vmspace->vm_dsize = round_page(data_size + bss_size) >> PAGE_SHIFT;
464 vmspace->vm_taddr = (caddr_t)(void *)(uintptr_t)text_address;
465 vmspace->vm_daddr = (caddr_t)(void *)(uintptr_t)data_address;
466
467 hole = (caddr_t)trunc_page((vm_offset_t)vmspace->vm_daddr) + ctob(vmspace->vm_dsize);
468
469
470 DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx, PAGE_SIZE, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n",
471 __FILE__, __LINE__, hole));
472 DPRINTF(("imgact: error = %d\n", error));
473
474 error = vm_map_find(&vmspace->vm_map, NULL, 0,
475 (vm_offset_t *) &hole, PAGE_SIZE, FALSE,
476 VM_PROT_ALL, VM_PROT_ALL, 0);
477
478 DPRINTF(("IBCS2: start vm_dsize = 0x%x, vm_daddr = 0x%x end = 0x%x\n",
479 ctob(vmspace->vm_dsize), vmspace->vm_daddr,
480 ctob(vmspace->vm_dsize) + vmspace->vm_daddr ));
481 DPRINTF(("%s(%d): returning successfully!\n", __FILE__, __LINE__));
482
483 fail:
484 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
485
486 return error;
487 }
488
489 /*
490 * Tell kern_execve.c about it, with a little help from the linker.
491 */
492 static struct execsw coff_execsw = { exec_coff_imgact, "coff" };
493 EXEC_SET(coff, coff_execsw);
Cache object: e599142776919f5f3b9b52d69e5759b7
|