imgact_coff.c revision 11397
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 withough 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 *	$Id: imgact_coff.c,v 1.7 1995/09/13 02:12:51 sef Exp $
30 */
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/resourcevar.h>
35#include <sys/exec.h>
36#include <sys/mman.h>
37#include <sys/imgact.h>
38#include <sys/kernel.h>
39#include <sys/file.h>
40#include <sys/malloc.h>
41#include <sys/mount.h>
42#include <sys/sysent.h>
43#include <vm/vm.h>
44#include <vm/vm_kern.h>
45
46#include <i386/ibcs2/coff.h>
47#include <i386/ibcs2/ibcs2_util.h>
48
49extern struct sysentvec ibcs2_svr3_sysvec;
50
51extern int coff_load_file __P((struct proc *p, char *name));
52extern int exec_coff_imgact __P((struct image_params *iparams));
53
54static int load_coff_section __P((struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot));
55
56
57static int
58load_coff_section(vmspace, vp, offset, vmaddr, memsz, filsz, prot)
59	struct vmspace *vmspace;
60	struct vnode *vp;
61	vm_offset_t offset;
62	caddr_t vmaddr;
63	size_t memsz, filsz;
64	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(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_FILE | 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_FILE | MAP_PRIVATE | MAP_FIXED,
102			     (caddr_t) vp,
103			     map_offset))
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(vmaddr + filsz);
121	map_len = round_page(memsz) - trunc_page(filsz);
122
123	DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx,0x%x, FALSE)\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);
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			    MAP_FILE,
138			    (caddr_t) vp,
139			    trunc_page(offset + filsz)))
140		return error;
141
142	bcopy(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 0;
150}
151
152int
153coff_load_file(struct proc *p, char *name)
154{
155  	struct vmspace *vmspace = p->p_vmspace;
156  	int error;
157  	struct nameidata nd;
158  	struct vnode *vnodep;
159  	struct vattr attr;
160  	struct filehdr *fhdr;
161  	struct aouthdr *ahdr;
162  	struct scnhdr *scns;
163  	char *ptr = 0;
164  	int nscns;
165  	unsigned long text_offset = 0, text_address = 0, text_size = 0;
166  	unsigned long data_offset = 0, data_address = 0, data_size = 0;
167  	unsigned long bss_size = 0;
168  	int i;
169
170	/* XXX use of 'curproc' should be 'p'?*/
171	NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, UIO_SYSSPACE, name, curproc);
172
173  	error = namei(&nd);
174  	if (error)
175    		return error;
176
177  	vnodep = nd.ni_vp;
178  	if (vnodep == NULL)
179    		return ENOEXEC;
180
181  	if (vnodep->v_writecount) {
182    		error = ETXTBSY;
183    		goto fail;
184  	}
185
186  	if (error = VOP_GETATTR(vnodep, &attr, p->p_ucred, p))
187    		goto fail;
188
189  	if ((vnodep->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(vnodep, VEXEC, p->p_ucred, p))
200    		goto fail;
201
202  	if (error = VOP_OPEN(vnodep, FREAD, p->p_ucred, p))
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(vnodep);
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			    MAP_FILE,
217			    (caddr_t) vnodep,
218			    0))
219    	goto 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, vnodep, text_offset,
261				      (caddr_t)text_address,
262				      text_size, text_size,
263				      VM_PROT_READ | VM_PROT_EXECUTE)) {
264    		goto dealloc_and_fail;
265  	}
266  	if (error = load_coff_section(vmspace, vnodep, data_offset,
267				      (caddr_t)data_address,
268				      data_size + bss_size, data_size,
269				      VM_PROT_ALL)) {
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(__FUNCTION__ " vm_map_remove failed");
280
281 fail:
282  	vput(nd.ni_vp);
283  	FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
284  	return error;
285}
286
287int
288exec_coff_imgact(iparams)
289	struct image_params *iparams;
290{
291	struct filehdr *fhdr = (struct filehdr*)iparams->image_header;
292	struct aouthdr *ahdr;
293	struct scnhdr *scns;
294	int i;
295	struct vmspace *vmspace = iparams->proc->p_vmspace;
296	unsigned long vmaddr;
297	int nscns;
298	int error, len;
299	unsigned long text_offset = 0, text_address = 0, text_size = 0;
300	unsigned long data_offset = 0, data_address = 0, data_size = 0;
301	unsigned long bss_size = 0;
302	int need_hack_p;
303	unsigned long data_end;
304	unsigned long data_map_start, data_map_len, data_map_addr = 0;
305	unsigned long bss_address, bss_map_start, data_copy_len, bss_map_len;
306	unsigned char *data_buf = 0;
307	caddr_t hole;
308
309	if (fhdr->f_magic != I386_COFF ||
310	    !(fhdr->f_flags & F_EXEC)) {
311
312		 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
313		 return -1;
314	}
315
316	nscns = fhdr->f_nscns;
317	if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
318	  	/*
319	   	 * For now, return an error -- need to be able to
320	   	 * read in all of the section structures.
321	   	 */
322
323		DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
324		return -1;
325	}
326
327	ahdr = (struct aouthdr*)((char*)(iparams->image_header) +
328				 sizeof(struct filehdr));
329	iparams->entry_addr = ahdr->entry;
330
331	scns = (struct scnhdr*)((char*)(iparams->image_header) +
332				sizeof(struct filehdr) +
333				sizeof(struct aouthdr));
334
335	if (error = exec_extract_strings(iparams)) {
336		DPRINTF(("%s(%d):  return %d\n", __FILE__, __LINE__, error));
337		return error;
338	}
339
340	exec_new_vmspace(iparams);
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, *ptr;
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				    MAP_FILE,
379				    (caddr_t) iparams->vnodep,
380				    foff)) {
381	      		return ENOEXEC;
382	    	}
383	    	for (j = off; j < scns[i].s_size + off; j++) {
384	      		char *libname;
385			char libbuf[40];
386
387	      		libname = buf + j + 4 * *(long*)(buf + j + 4);
388	      		j += 4* *(long*)(buf + j);
389
390			DPRINTF(("%s(%d):  shared library %s\n",
391				 __FILE__, __LINE__, libname));
392			strcpy(libbuf, "/emul/ibcs2");
393			strcpy(&libbuf[11], libname);
394	      		error = coff_load_file(iparams->proc, libbuf);
395	      		if (error)
396	      			error = coff_load_file(iparams->proc, libname);
397	      		if (error)
398				break;
399	    	}
400		if (vm_map_remove(kernel_map,
401				  (vm_offset_t) buf,
402				  (vm_offset_t) buf + len))
403	      		panic("exec_coff_imgact vm_map_remove failed");
404	    	if (error)
405	      		return error;
406	  	}
407	}
408	/*
409	 * Map in .text now
410	 */
411
412	DPRINTF(("%s(%d):  load_coff_section(vmspace, "
413		"iparams->vnodep, %08lx, %08lx, 0x%x, 0x%x, 0x%x)\n",
414		__FILE__, __LINE__, text_offset, text_address,
415		text_size, text_size, VM_PROT_READ | VM_PROT_EXECUTE));
416	if (error = load_coff_section(vmspace, iparams->vnodep,
417				      text_offset, (caddr_t)text_address,
418				      text_size, text_size,
419				      VM_PROT_READ | VM_PROT_EXECUTE)) {
420		DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
421		return error;
422       	}
423	/*
424	 * Map in .data and .bss now
425	 */
426
427
428	DPRINTF(("%s(%d): load_coff_section(vmspace, "
429		"iparams->vnodep, 0x%08lx, 0x%08lx, 0x%x, 0x%x, 0x%x)\n",
430		__FILE__, __LINE__, data_offset, data_address,
431		data_size + bss_size, data_size, VM_PROT_ALL));
432	if (error = load_coff_section(vmspace, iparams->vnodep,
433				      data_offset, (caddr_t)data_address,
434				      data_size + bss_size, data_size,
435				      VM_PROT_ALL)) {
436
437		DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
438		return error;
439	}
440
441	iparams->interpreted = 0;
442	iparams->proc->p_sysent = &ibcs2_svr3_sysvec;
443
444	vmspace->vm_tsize = round_page(text_size) >> PAGE_SHIFT;
445	vmspace->vm_dsize = round_page(data_size + bss_size) >> PAGE_SHIFT;
446	vmspace->vm_taddr = (caddr_t)text_address;
447	vmspace->vm_daddr = (caddr_t)data_address;
448
449	hole = (caddr_t)trunc_page(vmspace->vm_daddr) + ctob(vmspace->vm_dsize);
450
451
452	DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx, PAGE_SIZE, FALSE)\n",
453		__FILE__, __LINE__, hole));
454        DPRINTF(("imgact: error = %d\n", error));
455
456	error = vm_map_find(&vmspace->vm_map, NULL, 0,
457			    (vm_offset_t *) &hole, PAGE_SIZE, FALSE);
458
459	DPRINTF(("IBCS2: start vm_dsize = 0x%x, vm_daddr = 0x%x end = 0x%x\n",
460		ctob(vmspace->vm_dsize), vmspace->vm_daddr,
461		ctob(vmspace->vm_dsize) + vmspace->vm_daddr ));
462	DPRINTF(("%s(%d):  returning successfully!\n", __FILE__, __LINE__));
463
464	/* Indicate that this file should not be modified */
465	iparams->vnodep->v_flag |= VTEXT;
466	return 0;
467}
468
469/*
470 * Tell kern_execve.c about it, with a little help from the linker.
471 * Since `const' objects end up in the text segment, TEXT_SET is the
472 * correct directive to use.
473 */
474const struct execsw coff_execsw = { exec_coff_imgact, "coff" };
475TEXT_SET(execsw_set, coff_execsw);
476