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