imgact_coff.c revision 10221
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.4 1995/05/30 08:00:17 rgrimes 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#include "coff.h"
46#include "ibcs2.h"
47
48static int
49load_coff_section(vmspace, vp, offset, vmaddr, memsz, filsz, prot)
50	struct vmspace *vmspace;
51	struct vnode *vp;
52	vm_offset_t offset;
53	caddr_t vmaddr;
54	size_t memsz, filsz;
55	vm_prot_t prot;
56{
57	size_t map_len;
58	vm_offset_t map_offset;
59	vm_offset_t map_addr;
60	int error;
61	unsigned char *data_buf = 0;
62	size_t copy_len;
63
64	map_offset = trunc_page(offset);
65	map_addr = trunc_page(vmaddr);
66
67	if (memsz > filsz) {
68		/*
69		 * We have the stupid situation that
70		 * the section is longer than it is on file,
71		 * which means it has zero-filled areas, and
72		 * we have to work for it.  Stupid iBCS!
73		 */
74		map_len = trunc_page(offset + filsz) - trunc_page(map_offset);
75	} else {
76		/*
77		 * The only stuff we care about is on disk, and we
78		 * don't care if we map in more than is really there.
79		 */
80		map_len = round_page(offset + filsz) - trunc_page(map_offset);
81	}
82
83if (ibcs2_trace & IBCS2_TRACE_COFF) {
84printf("%s(%d):  vm_mmap(&vmspace->vm_map, &0x%08lx, 0x%x, 0x%x, "
85	"VM_PROT_ALL, MAP_FILE | MAP_PRIVATE | MAP_FIXED, vp, 0x%x)\n",
86	__FILE__, __LINE__, map_addr, map_len, prot, map_offset);
87}
88
89	if (error = vm_mmap(&vmspace->vm_map,
90			     &map_addr,
91			     map_len,
92			     prot,
93			     VM_PROT_ALL,
94			     MAP_FILE | MAP_PRIVATE | MAP_FIXED,
95			     (caddr_t) vp,
96			     map_offset))
97		return error;
98
99	if (memsz == filsz) {
100		/* We're done! */
101		return 0;
102	}
103
104	/*
105	 * Now we have screwball stuff, to accomodate stupid COFF.
106	 * We have to map the remaining bit of the file into the kernel's
107	 * memory map, allocate some anonymous memory, copy that last
108	 * bit into it, and then we're done. *sigh*
109	 * For clean-up reasons, we actally map in the file last.
110	 */
111
112	copy_len = (offset + filsz) - trunc_page(offset + filsz);
113	map_addr = trunc_page(vmaddr + filsz);
114	map_len = round_page(memsz) - trunc_page(filsz);
115
116if (ibcs2_trace & IBCS2_TRACE_COFF) {
117printf("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx, 0x%x, FALSE)\n",
118	__FILE__, __LINE__, map_addr, map_len);
119}
120
121	if (map_len != 0) {
122		error = vm_map_find(&vmspace->vm_map, NULL, 0, &map_addr,
123				    map_len, FALSE);
124		if (error)
125			return error;
126	}
127
128	if (error = vm_mmap(kernel_map,
129			    (vm_offset_t *) &data_buf,
130			    PAGE_SIZE,
131			    VM_PROT_READ,
132			    VM_PROT_READ,
133			    MAP_FILE,
134			    (caddr_t) vp,
135			    trunc_page(offset + filsz)))
136		return error;
137
138	bcopy(data_buf, (caddr_t) map_addr, copy_len);
139
140	if (vm_map_remove(kernel_map,
141			  (vm_offset_t) data_buf,
142			  (vm_offset_t) data_buf + PAGE_SIZE))
143		panic("load_coff_section vm_map_remove failed");
144
145	return 0;
146}
147
148int
149coff_load_file(struct proc *p, char *name)
150{
151  	struct vmspace *vmspace = p->p_vmspace;
152  	int error;
153  	struct nameidata nd;
154  	struct vnode *vnodep;
155  	struct vattr attr;
156  	struct filehdr *fhdr;
157  	struct aouthdr *ahdr;
158  	struct scnhdr *scns;
159  	char *ptr = 0;
160  	int nscns;
161  	unsigned long text_offset = 0, text_address = 0, text_size = 0;
162  	unsigned long data_offset = 0, data_address = 0, data_size = 0;
163  	unsigned long bss_size = 0;
164  	int i;
165
166  	nd.ni_cnd.cn_nameiop = LOOKUP;
167	nd.ni_cnd.cn_flags = LOCKLEAF | FOLLOW | SAVENAME;
168	nd.ni_cnd.cn_proc = curproc;
169	nd.ni_cnd.cn_cred = curproc->p_cred->pc_ucred;
170  	nd.ni_segflg = UIO_SYSSPACE;
171  	nd.ni_dirp = name;
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  	if (error = vm_mmap(kernel_map,
206			    (vm_offset_t *) &ptr,
207			    PAGE_SIZE,
208			    VM_PROT_READ,
209		       	    VM_PROT_READ,
210			    MAP_FILE,
211			    (caddr_t) vnodep,
212			    0))
213    	goto fail;
214
215  	fhdr = (struct filehdr *)ptr;
216
217  	if (fhdr->f_magic != I386_COFF) {
218    		error = ENOEXEC;
219    		goto dealloc_and_fail;
220  	}
221
222  	nscns = fhdr->f_nscns;
223
224  	if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
225    		/*
226     		 * XXX -- just fail.  I'm so lazy.
227     		 */
228    		error = ENOEXEC;
229    		goto dealloc_and_fail;
230  	}
231
232  	ahdr = (struct aouthdr*)(ptr + sizeof(struct filehdr));
233
234  	scns = (struct scnhdr*)(ptr + sizeof(struct filehdr)
235			  + sizeof(struct aouthdr));
236
237  	for (i = 0; i < nscns; i++) {
238    		if (scns[i].s_flags & STYP_NOLOAD)
239      			continue;
240    		else if (scns[i].s_flags & STYP_TEXT) {
241      			text_address = scns[i].s_vaddr;
242      			text_size = scns[i].s_size;
243      			text_offset = scns[i].s_scnptr;
244    		}
245		else if (scns[i].s_flags & STYP_DATA) {
246      			data_address = scns[i].s_vaddr;
247      			data_size = scns[i].s_size;
248      			data_offset = scns[i].s_scnptr;
249    		} else if (scns[i].s_flags & STYP_BSS) {
250      			bss_size = scns[i].s_size;
251    		}
252  	}
253
254  	if (error = load_coff_section(vmspace, vnodep, text_offset,
255				      (caddr_t)text_address,
256				      text_size, text_size,
257				      VM_PROT_READ | VM_PROT_EXECUTE)) {
258    		goto dealloc_and_fail;
259  	}
260  	if (error = load_coff_section(vmspace, vnodep, data_offset,
261				      (caddr_t)data_address,
262				      data_size + bss_size, data_size,
263				      VM_PROT_ALL)) {
264    		goto dealloc_and_fail;
265  	}
266
267  	error = 0;
268
269 	dealloc_and_fail:
270	if (vm_map_remove(kernel_map,
271			  (vm_offset_t) ptr,
272			  (vm_offset_t) ptr + PAGE_SIZE))
273    		panic(__FUNCTION__ " vm_map_remove failed");
274
275 fail:
276  	vput(nd.ni_vp);
277  	FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
278  	return error;
279}
280
281int
282exec_coff_imgact(iparams)
283	struct image_params *iparams;
284{
285	struct filehdr *fhdr = (struct filehdr*)iparams->image_header;
286	struct aouthdr *ahdr;
287	struct scnhdr *scns;
288	int i;
289	struct vmspace *vmspace = iparams->proc->p_vmspace;
290	unsigned long vmaddr;
291	int nscns;
292	int error, len;
293	extern struct sysentvec ibcs2_svr3_sysvec;
294	unsigned long text_offset = 0, text_address = 0, text_size = 0;
295	unsigned long data_offset = 0, data_address = 0, data_size = 0;
296	unsigned long bss_size = 0;
297	int need_hack_p;
298	unsigned long data_end;
299	unsigned long data_map_start, data_map_len, data_map_addr = 0;
300	unsigned long bss_address, bss_map_start, data_copy_len, bss_map_len;
301	unsigned char *data_buf = 0;
302	caddr_t hole;
303
304	if (fhdr->f_magic != I386_COFF ||
305	    !(fhdr->f_flags & F_EXEC)) {
306
307if (ibcs2_trace & IBCS2_TRACE_COFF) {
308printf("%s(%d): return -1\n", __FILE__, __LINE__);
309}
310
311	  	return -1;
312	}
313
314	nscns = fhdr->f_nscns;
315	if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
316	  	/*
317	   	 * For now, return an error -- need to be able to
318	   	 * read in all of the section structures.
319	   	 */
320
321if (ibcs2_trace & IBCS2_TRACE_COFF) {
322printf("%s(%d): return -1\n", __FILE__, __LINE__);
323}
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
337if (ibcs2_trace & IBCS2_TRACE_COFF) {
338printf("%s(%d):  return %d\n", __FILE__, __LINE__, error);
339}
340	  	return error;
341	}
342
343	exec_new_vmspace(iparams);
344
345	for (i = 0; i < nscns; i++) {
346
347if (ibcs2_trace & IBCS2_TRACE_COFF) {
348printf("i = %d, scns[i].s_name = %s, scns[i].s_vaddr = %08lx, "
349       "scns[i].s_scnptr = %d\n", i, scns[i].s_name,
350       scns[i].s_vaddr, scns[i].s_scnptr);
351}
352	  if (scns[i].s_flags & STYP_NOLOAD) {
353	    	/*
354	     	 * A section that is not loaded, for whatever
355	     	 * reason.  It takes precedance over other flag
356	     	 * bits...
357	     	 */
358	    	continue;
359	  } else if (scns[i].s_flags & STYP_TEXT) {
360	    	text_address = scns[i].s_vaddr;
361	    	text_size = scns[i].s_size;
362	    	text_offset = scns[i].s_scnptr;
363	  } else if (scns[i].s_flags & STYP_DATA) {
364	    	/* .data section */
365	    	data_address = scns[i].s_vaddr;
366	    	data_size = scns[i].s_size;
367	    	data_offset = scns[i].s_scnptr;
368	  } else if (scns[i].s_flags & STYP_BSS) {
369	    	/* .bss section */
370	    	bss_size = scns[i].s_size;
371	  } else if (scns[i].s_flags & STYP_LIB) {
372	    	char *buf = 0, *ptr;
373	    	int foff = trunc_page(scns[i].s_scnptr);
374	    	int off = scns[i].s_scnptr - foff;
375	    	int len = round_page(scns[i].s_size + PAGE_SIZE);
376	    	int j;
377
378	    	if (error = vm_mmap(kernel_map,
379				    (vm_offset_t *) &buf,
380				    len,
381				    VM_PROT_READ,
382				    VM_PROT_READ,
383				    MAP_FILE,
384				    (caddr_t) iparams->vnodep,
385				    foff)) {
386	      		return ENOEXEC;
387	    	}
388	    	for (j = off; j < scns[i].s_size + off; j++) {
389	      		char *libname;
390	      		libname = buf + j + 4 * *(long*)(buf + j + 4);
391	      		j += 4* *(long*)(buf + j);
392
393if (ibcs2_trace & IBCS2_TRACE_COFF) {
394printf("%s(%d):  shared library %s\n", __FILE__, __LINE__, libname);
395}
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
412if (ibcs2_trace & IBCS2_TRACE_COFF) {
413printf("%s(%d):  load_coff_section(vmspace, "
414	"iparams->vnodep, %08lx, %08lx, 0x%x, 0x%x, 0x%x)\n",
415	__FILE__, __LINE__, text_offset, text_address,
416	text_size, text_size, VM_PROT_READ | VM_PROT_EXECUTE);
417}
418	if (error = load_coff_section(vmspace, iparams->vnodep,
419				      text_offset, (caddr_t)text_address,
420				      text_size, text_size,
421				      VM_PROT_READ | VM_PROT_EXECUTE)) {
422
423if (ibcs2_trace & IBCS2_TRACE_COFF) {
424printf("%s(%d): error = %d\n", __FILE__, __LINE__, error);
425}
426		return error;
427       	}
428	/*
429	 * Map in .data and .bss now
430	 */
431
432
433if (ibcs2_trace & IBCS2_TRACE_COFF) {
434printf("%s(%d): load_coff_section(vmspace, "
435	"iparams->vnodep, 0x%08lx, 0x%08lx, 0x%x, 0x%x, 0x%x)\n",
436	__FILE__, __LINE__, data_offset, data_address,
437	data_size + bss_size, data_size, VM_PROT_ALL);
438}
439	if (error = load_coff_section(vmspace, iparams->vnodep,
440				      data_offset, (caddr_t)data_address,
441				      data_size + bss_size, data_size,
442				      VM_PROT_ALL)) {
443
444if (ibcs2_trace & IBCS2_TRACE_COFF) {
445printf("%s(%d): error = %d\n", __FILE__, __LINE__, error);
446}
447		return error;
448	}
449
450	iparams->interpreted = 0;
451	iparams->proc->p_sysent = &ibcs2_svr3_sysvec;
452
453	vmspace->vm_tsize = round_page(text_size) >> PAGE_SHIFT;
454	vmspace->vm_dsize = round_page(data_size + bss_size) >> PAGE_SHIFT;
455	vmspace->vm_taddr = (caddr_t)text_address;
456	vmspace->vm_daddr = (caddr_t)data_address;
457
458	hole = (caddr_t)trunc_page(vmspace->vm_daddr) + ctob(vmspace->vm_dsize);
459
460
461if (ibcs2_trace & IBCS2_TRACE_COFF) {
462printf("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx, PAGE_SIZE, FALSE)\n",
463	__FILE__, __LINE__, hole);
464printf("imgact: error = %d\n", error);
465}
466	error = vm_map_find(&vmspace->vm_map, NULL, 0,
467			    (vm_offset_t *) &hole, PAGE_SIZE, FALSE);
468
469if (ibcs2_trace & IBCS2_TRACE_COFF) {
470printf("IBCS2: start vm_dsize = 0x%x, vm_daddr = 0x%x end = 0x%x\n",
471	ctob(vmspace->vm_dsize), vmspace->vm_daddr,
472	ctob(vmspace->vm_dsize) + vmspace->vm_daddr );
473printf("%s(%d):  returning successfully!\n", __FILE__, __LINE__);
474}
475	/* Indicate that this file should not be modified */
476	iparams->vnodep->v_flag |= VTEXT;
477	return 0;
478}
479
480/*
481 * Tell kern_execve.c about it, with a little help from the linker.
482 * Since `const' objects end up in the text segment, TEXT_SET is the
483 * correct directive to use.
484 */
485const struct execsw coff_execsw = { exec_coff_imgact, "coff" };
486TEXT_SET(execsw_set, coff_execsw);
487