Home | History | Annotate | Line # | Download | only in mdsetimage
exec_elf32.c revision 1.6.2.1
      1  1.6.2.1  minoura /* $NetBSD: exec_elf32.c,v 1.6.2.1 2000/06/22 18:01:03 minoura Exp $ */
      2      1.1      cgd 
      3      1.1      cgd /*
      4  1.6.2.1  minoura  * Copyright (c) 1996 Christopher G. Demetriou
      5  1.6.2.1  minoura  * All rights reserved.
      6  1.6.2.1  minoura  *
      7      1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8      1.1      cgd  * modification, are permitted provided that the following conditions
      9      1.1      cgd  * are met:
     10      1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11      1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12      1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14      1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15      1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16      1.1      cgd  *    must display the following acknowledgement:
     17  1.6.2.1  minoura  *          This product includes software developed for the
     18  1.6.2.1  minoura  *          NetBSD Project.  See http://www.netbsd.org/ for
     19  1.6.2.1  minoura  *          information about NetBSD.
     20      1.1      cgd  * 4. The name of the author may not be used to endorse or promote products
     21  1.6.2.1  minoura  *    derived from this software without specific prior written permission.
     22  1.6.2.1  minoura  *
     23      1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24      1.1      cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25      1.1      cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26      1.1      cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27      1.1      cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28      1.1      cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29      1.1      cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30      1.1      cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31      1.1      cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32      1.1      cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  1.6.2.1  minoura  *
     34  1.6.2.1  minoura  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
     35      1.1      cgd  */
     36      1.1      cgd 
     37      1.2    lukem #include <sys/cdefs.h>
     38      1.1      cgd #ifndef lint
     39  1.6.2.1  minoura __RCSID("$NetBSD: exec_elf32.c,v 1.6.2.1 2000/06/22 18:01:03 minoura Exp $");
     40      1.1      cgd #endif /* not lint */
     41      1.1      cgd 
     42      1.1      cgd #ifndef ELFSIZE
     43      1.1      cgd #define	ELFSIZE		32
     44      1.1      cgd #endif
     45      1.1      cgd 
     46      1.1      cgd #include <sys/types.h>
     47      1.1      cgd #include <stdio.h>
     48      1.1      cgd #include <string.h>
     49      1.1      cgd #include "extern.h"
     50      1.1      cgd 
     51      1.1      cgd #if defined(NLIST_ELF32) || defined(NLIST_ELF64)
     52      1.1      cgd #include <sys/exec_elf.h>
     53      1.1      cgd #endif
     54      1.1      cgd 
     55      1.1      cgd #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
     56      1.1      cgd     (defined(NLIST_ELF64) && (ELFSIZE == 64))
     57      1.1      cgd 
     58      1.1      cgd #define	check(off, size)	((off < 0) || (off + size > mappedsize))
     59      1.1      cgd #define	BAD			do { rv = -1; goto out; } while (0)
     60      1.1      cgd 
     61      1.1      cgd int
     62      1.1      cgd ELFNAMEEND(check)(mappedfile, mappedsize)
     63      1.1      cgd 	const char *mappedfile;
     64      1.1      cgd 	size_t mappedsize;
     65      1.1      cgd {
     66      1.1      cgd 	Elf_Ehdr *ehdrp;
     67      1.1      cgd 	int rv;
     68      1.1      cgd 
     69      1.1      cgd 	rv = 0;
     70      1.1      cgd 
     71      1.1      cgd 	if (check(0, sizeof *ehdrp))
     72      1.1      cgd 		BAD;
     73      1.1      cgd 	ehdrp = (Elf_Ehdr *)&mappedfile[0];
     74      1.1      cgd 
     75      1.5   kleink 	if (memcmp(ehdrp->e_ident, ELFMAG, SELFMAG) != 0 ||
     76      1.5   kleink 	    ehdrp->e_ident[EI_CLASS] != ELFCLASS)
     77      1.1      cgd 		BAD;
     78      1.1      cgd 
     79      1.1      cgd 	switch (ehdrp->e_machine) {
     80      1.1      cgd 	ELFDEFNNAME(MACHDEP_ID_CASES)
     81      1.1      cgd 
     82      1.1      cgd 	default:
     83      1.1      cgd 		BAD;
     84      1.1      cgd 	}
     85      1.1      cgd 
     86      1.1      cgd out:
     87      1.1      cgd 	return (rv);
     88      1.1      cgd }
     89      1.1      cgd 
     90      1.1      cgd int
     91      1.1      cgd ELFNAMEEND(findoff)(mappedfile, mappedsize, vmaddr, fileoffp)
     92      1.1      cgd 	const char *mappedfile;
     93      1.1      cgd 	size_t mappedsize, *fileoffp;
     94      1.1      cgd 	u_long vmaddr;
     95      1.1      cgd {
     96      1.1      cgd 	Elf_Ehdr *ehdrp;
     97      1.1      cgd 	Elf_Shdr *shdrp;
     98      1.1      cgd 	Elf_Off shdr_off;
     99      1.1      cgd 	Elf_Word shdr_size;
    100      1.1      cgd #if (ELFSIZE == 32)
    101      1.1      cgd 	Elf32_Half nshdr, i;
    102      1.1      cgd #elif (ELFSIZE == 64)
    103      1.1      cgd 	Elf64_Half nshdr, i;
    104      1.1      cgd #endif
    105      1.1      cgd 	int rv;
    106      1.1      cgd 
    107      1.1      cgd 	rv = 0;
    108      1.1      cgd 
    109      1.1      cgd 	ehdrp = (Elf_Ehdr *)&mappedfile[0];
    110      1.1      cgd 	nshdr = ehdrp->e_shnum;
    111      1.1      cgd 	shdr_off = ehdrp->e_shoff;
    112      1.1      cgd 	shdr_size = ehdrp->e_shentsize * nshdr;
    113      1.1      cgd 
    114      1.1      cgd 	if (check(shdr_off, shdr_size) ||
    115      1.1      cgd 	    (sizeof *shdrp != ehdrp->e_shentsize))
    116      1.1      cgd 		BAD;
    117      1.1      cgd 	shdrp = (Elf_Shdr *)&mappedfile[shdr_off];
    118      1.1      cgd 
    119      1.1      cgd 	for (i = 0; i < nshdr; i++) {
    120      1.1      cgd 		if (shdrp[i].sh_addr <= vmaddr &&
    121      1.1      cgd 		    vmaddr < (shdrp[i].sh_addr + shdrp[i].sh_size)) {
    122      1.1      cgd 			*fileoffp = vmaddr -
    123      1.1      cgd 			    shdrp[i].sh_addr + shdrp[i].sh_offset;
    124      1.1      cgd 			break;
    125      1.1      cgd 		}
    126      1.1      cgd 	}
    127      1.1      cgd 	if (i == nshdr)
    128      1.1      cgd 		BAD;
    129      1.1      cgd 
    130      1.1      cgd out:
    131      1.1      cgd 	return (rv);
    132      1.1      cgd }
    133      1.1      cgd 
    134      1.1      cgd #endif /* include this size of ELF */
    135