Home | History | Annotate | Line # | Download | only in mdsetimage
exec_coff.c revision 1.6
      1  1.6  tsutsui /* $NetBSD: exec_coff.c,v 1.6 2009/07/30 15:16:37 tsutsui 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.6  tsutsui __RCSID("$NetBSD: exec_coff.c,v 1.6 2009/07/30 15:16:37 tsutsui 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.1   itojun check_coff(mappedfile, mappedsize)
     53  1.1   itojun 	const char *mappedfile;
     54  1.1   itojun 	size_t mappedsize;
     55  1.1   itojun {
     56  1.6  tsutsui 	const struct coff_exechdr *exechdrp;
     57  1.1   itojun 	int rv;
     58  1.1   itojun 
     59  1.1   itojun 	rv = 0;
     60  1.1   itojun 
     61  1.1   itojun 	if (check(0, sizeof *exechdrp))
     62  1.1   itojun 		BAD;
     63  1.6  tsutsui 	exechdrp = (const struct coff_exechdr *)&mappedfile[0];
     64  1.1   itojun 
     65  1.1   itojun 	if (COFF_BADMAG(&(exechdrp->f)))
     66  1.1   itojun 		BAD;
     67  1.1   itojun 
     68  1.1   itojun out:
     69  1.1   itojun 	return (rv);
     70  1.1   itojun }
     71  1.1   itojun 
     72  1.1   itojun int
     73  1.1   itojun findoff_coff(mappedfile, mappedsize, vmaddr, fileoffp)
     74  1.1   itojun 	const char *mappedfile;
     75  1.1   itojun 	size_t mappedsize, *fileoffp;
     76  1.1   itojun 	u_long vmaddr;
     77  1.1   itojun {
     78  1.6  tsutsui 	const struct coff_exechdr *exechdrp;
     79  1.1   itojun 	int rv;
     80  1.1   itojun 
     81  1.1   itojun 	rv = 0;
     82  1.6  tsutsui 	exechdrp = (const struct coff_exechdr *)&mappedfile[0];
     83  1.1   itojun 
     84  1.1   itojun #define COFF_TXTOFF_XXX(fp, ap) \
     85  1.1   itojun          (COFF_ROUND(COFF_HDR_SIZE + (fp)->f_nscns * \
     86  1.1   itojun 		    sizeof(struct coff_scnhdr), \
     87  1.1   itojun 		     COFF_SEGMENT_ALIGNMENT(fp, ap)))
     88  1.1   itojun 
     89  1.1   itojun #define COFF_DATOFF_XXX(fp, ap) \
     90  1.1   itojun         (COFF_TXTOFF_XXX(fp, ap) + (ap)->a_tsize)
     91  1.1   itojun 
     92  1.6  tsutsui 	if ((u_long)exechdrp->a.a_tstart <= vmaddr &&
     93  1.6  tsutsui 	    vmaddr < (u_long)(exechdrp->a.a_tstart + exechdrp->a.a_tsize))
     94  1.1   itojun 		*fileoffp = vmaddr - exechdrp->a.a_tstart +
     95  1.1   itojun 		    COFF_TXTOFF(&exechdrp->f, &(exechdrp->a));
     96  1.6  tsutsui 	else if ((u_long)exechdrp->a.a_dstart <= vmaddr &&
     97  1.6  tsutsui             vmaddr < (u_long)(exechdrp->a.a_dstart + exechdrp->a.a_dsize))
     98  1.1   itojun 		*fileoffp = vmaddr - exechdrp->a.a_dstart +
     99  1.1   itojun 		    COFF_DATOFF_XXX(&exechdrp->f, &(exechdrp->a));
    100  1.1   itojun 	else
    101  1.1   itojun 		BAD;
    102  1.1   itojun 
    103  1.1   itojun out:
    104  1.1   itojun 	return (rv);
    105  1.1   itojun }
    106  1.1   itojun 
    107  1.1   itojun #endif /* NLIST_COFF */
    108