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