exec_coff.c revision 1.1 1 1.1 itojun /* $NetBSD: exec_coff.c,v 1.1 1999/09/12 16:08:14 itojun Exp $ */
2 1.1 itojun
3 1.1 itojun /*
4 1.1 itojun * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
5 1.1 itojun *
6 1.1 itojun * Redistribution and use in source and binary forms, with or without
7 1.1 itojun * modification, are permitted provided that the following conditions
8 1.1 itojun * are met:
9 1.1 itojun * 1. Redistributions of source code must retain the above copyright
10 1.1 itojun * notice, this list of conditions and the following disclaimer.
11 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 itojun * notice, this list of conditions and the following disclaimer in the
13 1.1 itojun * documentation and/or other materials provided with the distribution.
14 1.1 itojun * 3. All advertising materials mentioning features or use of this software
15 1.1 itojun * must display the following acknowledgement:
16 1.1 itojun * This product includes software developed by Christopher G. Demetriou
17 1.1 itojun * for the NetBSD Project.
18 1.1 itojun * 4. The name of the author may not be used to endorse or promote products
19 1.1 itojun * derived from this software without specific prior written permission
20 1.1 itojun *
21 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 itojun * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 itojun * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 itojun * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 itojun * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 itojun * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 itojun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 itojun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 itojun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 itojun * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 itojun */
32 1.1 itojun
33 1.1 itojun #include <sys/cdefs.h>
34 1.1 itojun #ifndef lint
35 1.1 itojun __RCSID("$NetBSD: exec_coff.c,v 1.1 1999/09/12 16:08:14 itojun Exp $");
36 1.1 itojun #endif /* not lint */
37 1.1 itojun
38 1.1 itojun #include <sys/param.h>
39 1.1 itojun #include <sys/time.h>
40 1.1 itojun #include <sys/proc.h>
41 1.1 itojun #include <sys/types.h>
42 1.1 itojun #include <stdio.h>
43 1.1 itojun #include <string.h>
44 1.1 itojun #include "extern.h"
45 1.1 itojun
46 1.1 itojun #if defined(NLIST_COFF)
47 1.1 itojun #include <machine/exec_coff.h> /* XXX sys/exec_coff.h? */
48 1.1 itojun
49 1.1 itojun #define check(off, size) ((off < 0) || (off + size > mappedsize))
50 1.1 itojun #define BAD do { rv = -1; goto out; } while (0)
51 1.1 itojun
52 1.1 itojun int
53 1.1 itojun check_coff(mappedfile, mappedsize)
54 1.1 itojun const char *mappedfile;
55 1.1 itojun size_t mappedsize;
56 1.1 itojun {
57 1.1 itojun struct coff_exechdr *exechdrp;
58 1.1 itojun int rv;
59 1.1 itojun
60 1.1 itojun rv = 0;
61 1.1 itojun
62 1.1 itojun if (check(0, sizeof *exechdrp))
63 1.1 itojun BAD;
64 1.1 itojun exechdrp = (struct coff_exechdr *)&mappedfile[0];
65 1.1 itojun
66 1.1 itojun if (COFF_BADMAG(&(exechdrp->f)))
67 1.1 itojun BAD;
68 1.1 itojun
69 1.1 itojun out:
70 1.1 itojun return (rv);
71 1.1 itojun }
72 1.1 itojun
73 1.1 itojun int
74 1.1 itojun findoff_coff(mappedfile, mappedsize, vmaddr, fileoffp)
75 1.1 itojun const char *mappedfile;
76 1.1 itojun size_t mappedsize, *fileoffp;
77 1.1 itojun u_long vmaddr;
78 1.1 itojun {
79 1.1 itojun struct coff_exechdr *exechdrp;
80 1.1 itojun int rv;
81 1.1 itojun
82 1.1 itojun rv = 0;
83 1.1 itojun exechdrp = (struct coff_exechdr *)&mappedfile[0];
84 1.1 itojun
85 1.1 itojun #define COFF_TXTOFF_XXX(fp, ap) \
86 1.1 itojun (COFF_ROUND(COFF_HDR_SIZE + (fp)->f_nscns * \
87 1.1 itojun sizeof(struct coff_scnhdr), \
88 1.1 itojun COFF_SEGMENT_ALIGNMENT(fp, ap)))
89 1.1 itojun
90 1.1 itojun #define COFF_DATOFF_XXX(fp, ap) \
91 1.1 itojun (COFF_TXTOFF_XXX(fp, ap) + (ap)->a_tsize)
92 1.1 itojun
93 1.1 itojun if (exechdrp->a.a_tstart <= vmaddr &&
94 1.1 itojun vmaddr < (exechdrp->a.a_tstart + exechdrp->a.a_tsize))
95 1.1 itojun *fileoffp = vmaddr - exechdrp->a.a_tstart +
96 1.1 itojun COFF_TXTOFF(&exechdrp->f, &(exechdrp->a));
97 1.1 itojun else if (exechdrp->a.a_dstart <= vmaddr &&
98 1.1 itojun vmaddr < (exechdrp->a.a_dstart + exechdrp->a.a_dsize))
99 1.1 itojun *fileoffp = vmaddr - exechdrp->a.a_dstart +
100 1.1 itojun COFF_DATOFF_XXX(&exechdrp->f, &(exechdrp->a));
101 1.1 itojun else
102 1.1 itojun BAD;
103 1.1 itojun
104 1.1 itojun out:
105 1.1 itojun return (rv);
106 1.1 itojun }
107 1.1 itojun
108 1.1 itojun #endif /* NLIST_COFF */
109