Home | History | Annotate | Line # | Download | only in libtos
elf.c revision 1.2
      1 /*	$NetBSD: elf.c,v 1.2 2001/10/11 07:07:42 leo Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Leo Weppelman.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <stdio.h>
     40 #include <unistd.h>
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <sys/types.h>
     44 
     45 #ifdef TOSTOOLS
     46 #include "exec_elf.h"
     47 #else
     48 #include <sys/exec_elf.h>
     49 #endif
     50 #include "tosdefs.h"
     51 #include "kparamb.h"
     52 #include "libtos.h"
     53 #include "cread.h"
     54 
     55 /*
     56  * Load an elf image.
     57  * Exit codes:
     58  *	-1      : Not an ELF file.
     59  *	 0      : OK
     60  *	 error# : Error during load (*errp might contain error string).
     61  */
     62 #define ELFMAGIC	((ELFMAG0 << 24) | (ELFMAG1 << 16) | \
     63 				(ELFMAG2 << 8) | ELFMAG3)
     64 
     65 int
     66 elf_load(fd, od, errp, loadsyms)
     67 int	fd;
     68 osdsc_t	*od;
     69 char	**errp;
     70 int	loadsyms;
     71 {
     72 	int		i,j;
     73 	int		err;
     74 	Elf32_Ehdr	ehdr;
     75 	Elf32_Phdr	*phdrs;
     76 	Elf32_Word	symsize, symstart;
     77 	long		kernsize;
     78 
     79 	*errp = NULL;
     80 	lseek(fd, (off_t)0, SEEK_SET);
     81 	if (read(fd, (char *)&ehdr, sizeof(ehdr)) != sizeof(ehdr))
     82 		return -1;
     83 
     84 	if (*((u_int *)ehdr.e_ident) != ELFMAGIC)
     85 		return -1;
     86 
     87 	/*
     88 	 * calculate highest used address
     89 	 */
     90 	i = ehdr.e_phnum * sizeof(Elf32_Phdr);
     91 	err = 1;
     92 	if ((phdrs = (Elf32_Phdr *)malloc(i)) == NULL)
     93 		goto error;
     94 	err = 2;
     95 	if (read(fd, phdrs, i) != i)
     96 		goto error;
     97 
     98 	kernsize = 0;
     99 	for (i = 0; i < ehdr.e_phnum; i++) {
    100 		Elf32_Word sum;
    101 
    102 		sum = phdrs[i].p_vaddr + phdrs[i].p_memsz;
    103 		if ((phdrs[i].p_flags & (PF_W|PF_X)) && (sum > kernsize))
    104 			kernsize = sum;
    105 	}
    106 
    107 	/*
    108 	 * look for symbols and calculate the size
    109 	 * XXX: This increases the load time by a factor 2 for gzipped
    110 	 *      images!
    111 	 */
    112 	symsize  = 0;
    113 	symstart = 0;
    114 	if (loadsyms) {
    115 	    i = ehdr.e_shnum + 1;
    116 	    err = 3;
    117 	    if (lseek(fd, (off_t)ehdr.e_shoff, SEEK_SET) != ehdr.e_shoff)
    118 		goto error;
    119 	    while (--i) {
    120 	      Elf32_Shdr shdr;
    121 
    122 	      err = 4;
    123 	      if (read(fd, &shdr, sizeof(shdr)) != sizeof(shdr))
    124 		goto error;
    125 	      if ((shdr.sh_type == SHT_SYMTAB) || (shdr.sh_type == SHT_STRTAB))
    126 		symsize += shdr.sh_size;
    127 	    }
    128 	}
    129 
    130 	if (symsize) {
    131 	  symstart = kernsize;
    132 	  kernsize += symsize + sizeof(ehdr) + ehdr.e_shnum*sizeof(Elf32_Shdr);
    133 	}
    134 
    135 	/*
    136 	 * Extract various sizes from the kernel executable
    137 	 */
    138 	od->k_esym = symsize ? kernsize : 0;
    139 	od->ksize  = kernsize;
    140 	od->kentry = ehdr.e_entry;
    141 
    142 	err = 5;
    143 	if ((od->kstart = (u_char *)malloc(od->ksize)) == NULL)
    144 		goto error;
    145 
    146 	/*
    147 	 * Read text & data, clear bss
    148 	 */
    149 	for (i = 0; i < ehdr.e_phnum; i++) {
    150 	    u_char	*p;
    151 	    Elf32_Phdr	*php = &phdrs[i];
    152 
    153 	    if (php->p_flags & (PF_W|PF_X)) {
    154 		err = 6;
    155 		if (lseek(fd, (off_t)php->p_offset, SEEK_SET) != php->p_offset)
    156 		    goto error;
    157 		p = (u_char *)(od->kstart) + php->p_vaddr;
    158 		err = 7;
    159 		if (read(fd, p, php->p_filesz) != php->p_filesz)
    160 		    goto error;
    161 		if (php->p_memsz > php->p_filesz)
    162 		    memset(p + php->p_filesz, 0, php->p_memsz - php->p_filesz);
    163 	    }
    164 	}
    165 
    166 	/*
    167 	 * Read symbols and strings
    168 	 */
    169 	if (symsize) {
    170 	    u_char	*p, *symtab;
    171 	    int		nhdrs;
    172 	    Elf32_Shdr	*shp;
    173 
    174 	    symtab = od->kstart + symstart;
    175 
    176 	    p = symtab + sizeof(ehdr);
    177 	    nhdrs = ehdr.e_shnum;
    178 	    err = 8;
    179 	    if (lseek(fd, (off_t)ehdr.e_shoff, SEEK_SET) != ehdr.e_shoff)
    180 		goto error;
    181 	    err = 9;
    182 	    if (read(fd, p, nhdrs * sizeof(*shp)) != nhdrs * sizeof(*shp))
    183 		goto error;
    184 	    shp = (Elf32_Shdr*)p;
    185 	    p  += nhdrs * sizeof(*shp);
    186 	    for (i = 0; i < nhdrs; i++) {
    187 		if (shp[i].sh_type == SHT_SYMTAB) {
    188 		    if (shp[i].sh_offset == 0)
    189 			continue;
    190 		    /* Got the symbol table. */
    191 		    err = 10;
    192 		    if (lseek(fd, (off_t)shp[i].sh_offset, SEEK_SET) !=
    193 							shp[i].sh_offset)
    194 		    	goto error;
    195 		    err = 11;
    196 		    if (read(fd, p, shp[i].sh_size) != shp[i].sh_size)
    197 			goto error;
    198 		    shp[i].sh_offset = p - symtab;
    199 		    /* Find the string table to go with it. */
    200 		    j = shp[i].sh_link;
    201 		    if (shp[j].sh_offset == 0)
    202 			continue;
    203 		    p += shp[i].sh_size;
    204 		    err = 12;
    205 		    if (lseek(fd, (off_t)shp[j].sh_offset, SEEK_SET) !=
    206 				    			shp[j].sh_offset)
    207 		    	goto error;
    208 		    err = 13;
    209 		    if (read(fd, p, shp[j].sh_size) != shp[j].sh_size)
    210 			goto error;
    211 		    shp[j].sh_offset = p - symtab;
    212 		    /* There should only be one symbol table. */
    213 		    break;
    214 		}
    215 	    }
    216 	    ehdr.e_shoff = sizeof(ehdr);
    217 	    bcopy(&ehdr, symtab, sizeof(ehdr));
    218 	}
    219 	return 0;
    220 
    221 error:
    222 #ifdef TOSTOOLS
    223 	{
    224 		static char *errs[] = {
    225 			/*  1 */ "Cannot malloc Elf phdr storage space",
    226 			/*  2 */ "Cannot read Elf32_Phdrs",
    227 			/*  3 */ "Cannot seek to e_shoff location",
    228 			/*  4 */ "Cannot read Elf32_shdr",
    229 			/*  5 */ "Cannot malloc kernel image space",
    230 		    	/*  6 */ "Seek error while reading text segment\n",
    231 		    	/*  7 */ "Read error in text segment\n",
    232 			/*  8 */ "Error seeking to section headers",
    233 			/*  9 */ "Error reading section headers",
    234 		    	/* 10 */ "Error seeking to symbols",
    235 			/* 11 */ "Error reading symbols",
    236 		    	/* 12 */ "Error seeking to string table",
    237 			/* 13 */ "Error reading strings"
    238 		};
    239 		*errp = errs[err];
    240 	}
    241 #endif /* TOSTOOLS */
    242 
    243 	return err;
    244 }
    245