1 1.7 christos /* $NetBSD: exec_coff.c,v 1.7 2016/09/21 16:27:55 christos 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.5 cgd * 3. The name of the author may not be used to endorse or promote products 16 1.4 cgd * derived from this software without specific prior written permission. 17 1.4 cgd * 18 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 1.1 itojun * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 1.1 itojun * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 1.1 itojun * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 1.1 itojun * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 1.1 itojun * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 1.1 itojun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 1.1 itojun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 1.1 itojun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 1.1 itojun * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 1.4 cgd * 29 1.5 cgd * <<Id: LICENSE_GC,v 1.1 2001/10/01 23:24:05 cgd Exp>> 30 1.1 itojun */ 31 1.1 itojun 32 1.1 itojun #include <sys/cdefs.h> 33 1.1 itojun #ifndef lint 34 1.7 christos __RCSID("$NetBSD: exec_coff.c,v 1.7 2016/09/21 16:27:55 christos Exp $"); 35 1.1 itojun #endif /* not lint */ 36 1.1 itojun 37 1.1 itojun #include <sys/param.h> 38 1.1 itojun #include <sys/time.h> 39 1.1 itojun #include <sys/proc.h> 40 1.1 itojun #include <sys/types.h> 41 1.1 itojun #include <stdio.h> 42 1.1 itojun #include <string.h> 43 1.1 itojun #include "extern.h" 44 1.1 itojun 45 1.1 itojun #if defined(NLIST_COFF) 46 1.2 msaitoh #include <sys/exec_coff.h> 47 1.1 itojun 48 1.1 itojun #define check(off, size) ((off < 0) || (off + size > mappedsize)) 49 1.1 itojun #define BAD do { rv = -1; goto out; } while (0) 50 1.1 itojun 51 1.1 itojun int 52 1.7 christos check_coff(const char *mappedfile, size_t mappedsize) 53 1.1 itojun { 54 1.6 tsutsui const struct coff_exechdr *exechdrp; 55 1.1 itojun int rv; 56 1.1 itojun 57 1.1 itojun rv = 0; 58 1.1 itojun 59 1.1 itojun if (check(0, sizeof *exechdrp)) 60 1.1 itojun BAD; 61 1.6 tsutsui exechdrp = (const struct coff_exechdr *)&mappedfile[0]; 62 1.1 itojun 63 1.1 itojun if (COFF_BADMAG(&(exechdrp->f))) 64 1.1 itojun BAD; 65 1.1 itojun 66 1.1 itojun out: 67 1.1 itojun return (rv); 68 1.1 itojun } 69 1.1 itojun 70 1.1 itojun int 71 1.7 christos findoff_coff(const char *mappedfile, size_t mappedsize, u_long vmaddr, 72 1.7 christos size_t *fileoffp, u_long text_addr) 73 1.1 itojun { 74 1.6 tsutsui const struct coff_exechdr *exechdrp; 75 1.1 itojun int rv; 76 1.1 itojun 77 1.1 itojun rv = 0; 78 1.6 tsutsui exechdrp = (const struct coff_exechdr *)&mappedfile[0]; 79 1.1 itojun 80 1.1 itojun #define COFF_TXTOFF_XXX(fp, ap) \ 81 1.1 itojun (COFF_ROUND(COFF_HDR_SIZE + (fp)->f_nscns * \ 82 1.1 itojun sizeof(struct coff_scnhdr), \ 83 1.1 itojun COFF_SEGMENT_ALIGNMENT(fp, ap))) 84 1.1 itojun 85 1.1 itojun #define COFF_DATOFF_XXX(fp, ap) \ 86 1.1 itojun (COFF_TXTOFF_XXX(fp, ap) + (ap)->a_tsize) 87 1.1 itojun 88 1.6 tsutsui if ((u_long)exechdrp->a.a_tstart <= vmaddr && 89 1.6 tsutsui vmaddr < (u_long)(exechdrp->a.a_tstart + exechdrp->a.a_tsize)) 90 1.1 itojun *fileoffp = vmaddr - exechdrp->a.a_tstart + 91 1.1 itojun COFF_TXTOFF(&exechdrp->f, &(exechdrp->a)); 92 1.6 tsutsui else if ((u_long)exechdrp->a.a_dstart <= vmaddr && 93 1.6 tsutsui vmaddr < (u_long)(exechdrp->a.a_dstart + exechdrp->a.a_dsize)) 94 1.1 itojun *fileoffp = vmaddr - exechdrp->a.a_dstart + 95 1.1 itojun COFF_DATOFF_XXX(&exechdrp->f, &(exechdrp->a)); 96 1.1 itojun else 97 1.1 itojun BAD; 98 1.1 itojun 99 1.1 itojun out: 100 1.1 itojun return (rv); 101 1.1 itojun } 102 1.1 itojun 103 1.1 itojun #endif /* NLIST_COFF */ 104