1 /*	$NetBSD: ibcs2_exec_coff.c,v 1.26 2013/10/25 14:46:35 martin Exp $	*/
2 
3 /*
4  * Copyright (c) 1994, 1995, 1998 Scott Bartram
5  * Copyright (c) 1994 Adam Glass
6  * Copyright (c) 1993, 1994 Christopher G. Demetriou
7  * All rights reserved.
8  *
9  * originally from kern/exec_ecoff.c
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 Scott Bartram.
22  * 4. The name of the author may not be used to endorse or promote products
23  *    derived from this software without specific prior written permission
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ibcs2_exec_coff.c,v 1.26 2013/10/25 14:46:35 martin Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/proc.h>
44 #include <sys/malloc.h>
45 #include <sys/namei.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/exec.h>
49 #include <sys/exec_coff.h>
50 #include <sys/resourcevar.h>
51 
52 #include <sys/mman.h>
53 
54 #include <sys/cpu.h>
55 #include <machine/reg.h>
56 #include <machine/ibcs2_machdep.h>
57 
58 #include <compat/ibcs2/ibcs2_types.h>
59 #include <compat/ibcs2/ibcs2_exec.h>
60 #include <compat/ibcs2/ibcs2_errno.h>
61 #include <compat/ibcs2/ibcs2_util.h>
62 
63 
64 int exec_ibcs2_coff_prep_omagic(struct lwp *, struct exec_package *,
65 				     struct coff_filehdr *,
66 				     struct coff_aouthdr *);
67 int exec_ibcs2_coff_prep_nmagic(struct lwp *, struct exec_package *,
68 				     struct coff_filehdr *,
69 				     struct coff_aouthdr *);
70 int exec_ibcs2_coff_prep_zmagic(struct lwp *, struct exec_package *,
71 				     struct coff_filehdr *,
72 				     struct coff_aouthdr *);
73 void cpu_exec_ibcs2_coff_setup(int, struct proc *, struct exec_package *,
74 				    void *);
75 
76 static int coff_load_shlib(struct lwp *, const char *,
77 		struct exec_package *);
78 static int coff_find_section(struct lwp *, struct vnode *,
79 				  struct coff_filehdr *, struct coff_scnhdr *,
80 				  int);
81 
82 /*
83  * exec_ibcs2_coff_makecmds(): Check if it's an coff-format executable.
84  *
85  * Given a proc pointer and an exec package pointer, see if the referent
86  * of the epp is in coff format.  Check 'standard' magic numbers for
87  * this architecture.  If that fails, return failure.
88  *
89  * This function is  responsible for creating a set of vmcmds which can be
90  * used to build the process's vm space and inserting them into the exec
91  * package.
92  */
93 
94 int
exec_ibcs2_coff_makecmds(struct lwp * l,struct exec_package * epp)95 exec_ibcs2_coff_makecmds(struct lwp *l, struct exec_package *epp)
96 {
97 	int error;
98 	struct coff_filehdr *fp = epp->ep_hdr;
99 	struct coff_aouthdr *ap;
100 
101 	if (epp->ep_hdrvalid < COFF_HDR_SIZE) {
102 		DPRINTF(("ibcs2: bad coff hdr size\n"));
103 		return ENOEXEC;
104 	}
105 
106 	if (COFF_BADMAG(fp)) {
107 		DPRINTF(("ibcs2: bad coff magic\n"));
108 		return ENOEXEC;
109 	}
110 
111 	ap = (void *)((char *)epp->ep_hdr + sizeof(struct coff_filehdr));
112 	switch (ap->a_magic) {
113 	case COFF_OMAGIC:
114 		error = exec_ibcs2_coff_prep_omagic(l, epp, fp, ap);
115 		break;
116 	case COFF_NMAGIC:
117 		error = exec_ibcs2_coff_prep_nmagic(l, epp, fp, ap);
118 		break;
119 	case COFF_ZMAGIC:
120 		error = exec_ibcs2_coff_prep_zmagic(l, epp, fp, ap);
121 		break;
122 	default:
123 		DPRINTF(("ibcs2: bad coff magic %x\n", ap->a_magic));
124 		return ENOEXEC;
125 	}
126 
127 	if (error) {
128 		kill_vmcmds(&epp->ep_vmcmds);
129 		DPRINTF(("ibcs2: error loading magic %x (%d)\n", ap->a_magic,
130 		    error));
131 	}
132 
133 	return error;
134 }
135 
136 /*
137  * exec_ibcs2_coff_prep_omagic(): Prepare a COFF OMAGIC binary's exec package
138  */
139 
140 int
exec_ibcs2_coff_prep_omagic(struct lwp * l,struct exec_package * epp,struct coff_filehdr * fp,struct coff_aouthdr * ap)141 exec_ibcs2_coff_prep_omagic(struct lwp *l, struct exec_package *epp, struct coff_filehdr *fp, struct coff_aouthdr *ap)
142 {
143 	epp->ep_taddr = COFF_SEGMENT_ALIGN(fp, ap, ap->a_tstart);
144 	epp->ep_tsize = ap->a_tsize;
145 	epp->ep_daddr = COFF_SEGMENT_ALIGN(fp, ap, ap->a_dstart);
146 	epp->ep_dsize = ap->a_dsize;
147 	epp->ep_entry = ap->a_entry;
148 
149 	DPRINTF(("ibcs2_omagic: text=%#lx/%#lx, data=%#lx/%#lx, bss=%#lx/%#lx, entry=%#lx\n",
150 		epp->ep_taddr, epp->ep_tsize,
151 		epp->ep_daddr, epp->ep_dsize,
152 		ap->a_dstart + ap->a_dsize, ap->a_bsize,
153 		epp->ep_entry));
154 
155 	/* set up command for text and data segments */
156 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
157 		  ap->a_tsize + ap->a_dsize, epp->ep_taddr, epp->ep_vp,
158 		  COFF_TXTOFF(fp, ap),
159 		  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
160 
161 	/* set up command for bss segment */
162 	if (ap->a_bsize > 0) {
163 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, ap->a_bsize,
164 			  COFF_SEGMENT_ALIGN(fp, ap, ap->a_dstart + ap->a_dsize),
165 			  NULLVP, 0,
166 			  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
167 		epp->ep_dsize += ap->a_bsize;
168 	}
169 	/* The following is to make obreak(2) happy.  It expects daddr
170 	 * to on a page boundary and will round up dsize to a page
171 	 * address.
172 	 */
173 	if (trunc_page(epp->ep_daddr) != epp->ep_daddr) {
174 		epp->ep_dsize += epp->ep_daddr - trunc_page(epp->ep_daddr);
175 		epp->ep_daddr = trunc_page(epp->ep_daddr);
176 		if (epp->ep_taddr + epp->ep_tsize > epp->ep_daddr)
177 			epp->ep_tsize = epp->ep_daddr - epp->ep_taddr;
178 	}
179 
180 	return (*epp->ep_esch->es_setup_stack)(l, epp);
181 }
182 
183 /*
184  * exec_ibcs2_coff_prep_nmagic(): Prepare a 'native' NMAGIC COFF binary's exec
185  *                          package.
186  */
187 
188 int
exec_ibcs2_coff_prep_nmagic(struct lwp * l,struct exec_package * epp,struct coff_filehdr * fp,struct coff_aouthdr * ap)189 exec_ibcs2_coff_prep_nmagic(struct lwp *l, struct exec_package *epp, struct coff_filehdr *fp, struct coff_aouthdr *ap)
190 {
191 	long toverlap, doverlap;
192 	u_long tsize, tend;
193 
194 	epp->ep_taddr = ap->a_tstart;
195 	epp->ep_tsize = ap->a_tsize;
196 	epp->ep_daddr = ap->a_dstart;
197 	epp->ep_dsize = ap->a_dsize;
198 	epp->ep_entry = ap->a_entry;
199 
200 	DPRINTF(("ibcs2_nmagic: text=%#lx/%#lx, data=%#lx/%#lx, bss=%#lx/%#lx, entry=%#lx\n",
201 		epp->ep_taddr, epp->ep_tsize,
202 		epp->ep_daddr, epp->ep_dsize,
203 		ap->a_dstart + ap->a_dsize, ap->a_bsize,
204 		epp->ep_entry));
205 
206 	/* Do the text and data pages overlap?
207 	 */
208 	tend = epp->ep_taddr + epp->ep_tsize - 1;
209 	if (trunc_page(tend) == trunc_page(epp->ep_daddr)) {
210 		/* If the first page of text is the first page of data,
211 		 * then we consider it all data.
212 		 */
213 		if (trunc_page(epp->ep_taddr) == trunc_page(epp->ep_daddr)) {
214 			tsize = 0;
215 		} else {
216 			tsize = trunc_page(tend) - epp->ep_taddr;
217 		}
218 
219 		/* If the text and data file and VA offsets are the
220 		 * same, simply bring the data segment to start on
221 		 * the start of the page.
222 		 */
223 		if (epp->ep_daddr - epp->ep_taddr ==
224 		    COFF_DATOFF(fp, ap) - COFF_TXTOFF(fp, ap)) {
225 			u_long diff = epp->ep_daddr - trunc_page(epp->ep_daddr);
226 			toverlap = 0;
227 			doverlap = -diff;
228 		} else {
229 			/* otherwise copy the individual pieces */
230 			toverlap = epp->ep_tsize - tsize;
231 			doverlap = round_page(epp->ep_daddr) - epp->ep_daddr;
232 			if (doverlap > epp->ep_dsize)
233 				doverlap = epp->ep_dsize;
234 		}
235 	} else {
236 		tsize = epp->ep_tsize;
237 		toverlap = 0;
238 		doverlap = 0;
239 	}
240 
241 	DPRINTF(("nmagic_vmcmds:"));
242 	if (tsize > 0) {
243 		/* set up command for text segment */
244 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, tsize,
245 			  epp->ep_taddr, epp->ep_vp, COFF_TXTOFF(fp, ap),
246 			  VM_PROT_READ|VM_PROT_EXECUTE);
247 		DPRINTF((" map_readvn(%#lx/%#lx@%#lx)",
248 			epp->ep_taddr, tsize, (u_long) COFF_TXTOFF(fp, ap)));
249 	}
250 	if (toverlap > 0) {
251 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, toverlap,
252 			  epp->ep_taddr + tsize, epp->ep_vp,
253 			  COFF_TXTOFF(fp, ap) + tsize,
254 			  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
255 		DPRINTF((" map_readvn(%#lx/%#lx@%#lx)",
256 			epp->ep_taddr + tsize, toverlap,
257 			COFF_TXTOFF(fp, ap) + tsize));
258 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn, doverlap,
259 			  epp->ep_daddr, epp->ep_vp,
260 			  COFF_DATOFF(fp, ap),
261 			  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
262 		DPRINTF((" readvn(%#lx/%#lx@%#lx)", epp->ep_daddr, doverlap,
263 			COFF_DATOFF(fp, ap)));
264 	}
265 
266 	if (epp->ep_dsize > doverlap || doverlap < 0) {
267 		/* set up command for data segment */
268 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
269 			  epp->ep_dsize - doverlap, epp->ep_daddr + doverlap,
270 			  epp->ep_vp, COFF_DATOFF(fp, ap) + doverlap,
271 			  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
272 		DPRINTF((" map_readvn(%#lx/%#lx@%#lx)",
273 			epp->ep_daddr + doverlap, epp->ep_dsize - doverlap,
274 			COFF_DATOFF(fp, ap) + doverlap));
275 	}
276 
277 	/* Handle page remainders for pagedvn.
278 	 */
279 
280 	/* set up command for bss segment */
281 	if (ap->a_bsize > 0) {
282 		u_long dend = round_page(epp->ep_daddr + epp->ep_dsize);
283 		u_long dspace = dend - (epp->ep_daddr + epp->ep_dsize);
284 		if (ap->a_bsize > dspace) {
285 			NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
286 				  ap->a_bsize - dspace, dend, NULLVP, 0,
287 				  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
288 			DPRINTF((" map_zero(%#lx/%#lx)",
289 				dend, ap->a_bsize - dspace));
290 		}
291 		epp->ep_dsize += ap->a_bsize;
292 	}
293 	DPRINTF(("\n"));
294 	/* The following is to make obreak(2) happy.  It expects daddr
295 	 * to on a page boundary and will round up dsize to a page
296 	 * address.
297 	 */
298 	if (trunc_page(epp->ep_daddr) != epp->ep_daddr) {
299 		epp->ep_dsize += epp->ep_daddr - trunc_page(epp->ep_daddr);
300 		epp->ep_daddr = trunc_page(epp->ep_daddr);
301 		if (epp->ep_taddr + epp->ep_tsize > epp->ep_daddr)
302 			epp->ep_tsize = epp->ep_daddr - epp->ep_taddr;
303 	}
304 
305 	return (*epp->ep_esch->es_setup_stack)(l, epp);
306 }
307 
308 /*
309  * coff_find_section - load specified section header
310  *
311  * TODO - optimize by reading all section headers in at once
312  */
313 
314 static int
coff_find_section(struct lwp * l,struct vnode * vp,struct coff_filehdr * fp,struct coff_scnhdr * sh,int s_type)315 coff_find_section(struct lwp *l, struct vnode *vp, struct coff_filehdr *fp, struct coff_scnhdr *sh, int s_type)
316 {
317 	int i, pos, siz, error;
318 	size_t resid;
319 
320 	pos = COFF_HDR_SIZE;
321 	for (i = 0; i < fp->f_nscns; i++, pos += sizeof(struct coff_scnhdr)) {
322 		siz = sizeof(struct coff_scnhdr);
323 		error = vn_rdwr(UIO_READ, vp, (void *) sh,
324 		    siz, pos, UIO_SYSSPACE, IO_NODELOCKED, l->l_cred,
325 		    &resid, NULL);
326 		if (error) {
327 			DPRINTF(("section hdr %d read error %d\n", i, error));
328 			return error;
329 		}
330 		siz -= resid;
331 		if (siz != sizeof(struct coff_scnhdr)) {
332 			DPRINTF(("incomplete read: hdr %d ask=%d, rem=%lu got %d\n",
333 				 s_type, sizeof(struct coff_scnhdr),
334 				 (u_long) resid, siz));
335 			return ENOEXEC;
336 		}
337 		/* DPRINTF(("found section: %x\n", sh->s_flags)); */
338 		if (sh->s_flags == s_type)
339 			return 0;
340 	}
341 	return ENOEXEC;
342 }
343 
344 /*
345  * exec_ibcs2_coff_prep_zmagic(): Prepare a COFF ZMAGIC binary's exec package
346  *
347  * First, set the various offsets/lengths in the exec package.
348  *
349  * Then, mark the text image busy (so it can be demand paged) or error
350  * out if this is not possible.  Finally, set up vmcmds for the
351  * text, data, bss, and stack segments.
352  */
353 
354 int
exec_ibcs2_coff_prep_zmagic(struct lwp * l,struct exec_package * epp,struct coff_filehdr * fp,struct coff_aouthdr * ap)355 exec_ibcs2_coff_prep_zmagic(struct lwp *l, struct exec_package *epp, struct coff_filehdr *fp, struct coff_aouthdr *ap)
356 {
357 	int error;
358 	u_long offset;
359 	long dsize, baddr, bsize;
360 	struct coff_scnhdr sh;
361 
362 	/* DPRINTF(("enter exec_ibcs2_coff_prep_zmagic\n")); */
363 
364 	/* set up command for text segment */
365 	error = coff_find_section(l, epp->ep_vp, fp, &sh, COFF_STYP_TEXT);
366 	if (error) {
367 		DPRINTF(("can't find text section: %d\n", error));
368 		return error;
369 	}
370 	/* DPRINTF(("COFF text addr %x size %d offset %d\n", sh.s_vaddr,
371 		 sh.s_size, sh.s_scnptr)); */
372 	epp->ep_taddr = COFF_ALIGN(sh.s_vaddr);
373 	offset = sh.s_scnptr - (sh.s_vaddr - epp->ep_taddr);
374 	epp->ep_tsize = sh.s_size + (sh.s_vaddr - epp->ep_taddr);
375 
376 	/* DPRINTF(("VMCMD: addr %x size %d offset %d\n", epp->ep_taddr,
377 		 epp->ep_tsize, offset)); */
378 #ifdef notyet
379 	error = vn_marktext(epp->ep_vp);
380 	if (error)
381 		return (error);
382 
383 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, epp->ep_tsize,
384 		  epp->ep_taddr, epp->ep_vp, offset,
385 		  VM_PROT_READ|VM_PROT_EXECUTE);
386 #else
387 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_tsize,
388 		  epp->ep_taddr, epp->ep_vp, offset,
389 		  VM_PROT_READ|VM_PROT_EXECUTE);
390 #endif
391 
392 	/* set up command for data segment */
393 	error = coff_find_section(l, epp->ep_vp, fp, &sh, COFF_STYP_DATA);
394 	if (error) {
395 		DPRINTF(("can't find data section: %d\n", error));
396 		return error;
397 	}
398 	/* DPRINTF(("COFF data addr %x size %d offset %d\n", sh.s_vaddr,
399 		 sh.s_size, sh.s_scnptr)); */
400 	epp->ep_daddr = COFF_ALIGN(sh.s_vaddr);
401 	offset = sh.s_scnptr - (sh.s_vaddr - epp->ep_daddr);
402 	dsize = sh.s_size + (sh.s_vaddr - epp->ep_daddr);
403 	epp->ep_dsize = dsize + ap->a_bsize;
404 
405 	/* DPRINTF(("VMCMD: addr %x size %d offset %d\n", epp->ep_daddr,
406 		 dsize, offset)); */
407 #ifdef notyet
408 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, dsize,
409 		  epp->ep_daddr, epp->ep_vp, offset,
410 		  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
411 #else
412 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
413 		  dsize, epp->ep_daddr, epp->ep_vp, offset,
414 		  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
415 #endif
416 
417 	/* set up command for bss segment */
418 	baddr = round_page(epp->ep_daddr + dsize);
419 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
420 	if (bsize > 0) {
421 		/* DPRINTF(("VMCMD: addr %x size %d offset %d\n",
422 			 baddr, bsize, 0)); */
423 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
424 			  bsize, baddr, NULLVP, 0,
425 			  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
426 	}
427 
428 	/* load any shared libraries */
429 	error = coff_find_section(l, epp->ep_vp, fp, &sh, COFF_STYP_SHLIB);
430 	if (!error) {
431 		size_t resid;
432 		struct coff_slhdr *slhdr;
433 		char *tbuf, *bufp;
434 		size_t len = sh.s_size, /* path_index, */ entry_len;
435 
436 		if (len > 64 * 1024)
437 			return ENOEXEC;
438 
439 		tbuf = malloc(len, M_TEMP, M_WAITOK);
440 		if (tbuf == NULL)
441 			return ENOEXEC;
442 
443 		/* DPRINTF(("COFF shlib size %d offset %d\n",
444 			 sh.s_size, sh.s_scnptr)); */
445 
446 		error = vn_rdwr(UIO_READ, epp->ep_vp, tbuf,
447 				len, sh.s_scnptr,
448 				UIO_SYSSPACE, IO_NODELOCKED, l->l_cred,
449 				&resid, NULL);
450 		if (error) {
451 			DPRINTF(("shlib section read error %d\n", error));
452 			free(tbuf, M_TEMP);
453 			return ENOEXEC;
454 		}
455 		bufp = tbuf;
456 		while (len) {
457 			slhdr = (struct coff_slhdr *)bufp;
458 
459 			if (slhdr->path_index > LONG_MAX / sizeof(long) ||
460 			    slhdr->entry_len > LONG_MAX / sizeof(long)) {
461 				free(tbuf, M_TEMP);
462 				return ENOEXEC;
463 			}
464 
465 			/* path_index = slhdr->path_index * sizeof(long); */
466 			entry_len = slhdr->entry_len * sizeof(long);
467 
468 			if (entry_len > len) {
469 				free(tbuf, M_TEMP);
470 				return ENOEXEC;
471 			}
472 
473 			/* DPRINTF(("path_index: %d entry_len: %d name: %s\n",
474 				 path_index, entry_len, slhdr->sl_name)); */
475 
476 			error = coff_load_shlib(l, slhdr->sl_name, epp);
477 			if (error) {
478 				free(tbuf, M_TEMP);
479 				return ENOEXEC;
480 			}
481 			bufp += entry_len;
482 			len -= entry_len;
483 		}
484 		free(tbuf, M_TEMP);
485 	}
486 
487 	/* set up entry point */
488 	epp->ep_entry = ap->a_entry;
489 
490 	DPRINTF(("ibcs2_zmagic: text addr: %#lx size: %#lx data addr: %#lx size: %#lx entry: %#lx\n",
491 		 epp->ep_taddr, epp->ep_tsize,
492 		 epp->ep_daddr, epp->ep_dsize,
493 		 epp->ep_entry));
494 
495 	/* The following is to make obreak(2) happy.  It expects daddr
496 	 * to on a page boundary and will round up dsize to a page
497 	 * address.
498 	 */
499 	if (trunc_page(epp->ep_daddr) != epp->ep_daddr) {
500 		epp->ep_dsize += epp->ep_daddr - trunc_page(epp->ep_daddr);
501 		epp->ep_daddr = trunc_page(epp->ep_daddr);
502 		if (epp->ep_taddr + epp->ep_tsize > epp->ep_daddr)
503 			epp->ep_tsize = epp->ep_daddr - epp->ep_taddr;
504 	}
505 
506 
507 	return (*epp->ep_esch->es_setup_stack)(l, epp);
508 }
509 
510 static int
coff_load_shlib(struct lwp * l,const char * path,struct exec_package * epp)511 coff_load_shlib(struct lwp *l, const char *path, struct exec_package *epp)
512 {
513 	int error, siz;
514 	int taddr, tsize, daddr, dsize, offset;
515 	size_t resid;
516 	struct vnode *vp;
517 	struct coff_filehdr fh, *fhp = &fh;
518 	struct coff_scnhdr sh, *shp = &sh;
519 
520 	/*
521 	 * 1. open shlib file
522 	 * 2. read filehdr
523 	 * 3. map text, data, and bss out of it using VM_*
524 	 */
525 	/* first get the vnode */
526 	error = namei_simple_kernel(path, NSM_FOLLOW_TRYEMULROOT, &vp);
527 	if (error != 0) {
528 		DPRINTF(("coff_load_shlib: can't find library %s\n", path));
529 		return error;
530 	}
531 
532 	siz = sizeof(struct coff_filehdr);
533 	error = vn_rdwr(UIO_READ, vp, (void *) fhp, siz, 0,
534 	    UIO_SYSSPACE, IO_NODELOCKED, l->l_cred, &resid, l);
535 	if (error) {
536 	    DPRINTF(("filehdr read error %d\n", error));
537 	    vrele(vp);
538 	    return error;
539 	}
540 	siz -= resid;
541 	if (siz != sizeof(struct coff_filehdr)) {
542 	    DPRINTF(("coff_load_shlib: incomplete read: ask=%d, rem=%lu got %d\n",
543 		     sizeof(struct coff_filehdr), (u_long) resid, siz));
544 	    vrele(vp);
545 	    return ENOEXEC;
546 	}
547 
548 	/* load text */
549 	error = coff_find_section(l, vp, fhp, shp, COFF_STYP_TEXT);
550 	if (error) {
551 	    DPRINTF(("can't find shlib text section\n"));
552 	    vrele(vp);
553 	    return error;
554 	}
555 	/* DPRINTF(("COFF text addr %x size %d offset %d\n", sh.s_vaddr,
556 		 sh.s_size, sh.s_scnptr)); */
557 	taddr = COFF_ALIGN(shp->s_vaddr);
558 	offset = shp->s_scnptr - (shp->s_vaddr - taddr);
559 	tsize = shp->s_size + (shp->s_vaddr - taddr);
560 	/* DPRINTF(("VMCMD: addr %x size %d offset %d\n", taddr, tsize, offset)); */
561 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, tsize, taddr,
562 		  vp, offset,
563 		  VM_PROT_READ|VM_PROT_EXECUTE);
564 
565 	/* load data */
566 	error = coff_find_section(l, vp, fhp, shp, COFF_STYP_DATA);
567 	if (error) {
568 	    DPRINTF(("can't find shlib data section\n"));
569 	    vrele(vp);
570 	    return error;
571 	}
572 	/* DPRINTF(("COFF data addr %x size %d offset %d\n", shp->s_vaddr,
573 		 shp->s_size, shp->s_scnptr)); */
574 	daddr = COFF_ALIGN(shp->s_vaddr);
575 	offset = shp->s_scnptr - (shp->s_vaddr - daddr);
576 	dsize = shp->s_size + (shp->s_vaddr - daddr);
577 	/* epp->ep_dsize = dsize + ap->a_bsize; */
578 
579 	/* DPRINTF(("VMCMD: addr %x size %d offset %d\n", daddr, dsize, offset)); */
580 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
581 		  dsize, daddr, vp, offset,
582 		  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
583 
584 	/* load bss */
585 	error = coff_find_section(l, vp, fhp, shp, COFF_STYP_BSS);
586 	if (!error) {
587 		int baddr = round_page(daddr + dsize);
588 		int bsize = daddr + dsize + shp->s_size - baddr;
589 		if (bsize > 0) {
590 			/* DPRINTF(("VMCMD: addr %x size %d offset %d\n",
591 			   baddr, bsize, 0)); */
592 			NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
593 				  bsize, baddr, NULLVP, 0,
594 				  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
595 	    }
596 	}
597 	vrele(vp);
598 
599 	return 0;
600 }
601